• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.deviceowner;
18 
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertFalse;
21 import static junit.framework.Assert.assertTrue;
22 import static junit.framework.Assert.fail;
23 
24 import static org.junit.Assert.assertArrayEquals;
25 
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.content.Context;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 import java.util.Collections;
38 import java.util.Set;
39 
40 @RunWith(AndroidJUnit4.class)
41 public class AffiliationTest {
42 
43     private DevicePolicyManager mDevicePolicyManager;
44     private ComponentName mAdminComponent;
45 
46     @Before
setUp()47     public void setUp() {
48         Context context = InstrumentationRegistry.getContext();
49         mDevicePolicyManager = (DevicePolicyManager)
50                 context.getSystemService(Context.DEVICE_POLICY_SERVICE);
51         mAdminComponent = BasicAdminReceiver.getComponentName(context);
52     }
53 
54     @Test
testSetAffiliationId_null()55     public void testSetAffiliationId_null() {
56         try {
57             mDevicePolicyManager.setAffiliationIds(mAdminComponent, null);
58             fail("Should throw IllegalArgumentException");
59         } catch (IllegalArgumentException ex) {
60             // Expected
61         }
62     }
63 
64     @Test
testSetAffiliationId_containsEmptyString()65     public void testSetAffiliationId_containsEmptyString() {
66         try {
67             mDevicePolicyManager.setAffiliationIds(mAdminComponent, Collections.singleton(null));
68             fail("Should throw IllegalArgumentException");
69         } catch (IllegalArgumentException ex) {
70             // Expected
71         }
72     }
73 
74     @Test
testSetAffiliationId1()75     public void testSetAffiliationId1() {
76         setAffiliationIds(Collections.singleton("id.number.1"));
77     }
78 
79     @Test
testSetAffiliationId2()80     public void testSetAffiliationId2() {
81         setAffiliationIds(Collections.singleton("id.number.2"));
82     }
83 
84     @Test
testLockTaskMethodsThrowExceptionIfUnaffiliated()85     public void testLockTaskMethodsThrowExceptionIfUnaffiliated() {
86         checkLockTaskMethodsThrow();
87     }
88 
89     /** Assumes that the calling user is already affiliated before calling this method */
90     @Test
testSetLockTaskPackagesClearedIfUserBecomesUnaffiliated()91     public void testSetLockTaskPackagesClearedIfUserBecomesUnaffiliated() {
92         final String[] packages = {"package1", "package2"};
93         mDevicePolicyManager.setLockTaskPackages(mAdminComponent, packages);
94         assertArrayEquals(packages, mDevicePolicyManager.getLockTaskPackages(mAdminComponent));
95         assertTrue(mDevicePolicyManager.isLockTaskPermitted("package1"));
96         assertFalse(mDevicePolicyManager.isLockTaskPermitted("package3"));
97 
98         final Set<String> previousAffiliationIds =
99                 mDevicePolicyManager.getAffiliationIds(mAdminComponent);
100         try {
101             // Clearing affiliation ids for this user. Lock task methods unavailable.
102             setAffiliationIds(Collections.emptySet());
103             checkLockTaskMethodsThrow();
104             assertFalse(mDevicePolicyManager.isLockTaskPermitted("package1"));
105 
106             // Affiliating the user again. Previously set packages have been cleared.
107             setAffiliationIds(previousAffiliationIds);
108             assertEquals(0, mDevicePolicyManager.getLockTaskPackages(mAdminComponent).length);
109             assertFalse(mDevicePolicyManager.isLockTaskPermitted("package1"));
110         } finally {
111             mDevicePolicyManager.setAffiliationIds(mAdminComponent, previousAffiliationIds);
112         }
113     }
114 
setAffiliationIds(Set<String> ids)115     private void setAffiliationIds(Set<String> ids) {
116         mDevicePolicyManager.setAffiliationIds(mAdminComponent, ids);
117         assertEquals(ids, mDevicePolicyManager.getAffiliationIds(mAdminComponent));
118     }
119 
checkLockTaskMethodsThrow()120     private void checkLockTaskMethodsThrow() {
121         try {
122             mDevicePolicyManager.setLockTaskPackages(mAdminComponent, new String[0]);
123             fail("setLockTaskPackages did not throw expected SecurityException");
124         } catch (SecurityException expected) {
125         }
126         try {
127             mDevicePolicyManager.getLockTaskPackages(mAdminComponent);
128             fail("getLockTaskPackages did not throw expected SecurityException");
129         } catch (SecurityException expected) {
130         }
131     }
132 }
133