Add VpnTestBase for VPN test common code

This is a no-op change to move some common codes that share
between different VPN unit tests to the new base class.
It's helpful to reduce the code duplication for the follow up
work to add the VpnManagerServiceTest and other test codes
refactor.

Bug: 231373589
Test: atest FrameworksNetTests
Change-Id: Id5111e7ca33ea8f9eb4bcc6c13d0e681f0664d24
This commit is contained in:
chiachangwang
2022-06-28 08:00:35 +00:00
parent 522cea7d4e
commit 9f672bdae6
2 changed files with 148 additions and 88 deletions

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2022 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 com.android.server;
import static android.content.pm.UserInfo.FLAG_ADMIN;
import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
import static android.content.pm.UserInfo.FLAG_PRIMARY;
import static android.content.pm.UserInfo.FLAG_RESTRICTED;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.Process;
import android.os.UserHandle;
import android.util.ArrayMap;
import java.util.Map;
/** Common variables or methods shared between VpnTest and VpnManagerServiceTest. */
public class VpnTestBase {
protected static final String TEST_VPN_PKG = "com.testvpn.vpn";
/**
* Names and UIDs for some fake packages. Important points:
* - UID is ordered increasing.
* - One pair of packages have consecutive UIDs.
*/
protected static final String[] PKGS = {"com.example", "org.example", "net.example", "web.vpn"};
protected static final int[] PKG_UIDS = {10066, 10077, 10078, 10400};
// Mock packages
protected static final Map<String, Integer> sPackages = new ArrayMap<>();
static {
for (int i = 0; i < PKGS.length; i++) {
sPackages.put(PKGS[i], PKG_UIDS[i]);
}
sPackages.put(TEST_VPN_PKG, Process.myUid());
}
// Mock users
protected static final int SYSTEM_USER_ID = 0;
protected static final UserInfo SYSTEM_USER = new UserInfo(0, "system", UserInfo.FLAG_PRIMARY);
protected static final UserInfo PRIMARY_USER = new UserInfo(27, "Primary",
FLAG_ADMIN | FLAG_PRIMARY);
protected static final UserInfo SECONDARY_USER = new UserInfo(15, "Secondary", FLAG_ADMIN);
protected static final UserInfo RESTRICTED_PROFILE_A = new UserInfo(40, "RestrictedA",
FLAG_RESTRICTED);
protected static final UserInfo RESTRICTED_PROFILE_B = new UserInfo(42, "RestrictedB",
FLAG_RESTRICTED);
protected static final UserInfo MANAGED_PROFILE_A = new UserInfo(45, "ManagedA",
FLAG_MANAGED_PROFILE);
static {
RESTRICTED_PROFILE_A.restrictedProfileParentId = PRIMARY_USER.id;
RESTRICTED_PROFILE_B.restrictedProfileParentId = SECONDARY_USER.id;
MANAGED_PROFILE_A.profileGroupId = PRIMARY_USER.id;
}
// Populate a fake packageName-to-UID mapping.
protected void setMockedPackages(PackageManager mockPm, final Map<String, Integer> packages) {
try {
doAnswer(invocation -> {
final String appName = (String) invocation.getArguments()[0];
final int userId = (int) invocation.getArguments()[1];
final Integer appId = packages.get(appName);
if (appId == null) {
throw new PackageManager.NameNotFoundException(appName);
}
return UserHandle.getUid(userId, appId);
}).when(mockPm).getPackageUidAsUser(anyString(), anyInt());
} catch (Exception e) {
}
}
}

View File

