Convert TunnelConnectionParams to/from PersistableBundle

Add utility class to convert TunnelConnectionParams to and from
PersistableBundle.

Bug: 180664474
Test: atest EncryptedTunnelParamsUtilsTest
Change-Id: I93ae068eb6d1e1c4d3fcbaccbaae867db8d07a38
This commit is contained in:
Yan Yan
2021-03-24 11:20:31 -07:00
parent 83956e2a5d
commit c0b7c4fa5a
4 changed files with 157 additions and 2 deletions

View File

@@ -0,0 +1,106 @@
/*
* 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);
}
}
}

View File

@@ -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.

View File

@@ -0,0 +1,47 @@
/*
* 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)));
}
}

View File

@@ -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);
}