5353import android .content .pm .PackageInfo ;
5454import android .content .pm .PackageManager ;
5555import android .content .pm .Signature ;
56+ import android .content .pm .UserInfo ;
5657import android .net .ConnectivityManager ;
5758import android .net .IConnectivityManager ;
5859import android .net .INetworkManagementEventObserver ;
6970import android .os .INetworkManagementService ;
7071import android .os .IPowerManager ;
7172import android .os .MessageQueue .IdleHandler ;
73+ import android .os .UserId ;
7274import android .test .AndroidTestCase ;
7375import android .test .mock .MockPackageManager ;
7476import android .test .suitebuilder .annotation .LargeTest ;
8486import org .easymock .IAnswer ;
8587
8688import java .io .File ;
89+ import java .util .ArrayList ;
8790import java .util .LinkedHashSet ;
91+ import java .util .List ;
8892import java .util .concurrent .ExecutionException ;
8993import java .util .concurrent .Future ;
9094import java .util .concurrent .TimeUnit ;
@@ -126,8 +130,16 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
126130 private long mStartTime ;
127131 private long mElapsedRealtime ;
128132
129- private static final int UID_A = android .os .Process .FIRST_APPLICATION_UID + 800 ;
130- private static final int UID_B = android .os .Process .FIRST_APPLICATION_UID + 801 ;
133+ private static final int USER_ID = 0 ;
134+ private static final int USER_ID_GUEST = 1 ;
135+
136+ private static final int APP_ID_A = android .os .Process .FIRST_APPLICATION_UID + 800 ;
137+ private static final int APP_ID_B = android .os .Process .FIRST_APPLICATION_UID + 801 ;
138+
139+ private static final int UID_A = UserId .getUid (USER_ID , APP_ID_A );
140+ private static final int UID_B = UserId .getUid (USER_ID , APP_ID_B );
141+ private static final int UID_A_GUEST = UserId .getUid (USER_ID_GUEST , APP_ID_A );
142+ private static final int UID_B_GUEST = UserId .getUid (USER_ID_GUEST , APP_ID_B );
131143
132144 private static final int PID_1 = 400 ;
133145 private static final int PID_2 = 401 ;
@@ -161,6 +173,14 @@ public PackageInfo getPackageInfo(String packageName, int flags) {
161173 info .signatures = new Signature [] { signature };
162174 return info ;
163175 }
176+
177+ @ Override
178+ public List <UserInfo > getUsers () {
179+ final ArrayList <UserInfo > users = new ArrayList <UserInfo >();
180+ users .add (new UserInfo (USER_ID , "Primary" , UserInfo .FLAG_PRIMARY ));
181+ users .add (new UserInfo (USER_ID_GUEST , "Guest" , 0 ));
182+ return users ;
183+ }
164184 };
165185 }
166186
@@ -242,13 +262,13 @@ public void tearDown() throws Exception {
242262
243263 @ Suppress
244264 public void testPolicyChangeTriggersBroadcast () throws Exception {
245- mService .setAppPolicy (UID_A , POLICY_NONE );
265+ mService .setAppPolicy (APP_ID_A , POLICY_NONE );
246266
247267 // change background policy and expect broadcast
248268 final Future <Intent > backgroundChanged = mServiceContext .nextBroadcastIntent (
249269 ConnectivityManager .ACTION_BACKGROUND_DATA_SETTING_CHANGED );
250270
251- mService .setAppPolicy (UID_A , POLICY_REJECT_METERED_BACKGROUND );
271+ mService .setAppPolicy (APP_ID_A , POLICY_REJECT_METERED_BACKGROUND );
252272
253273 backgroundChanged .get ();
254274 }
@@ -302,6 +322,7 @@ public void testPidForegroundCombined() throws Exception {
302322
303323 public void testScreenChangesRules () throws Exception {
304324 Future <Void > future ;
325+ Future <Void > futureGuest ;
305326
306327 expectSetUidNetworkRules (UID_A , false );
307328 expectSetUidForeground (UID_A , true );
@@ -314,10 +335,14 @@ public void testScreenChangesRules() throws Exception {
314335 // push strict policy for foreground uid, verify ALLOW rule
315336 expectSetUidNetworkRules (UID_A , false );
316337 expectSetUidForeground (UID_A , true );
338+ expectSetUidNetworkRules (UID_A_GUEST , true );
339+ expectSetUidForeground (UID_A_GUEST , false );
317340 future = expectRulesChanged (UID_A , RULE_ALLOW_ALL );
341+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_REJECT_METERED );
318342 replay ();
319- mService .setAppPolicy (UID_A , POLICY_REJECT_METERED_BACKGROUND );
343+ mService .setAppPolicy (APP_ID_A , POLICY_REJECT_METERED_BACKGROUND );
320344 future .get ();
345+ futureGuest .get ();
321346 verifyAndReset ();
322347
323348 // now turn screen off and verify REJECT rule
@@ -343,6 +368,7 @@ public void testScreenChangesRules() throws Exception {
343368
344369 public void testPolicyNone () throws Exception {
345370 Future <Void > future ;
371+ Future <Void > futureGuest ;
346372
347373 expectSetUidNetworkRules (UID_A , false );
348374 expectSetUidForeground (UID_A , true );
@@ -355,10 +381,14 @@ public void testPolicyNone() throws Exception {
355381 // POLICY_NONE should RULE_ALLOW in foreground
356382 expectSetUidNetworkRules (UID_A , false );
357383 expectSetUidForeground (UID_A , true );
384+ expectSetUidNetworkRules (UID_A_GUEST , false );
385+ expectSetUidForeground (UID_A_GUEST , false );
358386 future = expectRulesChanged (UID_A , RULE_ALLOW_ALL );
387+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_ALLOW_ALL );
359388 replay ();
360- mService .setAppPolicy (UID_A , POLICY_NONE );
389+ mService .setAppPolicy (APP_ID_A , POLICY_NONE );
361390 future .get ();
391+ futureGuest .get ();
362392 verifyAndReset ();
363393
364394 // POLICY_NONE should RULE_ALLOW in background
@@ -373,14 +403,19 @@ public void testPolicyNone() throws Exception {
373403
374404 public void testPolicyReject () throws Exception {
375405 Future <Void > future ;
406+ Future <Void > futureGuest ;
376407
377408 // POLICY_REJECT should RULE_ALLOW in background
378409 expectSetUidNetworkRules (UID_A , true );
379410 expectSetUidForeground (UID_A , false );
411+ expectSetUidNetworkRules (UID_A_GUEST , true );
412+ expectSetUidForeground (UID_A_GUEST , false );
380413 future = expectRulesChanged (UID_A , RULE_REJECT_METERED );
414+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_REJECT_METERED );
381415 replay ();
382- mService .setAppPolicy (UID_A , POLICY_REJECT_METERED_BACKGROUND );
416+ mService .setAppPolicy (APP_ID_A , POLICY_REJECT_METERED_BACKGROUND );
383417 future .get ();
418+ futureGuest .get ();
384419 verifyAndReset ();
385420
386421 // POLICY_REJECT should RULE_ALLOW in foreground
@@ -404,33 +439,46 @@ public void testPolicyReject() throws Exception {
404439
405440 public void testPolicyRejectAddRemove () throws Exception {
406441 Future <Void > future ;
442+ Future <Void > futureGuest ;
407443
408444 // POLICY_NONE should have RULE_ALLOW in background
409445 expectSetUidNetworkRules (UID_A , false );
410446 expectSetUidForeground (UID_A , false );
447+ expectSetUidNetworkRules (UID_A_GUEST , false );
448+ expectSetUidForeground (UID_A_GUEST , false );
411449 future = expectRulesChanged (UID_A , RULE_ALLOW_ALL );
450+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_ALLOW_ALL );
412451 replay ();
413452 mProcessObserver .onForegroundActivitiesChanged (PID_1 , UID_A , false );
414- mService .setAppPolicy (UID_A , POLICY_NONE );
453+ mService .setAppPolicy (APP_ID_A , POLICY_NONE );
415454 future .get ();
455+ futureGuest .get ();
416456 verifyAndReset ();
417457
418458 // adding POLICY_REJECT should cause RULE_REJECT
419459 expectSetUidNetworkRules (UID_A , true );
420460 expectSetUidForeground (UID_A , false );
461+ expectSetUidNetworkRules (UID_A_GUEST , true );
462+ expectSetUidForeground (UID_A_GUEST , false );
421463 future = expectRulesChanged (UID_A , RULE_REJECT_METERED );
464+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_REJECT_METERED );
422465 replay ();
423- mService .setAppPolicy (UID_A , POLICY_REJECT_METERED_BACKGROUND );
466+ mService .setAppPolicy (APP_ID_A , POLICY_REJECT_METERED_BACKGROUND );
424467 future .get ();
468+ futureGuest .get ();
425469 verifyAndReset ();
426470
427471 // removing POLICY_REJECT should return us to RULE_ALLOW
428472 expectSetUidNetworkRules (UID_A , false );
429473 expectSetUidForeground (UID_A , false );
474+ expectSetUidNetworkRules (UID_A_GUEST , false );
475+ expectSetUidForeground (UID_A_GUEST , false );
430476 future = expectRulesChanged (UID_A , RULE_ALLOW_ALL );
477+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_ALLOW_ALL );
431478 replay ();
432- mService .setAppPolicy (UID_A , POLICY_NONE );
479+ mService .setAppPolicy (APP_ID_A , POLICY_NONE );
433480 future .get ();
481+ futureGuest .get ();
434482 verifyAndReset ();
435483 }
436484
@@ -599,25 +647,34 @@ public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
599647
600648 public void testUidRemovedPolicyCleared () throws Exception {
601649 Future <Void > future ;
650+ Future <Void > futureGuest ;
602651
603652 // POLICY_REJECT should RULE_REJECT in background
604653 expectSetUidNetworkRules (UID_A , true );
605654 expectSetUidForeground (UID_A , false );
655+ expectSetUidNetworkRules (UID_A_GUEST , true );
656+ expectSetUidForeground (UID_A_GUEST , false );
606657 future = expectRulesChanged (UID_A , RULE_REJECT_METERED );
658+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_REJECT_METERED );
607659 replay ();
608- mService .setAppPolicy (UID_A , POLICY_REJECT_METERED_BACKGROUND );
660+ mService .setAppPolicy (APP_ID_A , POLICY_REJECT_METERED_BACKGROUND );
609661 future .get ();
662+ futureGuest .get ();
610663 verifyAndReset ();
611664
612665 // uninstall should clear RULE_REJECT
613666 expectSetUidNetworkRules (UID_A , false );
614667 expectSetUidForeground (UID_A , false );
668+ expectSetUidNetworkRules (UID_A_GUEST , false );
669+ expectSetUidForeground (UID_A_GUEST , false );
615670 future = expectRulesChanged (UID_A , RULE_ALLOW_ALL );
671+ futureGuest = expectRulesChanged (UID_A_GUEST , RULE_ALLOW_ALL );
616672 replay ();
617673 final Intent intent = new Intent (ACTION_UID_REMOVED );
618674 intent .putExtra (EXTRA_UID , UID_A );
619675 mServiceContext .sendBroadcast (intent );
620676 future .get ();
677+ futureGuest .get ();
621678 verifyAndReset ();
622679 }
623680
0 commit comments