Merge "Remove Ikev2VpnProfile related shims usage" into main

This commit is contained in:
Chiachang Wang
2023-10-03 07:45:41 +00:00
committed by Gerrit Code Review

View File

@@ -22,6 +22,7 @@ import static android.net.NetworkCapabilities.TRANSPORT_VPN;
import static android.net.cts.util.CtsNetUtils.TestNetworkCallback; import static android.net.cts.util.CtsNetUtils.TestNetworkCallback;
import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity; import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
import static com.android.modules.utils.build.SdkLevel.isAtLeastU;
import static com.android.modules.utils.build.SdkLevel.isAtLeastT; import static com.android.modules.utils.build.SdkLevel.isAtLeastT;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2; import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
@@ -60,11 +61,7 @@ import androidx.test.InstrumentationRegistry;
import com.android.internal.util.HexDump; import com.android.internal.util.HexDump;
import com.android.networkstack.apishim.ConstantsShim; import com.android.networkstack.apishim.ConstantsShim;
import com.android.networkstack.apishim.Ikev2VpnProfileBuilderShimImpl;
import com.android.networkstack.apishim.Ikev2VpnProfileShimImpl;
import com.android.networkstack.apishim.VpnManagerShimImpl; import com.android.networkstack.apishim.VpnManagerShimImpl;
import com.android.networkstack.apishim.common.Ikev2VpnProfileBuilderShim;
import com.android.networkstack.apishim.common.Ikev2VpnProfileShim;
import com.android.networkstack.apishim.common.VpnManagerShim; import com.android.networkstack.apishim.common.VpnManagerShim;
import com.android.networkstack.apishim.common.VpnProfileStateShim; import com.android.networkstack.apishim.common.VpnProfileStateShim;
import com.android.testutils.DevSdkIgnoreRule; import com.android.testutils.DevSdkIgnoreRule;
@@ -227,28 +224,25 @@ public class Ikev2VpnTest {
} }
private Ikev2VpnProfile buildIkev2VpnProfileCommon( private Ikev2VpnProfile buildIkev2VpnProfileCommon(
@NonNull Ikev2VpnProfileBuilderShim builderShim, boolean isRestrictedToTestNetworks, @NonNull Ikev2VpnProfile.Builder builder, boolean isRestrictedToTestNetworks,
boolean requiresValidation, boolean automaticIpVersionSelectionEnabled, boolean requiresValidation, boolean automaticIpVersionSelectionEnabled,
boolean automaticNattKeepaliveTimerEnabled) throws Exception { boolean automaticNattKeepaliveTimerEnabled) throws Exception {
builderShim.setBypassable(true) builder.setBypassable(true)
.setAllowedAlgorithms(TEST_ALLOWED_ALGORITHMS) .setAllowedAlgorithms(TEST_ALLOWED_ALGORITHMS)
.setProxy(TEST_PROXY_INFO) .setProxy(TEST_PROXY_INFO)
.setMaxMtu(TEST_MTU) .setMaxMtu(TEST_MTU)
.setMetered(false); .setMetered(false);
if (TestUtils.shouldTestTApis()) { if (isAtLeastT()) {
builderShim.setRequiresInternetValidation(requiresValidation); builder.setRequiresInternetValidation(requiresValidation);
} }
if (TestUtils.shouldTestUApis()) { if (isAtLeastU()) {
builderShim.setAutomaticIpVersionSelectionEnabled(automaticIpVersionSelectionEnabled); builder.setAutomaticIpVersionSelectionEnabled(automaticIpVersionSelectionEnabled);
builderShim.setAutomaticNattKeepaliveTimerEnabled(automaticNattKeepaliveTimerEnabled); builder.setAutomaticNattKeepaliveTimerEnabled(automaticNattKeepaliveTimerEnabled);
} }
// Convert shim back to Ikev2VpnProfile.Builder since restrictToTestNetworks is a hidden
// method and is not defined in shims.
// TODO: replace it in alternative way to remove the hidden method usage // TODO: replace it in alternative way to remove the hidden method usage
final Ikev2VpnProfile.Builder builder = (Ikev2VpnProfile.Builder) builderShim.getBuilder();
if (isRestrictedToTestNetworks) { if (isRestrictedToTestNetworks) {
builder.restrictToTestNetworks(); builder.restrictToTestNetworks();
} }
@@ -264,16 +258,14 @@ public class Ikev2VpnTest {
? IkeSessionTestUtils.IKE_PARAMS_V6 : IkeSessionTestUtils.IKE_PARAMS_V4, ? IkeSessionTestUtils.IKE_PARAMS_V6 : IkeSessionTestUtils.IKE_PARAMS_V4,
IkeSessionTestUtils.CHILD_PARAMS); IkeSessionTestUtils.CHILD_PARAMS);
final Ikev2VpnProfileBuilderShim builderShim = final Ikev2VpnProfile.Builder builder =
Ikev2VpnProfileBuilderShimImpl.newInstance(params) new Ikev2VpnProfile.Builder(params)
.setRequiresInternetValidation(requiresValidation) .setRequiresInternetValidation(requiresValidation)
.setProxy(TEST_PROXY_INFO) .setProxy(TEST_PROXY_INFO)
.setMaxMtu(TEST_MTU) .setMaxMtu(TEST_MTU)
.setMetered(false); .setMetered(false);
// Convert shim back to Ikev2VpnProfile.Builder since restrictToTestNetworks is a hidden
// method and is not defined in shims.
// TODO: replace it in alternative way to remove the hidden method usage // TODO: replace it in alternative way to remove the hidden method usage
final Ikev2VpnProfile.Builder builder = (Ikev2VpnProfile.Builder) builderShim.getBuilder();
if (isRestrictedToTestNetworks) { if (isRestrictedToTestNetworks) {
builder.restrictToTestNetworks(); builder.restrictToTestNetworks();
} }
@@ -283,8 +275,8 @@ public class Ikev2VpnTest {
private Ikev2VpnProfile buildIkev2VpnProfilePsk(@NonNull String remote, private Ikev2VpnProfile buildIkev2VpnProfilePsk(@NonNull String remote,
boolean isRestrictedToTestNetworks, boolean requiresValidation) boolean isRestrictedToTestNetworks, boolean requiresValidation)
throws Exception { throws Exception {
final Ikev2VpnProfileBuilderShim builder = final Ikev2VpnProfile.Builder builder =
Ikev2VpnProfileBuilderShimImpl.newInstance(remote, TEST_IDENTITY) new Ikev2VpnProfile.Builder(remote, TEST_IDENTITY)
.setAuthPsk(TEST_PSK); .setAuthPsk(TEST_PSK);
return buildIkev2VpnProfileCommon(builder, isRestrictedToTestNetworks, return buildIkev2VpnProfileCommon(builder, isRestrictedToTestNetworks,
requiresValidation, false /* automaticIpVersionSelectionEnabled */, requiresValidation, false /* automaticIpVersionSelectionEnabled */,
@@ -293,8 +285,8 @@ public class Ikev2VpnTest {
private Ikev2VpnProfile buildIkev2VpnProfileUsernamePassword(boolean isRestrictedToTestNetworks) private Ikev2VpnProfile buildIkev2VpnProfileUsernamePassword(boolean isRestrictedToTestNetworks)
throws Exception { throws Exception {
final Ikev2VpnProfileBuilderShim builder = final Ikev2VpnProfile.Builder builder =
Ikev2VpnProfileBuilderShimImpl.newInstance(TEST_SERVER_ADDR_V6, TEST_IDENTITY) new Ikev2VpnProfile.Builder(TEST_SERVER_ADDR_V6, TEST_IDENTITY)
.setAuthUsernamePassword(TEST_USER, TEST_PASSWORD, mServerRootCa); .setAuthUsernamePassword(TEST_USER, TEST_PASSWORD, mServerRootCa);
return buildIkev2VpnProfileCommon(builder, isRestrictedToTestNetworks, return buildIkev2VpnProfileCommon(builder, isRestrictedToTestNetworks,
false /* requiresValidation */, false /* automaticIpVersionSelectionEnabled */, false /* requiresValidation */, false /* automaticIpVersionSelectionEnabled */,
@@ -303,8 +295,8 @@ public class Ikev2VpnTest {
private Ikev2VpnProfile buildIkev2VpnProfileDigitalSignature(boolean isRestrictedToTestNetworks) private Ikev2VpnProfile buildIkev2VpnProfileDigitalSignature(boolean isRestrictedToTestNetworks)
throws Exception { throws Exception {
final Ikev2VpnProfileBuilderShim builder = final Ikev2VpnProfile.Builder builder =
Ikev2VpnProfileBuilderShimImpl.newInstance(TEST_SERVER_ADDR_V6, TEST_IDENTITY) new Ikev2VpnProfile.Builder(TEST_SERVER_ADDR_V6, TEST_IDENTITY)
.setAuthDigitalSignature( .setAuthDigitalSignature(
mUserCertKey.cert, mUserCertKey.key, mServerRootCa); mUserCertKey.cert, mUserCertKey.key, mServerRootCa);
return buildIkev2VpnProfileCommon(builder, isRestrictedToTestNetworks, return buildIkev2VpnProfileCommon(builder, isRestrictedToTestNetworks,
@@ -347,7 +339,6 @@ public class Ikev2VpnTest {
@Test @Test
public void testBuildIkev2VpnProfileWithIkeTunnelConnectionParams() throws Exception { public void testBuildIkev2VpnProfileWithIkeTunnelConnectionParams() throws Exception {
assumeTrue(mCtsNetUtils.hasIpsecTunnelsFeature()); assumeTrue(mCtsNetUtils.hasIpsecTunnelsFeature());
assumeTrue(TestUtils.shouldTestTApis());
final IkeTunnelConnectionParams expectedParams = new IkeTunnelConnectionParams( final IkeTunnelConnectionParams expectedParams = new IkeTunnelConnectionParams(
IkeSessionTestUtils.IKE_PARAMS_V6, IkeSessionTestUtils.CHILD_PARAMS); IkeSessionTestUtils.IKE_PARAMS_V6, IkeSessionTestUtils.CHILD_PARAMS);
@@ -567,7 +558,7 @@ public class Ikev2VpnTest {
// regardless of its value. However, there is a race in Vpn(see b/228574221) that VPN may // regardless of its value. However, there is a race in Vpn(see b/228574221) that VPN may
// misuse VPN network itself as the underlying network. The fix is not available without // misuse VPN network itself as the underlying network. The fix is not available without
// SDK > T platform. Thus, verify this only on T+ platform. // SDK > T platform. Thus, verify this only on T+ platform.
if (!requiresValidation && TestUtils.shouldTestTApis()) { if (!requiresValidation && isAtLeastT()) {
cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, TIMEOUT_MS, cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, TIMEOUT_MS,
entry -> ((CallbackEntry.CapabilitiesChanged) entry).getCaps() entry -> ((CallbackEntry.CapabilitiesChanged) entry).getCaps()
.hasCapability(NET_CAPABILITY_VALIDATED)); .hasCapability(NET_CAPABILITY_VALIDATED));
@@ -647,7 +638,6 @@ public class Ikev2VpnTest {
@Test @IgnoreUpTo(SC_V2) @Test @IgnoreUpTo(SC_V2)
public void testStartStopVpnProfileV4WithValidation() throws Exception { public void testStartStopVpnProfileV4WithValidation() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(false /* testIpv6Only */, true /* requiresValidation */, doTestStartStopVpnProfile(false /* testIpv6Only */, true /* requiresValidation */,
false /* testSessionKey */, false /* testIkeTunConnParams */); false /* testSessionKey */, false /* testIkeTunConnParams */);
} }
@@ -660,35 +650,30 @@ public class Ikev2VpnTest {
@Test @IgnoreUpTo(SC_V2) @Test @IgnoreUpTo(SC_V2)
public void testStartStopVpnProfileV6WithValidation() throws Exception { public void testStartStopVpnProfileV6WithValidation() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(true /* testIpv6Only */, true /* requiresValidation */, doTestStartStopVpnProfile(true /* testIpv6Only */, true /* requiresValidation */,
false /* testSessionKey */, false /* testIkeTunConnParams */); false /* testSessionKey */, false /* testIkeTunConnParams */);
} }
@Test @IgnoreUpTo(SC_V2) @Test @IgnoreUpTo(SC_V2)
public void testStartStopVpnProfileIkeTunConnParamsV4() throws Exception { public void testStartStopVpnProfileIkeTunConnParamsV4() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(false /* testIpv6Only */, false /* requiresValidation */, doTestStartStopVpnProfile(false /* testIpv6Only */, false /* requiresValidation */,
false /* testSessionKey */, true /* testIkeTunConnParams */); false /* testSessionKey */, true /* testIkeTunConnParams */);
} }
@Test @IgnoreUpTo(SC_V2) @Test @IgnoreUpTo(SC_V2)
public void testStartStopVpnProfileIkeTunConnParamsV4WithValidation() throws Exception { public void testStartStopVpnProfileIkeTunConnParamsV4WithValidation() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(false /* testIpv6Only */, true /* requiresValidation */, doTestStartStopVpnProfile(false /* testIpv6Only */, true /* requiresValidation */,
false /* testSessionKey */, true /* testIkeTunConnParams */); false /* testSessionKey */, true /* testIkeTunConnParams */);
} }
@Test @IgnoreUpTo(SC_V2) @Test @IgnoreUpTo(SC_V2)
public void testStartStopVpnProfileIkeTunConnParamsV6() throws Exception { public void testStartStopVpnProfileIkeTunConnParamsV6() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(true /* testIpv6Only */, false /* requiresValidation */, doTestStartStopVpnProfile(true /* testIpv6Only */, false /* requiresValidation */,
false /* testSessionKey */, true /* testIkeTunConnParams */); false /* testSessionKey */, true /* testIkeTunConnParams */);
} }
@Test @IgnoreUpTo(SC_V2) @Test @IgnoreUpTo(SC_V2)
public void testStartStopVpnProfileIkeTunConnParamsV6WithValidation() throws Exception { public void testStartStopVpnProfileIkeTunConnParamsV6WithValidation() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(true /* testIpv6Only */, true /* requiresValidation */, doTestStartStopVpnProfile(true /* testIpv6Only */, true /* requiresValidation */,
false /* testSessionKey */, true /* testIkeTunConnParams */); false /* testSessionKey */, true /* testIkeTunConnParams */);
} }
@@ -696,7 +681,6 @@ public class Ikev2VpnTest {
@IgnoreUpTo(SC_V2) @IgnoreUpTo(SC_V2)
@Test @Test
public void testStartProvisionedVpnV4ProfileSession() throws Exception { public void testStartProvisionedVpnV4ProfileSession() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(false /* testIpv6Only */, false /* requiresValidation */, doTestStartStopVpnProfile(false /* testIpv6Only */, false /* requiresValidation */,
true /* testSessionKey */, false /* testIkeTunConnParams */); true /* testSessionKey */, false /* testIkeTunConnParams */);
} }
@@ -704,59 +688,44 @@ public class Ikev2VpnTest {
@IgnoreUpTo(SC_V2) @IgnoreUpTo(SC_V2)
@Test @Test
public void testStartProvisionedVpnV6ProfileSession() throws Exception { public void testStartProvisionedVpnV6ProfileSession() throws Exception {
assumeTrue(TestUtils.shouldTestTApis());
doTestStartStopVpnProfile(true /* testIpv6Only */, false /* requiresValidation */, doTestStartStopVpnProfile(true /* testIpv6Only */, false /* requiresValidation */,
true /* testSessionKey */, false /* testIkeTunConnParams */); true /* testSessionKey */, false /* testIkeTunConnParams */);
} }
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@Test @Test
public void testBuildIkev2VpnProfileWithAutomaticNattKeepaliveTimerEnabled() throws Exception { public void testBuildIkev2VpnProfileWithAutomaticNattKeepaliveTimerEnabled() throws Exception {
// Cannot use @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU) because this test also requires API
// 34 shims, and @IgnoreUpTo does not check that.
assumeTrue(TestUtils.shouldTestUApis());
final Ikev2VpnProfile profileWithDefaultValue = buildIkev2VpnProfilePsk(TEST_SERVER_ADDR_V6, final Ikev2VpnProfile profileWithDefaultValue = buildIkev2VpnProfilePsk(TEST_SERVER_ADDR_V6,
false /* isRestrictedToTestNetworks */, false /* requiresValidation */); false /* isRestrictedToTestNetworks */, false /* requiresValidation */);
final Ikev2VpnProfileShim<Ikev2VpnProfile> shimWithDefaultValue = assertFalse(profileWithDefaultValue.isAutomaticNattKeepaliveTimerEnabled());
Ikev2VpnProfileShimImpl.newInstance(profileWithDefaultValue);
assertFalse(shimWithDefaultValue.isAutomaticNattKeepaliveTimerEnabled());
final Ikev2VpnProfileBuilderShim builder = final Ikev2VpnProfile.Builder builder =
Ikev2VpnProfileBuilderShimImpl.newInstance(TEST_SERVER_ADDR_V6, TEST_IDENTITY) new Ikev2VpnProfile.Builder(TEST_SERVER_ADDR_V6, TEST_IDENTITY)
.setAuthPsk(TEST_PSK); .setAuthPsk(TEST_PSK);
final Ikev2VpnProfile profile = buildIkev2VpnProfileCommon(builder, final Ikev2VpnProfile profile = buildIkev2VpnProfileCommon(builder,
false /* isRestrictedToTestNetworks */, false /* isRestrictedToTestNetworks */,
false /* requiresValidation */, false /* requiresValidation */,
false /* automaticIpVersionSelectionEnabled */, false /* automaticIpVersionSelectionEnabled */,
true /* automaticNattKeepaliveTimerEnabled */); true /* automaticNattKeepaliveTimerEnabled */);
final Ikev2VpnProfileShim<Ikev2VpnProfile> shim = assertTrue(profile.isAutomaticNattKeepaliveTimerEnabled());
Ikev2VpnProfileShimImpl.newInstance(profile);
assertTrue(shim.isAutomaticNattKeepaliveTimerEnabled());
} }
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@Test @Test
public void testBuildIkev2VpnProfileWithAutomaticIpVersionSelectionEnabled() throws Exception { public void testBuildIkev2VpnProfileWithAutomaticIpVersionSelectionEnabled() throws Exception {
// Cannot use @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU) because this test also requires API
// 34 shims, and @IgnoreUpTo does not check that.
assumeTrue(TestUtils.shouldTestUApis());
final Ikev2VpnProfile profileWithDefaultValue = buildIkev2VpnProfilePsk(TEST_SERVER_ADDR_V6, final Ikev2VpnProfile profileWithDefaultValue = buildIkev2VpnProfilePsk(TEST_SERVER_ADDR_V6,
false /* isRestrictedToTestNetworks */, false /* requiresValidation */); false /* isRestrictedToTestNetworks */, false /* requiresValidation */);
final Ikev2VpnProfileShim<Ikev2VpnProfile> shimWithDefaultValue = assertFalse(profileWithDefaultValue.isAutomaticIpVersionSelectionEnabled());
Ikev2VpnProfileShimImpl.newInstance(profileWithDefaultValue);
assertFalse(shimWithDefaultValue.isAutomaticIpVersionSelectionEnabled());
final Ikev2VpnProfileBuilderShim builder = final Ikev2VpnProfile.Builder builder =
Ikev2VpnProfileBuilderShimImpl.newInstance(TEST_SERVER_ADDR_V6, TEST_IDENTITY) new Ikev2VpnProfile.Builder(TEST_SERVER_ADDR_V6, TEST_IDENTITY)
.setAuthPsk(TEST_PSK); .setAuthPsk(TEST_PSK);
final Ikev2VpnProfile profile = buildIkev2VpnProfileCommon(builder, final Ikev2VpnProfile profile = buildIkev2VpnProfileCommon(builder,
false /* isRestrictedToTestNetworks */, false /* isRestrictedToTestNetworks */,
false /* requiresValidation */, false /* requiresValidation */,
true /* automaticIpVersionSelectionEnabled */, true /* automaticIpVersionSelectionEnabled */,
false /* automaticNattKeepaliveTimerEnabled */); false /* automaticNattKeepaliveTimerEnabled */);
final Ikev2VpnProfileShim<Ikev2VpnProfile> shim = assertTrue(profile.isAutomaticIpVersionSelectionEnabled());
Ikev2VpnProfileShimImpl.newInstance(profile);
assertTrue(shim.isAutomaticIpVersionSelectionEnabled());
} }
private static class CertificateAndKey { private static class CertificateAndKey {