blob: 212b37cebead6951805ae6138d2a520e087cceec [file] [log] [blame]
Makoto Onukicc4bbeb2015-09-17 10:28:24 -07001/*
2 * Copyright (C) 2015 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 */
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070016package com.android.server.devicepolicy;
17
Makoto Onukif76b06a2015-09-22 15:03:44 -070018import android.Manifest.permission;
19import android.app.Activity;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070020import android.app.admin.DeviceAdminReceiver;
21import android.app.admin.DevicePolicyManager;
22import android.app.admin.DevicePolicyManagerInternal;
Makoto Onukif76b06a2015-09-22 15:03:44 -070023import android.content.BroadcastReceiver;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070024import android.content.ComponentName;
Rubin Xued1928a2016-02-11 17:23:06 +000025import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageInfo;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070027import android.content.pm.PackageManager;
Makoto Onukia31ebbc2015-11-23 17:15:21 -080028import android.net.wifi.WifiInfo;
Makoto Onuki3ab6f2e2015-11-05 13:55:37 -080029import android.os.Build.VERSION_CODES;
Rubin Xued1928a2016-02-11 17:23:06 +000030import android.os.Build;
Makoto Onukif76b06a2015-09-22 15:03:44 -070031import android.os.Bundle;
Makoto Onukic8a5a552015-11-19 14:29:12 -080032import android.os.Process;
Makoto Onukib643fb02015-09-22 15:03:44 -070033import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070034import android.os.UserManager;
Makoto Onuki2a3c3da2016-02-18 14:25:30 -080035import android.provider.Settings;
Makoto Onukia31ebbc2015-11-23 17:15:21 -080036import android.test.MoreAsserts;
Benjamin Franz6d009032016-01-25 18:56:38 +000037import android.test.suitebuilder.annotation.SmallTest;
Bartosz Fabianowski4c052f22016-01-25 14:18:43 +010038import android.util.ArraySet;
Makoto Onukib643fb02015-09-22 15:03:44 -070039import android.util.Pair;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070040
Alan Treadwayafad8782016-01-19 15:15:08 +000041import com.android.server.LocalServices;
42import com.android.server.SystemService;
43
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070044import org.mockito.ArgumentCaptor;
Makoto Onukib643fb02015-09-22 15:03:44 -070045import org.mockito.invocation.InvocationOnMock;
46import org.mockito.stubbing.Answer;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070047
Makoto Onukic8a5a552015-11-19 14:29:12 -080048import java.util.ArrayList;
Alan Treadwayafad8782016-01-19 15:15:08 +000049import java.util.Arrays;
Makoto Onukib643fb02015-09-22 15:03:44 -070050import java.util.HashMap;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070051import java.util.List;
Makoto Onukib643fb02015-09-22 15:03:44 -070052import java.util.Map;
Bartosz Fabianowski4c052f22016-01-25 14:18:43 +010053import java.util.Set;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070054
55import static org.mockito.Matchers.any;
Makoto Onukia52562c2015-10-01 16:12:31 -070056import static org.mockito.Matchers.anyInt;
Makoto Onukif76b06a2015-09-22 15:03:44 -070057import static org.mockito.Matchers.anyString;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070058import static org.mockito.Matchers.eq;
Makoto Onukif76b06a2015-09-22 15:03:44 -070059import static org.mockito.Matchers.isNull;
Makoto Onukib643fb02015-09-22 15:03:44 -070060import static org.mockito.Mockito.doAnswer;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070061import static org.mockito.Mockito.doReturn;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080062import static org.mockito.Mockito.reset;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070063import static org.mockito.Mockito.times;
Makoto Onuki2a3c3da2016-02-18 14:25:30 -080064import static org.mockito.Mockito.validateMockitoUsage;
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070065import static org.mockito.Mockito.verify;
66import static org.mockito.Mockito.when;
67
68/**
Makoto Onukif76b06a2015-09-22 15:03:44 -070069 * Tests for DevicePolicyManager( and DevicePolicyManagerService).
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070070 *
71 m FrameworksServicesTests &&
72 adb install \
Alan Treadwayafad8782016-01-19 15:15:08 +000073 -r ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070074 adb shell am instrument -e class com.android.server.devicepolicy.DevicePolicyManagerTest \
Makoto Onukic8a5a552015-11-19 14:29:12 -080075 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070076
77 (mmma frameworks/base/services/tests/servicestests/ for non-ninja build)
78 */
Benjamin Franz6d009032016-01-25 18:56:38 +000079@SmallTest
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070080public class DevicePolicyManagerTest extends DpmTestBase {
Alan Treadwayafad8782016-01-19 15:15:08 +000081 private static final List<String> OWNER_SETUP_PERMISSIONS = Arrays.asList(
82 permission.MANAGE_DEVICE_ADMINS, permission.MANAGE_PROFILE_AND_DEVICE_OWNERS,
83 permission.MANAGE_USERS, permission.INTERACT_ACROSS_USERS_FULL);
84
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070085 private DpmMockContext mContext;
86 public DevicePolicyManager dpm;
87 public DevicePolicyManagerServiceTestable dpms;
88
89 @Override
90 protected void setUp() throws Exception {
91 super.setUp();
92
93 mContext = getContext();
94
95 when(mContext.packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN)))
96 .thenReturn(true);
97
Makoto Onuki1a5ee772016-02-12 15:34:57 -080098 // By default, pretend all users are running and unlocked.
99 when(mContext.userManager.isUserUnlocked(anyInt())).thenReturn(true);
100
Makoto Onukia52562c2015-10-01 16:12:31 -0700101 initializeDpms();
Makoto Onukif76b06a2015-09-22 15:03:44 -0700102
Makoto Onukid932f762015-09-29 16:53:38 -0700103 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID);
104 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID);
105 setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_UID);
Makoto Onuki3ab6f2e2015-11-05 13:55:37 -0800106 setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID);
Makoto Onukif76b06a2015-09-22 15:03:44 -0700107
Makoto Onukib643fb02015-09-22 15:03:44 -0700108 setUpUserManager();
Makoto Onukif76b06a2015-09-22 15:03:44 -0700109 }
110
Makoto Onukia52562c2015-10-01 16:12:31 -0700111 private void initializeDpms() {
112 // Need clearCallingIdentity() to pass permission checks.
113 final long ident = mContext.binder.clearCallingIdentity();
114 try {
115 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
116
117 dpms = new DevicePolicyManagerServiceTestable(mContext, dataDir);
118
119 dpms.systemReady(SystemService.PHASE_LOCK_SETTINGS_READY);
120 dpms.systemReady(SystemService.PHASE_BOOT_COMPLETED);
121
122 dpm = new DevicePolicyManagerTestable(mContext, dpms);
123 } finally {
124 mContext.binder.restoreCallingIdentity(ident);
125 }
126 }
127
Makoto Onukib643fb02015-09-22 15:03:44 -0700128 private void setUpUserManager() {
129 // Emulate UserManager.set/getApplicationRestriction().
130 final Map<Pair<String, UserHandle>, Bundle> appRestrictions = new HashMap<>();
131
132 // UM.setApplicationRestrictions() will save to appRestrictions.
133 doAnswer(new Answer<Void>() {
134 @Override
135 public Void answer(InvocationOnMock invocation) throws Throwable {
136 String pkg = (String) invocation.getArguments()[0];
137 Bundle bundle = (Bundle) invocation.getArguments()[1];
138 UserHandle user = (UserHandle) invocation.getArguments()[2];
139
140 appRestrictions.put(Pair.create(pkg, user), bundle);
141
142 return null;
143 }
144 }).when(mContext.userManager).setApplicationRestrictions(
145 anyString(), any(Bundle.class), any(UserHandle.class));
146
147 // UM.getApplicationRestrictions() will read from appRestrictions.
148 doAnswer(new Answer<Bundle>() {
149 @Override
150 public Bundle answer(InvocationOnMock invocation) throws Throwable {
151 String pkg = (String) invocation.getArguments()[0];
152 UserHandle user = (UserHandle) invocation.getArguments()[1];
153
154 return appRestrictions.get(Pair.create(pkg, user));
155 }
156 }).when(mContext.userManager).getApplicationRestrictions(
157 anyString(), any(UserHandle.class));
158
Makoto Onukid932f762015-09-29 16:53:38 -0700159 // Add the first secondary user.
160 mContext.addUser(DpmMockContext.CALLER_USER_HANDLE, 0);
Makoto Onukib643fb02015-09-22 15:03:44 -0700161 }
162
163 private void setAsProfileOwner(ComponentName admin) {
164 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
165 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
166
Makoto Onukia4f11972015-10-01 13:19:58 -0700167 // PO needs to be an DA.
Makoto Onukib643fb02015-09-22 15:03:44 -0700168 dpm.setActiveAdmin(admin, /* replace =*/ false);
169
170 // Fire!
171 assertTrue(dpm.setProfileOwner(admin, "owner-name", DpmMockContext.CALLER_USER_HANDLE));
172
173 // Check
Makoto Onuki068c54a2015-10-13 14:34:03 -0700174 assertEquals(admin, dpm.getProfileOwnerAsUser(DpmMockContext.CALLER_USER_HANDLE));
Makoto Onukib643fb02015-09-22 15:03:44 -0700175 }
176
177 public void testHasNoFeature() throws Exception {
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700178 when(mContext.packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN)))
179 .thenReturn(false);
180
181 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
182 new DevicePolicyManagerServiceTestable(mContext, dataDir);
183
184 // If the device has no DPMS feature, it shouldn't register the local service.
185 assertNull(LocalServices.getService(DevicePolicyManagerInternal.class));
186 }
187
188 /**
189 * Caller doesn't have proper permissions.
190 */
191 public void testSetActiveAdmin_SecurityException() {
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700192 // 1. Failure cases.
193
194 // Caller doesn't have MANAGE_DEVICE_ADMINS.
195 try {
Makoto Onukif76b06a2015-09-22 15:03:44 -0700196 dpm.setActiveAdmin(admin1, false);
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700197 fail("Didn't throw SecurityException");
198 } catch (SecurityException expected) {
199 }
200
201 // Caller has MANAGE_DEVICE_ADMINS, but for different user.
202 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
203 try {
Makoto Onukif76b06a2015-09-22 15:03:44 -0700204 dpm.setActiveAdmin(admin1, false, DpmMockContext.CALLER_USER_HANDLE + 1);
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700205 fail("Didn't throw SecurityException");
206 } catch (SecurityException expected) {
207 }
208 }
209
Makoto Onukif76b06a2015-09-22 15:03:44 -0700210 /**
211 * Test for:
212 * {@link DevicePolicyManager#setActiveAdmin}
Makoto Onuki2a3c3da2016-02-18 14:25:30 -0800213 * with replace=false and replace=true
Makoto Onukif76b06a2015-09-22 15:03:44 -0700214 * {@link DevicePolicyManager#isAdminActive}
215 * {@link DevicePolicyManager#isAdminActiveAsUser}
216 * {@link DevicePolicyManager#getActiveAdmins}
217 * {@link DevicePolicyManager#getActiveAdminsAsUser}
218 */
219 public void testSetActiveAdmin() throws Exception {
220 // 1. Make sure the caller has proper permissions.
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700221 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
222
Makoto Onukif76b06a2015-09-22 15:03:44 -0700223 // 2. Call the API.
224 dpm.setActiveAdmin(admin1, /* replace =*/ false);
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700225
226 // 3. Verify internal calls.
227
228 // Check if the boradcast is sent.
Makoto Onukif76b06a2015-09-22 15:03:44 -0700229 verify(mContext.spiedContext).sendBroadcastAsUser(
230 MockUtils.checkIntentAction(
231 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED),
232 MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE));
233 verify(mContext.spiedContext).sendBroadcastAsUser(
234 MockUtils.checkIntentAction(
235 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700236 MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE));
237
Makoto Onukif76b06a2015-09-22 15:03:44 -0700238 verify(mContext.ipackageManager, times(1)).setApplicationEnabledSetting(
239 eq(admin1.getPackageName()),
240 eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT),
241 eq(PackageManager.DONT_KILL_APP),
242 eq(DpmMockContext.CALLER_USER_HANDLE),
243 anyString());
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700244
245 // TODO Verify other calls too.
Makoto Onukif76b06a2015-09-22 15:03:44 -0700246
247 // Make sure it's active admin1.
248 assertTrue(dpm.isAdminActive(admin1));
249 assertFalse(dpm.isAdminActive(admin2));
250 assertFalse(dpm.isAdminActive(admin3));
251
252 // But not admin1 for a different user.
253
254 // For this to work, caller needs android.permission.INTERACT_ACROSS_USERS_FULL.
255 // (Because we're checking a different user's status from CALLER_USER_HANDLE.)
256 mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL");
257
258 assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE + 1));
259 assertFalse(dpm.isAdminActiveAsUser(admin2, DpmMockContext.CALLER_USER_HANDLE + 1));
260
261 mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL");
262
263 // Next, add one more admin.
264 // Before doing so, update the application info, now it's enabled.
Makoto Onukia52562c2015-10-01 16:12:31 -0700265 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID,
266 PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
Makoto Onukif76b06a2015-09-22 15:03:44 -0700267
268 dpm.setActiveAdmin(admin2, /* replace =*/ false);
269
270 // Now we have two admins.
271 assertTrue(dpm.isAdminActive(admin1));
272 assertTrue(dpm.isAdminActive(admin2));
273 assertFalse(dpm.isAdminActive(admin3));
274
275 // Admin2 was already enabled, so setApplicationEnabledSetting() shouldn't have called
276 // again. (times(1) because it was previously called for admin1)
277 verify(mContext.ipackageManager, times(1)).setApplicationEnabledSetting(
278 eq(admin1.getPackageName()),
279 eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT),
280 eq(PackageManager.DONT_KILL_APP),
281 eq(DpmMockContext.CALLER_USER_HANDLE),
282 anyString());
283
284 // 4. Add the same admin1 again without replace, which should throw.
285 try {
286 dpm.setActiveAdmin(admin1, /* replace =*/ false);
287 fail("Didn't throw");
288 } catch (IllegalArgumentException expected) {
289 }
290
291 // 5. Add the same admin1 again with replace, which should succeed.
292 dpm.setActiveAdmin(admin1, /* replace =*/ true);
293
294 // TODO make sure it's replaced.
295
296 // 6. Test getActiveAdmins()
297 List<ComponentName> admins = dpm.getActiveAdmins();
298 assertEquals(2, admins.size());
299 assertEquals(admin1, admins.get(0));
300 assertEquals(admin2, admins.get(1));
301
302 // Another user has no admins.
303 mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL");
304
305 assertEquals(0, DpmTestUtils.getListSizeAllowingNull(
306 dpm.getActiveAdminsAsUser(DpmMockContext.CALLER_USER_HANDLE + 1)));
307
308 mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL");
309 }
310
Makoto Onukid932f762015-09-29 16:53:38 -0700311 public void testSetActiveAdmin_multiUsers() throws Exception {
312
313 final int ANOTHER_USER_ID = 100;
314 final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 20456);
315
316 mMockContext.addUser(ANOTHER_USER_ID, 0); // Add one more user.
317
318 // Set up pacakge manager for the other user.
319 setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID);
Makoto Onukid932f762015-09-29 16:53:38 -0700320
321 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
322
323 dpm.setActiveAdmin(admin1, /* replace =*/ false);
324
325 mMockContext.binder.callingUid = ANOTHER_ADMIN_UID;
326 dpm.setActiveAdmin(admin2, /* replace =*/ false);
327
328
329 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID;
330 assertTrue(dpm.isAdminActive(admin1));
331 assertFalse(dpm.isAdminActive(admin2));
332
333 mMockContext.binder.callingUid = ANOTHER_ADMIN_UID;
334 assertFalse(dpm.isAdminActive(admin1));
335 assertTrue(dpm.isAdminActive(admin2));
336 }
337
Makoto Onukif76b06a2015-09-22 15:03:44 -0700338 /**
339 * Test for:
340 * {@link DevicePolicyManager#setActiveAdmin}
Makoto Onuki2a3c3da2016-02-18 14:25:30 -0800341 * with replace=false
Makoto Onukif76b06a2015-09-22 15:03:44 -0700342 */
343 public void testSetActiveAdmin_twiceWithoutReplace() throws Exception {
344 // 1. Make sure the caller has proper permissions.
345 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
346
347 dpm.setActiveAdmin(admin1, /* replace =*/ false);
348 assertTrue(dpm.isAdminActive(admin1));
349
350 // Add the same admin1 again without replace, which should throw.
351 try {
352 dpm.setActiveAdmin(admin1, /* replace =*/ false);
353 fail("Didn't throw");
354 } catch (IllegalArgumentException expected) {
355 }
356 }
357
358 /**
359 * Test for:
Makoto Onuki3ab6f2e2015-11-05 13:55:37 -0800360 * {@link DevicePolicyManager#setActiveAdmin} when the admin isn't protected with
361 * BIND_DEVICE_ADMIN.
362 */
363 public void testSetActiveAdmin_permissionCheck() throws Exception {
364 // 1. Make sure the caller has proper permissions.
365 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
366
367 try {
368 dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false);
369 fail();
370 } catch (IllegalArgumentException expected) {
371 assertTrue(expected.getMessage().contains(permission.BIND_DEVICE_ADMIN));
372 }
373 assertFalse(dpm.isAdminActive(adminNoPerm));
374
375 // Change the target API level to MNC. Now it can be set as DA.
376 setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID, null,
377 VERSION_CODES.M);
378 dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false);
379 assertTrue(dpm.isAdminActive(adminNoPerm));
380
381 // TODO Test the "load from the file" case where DA will still be loaded even without
382 // BIND_DEVICE_ADMIN and target API is N.
383 }
384
385 /**
386 * Test for:
Makoto Onukif76b06a2015-09-22 15:03:44 -0700387 * {@link DevicePolicyManager#removeActiveAdmin}
388 */
389 public void testRemoveActiveAdmin_SecurityException() {
390 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
391
392 // Add admin.
393
394 dpm.setActiveAdmin(admin1, /* replace =*/ false);
395
396 assertTrue(dpm.isAdminActive(admin1));
397
398 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
399
400 // Directly call the DPMS method with a different userid, which should fail.
401 try {
402 dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE + 1);
403 fail("Didn't throw SecurityException");
404 } catch (SecurityException expected) {
405 }
406
407 // Try to remove active admin with a different caller userid should fail too, without
408 // having MANAGE_DEVICE_ADMINS.
409 mContext.callerPermissions.clear();
410
Makoto Onukid932f762015-09-29 16:53:38 -0700411 // Change the caller, and call into DPMS directly with a different user-id.
412
Makoto Onukif76b06a2015-09-22 15:03:44 -0700413 mContext.binder.callingUid = 1234567;
414 try {
Makoto Onukid932f762015-09-29 16:53:38 -0700415 dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE);
Makoto Onukif76b06a2015-09-22 15:03:44 -0700416 fail("Didn't throw SecurityException");
417 } catch (SecurityException expected) {
418 }
419 }
420
421 /**
Makoto Onuki1a5ee772016-02-12 15:34:57 -0800422 * {@link DevicePolicyManager#removeActiveAdmin} should fail with the user is not unlocked
423 * (because we can't send the remove broadcast).
424 */
425 public void testRemoveActiveAdmin_userNotRunningOrLocked() {
426 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
427
428 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
429
430 // Add admin.
431
432 dpm.setActiveAdmin(admin1, /* replace =*/ false);
433
434 assertTrue(dpm.isAdminActive(admin1));
435
436 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
437
438 // 1. User not unlocked.
439 when(mContext.userManager.isUserUnlocked(eq(DpmMockContext.CALLER_USER_HANDLE)))
440 .thenReturn(false);
441 try {
442 dpm.removeActiveAdmin(admin1);
443 fail("Didn't throw IllegalStateException");
444 } catch (IllegalStateException expected) {
445 MoreAsserts.assertContainsRegex(
446 "User must be running and unlocked", expected.getMessage());
447 }
448
449 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
450
451 // 2. User unlocked.
452 when(mContext.userManager.isUserUnlocked(eq(DpmMockContext.CALLER_USER_HANDLE)))
453 .thenReturn(true);
454
455 dpm.removeActiveAdmin(admin1);
456
457 assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
458 }
459
460 /**
Makoto Onukif76b06a2015-09-22 15:03:44 -0700461 * Test for:
462 * {@link DevicePolicyManager#removeActiveAdmin}
463 */
Makoto Onukid932f762015-09-29 16:53:38 -0700464 public void testRemoveActiveAdmin_fromDifferentUserWithINTERACT_ACROSS_USERS_FULL() {
Makoto Onukif76b06a2015-09-22 15:03:44 -0700465 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
466
467 // Add admin1.
468
469 dpm.setActiveAdmin(admin1, /* replace =*/ false);
470
471 assertTrue(dpm.isAdminActive(admin1));
472 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
473
474 // Different user, but should work, because caller has proper permissions.
475 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
Makoto Onukid932f762015-09-29 16:53:38 -0700476
477 // Change the caller, and call into DPMS directly with a different user-id.
Makoto Onukif76b06a2015-09-22 15:03:44 -0700478 mContext.binder.callingUid = 1234567;
Makoto Onukid932f762015-09-29 16:53:38 -0700479
480 dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE);
Makoto Onukif76b06a2015-09-22 15:03:44 -0700481
482 assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
483
484 // TODO DO Still can't be removed in this case.
485 }
486
487 /**
488 * Test for:
489 * {@link DevicePolicyManager#removeActiveAdmin}
490 */
491 public void testRemoveActiveAdmin_sameUserNoMANAGE_DEVICE_ADMINS() {
492 // Need MANAGE_DEVICE_ADMINS for setActiveAdmin. We'll remove it later.
493 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
494
495 // Add admin1.
496
497 dpm.setActiveAdmin(admin1, /* replace =*/ false);
498
499 assertTrue(dpm.isAdminActive(admin1));
500 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
501
502 // Broadcast from saveSettingsLocked().
503 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser(
504 MockUtils.checkIntentAction(
505 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED),
506 MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE));
507
508 // Remove. No permissions, but same user, so it'll work.
509 mContext.callerPermissions.clear();
510 dpm.removeActiveAdmin(admin1);
511
512 final ArgumentCaptor<BroadcastReceiver> brCap =
513 ArgumentCaptor.forClass(BroadcastReceiver.class);
514
515 // Is removing now, but not removed yet.
516 assertTrue(dpm.isAdminActive(admin1));
517 assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
518
519 verify(mContext.spiedContext).sendOrderedBroadcastAsUser(
520 MockUtils.checkIntentAction(
521 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED),
522 MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE),
523 isNull(String.class),
524 brCap.capture(),
525 eq(dpms.mHandler),
526 eq(Activity.RESULT_OK),
527 isNull(String.class),
528 isNull(Bundle.class));
529
530 brCap.getValue().onReceive(mContext, null);
531
532 assertFalse(dpm.isAdminActive(admin1));
533 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
534
535 // Again broadcast from saveSettingsLocked().
536 verify(mContext.spiedContext, times(2)).sendBroadcastAsUser(
537 MockUtils.checkIntentAction(
538 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED),
539 MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE));
540
541 // TODO Check other internal calls.
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700542 }
Makoto Onukib643fb02015-09-22 15:03:44 -0700543
544 /**
545 * Test for: {@link DevicePolicyManager#setDeviceOwner} DO on system user installs
546 * successfully.
547 */
548 public void testSetDeviceOwner() throws Exception {
549 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
Makoto Onukic8a5a552015-11-19 14:29:12 -0800550 mContext.callerPermissions.add(permission.MANAGE_USERS);
Makoto Onukib643fb02015-09-22 15:03:44 -0700551 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
552 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
553
Makoto Onukid932f762015-09-29 16:53:38 -0700554 // In this test, change the caller user to "system".
Makoto Onukib643fb02015-09-22 15:03:44 -0700555 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
556
Makoto Onukid932f762015-09-29 16:53:38 -0700557 // Make sure admin1 is installed on system user.
558 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
Makoto Onukid932f762015-09-29 16:53:38 -0700559
Makoto Onukic8a5a552015-11-19 14:29:12 -0800560 // Check various get APIs.
561 checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ false);
562
Makoto Onukib643fb02015-09-22 15:03:44 -0700563 // DO needs to be an DA.
564 dpm.setActiveAdmin(admin1, /* replace =*/ false);
565
566 // Fire!
Makoto Onukia52562c2015-10-01 16:12:31 -0700567 assertTrue(dpm.setDeviceOwner(admin1, "owner-name"));
Makoto Onukib643fb02015-09-22 15:03:44 -0700568
Makoto Onukic8a5a552015-11-19 14:29:12 -0800569 // getDeviceOwnerComponent should return the admin1 component.
570 assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser());
571 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
572
573 // Check various get APIs.
574 checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ true);
575
576 // getDeviceOwnerComponent should *NOT* return the admin1 component for other users.
577 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
578 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
579 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
580
581 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
582
Makoto Onuki219bbaf2015-11-12 01:38:47 +0000583 // Verify internal calls.
584 verify(mContext.iactivityManager, times(1)).updateDeviceOwner(
585 eq(admin1.getPackageName()));
586
Makoto Onukib643fb02015-09-22 15:03:44 -0700587 // TODO We should check if the caller has called clearCallerIdentity().
588 verify(mContext.ibackupManager, times(1)).setBackupServiceActive(
589 eq(UserHandle.USER_SYSTEM), eq(false));
590
591 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser(
592 MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED),
593 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM));
594
Makoto Onukic8a5a552015-11-19 14:29:12 -0800595 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
Makoto Onukib643fb02015-09-22 15:03:44 -0700596
Makoto Onuki803d6752015-10-30 12:58:39 -0700597 // Try to set a profile owner on the same user, which should fail.
598 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID);
599 dpm.setActiveAdmin(admin2, /* refreshing= */ true, UserHandle.USER_SYSTEM);
600 try {
601 dpm.setProfileOwner(admin2, "owner-name", UserHandle.USER_SYSTEM);
602 fail("IllegalStateException not thrown");
603 } catch (IllegalStateException expected) {
604 assertTrue("Message was: " + expected.getMessage(),
605 expected.getMessage().contains("already has a device owner"));
606 }
607
Makoto Onuki90b89652016-01-28 14:44:18 -0800608 // DO admin can't be deactivated.
609 dpm.removeActiveAdmin(admin1);
610 assertTrue(dpm.isAdminActive(admin1));
611
Makoto Onukib643fb02015-09-22 15:03:44 -0700612 // TODO Test getDeviceOwnerName() too. To do so, we need to change
613 // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable.
614 }
615
Makoto Onukic8a5a552015-11-19 14:29:12 -0800616 private void checkGetDeviceOwnerInfoApi(DevicePolicyManager dpm, boolean hasDeviceOwner) {
617 final int origCallingUser = mContext.binder.callingUid;
618 final List origPermissions = new ArrayList(mContext.callerPermissions);
619 mContext.callerPermissions.clear();
620
621 mContext.callerPermissions.add(permission.MANAGE_USERS);
622
623 mContext.binder.callingUid = Process.SYSTEM_UID;
624
625 // TODO Test getDeviceOwnerName() too. To do so, we need to change
626 // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable.
627 if (hasDeviceOwner) {
628 assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName()));
629 assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
630 assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser());
631
632 assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
633 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
634 assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId());
635 } else {
636 assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName()));
637 assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
638 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
639
640 assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
641 assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser());
642 assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId());
643 }
644
645 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
646 if (hasDeviceOwner) {
647 assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName()));
648 assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
649 assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser());
650
651 assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
652 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
653 assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId());
654 } else {
655 assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName()));
656 assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
657 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
658
659 assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
660 assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser());
661 assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId());
662 }
663
664 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
665 // Still with MANAGE_USERS.
666 assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName()));
667 assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
668 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
669
670 if (hasDeviceOwner) {
671 assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
672 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
673 assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId());
674 } else {
675 assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
676 assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser());
677 assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId());
678 }
679
680 mContext.binder.callingUid = Process.SYSTEM_UID;
681 mContext.callerPermissions.remove(permission.MANAGE_USERS);
682 // System can still call "OnAnyUser" without MANAGE_USERS.
683 if (hasDeviceOwner) {
684 assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName()));
685 assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
686 assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser());
687
688 assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
689 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
690 assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId());
691 } else {
692 assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName()));
693 assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
694 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
695
696 assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()));
697 assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser());
698 assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId());
699 }
700
701 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
702 // Still no MANAGE_USERS.
703 if (hasDeviceOwner) {
704 assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName()));
705 assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
706 assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser());
707 } else {
708 assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName()));
709 assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
710 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
711 }
712
713 try {
714 dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName());
715 fail();
716 } catch (SecurityException expected) {
717 }
718 try {
719 dpm.getDeviceOwnerComponentOnAnyUser();
720 fail();
721 } catch (SecurityException expected) {
722 }
723 try {
724 dpm.getDeviceOwnerUserId();
725 fail();
726 } catch (SecurityException expected) {
727 }
728 try {
729 dpm.getDeviceOwnerNameOnAnyUser();
730 fail();
731 } catch (SecurityException expected) {
732 }
733
734 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
735 // Still no MANAGE_USERS.
736 assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName()));
737 assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName()));
738 assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser());
739
740 try {
741 dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName());
742 fail();
743 } catch (SecurityException expected) {
744 }
745 try {
746 dpm.getDeviceOwnerComponentOnAnyUser();
747 fail();
748 } catch (SecurityException expected) {
749 }
750 try {
751 dpm.getDeviceOwnerUserId();
752 fail();
753 } catch (SecurityException expected) {
754 }
755 try {
756 dpm.getDeviceOwnerNameOnAnyUser();
757 fail();
758 } catch (SecurityException expected) {
759 }
760
761 // Restore.
762 mContext.binder.callingUid = origCallingUser;
763 mContext.callerPermissions.addAll(origPermissions);
764 }
765
766
Makoto Onukib643fb02015-09-22 15:03:44 -0700767 /**
768 * Test for: {@link DevicePolicyManager#setDeviceOwner} Package doesn't exist.
769 */
770 public void testSetDeviceOwner_noSuchPackage() {
771 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
Makoto Onukic8a5a552015-11-19 14:29:12 -0800772 mContext.callerPermissions.add(permission.MANAGE_USERS);
Makoto Onukib643fb02015-09-22 15:03:44 -0700773 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
774 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
775
776 // Call from a process on the system user.
777 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
778
Makoto Onukib643fb02015-09-22 15:03:44 -0700779 try {
Makoto Onukia52562c2015-10-01 16:12:31 -0700780 dpm.setDeviceOwner(new ComponentName("a.b.c", ".def"));
Makoto Onukib643fb02015-09-22 15:03:44 -0700781 fail("Didn't throw IllegalArgumentException");
782 } catch (IllegalArgumentException expected) {
Makoto Onuki803d6752015-10-30 12:58:39 -0700783 assertTrue("Message was: " + expected.getMessage(),
784 expected.getMessage().contains("Invalid component"));
Makoto Onukib643fb02015-09-22 15:03:44 -0700785 }
786 }
787
788 public void testSetDeviceOwner_failures() throws Exception {
789 // TODO Test more failure cases. Basically test all chacks in enforceCanSetDeviceOwner().
790 }
791
Makoto Onukia52562c2015-10-01 16:12:31 -0700792 public void testClearDeviceOwner() throws Exception {
793 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
Makoto Onukic8a5a552015-11-19 14:29:12 -0800794 mContext.callerPermissions.add(permission.MANAGE_USERS);
Makoto Onukia52562c2015-10-01 16:12:31 -0700795 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
796 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
797
798 // Set admin1 as a DA to the secondary user.
799 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID);
800
801 dpm.setActiveAdmin(admin1, /* replace =*/ false);
802
803 // Set admin 1 as the DO to the system user.
804
805 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
806 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
807 dpm.setActiveAdmin(admin1, /* replace =*/ false);
808 assertTrue(dpm.setDeviceOwner(admin1, "owner-name"));
809
Makoto Onuki219bbaf2015-11-12 01:38:47 +0000810 // Verify internal calls.
811 verify(mContext.iactivityManager, times(1)).updateDeviceOwner(
812 eq(admin1.getPackageName()));
813
Makoto Onukic8a5a552015-11-19 14:29:12 -0800814 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
Makoto Onukia52562c2015-10-01 16:12:31 -0700815
Makoto Onuki90b89652016-01-28 14:44:18 -0800816 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER);
817
818 assertTrue(dpm.isAdminActive(admin1));
819 assertFalse(dpm.isRemovingAdmin(admin1, UserHandle.USER_SYSTEM));
820
Makoto Onukia52562c2015-10-01 16:12:31 -0700821 // Set up other mocks.
822 when(mContext.userManager.getUserRestrictions()).thenReturn(new Bundle());
823
824 // Now call clear.
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700825 doReturn(DpmMockContext.CALLER_SYSTEM_USER_UID).when(mContext.packageManager).getPackageUidAsUser(
Makoto Onukia52562c2015-10-01 16:12:31 -0700826 eq(admin1.getPackageName()),
827 anyInt());
Makoto Onuki1a5ee772016-02-12 15:34:57 -0800828
829 // But first pretend the user is locked. Then it should fail.
830 when(mContext.userManager.isUserUnlocked(anyInt())).thenReturn(false);
831 try {
832 dpm.clearDeviceOwnerApp(admin1.getPackageName());
833 fail("Didn't throw IllegalStateException");
834 } catch (IllegalStateException expected) {
835 MoreAsserts.assertContainsRegex(
836 "User must be running and unlocked", expected.getMessage());
837 }
838
839 when(mContext.userManager.isUserUnlocked(anyInt())).thenReturn(true);
Makoto Onuki90b89652016-01-28 14:44:18 -0800840 reset(mContext.userManagerInternal);
Makoto Onukia52562c2015-10-01 16:12:31 -0700841 dpm.clearDeviceOwnerApp(admin1.getPackageName());
842
843 // Now DO shouldn't be set.
Makoto Onukic8a5a552015-11-19 14:29:12 -0800844 assertNull(dpm.getDeviceOwnerComponentOnAnyUser());
Makoto Onukia52562c2015-10-01 16:12:31 -0700845
Makoto Onuki90b89652016-01-28 14:44:18 -0800846 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
847 eq(UserHandle.USER_SYSTEM),
848 MockUtils.checkUserRestrictions(),
849 MockUtils.checkUserRestrictions()
850 );
851
852 assertTrue(dpm.isAdminActive(admin1));
853 assertTrue(dpm.isRemovingAdmin(admin1, UserHandle.USER_SYSTEM));
854
Makoto Onukia52562c2015-10-01 16:12:31 -0700855 // TODO Check other calls.
856 }
857
858 public void testClearDeviceOwner_fromDifferentUser() throws Exception {
859 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
Makoto Onukic8a5a552015-11-19 14:29:12 -0800860 mContext.callerPermissions.add(permission.MANAGE_USERS);
Makoto Onukia52562c2015-10-01 16:12:31 -0700861 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
862 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
863
864 // Set admin1 as a DA to the secondary user.
865 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID);
866
867 dpm.setActiveAdmin(admin1, /* replace =*/ false);
868
869 // Set admin 1 as the DO to the system user.
870
871 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
872 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
873 dpm.setActiveAdmin(admin1, /* replace =*/ false);
874 assertTrue(dpm.setDeviceOwner(admin1, "owner-name"));
875
Makoto Onuki219bbaf2015-11-12 01:38:47 +0000876 // Verify internal calls.
877 verify(mContext.iactivityManager, times(1)).updateDeviceOwner(
878 eq(admin1.getPackageName()));
879
Makoto Onukic8a5a552015-11-19 14:29:12 -0800880 assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser());
Makoto Onukia52562c2015-10-01 16:12:31 -0700881
882 // Now call clear from the secondary user, which should throw.
883 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
884
885 // Now call clear.
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700886 doReturn(DpmMockContext.CALLER_UID).when(mContext.packageManager).getPackageUidAsUser(
Makoto Onukia52562c2015-10-01 16:12:31 -0700887 eq(admin1.getPackageName()),
888 anyInt());
889 try {
890 dpm.clearDeviceOwnerApp(admin1.getPackageName());
891 fail("Didn't throw");
892 } catch (SecurityException e) {
893 assertEquals("clearDeviceOwner can only be called by the device owner", e.getMessage());
894 }
895
Makoto Onukic8a5a552015-11-19 14:29:12 -0800896 // DO shouldn't be removed.
897 assertTrue(dpm.isDeviceManaged());
Makoto Onukia52562c2015-10-01 16:12:31 -0700898 }
899
Makoto Onukib643fb02015-09-22 15:03:44 -0700900 public void testSetProfileOwner() throws Exception {
901 setAsProfileOwner(admin1);
Makoto Onuki803d6752015-10-30 12:58:39 -0700902
Makoto Onuki90b89652016-01-28 14:44:18 -0800903 // PO admin can't be deactivated.
904 dpm.removeActiveAdmin(admin1);
905 assertTrue(dpm.isAdminActive(admin1));
906
Makoto Onuki803d6752015-10-30 12:58:39 -0700907 // Try setting DO on the same user, which should fail.
908 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID);
909 dpm.setActiveAdmin(admin2, /* refreshing= */ true, DpmMockContext.CALLER_USER_HANDLE);
910 try {
911 dpm.setDeviceOwner(admin2, "owner-name", DpmMockContext.CALLER_USER_HANDLE);
912 fail("IllegalStateException not thrown");
913 } catch (IllegalStateException expected) {
914 assertTrue("Message was: " + expected.getMessage(),
915 expected.getMessage().contains("already has a profile owner"));
916 }
Makoto Onukib643fb02015-09-22 15:03:44 -0700917 }
918
Makoto Onuki90b89652016-01-28 14:44:18 -0800919 public void testClearProfileOwner() throws Exception {
920 setAsProfileOwner(admin1);
921
922 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
923
924 assertTrue(dpm.isProfileOwnerApp(admin1.getPackageName()));
925 assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
926
Makoto Onuki1a5ee772016-02-12 15:34:57 -0800927 // First try when the user is locked, which should fail.
928 when(mContext.userManager.isUserUnlocked(anyInt()))
929 .thenReturn(false);
930 try {
931 dpm.clearProfileOwner(admin1);
932 fail("Didn't throw IllegalStateException");
933 } catch (IllegalStateException expected) {
934 MoreAsserts.assertContainsRegex(
935 "User must be running and unlocked", expected.getMessage());
936 }
937 // Clear, really.
938 when(mContext.userManager.isUserUnlocked(anyInt()))
939 .thenReturn(true);
Makoto Onuki90b89652016-01-28 14:44:18 -0800940 dpm.clearProfileOwner(admin1);
941
942 // Check
943 assertFalse(dpm.isProfileOwnerApp(admin1.getPackageName()));
944 assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
945 }
946
Makoto Onukib643fb02015-09-22 15:03:44 -0700947 public void testSetProfileOwner_failures() throws Exception {
948 // TODO Test more failure cases. Basically test all chacks in enforceCanSetProfileOwner().
949 }
950
Makoto Onukia52562c2015-10-01 16:12:31 -0700951 public void testGetDeviceOwnerAdminLocked() throws Exception {
952 checkDeviceOwnerWithMultipleDeviceAdmins();
953 }
954
955 private void checkDeviceOwnerWithMultipleDeviceAdmins() throws Exception {
956 // In ths test, we use 3 users (system + 2 secondary users), set some device admins to them,
957 // set admin2 on CALLER_USER_HANDLE as DO, then call getDeviceOwnerAdminLocked() to
958 // make sure it gets the right component from the right user.
959
960 final int ANOTHER_USER_ID = 100;
961 final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 456);
962
963 mMockContext.addUser(ANOTHER_USER_ID, 0); // Add one more user.
964
965 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
Makoto Onukic8a5a552015-11-19 14:29:12 -0800966 mContext.callerPermissions.add(permission.MANAGE_USERS);
Makoto Onukia52562c2015-10-01 16:12:31 -0700967 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
968 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
969
970 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
971
972 // Make sure the admin packge is installed to each user.
973 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
974 setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_SYSTEM_USER_UID);
975
976 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID);
977 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID);
978
979 setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID);
980
981
982 // Set active admins to the users.
983 dpm.setActiveAdmin(admin1, /* replace =*/ false);
984 dpm.setActiveAdmin(admin3, /* replace =*/ false);
985
986 dpm.setActiveAdmin(admin1, /* replace =*/ false, DpmMockContext.CALLER_USER_HANDLE);
987 dpm.setActiveAdmin(admin2, /* replace =*/ false, DpmMockContext.CALLER_USER_HANDLE);
988
989 dpm.setActiveAdmin(admin2, /* replace =*/ false, ANOTHER_USER_ID);
990
991 // Set DO on the first non-system user.
992 mContext.setUserRunning(DpmMockContext.CALLER_USER_HANDLE, true);
993 assertTrue(dpm.setDeviceOwner(admin2, "owner-name", DpmMockContext.CALLER_USER_HANDLE));
994
Makoto Onukic8a5a552015-11-19 14:29:12 -0800995 assertEquals(admin2, dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false));
Makoto Onukia52562c2015-10-01 16:12:31 -0700996
997 // Then check getDeviceOwnerAdminLocked().
998 assertEquals(admin2, dpms.getDeviceOwnerAdminLocked().info.getComponent());
999 assertEquals(DpmMockContext.CALLER_UID, dpms.getDeviceOwnerAdminLocked().getUid());
1000 }
1001
1002 /**
1003 * This essentially tests
Makoto Onuki2a3c3da2016-02-18 14:25:30 -08001004 * {@code DevicePolicyManagerService.findOwnerComponentIfNecessaryLocked()}. (which is
1005 * private.)
Makoto Onukia52562c2015-10-01 16:12:31 -07001006 *
1007 * We didn't use to persist the DO component class name, but now we do, and the above method
1008 * finds the right component from a package name upon migration.
1009 */
1010 public void testDeviceOwnerMigration() throws Exception {
1011 checkDeviceOwnerWithMultipleDeviceAdmins();
1012
1013 // Overwrite the device owner setting and clears the clas name.
1014 dpms.mOwners.setDeviceOwner(
1015 new ComponentName(admin2.getPackageName(), ""),
1016 "owner-name", DpmMockContext.CALLER_USER_HANDLE);
1017 dpms.mOwners.writeDeviceOwner();
1018
1019 // Make sure the DO component name doesn't have a class name.
Makoto Onukic8a5a552015-11-19 14:29:12 -08001020 assertEquals("", dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false).getClassName());
Makoto Onukia52562c2015-10-01 16:12:31 -07001021
1022 // Then create a new DPMS to have it load the settings from files.
Makoto Onuki068c54a2015-10-13 14:34:03 -07001023 when(mContext.userManager.getUserRestrictions(any(UserHandle.class)))
1024 .thenReturn(new Bundle());
Makoto Onukia52562c2015-10-01 16:12:31 -07001025 initializeDpms();
1026
1027 // Now the DO component name is a full name.
1028 // *BUT* because both admin1 and admin2 belong to the same package, we think admin1 is the
1029 // DO.
Makoto Onukic8a5a552015-11-19 14:29:12 -08001030 assertEquals(admin1, dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false));
Makoto Onukia52562c2015-10-01 16:12:31 -07001031 }
1032
Makoto Onukib643fb02015-09-22 15:03:44 -07001033 public void testSetGetApplicationRestriction() {
1034 setAsProfileOwner(admin1);
1035
1036 {
1037 Bundle rest = new Bundle();
1038 rest.putString("KEY_STRING", "Foo1");
1039 dpm.setApplicationRestrictions(admin1, "pkg1", rest);
1040 }
1041
1042 {
1043 Bundle rest = new Bundle();
1044 rest.putString("KEY_STRING", "Foo2");
1045 dpm.setApplicationRestrictions(admin1, "pkg2", rest);
1046 }
1047
1048 {
1049 Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg1");
1050 assertNotNull(returned);
1051 assertEquals(returned.size(), 1);
1052 assertEquals(returned.get("KEY_STRING"), "Foo1");
1053 }
1054
1055 {
1056 Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg2");
1057 assertNotNull(returned);
1058 assertEquals(returned.size(), 1);
1059 assertEquals(returned.get("KEY_STRING"), "Foo2");
1060 }
1061
1062 dpm.setApplicationRestrictions(admin1, "pkg2", new Bundle());
1063 assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg2").size());
1064 }
Makoto Onukia4f11972015-10-01 13:19:58 -07001065
Esteban Talaverabf60f722015-12-10 16:26:44 +00001066 public void testApplicationRestrictionsManagingApp() throws Exception {
1067 setAsProfileOwner(admin1);
1068
Rubin Xued1928a2016-02-11 17:23:06 +00001069 final String nonExistAppRestrictionsManagerPackage = "com.google.app.restrictions.manager2";
Esteban Talaverabf60f722015-12-10 16:26:44 +00001070 final String appRestrictionsManagerPackage = "com.google.app.restrictions.manager";
1071 final int appRestrictionsManagerAppId = 20987;
1072 final int appRestrictionsManagerUid = UserHandle.getUid(
1073 DpmMockContext.CALLER_USER_HANDLE, appRestrictionsManagerAppId);
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001074 doReturn(appRestrictionsManagerUid).when(mContext.packageManager).getPackageUidAsUser(
Esteban Talaverabf60f722015-12-10 16:26:44 +00001075 eq(appRestrictionsManagerPackage),
1076 eq(DpmMockContext.CALLER_USER_HANDLE));
1077 mContext.binder.callingUid = appRestrictionsManagerUid;
1078
Rubin Xued1928a2016-02-11 17:23:06 +00001079 final PackageInfo pi = new PackageInfo();
1080 pi.applicationInfo = new ApplicationInfo();
1081 pi.applicationInfo.flags = ApplicationInfo.FLAG_HAS_CODE;
1082 doReturn(pi).when(mContext.ipackageManager).getPackageInfo(
1083 eq(appRestrictionsManagerPackage),
1084 anyInt(),
1085 eq(DpmMockContext.CALLER_USER_HANDLE));
1086
Esteban Talaverabf60f722015-12-10 16:26:44 +00001087 // appRestrictionsManager package shouldn't be able to manage restrictions as the PO hasn't
1088 // delegated that permission yet.
1089 assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage());
1090 Bundle rest = new Bundle();
1091 rest.putString("KEY_STRING", "Foo1");
1092 try {
1093 dpm.setApplicationRestrictions(null, "pkg1", rest);
1094 fail("Didn't throw expected SecurityException");
1095 } catch (SecurityException expected) {
1096 MoreAsserts.assertContainsRegex(
1097 "caller cannot manage application restrictions", expected.getMessage());
1098 }
1099 try {
1100 dpm.getApplicationRestrictions(null, "pkg1");
1101 fail("Didn't throw expected SecurityException");
1102 } catch (SecurityException expected) {
1103 MoreAsserts.assertContainsRegex(
1104 "caller cannot manage application restrictions", expected.getMessage());
1105 }
1106
1107 // Check via the profile owner that no restrictions were set.
1108 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
1109 assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size());
1110
Rubin Xued1928a2016-02-11 17:23:06 +00001111 // Check the API does not allow setting a non-existent package
1112 try {
1113 dpm.setApplicationRestrictionsManagingPackage(admin1,
1114 nonExistAppRestrictionsManagerPackage);
1115 fail("Non-existent app set as app restriction manager.");
1116 } catch (IllegalArgumentException expected) {
1117 MoreAsserts.assertContainsRegex(
1118 "is not installed on the current user", expected.getMessage());
1119 }
1120
Esteban Talaverabf60f722015-12-10 16:26:44 +00001121 // Let appRestrictionsManagerPackage manage app restrictions
1122 dpm.setApplicationRestrictionsManagingPackage(admin1, appRestrictionsManagerPackage);
1123 assertEquals(appRestrictionsManagerPackage,
1124 dpm.getApplicationRestrictionsManagingPackage(admin1));
1125
1126 // Now that package should be able to set and retrieve app restrictions.
1127 mContext.binder.callingUid = appRestrictionsManagerUid;
1128 assertTrue(dpm.isCallerApplicationRestrictionsManagingPackage());
1129 dpm.setApplicationRestrictions(null, "pkg1", rest);
1130 Bundle returned = dpm.getApplicationRestrictions(null, "pkg1");
1131 assertEquals(1, returned.size(), 1);
1132 assertEquals("Foo1", returned.get("KEY_STRING"));
1133
1134 // The same app running on a separate user shouldn't be able to manage app restrictions.
1135 mContext.binder.callingUid = UserHandle.getUid(
1136 UserHandle.USER_SYSTEM, appRestrictionsManagerAppId);
1137 assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage());
1138 try {
1139 dpm.setApplicationRestrictions(null, "pkg1", rest);
1140 fail("Didn't throw expected SecurityException");
1141 } catch (SecurityException expected) {
1142 MoreAsserts.assertContainsRegex(
1143 "caller cannot manage application restrictions", expected.getMessage());
1144 }
1145
1146 // The DPM is still able to manage app restrictions, even if it allowed another app to do it
1147 // too.
1148 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
1149 assertEquals(returned, dpm.getApplicationRestrictions(admin1, "pkg1"));
1150 dpm.setApplicationRestrictions(admin1, "pkg1", null);
1151 assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size());
1152
1153 // Removing the ability for the package to manage app restrictions.
1154 dpm.setApplicationRestrictionsManagingPackage(admin1, null);
1155 assertNull(dpm.getApplicationRestrictionsManagingPackage(admin1));
1156 mContext.binder.callingUid = appRestrictionsManagerUid;
1157 assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage());
1158 try {
1159 dpm.setApplicationRestrictions(null, "pkg1", null);
1160 fail("Didn't throw expected SecurityException");
1161 } catch (SecurityException expected) {
1162 MoreAsserts.assertContainsRegex(
1163 "caller cannot manage application restrictions", expected.getMessage());
1164 }
1165 }
1166
Makoto Onukia4f11972015-10-01 13:19:58 -07001167 public void testSetUserRestriction_asDo() throws Exception {
1168 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
Makoto Onukic8a5a552015-11-19 14:29:12 -08001169 mContext.callerPermissions.add(permission.MANAGE_USERS);
Makoto Onukia4f11972015-10-01 13:19:58 -07001170 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1171 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
1172
1173 // First, set DO.
1174
1175 // Call from a process on the system user.
1176 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1177
1178 // Make sure admin1 is installed on system user.
1179 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
Makoto Onukia4f11972015-10-01 13:19:58 -07001180
1181 // Call.
1182 dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM);
Makoto Onukia52562c2015-10-01 16:12:31 -07001183 assertTrue(dpm.setDeviceOwner(admin1, "owner-name",
Makoto Onukia4f11972015-10-01 13:19:58 -07001184 UserHandle.USER_SYSTEM));
1185
Makoto Onuki068c54a2015-10-13 14:34:03 -07001186 DpmTestUtils.assertRestrictions(
1187 DpmTestUtils.newRestrictions(),
1188 dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions()
1189 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001190 DpmTestUtils.assertRestrictions(
1191 DpmTestUtils.newRestrictions(),
1192 dpm.getUserRestrictions(admin1)
1193 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001194
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001195 reset(mContext.userManagerInternal);
1196
1197 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER);
1198 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1199 eq(UserHandle.USER_SYSTEM),
1200 MockUtils.checkUserRestrictions(),
1201 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER)
Makoto Onuki2a3c3da2016-02-18 14:25:30 -08001202 );
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001203 reset(mContext.userManagerInternal);
1204
Makoto Onukia4f11972015-10-01 13:19:58 -07001205 dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001206 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1207 eq(UserHandle.USER_SYSTEM),
1208 MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS),
1209 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER)
1210 );
1211 reset(mContext.userManagerInternal);
Makoto Onukia4f11972015-10-01 13:19:58 -07001212
Makoto Onuki068c54a2015-10-13 14:34:03 -07001213 DpmTestUtils.assertRestrictions(
1214 DpmTestUtils.newRestrictions(
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001215 UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS),
Makoto Onuki068c54a2015-10-13 14:34:03 -07001216 dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions()
1217 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001218 DpmTestUtils.assertRestrictions(
1219 DpmTestUtils.newRestrictions(
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001220 UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS),
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001221 dpm.getUserRestrictions(admin1)
1222 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001223
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001224 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADD_USER);
1225 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1226 eq(UserHandle.USER_SYSTEM),
1227 MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS),
1228 MockUtils.checkUserRestrictions()
1229 );
1230 reset(mContext.userManagerInternal);
Makoto Onukia4f11972015-10-01 13:19:58 -07001231
Makoto Onuki068c54a2015-10-13 14:34:03 -07001232 DpmTestUtils.assertRestrictions(
1233 DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS),
1234 dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions()
1235 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001236 DpmTestUtils.assertRestrictions(
1237 DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS),
1238 dpm.getUserRestrictions(admin1)
1239 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001240
1241 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001242 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1243 eq(UserHandle.USER_SYSTEM),
1244 MockUtils.checkUserRestrictions(),
1245 MockUtils.checkUserRestrictions()
1246 );
1247 reset(mContext.userManagerInternal);
Makoto Onukia4f11972015-10-01 13:19:58 -07001248
Makoto Onuki068c54a2015-10-13 14:34:03 -07001249 DpmTestUtils.assertRestrictions(
1250 DpmTestUtils.newRestrictions(),
1251 dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions()
1252 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001253 DpmTestUtils.assertRestrictions(
1254 DpmTestUtils.newRestrictions(),
1255 dpm.getUserRestrictions(admin1)
1256 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001257
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001258 // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE are PO restrictions, but when
1259 // DO sets them, the scope is global.
1260 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME);
1261 reset(mContext.userManagerInternal);
1262 dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1263 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1264 eq(UserHandle.USER_SYSTEM),
1265 MockUtils.checkUserRestrictions(),
1266 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME,
1267 UserManager.DISALLOW_UNMUTE_MICROPHONE)
1268 );
1269 reset(mContext.userManagerInternal);
1270
1271 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME);
1272 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1273
1274
1275 // More tests.
1276 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER);
1277 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1278 eq(UserHandle.USER_SYSTEM),
1279 MockUtils.checkUserRestrictions(),
1280 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER)
1281 );
1282 reset(mContext.userManagerInternal);
1283
1284 dpm.addUserRestriction(admin1, UserManager.DISALLOW_FUN);
1285 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1286 eq(UserHandle.USER_SYSTEM),
1287 MockUtils.checkUserRestrictions(),
1288 MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN,
1289 UserManager.DISALLOW_ADD_USER)
1290 );
1291 reset(mContext.userManagerInternal);
1292
1293 dpm.setCameraDisabled(admin1, true);
1294 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1295 eq(UserHandle.USER_SYSTEM),
1296 // DISALLOW_CAMERA will be applied to both local and global.
1297 MockUtils.checkUserRestrictions(UserManager.DISALLOW_CAMERA),
1298 MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN,
1299 UserManager.DISALLOW_CAMERA, UserManager.DISALLOW_ADD_USER)
1300 );
1301 reset(mContext.userManagerInternal);
1302
1303 // Set up another DA and let it disable camera. Now DISALLOW_CAMERA will only be applied
1304 // locally.
1305 dpm.setCameraDisabled(admin1, false);
1306 reset(mContext.userManagerInternal);
1307
1308 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID);
1309 dpm.setActiveAdmin(admin2, /* replace =*/ false, UserHandle.USER_SYSTEM);
1310 dpm.setCameraDisabled(admin2, true);
1311
1312 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1313 eq(UserHandle.USER_SYSTEM),
1314 // DISALLOW_CAMERA will be applied to both local and global.
1315 MockUtils.checkUserRestrictions(UserManager.DISALLOW_CAMERA),
1316 MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN,
1317 UserManager.DISALLOW_ADD_USER)
1318 );
1319 reset(mContext.userManagerInternal);
Makoto Onukia4f11972015-10-01 13:19:58 -07001320 // TODO Make sure restrictions are written to the file.
1321 }
1322
1323 public void testSetUserRestriction_asPo() {
1324 setAsProfileOwner(admin1);
1325
Makoto Onuki068c54a2015-10-13 14:34:03 -07001326 DpmTestUtils.assertRestrictions(
1327 DpmTestUtils.newRestrictions(),
1328 dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE)
1329 .ensureUserRestrictions()
1330 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001331
1332 dpm.addUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001333 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1334 eq(DpmMockContext.CALLER_USER_HANDLE),
1335 MockUtils.checkUserRestrictions(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES),
1336 isNull(Bundle.class)
1337 );
1338 reset(mContext.userManagerInternal);
1339
Makoto Onukia4f11972015-10-01 13:19:58 -07001340 dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001341 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1342 eq(DpmMockContext.CALLER_USER_HANDLE),
1343 MockUtils.checkUserRestrictions(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
1344 UserManager.DISALLOW_OUTGOING_CALLS),
1345 isNull(Bundle.class)
1346 );
1347 reset(mContext.userManagerInternal);
Makoto Onukia4f11972015-10-01 13:19:58 -07001348
Makoto Onuki068c54a2015-10-13 14:34:03 -07001349 DpmTestUtils.assertRestrictions(
1350 DpmTestUtils.newRestrictions(
1351 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
1352 UserManager.DISALLOW_OUTGOING_CALLS
1353 ),
1354 dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE)
1355 .ensureUserRestrictions()
1356 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001357 DpmTestUtils.assertRestrictions(
1358 DpmTestUtils.newRestrictions(
1359 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
1360 UserManager.DISALLOW_OUTGOING_CALLS
1361 ),
1362 dpm.getUserRestrictions(admin1)
1363 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001364
1365 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001366 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1367 eq(DpmMockContext.CALLER_USER_HANDLE),
1368 MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS),
1369 isNull(Bundle.class)
1370 );
1371 reset(mContext.userManagerInternal);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001372
1373 DpmTestUtils.assertRestrictions(
1374 DpmTestUtils.newRestrictions(
1375 UserManager.DISALLOW_OUTGOING_CALLS
1376 ),
1377 dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE)
1378 .ensureUserRestrictions()
1379 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001380 DpmTestUtils.assertRestrictions(
1381 DpmTestUtils.newRestrictions(
1382 UserManager.DISALLOW_OUTGOING_CALLS
1383 ),
1384 dpm.getUserRestrictions(admin1)
1385 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001386
1387 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001388 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1389 eq(DpmMockContext.CALLER_USER_HANDLE),
1390 MockUtils.checkUserRestrictions(),
1391 isNull(Bundle.class)
1392 );
1393 reset(mContext.userManagerInternal);
Makoto Onukia4f11972015-10-01 13:19:58 -07001394
Makoto Onuki068c54a2015-10-13 14:34:03 -07001395 DpmTestUtils.assertRestrictions(
1396 DpmTestUtils.newRestrictions(),
1397 dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE)
1398 .ensureUserRestrictions()
1399 );
Makoto Onuki3a3092f2015-10-30 11:07:51 -07001400 DpmTestUtils.assertRestrictions(
1401 DpmTestUtils.newRestrictions(),
1402 dpm.getUserRestrictions(admin1)
1403 );
Makoto Onukia4f11972015-10-01 13:19:58 -07001404
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001405 // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE can be set by PO too, even
1406 // though when DO sets them they'll be applied globally.
1407 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME);
1408 reset(mContext.userManagerInternal);
1409 dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE);
1410 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1411 eq(DpmMockContext.CALLER_USER_HANDLE),
1412 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME,
1413 UserManager.DISALLOW_UNMUTE_MICROPHONE),
1414 isNull(Bundle.class)
1415 );
1416 reset(mContext.userManagerInternal);
1417
1418 dpm.setCameraDisabled(admin1, true);
1419 verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions(
1420 eq(DpmMockContext.CALLER_USER_HANDLE),
1421 MockUtils.checkUserRestrictions(UserManager.DISALLOW_CAMERA,
1422 UserManager.DISALLOW_ADJUST_VOLUME,
1423 UserManager.DISALLOW_UNMUTE_MICROPHONE),
1424 isNull(Bundle.class)
1425 );
1426 reset(mContext.userManagerInternal);
1427
Makoto Onukia4f11972015-10-01 13:19:58 -07001428 // TODO Make sure restrictions are written to the file.
1429 }
Makoto Onukia31ebbc2015-11-23 17:15:21 -08001430
1431 public void testGetMacAddress() throws Exception {
1432 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
1433 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1434 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
1435
1436 // In this test, change the caller user to "system".
1437 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1438
1439 // Make sure admin1 is installed on system user.
1440 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
1441
1442 // Test 1. Caller doesn't have DO or DA.
1443 try {
1444 dpm.getWifiMacAddress();
1445 fail();
1446 } catch (SecurityException e) {
1447 MoreAsserts.assertContainsRegex("No active admin owned", e.getMessage());
1448 }
1449
1450 // DO needs to be an DA.
1451 dpm.setActiveAdmin(admin1, /* replace =*/ false);
1452 assertTrue(dpm.isAdminActive(admin1));
1453
1454 // Test 2. Caller has DA, but not DO.
1455 try {
1456 dpm.getWifiMacAddress();
1457 fail();
1458 } catch (SecurityException e) {
1459 MoreAsserts.assertContainsRegex("No active admin owned", e.getMessage());
1460 }
1461
1462 // Test 3. Caller has PO, but not DO.
1463 assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM));
1464 try {
1465 dpm.getWifiMacAddress();
1466 fail();
1467 } catch (SecurityException e) {
1468 MoreAsserts.assertContainsRegex("No active admin owned", e.getMessage());
1469 }
1470
1471 // Remove PO.
1472 dpm.clearProfileOwner(admin1);
1473
1474 // Test 4, Caller is DO now.
1475 assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM));
1476
1477 // 4-1. But no WifiInfo.
1478 assertNull(dpm.getWifiMacAddress());
1479
1480 // 4-2. Returns WifiInfo, but with the default MAC.
1481 when(mContext.wifiManager.getConnectionInfo()).thenReturn(new WifiInfo());
1482 assertNull(dpm.getWifiMacAddress());
1483
1484 // 4-3. With a real MAC address.
1485 final WifiInfo wi = new WifiInfo();
1486 wi.setMacAddress("11:22:33:44:55:66");
1487 when(mContext.wifiManager.getConnectionInfo()).thenReturn(wi);
1488 assertEquals("11:22:33:44:55:66", dpm.getWifiMacAddress());
1489 }
Mahaver Chopraf8373b52015-12-23 14:42:18 +00001490
1491 public void testRebootCanOnlyBeCalledByDeviceOwner() throws Exception {
1492 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
1493 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1494
1495 // In this test, change the caller user to "system".
1496 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1497
1498 // Make sure admin1 is installed on system user.
1499 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
1500
1501 // Set admin1 as DA.
1502 dpm.setActiveAdmin(admin1, false);
1503 assertTrue(dpm.isAdminActive(admin1));
1504 try {
1505 dpm.reboot(admin1);
1506 fail("DA calls DPM.reboot(), did not throw expected SecurityException");
1507 } catch (SecurityException expected) {
1508 MoreAsserts.assertContainsRegex("does not own the device", expected.getMessage());
1509 }
1510
1511 // Set admin1 as PO.
1512 assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM));
1513 try {
1514 dpm.reboot(admin1);
1515 fail("PO calls DPM.reboot(), did not throw expected SecurityException");
1516 } catch (SecurityException expected) {
1517 MoreAsserts.assertContainsRegex("does not own the device", expected.getMessage());
1518 }
1519
1520 // Remove PO and add DO.
1521 dpm.clearProfileOwner(admin1);
1522 assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM));
1523
1524 dpm.reboot(admin1);
1525 }
Kenny Guy06de4e72015-12-22 12:07:39 +00001526
1527 public void testSetGetSupportText() {
1528 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
1529 dpm.setActiveAdmin(admin1, true);
1530 dpm.setActiveAdmin(admin2, true);
1531 mContext.callerPermissions.remove(permission.MANAGE_DEVICE_ADMINS);
1532
1533 // Null default support messages.
1534 {
1535 assertNull(dpm.getLongSupportMessage(admin1));
1536 assertNull(dpm.getShortSupportMessage(admin1));
1537 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
1538 assertNull(dpm.getShortSupportMessageForUser(admin1,
1539 DpmMockContext.CALLER_USER_HANDLE));
1540 assertNull(dpm.getLongSupportMessageForUser(admin1,
1541 DpmMockContext.CALLER_USER_HANDLE));
1542 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID;
1543 }
1544
1545 // Only system can call the per user versions.
1546 {
1547 try {
1548 dpm.getShortSupportMessageForUser(admin1,
1549 DpmMockContext.CALLER_USER_HANDLE);
1550 fail("Only system should be able to call getXXXForUser versions");
1551 } catch (SecurityException expected) {
1552 MoreAsserts.assertContainsRegex("message for user", expected.getMessage());
1553 }
1554 try {
1555 dpm.getLongSupportMessageForUser(admin1,
1556 DpmMockContext.CALLER_USER_HANDLE);
1557 fail("Only system should be able to call getXXXForUser versions");
1558 } catch (SecurityException expected) {
1559 MoreAsserts.assertContainsRegex("message for user", expected.getMessage());
1560 }
1561 }
1562
1563 // Can't set message for admin in another uid.
1564 {
1565 mContext.binder.callingUid = DpmMockContext.CALLER_UID + 1;
1566 try {
1567 dpm.setShortSupportMessage(admin1, "Some text");
1568 fail("Admins should only be able to change their own support text.");
1569 } catch (SecurityException expected) {
1570 MoreAsserts.assertContainsRegex("is not owned by uid", expected.getMessage());
1571 }
1572 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
1573 }
1574
1575 // Set/Get short returns what it sets and other admins text isn't changed.
1576 {
1577 final String supportText = "Some text to test with.";
1578 dpm.setShortSupportMessage(admin1, supportText);
1579 assertEquals(supportText, dpm.getShortSupportMessage(admin1));
1580 assertNull(dpm.getLongSupportMessage(admin1));
1581 assertNull(dpm.getShortSupportMessage(admin2));
1582
1583 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
1584 assertEquals(supportText, dpm.getShortSupportMessageForUser(admin1,
1585 DpmMockContext.CALLER_USER_HANDLE));
1586 assertNull(dpm.getShortSupportMessageForUser(admin2,
1587 DpmMockContext.CALLER_USER_HANDLE));
1588 assertNull(dpm.getLongSupportMessageForUser(admin1,
1589 DpmMockContext.CALLER_USER_HANDLE));
1590 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID;
1591
1592 dpm.setShortSupportMessage(admin1, null);
1593 assertNull(dpm.getShortSupportMessage(admin1));
1594 }
1595
1596 // Set/Get long returns what it sets and other admins text isn't changed.
1597 {
1598 final String supportText = "Some text to test with.\nWith more text.";
1599 dpm.setLongSupportMessage(admin1, supportText);
1600 assertEquals(supportText, dpm.getLongSupportMessage(admin1));
1601 assertNull(dpm.getShortSupportMessage(admin1));
1602 assertNull(dpm.getLongSupportMessage(admin2));
1603
1604 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID;
1605 assertEquals(supportText, dpm.getLongSupportMessageForUser(admin1,
1606 DpmMockContext.CALLER_USER_HANDLE));
1607 assertNull(dpm.getLongSupportMessageForUser(admin2,
1608 DpmMockContext.CALLER_USER_HANDLE));
1609 assertNull(dpm.getShortSupportMessageForUser(admin1,
1610 DpmMockContext.CALLER_USER_HANDLE));
1611 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID;
1612
1613 dpm.setLongSupportMessage(admin1, null);
1614 assertNull(dpm.getLongSupportMessage(admin1));
1615 }
1616 }
Bartosz Fabianowski4c052f22016-01-25 14:18:43 +01001617
1618 /**
1619 * Test for:
1620 * {@link DevicePolicyManager#setAffiliationIds}
1621 * {@link DevicePolicyManager#isAffiliatedUser}
1622 */
1623 public void testUserAffiliation() throws Exception {
1624 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
1625 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1626 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
1627
1628 // Check that the system user is unaffiliated.
1629 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1630 assertFalse(dpm.isAffiliatedUser());
1631
1632 // Set a device owner on the system user. Check that the system user becomes affiliated.
1633 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
1634 dpm.setActiveAdmin(admin1, /* replace =*/ false);
1635 assertTrue(dpm.setDeviceOwner(admin1, "owner-name"));
1636 assertTrue(dpm.isAffiliatedUser());
1637
1638 // Install a profile owner whose package name matches the device owner on a test user. Check
1639 // that the test user is unaffiliated.
1640 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
1641 setAsProfileOwner(admin2);
1642 assertFalse(dpm.isAffiliatedUser());
1643
1644 // Have the profile owner specify a set of affiliation ids. Check that the test user remains
1645 // unaffiliated.
1646 final Set<String> userAffiliationIds = new ArraySet<>();
1647 userAffiliationIds.add("red");
1648 userAffiliationIds.add("green");
1649 userAffiliationIds.add("blue");
1650 dpm.setAffiliationIds(admin2, userAffiliationIds);
1651 assertFalse(dpm.isAffiliatedUser());
1652
1653 // Have the device owner specify a set of affiliation ids that do not intersect with those
1654 // specified by the profile owner. Check that the test user remains unaffiliated.
1655 final Set<String> deviceAffiliationIds = new ArraySet<>();
1656 deviceAffiliationIds.add("cyan");
1657 deviceAffiliationIds.add("yellow");
1658 deviceAffiliationIds.add("magenta");
1659 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1660 dpm.setAffiliationIds(admin1, deviceAffiliationIds);
1661 mContext.binder.callingUid = DpmMockContext.CALLER_UID;
1662 assertFalse(dpm.isAffiliatedUser());
1663
1664 // Have the profile owner specify a set of affiliation ids that intersect with those
1665 // specified by the device owner. Check that the test user becomes affiliated.
1666 userAffiliationIds.add("yellow");
1667 dpm.setAffiliationIds(admin2, userAffiliationIds);
1668 assertTrue(dpm.isAffiliatedUser());
1669
1670 // Change the profile owner to one whose package name does not match the device owner. Check
1671 // that the test user is not affiliated anymore.
1672 dpm.clearProfileOwner(admin2);
1673 final ComponentName admin = new ComponentName("test", "test");
Makoto Onuki184db602016-02-17 15:35:01 -08001674
1675 setUpPackageManagerForFakeAdmin(admin, DpmMockContext.CALLER_UID,
1676 /* enabledSetting =*/ PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
1677 /* appTargetSdk = */ null, admin2);
1678
1679 dpm.setActiveAdmin(admin, /* refreshing =*/ true, DpmMockContext.CALLER_USER_HANDLE);
Bartosz Fabianowski4c052f22016-01-25 14:18:43 +01001680 assertTrue(dpm.setProfileOwner(admin, "owner-name", DpmMockContext.CALLER_USER_HANDLE));
1681 assertFalse(dpm.isAffiliatedUser());
1682
1683 // Check that the system user remains affiliated.
1684 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1685 assertTrue(dpm.isAffiliatedUser());
1686 }
Alan Treadwayafad8782016-01-19 15:15:08 +00001687
1688 public void testGetUserProvisioningState_defaultResult() {
1689 assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState());
1690 }
1691
1692 public void testSetUserProvisioningState_permission() throws Exception {
1693 setupProfileOwner();
1694 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1695
1696 exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE,
1697 DevicePolicyManager.STATE_USER_SETUP_FINALIZED);
1698 }
1699
1700 public void testSetUserProvisioningState_unprivileged() throws Exception {
1701 setupProfileOwner();
1702 try {
1703 dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED,
1704 DpmMockContext.CALLER_USER_HANDLE);
1705 fail("Expected SecurityException");
1706 } catch (SecurityException expected) {
1707 }
1708 }
1709
1710 public void testSetUserProvisioningState_noManagement() {
1711 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1712 try {
1713 dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED,
1714 DpmMockContext.CALLER_USER_HANDLE);
1715 fail("IllegalStateException expected");
1716 } catch (IllegalStateException e) {
1717 MoreAsserts.assertContainsRegex("change provisioning state unless a .* owner is set",
1718 e.getMessage());
1719 }
1720 assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState());
1721 }
1722
1723 public void testSetUserProvisioningState_deviceOwnerFromSetupWizard() throws Exception {
1724 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1725 setupDeviceOwner();
1726 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1727
1728 exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM,
1729 DevicePolicyManager.STATE_USER_SETUP_COMPLETE,
1730 DevicePolicyManager.STATE_USER_SETUP_FINALIZED);
1731 }
1732
1733 public void testSetUserProvisioningState_deviceOwnerFromSetupWizardAlternative()
1734 throws Exception {
1735 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1736 setupDeviceOwner();
1737 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1738
1739 exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM,
1740 DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE,
1741 DevicePolicyManager.STATE_USER_SETUP_FINALIZED);
1742 }
1743
1744 public void testSetUserProvisioningState_deviceOwnerWithoutSetupWizard() throws Exception {
1745 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
1746 setupDeviceOwner();
1747 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1748
1749 exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM,
1750 DevicePolicyManager.STATE_USER_SETUP_FINALIZED);
1751 }
1752
1753 public void testSetUserProvisioningState_managedProfileFromSetupWizard_primaryUser()
1754 throws Exception {
1755 setupProfileOwner();
1756 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1757
1758 exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE,
1759 DevicePolicyManager.STATE_USER_PROFILE_COMPLETE,
1760 DevicePolicyManager.STATE_USER_UNMANAGED);
1761 }
1762
1763 public void testSetUserProvisioningState_managedProfileFromSetupWizard_managedProfile()
1764 throws Exception {
1765 setupProfileOwner();
1766 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1767
1768 exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE,
1769 DevicePolicyManager.STATE_USER_SETUP_COMPLETE,
1770 DevicePolicyManager.STATE_USER_SETUP_FINALIZED);
1771 }
1772
1773 public void testSetUserProvisioningState_managedProfileWithoutSetupWizard() throws Exception {
1774 setupProfileOwner();
1775 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1776
1777 exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE,
1778 DevicePolicyManager.STATE_USER_SETUP_FINALIZED);
1779 }
1780
1781 public void testSetUserProvisioningState_illegalTransitionOutOfFinalized1() throws Exception {
1782 setupProfileOwner();
1783 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1784
1785 try {
1786 exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE,
1787 DevicePolicyManager.STATE_USER_SETUP_FINALIZED,
1788 DevicePolicyManager.STATE_USER_UNMANAGED);
1789 fail("Expected IllegalStateException");
1790 } catch (IllegalStateException e) {
1791 MoreAsserts.assertContainsRegex("Cannot move to user provisioning state",
1792 e.getMessage());
1793 }
1794 }
1795
1796 public void testSetUserProvisioningState_illegalTransitionToAnotherInProgressState()
1797 throws Exception {
1798 setupProfileOwner();
1799 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
1800
1801 try {
1802 exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE,
1803 DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE,
1804 DevicePolicyManager.STATE_USER_SETUP_COMPLETE);
1805 fail("Expected IllegalStateException");
1806 } catch (IllegalStateException e) {
1807 MoreAsserts.assertContainsRegex("Cannot move to user provisioning state",
1808 e.getMessage());
1809 }
1810 }
1811
1812 private void exerciseUserProvisioningTransitions(int userId, int... states) {
1813 assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState());
1814 for (int state : states) {
1815 dpm.setUserProvisioningState(state, userId);
1816 assertEquals(state, dpm.getUserProvisioningState());
1817 }
1818 }
1819
1820 private void setupProfileOwner() throws Exception {
1821 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS);
1822
1823 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID);
1824 dpm.setActiveAdmin(admin1, false);
1825 assertTrue(dpm.setProfileOwner(admin1, null, DpmMockContext.CALLER_USER_HANDLE));
1826
1827 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS);
1828 }
1829
1830 private void setupDeviceOwner() throws Exception {
1831 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS);
1832
1833 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
1834 dpm.setActiveAdmin(admin1, false);
1835 assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM));
1836
1837 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS);
1838 }
Makoto Onuki2a3c3da2016-02-18 14:25:30 -08001839
1840 public void testSetMaximumTimeToLock() {
1841 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS);
1842
1843 dpm.setActiveAdmin(admin1, /* replace =*/ false);
1844 dpm.setActiveAdmin(admin2, /* replace =*/ false);
1845
1846 reset(mMockContext.powerManagerInternal);
1847 reset(mMockContext.settings);
1848
1849 dpm.setMaximumTimeToLock(admin1, 0);
1850 verifyScreenTimeoutCall(null, false);
1851 reset(mMockContext.powerManagerInternal);
1852 reset(mMockContext.settings);
1853
1854 dpm.setMaximumTimeToLock(admin1, 1);
1855 verifyScreenTimeoutCall(1, true);
1856 reset(mMockContext.powerManagerInternal);
1857 reset(mMockContext.settings);
1858
1859 dpm.setMaximumTimeToLock(admin2, 10);
1860 verifyScreenTimeoutCall(null, false);
1861 reset(mMockContext.powerManagerInternal);
1862 reset(mMockContext.settings);
1863
1864 dpm.setMaximumTimeToLock(admin1, 5);
1865 verifyScreenTimeoutCall(5, true);
1866 reset(mMockContext.powerManagerInternal);
1867 reset(mMockContext.settings);
1868
1869 dpm.setMaximumTimeToLock(admin2, 4);
1870 verifyScreenTimeoutCall(4, true);
1871 reset(mMockContext.powerManagerInternal);
1872 reset(mMockContext.settings);
1873
1874 dpm.setMaximumTimeToLock(admin1, 0);
1875 reset(mMockContext.powerManagerInternal);
1876 reset(mMockContext.settings);
1877
1878 dpm.setMaximumTimeToLock(admin2, Integer.MAX_VALUE);
1879 verifyScreenTimeoutCall(Integer.MAX_VALUE, true);
1880 reset(mMockContext.powerManagerInternal);
1881 reset(mMockContext.settings);
1882
1883 dpm.setMaximumTimeToLock(admin2, Integer.MAX_VALUE + 1);
1884 verifyScreenTimeoutCall(Integer.MAX_VALUE, true);
1885 reset(mMockContext.powerManagerInternal);
1886 reset(mMockContext.settings);
1887
1888 dpm.setMaximumTimeToLock(admin2, 10);
1889 verifyScreenTimeoutCall(10, true);
1890 reset(mMockContext.powerManagerInternal);
1891 reset(mMockContext.settings);
1892
1893 // There's no restriction; shold be set to MAX.
1894 dpm.setMaximumTimeToLock(admin2, 0);
1895 verifyScreenTimeoutCall(Integer.MAX_VALUE, false);
1896 }
1897
1898 private void verifyScreenTimeoutCall(Integer expectedTimeout,
1899 boolean shouldStayOnWhilePluggedInBeCleared) {
1900 if (expectedTimeout == null) {
1901 verify(mMockContext.powerManagerInternal, times(0))
1902 .setMaximumScreenOffTimeoutFromDeviceAdmin(anyInt());
1903 } else {
1904 verify(mMockContext.powerManagerInternal, times(1))
1905 .setMaximumScreenOffTimeoutFromDeviceAdmin(eq(expectedTimeout));
1906 }
1907 // TODO Verify calls to settingsGlobalPutInt. Tried but somehow mockito threw
1908 // UnfinishedVerificationException.
1909 }
Makoto Onukicc4bbeb2015-09-17 10:28:24 -07001910}
Makoto Onuki2a3c3da2016-02-18 14:25:30 -08001911