1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
|
/*
* Copyright (C) 2020 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.pm;
import static android.app.AppOpsManager.MODE_ALLOWED;
import static android.app.AppOpsManager.MODE_DEFAULT;
import static android.app.AppOpsManager.OP_INTERACT_ACROSS_PROFILES;
import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
import static android.content.Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND;
import static android.content.Intent.FLAG_RECEIVER_REGISTERED_ONLY;
import static android.content.pm.CrossProfileApps.ACTION_CAN_INTERACT_ACROSS_PROFILES_CHANGED;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.Manifest;
import android.annotation.UserIdInt;
import android.app.ActivityManagerInternal;
import android.app.AppOpsManager;
import android.app.AppOpsManager.Mode;
import android.app.admin.DevicePolicyManagerInternal;
import android.content.ComponentName;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManagerInternal;
import android.content.pm.PermissionInfo;
import android.content.pm.ResolveInfo;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.platform.test.annotations.Presubmit;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
import com.android.internal.util.FunctionalUtils.ThrowingSupplier;
import com.android.server.LocalServices;
import com.android.server.pm.parsing.pkg.AndroidPackage;
import com.android.server.pm.parsing.pkg.PackageImpl;
import com.android.server.pm.parsing.pkg.ParsedPackage;
import com.android.server.testing.shadows.ShadowApplicationPackageManager;
import com.android.server.testing.shadows.ShadowUserManager;
import com.android.server.wm.ActivityTaskManagerInternal;
import com.google.android.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** Unit tests for {@link CrossProfileAppsServiceImpl}. */
@RunWith(RobolectricTestRunner.class)
@Presubmit
@Config(shadows = {ShadowUserManager.class, ShadowApplicationPackageManager.class})
public class CrossProfileAppsServiceImplRoboTest {
private static final int CALLING_UID = 1111;
private static final int CALLING_PID = 1000;
private static final String CROSS_PROFILE_APP_PACKAGE_NAME =
"com.android.server.pm.crossprofileappsserviceimplrobotest.crossprofileapp";
@UserIdInt private static final int PERSONAL_PROFILE_USER_ID = 0;
private static final int PERSONAL_PROFILE_UID = 2222;
@UserIdInt private static final int WORK_PROFILE_USER_ID = 10;
private static final int WORK_PROFILE_UID = 3333;
private static final int OTHER_PROFILE_WITHOUT_CROSS_PROFILE_APP_USER_ID = 20;
@UserIdInt private static final int OTHER_PROFILE_GROUP_USER_ID = 30;
private static final int OTHER_PROFILE_GROUP_UID = 4444;
@UserIdInt private static final int OTHER_PROFILE_GROUP_2_USER_ID = 31;
private static final int OTHER_PROFILE_GROUP_2_UID = 5555;
private final ContextWrapper mContext = ApplicationProvider.getApplicationContext();
private final UserManager mUserManager = mContext.getSystemService(UserManager.class);
private final AppOpsManager mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
private final PackageManager mPackageManager = mContext.getPackageManager();
private final TestInjector mInjector = new TestInjector();
private final CrossProfileAppsServiceImpl mCrossProfileAppsServiceImpl =
new CrossProfileAppsServiceImpl(mContext, mInjector);
private final Map<UserHandle, Set<Intent>> mSentUserBroadcasts = new HashMap<>();
private final Map<Integer, List<ApplicationInfo>> installedApplications = new HashMap<>();
private final Set<Integer> mKilledUids = new HashSet<>();
@Mock private PackageManagerInternal mPackageManagerInternal;
@Mock private IPackageManager mIPackageManager;
@Mock private DevicePolicyManagerInternal mDevicePolicyManagerInternal;
@Before
public void initializeMocks() throws Exception {
MockitoAnnotations.initMocks(this);
initializeInstalledApplicationsMock();
mockCrossProfileAppInstalledAndEnabledOnEachProfile();
mockCrossProfileAppRequestsInteractAcrossProfiles();
mockCrossProfileAppRegistersBroadcastReceiver();
mockCrossProfileAppWhitelisted();
}
private void initializeInstalledApplicationsMock() {
when(mPackageManagerInternal.getInstalledApplications(anyInt(), anyInt(), eq(CALLING_UID)))
.thenAnswer(invocation -> installedApplications.get(invocation.getArgument(1)));
}
private void mockCrossProfileAppInstalledAndEnabledOnEachProfile() {
// They are enabled by default, so we simply have to ensure that a package info with an
// application info is returned.
final PackageInfo packageInfo = buildTestPackageInfo();
mockCrossProfileAppInstalledOnProfile(
packageInfo, PERSONAL_PROFILE_USER_ID, PERSONAL_PROFILE_UID);
mockCrossProfileAppInstalledOnProfile(packageInfo, WORK_PROFILE_USER_ID, WORK_PROFILE_UID);
mockCrossProfileAppInstalledOnProfile(
packageInfo, OTHER_PROFILE_GROUP_USER_ID, OTHER_PROFILE_GROUP_UID);
mockCrossProfileAppInstalledOnProfile(
packageInfo, OTHER_PROFILE_GROUP_2_USER_ID, OTHER_PROFILE_GROUP_2_UID);
}
private void mockCrossProfileAppInstalledOnProfile(
PackageInfo packageInfo, @UserIdInt int userId, int uid) {
when(mPackageManagerInternal.getPackageInfo(
eq(CROSS_PROFILE_APP_PACKAGE_NAME),
/* flags= */ anyInt(),
/* filterCallingUid= */ anyInt(),
eq(userId)))
.thenReturn(packageInfo);
when(mPackageManagerInternal.getPackage(uid))
.thenReturn(((ParsedPackage) PackageImpl.forTesting(CROSS_PROFILE_APP_PACKAGE_NAME)
.hideAsParsed()).hideAsFinal());
installedApplications.putIfAbsent(userId, new ArrayList<>());
installedApplications.get(userId).add(packageInfo.applicationInfo);
}
private PackageInfo buildTestPackageInfo() {
PackageInfo packageInfo = new PackageInfo();
packageInfo.applicationInfo = new ApplicationInfo();
packageInfo.applicationInfo.packageName = CROSS_PROFILE_APP_PACKAGE_NAME;
return packageInfo;
}
private void mockCrossProfileAppRequestsInteractAcrossProfiles() throws Exception {
final String permissionName = Manifest.permission.INTERACT_ACROSS_PROFILES;
when(mIPackageManager.getAppOpPermissionPackages(permissionName))
.thenReturn(new String[] {CROSS_PROFILE_APP_PACKAGE_NAME});
}
private void mockCrossProfileAppRegistersBroadcastReceiver() {
final ShadowApplicationPackageManager shadowApplicationPackageManager =
Shadow.extract(mPackageManager);
final Intent baseIntent =
new Intent(ACTION_CAN_INTERACT_ACROSS_PROFILES_CHANGED)
.setPackage(CROSS_PROFILE_APP_PACKAGE_NAME);
final Intent manifestIntent =
new Intent(baseIntent)
.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
| Intent.FLAG_RECEIVER_FOREGROUND);
final Intent registeredIntent =
new Intent(baseIntent).setFlags(FLAG_RECEIVER_REGISTERED_ONLY);
final List<ResolveInfo> resolveInfos = Lists.newArrayList(buildTestResolveInfo());
shadowApplicationPackageManager.setResolveInfosForIntent(manifestIntent, resolveInfos);
shadowApplicationPackageManager.setResolveInfosForIntent(registeredIntent, resolveInfos);
}
private ResolveInfo buildTestResolveInfo() {
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = new ActivityInfo();
resolveInfo.activityInfo.packageName = CROSS_PROFILE_APP_PACKAGE_NAME;
resolveInfo.activityInfo.name = CROSS_PROFILE_APP_PACKAGE_NAME + ".Receiver";
return resolveInfo;
}
private void mockCrossProfileAppWhitelisted() {
when(mDevicePolicyManagerInternal.getAllCrossProfilePackages())
.thenReturn(Lists.newArrayList(CROSS_PROFILE_APP_PACKAGE_NAME));
}
@Before
public void setUpCrossProfileAppUidsAndPackageNames() {
setUpCrossProfileAppUidAndPackageName(
PERSONAL_PROFILE_UID, PERSONAL_PROFILE_USER_ID);
setUpCrossProfileAppUidAndPackageName(
WORK_PROFILE_UID, WORK_PROFILE_USER_ID);
setUpCrossProfileAppUidAndPackageName(
OTHER_PROFILE_GROUP_UID, OTHER_PROFILE_GROUP_USER_ID);
setUpCrossProfileAppUidAndPackageName(
OTHER_PROFILE_GROUP_2_UID, OTHER_PROFILE_GROUP_2_USER_ID);
}
private void setUpCrossProfileAppUidAndPackageName(int uid, @UserIdInt int userId) {
ShadowApplicationPackageManager.setPackageUidAsUser(
CROSS_PROFILE_APP_PACKAGE_NAME, uid, userId);
when(mPackageManagerInternal
.getPackageUid(CROSS_PROFILE_APP_PACKAGE_NAME, /* flags= */ 0, userId))
.thenReturn(uid);
}
@Before
public void grantPermissions() {
grantPermissions(
Manifest.permission.MANAGE_APP_OPS_MODES,
Manifest.permission.UPDATE_APP_OPS_STATS,
Manifest.permission.CONFIGURE_INTERACT_ACROSS_PROFILES,
Manifest.permission.INTERACT_ACROSS_USERS,
Manifest.permission.INTERACT_ACROSS_USERS_FULL);
}
@Before
public void setUpProfiles() {
final ShadowUserManager shadowUserManager = Shadow.extract(mUserManager);
shadowUserManager.addProfileIds(
PERSONAL_PROFILE_USER_ID,
WORK_PROFILE_USER_ID,
OTHER_PROFILE_WITHOUT_CROSS_PROFILE_APP_USER_ID);
shadowUserManager.addProfileIds(
OTHER_PROFILE_GROUP_USER_ID,
OTHER_PROFILE_GROUP_2_USER_ID);
}
@Before
public void setInteractAcrossProfilesAppOpDefault() {
// It seems to be necessary to provide the shadow with the default already specified in
// AppOpsManager.
final int defaultMode = AppOpsManager.opToDefaultMode(OP_INTERACT_ACROSS_PROFILES);
explicitlySetInteractAcrossProfilesAppOp(PERSONAL_PROFILE_UID, defaultMode);
explicitlySetInteractAcrossProfilesAppOp(WORK_PROFILE_UID, defaultMode);
explicitlySetInteractAcrossProfilesAppOp(OTHER_PROFILE_GROUP_UID, defaultMode);
explicitlySetInteractAcrossProfilesAppOp(OTHER_PROFILE_GROUP_2_UID, defaultMode);
}
@Test
public void setInteractAcrossProfilesAppOp_noPermissions_throwsSecurityException() {
denyPermissions(
Manifest.permission.MANAGE_APP_OPS_MODES,
Manifest.permission.UPDATE_APP_OPS_STATS,
Manifest.permission.CONFIGURE_INTERACT_ACROSS_PROFILES,
Manifest.permission.INTERACT_ACROSS_USERS,
Manifest.permission.INTERACT_ACROSS_USERS_FULL);
try {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
fail();
} catch (SecurityException expected) {}
}
@Test
public void setInteractAcrossProfilesAppOp_missingInteractAcrossUsersAndFull_throwsSecurityException() {
denyPermissions(
Manifest.permission.INTERACT_ACROSS_USERS,
Manifest.permission.INTERACT_ACROSS_USERS_FULL);
grantPermissions(Manifest.permission.CONFIGURE_INTERACT_ACROSS_PROFILES);
try {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
fail();
} catch (SecurityException expected) {}
}
@Test
public void setInteractAcrossProfilesAppOp_setsAppOp() {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_configureInteractAcrossProfilesPermissionWithoutAppOpsPermissions_setsAppOp() {
denyPermissions(
Manifest.permission.MANAGE_APP_OPS_MODES,
Manifest.permission.UPDATE_APP_OPS_STATS);
grantPermissions(
Manifest.permission.CONFIGURE_INTERACT_ACROSS_PROFILES,
Manifest.permission.INTERACT_ACROSS_USERS);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_appOpsPermissionsWithoutConfigureInteractAcrossProfilesPermission_setsAppOp() {
denyPermissions(Manifest.permission.CONFIGURE_INTERACT_ACROSS_PROFILES);
grantPermissions(
Manifest.permission.MANAGE_APP_OPS_MODES,
Manifest.permission.UPDATE_APP_OPS_STATS,
Manifest.permission.INTERACT_ACROSS_USERS);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_setsAppOpWithUsersAndWithoutFull() {
denyPermissions(Manifest.permission.INTERACT_ACROSS_USERS_FULL);
grantPermissions(Manifest.permission.INTERACT_ACROSS_USERS);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_setsAppOpWithFullAndWithoutUsers() {
denyPermissions(Manifest.permission.INTERACT_ACROSS_USERS);
grantPermissions(Manifest.permission.INTERACT_ACROSS_USERS_FULL);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_setsAppOpOnOtherProfile() {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp(WORK_PROFILE_UID)).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_sendsBroadcast() {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedCanInteractAcrossProfilesChangedBroadcast()).isTrue();
}
@Test
public void setInteractAcrossProfilesAppOp_sendsBroadcastToOtherProfile() {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedCanInteractAcrossProfilesChangedBroadcast(WORK_PROFILE_USER_ID))
.isTrue();
}
@Test
public void setInteractAcrossProfilesAppOp_doesNotSendBroadcastToProfileWithoutPackage() {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedCanInteractAcrossProfilesChangedBroadcast(
OTHER_PROFILE_WITHOUT_CROSS_PROFILE_APP_USER_ID))
.isFalse();
}
@Test
public void setInteractAcrossProfilesAppOp_toSameAsCurrent_doesNotSendBroadcast() {
explicitlySetInteractAcrossProfilesAppOp(MODE_ALLOWED);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedCanInteractAcrossProfilesChangedBroadcast()).isFalse();
}
@Test
public void setInteractAcrossProfilesAppOp_toAllowed_whenNotAbleToRequest_doesNotSet() {
mockCrossProfileAppNotWhitelisted();
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(getCrossProfileAppOp()).isNotEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_toAllowed_whenNotAbleToRequest_doesNotSendBroadcast() {
mockCrossProfileAppNotWhitelisted();
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedCanInteractAcrossProfilesChangedBroadcast()).isFalse();
}
@Test
public void setInteractAcrossProfilesAppOp_withoutCrossProfileAttribute_manifestReceiversDoNotGetBroadcast() {
declareCrossProfileAttributeOnCrossProfileApp(false);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedManifestCanInteractAcrossProfilesChangedBroadcast()).isFalse();
}
@Test
public void setInteractAcrossProfilesAppOp_withCrossProfileAttribute_manifestReceiversGetBroadcast() {
declareCrossProfileAttributeOnCrossProfileApp(true);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(receivedManifestCanInteractAcrossProfilesChangedBroadcast()).isTrue();
}
@Test
public void setInteractAcrossProfilesAppOp_toAllowed_doesNotKillApp() {
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
assertThat(mKilledUids).isEmpty();
}
@Test
public void setInteractAcrossProfilesAppOp_toDisallowed_killsAppsInBothProfiles() {
shadowOf(mPackageManager).addPermissionInfo(createCrossProfilesPermissionInfo());
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED);
mCrossProfileAppsServiceImpl.setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_DEFAULT);
assertThat(mKilledUids).contains(WORK_PROFILE_UID);
assertThat(mKilledUids).contains(PERSONAL_PROFILE_UID);
}
private PermissionInfo createCrossProfilesPermissionInfo() {
PermissionInfo permissionInfo = new PermissionInfo();
permissionInfo.name = Manifest.permission.INTERACT_ACROSS_PROFILES;
permissionInfo.protectionLevel = PermissionInfo.PROTECTION_FLAG_APPOP;
return permissionInfo;
}
@Test
public void setInteractAcrossProfilesAppOp_userToSetInDifferentProfileGroupToCaller_setsAppOp() {
mCrossProfileAppsServiceImpl.getLocalService().setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED, OTHER_PROFILE_GROUP_USER_ID);
assertThat(getCrossProfileAppOp(OTHER_PROFILE_GROUP_UID)).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_userToSetInDifferentProfileGroupToCaller_setsAppOpOnOtherProfile() {
mCrossProfileAppsServiceImpl.getLocalService().setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED, OTHER_PROFILE_GROUP_USER_ID);
assertThat(getCrossProfileAppOp(OTHER_PROFILE_GROUP_2_UID)).isEqualTo(MODE_ALLOWED);
}
@Test
public void setInteractAcrossProfilesAppOp_userToSetInDifferentProfileGroupToCaller_doesNotSetCallerAppOp() {
mCrossProfileAppsServiceImpl.getLocalService().setInteractAcrossProfilesAppOp(
CROSS_PROFILE_APP_PACKAGE_NAME, MODE_ALLOWED, OTHER_PROFILE_GROUP_USER_ID);
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_DEFAULT);
}
@Test
public void canConfigureInteractAcrossProfiles_packageNotInstalledInProfile_returnsFalse() {
mockUninstallCrossProfileAppFromWorkProfile();
assertThat(mCrossProfileAppsServiceImpl
.canConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
private void mockUninstallCrossProfileAppFromWorkProfile() {
when(mPackageManagerInternal.getPackageInfo(
eq(CROSS_PROFILE_APP_PACKAGE_NAME),
/* flags= */ anyInt(),
/* filterCallingUid= */ anyInt(),
eq(WORK_PROFILE_USER_ID)))
.thenReturn(null);
when(mPackageManagerInternal.getPackage(WORK_PROFILE_UID)).thenReturn(null);
}
@Test
public void canConfigureInteractAcrossProfiles_packageDoesNotRequestInteractAcrossProfiles_returnsFalse()
throws Exception {
mockCrossProfileAppDoesNotRequestInteractAcrossProfiles();
assertThat(mCrossProfileAppsServiceImpl
.canConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
private void mockCrossProfileAppDoesNotRequestInteractAcrossProfiles() throws Exception {
final String permissionName = Manifest.permission.INTERACT_ACROSS_PROFILES;
when(mIPackageManager.getAppOpPermissionPackages(permissionName))
.thenReturn(new String[] {});
}
@Test
public void canConfigureInteractAcrossProfiles_packageNotWhitelisted_returnsFalse() {
mockCrossProfileAppNotWhitelisted();
assertThat(mCrossProfileAppsServiceImpl
.canConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
@Test
public void canConfigureInteractAcrossProfiles_returnsTrue() {
assertThat(mCrossProfileAppsServiceImpl
.canConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isTrue();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_packageNotInstalledInProfile_returnsTrue() {
mockUninstallCrossProfileAppFromWorkProfile();
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isTrue();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_packageDoesNotRequestInteractAcrossProfiles_returnsFalse()
throws Exception {
mockCrossProfileAppDoesNotRequestInteractAcrossProfiles();
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_packageNotWhitelisted_returnsTrue() {
mockCrossProfileAppNotWhitelisted();
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isTrue();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_platformSignedAppWithAutomaticPermission_returnsFalse() {
mockCrossProfileAppNotWhitelistedByOem();
shadowOf(mContext).grantPermissions(
Process.myPid(),
PERSONAL_PROFILE_UID,
Manifest.permission.INTERACT_ACROSS_PROFILES);
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_profileOwnerWorkProfile_returnsFalse() {
when(mDevicePolicyManagerInternal.getProfileOwnerAsUser(WORK_PROFILE_USER_ID))
.thenReturn(buildCrossProfileComponentName());
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_profileOwnerOtherProfile_returnsFalse() {
// Normally, the DPC would not be a profile owner of the personal profile, but for the
// purposes of this test, it is just a profile owner of any profile within the profile
// group.
when(mDevicePolicyManagerInternal.getProfileOwnerAsUser(PERSONAL_PROFILE_USER_ID))
.thenReturn(buildCrossProfileComponentName());
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isFalse();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_profileOwnerOutsideProfileGroup_returnsTrue() {
when(mDevicePolicyManagerInternal.getProfileOwnerAsUser(OTHER_PROFILE_GROUP_USER_ID))
.thenReturn(buildCrossProfileComponentName());
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isTrue();
}
@Test
public void canUserAttemptToConfigureInteractAcrossProfiles_returnsTrue() {
assertThat(mCrossProfileAppsServiceImpl
.canUserAttemptToConfigureInteractAcrossProfiles(CROSS_PROFILE_APP_PACKAGE_NAME))
.isTrue();
}
@Test
public void clearInteractAcrossProfilesAppOps() {
explicitlySetInteractAcrossProfilesAppOp(MODE_ALLOWED);
mCrossProfileAppsServiceImpl.clearInteractAcrossProfilesAppOps();
assertThat(getCrossProfileAppOp()).isEqualTo(MODE_DEFAULT);
}
private void explicitlySetInteractAcrossProfilesAppOp(@Mode int mode) {
explicitlySetInteractAcrossProfilesAppOp(PERSONAL_PROFILE_UID, mode);
}
private void explicitlySetInteractAcrossProfilesAppOp(int uid, @Mode int mode) {
shadowOf(mAppOpsManager).setMode(
OP_INTERACT_ACROSS_PROFILES, uid, CROSS_PROFILE_APP_PACKAGE_NAME, mode);
}
private void grantPermissions(String... permissions) {
shadowOf(mContext).grantPermissions(Process.myPid(), CALLING_UID, permissions);
}
private void denyPermissions(String... permissions) {
shadowOf(mContext).denyPermissions(Process.myPid(), CALLING_UID, permissions);
}
private @Mode int getCrossProfileAppOp() {
return getCrossProfileAppOp(PERSONAL_PROFILE_UID);
}
private @Mode int getCrossProfileAppOp(int uid) {
return mAppOpsManager.unsafeCheckOpNoThrow(
AppOpsManager.permissionToOp(Manifest.permission.INTERACT_ACROSS_PROFILES),
uid,
CROSS_PROFILE_APP_PACKAGE_NAME);
}
private boolean receivedCanInteractAcrossProfilesChangedBroadcast() {
return receivedCanInteractAcrossProfilesChangedBroadcast(PERSONAL_PROFILE_USER_ID);
}
private boolean receivedCanInteractAcrossProfilesChangedBroadcast(@UserIdInt int userId) {
final UserHandle userHandle = UserHandle.of(userId);
if (!mSentUserBroadcasts.containsKey(userHandle)) {
return false;
}
return mSentUserBroadcasts.get(userHandle)
.stream()
.anyMatch(this::isBroadcastCanInteractAcrossProfilesChanged);
}
private boolean isBroadcastCanInteractAcrossProfilesChanged(Intent intent) {
return intent.getAction().equals(ACTION_CAN_INTERACT_ACROSS_PROFILES_CHANGED)
&& CROSS_PROFILE_APP_PACKAGE_NAME.equals(intent.getPackage());
}
private void mockCrossProfileAndroidPackage(AndroidPackage androidPackage) {
when(mPackageManagerInternal.getPackage(CROSS_PROFILE_APP_PACKAGE_NAME))
.thenReturn(androidPackage);
when(mPackageManagerInternal.getPackage(PERSONAL_PROFILE_UID))
.thenReturn(androidPackage);
when(mPackageManagerInternal.getPackage(WORK_PROFILE_UID))
.thenReturn(androidPackage);
when(mPackageManagerInternal.getPackage(OTHER_PROFILE_GROUP_UID))
.thenReturn(androidPackage);
when(mPackageManagerInternal.getPackage(OTHER_PROFILE_GROUP_2_UID))
.thenReturn(androidPackage);
}
private void mockCrossProfileAppNotWhitelisted() {
when(mDevicePolicyManagerInternal.getAllCrossProfilePackages())
.thenReturn(new ArrayList<>());
}
private void mockCrossProfileAppNotWhitelistedByOem() {
when(mDevicePolicyManagerInternal.getDefaultCrossProfilePackages())
.thenReturn(new ArrayList<>());
}
private boolean receivedManifestCanInteractAcrossProfilesChangedBroadcast() {
final UserHandle userHandle = UserHandle.of(PERSONAL_PROFILE_USER_ID);
if (!mSentUserBroadcasts.containsKey(userHandle)) {
return false;
}
return mSentUserBroadcasts.get(userHandle)
.stream()
.anyMatch(this::isBroadcastManifestCanInteractAcrossProfilesChanged);
}
private boolean isBroadcastManifestCanInteractAcrossProfilesChanged(Intent intent) {
return isBroadcastCanInteractAcrossProfilesChanged(intent)
&& (intent.getFlags() & FLAG_RECEIVER_REGISTERED_ONLY) == 0
&& (intent.getFlags() & FLAG_RECEIVER_INCLUDE_BACKGROUND) != 0
&& (intent.getFlags() & FLAG_RECEIVER_FOREGROUND) != 0
&& intent.getComponent() != null
&& intent.getComponent().getPackageName().equals(CROSS_PROFILE_APP_PACKAGE_NAME);
}
private void declareCrossProfileAttributeOnCrossProfileApp(boolean value) {
mockCrossProfileAndroidPackage(
((ParsedPackage) PackageImpl.forTesting(CROSS_PROFILE_APP_PACKAGE_NAME)
.setCrossProfile(value)
.hideAsParsed()).hideAsFinal());
}
private ComponentName buildCrossProfileComponentName() {
return new ComponentName(CROSS_PROFILE_APP_PACKAGE_NAME, "testClassName");
}
private class TestInjector implements CrossProfileAppsServiceImpl.Injector {
@Override
public int getCallingUid() {
return CALLING_UID;
}
@Override
public int getCallingPid() {
return CALLING_PID;
}
@Override
public @UserIdInt int getCallingUserId() {
return PERSONAL_PROFILE_USER_ID;
}
@Override
public UserHandle getCallingUserHandle() {
return UserHandle.of(getCallingUserId());
}
@Override
public long clearCallingIdentity() {
return 0;
}
@Override
public void restoreCallingIdentity(long token) {}
@Override
public void withCleanCallingIdentity(ThrowingRunnable action) {
action.run();
}
@Override
public <T> T withCleanCallingIdentity(ThrowingSupplier<T> action) {
return action.get();
}
@Override
public UserManager getUserManager() {
return mUserManager;
}
@Override
public PackageManagerInternal getPackageManagerInternal() {
return mPackageManagerInternal;
}
@Override
public PackageManager getPackageManager() {
return mPackageManager;
}
@Override
public AppOpsManager getAppOpsManager() {
return mAppOpsManager;
}
@Override
public ActivityManagerInternal getActivityManagerInternal() {
return LocalServices.getService(ActivityManagerInternal.class);
}
@Override
public ActivityTaskManagerInternal getActivityTaskManagerInternal() {
return LocalServices.getService(ActivityTaskManagerInternal.class);
}
@Override
public IPackageManager getIPackageManager() {
return mIPackageManager;
}
@Override
public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
return mDevicePolicyManagerInternal;
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
// Robolectric's shadows do not currently support sendBroadcastAsUser.
final Set<Intent> broadcasts =
mSentUserBroadcasts.containsKey(user)
? mSentUserBroadcasts.get(user)
: new HashSet<>();
broadcasts.add(intent);
mSentUserBroadcasts.put(user, broadcasts);
mContext.sendBroadcastAsUser(intent, user);
}
@Override
public int checkComponentPermission(
String permission, int uid, int owningUid, boolean exported) {
// ActivityManager#checkComponentPermission calls through to
// AppGlobals.getPackageManager()#checkUidPermission, which calls through to
// ShadowActivityThread with Robolectric. This method is currently not supported there.
return mContext.checkPermission(permission, Process.myPid(), uid);
}
@Override
public void killUid(int uid) {
mKilledUids.add(uid);
}
}
}
|