@@ -20,10 +20,6 @@ import static android.Manifest.permission.BIND_VPN_SERVICE;
import static android.Manifest.permission.CONTROL_VPN; import static android.Manifest.permission.CONTROL_VPN;
import static android.content.pm.PackageManager.PERMISSION_DENIED; import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.content.pm.UserInfo.FLAG_ADMIN;
import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
import static android.content.pm.UserInfo.FLAG_PRIMARY;
import static android.content.pm.UserInfo.FLAG_RESTRICTED;
import static android.net.ConnectivityManager.NetworkCallback; import static android.net.ConnectivityManager.NetworkCallback;
import static android.net.INetd.IF_STATE_DOWN; import static android.net.INetd.IF_STATE_DOWN;
import static android.net.INetd.IF_STATE_UP; import static android.net.INetd.IF_STATE_UP;
@@ -145,6 +141,7 @@ import com.android.internal.util.HexDump;
import com.android.modules.utils.build.SdkLevel; import com.android.modules.utils.build.SdkLevel;
import com.android.server.DeviceIdleInternal; import com.android.server.DeviceIdleInternal;
import com.android.server.IpSecService; import com.android.server.IpSecService;
import com.android.server.VpnTestBase;
import com.android.server.vcn.util.PersistableBundleUtils; import com.android.server.vcn.util.PersistableBundleUtils;
import com.android.testutils.DevSdkIgnoreRule; import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner; import com.android.testutils.DevSdkIgnoreRunner;
@@ -192,27 +189,14 @@ import java.util.stream.Stream;
@RunWith(DevSdkIgnoreRunner.class) @RunWith(DevSdkIgnoreRunner.class)
@SmallTest @SmallTest
@IgnoreUpTo(VERSION_CODES.S_V2) @IgnoreUpTo(VERSION_CODES.S_V2)
public class VpnTest { public class VpnTest extends VpnTestBase {
private static final String TAG = "VpnTest"; private static final String TAG = "VpnTest";
@Rule @Rule
public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); 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);
static final UserInfo restrictedProfileA = new UserInfo(40, "RestrictedA", FLAG_RESTRICTED);
static final UserInfo restrictedProfileB = new UserInfo(42, "RestrictedB", FLAG_RESTRICTED);
static final UserInfo managedProfileA = new UserInfo(45, "ManagedA", FLAG_MANAGED_PROFILE);
static {
restrictedProfileA.restrictedProfileParentId = primaryUser.id;
restrictedProfileB.restrictedProfileParentId = secondaryUser.id;
managedProfileA.profileGroupId = primaryUser.id;
}
static final Network EGRESS_NETWORK = new Network(101); static final Network EGRESS_NETWORK = new Network(101);
static final String EGRESS_IFACE = "wlan0"; static final String EGRESS_IFACE = "wlan0";
static final String TEST_VPN_PKG = "com.testvpn.vpn";
private static final String TEST_VPN_CLIENT = "2.4.6.8"; private static final String TEST_VPN_CLIENT = "2.4.6.8";
private static final String TEST_VPN_SERVER = "1.2.3.4"; private static final String TEST_VPN_SERVER = "1.2.3.4";
private static final String TEST_VPN_IDENTITY = "identity"; private static final String TEST_VPN_IDENTITY = "identity";
@@ -249,23 +233,8 @@ public class VpnTest {
private static final long TEST_TIMEOUT_MS = 500L; private static final long TEST_TIMEOUT_MS = 500L;
private static final String PRIMARY_USER_APP_EXCLUDE_KEY = private static final String PRIMARY_USER_APP_EXCLUDE_KEY =
"VPN_APP_EXCLUDED_27_com.testvpn.vpn"; "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 = getPackageByteString(List.of(PKGS)); static final String PKGS_BYTES = getPackageByteString(List.of(PKGS));
static final int[] PKG_UIDS = {10066, 10077, 10078, 10400}; private static final Range<Integer> PRIMARY_USER_RANGE = uidRangeForUser(PRIMARY_USER.id);
// Mock packages
static final Map<String, Integer> mPackages = new ArrayMap<>();
static {
for (int i = 0; i < PKGS.length; i++) {
mPackages.put(PKGS[i], PKG_UIDS[i]);
}
}
private static final Range<Integer> PRI_USER_RANGE = uidRangeForUser(primaryUser.id);
@Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext;
@Mock private UserManager mUserManager; @Mock private UserManager mUserManager;
@@ -307,7 +276,7 @@ public class VpnTest {
mTestDeps = spy(new TestDeps()); mTestDeps = spy(new TestDeps());
when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mContext.getPackageManager()).thenReturn(mPackageManager);
setMockedPackages(mPackages); setMockedPackages(sPackages);
when(mContext.getPackageName()).thenReturn(TEST_VPN_PKG); when(mContext.getPackageName()).thenReturn(TEST_VPN_PKG);
when(mContext.getOpPackageName()).thenReturn(TEST_VPN_PKG); when(mContext.getOpPackageName()).thenReturn(TEST_VPN_PKG);
@@ -412,50 +381,51 @@ public class VpnTest {
@Test @Test
public void testRestrictedProfilesAreAddedToVpn() { public void testRestrictedProfilesAreAddedToVpn() {
setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB); setMockedUsers(PRIMARY_USER, SECONDARY_USER, RESTRICTED_PROFILE_A, RESTRICTED_PROFILE_B);
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
// Assume the user can have restricted profiles. // Assume the user can have restricted profiles.
doReturn(true).when(mUserManager).canHaveRestrictedProfile(); doReturn(true).when(mUserManager).canHaveRestrictedProfile();
final Set<Range<Integer>> ranges = final Set<Range<Integer>> ranges =
vpn.createUserAndRestrictedProfilesRanges(primaryUser.id, null, null); vpn.createUserAndRestrictedProfilesRanges(PRIMARY_USER.id, null, null);
assertEquals(rangeSet(PRI_USER_RANGE, uidRangeForUser(restrictedProfileA.id)), ranges); assertEquals(rangeSet(PRIMARY_USER_RANGE, uidRangeForUser(RESTRICTED_PROFILE_A.id)),
ranges);
} }
@Test @Test
public void testManagedProfilesAreNotAddedToVpn() { public void testManagedProfilesAreNotAddedToVpn() {
setMockedUsers(primaryUser, managedProfileA); setMockedUsers(PRIMARY_USER, MANAGED_PROFILE_A);
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final Set<Range<Integer>> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id, final Set<Range<Integer>> ranges = vpn.createUserAndRestrictedProfilesRanges(
null, null); PRIMARY_USER.id, null, null);
assertEquals(rangeSet(PRI_USER_RANGE), ranges); assertEquals(rangeSet(PRIMARY_USER_RANGE), ranges);
} }
@Test @Test
public void testAddUserToVpnOnlyAddsOneUser() { public void testAddUserToVpnOnlyAddsOneUser() {
setMockedUsers(primaryUser, restrictedProfileA, managedProfileA); setMockedUsers(PRIMARY_USER, RESTRICTED_PROFILE_A, MANAGED_PROFILE_A);
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final Set<Range<Integer>> ranges = new ArraySet<>(); final Set<Range<Integer>> ranges = new ArraySet<>();
vpn.addUserToRanges(ranges, primaryUser.id, null, null); vpn.addUserToRanges(ranges, PRIMARY_USER.id, null, null);
assertEquals(rangeSet(PRI_USER_RANGE), ranges); assertEquals(rangeSet(PRIMARY_USER_RANGE), ranges);
} }
@Test @Test
public void testUidAllowAndDenylist() throws Exception { public void testUidAllowAndDenylist() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final Range<Integer> user = PRI_USER_RANGE; final Range<Integer> user = PRIMARY_USER_RANGE;
final int userStart = user.getLower(); final int userStart = user.getLower();
final int userStop = user.getUpper(); final int userStop = user.getUpper();
final String[] packages = {PKGS[0], PKGS[1], PKGS[2]}; final String[] packages = {PKGS[0], PKGS[1], PKGS[2]};
// Allowed list // Allowed list
final Set<Range<Integer>> allow = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id, final Set<Range<Integer>> allow = vpn.createUserAndRestrictedProfilesRanges(PRIMARY_USER.id,
Arrays.asList(packages), null /* disallowedApplications */); Arrays.asList(packages), null /* disallowedApplications */);
assertEquals(rangeSet( assertEquals(rangeSet(
uidRange(userStart + PKG_UIDS[0], userStart + PKG_UIDS[0]), uidRange(userStart + PKG_UIDS[0], userStart + PKG_UIDS[0]),
@@ -468,7 +438,7 @@ public class VpnTest {
// Denied list // Denied list
final Set<Range<Integer>> disallow = final Set<Range<Integer>> disallow =
vpn.createUserAndRestrictedProfilesRanges(primaryUser.id, vpn.createUserAndRestrictedProfilesRanges(PRIMARY_USER.id,
null /* allowedApplications */, Arrays.asList(packages)); null /* allowedApplications */, Arrays.asList(packages));
assertEquals(rangeSet( assertEquals(rangeSet(
uidRange(userStart, userStart + PKG_UIDS[0] - 1), uidRange(userStart, userStart + PKG_UIDS[0] - 1),
@@ -490,7 +460,7 @@ public class VpnTest {
@Test @Test
public void testGetAlwaysAndOnGetLockDown() throws Exception { public void testGetAlwaysAndOnGetLockDown() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
// Default state. // Default state.
assertFalse(vpn.getAlwaysOn()); assertFalse(vpn.getAlwaysOn());
@@ -514,8 +484,8 @@ public class VpnTest {
@Test @Test
public void testLockdownChangingPackage() throws Exception { public void testLockdownChangingPackage() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final Range<Integer> user = PRI_USER_RANGE; final Range<Integer> user = PRIMARY_USER_RANGE;
final int userStart = user.getLower(); final int userStart = user.getLower();
final int userStop = user.getUpper(); final int userStop = user.getUpper();
// Set always-on without lockdown. // Set always-on without lockdown.
@@ -548,8 +518,8 @@ public class VpnTest {
@Test @Test
public void testLockdownAllowlist() throws Exception { public void testLockdownAllowlist() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final Range<Integer> user = PRI_USER_RANGE; final Range<Integer> user = PRIMARY_USER_RANGE;
final int userStart = user.getLower(); final int userStart = user.getLower();
final int userStop = user.getUpper(); final int userStop = user.getUpper();
// Set always-on with lockdown and allow app PKGS[2] from lockdown. // Set always-on with lockdown and allow app PKGS[2] from lockdown.
@@ -659,9 +629,9 @@ public class VpnTest {
@Test @Test
public void testLockdownRuleRepeatability() throws Exception { public void testLockdownRuleRepeatability() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final UidRangeParcel[] primaryUserRangeParcel = new UidRangeParcel[] { final UidRangeParcel[] primaryUserRangeParcel = new UidRangeParcel[] {
new UidRangeParcel(PRI_USER_RANGE.getLower(), PRI_USER_RANGE.getUpper())}; new UidRangeParcel(PRIMARY_USER_RANGE.getLower(), PRIMARY_USER_RANGE.getUpper())};
// Given legacy lockdown is already enabled, // Given legacy lockdown is already enabled,
vpn.setLockdown(true); vpn.setLockdown(true);
verify(mConnectivityManager, times(1)).setRequireVpnForUids(true, verify(mConnectivityManager, times(1)).setRequireVpnForUids(true,
@@ -692,9 +662,9 @@ public class VpnTest {
@Test @Test
public void testLockdownRuleReversibility() throws Exception { public void testLockdownRuleReversibility() throws Exception {
doReturn(PERMISSION_GRANTED).when(mContext).checkCallingOrSelfPermission(CONTROL_VPN); doReturn(PERMISSION_GRANTED).when(mContext).checkCallingOrSelfPermission(CONTROL_VPN);
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
final UidRangeParcel[] entireUser = { final UidRangeParcel[] entireUser = {
new UidRangeParcel(PRI_USER_RANGE.getLower(), PRI_USER_RANGE.getUpper()) new UidRangeParcel(PRIMARY_USER_RANGE.getLower(), PRIMARY_USER_RANGE.getUpper())
}; };
final UidRangeParcel[] exceptPkg0 = { final UidRangeParcel[] exceptPkg0 = {
new UidRangeParcel(entireUser[0].start, entireUser[0].start + PKG_UIDS[0] - 1), new UidRangeParcel(entireUser[0].start, entireUser[0].start + PKG_UIDS[0] - 1),
@@ -744,17 +714,17 @@ public class VpnTest {
@Test @Test
public void testIsAlwaysOnPackageSupported() throws Exception { public void testIsAlwaysOnPackageSupported() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
ApplicationInfo appInfo = new ApplicationInfo(); ApplicationInfo appInfo = new ApplicationInfo();
when(mPackageManager.getApplicationInfoAsUser(eq(PKGS[0]), anyInt(), eq(primaryUser.id))) when(mPackageManager.getApplicationInfoAsUser(eq(PKGS[0]), anyInt(), eq(PRIMARY_USER.id)))
.thenReturn(appInfo); .thenReturn(appInfo);
ServiceInfo svcInfo = new ServiceInfo(); ServiceInfo svcInfo = new ServiceInfo();
ResolveInfo resInfo = new ResolveInfo(); ResolveInfo resInfo = new ResolveInfo();
resInfo.serviceInfo = svcInfo; resInfo.serviceInfo = svcInfo;
when(mPackageManager.queryIntentServicesAsUser(any(), eq(PackageManager.GET_META_DATA), when(mPackageManager.queryIntentServicesAsUser(any(), eq(PackageManager.GET_META_DATA),
eq(primaryUser.id))) eq(PRIMARY_USER.id)))
.thenReturn(Collections.singletonList(resInfo)); .thenReturn(Collections.singletonList(resInfo));
// null package name should return false // null package name should return false
@@ -778,9 +748,9 @@ public class VpnTest {
@Test @Test
public void testNotificationShownForAlwaysOnApp() throws Exception { public void testNotificationShownForAlwaysOnApp() throws Exception {
final UserHandle userHandle = UserHandle.of(primaryUser.id); final UserHandle userHandle = UserHandle.of(PRIMARY_USER.id);
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
setMockedUsers(primaryUser); setMockedUsers(PRIMARY_USER);
final InOrder order = inOrder(mNotificationManager); final InOrder order = inOrder(mNotificationManager);
@@ -813,15 +783,15 @@ public class VpnTest {
*/ */
@Test @Test
public void testGetProfileNameForPackage() throws Exception { public void testGetProfileNameForPackage() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
setMockedUsers(primaryUser); setMockedUsers(PRIMARY_USER);
final String expected = Credentials.PLATFORM_VPN + primaryUser.id + "_" + TEST_VPN_PKG; final String expected = Credentials.PLATFORM_VPN + PRIMARY_USER.id + "_" + TEST_VPN_PKG;
assertEquals(expected, vpn.getProfileNameForPackage(TEST_VPN_PKG)); assertEquals(expected, vpn.getProfileNameForPackage(TEST_VPN_PKG));
} }
private Vpn createVpnAndSetupUidChecks(String... grantedOps) throws Exception { private Vpn createVpnAndSetupUidChecks(String... grantedOps) throws Exception {
return createVpnAndSetupUidChecks(primaryUser, grantedOps); return createVpnAndSetupUidChecks(PRIMARY_USER, grantedOps);
} }
private Vpn createVpnAndSetupUidChecks(UserInfo user, String... grantedOps) throws Exception { private Vpn createVpnAndSetupUidChecks(UserInfo user, String... grantedOps) throws Exception {
@@ -894,7 +864,7 @@ public class VpnTest {
.put(eq(PRIMARY_USER_APP_EXCLUDE_KEY), .put(eq(PRIMARY_USER_APP_EXCLUDE_KEY),
eq(HexDump.hexStringToByteArray(PKGS_BYTES))); eq(HexDump.hexStringToByteArray(PKGS_BYTES)));
assertEquals(vpn.createUserAndRestrictedProfilesRanges( assertEquals(vpn.createUserAndRestrictedProfilesRanges(
primaryUser.id, null, Arrays.asList(PKGS)), PRIMARY_USER.id, null, Arrays.asList(PKGS)),
vpn.mNetworkCapabilities.getUids()); vpn.mNetworkCapabilities.getUids());
assertEquals(Arrays.asList(PKGS), vpn.getAppExclusionList(TEST_VPN_PKG)); assertEquals(Arrays.asList(PKGS), vpn.getAppExclusionList(TEST_VPN_PKG));
} }
@@ -903,7 +873,7 @@ public class VpnTest {
public void testSetAndGetAppExclusionListRestrictedUser() throws Exception { public void testSetAndGetAppExclusionListRestrictedUser() throws Exception {
final Vpn vpn = prepareVpnForVerifyAppExclusionList(); final Vpn vpn = prepareVpnForVerifyAppExclusionList();
// Mock it to restricted profile // Mock it to restricted profile
when(mUserManager.getUserInfo(anyInt())).thenReturn(restrictedProfileA); when(mUserManager.getUserInfo(anyInt())).thenReturn(RESTRICTED_PROFILE_A);
// Restricted users cannot configure VPNs // Restricted users cannot configure VPNs
assertThrows(SecurityException.class, assertThrows(SecurityException.class,
() -> vpn.setAppExclusionList(TEST_VPN_PKG, new ArrayList<>())); () -> vpn.setAppExclusionList(TEST_VPN_PKG, new ArrayList<>()));
@@ -953,7 +923,7 @@ public class VpnTest {
public void testProvisionVpnProfileRestrictedUser() throws Exception { public void testProvisionVpnProfileRestrictedUser() throws Exception {
final Vpn vpn = final Vpn vpn =
createVpnAndSetupUidChecks( createVpnAndSetupUidChecks(
restrictedProfileA, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN); RESTRICTED_PROFILE_A, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN);
try { try {
vpn.provisionVpnProfile(TEST_VPN_PKG, mVpnProfile); vpn.provisionVpnProfile(TEST_VPN_PKG, mVpnProfile);
@@ -976,7 +946,7 @@ public class VpnTest {
public void testDeleteVpnProfileRestrictedUser() throws Exception { public void testDeleteVpnProfileRestrictedUser() throws Exception {
final Vpn vpn = final Vpn vpn =
createVpnAndSetupUidChecks( createVpnAndSetupUidChecks(
restrictedProfileA, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN); RESTRICTED_PROFILE_A, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN);
try { try {
vpn.deleteVpnProfile(TEST_VPN_PKG); vpn.deleteVpnProfile(TEST_VPN_PKG);
@@ -1099,7 +1069,7 @@ public class VpnTest {
public void testStartVpnProfileRestrictedUser() throws Exception { public void testStartVpnProfileRestrictedUser() throws Exception {
final Vpn vpn = final Vpn vpn =
createVpnAndSetupUidChecks( createVpnAndSetupUidChecks(
restrictedProfileA, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN); RESTRICTED_PROFILE_A, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN);
try { try {
vpn.startVpnProfile(TEST_VPN_PKG); vpn.startVpnProfile(TEST_VPN_PKG);
@@ -1112,7 +1082,7 @@ public class VpnTest {
public void testStopVpnProfileRestrictedUser() throws Exception { public void testStopVpnProfileRestrictedUser() throws Exception {
final Vpn vpn = final Vpn vpn =
createVpnAndSetupUidChecks( createVpnAndSetupUidChecks(
restrictedProfileA, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN); RESTRICTED_PROFILE_A, AppOpsManager.OPSTR_ACTIVATE_PLATFORM_VPN);
try { try {
vpn.stopVpnProfile(TEST_VPN_PKG); vpn.stopVpnProfile(TEST_VPN_PKG);
@@ -1183,7 +1153,7 @@ public class VpnTest {
private void verifyVpnManagerEvent(String sessionKey, String category, int errorClass, private void verifyVpnManagerEvent(String sessionKey, String category, int errorClass,
int errorCode, VpnProfileState... profileState) { int errorCode, VpnProfileState... profileState) {
final Context userContext = final Context userContext =
mContext.createContextAsUser(UserHandle.of(primaryUser.id), 0 /* flags */); mContext.createContextAsUser(UserHandle.of(PRIMARY_USER.id), 0 /* flags */);
final ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); final ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
final int verifyTimes = (profileState == null) ? 1 : profileState.length; final int verifyTimes = (profileState == null) ? 1 : profileState.length;
@@ -1250,7 +1220,7 @@ public class VpnTest {
assumeTrue(SdkLevel.isAtLeastT()); assumeTrue(SdkLevel.isAtLeastT());
// Calling setAlwaysOnPackage() needs to hold CONTROL_VPN. // Calling setAlwaysOnPackage() needs to hold CONTROL_VPN.
doReturn(PERMISSION_GRANTED).when(mContext).checkCallingOrSelfPermission(CONTROL_VPN); doReturn(PERMISSION_GRANTED).when(mContext).checkCallingOrSelfPermission(CONTROL_VPN);
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
// Enable VPN always-on for PKGS[1]. // Enable VPN always-on for PKGS[1].
assertTrue(vpn.setAlwaysOnPackage(PKGS[1], false /* lockdown */, assertTrue(vpn.setAlwaysOnPackage(PKGS[1], false /* lockdown */,
null /* lockdownAllowlist */)); null /* lockdownAllowlist */));
@@ -1512,7 +1482,7 @@ public class VpnTest {
public void testStartPlatformVpnIllegalArgumentExceptionInSetup() throws Exception { public void testStartPlatformVpnIllegalArgumentExceptionInSetup() throws Exception {
when(mIkev2SessionCreator.createIkeSession(any(), any(), any(), any(), any(), any())) when(mIkev2SessionCreator.createIkeSession(any(), any(), any(), any(), any(), any()))
.thenThrow(new IllegalArgumentException()); .thenThrow(new IllegalArgumentException());
final Vpn vpn = startLegacyVpn(createVpn(primaryUser.id), mVpnProfile); final Vpn vpn = startLegacyVpn(createVpn(PRIMARY_USER.id), mVpnProfile);
final NetworkCallback cb = triggerOnAvailableAndGetCallback(); final NetworkCallback cb = triggerOnAvailableAndGetCallback();
verifyInterfaceSetCfgWithFlags(IF_STATE_UP); verifyInterfaceSetCfgWithFlags(IF_STATE_UP);
@@ -1532,18 +1502,18 @@ public class VpnTest {
eq(AppOpsManager.MODE_ALLOWED)); eq(AppOpsManager.MODE_ALLOWED));
verify(mSystemServices).settingsSecurePutStringForUser( verify(mSystemServices).settingsSecurePutStringForUser(
eq(Settings.Secure.ALWAYS_ON_VPN_APP), eq(TEST_VPN_PKG), eq(primaryUser.id)); eq(Settings.Secure.ALWAYS_ON_VPN_APP), eq(TEST_VPN_PKG), eq(PRIMARY_USER.id));
verify(mSystemServices).settingsSecurePutIntForUser( verify(mSystemServices).settingsSecurePutIntForUser(
eq(Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN), eq(lockdownEnabled ? 1 : 0), eq(Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN), eq(lockdownEnabled ? 1 : 0),
eq(primaryUser.id)); eq(PRIMARY_USER.id));
verify(mSystemServices).settingsSecurePutStringForUser( verify(mSystemServices).settingsSecurePutStringForUser(
eq(Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN_WHITELIST), eq(""), eq(primaryUser.id)); eq(Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN_WHITELIST), eq(""), eq(PRIMARY_USER.id));
} }
@Test @Test
public void testSetAndStartAlwaysOnVpn() throws Exception { public void testSetAndStartAlwaysOnVpn() throws Exception {
final Vpn vpn = createVpn(primaryUser.id); final Vpn vpn = createVpn(PRIMARY_USER.id);
setMockedUsers(primaryUser); setMockedUsers(PRIMARY_USER);
// UID checks must return a different UID; otherwise it'll be treated as already prepared. // UID checks must return a different UID; otherwise it'll be treated as already prepared.
final int uid = Process.myUid() + 1; final int uid = Process.myUid() + 1;
@@ -1560,7 +1530,7 @@ public class VpnTest {
} }
private Vpn startLegacyVpn(final Vpn vpn, final VpnProfile vpnProfile) throws Exception { private Vpn startLegacyVpn(final Vpn vpn, final VpnProfile vpnProfile) throws Exception {
setMockedUsers(primaryUser); setMockedUsers(PRIMARY_USER);
// Dummy egress interface // Dummy egress interface
final LinkProperties lp = new LinkProperties(); final LinkProperties lp = new LinkProperties();
@@ -1876,7 +1846,7 @@ public class VpnTest {
doReturn(new Network(102)).when(mConnectivityManager).registerNetworkAgent(any(), any(), doReturn(new Network(102)).when(mConnectivityManager).registerNetworkAgent(any(), any(),
any(), any(), any(), any(), anyInt()); any(), any(), any(), any(), anyInt());
final Vpn vpn = startLegacyVpn(createVpn(primaryUser.id), profile); final Vpn vpn = startLegacyVpn(createVpn(PRIMARY_USER.id), profile);
final TestDeps deps = (TestDeps) vpn.mDeps; final TestDeps deps = (TestDeps) vpn.mDeps;
// TODO: use import when this is merged in all branches and there's no merge conflict // TODO: use import when this is merged in all branches and there's no merge conflict
@@ -1928,7 +1898,7 @@ public class VpnTest {
legacyRunnerReady.open(); legacyRunnerReady.open();
return new Network(102); return new Network(102);
}); });
final Vpn vpn = startLegacyVpn(createVpn(primaryUser.id), profile); final Vpn vpn = startLegacyVpn(createVpn(PRIMARY_USER.id), profile);
final TestDeps deps = (TestDeps) vpn.mDeps; final TestDeps deps = (TestDeps) vpn.mDeps;
try { try {
// udppsk and 1701 are the values for TYPE_L2TP_IPSEC_PSK // udppsk and 1701 are the values for TYPE_L2TP_IPSEC_PSK