Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e80e82bb authored by Yan Yan's avatar Yan Yan Committed by Automerger Merge Worker
Browse files

Convert TunnelConnectionParams to/from PersistableBundle am: c0b7c4fa

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1653048

Change-Id: I71e0b9735af25bb3d2f8de17df1dac3df0e10106
parents 586eca54 c0b7c4fa
Loading
Loading
Loading
Loading
+106 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.net.vcn.persistablebundleutils;

import android.annotation.NonNull;
import android.net.TunnelConnectionParams;
import android.net.ipsec.ike.IkeSessionParams;
import android.net.ipsec.ike.IkeTunnelConnectionParams;
import android.net.ipsec.ike.TunnelModeChildSessionParams;
import android.os.PersistableBundle;

import java.util.Objects;

/**
 * Utility class to convert TunnelConnectionParams to/from PersistableBundle
 *
 * @hide
 */
public final class TunnelConnectionParamsUtils {
    private static final int EXPECTED_BUNDLE_KEY_CNT = 1;

    private static final String PARAMS_TYPE_IKE = "IKE";

    /** Serializes an TunnelConnectionParams to a PersistableBundle. */
    @NonNull
    public static PersistableBundle toPersistableBundle(@NonNull TunnelConnectionParams params) {
        final PersistableBundle result = new PersistableBundle();

        if (params instanceof IkeTunnelConnectionParams) {
            result.putPersistableBundle(
                    PARAMS_TYPE_IKE,
                    IkeTunnelConnectionParamsUtils.serializeIkeParams(
                            (IkeTunnelConnectionParams) params));
            return result;
        } else {
            throw new UnsupportedOperationException("Invalid TunnelConnectionParams type");
        }
    }

    /** Constructs an TunnelConnectionParams by deserializing a PersistableBundle. */
    @NonNull
    public static TunnelConnectionParams fromPersistableBundle(@NonNull PersistableBundle in) {
        Objects.requireNonNull(in, "PersistableBundle was null");

        if (in.keySet().size() != EXPECTED_BUNDLE_KEY_CNT) {
            throw new IllegalArgumentException(
                    "Expect PersistableBundle to have one element but found: " + in.keySet());
        }

        if (in.get(PARAMS_TYPE_IKE) != null) {
            return IkeTunnelConnectionParamsUtils.deserializeIkeParams(
                    in.getPersistableBundle(PARAMS_TYPE_IKE));
        }

        throw new IllegalArgumentException(
                "Invalid TunnelConnectionParams type " + in.keySet().iterator().next());
    }

    private static final class IkeTunnelConnectionParamsUtils {
        private static final String IKE_PARAMS_KEY = "IKE_PARAMS_KEY";
        private static final String CHILD_PARAMS_KEY = "CHILD_PARAMS_KEY";

        @NonNull
        public static PersistableBundle serializeIkeParams(
                @NonNull IkeTunnelConnectionParams ikeParams) {
            final PersistableBundle result = new PersistableBundle();

            result.putPersistableBundle(
                    IKE_PARAMS_KEY,
                    IkeSessionParamsUtils.toPersistableBundle(ikeParams.getIkeSessionParams()));
            result.putPersistableBundle(
                    CHILD_PARAMS_KEY,
                    TunnelModeChildSessionParamsUtils.toPersistableBundle(
                            ikeParams.getTunnelModeChildSessionParams()));
            return result;
        }

        @NonNull
        public static IkeTunnelConnectionParams deserializeIkeParams(
                @NonNull PersistableBundle in) {
            final PersistableBundle ikeBundle = in.getPersistableBundle(IKE_PARAMS_KEY);
            final PersistableBundle childBundle = in.getPersistableBundle(CHILD_PARAMS_KEY);
            Objects.requireNonNull(ikeBundle, "IkeSessionParams was null");
            Objects.requireNonNull(ikeBundle, "TunnelModeChildSessionParams was null");

            final IkeSessionParams ikeParams =
                    IkeSessionParamsUtils.fromPersistableBundle(ikeBundle);
            final TunnelModeChildSessionParams childParams =
                    TunnelModeChildSessionParamsUtils.fromPersistableBundle(childBundle);
            return new IkeTunnelConnectionParams(ikeParams, childParams);
        }
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -52,7 +52,8 @@ import java.util.concurrent.TimeUnit;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class IkeSessionParamsUtilsTest {
    private static IkeSessionParams.Builder createBuilderMinimum() {
    // Package private for use in EncryptedTunnelParamsUtilsTest
    static IkeSessionParams.Builder createBuilderMinimum() {
        final InetAddress serverAddress = InetAddresses.parseNumericAddress("192.0.2.100");

        // TODO: b/185941731 Make sure all valid IKE_OPTIONS are added and validated.
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.vcn.persistablebundleutils;

import static org.junit.Assert.assertEquals;

import android.net.ipsec.ike.IkeTunnelConnectionParams;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class TunnelConnectionParamsUtilsTest {
    private static IkeTunnelConnectionParams buildTestParams() {
        return new IkeTunnelConnectionParams(
                IkeSessionParamsUtilsTest.createBuilderMinimum().build(),
                TunnelModeChildSessionParamsUtilsTest.createBuilderMinimum().build());
    }

    @Test
    public void testIkeTunnelConnectionParamsToFromPersistableBundle() {
        final IkeTunnelConnectionParams params = buildTestParams();

        assertEquals(
                params,
                TunnelConnectionParamsUtils.fromPersistableBundle(
                        TunnelConnectionParamsUtils.toPersistableBundle(params)));
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -40,7 +40,8 @@ import java.util.concurrent.TimeUnit;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class TunnelModeChildSessionParamsUtilsTest {
    private TunnelModeChildSessionParams.Builder createBuilderMinimum() {
    // Package private for use in EncryptedTunnelParamsUtilsTest
    static TunnelModeChildSessionParams.Builder createBuilderMinimum() {
        final ChildSaProposal saProposal = SaProposalUtilsTest.buildTestChildSaProposal();
        return new TunnelModeChildSessionParams.Builder().addSaProposal(saProposal);
    }