Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 16 | package com.android.server.devicepolicy; |
| 17 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 18 | import static android.os.UserManagerInternal.CAMERA_DISABLED_GLOBALLY; |
| 19 | import static android.os.UserManagerInternal.CAMERA_DISABLED_LOCALLY; |
| 20 | import static android.os.UserManagerInternal.CAMERA_NOT_DISABLED; |
| 21 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 22 | import android.Manifest.permission; |
| 23 | import android.app.Activity; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 24 | import android.app.Notification; |
| 25 | import android.app.NotificationManager; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 26 | import android.app.admin.DeviceAdminReceiver; |
| 27 | import android.app.admin.DevicePolicyManager; |
| 28 | import android.app.admin.DevicePolicyManagerInternal; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 30 | import android.content.ComponentName; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.ServiceConnection; |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 34 | import android.content.pm.ApplicationInfo; |
| 35 | import android.content.pm.PackageInfo; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 36 | import android.content.pm.PackageManager; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 37 | import android.content.pm.ParceledListSlice; |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 38 | import android.content.res.Resources; |
| 39 | import android.graphics.Color; |
| 40 | import android.net.IIpConnectivityMetrics; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 41 | import android.content.pm.UserInfo; |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 42 | import android.net.wifi.WifiInfo; |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 43 | import android.os.Build.VERSION_CODES; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 44 | import android.os.Bundle; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 45 | import android.os.IBinder; |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 46 | import android.os.Process; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 48 | import android.os.UserManager; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 49 | import android.os.UserManagerInternal; |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 50 | import android.provider.Settings; |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 51 | import android.telephony.TelephonyManager; |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 52 | import android.test.MoreAsserts; |
Benjamin Franz | 6d00903 | 2016-01-25 18:56:38 +0000 | [diff] [blame] | 53 | import android.test.suitebuilder.annotation.SmallTest; |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 54 | import android.util.ArraySet; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 55 | import android.util.Pair; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 56 | |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 57 | import com.android.internal.R; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 58 | import com.android.internal.util.ParcelableString; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 59 | import com.android.server.LocalServices; |
| 60 | import com.android.server.SystemService; |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 61 | import com.android.server.pm.UserRestrictionsUtils; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 62 | |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 63 | import org.hamcrest.BaseMatcher; |
| 64 | import org.hamcrest.Description; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 65 | import org.mockito.invocation.InvocationOnMock; |
| 66 | import org.mockito.stubbing.Answer; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 67 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 68 | import java.util.ArrayList; |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 69 | import java.util.Arrays; |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 70 | import java.util.Collections; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 71 | import java.util.HashMap; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 72 | import java.util.List; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 73 | import java.util.Map; |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 74 | import java.util.Set; |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 75 | import java.util.concurrent.TimeUnit; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 76 | |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 77 | import static android.app.admin.DevicePolicyManager.DELEGATION_APP_RESTRICTIONS; |
| 78 | import static android.app.admin.DevicePolicyManager.DELEGATION_CERT_INSTALL; |
| 79 | |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 80 | import static org.mockito.Matchers.any; |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 81 | import static org.mockito.Matchers.anyInt; |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 82 | import static org.mockito.Matchers.anyLong; |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 83 | import static org.mockito.Matchers.anyObject; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 84 | import static org.mockito.Matchers.anyString; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 85 | import static org.mockito.Matchers.argThat; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 86 | import static org.mockito.Matchers.eq; |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 87 | import static org.mockito.Matchers.isNull; |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 88 | import static org.mockito.Mockito.atLeast; |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 89 | import static org.mockito.Mockito.doAnswer; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 90 | import static org.mockito.Mockito.doReturn; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 91 | import static org.mockito.Mockito.mock; |
Robin Lee | d2a73ed | 2016-12-19 09:07:16 +0000 | [diff] [blame] | 92 | import static org.mockito.Mockito.never; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 93 | import static org.mockito.Mockito.reset; |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 94 | import static org.mockito.Mockito.timeout; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 95 | import static org.mockito.Mockito.times; |
| 96 | import static org.mockito.Mockito.verify; |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 97 | import static org.mockito.Mockito.verifyZeroInteractions; |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 98 | import static org.mockito.Mockito.when; |
| 99 | |
| 100 | /** |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 101 | * Tests for DevicePolicyManager( and DevicePolicyManagerService). |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 102 | * You can run them via: |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 103 | m FrameworksServicesTests && |
| 104 | adb install \ |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 105 | -r ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 106 | adb shell am instrument -e class com.android.server.devicepolicy.DevicePolicyManagerTest \ |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 107 | -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 108 | |
| 109 | (mmma frameworks/base/services/tests/servicestests/ for non-ninja build) |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 110 | * |
| 111 | * , or: |
| 112 | * runtest -c com.android.server.devicepolicy.DevicePolicyManagerTest frameworks-services |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 113 | */ |
Benjamin Franz | 6d00903 | 2016-01-25 18:56:38 +0000 | [diff] [blame] | 114 | @SmallTest |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 115 | public class DevicePolicyManagerTest extends DpmTestBase { |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 116 | private static final List<String> OWNER_SETUP_PERMISSIONS = Arrays.asList( |
| 117 | permission.MANAGE_DEVICE_ADMINS, permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, |
| 118 | permission.MANAGE_USERS, permission.INTERACT_ACROSS_USERS_FULL); |
| 119 | |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 120 | private DpmMockContext mContext; |
| 121 | public DevicePolicyManager dpm; |
| 122 | public DevicePolicyManagerServiceTestable dpms; |
| 123 | |
| 124 | @Override |
| 125 | protected void setUp() throws Exception { |
| 126 | super.setUp(); |
| 127 | |
| 128 | mContext = getContext(); |
| 129 | |
| 130 | when(mContext.packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) |
| 131 | .thenReturn(true); |
| 132 | |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 133 | // By default, pretend all users are running and unlocked. |
| 134 | when(mContext.userManager.isUserUnlocked(anyInt())).thenReturn(true); |
| 135 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 136 | initializeDpms(); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 137 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 138 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 139 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); |
| 140 | setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_UID); |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 141 | setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 142 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 143 | setUpUserManager(); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 146 | private void initializeDpms() { |
| 147 | // Need clearCallingIdentity() to pass permission checks. |
| 148 | final long ident = mContext.binder.clearCallingIdentity(); |
| 149 | try { |
| 150 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
| 151 | |
| 152 | dpms = new DevicePolicyManagerServiceTestable(mContext, dataDir); |
| 153 | |
| 154 | dpms.systemReady(SystemService.PHASE_LOCK_SETTINGS_READY); |
| 155 | dpms.systemReady(SystemService.PHASE_BOOT_COMPLETED); |
| 156 | |
| 157 | dpm = new DevicePolicyManagerTestable(mContext, dpms); |
| 158 | } finally { |
| 159 | mContext.binder.restoreCallingIdentity(ident); |
| 160 | } |
| 161 | } |
| 162 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 163 | private void setUpUserManager() { |
| 164 | // Emulate UserManager.set/getApplicationRestriction(). |
| 165 | final Map<Pair<String, UserHandle>, Bundle> appRestrictions = new HashMap<>(); |
| 166 | |
| 167 | // UM.setApplicationRestrictions() will save to appRestrictions. |
| 168 | doAnswer(new Answer<Void>() { |
| 169 | @Override |
| 170 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 171 | String pkg = (String) invocation.getArguments()[0]; |
| 172 | Bundle bundle = (Bundle) invocation.getArguments()[1]; |
| 173 | UserHandle user = (UserHandle) invocation.getArguments()[2]; |
| 174 | |
| 175 | appRestrictions.put(Pair.create(pkg, user), bundle); |
| 176 | |
| 177 | return null; |
| 178 | } |
| 179 | }).when(mContext.userManager).setApplicationRestrictions( |
| 180 | anyString(), any(Bundle.class), any(UserHandle.class)); |
| 181 | |
| 182 | // UM.getApplicationRestrictions() will read from appRestrictions. |
| 183 | doAnswer(new Answer<Bundle>() { |
| 184 | @Override |
| 185 | public Bundle answer(InvocationOnMock invocation) throws Throwable { |
| 186 | String pkg = (String) invocation.getArguments()[0]; |
| 187 | UserHandle user = (UserHandle) invocation.getArguments()[1]; |
| 188 | |
| 189 | return appRestrictions.get(Pair.create(pkg, user)); |
| 190 | } |
| 191 | }).when(mContext.userManager).getApplicationRestrictions( |
| 192 | anyString(), any(UserHandle.class)); |
| 193 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 194 | // Add the first secondary user. |
| 195 | mContext.addUser(DpmMockContext.CALLER_USER_HANDLE, 0); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private void setAsProfileOwner(ComponentName admin) { |
| 199 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 200 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 201 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 202 | // PO needs to be an DA. |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 203 | dpm.setActiveAdmin(admin, /* replace =*/ false); |
| 204 | |
| 205 | // Fire! |
| 206 | assertTrue(dpm.setProfileOwner(admin, "owner-name", DpmMockContext.CALLER_USER_HANDLE)); |
| 207 | |
| 208 | // Check |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 209 | assertEquals(admin, dpm.getProfileOwnerAsUser(DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | public void testHasNoFeature() throws Exception { |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 213 | when(mContext.packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) |
| 214 | .thenReturn(false); |
| 215 | |
| 216 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
| 217 | new DevicePolicyManagerServiceTestable(mContext, dataDir); |
| 218 | |
| 219 | // If the device has no DPMS feature, it shouldn't register the local service. |
| 220 | assertNull(LocalServices.getService(DevicePolicyManagerInternal.class)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Caller doesn't have proper permissions. |
| 225 | */ |
| 226 | public void testSetActiveAdmin_SecurityException() { |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 227 | // 1. Failure cases. |
| 228 | |
| 229 | // Caller doesn't have MANAGE_DEVICE_ADMINS. |
| 230 | try { |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 231 | dpm.setActiveAdmin(admin1, false); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 232 | fail("Didn't throw SecurityException"); |
| 233 | } catch (SecurityException expected) { |
| 234 | } |
| 235 | |
| 236 | // Caller has MANAGE_DEVICE_ADMINS, but for different user. |
| 237 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 238 | try { |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 239 | dpm.setActiveAdmin(admin1, false, DpmMockContext.CALLER_USER_HANDLE + 1); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 240 | fail("Didn't throw SecurityException"); |
| 241 | } catch (SecurityException expected) { |
| 242 | } |
| 243 | } |
| 244 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Test for: |
| 247 | * {@link DevicePolicyManager#setActiveAdmin} |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 248 | * with replace=false and replace=true |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 249 | * {@link DevicePolicyManager#isAdminActive} |
| 250 | * {@link DevicePolicyManager#isAdminActiveAsUser} |
| 251 | * {@link DevicePolicyManager#getActiveAdmins} |
| 252 | * {@link DevicePolicyManager#getActiveAdminsAsUser} |
| 253 | */ |
| 254 | public void testSetActiveAdmin() throws Exception { |
| 255 | // 1. Make sure the caller has proper permissions. |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 256 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 257 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 258 | // 2. Call the API. |
| 259 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 260 | |
| 261 | // 3. Verify internal calls. |
| 262 | |
| 263 | // Check if the boradcast is sent. |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 264 | verify(mContext.spiedContext).sendBroadcastAsUser( |
| 265 | MockUtils.checkIntentAction( |
| 266 | DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), |
| 267 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 268 | verify(mContext.spiedContext).sendBroadcastAsUser( |
| 269 | MockUtils.checkIntentAction( |
| 270 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED), |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 271 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 272 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 273 | verify(mContext.ipackageManager, times(1)).setApplicationEnabledSetting( |
| 274 | eq(admin1.getPackageName()), |
| 275 | eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT), |
| 276 | eq(PackageManager.DONT_KILL_APP), |
| 277 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 278 | anyString()); |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 279 | |
| 280 | // TODO Verify other calls too. |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 281 | |
| 282 | // Make sure it's active admin1. |
| 283 | assertTrue(dpm.isAdminActive(admin1)); |
| 284 | assertFalse(dpm.isAdminActive(admin2)); |
| 285 | assertFalse(dpm.isAdminActive(admin3)); |
| 286 | |
| 287 | // But not admin1 for a different user. |
| 288 | |
| 289 | // For this to work, caller needs android.permission.INTERACT_ACROSS_USERS_FULL. |
| 290 | // (Because we're checking a different user's status from CALLER_USER_HANDLE.) |
| 291 | mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 292 | |
| 293 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE + 1)); |
| 294 | assertFalse(dpm.isAdminActiveAsUser(admin2, DpmMockContext.CALLER_USER_HANDLE + 1)); |
| 295 | |
| 296 | mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 297 | |
| 298 | // Next, add one more admin. |
| 299 | // Before doing so, update the application info, now it's enabled. |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 300 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID, |
| 301 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 302 | |
| 303 | dpm.setActiveAdmin(admin2, /* replace =*/ false); |
| 304 | |
| 305 | // Now we have two admins. |
| 306 | assertTrue(dpm.isAdminActive(admin1)); |
| 307 | assertTrue(dpm.isAdminActive(admin2)); |
| 308 | assertFalse(dpm.isAdminActive(admin3)); |
| 309 | |
| 310 | // Admin2 was already enabled, so setApplicationEnabledSetting() shouldn't have called |
| 311 | // again. (times(1) because it was previously called for admin1) |
| 312 | verify(mContext.ipackageManager, times(1)).setApplicationEnabledSetting( |
| 313 | eq(admin1.getPackageName()), |
| 314 | eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT), |
| 315 | eq(PackageManager.DONT_KILL_APP), |
| 316 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 317 | anyString()); |
| 318 | |
| 319 | // 4. Add the same admin1 again without replace, which should throw. |
| 320 | try { |
| 321 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 322 | fail("Didn't throw"); |
| 323 | } catch (IllegalArgumentException expected) { |
| 324 | } |
| 325 | |
| 326 | // 5. Add the same admin1 again with replace, which should succeed. |
| 327 | dpm.setActiveAdmin(admin1, /* replace =*/ true); |
| 328 | |
| 329 | // TODO make sure it's replaced. |
| 330 | |
| 331 | // 6. Test getActiveAdmins() |
| 332 | List<ComponentName> admins = dpm.getActiveAdmins(); |
| 333 | assertEquals(2, admins.size()); |
| 334 | assertEquals(admin1, admins.get(0)); |
| 335 | assertEquals(admin2, admins.get(1)); |
| 336 | |
| 337 | // Another user has no admins. |
| 338 | mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 339 | |
| 340 | assertEquals(0, DpmTestUtils.getListSizeAllowingNull( |
| 341 | dpm.getActiveAdminsAsUser(DpmMockContext.CALLER_USER_HANDLE + 1))); |
| 342 | |
| 343 | mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL"); |
| 344 | } |
| 345 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 346 | public void testSetActiveAdmin_multiUsers() throws Exception { |
| 347 | |
| 348 | final int ANOTHER_USER_ID = 100; |
| 349 | final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 20456); |
| 350 | |
| 351 | mMockContext.addUser(ANOTHER_USER_ID, 0); // Add one more user. |
| 352 | |
| 353 | // Set up pacakge manager for the other user. |
| 354 | setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID); |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 355 | |
| 356 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 357 | |
| 358 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 359 | |
| 360 | mMockContext.binder.callingUid = ANOTHER_ADMIN_UID; |
| 361 | dpm.setActiveAdmin(admin2, /* replace =*/ false); |
| 362 | |
| 363 | |
| 364 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 365 | assertTrue(dpm.isAdminActive(admin1)); |
| 366 | assertFalse(dpm.isAdminActive(admin2)); |
| 367 | |
| 368 | mMockContext.binder.callingUid = ANOTHER_ADMIN_UID; |
| 369 | assertFalse(dpm.isAdminActive(admin1)); |
| 370 | assertTrue(dpm.isAdminActive(admin2)); |
| 371 | } |
| 372 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Test for: |
| 375 | * {@link DevicePolicyManager#setActiveAdmin} |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 376 | * with replace=false |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 377 | */ |
| 378 | public void testSetActiveAdmin_twiceWithoutReplace() throws Exception { |
| 379 | // 1. Make sure the caller has proper permissions. |
| 380 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 381 | |
| 382 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 383 | assertTrue(dpm.isAdminActive(admin1)); |
| 384 | |
| 385 | // Add the same admin1 again without replace, which should throw. |
| 386 | try { |
| 387 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 388 | fail("Didn't throw"); |
| 389 | } catch (IllegalArgumentException expected) { |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Test for: |
Makoto Onuki | 3ab6f2e | 2015-11-05 13:55:37 -0800 | [diff] [blame] | 395 | * {@link DevicePolicyManager#setActiveAdmin} when the admin isn't protected with |
| 396 | * BIND_DEVICE_ADMIN. |
| 397 | */ |
| 398 | public void testSetActiveAdmin_permissionCheck() throws Exception { |
| 399 | // 1. Make sure the caller has proper permissions. |
| 400 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 401 | |
| 402 | try { |
| 403 | dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false); |
| 404 | fail(); |
| 405 | } catch (IllegalArgumentException expected) { |
| 406 | assertTrue(expected.getMessage().contains(permission.BIND_DEVICE_ADMIN)); |
| 407 | } |
| 408 | assertFalse(dpm.isAdminActive(adminNoPerm)); |
| 409 | |
| 410 | // Change the target API level to MNC. Now it can be set as DA. |
| 411 | setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID, null, |
| 412 | VERSION_CODES.M); |
| 413 | dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false); |
| 414 | assertTrue(dpm.isAdminActive(adminNoPerm)); |
| 415 | |
| 416 | // TODO Test the "load from the file" case where DA will still be loaded even without |
| 417 | // BIND_DEVICE_ADMIN and target API is N. |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Test for: |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 422 | * {@link DevicePolicyManager#removeActiveAdmin} |
| 423 | */ |
| 424 | public void testRemoveActiveAdmin_SecurityException() { |
| 425 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 426 | |
| 427 | // Add admin. |
| 428 | |
| 429 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 430 | |
| 431 | assertTrue(dpm.isAdminActive(admin1)); |
| 432 | |
| 433 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 434 | |
| 435 | // Directly call the DPMS method with a different userid, which should fail. |
| 436 | try { |
| 437 | dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE + 1); |
| 438 | fail("Didn't throw SecurityException"); |
| 439 | } catch (SecurityException expected) { |
| 440 | } |
| 441 | |
| 442 | // Try to remove active admin with a different caller userid should fail too, without |
| 443 | // having MANAGE_DEVICE_ADMINS. |
| 444 | mContext.callerPermissions.clear(); |
| 445 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 446 | // Change the caller, and call into DPMS directly with a different user-id. |
| 447 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 448 | mContext.binder.callingUid = 1234567; |
| 449 | try { |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 450 | dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 451 | fail("Didn't throw SecurityException"); |
| 452 | } catch (SecurityException expected) { |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 457 | * {@link DevicePolicyManager#removeActiveAdmin} should fail with the user is not unlocked |
| 458 | * (because we can't send the remove broadcast). |
| 459 | */ |
| 460 | public void testRemoveActiveAdmin_userNotRunningOrLocked() { |
| 461 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 462 | |
| 463 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 464 | |
| 465 | // Add admin. |
| 466 | |
| 467 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 468 | |
| 469 | assertTrue(dpm.isAdminActive(admin1)); |
| 470 | |
| 471 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 472 | |
| 473 | // 1. User not unlocked. |
| 474 | when(mContext.userManager.isUserUnlocked(eq(DpmMockContext.CALLER_USER_HANDLE))) |
| 475 | .thenReturn(false); |
| 476 | try { |
| 477 | dpm.removeActiveAdmin(admin1); |
| 478 | fail("Didn't throw IllegalStateException"); |
| 479 | } catch (IllegalStateException expected) { |
| 480 | MoreAsserts.assertContainsRegex( |
| 481 | "User must be running and unlocked", expected.getMessage()); |
| 482 | } |
| 483 | |
| 484 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 485 | |
| 486 | // 2. User unlocked. |
| 487 | when(mContext.userManager.isUserUnlocked(eq(DpmMockContext.CALLER_USER_HANDLE))) |
| 488 | .thenReturn(true); |
| 489 | |
| 490 | dpm.removeActiveAdmin(admin1); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 491 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 495 | * Test for: |
| 496 | * {@link DevicePolicyManager#removeActiveAdmin} |
| 497 | */ |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 498 | public void testRemoveActiveAdmin_fromDifferentUserWithINTERACT_ACROSS_USERS_FULL() { |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 499 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 500 | |
| 501 | // Add admin1. |
| 502 | |
| 503 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 504 | |
| 505 | assertTrue(dpm.isAdminActive(admin1)); |
| 506 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 507 | |
| 508 | // Different user, but should work, because caller has proper permissions. |
| 509 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 510 | |
| 511 | // Change the caller, and call into DPMS directly with a different user-id. |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 512 | mContext.binder.callingUid = 1234567; |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 513 | |
| 514 | dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 515 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 516 | |
| 517 | // TODO DO Still can't be removed in this case. |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Test for: |
| 522 | * {@link DevicePolicyManager#removeActiveAdmin} |
| 523 | */ |
| 524 | public void testRemoveActiveAdmin_sameUserNoMANAGE_DEVICE_ADMINS() { |
| 525 | // Need MANAGE_DEVICE_ADMINS for setActiveAdmin. We'll remove it later. |
| 526 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 527 | |
| 528 | // Add admin1. |
| 529 | |
| 530 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 531 | |
| 532 | assertTrue(dpm.isAdminActive(admin1)); |
| 533 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 534 | |
| 535 | // Broadcast from saveSettingsLocked(). |
| 536 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 537 | MockUtils.checkIntentAction( |
| 538 | DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), |
| 539 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 540 | |
| 541 | // Remove. No permissions, but same user, so it'll work. |
| 542 | mContext.callerPermissions.clear(); |
| 543 | dpm.removeActiveAdmin(admin1); |
| 544 | |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 545 | verify(mContext.spiedContext).sendOrderedBroadcastAsUser( |
| 546 | MockUtils.checkIntentAction( |
| 547 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED), |
| 548 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE), |
| 549 | isNull(String.class), |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 550 | any(BroadcastReceiver.class), |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 551 | eq(dpms.mHandler), |
| 552 | eq(Activity.RESULT_OK), |
| 553 | isNull(String.class), |
| 554 | isNull(Bundle.class)); |
| 555 | |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 556 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | f76b06a | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 557 | |
| 558 | // Again broadcast from saveSettingsLocked(). |
| 559 | verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( |
| 560 | MockUtils.checkIntentAction( |
| 561 | DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), |
| 562 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 563 | |
| 564 | // TODO Check other internal calls. |
Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame] | 565 | } |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 566 | |
| 567 | /** |
Robin Lee | d2a73ed | 2016-12-19 09:07:16 +0000 | [diff] [blame] | 568 | * Test for: @{link DevicePolicyManager#setActivePasswordState} |
| 569 | * |
| 570 | * Validates that when the password for a user changes, the notification broadcast intent |
| 571 | * {@link DeviceAdminReceiver#ACTION_PASSWORD_CHANGED} is sent to managed profile owners, in |
| 572 | * addition to ones in the original user. |
| 573 | */ |
| 574 | public void testSetActivePasswordState_sendToProfiles() throws Exception { |
| 575 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 576 | |
| 577 | final int MANAGED_PROFILE_USER_ID = 78; |
| 578 | final int MANAGED_PROFILE_ADMIN_UID = |
| 579 | UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); |
| 580 | |
| 581 | // Setup device owner. |
| 582 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 583 | mContext.packageName = admin1.getPackageName(); |
| 584 | setupDeviceOwner(); |
| 585 | |
| 586 | // Add a managed profile belonging to the system user. |
| 587 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 588 | |
| 589 | // Change the parent user's password. |
| 590 | dpm.reportPasswordChanged(UserHandle.USER_SYSTEM); |
| 591 | |
| 592 | // Both the device owner and the managed profile owner should receive this broadcast. |
| 593 | final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); |
| 594 | intent.setComponent(admin1); |
| 595 | intent.putExtra(Intent.EXTRA_USER, UserHandle.of(UserHandle.USER_SYSTEM)); |
| 596 | |
| 597 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 598 | MockUtils.checkIntent(intent), |
| 599 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 600 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 601 | MockUtils.checkIntent(intent), |
| 602 | MockUtils.checkUserHandle(MANAGED_PROFILE_USER_ID)); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Test for: @{link DevicePolicyManager#setActivePasswordState} |
| 607 | * |
| 608 | * Validates that when the password for a managed profile changes, the notification broadcast |
| 609 | * intent {@link DeviceAdminReceiver#ACTION_PASSWORD_CHANGED} is only sent to the profile, not |
| 610 | * its parent. |
| 611 | */ |
| 612 | public void testSetActivePasswordState_notSentToParent() throws Exception { |
| 613 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 614 | |
| 615 | final int MANAGED_PROFILE_USER_ID = 78; |
| 616 | final int MANAGED_PROFILE_ADMIN_UID = |
| 617 | UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); |
| 618 | |
| 619 | // Setup device owner. |
| 620 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 621 | mContext.packageName = admin1.getPackageName(); |
| 622 | doReturn(true).when(mContext.lockPatternUtils) |
| 623 | .isSeparateProfileChallengeEnabled(MANAGED_PROFILE_USER_ID); |
| 624 | setupDeviceOwner(); |
| 625 | |
| 626 | // Add a managed profile belonging to the system user. |
| 627 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 628 | |
| 629 | // Change the profile's password. |
| 630 | dpm.reportPasswordChanged(MANAGED_PROFILE_USER_ID); |
| 631 | |
| 632 | // Both the device owner and the managed profile owner should receive this broadcast. |
| 633 | final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); |
| 634 | intent.setComponent(admin1); |
| 635 | intent.putExtra(Intent.EXTRA_USER, UserHandle.of(MANAGED_PROFILE_USER_ID)); |
| 636 | |
| 637 | verify(mContext.spiedContext, never()).sendBroadcastAsUser( |
| 638 | MockUtils.checkIntent(intent), |
| 639 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 640 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 641 | MockUtils.checkIntent(intent), |
| 642 | MockUtils.checkUserHandle(MANAGED_PROFILE_USER_ID)); |
| 643 | } |
| 644 | /** |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 645 | * Test for: {@link DevicePolicyManager#setDeviceOwner} DO on system user installs successfully. |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 646 | */ |
| 647 | public void testSetDeviceOwner() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 648 | setDeviceOwner(); |
| 649 | |
| 650 | // Try to set a profile owner on the same user, which should fail. |
| 651 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 652 | dpm.setActiveAdmin(admin2, /* refreshing= */ true, UserHandle.USER_SYSTEM); |
| 653 | try { |
| 654 | dpm.setProfileOwner(admin2, "owner-name", UserHandle.USER_SYSTEM); |
| 655 | fail("IllegalStateException not thrown"); |
| 656 | } catch (IllegalStateException expected) { |
| 657 | assertTrue("Message was: " + expected.getMessage(), |
| 658 | expected.getMessage().contains("already has a device owner")); |
| 659 | } |
| 660 | |
| 661 | // DO admin can't be deactivated. |
| 662 | dpm.removeActiveAdmin(admin1); |
| 663 | assertTrue(dpm.isAdminActive(admin1)); |
| 664 | |
| 665 | // TODO Test getDeviceOwnerName() too. To do so, we need to change |
| 666 | // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable. |
| 667 | } |
| 668 | |
| 669 | private void setDeviceOwner() throws Exception { |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 670 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 671 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 672 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 673 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 674 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 675 | // In this test, change the caller user to "system". |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 676 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 677 | |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 678 | // Make sure admin1 is installed on system user. |
| 679 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
Makoto Onuki | d932f76 | 2015-09-29 16:53:38 -0700 | [diff] [blame] | 680 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 681 | // Check various get APIs. |
| 682 | checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ false); |
| 683 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 684 | // DO needs to be an DA. |
| 685 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 686 | |
| 687 | // Fire! |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 688 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 689 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 690 | // getDeviceOwnerComponent should return the admin1 component. |
| 691 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 692 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 693 | |
| 694 | // Check various get APIs. |
| 695 | checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ true); |
| 696 | |
| 697 | // getDeviceOwnerComponent should *NOT* return the admin1 component for other users. |
| 698 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 699 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 700 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 701 | |
| 702 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 703 | |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 704 | // Verify internal calls. |
| 705 | verify(mContext.iactivityManager, times(1)).updateDeviceOwner( |
| 706 | eq(admin1.getPackageName())); |
| 707 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 708 | // TODO We should check if the caller has called clearCallerIdentity(). |
| 709 | verify(mContext.ibackupManager, times(1)).setBackupServiceActive( |
| 710 | eq(UserHandle.USER_SYSTEM), eq(false)); |
| 711 | |
| 712 | verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( |
| 713 | MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), |
| 714 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
| 715 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 716 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 719 | private void checkGetDeviceOwnerInfoApi(DevicePolicyManager dpm, boolean hasDeviceOwner) { |
| 720 | final int origCallingUser = mContext.binder.callingUid; |
| 721 | final List origPermissions = new ArrayList(mContext.callerPermissions); |
| 722 | mContext.callerPermissions.clear(); |
| 723 | |
| 724 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 725 | |
| 726 | mContext.binder.callingUid = Process.SYSTEM_UID; |
| 727 | |
| 728 | // TODO Test getDeviceOwnerName() too. To do so, we need to change |
| 729 | // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable. |
| 730 | if (hasDeviceOwner) { |
| 731 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 732 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 733 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 734 | |
| 735 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 736 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 737 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 738 | } else { |
| 739 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 740 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 741 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 742 | |
| 743 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 744 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 745 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 746 | } |
| 747 | |
| 748 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 749 | if (hasDeviceOwner) { |
| 750 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 751 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 752 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 753 | |
| 754 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 755 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 756 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 757 | } else { |
| 758 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 759 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 760 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 761 | |
| 762 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 763 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 764 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 765 | } |
| 766 | |
| 767 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 768 | // Still with MANAGE_USERS. |
| 769 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 770 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 771 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 772 | |
| 773 | if (hasDeviceOwner) { |
| 774 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 775 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 776 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 777 | } else { |
| 778 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 779 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 780 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 781 | } |
| 782 | |
| 783 | mContext.binder.callingUid = Process.SYSTEM_UID; |
| 784 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 785 | // System can still call "OnAnyUser" without MANAGE_USERS. |
| 786 | if (hasDeviceOwner) { |
| 787 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 788 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 789 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 790 | |
| 791 | assertTrue(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 792 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 793 | assertEquals(UserHandle.USER_SYSTEM, dpm.getDeviceOwnerUserId()); |
| 794 | } else { |
| 795 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 796 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 797 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 798 | |
| 799 | assertFalse(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); |
| 800 | assertEquals(null, dpm.getDeviceOwnerComponentOnAnyUser()); |
| 801 | assertEquals(UserHandle.USER_NULL, dpm.getDeviceOwnerUserId()); |
| 802 | } |
| 803 | |
| 804 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 805 | // Still no MANAGE_USERS. |
| 806 | if (hasDeviceOwner) { |
| 807 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 808 | assertTrue(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 809 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 810 | } else { |
| 811 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 812 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 813 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 814 | } |
| 815 | |
| 816 | try { |
| 817 | dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()); |
| 818 | fail(); |
| 819 | } catch (SecurityException expected) { |
| 820 | } |
| 821 | try { |
| 822 | dpm.getDeviceOwnerComponentOnAnyUser(); |
| 823 | fail(); |
| 824 | } catch (SecurityException expected) { |
| 825 | } |
| 826 | try { |
| 827 | dpm.getDeviceOwnerUserId(); |
| 828 | fail(); |
| 829 | } catch (SecurityException expected) { |
| 830 | } |
| 831 | try { |
| 832 | dpm.getDeviceOwnerNameOnAnyUser(); |
| 833 | fail(); |
| 834 | } catch (SecurityException expected) { |
| 835 | } |
| 836 | |
| 837 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 838 | // Still no MANAGE_USERS. |
| 839 | assertFalse(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 840 | assertFalse(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())); |
| 841 | assertEquals(null, dpm.getDeviceOwnerComponentOnCallingUser()); |
| 842 | |
| 843 | try { |
| 844 | dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName()); |
| 845 | fail(); |
| 846 | } catch (SecurityException expected) { |
| 847 | } |
| 848 | try { |
| 849 | dpm.getDeviceOwnerComponentOnAnyUser(); |
| 850 | fail(); |
| 851 | } catch (SecurityException expected) { |
| 852 | } |
| 853 | try { |
| 854 | dpm.getDeviceOwnerUserId(); |
| 855 | fail(); |
| 856 | } catch (SecurityException expected) { |
| 857 | } |
| 858 | try { |
| 859 | dpm.getDeviceOwnerNameOnAnyUser(); |
| 860 | fail(); |
| 861 | } catch (SecurityException expected) { |
| 862 | } |
| 863 | |
| 864 | // Restore. |
| 865 | mContext.binder.callingUid = origCallingUser; |
| 866 | mContext.callerPermissions.addAll(origPermissions); |
| 867 | } |
| 868 | |
| 869 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 870 | /** |
| 871 | * Test for: {@link DevicePolicyManager#setDeviceOwner} Package doesn't exist. |
| 872 | */ |
| 873 | public void testSetDeviceOwner_noSuchPackage() { |
| 874 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 875 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 876 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 877 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 878 | |
| 879 | // Call from a process on the system user. |
| 880 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 881 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 882 | try { |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 883 | dpm.setDeviceOwner(new ComponentName("a.b.c", ".def")); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 884 | fail("Didn't throw IllegalArgumentException"); |
| 885 | } catch (IllegalArgumentException expected) { |
Makoto Onuki | 803d675 | 2015-10-30 12:58:39 -0700 | [diff] [blame] | 886 | assertTrue("Message was: " + expected.getMessage(), |
| 887 | expected.getMessage().contains("Invalid component")); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | public void testSetDeviceOwner_failures() throws Exception { |
| 892 | // TODO Test more failure cases. Basically test all chacks in enforceCanSetDeviceOwner(). |
| 893 | } |
| 894 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 895 | public void testClearDeviceOwner() throws Exception { |
| 896 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 897 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 898 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 899 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 900 | |
| 901 | // Set admin1 as a DA to the secondary user. |
| 902 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 903 | |
| 904 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 905 | |
| 906 | // Set admin 1 as the DO to the system user. |
| 907 | |
| 908 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 909 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 910 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 911 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
| 912 | |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 913 | // Verify internal calls. |
| 914 | verify(mContext.iactivityManager, times(1)).updateDeviceOwner( |
| 915 | eq(admin1.getPackageName())); |
| 916 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 917 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 918 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 919 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
| 920 | |
| 921 | assertTrue(dpm.isAdminActive(admin1)); |
| 922 | assertFalse(dpm.isRemovingAdmin(admin1, UserHandle.USER_SYSTEM)); |
| 923 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 924 | // Set up other mocks. |
| 925 | when(mContext.userManager.getUserRestrictions()).thenReturn(new Bundle()); |
| 926 | |
| 927 | // Now call clear. |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 928 | doReturn(DpmMockContext.CALLER_SYSTEM_USER_UID).when(mContext.packageManager).getPackageUidAsUser( |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 929 | eq(admin1.getPackageName()), |
| 930 | anyInt()); |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 931 | |
| 932 | // But first pretend the user is locked. Then it should fail. |
| 933 | when(mContext.userManager.isUserUnlocked(anyInt())).thenReturn(false); |
| 934 | try { |
| 935 | dpm.clearDeviceOwnerApp(admin1.getPackageName()); |
| 936 | fail("Didn't throw IllegalStateException"); |
| 937 | } catch (IllegalStateException expected) { |
| 938 | MoreAsserts.assertContainsRegex( |
| 939 | "User must be running and unlocked", expected.getMessage()); |
| 940 | } |
| 941 | |
| 942 | when(mContext.userManager.isUserUnlocked(anyInt())).thenReturn(true); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 943 | reset(mContext.userManagerInternal); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 944 | dpm.clearDeviceOwnerApp(admin1.getPackageName()); |
| 945 | |
| 946 | // Now DO shouldn't be set. |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 947 | assertNull(dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 948 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 949 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 950 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 951 | eq(null), |
| 952 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 953 | |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 954 | assertFalse(dpm.isAdminActiveAsUser(admin1, UserHandle.USER_SYSTEM)); |
Tony Mak | 1970f97 | 2016-08-30 17:41:48 +0100 | [diff] [blame] | 955 | |
| 956 | // ACTION_DEVICE_OWNER_CHANGED should be sent twice, once for setting the device owner |
| 957 | // and once for clearing it. |
| 958 | verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( |
| 959 | MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), |
| 960 | MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 961 | // TODO Check other calls. |
| 962 | } |
| 963 | |
| 964 | public void testClearDeviceOwner_fromDifferentUser() throws Exception { |
| 965 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 966 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 967 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 968 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 969 | |
| 970 | // Set admin1 as a DA to the secondary user. |
| 971 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 972 | |
| 973 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 974 | |
| 975 | // Set admin 1 as the DO to the system user. |
| 976 | |
| 977 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 978 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 979 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 980 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
| 981 | |
Makoto Onuki | 219bbaf | 2015-11-12 01:38:47 +0000 | [diff] [blame] | 982 | // Verify internal calls. |
| 983 | verify(mContext.iactivityManager, times(1)).updateDeviceOwner( |
| 984 | eq(admin1.getPackageName())); |
| 985 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 986 | assertEquals(admin1, dpm.getDeviceOwnerComponentOnAnyUser()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 987 | |
| 988 | // Now call clear from the secondary user, which should throw. |
| 989 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 990 | |
| 991 | // Now call clear. |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 992 | doReturn(DpmMockContext.CALLER_UID).when(mContext.packageManager).getPackageUidAsUser( |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 993 | eq(admin1.getPackageName()), |
| 994 | anyInt()); |
| 995 | try { |
| 996 | dpm.clearDeviceOwnerApp(admin1.getPackageName()); |
| 997 | fail("Didn't throw"); |
| 998 | } catch (SecurityException e) { |
| 999 | assertEquals("clearDeviceOwner can only be called by the device owner", e.getMessage()); |
| 1000 | } |
| 1001 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1002 | // DO shouldn't be removed. |
| 1003 | assertTrue(dpm.isDeviceManaged()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1006 | public void testSetProfileOwner() throws Exception { |
| 1007 | setAsProfileOwner(admin1); |
Makoto Onuki | 803d675 | 2015-10-30 12:58:39 -0700 | [diff] [blame] | 1008 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1009 | // PO admin can't be deactivated. |
| 1010 | dpm.removeActiveAdmin(admin1); |
| 1011 | assertTrue(dpm.isAdminActive(admin1)); |
| 1012 | |
Makoto Onuki | 803d675 | 2015-10-30 12:58:39 -0700 | [diff] [blame] | 1013 | // Try setting DO on the same user, which should fail. |
| 1014 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); |
| 1015 | dpm.setActiveAdmin(admin2, /* refreshing= */ true, DpmMockContext.CALLER_USER_HANDLE); |
| 1016 | try { |
| 1017 | dpm.setDeviceOwner(admin2, "owner-name", DpmMockContext.CALLER_USER_HANDLE); |
| 1018 | fail("IllegalStateException not thrown"); |
| 1019 | } catch (IllegalStateException expected) { |
| 1020 | assertTrue("Message was: " + expected.getMessage(), |
| 1021 | expected.getMessage().contains("already has a profile owner")); |
| 1022 | } |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1025 | public void testClearProfileOwner() throws Exception { |
| 1026 | setAsProfileOwner(admin1); |
| 1027 | |
| 1028 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1029 | |
| 1030 | assertTrue(dpm.isProfileOwnerApp(admin1.getPackageName())); |
| 1031 | assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
| 1032 | |
Makoto Onuki | 1a5ee77 | 2016-02-12 15:34:57 -0800 | [diff] [blame] | 1033 | // First try when the user is locked, which should fail. |
| 1034 | when(mContext.userManager.isUserUnlocked(anyInt())) |
| 1035 | .thenReturn(false); |
| 1036 | try { |
| 1037 | dpm.clearProfileOwner(admin1); |
| 1038 | fail("Didn't throw IllegalStateException"); |
| 1039 | } catch (IllegalStateException expected) { |
| 1040 | MoreAsserts.assertContainsRegex( |
| 1041 | "User must be running and unlocked", expected.getMessage()); |
| 1042 | } |
| 1043 | // Clear, really. |
| 1044 | when(mContext.userManager.isUserUnlocked(anyInt())) |
| 1045 | .thenReturn(true); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1046 | dpm.clearProfileOwner(admin1); |
| 1047 | |
| 1048 | // Check |
| 1049 | assertFalse(dpm.isProfileOwnerApp(admin1.getPackageName())); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 1050 | assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE)); |
Makoto Onuki | 90b8965 | 2016-01-28 14:44:18 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1053 | public void testSetProfileOwner_failures() throws Exception { |
| 1054 | // TODO Test more failure cases. Basically test all chacks in enforceCanSetProfileOwner(). |
| 1055 | } |
| 1056 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1057 | public void testGetDeviceOwnerAdminLocked() throws Exception { |
| 1058 | checkDeviceOwnerWithMultipleDeviceAdmins(); |
| 1059 | } |
| 1060 | |
| 1061 | private void checkDeviceOwnerWithMultipleDeviceAdmins() throws Exception { |
| 1062 | // In ths test, we use 3 users (system + 2 secondary users), set some device admins to them, |
| 1063 | // set admin2 on CALLER_USER_HANDLE as DO, then call getDeviceOwnerAdminLocked() to |
| 1064 | // make sure it gets the right component from the right user. |
| 1065 | |
| 1066 | final int ANOTHER_USER_ID = 100; |
| 1067 | final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 456); |
| 1068 | |
| 1069 | mMockContext.addUser(ANOTHER_USER_ID, 0); // Add one more user. |
| 1070 | |
| 1071 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1072 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1073 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1074 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1075 | |
| 1076 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1077 | |
Victor Chang | e29cd47 | 2016-03-02 20:57:42 +0000 | [diff] [blame] | 1078 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 1079 | |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1080 | // Make sure the admin packge is installed to each user. |
| 1081 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1082 | setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1083 | |
| 1084 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 1085 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); |
| 1086 | |
| 1087 | setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID); |
| 1088 | |
| 1089 | |
| 1090 | // Set active admins to the users. |
| 1091 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 1092 | dpm.setActiveAdmin(admin3, /* replace =*/ false); |
| 1093 | |
| 1094 | dpm.setActiveAdmin(admin1, /* replace =*/ false, DpmMockContext.CALLER_USER_HANDLE); |
| 1095 | dpm.setActiveAdmin(admin2, /* replace =*/ false, DpmMockContext.CALLER_USER_HANDLE); |
| 1096 | |
| 1097 | dpm.setActiveAdmin(admin2, /* replace =*/ false, ANOTHER_USER_ID); |
| 1098 | |
| 1099 | // Set DO on the first non-system user. |
| 1100 | mContext.setUserRunning(DpmMockContext.CALLER_USER_HANDLE, true); |
| 1101 | assertTrue(dpm.setDeviceOwner(admin2, "owner-name", DpmMockContext.CALLER_USER_HANDLE)); |
| 1102 | |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1103 | assertEquals(admin2, dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false)); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1104 | |
| 1105 | // Then check getDeviceOwnerAdminLocked(). |
| 1106 | assertEquals(admin2, dpms.getDeviceOwnerAdminLocked().info.getComponent()); |
| 1107 | assertEquals(DpmMockContext.CALLER_UID, dpms.getDeviceOwnerAdminLocked().getUid()); |
| 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * This essentially tests |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 1112 | * {@code DevicePolicyManagerService.findOwnerComponentIfNecessaryLocked()}. (which is |
| 1113 | * private.) |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1114 | * |
| 1115 | * We didn't use to persist the DO component class name, but now we do, and the above method |
| 1116 | * finds the right component from a package name upon migration. |
| 1117 | */ |
| 1118 | public void testDeviceOwnerMigration() throws Exception { |
Victor Chang | e29cd47 | 2016-03-02 20:57:42 +0000 | [diff] [blame] | 1119 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1120 | checkDeviceOwnerWithMultipleDeviceAdmins(); |
| 1121 | |
| 1122 | // Overwrite the device owner setting and clears the clas name. |
| 1123 | dpms.mOwners.setDeviceOwner( |
| 1124 | new ComponentName(admin2.getPackageName(), ""), |
| 1125 | "owner-name", DpmMockContext.CALLER_USER_HANDLE); |
| 1126 | dpms.mOwners.writeDeviceOwner(); |
| 1127 | |
| 1128 | // Make sure the DO component name doesn't have a class name. |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1129 | assertEquals("", dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false).getClassName()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1130 | |
| 1131 | // Then create a new DPMS to have it load the settings from files. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1132 | when(mContext.userManager.getUserRestrictions(any(UserHandle.class))) |
| 1133 | .thenReturn(new Bundle()); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1134 | initializeDpms(); |
| 1135 | |
| 1136 | // Now the DO component name is a full name. |
| 1137 | // *BUT* because both admin1 and admin2 belong to the same package, we think admin1 is the |
| 1138 | // DO. |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1139 | assertEquals(admin1, dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false)); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1142 | public void testSetGetApplicationRestriction() { |
| 1143 | setAsProfileOwner(admin1); |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1144 | mContext.packageName = admin1.getPackageName(); |
Makoto Onuki | b643fb0 | 2015-09-22 15:03:44 -0700 | [diff] [blame] | 1145 | |
| 1146 | { |
| 1147 | Bundle rest = new Bundle(); |
| 1148 | rest.putString("KEY_STRING", "Foo1"); |
| 1149 | dpm.setApplicationRestrictions(admin1, "pkg1", rest); |
| 1150 | } |
| 1151 | |
| 1152 | { |
| 1153 | Bundle rest = new Bundle(); |
| 1154 | rest.putString("KEY_STRING", "Foo2"); |
| 1155 | dpm.setApplicationRestrictions(admin1, "pkg2", rest); |
| 1156 | } |
| 1157 | |
| 1158 | { |
| 1159 | Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg1"); |
| 1160 | assertNotNull(returned); |
| 1161 | assertEquals(returned.size(), 1); |
| 1162 | assertEquals(returned.get("KEY_STRING"), "Foo1"); |
| 1163 | } |
| 1164 | |
| 1165 | { |
| 1166 | Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg2"); |
| 1167 | assertNotNull(returned); |
| 1168 | assertEquals(returned.size(), 1); |
| 1169 | assertEquals(returned.get("KEY_STRING"), "Foo2"); |
| 1170 | } |
| 1171 | |
| 1172 | dpm.setApplicationRestrictions(admin1, "pkg2", new Bundle()); |
| 1173 | assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg2").size()); |
| 1174 | } |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1175 | |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1176 | /** |
| 1177 | * Setup a package in the package manager mock. Useful for faking installed applications. |
| 1178 | * |
| 1179 | * @param packageName the name of the package to be setup |
| 1180 | * @param appId the application ID to be given to the package |
| 1181 | * @return the UID of the package as known by the mock package manager |
| 1182 | */ |
| 1183 | private int setupPackageInPackageManager(final String packageName, final int appId) |
| 1184 | throws Exception { |
| 1185 | // Make the PackageManager return the package instead of throwing a NameNotFoundException |
| 1186 | final PackageInfo pi = new PackageInfo(); |
| 1187 | pi.applicationInfo = new ApplicationInfo(); |
| 1188 | pi.applicationInfo.flags = ApplicationInfo.FLAG_HAS_CODE; |
| 1189 | doReturn(pi).when(mContext.ipackageManager).getPackageInfo( |
| 1190 | eq(packageName), |
| 1191 | anyInt(), |
| 1192 | eq(DpmMockContext.CALLER_USER_HANDLE)); |
| 1193 | // Setup application UID with the PackageManager |
| 1194 | final int uid = UserHandle.getUid(DpmMockContext.CALLER_USER_HANDLE, appId); |
| 1195 | doReturn(uid).when(mContext.packageManager).getPackageUidAsUser( |
| 1196 | eq(packageName), |
| 1197 | eq(DpmMockContext.CALLER_USER_HANDLE)); |
| 1198 | // Associate packageName to uid |
| 1199 | doReturn(packageName).when(mContext.ipackageManager).getNameForUid(eq(uid)); |
| 1200 | doReturn(new String[]{packageName}) |
| 1201 | .when(mContext.ipackageManager).getPackagesForUid(eq(uid)); |
| 1202 | return uid; |
| 1203 | } |
| 1204 | |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 1205 | public void testCertificateDisclosure() throws Exception { |
| 1206 | final int userId = DpmMockContext.CALLER_USER_HANDLE; |
| 1207 | final UserHandle user = UserHandle.of(userId); |
| 1208 | |
| 1209 | mContext.applicationInfo = new ApplicationInfo(); |
| 1210 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 1211 | mContext.packageName = "com.android.frameworks.servicestests"; |
| 1212 | mContext.userContexts.put(user, mContext); |
| 1213 | when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); |
| 1214 | |
| 1215 | ParceledListSlice<ParcelableString> oneCert = asSlice(new String[] {"1"}); |
| 1216 | ParceledListSlice<ParcelableString> fourCerts = asSlice(new String[] {"1", "2", "3", "4"}); |
| 1217 | |
| 1218 | final String TEST_STRING = "Test for exactly 2 certs out of 4"; |
| 1219 | doReturn(TEST_STRING).when(mContext.resources).getQuantityText(anyInt(), eq(2)); |
| 1220 | |
| 1221 | // Given that we have exactly one certificate installed, |
| 1222 | when(mContext.keyChainConnection.getService().getUserCaAliases()).thenReturn(oneCert); |
| 1223 | // when that certificate is approved, |
| 1224 | dpms.approveCaCert(oneCert.getList().get(0).string, userId, true); |
| 1225 | // a notification should not be shown. |
| 1226 | verify(mContext.notificationManager, timeout(1000)) |
| 1227 | .cancelAsUser(anyString(), anyInt(), eq(user)); |
| 1228 | |
| 1229 | // Given that we have four certificates installed, |
| 1230 | when(mContext.keyChainConnection.getService().getUserCaAliases()).thenReturn(fourCerts); |
| 1231 | // when two of them are approved (one of them approved twice hence no action), |
| 1232 | dpms.approveCaCert(fourCerts.getList().get(0).string, userId, true); |
| 1233 | dpms.approveCaCert(fourCerts.getList().get(1).string, userId, true); |
| 1234 | // a notification should be shown saying that there are two certificates left to approve. |
| 1235 | verify(mContext.notificationManager, timeout(1000)) |
| 1236 | .notifyAsUser(anyString(), anyInt(), argThat( |
| 1237 | new BaseMatcher<Notification>() { |
| 1238 | @Override |
| 1239 | public boolean matches(Object item) { |
| 1240 | final Notification noti = (Notification) item; |
| 1241 | return TEST_STRING.equals( |
| 1242 | noti.extras.getString(Notification.EXTRA_TITLE)); |
| 1243 | } |
| 1244 | @Override |
| 1245 | public void describeTo(Description description) { |
| 1246 | description.appendText( |
| 1247 | "Notification{title=\"" + TEST_STRING + "\"}"); |
| 1248 | } |
| 1249 | }), eq(user)); |
| 1250 | } |
| 1251 | |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1252 | /** |
| 1253 | * Simple test for delegate set/get and general delegation. Tests verifying that delegated |
| 1254 | * privileges can acually be exercised by a delegate are not covered here. |
| 1255 | */ |
| 1256 | public void testDelegation() throws Exception { |
| 1257 | setAsProfileOwner(admin1); |
| 1258 | |
| 1259 | final int userHandle = DpmMockContext.CALLER_USER_HANDLE; |
| 1260 | |
| 1261 | // Given two packages |
| 1262 | final String CERT_DELEGATE = "com.delegate.certs"; |
| 1263 | final String RESTRICTIONS_DELEGATE = "com.delegate.apprestrictions"; |
| 1264 | final int CERT_DELEGATE_UID = setupPackageInPackageManager(CERT_DELEGATE, 20988); |
| 1265 | final int RESTRICTIONS_DELEGATE_UID = setupPackageInPackageManager(RESTRICTIONS_DELEGATE, |
| 1266 | 20989); |
| 1267 | |
| 1268 | // On delegation |
| 1269 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1270 | mContext.packageName = admin1.getPackageName(); |
| 1271 | dpm.setCertInstallerPackage(admin1, CERT_DELEGATE); |
| 1272 | dpm.setApplicationRestrictionsManagingPackage(admin1, RESTRICTIONS_DELEGATE); |
| 1273 | |
| 1274 | // DPMS correctly stores and retrieves the delegates |
| 1275 | DevicePolicyManagerService.DevicePolicyData policy = dpms.mUserData.get(userHandle); |
| 1276 | assertEquals(2, policy.mDelegationMap.size()); |
| 1277 | MoreAsserts.assertContentsInAnyOrder(policy.mDelegationMap.get(CERT_DELEGATE), |
| 1278 | DELEGATION_CERT_INSTALL); |
| 1279 | MoreAsserts.assertContentsInAnyOrder(dpm.getDelegatedScopes(admin1, CERT_DELEGATE), |
| 1280 | DELEGATION_CERT_INSTALL); |
| 1281 | assertEquals(CERT_DELEGATE, dpm.getCertInstallerPackage(admin1)); |
| 1282 | MoreAsserts.assertContentsInAnyOrder(policy.mDelegationMap.get(RESTRICTIONS_DELEGATE), |
| 1283 | DELEGATION_APP_RESTRICTIONS); |
| 1284 | MoreAsserts.assertContentsInAnyOrder(dpm.getDelegatedScopes(admin1, RESTRICTIONS_DELEGATE), |
| 1285 | DELEGATION_APP_RESTRICTIONS); |
| 1286 | assertEquals(RESTRICTIONS_DELEGATE, dpm.getApplicationRestrictionsManagingPackage(admin1)); |
| 1287 | |
| 1288 | // On calling install certificate APIs from an unauthorized process |
| 1289 | mContext.binder.callingUid = RESTRICTIONS_DELEGATE_UID; |
| 1290 | mContext.packageName = RESTRICTIONS_DELEGATE; |
| 1291 | |
| 1292 | // DPMS throws a SecurityException |
| 1293 | try { |
| 1294 | dpm.installCaCert(null, null); |
| 1295 | fail("Didn't throw SecurityException on unauthorized access"); |
| 1296 | } catch (SecurityException expected) { |
| 1297 | } |
| 1298 | |
| 1299 | // On calling install certificate APIs from an authorized process |
| 1300 | mContext.binder.callingUid = CERT_DELEGATE_UID; |
| 1301 | mContext.packageName = CERT_DELEGATE; |
| 1302 | |
| 1303 | // DPMS executes without a SecurityException |
| 1304 | try { |
| 1305 | dpm.installCaCert(null, null); |
| 1306 | } catch (SecurityException unexpected) { |
| 1307 | fail("Threw SecurityException on authorized access"); |
| 1308 | } catch (NullPointerException expected) { |
| 1309 | } |
| 1310 | |
| 1311 | // On removing a delegate |
| 1312 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1313 | mContext.packageName = admin1.getPackageName(); |
| 1314 | dpm.setCertInstallerPackage(admin1, null); |
| 1315 | |
| 1316 | // DPMS does not allow access to ex-delegate |
| 1317 | mContext.binder.callingUid = CERT_DELEGATE_UID; |
| 1318 | mContext.packageName = CERT_DELEGATE; |
| 1319 | try { |
| 1320 | dpm.installCaCert(null, null); |
| 1321 | fail("Didn't throw SecurityException on unauthorized access"); |
| 1322 | } catch (SecurityException expected) { |
| 1323 | } |
| 1324 | |
| 1325 | // But still allows access to other existing delegates |
| 1326 | mContext.binder.callingUid = RESTRICTIONS_DELEGATE_UID; |
| 1327 | mContext.packageName = RESTRICTIONS_DELEGATE; |
| 1328 | try { |
| 1329 | dpm.getApplicationRestrictions(null, "pkg"); |
| 1330 | } catch (SecurityException expected) { |
| 1331 | fail("Threw SecurityException on authorized access"); |
| 1332 | } |
| 1333 | } |
| 1334 | |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1335 | public void testApplicationRestrictionsManagingApp() throws Exception { |
| 1336 | setAsProfileOwner(admin1); |
| 1337 | |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1338 | final String nonExistAppRestrictionsManagerPackage = "com.google.app.restrictions.manager2"; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1339 | final String appRestrictionsManagerPackage = "com.google.app.restrictions.manager"; |
| 1340 | final int appRestrictionsManagerAppId = 20987; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1341 | final int appRestrictionsManagerUid = setupPackageInPackageManager( |
| 1342 | appRestrictionsManagerPackage, appRestrictionsManagerAppId); |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1343 | |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1344 | // appRestrictionsManager package shouldn't be able to manage restrictions as the PO hasn't |
| 1345 | // delegated that permission yet. |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1346 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1347 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1348 | assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage()); |
| 1349 | Bundle rest = new Bundle(); |
| 1350 | rest.putString("KEY_STRING", "Foo1"); |
| 1351 | try { |
| 1352 | dpm.setApplicationRestrictions(null, "pkg1", rest); |
| 1353 | fail("Didn't throw expected SecurityException"); |
| 1354 | } catch (SecurityException expected) { |
| 1355 | MoreAsserts.assertContainsRegex( |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1356 | "Caller with uid \\d+ is not a delegate of scope delegation-app-restrictions.", |
| 1357 | expected.getMessage()); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1358 | } |
| 1359 | try { |
| 1360 | dpm.getApplicationRestrictions(null, "pkg1"); |
| 1361 | fail("Didn't throw expected SecurityException"); |
| 1362 | } catch (SecurityException expected) { |
| 1363 | MoreAsserts.assertContainsRegex( |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1364 | "Caller with uid \\d+ is not a delegate of scope delegation-app-restrictions.", |
| 1365 | expected.getMessage()); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | // Check via the profile owner that no restrictions were set. |
| 1369 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1370 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1371 | assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size()); |
| 1372 | |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1373 | // Check the API does not allow setting a non-existent package |
| 1374 | try { |
| 1375 | dpm.setApplicationRestrictionsManagingPackage(admin1, |
| 1376 | nonExistAppRestrictionsManagerPackage); |
| 1377 | fail("Non-existent app set as app restriction manager."); |
Victor Chang | cd14c0a | 2016-03-16 19:10:15 +0000 | [diff] [blame] | 1378 | } catch (PackageManager.NameNotFoundException expected) { |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1379 | MoreAsserts.assertContainsRegex( |
Victor Chang | cd14c0a | 2016-03-16 19:10:15 +0000 | [diff] [blame] | 1380 | nonExistAppRestrictionsManagerPackage, expected.getMessage()); |
Rubin Xu | ed1928a | 2016-02-11 17:23:06 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1383 | // Let appRestrictionsManagerPackage manage app restrictions |
| 1384 | dpm.setApplicationRestrictionsManagingPackage(admin1, appRestrictionsManagerPackage); |
| 1385 | assertEquals(appRestrictionsManagerPackage, |
| 1386 | dpm.getApplicationRestrictionsManagingPackage(admin1)); |
| 1387 | |
| 1388 | // Now that package should be able to set and retrieve app restrictions. |
| 1389 | mContext.binder.callingUid = appRestrictionsManagerUid; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1390 | mContext.packageName = appRestrictionsManagerPackage; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1391 | assertTrue(dpm.isCallerApplicationRestrictionsManagingPackage()); |
| 1392 | dpm.setApplicationRestrictions(null, "pkg1", rest); |
| 1393 | Bundle returned = dpm.getApplicationRestrictions(null, "pkg1"); |
| 1394 | assertEquals(1, returned.size(), 1); |
| 1395 | assertEquals("Foo1", returned.get("KEY_STRING")); |
| 1396 | |
| 1397 | // The same app running on a separate user shouldn't be able to manage app restrictions. |
| 1398 | mContext.binder.callingUid = UserHandle.getUid( |
| 1399 | UserHandle.USER_SYSTEM, appRestrictionsManagerAppId); |
| 1400 | assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage()); |
| 1401 | try { |
| 1402 | dpm.setApplicationRestrictions(null, "pkg1", rest); |
| 1403 | fail("Didn't throw expected SecurityException"); |
| 1404 | } catch (SecurityException expected) { |
| 1405 | MoreAsserts.assertContainsRegex( |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1406 | "Caller with uid \\d+ is not a delegate of scope delegation-app-restrictions.", |
| 1407 | expected.getMessage()); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // The DPM is still able to manage app restrictions, even if it allowed another app to do it |
| 1411 | // too. |
| 1412 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1413 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1414 | assertEquals(returned, dpm.getApplicationRestrictions(admin1, "pkg1")); |
| 1415 | dpm.setApplicationRestrictions(admin1, "pkg1", null); |
| 1416 | assertEquals(0, dpm.getApplicationRestrictions(admin1, "pkg1").size()); |
| 1417 | |
| 1418 | // Removing the ability for the package to manage app restrictions. |
| 1419 | dpm.setApplicationRestrictionsManagingPackage(admin1, null); |
| 1420 | assertNull(dpm.getApplicationRestrictionsManagingPackage(admin1)); |
| 1421 | mContext.binder.callingUid = appRestrictionsManagerUid; |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1422 | mContext.packageName = appRestrictionsManagerPackage; |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1423 | assertFalse(dpm.isCallerApplicationRestrictionsManagingPackage()); |
| 1424 | try { |
| 1425 | dpm.setApplicationRestrictions(null, "pkg1", null); |
| 1426 | fail("Didn't throw expected SecurityException"); |
| 1427 | } catch (SecurityException expected) { |
| 1428 | MoreAsserts.assertContainsRegex( |
Edman Anjos | f994677 | 2016-11-28 16:35:15 +0100 | [diff] [blame] | 1429 | "Caller with uid \\d+ is not a delegate of scope delegation-app-restrictions.", |
| 1430 | expected.getMessage()); |
Esteban Talavera | bf60f72 | 2015-12-10 16:26:44 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1434 | public void testSetUserRestriction_asDo() throws Exception { |
| 1435 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 1436 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1437 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1438 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1439 | |
| 1440 | // First, set DO. |
| 1441 | |
| 1442 | // Call from a process on the system user. |
| 1443 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1444 | |
| 1445 | // Make sure admin1 is installed on system user. |
| 1446 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1447 | |
| 1448 | // Call. |
| 1449 | dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); |
Makoto Onuki | a52562c | 2015-10-01 16:12:31 -0700 | [diff] [blame] | 1450 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name", |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1451 | UserHandle.USER_SYSTEM)); |
| 1452 | |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 1453 | // Check that the user restrictions that are enabled by default are set. Then unset them. |
| 1454 | String[] defaultRestrictions = UserRestrictionsUtils |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1455 | .getDefaultEnabledForDeviceOwner().toArray(new String[0]); |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 1456 | DpmTestUtils.assertRestrictions( |
| 1457 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1458 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1459 | ); |
| 1460 | DpmTestUtils.assertRestrictions( |
| 1461 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1462 | dpm.getUserRestrictions(admin1) |
| 1463 | ); |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1464 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1465 | eq(UserHandle.USER_SYSTEM), |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 1466 | MockUtils.checkUserRestrictions(defaultRestrictions), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1467 | eq(true) /* isDeviceOwner */, |
| 1468 | eq(CAMERA_NOT_DISABLED) |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1469 | ); |
| 1470 | reset(mContext.userManagerInternal); |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 1471 | |
| 1472 | for (String restriction : defaultRestrictions) { |
| 1473 | dpm.clearUserRestriction(admin1, restriction); |
| 1474 | } |
| 1475 | |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1476 | assertNoDeviceOwnerRestrictions(); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1477 | reset(mContext.userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1478 | |
| 1479 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
| 1480 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1481 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1482 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), |
| 1483 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1484 | reset(mContext.userManagerInternal); |
| 1485 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1486 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1487 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1488 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1489 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS, |
| 1490 | UserManager.DISALLOW_ADD_USER), |
| 1491 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1492 | reset(mContext.userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1493 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1494 | DpmTestUtils.assertRestrictions( |
| 1495 | DpmTestUtils.newRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1496 | UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS), |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1497 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1498 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1499 | DpmTestUtils.assertRestrictions( |
| 1500 | DpmTestUtils.newRestrictions( |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1501 | UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS), |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1502 | dpm.getUserRestrictions(admin1) |
| 1503 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1504 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1505 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
| 1506 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1507 | eq(UserHandle.USER_SYSTEM), |
| 1508 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1509 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1510 | reset(mContext.userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1511 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1512 | DpmTestUtils.assertRestrictions( |
| 1513 | DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
| 1514 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1515 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1516 | DpmTestUtils.assertRestrictions( |
| 1517 | DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
| 1518 | dpm.getUserRestrictions(admin1) |
| 1519 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1520 | |
| 1521 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1522 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1523 | eq(UserHandle.USER_SYSTEM), |
| 1524 | MockUtils.checkUserRestrictions(), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1525 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1526 | reset(mContext.userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1527 | |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1528 | assertNoDeviceOwnerRestrictions(); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1529 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1530 | // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE are PO restrictions, but when |
| 1531 | // DO sets them, the scope is global. |
| 1532 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); |
| 1533 | reset(mContext.userManagerInternal); |
| 1534 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1535 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1536 | eq(UserHandle.USER_SYSTEM), |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1537 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1538 | UserManager.DISALLOW_UNMUTE_MICROPHONE), |
| 1539 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1540 | reset(mContext.userManagerInternal); |
| 1541 | |
| 1542 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); |
| 1543 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1544 | reset(mContext.userManagerInternal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1545 | |
| 1546 | // More tests. |
| 1547 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); |
| 1548 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1549 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1550 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), |
| 1551 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1552 | reset(mContext.userManagerInternal); |
| 1553 | |
| 1554 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_FUN); |
| 1555 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1556 | eq(UserHandle.USER_SYSTEM), |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1557 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1558 | UserManager.DISALLOW_ADD_USER), |
| 1559 | eq(true), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1560 | reset(mContext.userManagerInternal); |
| 1561 | |
| 1562 | dpm.setCameraDisabled(admin1, true); |
| 1563 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1564 | eq(UserHandle.USER_SYSTEM), |
| 1565 | // DISALLOW_CAMERA will be applied to both local and global. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1566 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1567 | UserManager.DISALLOW_ADD_USER), |
| 1568 | eq(true), eq(CAMERA_DISABLED_GLOBALLY)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1569 | reset(mContext.userManagerInternal); |
| 1570 | |
| 1571 | // Set up another DA and let it disable camera. Now DISALLOW_CAMERA will only be applied |
| 1572 | // locally. |
| 1573 | dpm.setCameraDisabled(admin1, false); |
| 1574 | reset(mContext.userManagerInternal); |
| 1575 | |
| 1576 | setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1577 | dpm.setActiveAdmin(admin2, /* replace =*/ false, UserHandle.USER_SYSTEM); |
| 1578 | dpm.setCameraDisabled(admin2, true); |
| 1579 | |
| 1580 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1581 | eq(UserHandle.USER_SYSTEM), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1582 | // DISALLOW_CAMERA will be applied to both local and global. <- TODO: fix this |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1583 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1584 | UserManager.DISALLOW_ADD_USER), |
| 1585 | eq(true), eq(CAMERA_DISABLED_LOCALLY)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1586 | reset(mContext.userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1587 | // TODO Make sure restrictions are written to the file. |
| 1588 | } |
| 1589 | |
| 1590 | public void testSetUserRestriction_asPo() { |
| 1591 | setAsProfileOwner(admin1); |
| 1592 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1593 | DpmTestUtils.assertRestrictions( |
| 1594 | DpmTestUtils.newRestrictions(), |
| 1595 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1596 | .ensureUserRestrictions() |
| 1597 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1598 | |
| 1599 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1600 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1601 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1602 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1603 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1604 | reset(mContext.userManagerInternal); |
| 1605 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1606 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1607 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1608 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1609 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1610 | UserManager.DISALLOW_OUTGOING_CALLS), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1611 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1612 | reset(mContext.userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1613 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1614 | DpmTestUtils.assertRestrictions( |
| 1615 | DpmTestUtils.newRestrictions( |
| 1616 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1617 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1618 | ), |
| 1619 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1620 | .ensureUserRestrictions() |
| 1621 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1622 | DpmTestUtils.assertRestrictions( |
| 1623 | DpmTestUtils.newRestrictions( |
| 1624 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 1625 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1626 | ), |
| 1627 | dpm.getUserRestrictions(admin1) |
| 1628 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1629 | |
| 1630 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1631 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1632 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1633 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1634 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1635 | reset(mContext.userManagerInternal); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1636 | |
| 1637 | DpmTestUtils.assertRestrictions( |
| 1638 | DpmTestUtils.newRestrictions( |
| 1639 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1640 | ), |
| 1641 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1642 | .ensureUserRestrictions() |
| 1643 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1644 | DpmTestUtils.assertRestrictions( |
| 1645 | DpmTestUtils.newRestrictions( |
| 1646 | UserManager.DISALLOW_OUTGOING_CALLS |
| 1647 | ), |
| 1648 | dpm.getUserRestrictions(admin1) |
| 1649 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1650 | |
| 1651 | dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1652 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1653 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1654 | MockUtils.checkUserRestrictions(), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1655 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1656 | reset(mContext.userManagerInternal); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1657 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1658 | DpmTestUtils.assertRestrictions( |
| 1659 | DpmTestUtils.newRestrictions(), |
| 1660 | dpms.getProfileOwnerAdminLocked(DpmMockContext.CALLER_USER_HANDLE) |
| 1661 | .ensureUserRestrictions() |
| 1662 | ); |
Makoto Onuki | 3a3092f | 2015-10-30 11:07:51 -0700 | [diff] [blame] | 1663 | DpmTestUtils.assertRestrictions( |
| 1664 | DpmTestUtils.newRestrictions(), |
| 1665 | dpm.getUserRestrictions(admin1) |
| 1666 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1667 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1668 | // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE can be set by PO too, even |
| 1669 | // though when DO sets them they'll be applied globally. |
| 1670 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); |
| 1671 | reset(mContext.userManagerInternal); |
| 1672 | dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1673 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1674 | eq(DpmMockContext.CALLER_USER_HANDLE), |
| 1675 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, |
| 1676 | UserManager.DISALLOW_UNMUTE_MICROPHONE), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1677 | eq(false), eq(CAMERA_NOT_DISABLED)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1678 | reset(mContext.userManagerInternal); |
| 1679 | |
| 1680 | dpm.setCameraDisabled(admin1, true); |
| 1681 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1682 | eq(DpmMockContext.CALLER_USER_HANDLE), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1683 | MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1684 | UserManager.DISALLOW_UNMUTE_MICROPHONE), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1685 | eq(false), eq(CAMERA_DISABLED_LOCALLY)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1686 | reset(mContext.userManagerInternal); |
| 1687 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1688 | // TODO Make sure restrictions are written to the file. |
| 1689 | } |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1690 | |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1691 | |
| 1692 | public void testDefaultEnabledUserRestrictions() throws Exception { |
| 1693 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1694 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 1695 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1696 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1697 | |
| 1698 | // First, set DO. |
| 1699 | |
| 1700 | // Call from a process on the system user. |
| 1701 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1702 | |
| 1703 | // Make sure admin1 is installed on system user. |
| 1704 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1705 | |
| 1706 | dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); |
| 1707 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name", |
| 1708 | UserHandle.USER_SYSTEM)); |
| 1709 | |
| 1710 | // Check that the user restrictions that are enabled by default are set. Then unset them. |
| 1711 | String[] defaultRestrictions = UserRestrictionsUtils |
| 1712 | .getDefaultEnabledForDeviceOwner().toArray(new String[0]); |
| 1713 | assertTrue(defaultRestrictions.length > 0); |
| 1714 | DpmTestUtils.assertRestrictions( |
| 1715 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1716 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1717 | ); |
| 1718 | DpmTestUtils.assertRestrictions( |
| 1719 | DpmTestUtils.newRestrictions(defaultRestrictions), |
| 1720 | dpm.getUserRestrictions(admin1) |
| 1721 | ); |
| 1722 | verify(mContext.userManagerInternal).setDevicePolicyUserRestrictions( |
| 1723 | eq(UserHandle.USER_SYSTEM), |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 1724 | MockUtils.checkUserRestrictions(defaultRestrictions), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1725 | eq(true) /* isDeviceOwner */, |
| 1726 | eq(CAMERA_NOT_DISABLED) |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1727 | ); |
| 1728 | reset(mContext.userManagerInternal); |
| 1729 | |
| 1730 | for (String restriction : defaultRestrictions) { |
| 1731 | dpm.clearUserRestriction(admin1, restriction); |
| 1732 | } |
| 1733 | |
| 1734 | assertNoDeviceOwnerRestrictions(); |
| 1735 | |
| 1736 | // Initialize DPMS again and check that the user restriction wasn't enabled again. |
| 1737 | reset(mContext.userManagerInternal); |
| 1738 | initializeDpms(); |
| 1739 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 1740 | assertNotNull(dpms.getDeviceOwnerAdminLocked()); |
| 1741 | |
| 1742 | assertNoDeviceOwnerRestrictions(); |
| 1743 | |
| 1744 | // Add a new restriction to the default set, initialize DPMS, and check that the restriction |
| 1745 | // is set as it wasn't enabled during setDeviceOwner. |
| 1746 | final String newDefaultEnabledRestriction = UserManager.DISALLOW_REMOVE_MANAGED_PROFILE; |
| 1747 | assertFalse(UserRestrictionsUtils |
| 1748 | .getDefaultEnabledForDeviceOwner().contains(newDefaultEnabledRestriction)); |
| 1749 | UserRestrictionsUtils |
| 1750 | .getDefaultEnabledForDeviceOwner().add(newDefaultEnabledRestriction); |
| 1751 | try { |
| 1752 | reset(mContext.userManagerInternal); |
| 1753 | initializeDpms(); |
| 1754 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 1755 | assertNotNull(dpms.getDeviceOwnerAdminLocked()); |
| 1756 | |
| 1757 | DpmTestUtils.assertRestrictions( |
| 1758 | DpmTestUtils.newRestrictions(newDefaultEnabledRestriction), |
| 1759 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1760 | ); |
| 1761 | DpmTestUtils.assertRestrictions( |
| 1762 | DpmTestUtils.newRestrictions(newDefaultEnabledRestriction), |
| 1763 | dpm.getUserRestrictions(admin1) |
| 1764 | ); |
| 1765 | verify(mContext.userManagerInternal, atLeast(1)).setDevicePolicyUserRestrictions( |
| 1766 | eq(UserHandle.USER_SYSTEM), |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 1767 | MockUtils.checkUserRestrictions(newDefaultEnabledRestriction), |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 1768 | eq(true) /* isDeviceOwner */, |
| 1769 | eq(CAMERA_NOT_DISABLED) |
Esteban Talavera | 548a04b | 2016-12-20 15:22:30 +0000 | [diff] [blame] | 1770 | ); |
| 1771 | reset(mContext.userManagerInternal); |
| 1772 | |
| 1773 | // Remove the restriction. |
| 1774 | dpm.clearUserRestriction(admin1, newDefaultEnabledRestriction); |
| 1775 | |
| 1776 | // Initialize DPMS again. The restriction shouldn't be enabled for a second time. |
| 1777 | initializeDpms(); |
| 1778 | assertTrue(dpm.isDeviceOwnerApp(admin1.getPackageName())); |
| 1779 | assertNotNull(dpms.getDeviceOwnerAdminLocked()); |
| 1780 | assertNoDeviceOwnerRestrictions(); |
| 1781 | } finally { |
| 1782 | UserRestrictionsUtils |
| 1783 | .getDefaultEnabledForDeviceOwner().remove(newDefaultEnabledRestriction); |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | private void assertNoDeviceOwnerRestrictions() { |
| 1788 | DpmTestUtils.assertRestrictions( |
| 1789 | DpmTestUtils.newRestrictions(), |
| 1790 | dpms.getDeviceOwnerAdminLocked().ensureUserRestrictions() |
| 1791 | ); |
| 1792 | DpmTestUtils.assertRestrictions( |
| 1793 | DpmTestUtils.newRestrictions(), |
| 1794 | dpm.getUserRestrictions(admin1) |
| 1795 | ); |
| 1796 | } |
| 1797 | |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1798 | public void testGetMacAddress() throws Exception { |
| 1799 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1800 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1801 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 1802 | |
| 1803 | // In this test, change the caller user to "system". |
| 1804 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1805 | |
| 1806 | // Make sure admin1 is installed on system user. |
| 1807 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1808 | |
| 1809 | // Test 1. Caller doesn't have DO or DA. |
| 1810 | try { |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1811 | dpm.getWifiMacAddress(admin1); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1812 | fail(); |
| 1813 | } catch (SecurityException e) { |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1814 | MoreAsserts.assertContainsRegex("No active admin", e.getMessage()); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | // DO needs to be an DA. |
| 1818 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 1819 | assertTrue(dpm.isAdminActive(admin1)); |
| 1820 | |
| 1821 | // Test 2. Caller has DA, but not DO. |
| 1822 | try { |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1823 | dpm.getWifiMacAddress(admin1); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1824 | fail(); |
| 1825 | } catch (SecurityException e) { |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1826 | MoreAsserts.assertContainsRegex("does not own the device", e.getMessage()); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | // Test 3. Caller has PO, but not DO. |
| 1830 | assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 1831 | try { |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1832 | dpm.getWifiMacAddress(admin1); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1833 | fail(); |
| 1834 | } catch (SecurityException e) { |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1835 | MoreAsserts.assertContainsRegex("does not own the device", e.getMessage()); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | // Remove PO. |
| 1839 | dpm.clearProfileOwner(admin1); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 1840 | dpm.setActiveAdmin(admin1, false); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1841 | // Test 4, Caller is DO now. |
| 1842 | assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 1843 | |
| 1844 | // 4-1. But no WifiInfo. |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1845 | assertNull(dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1846 | |
| 1847 | // 4-2. Returns WifiInfo, but with the default MAC. |
| 1848 | when(mContext.wifiManager.getConnectionInfo()).thenReturn(new WifiInfo()); |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1849 | assertNull(dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1850 | |
| 1851 | // 4-3. With a real MAC address. |
| 1852 | final WifiInfo wi = new WifiInfo(); |
| 1853 | wi.setMacAddress("11:22:33:44:55:66"); |
| 1854 | when(mContext.wifiManager.getConnectionInfo()).thenReturn(wi); |
Sudheer Shanka | 3cb4da1 | 2016-03-07 18:51:49 -0800 | [diff] [blame] | 1855 | assertEquals("11:22:33:44:55:66", dpm.getWifiMacAddress(admin1)); |
Makoto Onuki | a31ebbc | 2015-11-23 17:15:21 -0800 | [diff] [blame] | 1856 | } |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1857 | |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1858 | public void testReboot() throws Exception { |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1859 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1860 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 1861 | |
| 1862 | // In this test, change the caller user to "system". |
| 1863 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 1864 | |
| 1865 | // Make sure admin1 is installed on system user. |
| 1866 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 1867 | |
| 1868 | // Set admin1 as DA. |
| 1869 | dpm.setActiveAdmin(admin1, false); |
| 1870 | assertTrue(dpm.isAdminActive(admin1)); |
| 1871 | try { |
| 1872 | dpm.reboot(admin1); |
| 1873 | fail("DA calls DPM.reboot(), did not throw expected SecurityException"); |
| 1874 | } catch (SecurityException expected) { |
| 1875 | MoreAsserts.assertContainsRegex("does not own the device", expected.getMessage()); |
| 1876 | } |
| 1877 | |
| 1878 | // Set admin1 as PO. |
| 1879 | assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 1880 | try { |
| 1881 | dpm.reboot(admin1); |
| 1882 | fail("PO calls DPM.reboot(), did not throw expected SecurityException"); |
| 1883 | } catch (SecurityException expected) { |
| 1884 | MoreAsserts.assertContainsRegex("does not own the device", expected.getMessage()); |
| 1885 | } |
| 1886 | |
| 1887 | // Remove PO and add DO. |
| 1888 | dpm.clearProfileOwner(admin1); |
Suprabh Shukla | 3cb2b49 | 2016-08-09 17:20:57 -0700 | [diff] [blame] | 1889 | dpm.setActiveAdmin(admin1, false); |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1890 | assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 1891 | |
Mahaver Chopra | 1216ae5 | 2016-03-11 15:39:48 +0000 | [diff] [blame] | 1892 | // admin1 is DO. |
| 1893 | // Set current call state of device to ringing. |
| 1894 | when(mContext.telephonyManager.getCallState()) |
| 1895 | .thenReturn(TelephonyManager.CALL_STATE_RINGING); |
| 1896 | try { |
| 1897 | dpm.reboot(admin1); |
| 1898 | fail("DPM.reboot() called when receiveing a call, should thrown IllegalStateException"); |
| 1899 | } catch (IllegalStateException expected) { |
| 1900 | MoreAsserts.assertContainsRegex("ongoing call on the device", expected.getMessage()); |
| 1901 | } |
| 1902 | |
| 1903 | // Set current call state of device to dialing/active. |
| 1904 | when(mContext.telephonyManager.getCallState()) |
| 1905 | .thenReturn(TelephonyManager.CALL_STATE_OFFHOOK); |
| 1906 | try { |
| 1907 | dpm.reboot(admin1); |
| 1908 | fail("DPM.reboot() called when dialing, should thrown IllegalStateException"); |
| 1909 | } catch (IllegalStateException expected) { |
| 1910 | MoreAsserts.assertContainsRegex("ongoing call on the device", expected.getMessage()); |
| 1911 | } |
| 1912 | |
| 1913 | // Set current call state of device to idle. |
| 1914 | when(mContext.telephonyManager.getCallState()).thenReturn(TelephonyManager.CALL_STATE_IDLE); |
Mahaver Chopra | f8373b5 | 2015-12-23 14:42:18 +0000 | [diff] [blame] | 1915 | dpm.reboot(admin1); |
| 1916 | } |
Kenny Guy | 06de4e7 | 2015-12-22 12:07:39 +0000 | [diff] [blame] | 1917 | |
| 1918 | public void testSetGetSupportText() { |
| 1919 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 1920 | dpm.setActiveAdmin(admin1, true); |
| 1921 | dpm.setActiveAdmin(admin2, true); |
| 1922 | mContext.callerPermissions.remove(permission.MANAGE_DEVICE_ADMINS); |
| 1923 | |
| 1924 | // Null default support messages. |
| 1925 | { |
| 1926 | assertNull(dpm.getLongSupportMessage(admin1)); |
| 1927 | assertNull(dpm.getShortSupportMessage(admin1)); |
| 1928 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1929 | assertNull(dpm.getShortSupportMessageForUser(admin1, |
| 1930 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1931 | assertNull(dpm.getLongSupportMessageForUser(admin1, |
| 1932 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1933 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1934 | } |
| 1935 | |
| 1936 | // Only system can call the per user versions. |
| 1937 | { |
| 1938 | try { |
| 1939 | dpm.getShortSupportMessageForUser(admin1, |
| 1940 | DpmMockContext.CALLER_USER_HANDLE); |
| 1941 | fail("Only system should be able to call getXXXForUser versions"); |
| 1942 | } catch (SecurityException expected) { |
| 1943 | MoreAsserts.assertContainsRegex("message for user", expected.getMessage()); |
| 1944 | } |
| 1945 | try { |
| 1946 | dpm.getLongSupportMessageForUser(admin1, |
| 1947 | DpmMockContext.CALLER_USER_HANDLE); |
| 1948 | fail("Only system should be able to call getXXXForUser versions"); |
| 1949 | } catch (SecurityException expected) { |
| 1950 | MoreAsserts.assertContainsRegex("message for user", expected.getMessage()); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | // Can't set message for admin in another uid. |
| 1955 | { |
| 1956 | mContext.binder.callingUid = DpmMockContext.CALLER_UID + 1; |
| 1957 | try { |
| 1958 | dpm.setShortSupportMessage(admin1, "Some text"); |
| 1959 | fail("Admins should only be able to change their own support text."); |
| 1960 | } catch (SecurityException expected) { |
| 1961 | MoreAsserts.assertContainsRegex("is not owned by uid", expected.getMessage()); |
| 1962 | } |
| 1963 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1964 | } |
| 1965 | |
| 1966 | // Set/Get short returns what it sets and other admins text isn't changed. |
| 1967 | { |
| 1968 | final String supportText = "Some text to test with."; |
| 1969 | dpm.setShortSupportMessage(admin1, supportText); |
| 1970 | assertEquals(supportText, dpm.getShortSupportMessage(admin1)); |
| 1971 | assertNull(dpm.getLongSupportMessage(admin1)); |
| 1972 | assertNull(dpm.getShortSupportMessage(admin2)); |
| 1973 | |
| 1974 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1975 | assertEquals(supportText, dpm.getShortSupportMessageForUser(admin1, |
| 1976 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1977 | assertNull(dpm.getShortSupportMessageForUser(admin2, |
| 1978 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1979 | assertNull(dpm.getLongSupportMessageForUser(admin1, |
| 1980 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1981 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 1982 | |
| 1983 | dpm.setShortSupportMessage(admin1, null); |
| 1984 | assertNull(dpm.getShortSupportMessage(admin1)); |
| 1985 | } |
| 1986 | |
| 1987 | // Set/Get long returns what it sets and other admins text isn't changed. |
| 1988 | { |
| 1989 | final String supportText = "Some text to test with.\nWith more text."; |
| 1990 | dpm.setLongSupportMessage(admin1, supportText); |
| 1991 | assertEquals(supportText, dpm.getLongSupportMessage(admin1)); |
| 1992 | assertNull(dpm.getShortSupportMessage(admin1)); |
| 1993 | assertNull(dpm.getLongSupportMessage(admin2)); |
| 1994 | |
| 1995 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 1996 | assertEquals(supportText, dpm.getLongSupportMessageForUser(admin1, |
| 1997 | DpmMockContext.CALLER_USER_HANDLE)); |
| 1998 | assertNull(dpm.getLongSupportMessageForUser(admin2, |
| 1999 | DpmMockContext.CALLER_USER_HANDLE)); |
| 2000 | assertNull(dpm.getShortSupportMessageForUser(admin1, |
| 2001 | DpmMockContext.CALLER_USER_HANDLE)); |
| 2002 | mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2003 | |
| 2004 | dpm.setLongSupportMessage(admin1, null); |
| 2005 | assertNull(dpm.getLongSupportMessage(admin1)); |
| 2006 | } |
| 2007 | } |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2008 | |
phweiss | 73145f4 | 2017-01-17 19:06:38 +0100 | [diff] [blame] | 2009 | public void testCreateAdminSupportIntent() throws Exception { |
| 2010 | // Setup device owner. |
| 2011 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2012 | setupDeviceOwner(); |
| 2013 | |
| 2014 | // Nonexisting permission returns null |
| 2015 | Intent intent = dpm.createAdminSupportIntent("disallow_nothing"); |
| 2016 | assertNull(intent); |
| 2017 | |
| 2018 | // Existing permission that is not set returns null |
| 2019 | intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); |
| 2020 | assertNull(intent); |
| 2021 | |
| 2022 | // Existing permission that is not set by device/profile owner returns null |
| 2023 | when(mContext.userManager.hasUserRestriction( |
| 2024 | eq(UserManager.DISALLOW_ADJUST_VOLUME), |
| 2025 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2026 | .thenReturn(true); |
| 2027 | intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); |
| 2028 | assertNull(intent); |
| 2029 | |
| 2030 | // Permission that is set by device owner returns correct intent |
| 2031 | when(mContext.userManager.getUserRestrictionSource( |
| 2032 | eq(UserManager.DISALLOW_ADJUST_VOLUME), |
| 2033 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2034 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 2035 | intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); |
| 2036 | assertNotNull(intent); |
| 2037 | assertEquals(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS, intent.getAction()); |
| 2038 | assertEquals(UserHandle.getUserId(DpmMockContext.CALLER_SYSTEM_USER_UID), |
| 2039 | intent.getIntExtra(Intent.EXTRA_USER_ID, -1)); |
| 2040 | assertEquals(admin1, |
| 2041 | (ComponentName) intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN)); |
| 2042 | assertEquals(UserManager.DISALLOW_ADJUST_VOLUME, |
| 2043 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 2044 | |
| 2045 | // Try with POLICY_DISABLE_CAMERA and POLICY_DISABLE_SCREEN_CAPTURE, which are not |
| 2046 | // user restrictions |
| 2047 | |
| 2048 | // Camera is not disabled |
| 2049 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); |
| 2050 | assertNull(intent); |
| 2051 | |
| 2052 | // Camera is disabled |
| 2053 | dpm.setCameraDisabled(admin1, true); |
| 2054 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); |
| 2055 | assertNotNull(intent); |
| 2056 | assertEquals(DevicePolicyManager.POLICY_DISABLE_CAMERA, |
| 2057 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 2058 | |
| 2059 | // Screen capture is not disabled |
| 2060 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 2061 | assertNull(intent); |
| 2062 | |
| 2063 | // Screen capture is disabled |
| 2064 | dpm.setScreenCaptureDisabled(admin1, true); |
| 2065 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 2066 | assertNotNull(intent); |
| 2067 | assertEquals(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE, |
| 2068 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 2069 | |
| 2070 | // Same checks for different user |
| 2071 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2072 | // Camera should be disabled by device owner |
| 2073 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); |
| 2074 | assertNotNull(intent); |
| 2075 | assertEquals(DevicePolicyManager.POLICY_DISABLE_CAMERA, |
| 2076 | intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)); |
| 2077 | assertEquals(UserHandle.getUserId(DpmMockContext.CALLER_SYSTEM_USER_UID), |
| 2078 | intent.getIntExtra(Intent.EXTRA_USER_ID, -1)); |
| 2079 | // ScreenCapture should not be disabled by device owner |
| 2080 | intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); |
| 2081 | assertNull(intent); |
| 2082 | } |
| 2083 | |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2084 | /** |
| 2085 | * Test for: |
| 2086 | * {@link DevicePolicyManager#setAffiliationIds} |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2087 | * {@link DevicePolicyManager#getAffiliationIds} |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2088 | * {@link DevicePolicyManager#isAffiliatedUser} |
| 2089 | */ |
| 2090 | public void testUserAffiliation() throws Exception { |
| 2091 | mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); |
| 2092 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2093 | mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); |
| 2094 | |
| 2095 | // Check that the system user is unaffiliated. |
| 2096 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2097 | assertFalse(dpm.isAffiliatedUser()); |
| 2098 | |
| 2099 | // Set a device owner on the system user. Check that the system user becomes affiliated. |
| 2100 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 2101 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 2102 | assertTrue(dpm.setDeviceOwner(admin1, "owner-name")); |
| 2103 | assertTrue(dpm.isAffiliatedUser()); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2104 | assertTrue(dpm.getAffiliationIds(admin1).isEmpty()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2105 | |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2106 | // Install a profile owner. Check that the test user is unaffiliated. |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2107 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2108 | setAsProfileOwner(admin2); |
| 2109 | assertFalse(dpm.isAffiliatedUser()); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2110 | assertTrue(dpm.getAffiliationIds(admin2).isEmpty()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2111 | |
| 2112 | // Have the profile owner specify a set of affiliation ids. Check that the test user remains |
| 2113 | // unaffiliated. |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2114 | final List<String> userAffiliationIds = new ArrayList<>(); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2115 | userAffiliationIds.add("red"); |
| 2116 | userAffiliationIds.add("green"); |
| 2117 | userAffiliationIds.add("blue"); |
| 2118 | dpm.setAffiliationIds(admin2, userAffiliationIds); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2119 | MoreAsserts.assertContentsInAnyOrder(dpm.getAffiliationIds(admin2), "red", "green", "blue"); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2120 | assertFalse(dpm.isAffiliatedUser()); |
| 2121 | |
| 2122 | // Have the device owner specify a set of affiliation ids that do not intersect with those |
| 2123 | // specified by the profile owner. Check that the test user remains unaffiliated. |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2124 | final List<String> deviceAffiliationIds = new ArrayList<>(); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2125 | deviceAffiliationIds.add("cyan"); |
| 2126 | deviceAffiliationIds.add("yellow"); |
| 2127 | deviceAffiliationIds.add("magenta"); |
| 2128 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2129 | dpm.setAffiliationIds(admin1, deviceAffiliationIds); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2130 | MoreAsserts.assertContentsInAnyOrder( |
| 2131 | dpm.getAffiliationIds(admin1), "cyan", "yellow", "magenta"); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2132 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 2133 | assertFalse(dpm.isAffiliatedUser()); |
| 2134 | |
| 2135 | // Have the profile owner specify a set of affiliation ids that intersect with those |
| 2136 | // specified by the device owner. Check that the test user becomes affiliated. |
| 2137 | userAffiliationIds.add("yellow"); |
| 2138 | dpm.setAffiliationIds(admin2, userAffiliationIds); |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2139 | MoreAsserts.assertContentsInAnyOrder( |
| 2140 | dpm.getAffiliationIds(admin2), "red", "green", "blue", "yellow"); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2141 | assertTrue(dpm.isAffiliatedUser()); |
| 2142 | |
Esteban Talavera | c9bb378 | 2016-11-11 15:41:14 +0000 | [diff] [blame] | 2143 | // Clear affiliation ids for the profile owner. The user becomes unaffiliated. |
| 2144 | dpm.setAffiliationIds(admin2, Collections.emptyList()); |
| 2145 | assertTrue(dpm.getAffiliationIds(admin2).isEmpty()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2146 | assertFalse(dpm.isAffiliatedUser()); |
| 2147 | |
Esteban Talavera | a7bd04e | 2017-02-02 18:28:04 +0000 | [diff] [blame] | 2148 | // Set affiliation ids again, then clear PO to check that the user becomes unaffiliated |
| 2149 | dpm.setAffiliationIds(admin2, userAffiliationIds); |
| 2150 | assertTrue(dpm.isAffiliatedUser()); |
| 2151 | dpm.clearProfileOwner(admin2); |
| 2152 | assertFalse(dpm.isAffiliatedUser()); |
| 2153 | |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2154 | // Check that the system user remains affiliated. |
| 2155 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2156 | assertTrue(dpm.isAffiliatedUser()); |
Esteban Talavera | a7bd04e | 2017-02-02 18:28:04 +0000 | [diff] [blame] | 2157 | |
| 2158 | // Clear the device owner - the user becomes unaffiliated. |
| 2159 | clearDeviceOwner(); |
| 2160 | assertFalse(dpm.isAffiliatedUser()); |
Bartosz Fabianowski | 4c052f2 | 2016-01-25 14:18:43 +0100 | [diff] [blame] | 2161 | } |
Alan Treadway | afad878 | 2016-01-19 15:15:08 +0000 | [diff] [blame] | 2162 | |
| 2163 | public void testGetUserProvisioningState_defaultResult() { |
| 2164 | assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState()); |
| 2165 | } |
| 2166 | |
| 2167 | public void testSetUserProvisioningState_permission() throws Exception { |
| 2168 | setupProfileOwner(); |
| 2169 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2170 | |
| 2171 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2172 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2173 | } |
| 2174 | |
| 2175 | public void testSetUserProvisioningState_unprivileged() throws Exception { |
| 2176 | setupProfileOwner(); |
| 2177 | try { |
| 2178 | dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED, |
| 2179 | DpmMockContext.CALLER_USER_HANDLE); |
| 2180 | fail("Expected SecurityException"); |
| 2181 | } catch (SecurityException expected) { |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | public void testSetUserProvisioningState_noManagement() { |
| 2186 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2187 | try { |
| 2188 | dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED, |
| 2189 | DpmMockContext.CALLER_USER_HANDLE); |
| 2190 | fail("IllegalStateException expected"); |
| 2191 | } catch (IllegalStateException e) { |
| 2192 | MoreAsserts.assertContainsRegex("change provisioning state unless a .* owner is set", |
| 2193 | e.getMessage()); |
| 2194 | } |
| 2195 | assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState()); |
| 2196 | } |
| 2197 | |
| 2198 | public void testSetUserProvisioningState_deviceOwnerFromSetupWizard() throws Exception { |
| 2199 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2200 | setupDeviceOwner(); |
| 2201 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2202 | |
| 2203 | exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, |
| 2204 | DevicePolicyManager.STATE_USER_SETUP_COMPLETE, |
| 2205 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2206 | } |
| 2207 | |
| 2208 | public void testSetUserProvisioningState_deviceOwnerFromSetupWizardAlternative() |
| 2209 | throws Exception { |
| 2210 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2211 | setupDeviceOwner(); |
| 2212 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2213 | |
| 2214 | exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, |
| 2215 | DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE, |
| 2216 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2217 | } |
| 2218 | |
| 2219 | public void testSetUserProvisioningState_deviceOwnerWithoutSetupWizard() throws Exception { |
| 2220 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2221 | setupDeviceOwner(); |
| 2222 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2223 | |
| 2224 | exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, |
| 2225 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2226 | } |
| 2227 | |
| 2228 | public void testSetUserProvisioningState_managedProfileFromSetupWizard_primaryUser() |
| 2229 | throws Exception { |
| 2230 | setupProfileOwner(); |
| 2231 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2232 | |
| 2233 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2234 | DevicePolicyManager.STATE_USER_PROFILE_COMPLETE, |
| 2235 | DevicePolicyManager.STATE_USER_UNMANAGED); |
| 2236 | } |
| 2237 | |
| 2238 | public void testSetUserProvisioningState_managedProfileFromSetupWizard_managedProfile() |
| 2239 | throws Exception { |
| 2240 | setupProfileOwner(); |
| 2241 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2242 | |
| 2243 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2244 | DevicePolicyManager.STATE_USER_SETUP_COMPLETE, |
| 2245 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2246 | } |
| 2247 | |
| 2248 | public void testSetUserProvisioningState_managedProfileWithoutSetupWizard() throws Exception { |
| 2249 | setupProfileOwner(); |
| 2250 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2251 | |
| 2252 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2253 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED); |
| 2254 | } |
| 2255 | |
| 2256 | public void testSetUserProvisioningState_illegalTransitionOutOfFinalized1() throws Exception { |
| 2257 | setupProfileOwner(); |
| 2258 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2259 | |
| 2260 | try { |
| 2261 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2262 | DevicePolicyManager.STATE_USER_SETUP_FINALIZED, |
| 2263 | DevicePolicyManager.STATE_USER_UNMANAGED); |
| 2264 | fail("Expected IllegalStateException"); |
| 2265 | } catch (IllegalStateException e) { |
| 2266 | MoreAsserts.assertContainsRegex("Cannot move to user provisioning state", |
| 2267 | e.getMessage()); |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | public void testSetUserProvisioningState_illegalTransitionToAnotherInProgressState() |
| 2272 | throws Exception { |
| 2273 | setupProfileOwner(); |
| 2274 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2275 | |
| 2276 | try { |
| 2277 | exerciseUserProvisioningTransitions(DpmMockContext.CALLER_USER_HANDLE, |
| 2278 | DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE, |
| 2279 | DevicePolicyManager.STATE_USER_SETUP_COMPLETE); |
| 2280 | fail("Expected IllegalStateException"); |
| 2281 | } catch (IllegalStateException e) { |
| 2282 | MoreAsserts.assertContainsRegex("Cannot move to user provisioning state", |
| 2283 | e.getMessage()); |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | private void exerciseUserProvisioningTransitions(int userId, int... states) { |
| 2288 | assertEquals(DevicePolicyManager.STATE_USER_UNMANAGED, dpm.getUserProvisioningState()); |
| 2289 | for (int state : states) { |
| 2290 | dpm.setUserProvisioningState(state, userId); |
| 2291 | assertEquals(state, dpm.getUserProvisioningState()); |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | private void setupProfileOwner() throws Exception { |
| 2296 | mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); |
| 2297 | |
| 2298 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); |
| 2299 | dpm.setActiveAdmin(admin1, false); |
| 2300 | assertTrue(dpm.setProfileOwner(admin1, null, DpmMockContext.CALLER_USER_HANDLE)); |
| 2301 | |
| 2302 | mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); |
| 2303 | } |
| 2304 | |
| 2305 | private void setupDeviceOwner() throws Exception { |
| 2306 | mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); |
| 2307 | |
| 2308 | setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); |
| 2309 | dpm.setActiveAdmin(admin1, false); |
| 2310 | assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM)); |
| 2311 | |
| 2312 | mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); |
| 2313 | } |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2314 | |
| 2315 | public void testSetMaximumTimeToLock() { |
| 2316 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 2317 | |
| 2318 | dpm.setActiveAdmin(admin1, /* replace =*/ false); |
| 2319 | dpm.setActiveAdmin(admin2, /* replace =*/ false); |
| 2320 | |
| 2321 | reset(mMockContext.powerManagerInternal); |
| 2322 | reset(mMockContext.settings); |
| 2323 | |
| 2324 | dpm.setMaximumTimeToLock(admin1, 0); |
| 2325 | verifyScreenTimeoutCall(null, false); |
| 2326 | reset(mMockContext.powerManagerInternal); |
| 2327 | reset(mMockContext.settings); |
| 2328 | |
| 2329 | dpm.setMaximumTimeToLock(admin1, 1); |
| 2330 | verifyScreenTimeoutCall(1, true); |
| 2331 | reset(mMockContext.powerManagerInternal); |
| 2332 | reset(mMockContext.settings); |
| 2333 | |
| 2334 | dpm.setMaximumTimeToLock(admin2, 10); |
| 2335 | verifyScreenTimeoutCall(null, false); |
| 2336 | reset(mMockContext.powerManagerInternal); |
| 2337 | reset(mMockContext.settings); |
| 2338 | |
| 2339 | dpm.setMaximumTimeToLock(admin1, 5); |
| 2340 | verifyScreenTimeoutCall(5, true); |
| 2341 | reset(mMockContext.powerManagerInternal); |
| 2342 | reset(mMockContext.settings); |
| 2343 | |
| 2344 | dpm.setMaximumTimeToLock(admin2, 4); |
| 2345 | verifyScreenTimeoutCall(4, true); |
| 2346 | reset(mMockContext.powerManagerInternal); |
| 2347 | reset(mMockContext.settings); |
| 2348 | |
| 2349 | dpm.setMaximumTimeToLock(admin1, 0); |
| 2350 | reset(mMockContext.powerManagerInternal); |
| 2351 | reset(mMockContext.settings); |
| 2352 | |
| 2353 | dpm.setMaximumTimeToLock(admin2, Integer.MAX_VALUE); |
| 2354 | verifyScreenTimeoutCall(Integer.MAX_VALUE, true); |
| 2355 | reset(mMockContext.powerManagerInternal); |
| 2356 | reset(mMockContext.settings); |
| 2357 | |
| 2358 | dpm.setMaximumTimeToLock(admin2, Integer.MAX_VALUE + 1); |
| 2359 | verifyScreenTimeoutCall(Integer.MAX_VALUE, true); |
| 2360 | reset(mMockContext.powerManagerInternal); |
| 2361 | reset(mMockContext.settings); |
| 2362 | |
| 2363 | dpm.setMaximumTimeToLock(admin2, 10); |
| 2364 | verifyScreenTimeoutCall(10, true); |
| 2365 | reset(mMockContext.powerManagerInternal); |
| 2366 | reset(mMockContext.settings); |
| 2367 | |
| 2368 | // There's no restriction; shold be set to MAX. |
| 2369 | dpm.setMaximumTimeToLock(admin2, 0); |
| 2370 | verifyScreenTimeoutCall(Integer.MAX_VALUE, false); |
| 2371 | } |
| 2372 | |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2373 | public void testSetRequiredStrongAuthTimeout_DeviceOwner() throws Exception { |
| 2374 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 2375 | setupDeviceOwner(); |
| 2376 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2377 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2378 | final long MINIMUM_STRONG_AUTH_TIMEOUT_MS = TimeUnit.HOURS.toMillis(1); |
| 2379 | final long ONE_MINUTE = TimeUnit.MINUTES.toMillis(1); |
| 2380 | final long MIN_PLUS_ONE_MINUTE = MINIMUM_STRONG_AUTH_TIMEOUT_MS + ONE_MINUTE; |
| 2381 | final long MAX_MINUS_ONE_MINUTE = DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS |
| 2382 | - ONE_MINUTE; |
| 2383 | |
| 2384 | // verify that the minimum timeout cannot be modified on user builds (system property is |
| 2385 | // not being read) |
| 2386 | mContext.buildMock.isDebuggable = false; |
| 2387 | |
| 2388 | dpm.setRequiredStrongAuthTimeout(admin1, MAX_MINUS_ONE_MINUTE); |
| 2389 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MAX_MINUS_ONE_MINUTE); |
| 2390 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MAX_MINUS_ONE_MINUTE); |
| 2391 | |
| 2392 | verify(mContext.systemProperties, never()).getLong(anyString(), anyLong()); |
| 2393 | |
| 2394 | // restore to the debuggable build state |
| 2395 | mContext.buildMock.isDebuggable = true; |
| 2396 | |
| 2397 | // Always return the default (second arg) when getting system property for long type |
| 2398 | when(mContext.systemProperties.getLong(anyString(), anyLong())).thenAnswer( |
| 2399 | new Answer<Long>() { |
| 2400 | @Override |
| 2401 | public Long answer(InvocationOnMock invocation) throws Throwable { |
| 2402 | return (Long) invocation.getArguments()[1]; |
| 2403 | } |
| 2404 | } |
| 2405 | ); |
| 2406 | |
| 2407 | // reset to default (0 means the admin is not participating, so default should be returned) |
| 2408 | dpm.setRequiredStrongAuthTimeout(admin1, 0); |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2409 | |
| 2410 | // aggregation should be the default if unset by any admin |
| 2411 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2412 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2413 | |
| 2414 | // admin not participating by default |
| 2415 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), 0); |
| 2416 | |
| 2417 | //clamping from the top |
| 2418 | dpm.setRequiredStrongAuthTimeout(admin1, |
| 2419 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS + ONE_MINUTE); |
| 2420 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), |
| 2421 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2422 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2423 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2424 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2425 | // 0 means the admin is not participating, so default should be returned |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2426 | dpm.setRequiredStrongAuthTimeout(admin1, 0); |
| 2427 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), 0); |
| 2428 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2429 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2430 | |
| 2431 | // clamping from the bottom |
| 2432 | dpm.setRequiredStrongAuthTimeout(admin1, MINIMUM_STRONG_AUTH_TIMEOUT_MS - ONE_MINUTE); |
| 2433 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MINIMUM_STRONG_AUTH_TIMEOUT_MS); |
| 2434 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MINIMUM_STRONG_AUTH_TIMEOUT_MS); |
| 2435 | |
Michal Karpinski | d084ca5 | 2017-01-18 15:54:18 +0000 | [diff] [blame] | 2436 | // values within range |
| 2437 | dpm.setRequiredStrongAuthTimeout(admin1, MIN_PLUS_ONE_MINUTE); |
| 2438 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MIN_PLUS_ONE_MINUTE); |
| 2439 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MIN_PLUS_ONE_MINUTE); |
| 2440 | |
| 2441 | dpm.setRequiredStrongAuthTimeout(admin1, MAX_MINUS_ONE_MINUTE); |
| 2442 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), MAX_MINUS_ONE_MINUTE); |
| 2443 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), MAX_MINUS_ONE_MINUTE); |
Michal Karpinski | 943aabd | 2016-10-06 11:09:25 +0100 | [diff] [blame] | 2444 | |
| 2445 | // reset to default |
| 2446 | dpm.setRequiredStrongAuthTimeout(admin1, 0); |
| 2447 | assertEquals(dpm.getRequiredStrongAuthTimeout(admin1), 0); |
| 2448 | assertEquals(dpm.getRequiredStrongAuthTimeout(null), |
| 2449 | DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS); |
| 2450 | |
| 2451 | // negative value |
| 2452 | try { |
| 2453 | dpm.setRequiredStrongAuthTimeout(admin1, -ONE_MINUTE); |
| 2454 | fail("Didn't throw IllegalArgumentException"); |
| 2455 | } catch (IllegalArgumentException iae) { |
| 2456 | } |
| 2457 | } |
| 2458 | |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2459 | private void verifyScreenTimeoutCall(Integer expectedTimeout, |
| 2460 | boolean shouldStayOnWhilePluggedInBeCleared) { |
| 2461 | if (expectedTimeout == null) { |
| 2462 | verify(mMockContext.powerManagerInternal, times(0)) |
| 2463 | .setMaximumScreenOffTimeoutFromDeviceAdmin(anyInt()); |
| 2464 | } else { |
| 2465 | verify(mMockContext.powerManagerInternal, times(1)) |
| 2466 | .setMaximumScreenOffTimeoutFromDeviceAdmin(eq(expectedTimeout)); |
| 2467 | } |
| 2468 | // TODO Verify calls to settingsGlobalPutInt. Tried but somehow mockito threw |
| 2469 | // UnfinishedVerificationException. |
| 2470 | } |
Makoto Onuki | 2a3c3da | 2016-02-18 14:25:30 -0800 | [diff] [blame] | 2471 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2472 | private void setup_DeviceAdminFeatureOff() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2473 | when(mContext.packageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) |
| 2474 | .thenReturn(false); |
| 2475 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2476 | .thenReturn(false); |
| 2477 | initializeDpms(); |
| 2478 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2479 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2480 | .thenReturn(true); |
| 2481 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2482 | |
| 2483 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2484 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2485 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2486 | public void testIsProvisioningAllowed_DeviceAdminFeatureOff() throws Exception { |
| 2487 | setup_DeviceAdminFeatureOff(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2488 | mContext.packageName = admin1.getPackageName(); |
| 2489 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2490 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); |
| 2491 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2492 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2493 | false); |
| 2494 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, false); |
| 2495 | } |
| 2496 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2497 | public void testCheckProvisioningPreCondition_DeviceAdminFeatureOff() throws Exception { |
| 2498 | setup_DeviceAdminFeatureOff(); |
| 2499 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2500 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2501 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2502 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2503 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2504 | assertCheckProvisioningPreCondition( |
| 2505 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2506 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2507 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2508 | DevicePolicyManager.CODE_DEVICE_ADMIN_NOT_SUPPORTED); |
| 2509 | } |
| 2510 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2511 | private void setup_ManagedProfileFeatureOff() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2512 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2513 | .thenReturn(false); |
| 2514 | initializeDpms(); |
| 2515 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2516 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2517 | .thenReturn(true); |
| 2518 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2519 | |
| 2520 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2521 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2522 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2523 | public void testIsProvisioningAllowed_ManagedProfileFeatureOff() throws Exception { |
| 2524 | setup_ManagedProfileFeatureOff(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2525 | mContext.packageName = admin1.getPackageName(); |
| 2526 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2527 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2528 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2529 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2530 | false); |
| 2531 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, false); |
| 2532 | |
| 2533 | // Test again when split user is on |
| 2534 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2535 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2536 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2537 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2538 | true); |
| 2539 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, false); |
| 2540 | } |
| 2541 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2542 | public void testCheckProvisioningPreCondition_ManagedProfileFeatureOff() throws Exception { |
| 2543 | setup_ManagedProfileFeatureOff(); |
| 2544 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2545 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2546 | DevicePolicyManager.CODE_OK); |
| 2547 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2548 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2549 | assertCheckProvisioningPreCondition( |
| 2550 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2551 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2552 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2553 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2554 | |
| 2555 | // Test again when split user is on |
| 2556 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2557 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2558 | DevicePolicyManager.CODE_OK); |
| 2559 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2560 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2561 | assertCheckProvisioningPreCondition( |
| 2562 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2563 | DevicePolicyManager.CODE_OK); |
| 2564 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2565 | DevicePolicyManager.CODE_MANAGED_USERS_NOT_SUPPORTED); |
| 2566 | } |
| 2567 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2568 | private void setup_nonSplitUser_firstBoot_primaryUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2569 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2570 | .thenReturn(true); |
| 2571 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2572 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2573 | .thenReturn(true); |
| 2574 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2575 | |
| 2576 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2577 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2578 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2579 | public void testIsProvisioningAllowed_nonSplitUser_firstBoot_primaryUser() throws Exception { |
| 2580 | setup_nonSplitUser_firstBoot_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2581 | mContext.packageName = admin1.getPackageName(); |
| 2582 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2583 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2584 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2585 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2586 | false /* because of non-split user */); |
| 2587 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2588 | false /* because of non-split user */); |
| 2589 | } |
| 2590 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2591 | public void testCheckProvisioningPreCondition_nonSplitUser_firstBoot_primaryUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2592 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2593 | setup_nonSplitUser_firstBoot_primaryUser(); |
| 2594 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2595 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2596 | DevicePolicyManager.CODE_OK); |
| 2597 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2598 | DevicePolicyManager.CODE_OK); |
| 2599 | assertCheckProvisioningPreCondition( |
| 2600 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2601 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2602 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2603 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2604 | } |
| 2605 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2606 | private void setup_nonSplitUser_afterDeviceSetup_primaryUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2607 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2608 | .thenReturn(true); |
| 2609 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(false); |
| 2610 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2611 | .thenReturn(true); |
| 2612 | setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); |
| 2613 | |
| 2614 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2615 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2616 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2617 | private void setup_nonSplitUser_withDo_primaryUser() throws Exception { |
| 2618 | setDeviceOwner(); |
| 2619 | setup_nonSplitUser_afterDeviceSetup_primaryUser(); |
| 2620 | setUpPackageManagerForFakeAdmin(adminAnotherPackage, DpmMockContext.ANOTHER_UID, admin2); |
| 2621 | } |
| 2622 | |
| 2623 | private void setup_nonSplitUser_withDo_primaryUser_ManagedProfile() throws Exception { |
| 2624 | setup_nonSplitUser_withDo_primaryUser(); |
| 2625 | final int MANAGED_PROFILE_USER_ID = 18; |
| 2626 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 1308); |
| 2627 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 2628 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 2629 | false /* we can't remove a managed profile */)).thenReturn(false); |
| 2630 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, |
| 2631 | true)).thenReturn(true); |
| 2632 | } |
| 2633 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2634 | public void testIsProvisioningAllowed_nonSplitUser_afterDeviceSetup_primaryUser() |
| 2635 | throws Exception { |
| 2636 | setup_nonSplitUser_afterDeviceSetup_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2637 | mContext.packageName = admin1.getPackageName(); |
| 2638 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2639 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2640 | false/* because of completed device setup */); |
| 2641 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2642 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2643 | false/* because of non-split user */); |
| 2644 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2645 | false/* because of non-split user */); |
| 2646 | } |
| 2647 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2648 | public void testCheckProvisioningPreCondition_nonSplitUser_afterDeviceSetup_primaryUser() |
| 2649 | throws Exception { |
| 2650 | setup_nonSplitUser_afterDeviceSetup_primaryUser(); |
| 2651 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2652 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2653 | DevicePolicyManager.CODE_USER_SETUP_COMPLETED); |
| 2654 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2655 | DevicePolicyManager.CODE_OK); |
| 2656 | assertCheckProvisioningPreCondition( |
| 2657 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2658 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2659 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2660 | DevicePolicyManager.CODE_NOT_SYSTEM_USER_SPLIT); |
| 2661 | } |
| 2662 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2663 | public void testProvisioning_nonSplitUser_withDo_primaryUser() throws Exception { |
| 2664 | setup_nonSplitUser_withDo_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2665 | mContext.packageName = admin1.getPackageName(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2666 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2667 | |
| 2668 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2669 | DevicePolicyManager.CODE_HAS_DEVICE_OWNER); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2670 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2671 | |
| 2672 | // COMP mode is allowed. |
| 2673 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2674 | DevicePolicyManager.CODE_OK); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2675 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2676 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2677 | // And other DPCs can also provision a managed profile (DO + BYOD case). |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2678 | assertCheckProvisioningPreCondition( |
| 2679 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2680 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2681 | DevicePolicyManager.CODE_OK); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2682 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true, |
| 2683 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2684 | } |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2685 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2686 | public void testProvisioning_nonSplitUser_withDo_primaryUser_restrictedByDo() throws Exception { |
| 2687 | setup_nonSplitUser_withDo_primaryUser(); |
| 2688 | mContext.packageName = admin1.getPackageName(); |
| 2689 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2690 | // The DO should be allowed to initiate provisioning if it set the restriction itself, but |
| 2691 | // other packages should be forbidden. |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2692 | when(mContext.userManager.hasUserRestriction( |
| 2693 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2694 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2695 | .thenReturn(true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2696 | when(mContext.userManager.getUserRestrictionSource( |
| 2697 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2698 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2699 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 2700 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2701 | DevicePolicyManager.CODE_OK); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2702 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2703 | assertCheckProvisioningPreCondition( |
| 2704 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2705 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2706 | DevicePolicyManager.CODE_ADD_MANAGED_PROFILE_DISALLOWED); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2707 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, |
| 2708 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2709 | } |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2710 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2711 | public void testProvisioning_nonSplitUser_withDo_primaryUser_restrictedBySystem() |
| 2712 | throws Exception { |
| 2713 | setup_nonSplitUser_withDo_primaryUser(); |
| 2714 | mContext.packageName = admin1.getPackageName(); |
| 2715 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2716 | // The DO should not be allowed to initiate provisioning if the restriction is set by |
| 2717 | // another entity. |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2718 | when(mContext.userManager.hasUserRestriction( |
| 2719 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2720 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2721 | .thenReturn(true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2722 | when(mContext.userManager.getUserRestrictionSource( |
| 2723 | eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), |
| 2724 | eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) |
| 2725 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 2726 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2727 | DevicePolicyManager.CODE_ADD_MANAGED_PROFILE_DISALLOWED); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2728 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 2729 | |
| 2730 | assertCheckProvisioningPreCondition( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2731 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2732 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2733 | DevicePolicyManager.CODE_ADD_MANAGED_PROFILE_DISALLOWED); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2734 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, |
| 2735 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2736 | } |
| 2737 | |
| 2738 | public void testCheckProvisioningPreCondition_nonSplitUser_comp() throws Exception { |
| 2739 | setup_nonSplitUser_withDo_primaryUser_ManagedProfile(); |
| 2740 | mContext.packageName = admin1.getPackageName(); |
| 2741 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2742 | |
| 2743 | // We can delete the managed profile to create a new one, so provisioning is allowed. |
| 2744 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2745 | DevicePolicyManager.CODE_OK); |
| 2746 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2747 | assertCheckProvisioningPreCondition( |
| 2748 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2749 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
| 2750 | DevicePolicyManager.CODE_OK); |
| 2751 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true, |
| 2752 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
| 2753 | } |
| 2754 | |
| 2755 | public void testCheckProvisioningPreCondition_nonSplitUser_comp_cannot_remove_profile() |
| 2756 | throws Exception { |
| 2757 | setup_nonSplitUser_withDo_primaryUser_ManagedProfile(); |
| 2758 | mContext.packageName = admin1.getPackageName(); |
| 2759 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2760 | when(mContext.userManager.hasUserRestriction( |
| 2761 | eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), |
| 2762 | eq(UserHandle.SYSTEM))) |
| 2763 | .thenReturn(true); |
Nicolas Prevot | d37c4a9 | 2017-01-23 11:56:00 +0000 | [diff] [blame] | 2764 | when(mContext.userManager.getUserRestrictionSource( |
| 2765 | eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), |
| 2766 | eq(UserHandle.SYSTEM))) |
| 2767 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2768 | |
| 2769 | // We can't remove the profile to create a new one. |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 2770 | assertCheckProvisioningPreCondition( |
| 2771 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2772 | DpmMockContext.ANOTHER_PACKAGE_NAME, |
| 2773 | DevicePolicyManager.CODE_CANNOT_ADD_MANAGED_PROFILE); |
| 2774 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, |
| 2775 | DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); |
Nicolas Prevot | d37c4a9 | 2017-01-23 11:56:00 +0000 | [diff] [blame] | 2776 | |
| 2777 | // But the device owner can still do it because it has set the restriction itself. |
| 2778 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2779 | DevicePolicyManager.CODE_OK); |
| 2780 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | private void setup_splitUser_firstBoot_systemUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2784 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2785 | .thenReturn(true); |
| 2786 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2787 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2788 | .thenReturn(false); |
| 2789 | setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); |
| 2790 | |
| 2791 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2792 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2793 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2794 | public void testIsProvisioningAllowed_splitUser_firstBoot_systemUser() throws Exception { |
| 2795 | setup_splitUser_firstBoot_systemUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2796 | mContext.packageName = admin1.getPackageName(); |
| 2797 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2798 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2799 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2800 | false /* because canAddMoreManagedProfiles returns false */); |
| 2801 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2802 | true); |
| 2803 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2804 | false/* because calling uid is system user */); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2807 | public void testCheckProvisioningPreCondition_splitUser_firstBoot_systemUser() |
| 2808 | throws Exception { |
| 2809 | setup_splitUser_firstBoot_systemUser(); |
| 2810 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2811 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2812 | DevicePolicyManager.CODE_OK); |
| 2813 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 2814 | DevicePolicyManager.CODE_SPLIT_SYSTEM_USER_DEVICE_SYSTEM_USER); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2815 | assertCheckProvisioningPreCondition( |
| 2816 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2817 | DevicePolicyManager.CODE_OK); |
| 2818 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2819 | DevicePolicyManager.CODE_SYSTEM_USER); |
| 2820 | } |
| 2821 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2822 | private void setup_splitUser_afterDeviceSetup_systemUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2823 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2824 | .thenReturn(true); |
| 2825 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2826 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2827 | .thenReturn(false); |
| 2828 | setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); |
| 2829 | |
| 2830 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2831 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2832 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2833 | public void testIsProvisioningAllowed_splitUser_afterDeviceSetup_systemUser() throws Exception { |
| 2834 | setup_splitUser_afterDeviceSetup_systemUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2835 | mContext.packageName = admin1.getPackageName(); |
| 2836 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2837 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2838 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2839 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2840 | false /* because canAddMoreManagedProfiles returns false */); |
| 2841 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2842 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2843 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2844 | false/* because calling uid is system user */); |
| 2845 | } |
| 2846 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2847 | public void testCheckProvisioningPreCondition_splitUser_afterDeviceSetup_systemUser() |
| 2848 | throws Exception { |
| 2849 | setup_splitUser_afterDeviceSetup_systemUser(); |
| 2850 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2851 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2852 | DevicePolicyManager.CODE_OK); |
| 2853 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 2854 | DevicePolicyManager.CODE_SPLIT_SYSTEM_USER_DEVICE_SYSTEM_USER); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2855 | assertCheckProvisioningPreCondition( |
| 2856 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2857 | DevicePolicyManager.CODE_OK); |
| 2858 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2859 | DevicePolicyManager.CODE_SYSTEM_USER); |
| 2860 | } |
| 2861 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2862 | private void setup_splitUser_firstBoot_primaryUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2863 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2864 | .thenReturn(true); |
| 2865 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2866 | when(mContext.userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
| 2867 | true)).thenReturn(true); |
| 2868 | setUserSetupCompleteForUser(false, DpmMockContext.CALLER_USER_HANDLE); |
| 2869 | |
| 2870 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2871 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2872 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2873 | public void testIsProvisioningAllowed_splitUser_firstBoot_primaryUser() throws Exception { |
| 2874 | setup_splitUser_firstBoot_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2875 | mContext.packageName = admin1.getPackageName(); |
| 2876 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2877 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); |
| 2878 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2879 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2880 | true); |
| 2881 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, true); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2884 | public void testCheckProvisioningPreCondition_splitUser_firstBoot_primaryUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2885 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2886 | setup_splitUser_firstBoot_primaryUser(); |
| 2887 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2888 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2889 | DevicePolicyManager.CODE_OK); |
| 2890 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2891 | DevicePolicyManager.CODE_OK); |
| 2892 | assertCheckProvisioningPreCondition( |
| 2893 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2894 | DevicePolicyManager.CODE_OK); |
| 2895 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2896 | DevicePolicyManager.CODE_OK); |
| 2897 | } |
| 2898 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2899 | private void setup_splitUser_afterDeviceSetup_primaryUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2900 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2901 | .thenReturn(true); |
| 2902 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2903 | when(mContext.userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
| 2904 | true)).thenReturn(true); |
| 2905 | setUserSetupCompleteForUser(true, DpmMockContext.CALLER_USER_HANDLE); |
| 2906 | |
| 2907 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2908 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2909 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2910 | public void testIsProvisioningAllowed_splitUser_afterDeviceSetup_primaryUser() |
| 2911 | throws Exception { |
| 2912 | setup_splitUser_afterDeviceSetup_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2913 | mContext.packageName = admin1.getPackageName(); |
| 2914 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2915 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2916 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2917 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2918 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2919 | true/* it's undefined behavior. Can be changed into false in the future */); |
| 2920 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2921 | false/* because user setup completed */); |
| 2922 | } |
| 2923 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2924 | public void testCheckProvisioningPreCondition_splitUser_afterDeviceSetup_primaryUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2925 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2926 | setup_splitUser_afterDeviceSetup_primaryUser(); |
| 2927 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2928 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, |
| 2929 | DevicePolicyManager.CODE_OK); |
| 2930 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2931 | DevicePolicyManager.CODE_OK); |
| 2932 | assertCheckProvisioningPreCondition( |
| 2933 | DevicePolicyManager.ACTION_PROVISION_MANAGED_SHAREABLE_DEVICE, |
| 2934 | DevicePolicyManager.CODE_OK); |
| 2935 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_USER, |
| 2936 | DevicePolicyManager.CODE_USER_SETUP_COMPLETED); |
| 2937 | } |
| 2938 | |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2939 | private void setup_provisionManagedProfileWithDeviceOwner_systemUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2940 | setDeviceOwner(); |
| 2941 | |
| 2942 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2943 | .thenReturn(true); |
| 2944 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2945 | when(mContext.userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) |
| 2946 | .thenReturn(false); |
| 2947 | setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); |
| 2948 | |
| 2949 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2950 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2951 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2952 | public void testIsProvisioningAllowed_provisionManagedProfileWithDeviceOwner_systemUser() |
| 2953 | throws Exception { |
| 2954 | setup_provisionManagedProfileWithDeviceOwner_systemUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2955 | mContext.packageName = admin1.getPackageName(); |
| 2956 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2957 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2958 | false /* can't provision managed profile on system user */); |
| 2959 | } |
| 2960 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2961 | public void testCheckProvisioningPreCondition_provisionManagedProfileWithDeviceOwner_systemUser() |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2962 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2963 | setup_provisionManagedProfileWithDeviceOwner_systemUser(); |
| 2964 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 2965 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2966 | DevicePolicyManager.CODE_SPLIT_SYSTEM_USER_DEVICE_SYSTEM_USER); |
| 2967 | } |
| 2968 | |
| 2969 | private void setup_provisionManagedProfileWithDeviceOwner_primaryUser() throws Exception { |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2970 | setDeviceOwner(); |
| 2971 | |
| 2972 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 2973 | .thenReturn(true); |
| 2974 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
| 2975 | when(mContext.userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
| 2976 | true)).thenReturn(true); |
| 2977 | setUserSetupCompleteForUser(false, DpmMockContext.CALLER_USER_HANDLE); |
| 2978 | |
| 2979 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2980 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2981 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2982 | public void testIsProvisioningAllowed_provisionManagedProfileWithDeviceOwner_primaryUser() |
| 2983 | throws Exception { |
| 2984 | setup_provisionManagedProfileWithDeviceOwner_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2985 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
| 2986 | mContext.packageName = admin1.getPackageName(); |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 2987 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); |
| 2988 | } |
| 2989 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2990 | public void testCheckProvisioningPreCondition_provisionManagedProfileWithDeviceOwner_primaryUser() |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 2991 | throws Exception { |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2992 | setup_provisionManagedProfileWithDeviceOwner_primaryUser(); |
| 2993 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 2994 | |
| 2995 | // COMP mode is allowed. |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 2996 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 2997 | DevicePolicyManager.CODE_OK); |
| 2998 | } |
| 2999 | |
| 3000 | private void setup_provisionManagedProfileCantRemoveUser_primaryUser() throws Exception { |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 3001 | setDeviceOwner(); |
| 3002 | |
| 3003 | when(mContext.ipackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)) |
| 3004 | .thenReturn(true); |
| 3005 | when(mContext.userManagerForMock.isSplitSystemUser()).thenReturn(true); |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 3006 | when(mContext.userManager.hasUserRestriction( |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 3007 | eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), |
| 3008 | eq(UserHandle.of(DpmMockContext.CALLER_USER_HANDLE)))) |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 3009 | .thenReturn(true); |
| 3010 | when(mContext.userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3011 | false /* we can't remove a managed profile */)).thenReturn(false); |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 3012 | when(mContext.userManager.canAddMoreManagedProfiles(DpmMockContext.CALLER_USER_HANDLE, |
| 3013 | true)).thenReturn(true); |
| 3014 | setUserSetupCompleteForUser(false, DpmMockContext.CALLER_USER_HANDLE); |
| 3015 | |
| 3016 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3017 | } |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 3018 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3019 | public void testIsProvisioningAllowed_provisionManagedProfileCantRemoveUser_primaryUser() |
| 3020 | throws Exception { |
| 3021 | setup_provisionManagedProfileCantRemoveUser_primaryUser(); |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 3022 | mContext.packageName = admin1.getPackageName(); |
| 3023 | setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); |
Nicolas Prevot | 56400a4 | 2016-11-10 12:57:54 +0000 | [diff] [blame] | 3024 | assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); |
| 3025 | } |
| 3026 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3027 | public void testCheckProvisioningPreCondition_provisionManagedProfileCantRemoveUser_primaryUser() |
| 3028 | throws Exception { |
| 3029 | setup_provisionManagedProfileCantRemoveUser_primaryUser(); |
| 3030 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 3031 | assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, |
| 3032 | DevicePolicyManager.CODE_CANNOT_ADD_MANAGED_PROFILE); |
| 3033 | } |
| 3034 | |
| 3035 | public void testCheckProvisioningPreCondition_permission() { |
| 3036 | // GIVEN the permission MANAGE_PROFILE_AND_DEVICE_OWNERS is not granted |
| 3037 | try { |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 3038 | dpm.checkProvisioningPreCondition( |
| 3039 | DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, "some.package"); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3040 | fail("Didn't throw SecurityException"); |
| 3041 | } catch (SecurityException expected) { |
| 3042 | } |
| 3043 | } |
| 3044 | |
Victor Chang | 3577ed2 | 2016-08-25 18:49:26 +0100 | [diff] [blame] | 3045 | public void testForceUpdateUserSetupComplete_permission() { |
| 3046 | // GIVEN the permission MANAGE_PROFILE_AND_DEVICE_OWNERS is not granted |
| 3047 | try { |
| 3048 | dpm.forceUpdateUserSetupComplete(); |
| 3049 | fail("Didn't throw SecurityException"); |
| 3050 | } catch (SecurityException expected) { |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | public void testForceUpdateUserSetupComplete_systemUser() { |
| 3055 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 3056 | // GIVEN calling from user 20 |
| 3057 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 3058 | try { |
| 3059 | dpm.forceUpdateUserSetupComplete(); |
| 3060 | fail("Didn't throw SecurityException"); |
| 3061 | } catch (SecurityException expected) { |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | public void testForceUpdateUserSetupComplete_userbuild() { |
| 3066 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 3067 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3068 | |
| 3069 | final int userId = UserHandle.USER_SYSTEM; |
| 3070 | // GIVEN userComplete is false in SettingsProvider |
| 3071 | setUserSetupCompleteForUser(false, userId); |
| 3072 | |
| 3073 | // GIVEN userComplete is true in DPM |
| 3074 | DevicePolicyManagerService.DevicePolicyData userData = |
| 3075 | new DevicePolicyManagerService.DevicePolicyData(userId); |
| 3076 | userData.mUserSetupComplete = true; |
| 3077 | dpms.mUserData.put(UserHandle.USER_SYSTEM, userData); |
| 3078 | |
| 3079 | // GIVEN it's user build |
| 3080 | mContext.buildMock.isDebuggable = false; |
| 3081 | |
| 3082 | assertTrue(dpms.hasUserSetupCompleted()); |
| 3083 | |
| 3084 | dpm.forceUpdateUserSetupComplete(); |
| 3085 | |
| 3086 | // THEN the state in dpms is not changed |
| 3087 | assertTrue(dpms.hasUserSetupCompleted()); |
| 3088 | } |
| 3089 | |
| 3090 | public void testForceUpdateUserSetupComplete_userDebugbuild() { |
| 3091 | mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); |
| 3092 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3093 | |
| 3094 | final int userId = UserHandle.USER_SYSTEM; |
| 3095 | // GIVEN userComplete is false in SettingsProvider |
| 3096 | setUserSetupCompleteForUser(false, userId); |
| 3097 | |
| 3098 | // GIVEN userComplete is true in DPM |
| 3099 | DevicePolicyManagerService.DevicePolicyData userData = |
| 3100 | new DevicePolicyManagerService.DevicePolicyData(userId); |
| 3101 | userData.mUserSetupComplete = true; |
| 3102 | dpms.mUserData.put(UserHandle.USER_SYSTEM, userData); |
| 3103 | |
| 3104 | // GIVEN it's userdebug build |
| 3105 | mContext.buildMock.isDebuggable = true; |
| 3106 | |
| 3107 | assertTrue(dpms.hasUserSetupCompleted()); |
| 3108 | |
| 3109 | dpm.forceUpdateUserSetupComplete(); |
| 3110 | |
| 3111 | // THEN the state in dpms is not changed |
| 3112 | assertFalse(dpms.hasUserSetupCompleted()); |
| 3113 | } |
| 3114 | |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3115 | private void clearDeviceOwner() throws Exception { |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3116 | final long ident = mContext.binder.clearCallingIdentity(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3117 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3118 | doReturn(DpmMockContext.CALLER_SYSTEM_USER_UID).when(mContext.packageManager) |
| 3119 | .getPackageUidAsUser(eq(admin1.getPackageName()), anyInt()); |
| 3120 | dpm.clearDeviceOwnerApp(admin1.getPackageName()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3121 | mContext.binder.restoreCallingIdentity(ident); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | public void testGetLastSecurityLogRetrievalTime() throws Exception { |
| 3125 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3126 | setupDeviceOwner(); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3127 | |
| 3128 | // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the |
| 3129 | // feature is disabled because there are non-affiliated secondary users. |
| 3130 | mContext.removeUser(DpmMockContext.CALLER_USER_HANDLE); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3131 | when(mContext.resources.getBoolean(R.bool.config_supportPreRebootSecurityLogs)) |
| 3132 | .thenReturn(true); |
| 3133 | |
| 3134 | // No logs were retrieved so far. |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3135 | assertEquals(-1, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3136 | |
| 3137 | // Enabling logging should not change the timestamp. |
| 3138 | dpm.setSecurityLoggingEnabled(admin1, true); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3139 | verify(mContext.settings) |
| 3140 | .securityLogSetLoggingEnabledProperty(true); |
| 3141 | when(mContext.settings.securityLogGetLoggingEnabledProperty()) |
| 3142 | .thenReturn(true); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3143 | assertEquals(-1, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3144 | |
| 3145 | // Retrieving the logs should update the timestamp. |
| 3146 | final long beforeRetrieval = System.currentTimeMillis(); |
| 3147 | dpm.retrieveSecurityLogs(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3148 | final long firstSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3149 | final long afterRetrieval = System.currentTimeMillis(); |
| 3150 | assertTrue(firstSecurityLogRetrievalTime >= beforeRetrieval); |
| 3151 | assertTrue(firstSecurityLogRetrievalTime <= afterRetrieval); |
| 3152 | |
| 3153 | // Retrieving the pre-boot logs should update the timestamp. |
| 3154 | Thread.sleep(2); |
| 3155 | dpm.retrievePreRebootSecurityLogs(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3156 | final long secondSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3157 | assertTrue(secondSecurityLogRetrievalTime > firstSecurityLogRetrievalTime); |
| 3158 | |
| 3159 | // Checking the timestamp again should not change it. |
| 3160 | Thread.sleep(2); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3161 | assertEquals(secondSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3162 | |
| 3163 | // Retrieving the logs again should update the timestamp. |
| 3164 | dpm.retrieveSecurityLogs(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3165 | final long thirdSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3166 | assertTrue(thirdSecurityLogRetrievalTime > secondSecurityLogRetrievalTime); |
| 3167 | |
| 3168 | // Disabling logging should not change the timestamp. |
| 3169 | Thread.sleep(2); |
| 3170 | dpm.setSecurityLoggingEnabled(admin1, false); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3171 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3172 | |
| 3173 | // Restarting the DPMS should not lose the timestamp. |
| 3174 | initializeDpms(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3175 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3176 | |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3177 | // Any uid holding MANAGE_USERS permission can retrieve the timestamp. |
| 3178 | mContext.binder.callingUid = 1234567; |
| 3179 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3180 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
| 3181 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3182 | |
| 3183 | // System can retrieve the timestamp. |
| 3184 | mContext.binder.clearCallingIdentity(); |
| 3185 | assertEquals(thirdSecurityLogRetrievalTime, dpm.getLastSecurityLogRetrievalTime()); |
| 3186 | |
| 3187 | // Removing the device owner should clear the timestamp. |
| 3188 | clearDeviceOwner(); |
| 3189 | assertEquals(-1, dpm.getLastSecurityLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | public void testGetLastBugReportRequestTime() throws Exception { |
| 3193 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3194 | setupDeviceOwner(); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3195 | |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3196 | mContext.packageName = admin1.getPackageName(); |
| 3197 | mContext.applicationInfo = new ApplicationInfo(); |
| 3198 | when(mContext.resources.getColor(eq(R.color.notification_action_list), anyObject())) |
| 3199 | .thenReturn(Color.WHITE); |
| 3200 | when(mContext.resources.getColor(eq(R.color.notification_material_background_color), |
| 3201 | anyObject())).thenReturn(Color.WHITE); |
| 3202 | |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3203 | // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the |
| 3204 | // feature is disabled because there are non-affiliated secondary users. |
| 3205 | mContext.removeUser(DpmMockContext.CALLER_USER_HANDLE); |
| 3206 | |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3207 | // No bug reports were requested so far. |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3208 | assertEquals(-1, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3209 | |
| 3210 | // Requesting a bug report should update the timestamp. |
| 3211 | final long beforeRequest = System.currentTimeMillis(); |
| 3212 | dpm.requestBugreport(admin1); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3213 | final long bugReportRequestTime = dpm.getLastBugReportRequestTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3214 | final long afterRequest = System.currentTimeMillis(); |
| 3215 | assertTrue(bugReportRequestTime >= beforeRequest); |
| 3216 | assertTrue(bugReportRequestTime <= afterRequest); |
| 3217 | |
| 3218 | // Checking the timestamp again should not change it. |
| 3219 | Thread.sleep(2); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3220 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3221 | |
| 3222 | // Restarting the DPMS should not lose the timestamp. |
| 3223 | initializeDpms(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3224 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3225 | |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3226 | // Any uid holding MANAGE_USERS permission can retrieve the timestamp. |
| 3227 | mContext.binder.callingUid = 1234567; |
| 3228 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3229 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
| 3230 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3231 | |
| 3232 | // System can retrieve the timestamp. |
| 3233 | mContext.binder.clearCallingIdentity(); |
| 3234 | assertEquals(bugReportRequestTime, dpm.getLastBugReportRequestTime()); |
| 3235 | |
| 3236 | // Removing the device owner should clear the timestamp. |
| 3237 | clearDeviceOwner(); |
| 3238 | assertEquals(-1, dpm.getLastBugReportRequestTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | public void testGetLastNetworkLogRetrievalTime() throws Exception { |
| 3242 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3243 | setupDeviceOwner(); |
Esteban Talavera | d36dd15 | 2016-12-15 08:51:45 +0000 | [diff] [blame] | 3244 | mContext.packageName = admin1.getPackageName(); |
| 3245 | mContext.applicationInfo = new ApplicationInfo(); |
| 3246 | when(mContext.resources.getColor(eq(R.color.notification_action_list), anyObject())) |
| 3247 | .thenReturn(Color.WHITE); |
| 3248 | when(mContext.resources.getColor(eq(R.color.notification_material_background_color), |
| 3249 | anyObject())).thenReturn(Color.WHITE); |
| 3250 | |
| 3251 | // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the |
| 3252 | // feature is disabled because there are non-affiliated secondary users. |
| 3253 | mContext.removeUser(DpmMockContext.CALLER_USER_HANDLE); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3254 | when(mContext.iipConnectivityMetrics.registerNetdEventCallback(anyObject())) |
| 3255 | .thenReturn(true); |
| 3256 | |
| 3257 | // No logs were retrieved so far. |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3258 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3259 | |
| 3260 | // Attempting to retrieve logs without enabling logging should not change the timestamp. |
| 3261 | dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3262 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3263 | |
| 3264 | // Enabling logging should not change the timestamp. |
| 3265 | dpm.setNetworkLoggingEnabled(admin1, true); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3266 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3267 | |
| 3268 | // Retrieving the logs should update the timestamp. |
| 3269 | final long beforeRetrieval = System.currentTimeMillis(); |
| 3270 | dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3271 | final long firstNetworkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3272 | final long afterRetrieval = System.currentTimeMillis(); |
| 3273 | assertTrue(firstNetworkLogRetrievalTime >= beforeRetrieval); |
| 3274 | assertTrue(firstNetworkLogRetrievalTime <= afterRetrieval); |
| 3275 | |
| 3276 | // Checking the timestamp again should not change it. |
| 3277 | Thread.sleep(2); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3278 | assertEquals(firstNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3279 | |
| 3280 | // Retrieving the logs again should update the timestamp. |
| 3281 | dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3282 | final long secondNetworkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3283 | assertTrue(secondNetworkLogRetrievalTime > firstNetworkLogRetrievalTime); |
| 3284 | |
| 3285 | // Disabling logging should not change the timestamp. |
| 3286 | Thread.sleep(2); |
| 3287 | dpm.setNetworkLoggingEnabled(admin1, false); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3288 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3289 | |
| 3290 | // Restarting the DPMS should not lose the timestamp. |
| 3291 | initializeDpms(); |
Bartosz Fabianowski | 8d76e72 | 2016-11-25 12:36:20 +0100 | [diff] [blame] | 3292 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
| 3293 | |
| 3294 | // Any uid holding MANAGE_USERS permission can retrieve the timestamp. |
| 3295 | mContext.binder.callingUid = 1234567; |
| 3296 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3297 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
| 3298 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3299 | |
| 3300 | // System can retrieve the timestamp. |
| 3301 | mContext.binder.clearCallingIdentity(); |
| 3302 | assertEquals(secondNetworkLogRetrievalTime, dpm.getLastNetworkLogRetrievalTime()); |
| 3303 | |
| 3304 | // Removing the device owner should clear the timestamp. |
| 3305 | clearDeviceOwner(); |
| 3306 | assertEquals(-1, dpm.getLastNetworkLogRetrievalTime()); |
Bartosz Fabianowski | b21b241 | 2016-11-17 04:53:33 +0100 | [diff] [blame] | 3307 | } |
| 3308 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3309 | public void testGetBindDeviceAdminTargetUsers() throws Exception { |
| 3310 | // Setup device owner. |
| 3311 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3312 | setupDeviceOwner(); |
| 3313 | |
| 3314 | // Only device owner is setup, the result list should be empty. |
| 3315 | List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3316 | MoreAsserts.assertEmpty(targetUsers); |
| 3317 | |
| 3318 | // Setup a managed profile managed by the same admin. |
| 3319 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3320 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); |
| 3321 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3322 | |
| 3323 | // Add a secondary user, it should never talk with. |
| 3324 | final int ANOTHER_USER_ID = 36; |
| 3325 | mContext.addUser(ANOTHER_USER_ID, 0); |
| 3326 | |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3327 | // Since the managed profile is not affiliated, they should not be allowed to talk to each |
| 3328 | // other. |
| 3329 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3330 | MoreAsserts.assertEmpty(targetUsers); |
| 3331 | |
| 3332 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3333 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3334 | MoreAsserts.assertEmpty(targetUsers); |
| 3335 | |
| 3336 | // Setting affiliation ids |
| 3337 | final List<String> userAffiliationIds = Arrays.asList("some.affiliation-id"); |
| 3338 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3339 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3340 | |
| 3341 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3342 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3343 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3344 | // Calling from device owner admin, the result list should just contain the managed |
| 3345 | // profile user id. |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3346 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3347 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3348 | MoreAsserts.assertContentsInAnyOrder(targetUsers, UserHandle.of(MANAGED_PROFILE_USER_ID)); |
| 3349 | |
| 3350 | // Calling from managed profile admin, the result list should just contain the system |
| 3351 | // user id. |
| 3352 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3353 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3354 | MoreAsserts.assertContentsInAnyOrder(targetUsers, UserHandle.SYSTEM); |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3355 | |
| 3356 | // Changing affiliation ids in one |
| 3357 | dpm.setAffiliationIds(admin1, Arrays.asList("some-different-affiliation-id")); |
| 3358 | |
| 3359 | // Since the managed profile is not affiliated any more, they should not be allowed to talk |
| 3360 | // to each other. |
| 3361 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3362 | MoreAsserts.assertEmpty(targetUsers); |
| 3363 | |
| 3364 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3365 | targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3366 | MoreAsserts.assertEmpty(targetUsers); |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | public void testGetBindDeviceAdminTargetUsers_differentPackage() throws Exception { |
| 3370 | // Setup a device owner. |
| 3371 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3372 | setupDeviceOwner(); |
| 3373 | |
| 3374 | // Set up a managed profile managed by different package. |
| 3375 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3376 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); |
| 3377 | final ComponentName adminDifferentPackage = |
| 3378 | new ComponentName("another.package", "whatever.class"); |
| 3379 | addManagedProfile(adminDifferentPackage, MANAGED_PROFILE_ADMIN_UID, admin2); |
| 3380 | |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3381 | // Setting affiliation ids |
| 3382 | final List<String> userAffiliationIds = Arrays.asList("some-affiliation-id"); |
| 3383 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3384 | |
| 3385 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3386 | dpm.setAffiliationIds(adminDifferentPackage, userAffiliationIds); |
| 3387 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3388 | // Calling from device owner admin, we should get zero bind device admin target users as |
| 3389 | // their packages are different. |
Nicolas Prevot | d5b0360 | 2016-12-06 13:32:22 +0000 | [diff] [blame] | 3390 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3391 | List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); |
| 3392 | MoreAsserts.assertEmpty(targetUsers); |
| 3393 | |
| 3394 | // Calling from managed profile admin, we should still get zero target users for the same |
| 3395 | // reason. |
| 3396 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3397 | targetUsers = dpm.getBindDeviceAdminTargetUsers(adminDifferentPackage); |
| 3398 | MoreAsserts.assertEmpty(targetUsers); |
| 3399 | } |
| 3400 | |
Esteban Talavera | bdcada9 | 2017-02-01 14:20:06 +0000 | [diff] [blame] | 3401 | public void testLockTaskPackagesAllowedForAffiliatedUsers() throws Exception { |
| 3402 | // Setup a device owner. |
| 3403 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3404 | setupDeviceOwner(); |
| 3405 | // Lock task packages are updated when loading user data. |
| 3406 | verify(mContext.iactivityManager) |
| 3407 | .updateLockTaskPackages(eq(UserHandle.USER_SYSTEM), eq(new String[0])); |
| 3408 | |
| 3409 | // Set up a managed profile managed by different package (package name shouldn't matter) |
| 3410 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3411 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); |
| 3412 | final ComponentName adminDifferentPackage = |
| 3413 | new ComponentName("another.package", "whatever.class"); |
| 3414 | addManagedProfile(adminDifferentPackage, MANAGED_PROFILE_ADMIN_UID, admin2); |
| 3415 | verify(mContext.iactivityManager) |
| 3416 | .updateLockTaskPackages(eq(MANAGED_PROFILE_USER_ID), eq(new String[0])); |
| 3417 | |
| 3418 | // The DO can still set lock task packages |
| 3419 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3420 | final String[] doPackages = {"doPackage1", "doPackage2"}; |
| 3421 | dpm.setLockTaskPackages(admin1, doPackages); |
| 3422 | MoreAsserts.assertEquals(doPackages, dpm.getLockTaskPackages(admin1)); |
| 3423 | assertTrue(dpm.isLockTaskPermitted("doPackage1")); |
| 3424 | assertFalse(dpm.isLockTaskPermitted("anotherPackage")); |
| 3425 | verify(mContext.iactivityManager) |
| 3426 | .updateLockTaskPackages(eq(UserHandle.USER_SYSTEM), eq(doPackages)); |
| 3427 | |
| 3428 | // Managed profile is unaffiliated - shouldn't be able to setLockTaskPackages. |
| 3429 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3430 | final String[] poPackages = {"poPackage1", "poPackage2"}; |
| 3431 | try { |
| 3432 | dpm.setLockTaskPackages(adminDifferentPackage, poPackages); |
| 3433 | fail("Didn't throw expected security exception."); |
| 3434 | } catch (SecurityException expected) { |
| 3435 | } |
| 3436 | try { |
| 3437 | dpm.getLockTaskPackages(adminDifferentPackage); |
| 3438 | fail("Didn't throw expected security exception."); |
| 3439 | } catch (SecurityException expected) { |
| 3440 | } |
| 3441 | assertFalse(dpm.isLockTaskPermitted("doPackage1")); |
| 3442 | |
| 3443 | // Setting same affiliation ids |
| 3444 | final List<String> userAffiliationIds = Arrays.asList("some-affiliation-id"); |
| 3445 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3446 | dpm.setAffiliationIds(admin1, userAffiliationIds); |
| 3447 | |
| 3448 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3449 | dpm.setAffiliationIds(adminDifferentPackage, userAffiliationIds); |
| 3450 | |
| 3451 | // Now the managed profile can set lock task packages. |
| 3452 | dpm.setLockTaskPackages(adminDifferentPackage, poPackages); |
| 3453 | MoreAsserts.assertEquals(poPackages, dpm.getLockTaskPackages(adminDifferentPackage)); |
| 3454 | assertTrue(dpm.isLockTaskPermitted("poPackage1")); |
| 3455 | assertFalse(dpm.isLockTaskPermitted("doPackage2")); |
| 3456 | verify(mContext.iactivityManager) |
| 3457 | .updateLockTaskPackages(eq(MANAGED_PROFILE_USER_ID), eq(poPackages)); |
| 3458 | |
| 3459 | // Unaffiliate the profile, lock task mode no longer available on the profile. |
| 3460 | dpm.setAffiliationIds(adminDifferentPackage, Collections.<String>emptyList()); |
| 3461 | assertFalse(dpm.isLockTaskPermitted("poPackage1")); |
| 3462 | // Lock task packages cleared when loading user data and when the user becomes unaffiliated. |
| 3463 | verify(mContext.iactivityManager, times(2)) |
| 3464 | .updateLockTaskPackages(eq(MANAGED_PROFILE_USER_ID), eq(new String[0])); |
| 3465 | |
| 3466 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3467 | assertTrue(dpm.isLockTaskPermitted("doPackage1")); |
| 3468 | } |
| 3469 | |
Bartosz Fabianowski | dd7f8da | 2016-11-30 11:09:22 +0100 | [diff] [blame] | 3470 | public void testIsDeviceManaged() throws Exception { |
| 3471 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3472 | setupDeviceOwner(); |
| 3473 | |
| 3474 | // The device owner itself, any uid holding MANAGE_USERS permission and the system can |
| 3475 | // find out that the device has a device owner. |
| 3476 | assertTrue(dpm.isDeviceManaged()); |
| 3477 | mContext.binder.callingUid = 1234567; |
| 3478 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3479 | assertTrue(dpm.isDeviceManaged()); |
| 3480 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3481 | mContext.binder.clearCallingIdentity(); |
| 3482 | assertTrue(dpm.isDeviceManaged()); |
| 3483 | |
| 3484 | clearDeviceOwner(); |
| 3485 | |
| 3486 | // Any uid holding MANAGE_USERS permission and the system can find out that the device does |
| 3487 | // not have a device owner. |
| 3488 | mContext.binder.callingUid = 1234567; |
| 3489 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3490 | assertFalse(dpm.isDeviceManaged()); |
| 3491 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3492 | mContext.binder.clearCallingIdentity(); |
| 3493 | assertFalse(dpm.isDeviceManaged()); |
| 3494 | } |
| 3495 | |
Bartosz Fabianowski | 365a3db | 2016-11-30 18:28:10 +0100 | [diff] [blame] | 3496 | public void testDeviceOwnerOrganizationName() throws Exception { |
| 3497 | mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; |
| 3498 | setupDeviceOwner(); |
| 3499 | |
| 3500 | dpm.setOrganizationName(admin1, "organization"); |
| 3501 | |
| 3502 | // Device owner can retrieve organization managing the device. |
| 3503 | assertEquals("organization", dpm.getDeviceOwnerOrganizationName()); |
| 3504 | |
| 3505 | // Any uid holding MANAGE_USERS permission can retrieve organization managing the device. |
| 3506 | mContext.binder.callingUid = 1234567; |
| 3507 | mContext.callerPermissions.add(permission.MANAGE_USERS); |
| 3508 | assertEquals("organization", dpm.getDeviceOwnerOrganizationName()); |
| 3509 | mContext.callerPermissions.remove(permission.MANAGE_USERS); |
| 3510 | |
| 3511 | // System can retrieve organization managing the device. |
| 3512 | mContext.binder.clearCallingIdentity(); |
| 3513 | assertEquals("organization", dpm.getDeviceOwnerOrganizationName()); |
| 3514 | |
| 3515 | // Removing the device owner clears the organization managing the device. |
| 3516 | clearDeviceOwner(); |
| 3517 | assertNull(dpm.getDeviceOwnerOrganizationName()); |
| 3518 | } |
| 3519 | |
Nicolas Prevot | c9c1b2f | 2017-01-12 10:52:50 +0000 | [diff] [blame] | 3520 | public void testWipeDataManagedProfile() throws Exception { |
| 3521 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3522 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3523 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3524 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3525 | |
| 3526 | // Even if the caller is the managed profile, the current user is the user 0 |
| 3527 | when(mContext.iactivityManager.getCurrentUser()) |
| 3528 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3529 | |
| 3530 | dpm.wipeData(0); |
| 3531 | verify(mContext.userManagerInternal).removeUserEvenWhenDisallowed( |
| 3532 | MANAGED_PROFILE_USER_ID); |
| 3533 | } |
| 3534 | |
| 3535 | public void testWipeDataManagedProfileDisallowed() throws Exception { |
| 3536 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3537 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3538 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3539 | |
| 3540 | // Even if the caller is the managed profile, the current user is the user 0 |
| 3541 | when(mContext.iactivityManager.getCurrentUser()) |
| 3542 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3543 | |
| 3544 | when(mContext.userManager.getUserRestrictionSource( |
| 3545 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
| 3546 | UserHandle.of(MANAGED_PROFILE_USER_ID))) |
| 3547 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3548 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3549 | try { |
| 3550 | // The PO is not allowed to remove the profile if the user restriction was set on the |
| 3551 | // profile by the system |
| 3552 | dpm.wipeData(0); |
| 3553 | fail("SecurityException not thrown"); |
| 3554 | } catch (SecurityException expected) { |
| 3555 | } |
| 3556 | } |
| 3557 | |
Esteban Talavera | b88f42b | 2017-01-24 16:47:16 +0000 | [diff] [blame] | 3558 | public void testWipeDataDeviceOwner() throws Exception { |
| 3559 | setDeviceOwner(); |
| 3560 | when(mContext.userManager.getUserRestrictionSource( |
| 3561 | UserManager.DISALLOW_FACTORY_RESET, |
| 3562 | UserHandle.SYSTEM)) |
| 3563 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 3564 | |
| 3565 | dpm.wipeData(0); |
| 3566 | verify(mContext.recoverySystem).rebootWipeUserData( |
| 3567 | /*shutdown=*/ eq(false), anyString(), /*force=*/ eq(true)); |
| 3568 | } |
| 3569 | |
| 3570 | public void testWipeDataDeviceOwnerDisallowed() throws Exception { |
| 3571 | setDeviceOwner(); |
| 3572 | when(mContext.userManager.getUserRestrictionSource( |
| 3573 | UserManager.DISALLOW_FACTORY_RESET, |
| 3574 | UserHandle.SYSTEM)) |
| 3575 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3576 | try { |
| 3577 | // The DO is not allowed to wipe the device if the user restriction was set |
| 3578 | // by the system |
| 3579 | dpm.wipeData(0); |
| 3580 | fail("SecurityException not thrown"); |
| 3581 | } catch (SecurityException expected) { |
| 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | public void testMaximumFailedPasswordAttemptsReachedManagedProfile() throws Exception { |
| 3586 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3587 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3588 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3589 | |
| 3590 | // Even if the caller is the managed profile, the current user is the user 0 |
| 3591 | when(mContext.iactivityManager.getCurrentUser()) |
| 3592 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3593 | |
| 3594 | when(mContext.userManager.getUserRestrictionSource( |
| 3595 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
| 3596 | UserHandle.of(MANAGED_PROFILE_USER_ID))) |
| 3597 | .thenReturn(UserManager.RESTRICTION_SOURCE_PROFILE_OWNER); |
| 3598 | |
| 3599 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3600 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3601 | |
| 3602 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3603 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3604 | // Failed password attempts on the parent user are taken into account, as there isn't a |
| 3605 | // separate work challenge. |
| 3606 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3607 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3608 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3609 | |
| 3610 | // The profile should be wiped even if DISALLOW_REMOVE_MANAGED_PROFILE is enabled, because |
| 3611 | // both the user restriction and the policy were set by the PO. |
| 3612 | verify(mContext.userManagerInternal).removeUserEvenWhenDisallowed( |
| 3613 | MANAGED_PROFILE_USER_ID); |
| 3614 | verifyZeroInteractions(mContext.recoverySystem); |
| 3615 | } |
| 3616 | |
| 3617 | public void testMaximumFailedPasswordAttemptsReachedManagedProfileDisallowed() |
| 3618 | throws Exception { |
| 3619 | final int MANAGED_PROFILE_USER_ID = 15; |
| 3620 | final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); |
| 3621 | addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); |
| 3622 | |
| 3623 | // Even if the caller is the managed profile, the current user is the user 0 |
| 3624 | when(mContext.iactivityManager.getCurrentUser()) |
| 3625 | .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); |
| 3626 | |
| 3627 | when(mContext.userManager.getUserRestrictionSource( |
| 3628 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
| 3629 | UserHandle.of(MANAGED_PROFILE_USER_ID))) |
| 3630 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3631 | |
| 3632 | mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; |
| 3633 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3634 | |
| 3635 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3636 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3637 | // Failed password attempts on the parent user are taken into account, as there isn't a |
| 3638 | // separate work challenge. |
| 3639 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3640 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3641 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3642 | |
| 3643 | // DISALLOW_REMOVE_MANAGED_PROFILE was set by the system, not the PO, so the profile is |
| 3644 | // not wiped. |
| 3645 | verify(mContext.userManagerInternal, never()) |
| 3646 | .removeUserEvenWhenDisallowed(anyInt()); |
| 3647 | verifyZeroInteractions(mContext.recoverySystem); |
| 3648 | } |
| 3649 | |
| 3650 | public void testMaximumFailedPasswordAttemptsReachedDeviceOwner() throws Exception { |
| 3651 | setDeviceOwner(); |
| 3652 | when(mContext.userManager.getUserRestrictionSource( |
| 3653 | UserManager.DISALLOW_FACTORY_RESET, |
| 3654 | UserHandle.SYSTEM)) |
| 3655 | .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); |
| 3656 | |
| 3657 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3658 | |
| 3659 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3660 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3661 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3662 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3663 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3664 | |
| 3665 | // The device should be wiped even if DISALLOW_FACTORY_RESET is enabled, because both the |
| 3666 | // user restriction and the policy were set by the DO. |
| 3667 | verify(mContext.recoverySystem).rebootWipeUserData( |
| 3668 | /*shutdown=*/ eq(false), anyString(), /*force=*/ eq(true)); |
| 3669 | } |
| 3670 | |
| 3671 | public void testMaximumFailedPasswordAttemptsReachedDeviceOwnerDisallowed() throws Exception { |
| 3672 | setDeviceOwner(); |
| 3673 | when(mContext.userManager.getUserRestrictionSource( |
| 3674 | UserManager.DISALLOW_FACTORY_RESET, |
| 3675 | UserHandle.SYSTEM)) |
| 3676 | .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); |
| 3677 | |
| 3678 | dpm.setMaximumFailedPasswordsForWipe(admin1, 3); |
| 3679 | |
| 3680 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
| 3681 | mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); |
| 3682 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3683 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3684 | dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); |
| 3685 | |
| 3686 | // DISALLOW_FACTORY_RESET was set by the system, not the DO, so the device is not wiped. |
| 3687 | verifyZeroInteractions(mContext.recoverySystem); |
| 3688 | verify(mContext.userManagerInternal, never()) |
| 3689 | .removeUserEvenWhenDisallowed(anyInt()); |
| 3690 | } |
| 3691 | |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3692 | public void testGetPermissionGrantState() throws Exception { |
| 3693 | final String permission = "some.permission"; |
| 3694 | final String app1 = "com.example.app1"; |
| 3695 | final String app2 = "com.example.app2"; |
| 3696 | |
| 3697 | when(mContext.ipackageManager.checkPermission(eq(permission), eq(app1), anyInt())) |
| 3698 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 3699 | doReturn(PackageManager.FLAG_PERMISSION_POLICY_FIXED).when(mContext.packageManager) |
| 3700 | .getPermissionFlags(permission, app1, UserHandle.SYSTEM); |
| 3701 | when(mContext.packageManager.getPermissionFlags(permission, app1, |
| 3702 | UserHandle.of(DpmMockContext.CALLER_USER_HANDLE))) |
| 3703 | .thenReturn(PackageManager.FLAG_PERMISSION_POLICY_FIXED); |
| 3704 | when(mContext.ipackageManager.checkPermission(eq(permission), eq(app2), anyInt())) |
| 3705 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 3706 | doReturn(0).when(mContext.packageManager).getPermissionFlags(permission, app2, |
| 3707 | UserHandle.SYSTEM); |
| 3708 | when(mContext.packageManager.getPermissionFlags(permission, app2, |
| 3709 | UserHandle.of(DpmMockContext.CALLER_USER_HANDLE))).thenReturn(0); |
| 3710 | |
| 3711 | // System can retrieve permission grant state. |
| 3712 | mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3713 | mContext.packageName = "com.example.system"; |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3714 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED, |
| 3715 | dpm.getPermissionGrantState(null, app1, permission)); |
| 3716 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT, |
| 3717 | dpm.getPermissionGrantState(null, app2, permission)); |
| 3718 | |
| 3719 | // A regular app cannot retrieve permission grant state. |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3720 | mContext.binder.callingUid = setupPackageInPackageManager(app1, 1); |
| 3721 | mContext.packageName = app1; |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3722 | try { |
| 3723 | dpm.getPermissionGrantState(null, app1, permission); |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3724 | fail("Didn't throw SecurityException"); |
| 3725 | } catch (SecurityException expected) { |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | // Profile owner can retrieve permission grant state. |
Bartosz Fabianowski | 2dbf9ae | 2017-01-27 14:47:17 +0100 | [diff] [blame] | 3729 | mContext.binder.callingUid = DpmMockContext.CALLER_UID; |
| 3730 | mContext.packageName = admin1.getPackageName(); |
Bartosz Fabianowski | 4b0624f | 2017-01-16 11:47:28 +0100 | [diff] [blame] | 3731 | setAsProfileOwner(admin1); |
| 3732 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED, |
| 3733 | dpm.getPermissionGrantState(admin1, app1, permission)); |
| 3734 | assertEquals(DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT, |
| 3735 | dpm.getPermissionGrantState(admin1, app2, permission)); |
| 3736 | } |
| 3737 | |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 3738 | private void setUserSetupCompleteForUser(boolean isUserSetupComplete, int userhandle) { |
| 3739 | when(mContext.settings.settingsSecureGetIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 0, |
| 3740 | userhandle)).thenReturn(isUserSetupComplete ? 1 : 0); |
| 3741 | dpms.notifyChangeToContentObserver( |
| 3742 | Settings.Secure.getUriFor(Settings.Secure.USER_SETUP_COMPLETE), userhandle); |
| 3743 | } |
| 3744 | |
| 3745 | private void assertProvisioningAllowed(String action, boolean expected) { |
| 3746 | assertEquals("isProvisioningAllowed(" + action + ") returning unexpected result", expected, |
| 3747 | dpm.isProvisioningAllowed(action)); |
| 3748 | } |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3749 | |
Nicolas Prevot | 45d2907 | 2017-01-18 16:11:19 +0000 | [diff] [blame] | 3750 | private void assertProvisioningAllowed(String action, boolean expected, String packageName, |
| 3751 | int uid) { |
| 3752 | String previousPackageName = mContext.packageName; |
| 3753 | int previousUid = mMockContext.binder.callingUid; |
| 3754 | |
| 3755 | // Call assertProvisioningAllowed with the packageName / uid passed as arguments. |
| 3756 | mContext.packageName = packageName; |
| 3757 | mMockContext.binder.callingUid = uid; |
| 3758 | assertProvisioningAllowed(action, expected); |
| 3759 | |
| 3760 | // Set the previous package name / calling uid to go back to the initial state. |
| 3761 | mContext.packageName = previousPackageName; |
| 3762 | mMockContext.binder.callingUid = previousUid; |
| 3763 | } |
| 3764 | |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3765 | private void assertCheckProvisioningPreCondition(String action, int provisioningCondition) { |
Esteban Talavera | 0157686 | 2016-12-15 11:16:44 +0000 | [diff] [blame] | 3766 | assertCheckProvisioningPreCondition(action, admin1.getPackageName(), provisioningCondition); |
| 3767 | } |
| 3768 | |
| 3769 | private void assertCheckProvisioningPreCondition( |
| 3770 | String action, String packageName, int provisioningCondition) { |
| 3771 | assertEquals("checkProvisioningPreCondition(" |
| 3772 | + action + ", " + packageName + ") returning unexpected result", |
| 3773 | provisioningCondition, dpm.checkProvisioningPreCondition(action, packageName)); |
Mahaver Chopra | 849fd6f | 2016-11-03 20:28:10 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
Tony Mak | 2f26b79 | 2016-11-28 17:54:51 +0000 | [diff] [blame] | 3776 | /** |
| 3777 | * Setup a managed profile with the specified admin and its uid. |
| 3778 | * @param admin ComponentName that's visible to the test code, which doesn't have to exist. |
| 3779 | * @param adminUid uid of the admin package. |
| 3780 | * @param copyFromAdmin package information for {@code admin} will be built based on this |
| 3781 | * component's information. |
| 3782 | */ |
| 3783 | private void addManagedProfile( |
| 3784 | ComponentName admin, int adminUid, ComponentName copyFromAdmin) throws Exception { |
| 3785 | final int userId = UserHandle.getUserId(adminUid); |
| 3786 | mContext.addUser(userId, UserInfo.FLAG_MANAGED_PROFILE, UserHandle.USER_SYSTEM); |
| 3787 | mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); |
| 3788 | setUpPackageManagerForFakeAdmin(admin, adminUid, copyFromAdmin); |
| 3789 | dpm.setActiveAdmin(admin, false, userId); |
| 3790 | assertTrue(dpm.setProfileOwner(admin, null, userId)); |
| 3791 | mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); |
| 3792 | } |
Robin Lee | 7f5c91c | 2017-02-08 21:27:02 +0000 | [diff] [blame^] | 3793 | |
| 3794 | /** |
| 3795 | * Convert String[] to ParceledListSlice<ParcelableString>. |
| 3796 | * <p> |
| 3797 | * TODO: This shouldn't be necessary. If ParcelableString does need to exist, it also needs |
| 3798 | * a real constructor. |
| 3799 | */ |
| 3800 | private static ParceledListSlice<ParcelableString> asSlice(String[] s) { |
| 3801 | List<ParcelableString> list = new ArrayList<>(s.length); |
| 3802 | for (int i = 0; i < s.length; i++) { |
| 3803 | ParcelableString item = new ParcelableString(); |
| 3804 | item.string = s[i]; |
| 3805 | list.add(i, item); |
| 3806 | } |
| 3807 | return new ParceledListSlice<ParcelableString>(list); |
| 3808 | } |
Victor Chang | 3e794af | 2016-03-04 13:48:17 +0000 | [diff] [blame] | 3809 | } |