From 1e66940f0b1867f12783baa3a8b16572e63a24ba Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Tue, 22 Apr 2025 11:51:19 +0900 Subject: [PATCH] Remove last @hide usage in CtsNetTestCases Inline the last remaining constants, and add a workaround for test APIs that are missing in module API stubs. While inlining the constants and reflection usage is far from ideal, this is effectively what the tests are doing today, and it allows preventing further usage of hidden API in the tests by making them build against API stubs (in subsequent change). Test: atest Flag: TEST_ONLY Bug: 410435307 Change-Id: Id2ffc0cb41bc31e4cd049b711a392904910414a3 --- .../src/android/net/cts/ApfIntegrationTest.kt | 7 ++- .../net/src/android/net/cts/Ikev2VpnTest.java | 47 ++++++++++++------- .../net/cts/IpSecManagerTunnelTest.java | 4 +- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt index 52777fd0ec..3e35968738 100644 --- a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt +++ b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt @@ -72,8 +72,8 @@ import com.android.compatibility.common.util.PropertyUtil.getVsrApiLevel import com.android.compatibility.common.util.SystemUtil.runShellCommand import com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow import com.android.compatibility.common.util.VsrTest -import com.android.internal.util.HexDump import com.android.modules.utils.build.SdkLevel +import com.android.net.module.util.HexDump import com.android.net.module.util.NetworkStackConstants.ETHER_ADDR_LEN import com.android.net.module.util.NetworkStackConstants.ETHER_DST_ADDR_OFFSET import com.android.net.module.util.NetworkStackConstants.ETHER_HEADER_LEN @@ -170,7 +170,10 @@ class ApfIntegrationTest { val packageManager = context.getPackageManager() val userManager = context.getSystemService(UserManager::class.java)!! return (packageManager.hasSystemFeature(FEATURE_AUTOMOTIVE) && - userManager.isVisibleBackgroundUsersSupported) + // isVisibleBackgroundUsersSupported is @TestApi, but this test should build + // against module API stubs, which do not include it (b/409931932). + userManager.javaClass.getMethod("isVisibleBackgroundUsersSupported") + .invoke(userManager) as Boolean) } private fun disableLowPowerStandby() { diff --git a/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java b/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java index d948241e02..3289385b5d 100644 --- a/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java +++ b/tests/cts/net/src/android/net/cts/Ikev2VpnTest.java @@ -22,8 +22,8 @@ import static android.net.NetworkCapabilities.TRANSPORT_VPN; import static android.net.cts.util.CtsNetUtils.TestNetworkCallback; 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.isAtLeastU; import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2; import static org.junit.Assert.assertArrayEquals; @@ -34,12 +34,11 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeTrue; import android.Manifest; import android.annotation.NonNull; -import android.app.AppOpsManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -99,6 +98,10 @@ import javax.security.auth.x500.X500Principal; public class Ikev2VpnTest { private static final String TAG = Ikev2VpnTest.class.getSimpleName(); + // TODO: make AppOpsManager.OP_ACTIVATE_VPN API + private static final int OP_ACTIVATE_VPN = 47; + private static final int OP_ACTIVATE_PLATFORM_VPN = 94; + @Rule public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); @@ -219,8 +222,8 @@ public class Ikev2VpnTest { for (TestableNetworkCallback callback : mCallbacksToUnregister) { sCM.unregisterNetworkCallback(callback); } - setAppop(AppOpsManager.OP_ACTIVATE_VPN, false); - setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, false); + setAppop(OP_ACTIVATE_VPN, false); + setAppop(OP_ACTIVATE_PLATFORM_VPN, false); // Make sure the VpnProfile is not provisioned already. sVpnMgr.stopProvisionedVpnProfile(); @@ -265,14 +268,22 @@ public class Ikev2VpnTest { builder.setAutomaticNattKeepaliveTimerEnabled(automaticNattKeepaliveTimerEnabled); } - // TODO: replace it in alternative way to remove the hidden method usage if (isRestrictedToTestNetworks) { - builder.restrictToTestNetworks(); + restrictToTestNetworks(builder); } return builder.build(); } + private void restrictToTestNetworks(@NonNull Ikev2VpnProfile.Builder builder) { + try { + // TODO: replace it in alternative way to remove the hidden method usage + builder.getClass().getMethod("restrictToTestNetworks").invoke(builder); + } catch (ReflectiveOperationException e) { + throw new AssertionError(e); + } + } + private Ikev2VpnProfile buildIkev2VpnProfileIkeTunConnParams( final boolean isRestrictedToTestNetworks, final boolean requiresValidation, final boolean testIpv6) throws Exception { @@ -288,9 +299,8 @@ public class Ikev2VpnTest { .setMaxMtu(TEST_MTU) .setMetered(false); - // TODO: replace it in alternative way to remove the hidden method usage if (isRestrictedToTestNetworks) { - builder.restrictToTestNetworks(); + restrictToTestNetworks(builder); } return builder.build(); } @@ -335,7 +345,8 @@ public class Ikev2VpnTest { assertTrue(profile.isBypassable()); assertFalse(profile.isMetered()); assertEquals(TEST_MTU, profile.getMaxMtu()); - assertFalse(profile.isRestrictedToTestNetworks()); + assertFalse((boolean) profile.getClass().getMethod("isRestrictedToTestNetworks") + .invoke(profile)); } public void doTestBuildIkev2VpnProfilePsk(final boolean requiresValidation) throws Exception { @@ -427,8 +438,8 @@ public class Ikev2VpnTest { throws Exception { assumeTrue(mCtsNetUtils.hasIpsecTunnelsFeature()); - setAppop(AppOpsManager.OP_ACTIVATE_VPN, hasActivateVpn); - setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, hasActivatePlatformVpn); + setAppop(OP_ACTIVATE_VPN, hasActivateVpn); + setAppop(OP_ACTIVATE_PLATFORM_VPN, hasActivatePlatformVpn); final Ikev2VpnProfile profile = buildIkev2VpnProfilePsk(TEST_SERVER_ADDR_V6, false /* isRestrictedToTestNetworks */, false /* requiresValidation */); @@ -472,19 +483,19 @@ public class Ikev2VpnTest { public void testDeleteVpnProfile() throws Exception { assumeTrue(mCtsNetUtils.hasIpsecTunnelsFeature()); - setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, true); + setAppop(OP_ACTIVATE_PLATFORM_VPN, true); final Ikev2VpnProfile profile = buildIkev2VpnProfilePsk(TEST_SERVER_ADDR_V6, false /* isRestrictedToTestNetworks */, false /* requiresValidation */); assertNull(sVpnMgr.provisionVpnProfile(profile)); // Verify that deleting the profile works (even without the appop) - setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, false); + setAppop(OP_ACTIVATE_PLATFORM_VPN, false); sVpnMgr.deleteProvisionedVpnProfile(); // Test that the profile was deleted - starting it should throw an IAE. try { - setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, true); + setAppop(OP_ACTIVATE_PLATFORM_VPN, true); sVpnMgr.startProvisionedVpnProfile(); fail("Expected IllegalArgumentException due to missing profile"); } catch (IllegalArgumentException expected) { @@ -495,8 +506,8 @@ public class Ikev2VpnTest { public void testStartVpnProfileNoPreviousConsent() throws Exception { assumeTrue(mCtsNetUtils.hasIpsecTunnelsFeature()); - setAppop(AppOpsManager.OP_ACTIVATE_VPN, false); - setAppop(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, false); + setAppop(OP_ACTIVATE_VPN, false); + setAppop(OP_ACTIVATE_PLATFORM_VPN, false); // Make sure the VpnProfile is not provisioned already. sVpnMgr.stopProvisionedVpnProfile(); @@ -518,7 +529,7 @@ public class Ikev2VpnTest { boolean hasNat = !testIpv6; // Requires MANAGE_TEST_NETWORKS to provision a test-mode profile. - mCtsNetUtils.setAppopPrivileged(AppOpsManager.OP_ACTIVATE_PLATFORM_VPN, true); + mCtsNetUtils.setAppopPrivileged(OP_ACTIVATE_PLATFORM_VPN, true); final Ikev2VpnProfile profile = testIkeTunConnParams ? buildIkev2VpnProfileIkeTunConnParams(true /* isRestrictedToTestNetworks */, diff --git a/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java b/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java index f2c6d337a9..2f8ab032ad 100644 --- a/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java +++ b/tests/cts/net/src/android/net/cts/IpSecManagerTunnelTest.java @@ -16,7 +16,6 @@ package android.net.cts; -import static android.app.AppOpsManager.OP_MANAGE_IPSEC_TUNNELS; import static android.net.IpSecManager.UdpEncapsulationSocket; import static android.net.cts.IpSecManagerTest.assumeExperimentalIpv6UdpEncapSupported; import static android.net.cts.IpSecManagerTest.assumeRequestIpSecTransformStateSupported; @@ -87,6 +86,9 @@ import java.net.NetworkInterface; public class IpSecManagerTunnelTest extends IpSecBaseTest { @Rule public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule(); + // TODO: make AppOpsManager.OP_MANAGE_IPSEC_TUNNELS API + private static final int OP_MANAGE_IPSEC_TUNNELS = 75; + private static final String TAG = IpSecManagerTunnelTest.class.getSimpleName(); private static final InetAddress LOCAL_OUTER_4 = InetAddress.parseNumericAddress("192.0.2.1");