24
24
import static com .google .common .truth .Truth .assertThat ;
25
25
import static org .robolectric .Shadows .shadowOf ;
26
26
27
+ import android .app .admin .DevicePolicyManager ;
28
+ import android .content .ComponentName ;
27
29
import android .content .Context ;
28
30
import android .content .pm .PackageInfo ;
29
31
import android .content .pm .PackageManager ;
30
32
import android .content .pm .Signature ;
33
+ import android .os .Build ;
34
+ import android .os .Build .VERSION ;
35
+ import android .os .Build .VERSION_CODES ;
31
36
import android .os .Process ;
32
37
import androidx .test .core .app .ApplicationProvider ;
33
38
import com .google .common .collect .ImmutableList ;
40
45
import org .junit .Test ;
41
46
import org .junit .runner .RunWith ;
42
47
import org .robolectric .RobolectricTestRunner ;
48
+ import org .robolectric .annotation .Config ;
43
49
44
50
@ RunWith (RobolectricTestRunner .class )
45
51
public final class SecurityPoliciesTest {
@@ -59,13 +65,16 @@ public final class SecurityPoliciesTest {
59
65
60
66
private Context appContext ;
61
67
private PackageManager packageManager ;
68
+ private DevicePolicyManager devicePolicyManager ;
62
69
63
70
private SecurityPolicy policy ;
64
71
65
72
@ Before
66
73
public void setUp () {
67
74
appContext = ApplicationProvider .getApplicationContext ();
68
75
packageManager = appContext .getPackageManager ();
76
+ devicePolicyManager =
77
+ (DevicePolicyManager ) appContext .getSystemService (Context .DEVICE_POLICY_SERVICE );
69
78
}
70
79
71
80
@ SuppressWarnings ("deprecation" )
@@ -323,6 +332,171 @@ public void testHasPermissions_failsIfPackageDoesNotHavePermissions() throws Exc
323
332
.contains (OTHER_UID_PACKAGE_NAME );
324
333
}
325
334
335
+ @ Test
336
+ @ Config (sdk = 18 )
337
+ public void testIsDeviceOwner_succeedsForDeviceOwner () throws Exception {
338
+ PackageInfo info =
339
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
340
+
341
+ installPackages (OTHER_UID , info );
342
+ shadowOf (devicePolicyManager )
343
+ .setDeviceOwner (new ComponentName (OTHER_UID_PACKAGE_NAME , "foo" ));
344
+
345
+ policy = SecurityPolicies .isDeviceOwner (appContext );
346
+
347
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .OK .getCode ());
348
+ }
349
+
350
+ @ Test
351
+ @ Config (sdk = 18 )
352
+ public void testIsDeviceOwner_failsForNotDeviceOwner () throws Exception {
353
+ PackageInfo info =
354
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
355
+
356
+ installPackages (OTHER_UID , info );
357
+
358
+ policy = SecurityPolicies .isDeviceOwner (appContext );
359
+
360
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .PERMISSION_DENIED .getCode ());
361
+ }
362
+
363
+ @ Test
364
+ @ Config (sdk = 18 )
365
+ public void testIsDeviceOwner_failsWhenNoPackagesForUid () throws Exception {
366
+ policy = SecurityPolicies .isDeviceOwner (appContext );
367
+
368
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .UNAUTHENTICATED .getCode ());
369
+ }
370
+
371
+ @ Test
372
+ @ Config (sdk = 17 )
373
+ public void testIsDeviceOwner_failsForSdkLevelTooLow () throws Exception {
374
+ PackageInfo info =
375
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
376
+
377
+ installPackages (OTHER_UID , info );
378
+
379
+ policy = SecurityPolicies .isDeviceOwner (appContext );
380
+
381
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .PERMISSION_DENIED .getCode ());
382
+ }
383
+
384
+ @ Test
385
+ @ Config (sdk = 21 )
386
+ public void testIsProfileOwner_succeedsForProfileOwner () throws Exception {
387
+ PackageInfo info =
388
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
389
+
390
+ installPackages (OTHER_UID , info );
391
+ shadowOf (devicePolicyManager )
392
+ .setProfileOwner (new ComponentName (OTHER_UID_PACKAGE_NAME , "foo" ));
393
+
394
+ policy = SecurityPolicies .isProfileOwner (appContext );
395
+
396
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .OK .getCode ());
397
+ }
398
+
399
+ @ Test
400
+ @ Config (sdk = 21 )
401
+ public void testIsProfileOwner_failsForNotProfileOwner () throws Exception {
402
+ PackageInfo info =
403
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
404
+
405
+ installPackages (OTHER_UID , info );
406
+
407
+ policy = SecurityPolicies .isProfileOwner (appContext );
408
+
409
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .PERMISSION_DENIED .getCode ());
410
+ }
411
+
412
+ @ Test
413
+ @ Config (sdk = 21 )
414
+ public void testIsProfileOwner_failsWhenNoPackagesForUid () throws Exception {
415
+ policy = SecurityPolicies .isProfileOwner (appContext );
416
+
417
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .UNAUTHENTICATED .getCode ());
418
+ }
419
+
420
+ @ Test
421
+ @ Config (sdk = 19 )
422
+ public void testIsProfileOwner_failsForSdkLevelTooLow () throws Exception {
423
+ PackageInfo info =
424
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
425
+
426
+ installPackages (OTHER_UID , info );
427
+
428
+ policy = SecurityPolicies .isProfileOwner (appContext );
429
+
430
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .PERMISSION_DENIED .getCode ());
431
+ }
432
+
433
+ @ Test
434
+ @ Config (sdk = 30 )
435
+ public void testIsProfileOwnerOnOrgOwned_succeedsForProfileOwnerOnOrgOwned () throws Exception {
436
+ PackageInfo info =
437
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
438
+
439
+ installPackages (OTHER_UID , info );
440
+ shadowOf (devicePolicyManager )
441
+ .setProfileOwner (new ComponentName (OTHER_UID_PACKAGE_NAME , "foo" ));
442
+ shadowOf (devicePolicyManager ).setOrganizationOwnedDeviceWithManagedProfile (true );
443
+
444
+ policy = SecurityPolicies .isProfileOwnerOnOrganizationOwnedDevice (appContext );
445
+
446
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .OK .getCode ());
447
+
448
+ }
449
+
450
+ @ Test
451
+ @ Config (sdk = 30 )
452
+ public void testIsProfileOwnerOnOrgOwned_failsForProfileOwnerOnNonOrgOwned () throws Exception {
453
+ PackageInfo info =
454
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
455
+
456
+ installPackages (OTHER_UID , info );
457
+ shadowOf (devicePolicyManager )
458
+ .setProfileOwner (new ComponentName (OTHER_UID_PACKAGE_NAME , "foo" ));
459
+ shadowOf (devicePolicyManager ).setOrganizationOwnedDeviceWithManagedProfile (false );
460
+
461
+ policy = SecurityPolicies .isProfileOwnerOnOrganizationOwnedDevice (appContext );
462
+
463
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .OK .getCode ());
464
+ }
465
+
466
+ @ Test
467
+ @ Config (sdk = 21 )
468
+ public void testIsProfileOwnerOnOrgOwned_failsForNotProfileOwner () throws Exception {
469
+ PackageInfo info =
470
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
471
+
472
+ installPackages (OTHER_UID , info );
473
+
474
+ policy = SecurityPolicies .isProfileOwnerOnOrganizationOwnedDevice (appContext );
475
+
476
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .PERMISSION_DENIED .getCode ());
477
+ }
478
+
479
+ @ Test
480
+ @ Config (sdk = 21 )
481
+ public void testIsProfileOwnerOnOrgOwned_failsWhenNoPackagesForUid () throws Exception {
482
+ policy = SecurityPolicies .isProfileOwnerOnOrganizationOwnedDevice (appContext );
483
+
484
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .UNAUTHENTICATED .getCode ());
485
+ }
486
+
487
+ @ Test
488
+ @ Config (sdk = 29 )
489
+ public void testIsProfileOwnerOnOrgOwned_failsForSdkLevelTooLow () throws Exception {
490
+ PackageInfo info =
491
+ newBuilder ().setPackageName (OTHER_UID_PACKAGE_NAME ).setSignatures (SIG2 ).build ();
492
+
493
+ installPackages (OTHER_UID , info );
494
+
495
+ policy = SecurityPolicies .isProfileOwner (appContext );
496
+
497
+ assertThat (policy .checkAuthorization (OTHER_UID ).getCode ()).isEqualTo (Status .PERMISSION_DENIED .getCode ());
498
+ }
499
+
326
500
private static PackageInfoBuilder newBuilder () {
327
501
return new PackageInfoBuilder ();
328
502
}
0 commit comments