Mock onUserAdded() and onUserRemoved()
Replace calls to onUserAdded() and onUserRemoved() to their
equivalent setUids() or setRequireVpnForUids() calls.
Note coverage for the calls to Vpn were added in VpnTest.
Bug: 230548427
Test: atest FrameworksNetTests
Change-Id: Ifa895f71f78bd3376216fd2759c7a5a33cd3aff1
diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
index 2618f57..d44644a 100755
--- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java
+++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java
@@ -1894,6 +1894,8 @@
private static final UserHandle TERTIARY_USER_HANDLE = new UserHandle(TERTIARY_USER);
private static final int RESTRICTED_USER = 1;
+ private static final UidRange RESTRICTED_USER_UIDRANGE =
+ UidRange.createForUser(UserHandle.of(RESTRICTED_USER));
private static final UserInfo RESTRICTED_USER_INFO = new UserInfo(RESTRICTED_USER, "",
UserInfo.FLAG_RESTRICTED);
static {
@@ -9405,11 +9407,11 @@
&& c.hasTransport(TRANSPORT_WIFI));
callback.expectCaps(mWiFiAgent, c -> c.hasCapability(NET_CAPABILITY_VALIDATED));
- doReturn(UserHandle.getUid(RESTRICTED_USER, VPN_UID)).when(mPackageManager)
- .getPackageUidAsUser(ALWAYS_ON_PACKAGE, RESTRICTED_USER);
-
- // New user added
- mMockVpn.onUserAdded(RESTRICTED_USER);
+ // New user added, this updates the Vpn uids, coverage in VpnTest.
+ // This is equivalent to `mMockVpn.onUserAdded(RESTRICTED_USER);`
+ final Set<UidRange> ranges = uidRangesForUids(uid);
+ ranges.add(RESTRICTED_USER_UIDRANGE);
+ mMockVpn.setUids(ranges);
// Expect that the VPN UID ranges contain both |uid| and the UID range for the newly-added
// restricted user.
@@ -9434,7 +9436,9 @@
&& !c.hasTransport(TRANSPORT_WIFI));
// User removed and expect to lose the UID range for the restricted user.
- mMockVpn.onUserRemoved(RESTRICTED_USER);
+ // This updates the Vpn uids, coverage in VpnTest.
+ // This is equivalent to `mMockVpn.onUserRemoved(RESTRICTED_USER);`
+ mMockVpn.setUids(uidRangesForUids(uid));
// Expect that the VPN gains the UID range for the restricted user, and that the capability
// change made just before that (i.e., loss of TRANSPORT_WIFI) is preserved.
@@ -9485,28 +9489,29 @@
assertNotNull(mCm.getActiveNetworkForUid(restrictedUid));
// Start the restricted profile, and check that the UID within it loses network access.
- doReturn(UserHandle.getUid(RESTRICTED_USER, VPN_UID)).when(mPackageManager)
- .getPackageUidAsUser(ALWAYS_ON_PACKAGE, RESTRICTED_USER);
- doReturn(asList(PRIMARY_USER_INFO, RESTRICTED_USER_INFO)).when(mUserManager)
- .getAliveUsers();
// TODO: check that VPN app within restricted profile still has access, etc.
- mMockVpn.onUserAdded(RESTRICTED_USER);
- final Intent addedIntent = new Intent(ACTION_USER_ADDED);
- addedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER));
- addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER);
- processBroadcast(addedIntent);
+ // Add a restricted user.
+ // This is equivalent to `mMockVpn.onUserAdded(RESTRICTED_USER);`, coverage in VpnTest.
+ final List<Integer> excludedUids = new ArrayList<Integer>();
+ excludedUids.add(VPN_UID);
+ if (mDeps.isAtLeastT()) {
+ // On T onwards, the corresponding SDK sandbox UID should also be excluded
+ excludedUids.add(toSdkSandboxUid(VPN_UID));
+ }
+ final List<Range<Integer>> restrictedRanges =
+ intRangesExcludingUids(RESTRICTED_USER, excludedUids);
+ mCm.setRequireVpnForUids(true, restrictedRanges);
+ waitForIdle();
+
assertNull(mCm.getActiveNetworkForUid(uid));
assertNull(mCm.getActiveNetworkForUid(restrictedUid));
// Stop the restricted profile, and check that the UID within it has network access again.
- doReturn(asList(PRIMARY_USER_INFO)).when(mUserManager).getAliveUsers();
+ // Remove the restricted user.
+ // This is equivalent to `mMockVpn.onUserRemoved(RESTRICTED_USER);`, coverage in VpnTest.
+ mCm.setRequireVpnForUids(false, restrictedRanges);
+ waitForIdle();
- // Send a USER_REMOVED broadcast and expect to lose the UID range for the restricted user.
- mMockVpn.onUserRemoved(RESTRICTED_USER);
- final Intent removedIntent = new Intent(ACTION_USER_REMOVED);
- removedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER));
- removedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER);
- processBroadcast(removedIntent);
assertNull(mCm.getActiveNetworkForUid(uid));
assertNotNull(mCm.getActiveNetworkForUid(restrictedUid));