From bc65e7e9928e51a300b5c342cc01d5148252e8e7 Mon Sep 17 00:00:00 2001 From: Chiachang Date: Wed, 11 May 2022 11:53:47 +0000 Subject: [PATCH] Address leftover comments This is a follow-up commit from aosp/2076483. Mainly to update: 1. Update the hex string to human understandable code instead of byte code. 2. Use IgnoreUpTo() to do the SDK check 3. Avoid relying on the code being tested Bug: 192078259 Test: atest FrameworksNetTests Change-Id: I8973d248a1d30fdcb597677dbf051e146041f905 (cherry picked from commit a023f88193c62b78afc0508865c8961863c9efda) Merged-In: I8973d248a1d30fdcb597677dbf051e146041f905 --- .../android/server/connectivity/VpnTest.java | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/tests/unit/java/com/android/server/connectivity/VpnTest.java b/tests/unit/java/com/android/server/connectivity/VpnTest.java index 6e5bf3b1c9..59d73783dd 100644 --- a/tests/unit/java/com/android/server/connectivity/VpnTest.java +++ b/tests/unit/java/com/android/server/connectivity/VpnTest.java @@ -27,9 +27,11 @@ import static android.content.pm.UserInfo.FLAG_RESTRICTED; import static android.net.ConnectivityManager.NetworkCallback; import static android.net.INetd.IF_STATE_DOWN; import static android.net.INetd.IF_STATE_UP; +import static android.os.Build.VERSION_CODES.S_V2; import static android.os.UserHandle.PER_USER_RANGE; import static com.android.modules.utils.build.SdkLevel.isAtLeastT; +import static com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; import static com.android.testutils.MiscAsserts.assertThrows; import static org.junit.Assert.assertArrayEquals; @@ -120,10 +122,12 @@ import com.android.internal.net.VpnProfile; import com.android.internal.util.HexDump; import com.android.modules.utils.build.SdkLevel; import com.android.server.IpSecService; +import com.android.server.vcn.util.PersistableBundleUtils; import com.android.testutils.DevSdkIgnoreRule; import com.android.testutils.DevSdkIgnoreRunner; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.AdditionalAnswers; @@ -159,10 +163,13 @@ import java.util.stream.Stream; */ @RunWith(DevSdkIgnoreRunner.class) @SmallTest -@DevSdkIgnoreRule.IgnoreUpTo(VERSION_CODES.S_V2) +@IgnoreUpTo(VERSION_CODES.S_V2) public class VpnTest { private static final String TAG = "VpnTest"; + @Rule + public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); + // Mock users static final UserInfo primaryUser = new UserInfo(27, "Primary", FLAG_ADMIN | FLAG_PRIMARY); static final UserInfo secondaryUser = new UserInfo(15, "Secondary", FLAG_ADMIN); @@ -186,27 +193,15 @@ public class VpnTest { private static final String TEST_IFACE_NAME = "TEST_IFACE"; private static final int TEST_TUNNEL_RESOURCE_ID = 0x2345; private static final long TEST_TIMEOUT_MS = 500L; - + private static final String PRIMARY_USER_APP_EXCLUDE_KEY = + "VPN_APP_EXCLUDED_27_com.testvpn.vpn"; /** * Names and UIDs for some fake packages. Important points: * - UID is ordered increasing. * - One pair of packages have consecutive UIDs. */ static final String[] PKGS = {"com.example", "org.example", "net.example", "web.vpn"}; - static final String PKGS_BYTES = - "3C62756E646C653E0A3C696E74206E616D653D22434F4C4C454354494F4E5F4C454E4754482220" - + "76616C75653D223422202F3E0A3C7062756E646C655F61735F6D6170206E616D653D224C4953" - + "545F4954454D5F30223E0A3C737472696E67206E616D653D22535452494E475F4B4559223E63" - + "6F6D2E6578616D706C653C2F737472696E673E0A3C2F7062756E646C655F61735F6D61703E0A" - + "3C7062756E646C655F61735F6D6170206E616D653D224C4953545F4954454D5F31223E0A3C73" - + "7472696E67206E616D653D22535452494E475F4B4559223E6F72672E6578616D706C653C2F73" - + "7472696E673E0A3C2F7062756E646C655F61735F6D61703E0A3C7062756E646C655F61735F6D" - + "6170206E616D653D224C4953545F4954454D5F32223E0A3C737472696E67206E616D653D2253" - + "5452494E475F4B4559223E6E65742E6578616D706C653C2F737472696E673E0A3C2F7062756E" - + "646C655F61735F6D61703E0A3C7062756E646C655F61735F6D6170206E616D653D224C495354" - + "5F4954454D5F33223E0A3C737472696E67206E616D653D22535452494E475F4B4559223E7765" - + "622E76706E3C2F737472696E673E0A3C2F7062756E646C655F61735F6D61703E0A3C2F62756E" - + "646C653E0A"; + static final String PKGS_BYTES = getPackageByteString(List.of(PKGS)); static final int[] PKG_UIDS = {10066, 10077, 10078, 10400}; // Mock packages @@ -315,6 +310,17 @@ public class VpnTest { return new Range(start, stop); } + private static String getPackageByteString(List packages) { + try { + return HexDump.toHexString( + PersistableBundleUtils.toDiskStableBytes(PersistableBundleUtils.fromList( + packages, PersistableBundleUtils.STRING_SERIALIZER)), + true /* upperCase */); + } catch (IOException e) { + return null; + } + } + @Test public void testRestrictedProfilesAreAddedToVpn() { setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB); @@ -769,11 +775,10 @@ public class VpnTest { } private Vpn prepareVpnForVerifyAppExclusionList() throws Exception { - assumeTrue(isAtLeastT()); final Vpn vpn = createVpnAndSetupUidChecks(AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN); when(mVpnProfileStore.get(vpn.getProfileNameForPackage(TEST_VPN_PKG))) .thenReturn(mVpnProfile.encode()); - when(mVpnProfileStore.get(vpn.getVpnAppExcludedForPackage(TEST_VPN_PKG))) + when(mVpnProfileStore.get(PRIMARY_USER_APP_EXCLUDE_KEY)) .thenReturn(HexDump.hexStringToByteArray(PKGS_BYTES)); vpn.startVpnProfile(TEST_VPN_PKG); @@ -785,12 +790,13 @@ public class VpnTest { return vpn; } - @Test + @Test @IgnoreUpTo(S_V2) public void testSetAndGetAppExclusionList() throws Exception { final Vpn vpn = prepareVpnForVerifyAppExclusionList(); + verify(mVpnProfileStore, never()).put(eq(PRIMARY_USER_APP_EXCLUDE_KEY), any()); vpn.setAppExclusionList(TEST_VPN_PKG, Arrays.asList(PKGS)); verify(mVpnProfileStore) - .put(eq(vpn.getVpnAppExcludedForPackage(TEST_VPN_PKG)), + .put(eq(PRIMARY_USER_APP_EXCLUDE_KEY), eq(HexDump.hexStringToByteArray(PKGS_BYTES))); assertEquals(vpn.createUserAndRestrictedProfilesRanges( primaryUser.id, null, Arrays.asList(PKGS)), @@ -798,7 +804,7 @@ public class VpnTest { assertEquals(Arrays.asList(PKGS), vpn.getAppExclusionList(TEST_VPN_PKG)); } - @Test + @Test @IgnoreUpTo(S_V2) public void testSetAndGetAppExclusionListRestrictedUser() throws Exception { final Vpn vpn = prepareVpnForVerifyAppExclusionList(); // Mock it to restricted profile