Merge "Applying OEM network pref to all users"

This commit is contained in:
James Mattis
2021-03-05 19:09:24 +00:00
committed by Gerrit Code Review
2 changed files with 208 additions and 29 deletions

View File

@@ -5049,10 +5049,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
private void onUserAdded(UserHandle user) { private void onUserAdded(UserHandle user) {
mPermissionMonitor.onUserAdded(user); mPermissionMonitor.onUserAdded(user);
if (mOemNetworkPreferences.getNetworkPreferences().size() > 0) {
handleSetOemNetworkPreference(mOemNetworkPreferences, null);
}
} }
private void onUserRemoved(UserHandle user) { private void onUserRemoved(UserHandle user) {
mPermissionMonitor.onUserRemoved(user); mPermissionMonitor.onUserRemoved(user);
if (mOemNetworkPreferences.getNetworkPreferences().size() > 0) {
handleSetOemNetworkPreference(mOemNetworkPreferences, null);
}
} }
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@@ -9076,7 +9082,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
try { try {
listener.onComplete(); listener.onComplete();
} catch (RemoteException e) { } catch (RemoteException e) {
loge("handleMessage.EVENT_SET_OEM_NETWORK_PREFERENCE failed", e); loge("Can't send onComplete in handleSetOemNetworkPreference", e);
} }
} }
} }
@@ -9203,6 +9209,14 @@ public class ConnectivityService extends IConnectivityManager.Stub
@NonNull final OemNetworkPreferences preference) { @NonNull final OemNetworkPreferences preference) {
final SparseArray<Set<Integer>> uids = new SparseArray<>(); final SparseArray<Set<Integer>> uids = new SparseArray<>();
final PackageManager pm = mContext.getPackageManager(); final PackageManager pm = mContext.getPackageManager();
final List<UserHandle> users =
mContext.getSystemService(UserManager.class).getUserHandles(true);
if (null == users || users.size() == 0) {
if (VDBG || DDBG) {
log("No users currently available for setting the OEM network preference.");
}
return uids;
}
for (final Map.Entry<String, Integer> entry : for (final Map.Entry<String, Integer> entry :
preference.getNetworkPreferences().entrySet()) { preference.getNetworkPreferences().entrySet()) {
@OemNetworkPreferences.OemNetworkPreference final int pref = entry.getValue(); @OemNetworkPreferences.OemNetworkPreference final int pref = entry.getValue();
@@ -9211,7 +9225,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
if (!uids.contains(pref)) { if (!uids.contains(pref)) {
uids.put(pref, new ArraySet<>()); uids.put(pref, new ArraySet<>());
} }
uids.get(pref).add(uid); for (final UserHandle ui : users) {
// Add the rules for all users as this policy is device wide.
uids.get(pref).add(UserHandle.getUid(ui, uid));
}
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
// Although this may seem like an error scenario, it is ok that uninstalled // Although this may seem like an error scenario, it is ok that uninstalled
// packages are sent on a network preference as the system will watch for // packages are sent on a network preference as the system will watch for

View File

@@ -1085,9 +1085,11 @@ public class ConnectivityServiceTest {
} }
} }
private Set<UidRange> uidRangesForUid(int uid) { private Set<UidRange> uidRangesForUids(int... uids) {
final ArraySet<UidRange> ranges = new ArraySet<>(); final ArraySet<UidRange> ranges = new ArraySet<>();
ranges.add(new UidRange(uid, uid)); for (final int uid : uids) {
ranges.add(new UidRange(uid, uid));
}
return ranges; return ranges;
} }
@@ -1217,13 +1219,13 @@ public class ConnectivityServiceTest {
public void establishForMyUid(LinkProperties lp) throws Exception { public void establishForMyUid(LinkProperties lp) throws Exception {
final int uid = Process.myUid(); final int uid = Process.myUid();
establish(lp, uid, uidRangesForUid(uid), true, true, false); establish(lp, uid, uidRangesForUids(uid), true, true, false);
} }
public void establishForMyUid(boolean validated, boolean hasInternet, boolean isStrictMode) public void establishForMyUid(boolean validated, boolean hasInternet, boolean isStrictMode)
throws Exception { throws Exception {
final int uid = Process.myUid(); final int uid = Process.myUid();
establish(makeLinkProperties(), uid, uidRangesForUid(uid), validated, hasInternet, establish(makeLinkProperties(), uid, uidRangesForUids(uid), validated, hasInternet,
isStrictMode); isStrictMode);
} }
@@ -1332,7 +1334,7 @@ public class ConnectivityServiceTest {
} }
private void processBroadcastForVpn(Intent intent) { private void processBroadcast(Intent intent) {
mServiceContext.sendBroadcast(intent); mServiceContext.sendBroadcast(intent);
HandlerUtils.waitForIdle(mVMSHandlerThread, TIMEOUT_MS); HandlerUtils.waitForIdle(mVMSHandlerThread, TIMEOUT_MS);
waitForIdle(); waitForIdle();
@@ -1423,6 +1425,7 @@ public class ConnectivityServiceTest {
private static final int VPN_UID = UserHandle.getUid(PRIMARY_USER, 10043); private static final int VPN_UID = UserHandle.getUid(PRIMARY_USER, 10043);
private static final UserInfo PRIMARY_USER_INFO = new UserInfo(PRIMARY_USER, "", private static final UserInfo PRIMARY_USER_INFO = new UserInfo(PRIMARY_USER, "",
UserInfo.FLAG_PRIMARY); UserInfo.FLAG_PRIMARY);
private static final UserHandle PRIMARY_USER_HANDLE = new UserHandle(PRIMARY_USER);
private static final int RESTRICTED_USER = 1; private static final int RESTRICTED_USER = 1;
private static final UserInfo RESTRICTED_USER_INFO = new UserInfo(RESTRICTED_USER, "", private static final UserInfo RESTRICTED_USER_INFO = new UserInfo(RESTRICTED_USER, "",
@@ -1440,6 +1443,8 @@ public class ConnectivityServiceTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(mUserManager.getAliveUsers()).thenReturn(Arrays.asList(PRIMARY_USER_INFO)); when(mUserManager.getAliveUsers()).thenReturn(Arrays.asList(PRIMARY_USER_INFO));
when(mUserManager.getUserHandles(anyBoolean())).thenReturn(
Arrays.asList(PRIMARY_USER_HANDLE));
when(mUserManager.getUserInfo(PRIMARY_USER)).thenReturn(PRIMARY_USER_INFO); when(mUserManager.getUserInfo(PRIMARY_USER)).thenReturn(PRIMARY_USER_INFO);
// canHaveRestrictedProfile does not take a userId. It applies to the userId of the context // canHaveRestrictedProfile does not take a userId. It applies to the userId of the context
// it was started from, i.e., PRIMARY_USER. // it was started from, i.e., PRIMARY_USER.
@@ -6390,7 +6395,7 @@ public class ConnectivityServiceTest {
vpnNetworkCallback.assertNoCallback(); vpnNetworkCallback.assertNoCallback();
assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork()); assertEquals(defaultCallback.getLastAvailableNetwork(), mCm.getActiveNetwork());
final Set<UidRange> ranges = uidRangesForUid(uid); final Set<UidRange> ranges = uidRangesForUids(uid);
mMockVpn.registerAgent(ranges); mMockVpn.registerAgent(ranges);
mMockVpn.setUnderlyingNetworks(new Network[0]); mMockVpn.setUnderlyingNetworks(new Network[0]);
@@ -6862,7 +6867,7 @@ public class ConnectivityServiceTest {
final int uid = Process.myUid(); final int uid = Process.myUid();
NetworkCapabilities nc = mCm.getNetworkCapabilities(mMockVpn.getNetwork()); NetworkCapabilities nc = mCm.getNetworkCapabilities(mMockVpn.getNetwork());
assertNotNull("nc=" + nc, nc.getUids()); assertNotNull("nc=" + nc, nc.getUids());
assertEquals(nc.getUids(), uidRangesForUid(uid)); assertEquals(nc.getUids(), uidRangesForUids(uid));
assertVpnTransportInfo(nc, VpnManager.TYPE_VPN_SERVICE); assertVpnTransportInfo(nc, VpnManager.TYPE_VPN_SERVICE);
// Set an underlying network and expect to see the VPN transports change. // Set an underlying network and expect to see the VPN transports change.
@@ -6883,7 +6888,7 @@ public class ConnectivityServiceTest {
addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER); addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER);
// Send a USER_ADDED broadcast for it. // Send a USER_ADDED broadcast for it.
processBroadcastForVpn(addedIntent); processBroadcast(addedIntent);
// Expect that the VPN UID ranges contain both |uid| and the UID range for the newly-added // Expect that the VPN UID ranges contain both |uid| and the UID range for the newly-added
// restricted user. // restricted user.
@@ -6908,7 +6913,7 @@ public class ConnectivityServiceTest {
final Intent removedIntent = new Intent(ACTION_USER_REMOVED); final Intent removedIntent = new Intent(ACTION_USER_REMOVED);
removedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER)); removedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER));
removedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER); removedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER);
processBroadcastForVpn(removedIntent); processBroadcast(removedIntent);
// Expect that the VPN gains the UID range for the restricted user, and that the capability // 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. // change made just before that (i.e., loss of TRANSPORT_WIFI) is preserved.
@@ -6966,7 +6971,7 @@ public class ConnectivityServiceTest {
final Intent addedIntent = new Intent(ACTION_USER_ADDED); final Intent addedIntent = new Intent(ACTION_USER_ADDED);
addedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER)); addedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER));
addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER); addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER);
processBroadcastForVpn(addedIntent); processBroadcast(addedIntent);
assertNull(mCm.getActiveNetworkForUid(uid)); assertNull(mCm.getActiveNetworkForUid(uid));
assertNull(mCm.getActiveNetworkForUid(restrictedUid)); assertNull(mCm.getActiveNetworkForUid(restrictedUid));
@@ -6977,7 +6982,7 @@ public class ConnectivityServiceTest {
final Intent removedIntent = new Intent(ACTION_USER_REMOVED); final Intent removedIntent = new Intent(ACTION_USER_REMOVED);
removedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER)); removedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(RESTRICTED_USER));
removedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER); removedIntent.putExtra(Intent.EXTRA_USER_HANDLE, RESTRICTED_USER);
processBroadcastForVpn(removedIntent); processBroadcast(removedIntent);
assertNull(mCm.getActiveNetworkForUid(uid)); assertNull(mCm.getActiveNetworkForUid(uid));
assertNotNull(mCm.getActiveNetworkForUid(restrictedUid)); assertNotNull(mCm.getActiveNetworkForUid(restrictedUid));
@@ -7132,7 +7137,7 @@ public class ConnectivityServiceTest {
assertFalse(mCm.isActiveNetworkMetered()); assertFalse(mCm.isActiveNetworkMetered());
// Connect VPN network. // Connect VPN network.
mMockVpn.registerAgent(true /* isAlwaysMetered */, uidRangesForUid(Process.myUid()), mMockVpn.registerAgent(true /* isAlwaysMetered */, uidRangesForUids(Process.myUid()),
new LinkProperties()); new LinkProperties());
mMockVpn.connect(true); mMockVpn.connect(true);
waitForIdle(); waitForIdle();
@@ -7571,7 +7576,7 @@ public class ConnectivityServiceTest {
final Intent addedIntent = new Intent(ACTION_USER_UNLOCKED); final Intent addedIntent = new Intent(ACTION_USER_UNLOCKED);
addedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(userId)); addedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(userId));
addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
processBroadcastForVpn(addedIntent); processBroadcast(addedIntent);
// Lockdown VPN disables teardown and enables lockdown. // Lockdown VPN disables teardown and enables lockdown.
assertFalse(mMockVpn.getEnableTeardown()); assertFalse(mMockVpn.getEnableTeardown());
@@ -9290,7 +9295,7 @@ public class ConnectivityServiceTest {
private void assertUidRangesUpdatedForMyUid(boolean add) throws Exception { private void assertUidRangesUpdatedForMyUid(boolean add) throws Exception {
final int uid = Process.myUid(); final int uid = Process.myUid();
assertVpnUidRangesUpdated(add, uidRangesForUid(uid), uid); assertVpnUidRangesUpdated(add, uidRangesForUids(uid), uid);
} }
private void assertVpnUidRangesUpdated(boolean add, Set<UidRange> vpnRanges, int exemptUid) private void assertVpnUidRangesUpdated(boolean add, Set<UidRange> vpnRanges, int exemptUid)
@@ -9679,7 +9684,7 @@ public class ConnectivityServiceTest {
} }
@Test @Test
public void testOemNetworkRequestFactoryCorrectlySetsUids() public void testOemNetworkRequestFactoryMultiplePrefsCorrectlySetsUids()
throws Exception { throws Exception {
// Arrange PackageManager mocks // Arrange PackageManager mocks
final String testPackageName2 = "com.google.apps.dialer"; final String testPackageName2 = "com.google.apps.dialer";
@@ -9709,6 +9714,46 @@ public class ConnectivityServiceTest {
assertEquals(testPackageNameUid2, getNriFirstUidRange(nris.get(1)).stop); assertEquals(testPackageNameUid2, getNriFirstUidRange(nris.get(1)).stop);
} }
@Test
public void testOemNetworkRequestFactoryMultipleUsersCorrectlySetsUids()
throws Exception {
// Arrange users
final int secondUser = 10;
final UserHandle secondUserHandle = new UserHandle(secondUser);
when(mUserManager.getUserHandles(anyBoolean())).thenReturn(
Arrays.asList(PRIMARY_USER_HANDLE, secondUserHandle));
// Arrange PackageManager mocks
mockGetApplicationInfo(TEST_PACKAGE_NAME, TEST_PACKAGE_UID);
// Build OemNetworkPreferences object
final int testOemPref = OEM_NETWORK_PREFERENCE_OEM_PAID;
final OemNetworkPreferences pref = new OemNetworkPreferences.Builder()
.addNetworkPreference(TEST_PACKAGE_NAME, testOemPref)
.build();
// Act on OemNetworkRequestFactory.createNrisFromOemNetworkPreferences()
final List<ConnectivityService.NetworkRequestInfo> nris =
new ArrayList<>(
mService.new OemNetworkRequestFactory().createNrisFromOemNetworkPreferences(
pref));
// UIDs for all users and all managed packages should be present.
// Two users each with two packages.
final int expectedUidSize = 2;
final List<UidRange> uids =
new ArrayList<>(nris.get(0).mRequests.get(0).networkCapabilities.getUids());
assertEquals(expectedUidSize, uids.size());
// Sort by uid to access nris by index
uids.sort(Comparator.comparingInt(uid -> uid.start));
final int secondUserTestPackageUid = UserHandle.getUid(secondUser, TEST_PACKAGE_UID);
assertEquals(TEST_PACKAGE_UID, uids.get(0).start);
assertEquals(TEST_PACKAGE_UID, uids.get(0).stop);
assertEquals(secondUserTestPackageUid, uids.get(1).start);
assertEquals(secondUserTestPackageUid, uids.get(1).stop);
}
@Test @Test
public void testOemNetworkRequestFactoryAddsPackagesToCorrectPreference() public void testOemNetworkRequestFactoryAddsPackagesToCorrectPreference()
throws Exception { throws Exception {
@@ -9914,7 +9959,7 @@ public class ConnectivityServiceTest {
assertEquals(1, mService.mDefaultNetworkRequests.size()); assertEquals(1, mService.mDefaultNetworkRequests.size());
final UidRangeParcel[] uidRanges = final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(uidRangesForUid(testPackageUid)); toUidRangeStableParcels(uidRangesForUids(testPackageUid));
setupSetOemNetworkPreferenceForPreferenceTest( setupSetOemNetworkPreferenceForPreferenceTest(
networkPrefToSetup, uidRanges, testPackageName); networkPrefToSetup, uidRanges, testPackageName);
} }
@@ -10144,6 +10189,10 @@ public class ConnectivityServiceTest {
mCm.unregisterNetworkCallback(defaultNetworkCallback); mCm.unregisterNetworkCallback(defaultNetworkCallback);
} }
/**
* This method assumes that the same uidRanges input will be used to verify that dependencies
* are called as expected.
*/
private void verifySetOemNetworkPreferenceForPreference( private void verifySetOemNetworkPreferenceForPreference(
@NonNull final UidRangeParcel[] uidRanges, @NonNull final UidRangeParcel[] uidRanges,
final int addUidRangesNetId, final int addUidRangesNetId,
@@ -10151,16 +10200,30 @@ public class ConnectivityServiceTest {
final int removeUidRangesNetId, final int removeUidRangesNetId,
final int removeUidRangesTimes, final int removeUidRangesTimes,
final boolean shouldDestroyNetwork) throws RemoteException { final boolean shouldDestroyNetwork) throws RemoteException {
verifySetOemNetworkPreferenceForPreference(uidRanges, uidRanges,
addUidRangesNetId, addUidRangesTimes, removeUidRangesNetId, removeUidRangesTimes,
shouldDestroyNetwork);
}
private void verifySetOemNetworkPreferenceForPreference(
@NonNull final UidRangeParcel[] addedUidRanges,
@NonNull final UidRangeParcel[] removedUidRanges,
final int addUidRangesNetId,
final int addUidRangesTimes,
final int removeUidRangesNetId,
final int removeUidRangesTimes,
final boolean shouldDestroyNetwork) throws RemoteException {
final boolean useAnyIdForAdd = OEM_PREF_ANY_NET_ID == addUidRangesNetId; final boolean useAnyIdForAdd = OEM_PREF_ANY_NET_ID == addUidRangesNetId;
final boolean useAnyIdForRemove = OEM_PREF_ANY_NET_ID == removeUidRangesNetId; final boolean useAnyIdForRemove = OEM_PREF_ANY_NET_ID == removeUidRangesNetId;
// Validate netd. // Validate netd.
verify(mMockNetd, times(addUidRangesTimes)) verify(mMockNetd, times(addUidRangesTimes))
.networkAddUidRanges( .networkAddUidRanges(
(useAnyIdForAdd ? anyInt() : eq(addUidRangesNetId)), eq(uidRanges)); (useAnyIdForAdd ? anyInt() : eq(addUidRangesNetId)), eq(addedUidRanges));
verify(mMockNetd, times(removeUidRangesTimes)) verify(mMockNetd, times(removeUidRangesTimes))
.networkRemoveUidRanges( .networkRemoveUidRanges(
(useAnyIdForRemove ? anyInt() : eq(removeUidRangesNetId)), eq(uidRanges)); (useAnyIdForRemove ? anyInt() : eq(removeUidRangesNetId)),
eq(removedUidRanges));
if (shouldDestroyNetwork) { if (shouldDestroyNetwork) {
verify(mMockNetd, times(1)) verify(mMockNetd, times(1))
.networkDestroy((useAnyIdForRemove ? anyInt() : eq(removeUidRangesNetId))); .networkDestroy((useAnyIdForRemove ? anyInt() : eq(removeUidRangesNetId)));
@@ -10178,7 +10241,7 @@ public class ConnectivityServiceTest {
final int testPackageUid = 123; final int testPackageUid = 123;
final String testPackageName = "com.google.apps.contacts"; final String testPackageName = "com.google.apps.contacts";
final UidRangeParcel[] uidRanges = final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(uidRangesForUid(testPackageUid)); toUidRangeStableParcels(uidRangesForUids(testPackageUid));
// Validate the starting requests only includes the fallback request. // Validate the starting requests only includes the fallback request.
assertEquals(1, mService.mDefaultNetworkRequests.size()); assertEquals(1, mService.mDefaultNetworkRequests.size());
@@ -10207,9 +10270,8 @@ public class ConnectivityServiceTest {
OEM_NETWORK_PREFERENCE_OEM_PAID; OEM_NETWORK_PREFERENCE_OEM_PAID;
// Arrange PackageManager mocks // Arrange PackageManager mocks
final int testPackageNameUid = 123;
final UidRangeParcel[] uidRanges = final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(uidRangesForUid(testPackageNameUid)); toUidRangeStableParcels(uidRangesForUids(TEST_PACKAGE_UID));
setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME); setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME);
// Verify the starting state. No networks should be connected. // Verify the starting state. No networks should be connected.
@@ -10274,9 +10336,8 @@ public class ConnectivityServiceTest {
OEM_NETWORK_PREFERENCE_OEM_PAID_NO_FALLBACK; OEM_NETWORK_PREFERENCE_OEM_PAID_NO_FALLBACK;
// Arrange PackageManager mocks // Arrange PackageManager mocks
final int testPackageNameUid = 123;
final UidRangeParcel[] uidRanges = final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(uidRangesForUid(testPackageNameUid)); toUidRangeStableParcels(uidRangesForUids(TEST_PACKAGE_UID));
setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME); setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME);
// Verify the starting state. This preference doesn't support using the fallback network // Verify the starting state. This preference doesn't support using the fallback network
@@ -10337,9 +10398,8 @@ public class ConnectivityServiceTest {
OEM_NETWORK_PREFERENCE_OEM_PAID_ONLY; OEM_NETWORK_PREFERENCE_OEM_PAID_ONLY;
// Arrange PackageManager mocks // Arrange PackageManager mocks
final int testPackageNameUid = 123;
final UidRangeParcel[] uidRanges = final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(uidRangesForUid(testPackageNameUid)); toUidRangeStableParcels(uidRangesForUids(TEST_PACKAGE_UID));
setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME); setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME);
// Verify the starting state. This preference doesn't support using the fallback network // Verify the starting state. This preference doesn't support using the fallback network
@@ -10390,9 +10450,8 @@ public class ConnectivityServiceTest {
OEM_NETWORK_PREFERENCE_OEM_PRIVATE_ONLY; OEM_NETWORK_PREFERENCE_OEM_PRIVATE_ONLY;
// Arrange PackageManager mocks // Arrange PackageManager mocks
final int testPackageNameUid = 123;
final UidRangeParcel[] uidRanges = final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(uidRangesForUid(testPackageNameUid)); toUidRangeStableParcels(uidRangesForUids(TEST_PACKAGE_UID));
setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME); setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME);
// Verify the starting state. This preference doesn't support using the fallback network // Verify the starting state. This preference doesn't support using the fallback network
@@ -10431,6 +10490,109 @@ public class ConnectivityServiceTest {
true /* shouldDestroyNetwork */); true /* shouldDestroyNetwork */);
} }
@Test
public void testMultilayerForMultipleUsersEvaluatesCorrectly()
throws Exception {
@OemNetworkPreferences.OemNetworkPreference final int networkPref =
OEM_NETWORK_PREFERENCE_OEM_PAID;
// Arrange users
final int secondUser = 10;
final UserHandle secondUserHandle = new UserHandle(secondUser);
when(mUserManager.getUserHandles(anyBoolean())).thenReturn(
Arrays.asList(PRIMARY_USER_HANDLE, secondUserHandle));
// Arrange PackageManager mocks
final int secondUserTestPackageUid = UserHandle.getUid(secondUser, TEST_PACKAGE_UID);
final UidRangeParcel[] uidRanges =
toUidRangeStableParcels(
uidRangesForUids(TEST_PACKAGE_UID, secondUserTestPackageUid));
setupSetOemNetworkPreferenceForPreferenceTest(networkPref, uidRanges, TEST_PACKAGE_NAME);
// Verify the starting state. No networks should be connected.
verifySetOemNetworkPreferenceForPreference(uidRanges,
OEM_PREF_ANY_NET_ID, 0 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
// Test that we correctly add the expected values for multiple users.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifySetOemNetworkPreferenceForPreference(uidRanges,
mCellNetworkAgent.getNetwork().netId, 1 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
// Test that we correctly remove the expected values for multiple users.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, false);
verifySetOemNetworkPreferenceForPreference(uidRanges,
OEM_PREF_ANY_NET_ID, 0 /* times */,
mCellNetworkAgent.getNetwork().netId, 0 /* times */,
true /* shouldDestroyNetwork */);
}
@Test
public void testMultilayerForBroadcastedUsersEvaluatesCorrectly()
throws Exception {
@OemNetworkPreferences.OemNetworkPreference final int networkPref =
OEM_NETWORK_PREFERENCE_OEM_PAID;
// Arrange users
final int secondUser = 10;
final UserHandle secondUserHandle = new UserHandle(secondUser);
when(mUserManager.getUserHandles(anyBoolean())).thenReturn(
Arrays.asList(PRIMARY_USER_HANDLE));
// Arrange PackageManager mocks
final int secondUserTestPackageUid = UserHandle.getUid(secondUser, TEST_PACKAGE_UID);
final UidRangeParcel[] uidRangesSingleUser =
toUidRangeStableParcels(
uidRangesForUids(TEST_PACKAGE_UID));
final UidRangeParcel[] uidRangesBothUsers =
toUidRangeStableParcels(
uidRangesForUids(TEST_PACKAGE_UID, secondUserTestPackageUid));
setupSetOemNetworkPreferenceForPreferenceTest(
networkPref, uidRangesSingleUser, TEST_PACKAGE_NAME);
// Verify the starting state. No networks should be connected.
verifySetOemNetworkPreferenceForPreference(uidRangesSingleUser,
OEM_PREF_ANY_NET_ID, 0 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
// Test that we correctly add the expected values for multiple users.
setOemNetworkPreferenceAgentConnected(TRANSPORT_CELLULAR, true);
verifySetOemNetworkPreferenceForPreference(uidRangesSingleUser,
mCellNetworkAgent.getNetwork().netId, 1 /* times */,
OEM_PREF_ANY_NET_ID, 0 /* times */,
false /* shouldDestroyNetwork */);
// Send a broadcast indicating a user was added.
when(mUserManager.getUserHandles(anyBoolean())).thenReturn(
Arrays.asList(PRIMARY_USER_HANDLE, secondUserHandle));
final Intent addedIntent = new Intent(ACTION_USER_ADDED);
addedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(secondUser));
processBroadcast(addedIntent);
// Test that we correctly add values for all users and remove for the single user.
verifySetOemNetworkPreferenceForPreference(uidRangesBothUsers, uidRangesSingleUser,
mCellNetworkAgent.getNetwork().netId, 1 /* times */,
mCellNetworkAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
// Send a broadcast indicating a user was removed.
when(mUserManager.getUserHandles(anyBoolean())).thenReturn(
Arrays.asList(PRIMARY_USER_HANDLE));
final Intent removedIntent = new Intent(ACTION_USER_REMOVED);
removedIntent.putExtra(Intent.EXTRA_USER, UserHandle.of(secondUser));
processBroadcast(removedIntent);
// Test that we correctly add values for the single user and remove for the all users.
verifySetOemNetworkPreferenceForPreference(uidRangesSingleUser, uidRangesBothUsers,
mCellNetworkAgent.getNetwork().netId, 1 /* times */,
mCellNetworkAgent.getNetwork().netId, 1 /* times */,
false /* shouldDestroyNetwork */);
}
/** /**
* Test network priority for preference OEM_NETWORK_PREFERENCE_OEM_PAID in the following order: * Test network priority for preference OEM_NETWORK_PREFERENCE_OEM_PAID in the following order:
* NET_CAPABILITY_NOT_METERED -> NET_CAPABILITY_OEM_PAID -> fallback * NET_CAPABILITY_NOT_METERED -> NET_CAPABILITY_OEM_PAID -> fallback