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 */ 16 package com.android.server.devicepolicy; 17 18 import static android.app.AppOpsManager.MODE_ALLOWED; 19 import static android.app.AppOpsManager.MODE_DEFAULT; 20 import static android.app.AppOpsManager.OP_ACTIVATE_VPN; 21 import static android.app.Notification.EXTRA_TITLE; 22 import static android.app.admin.DevicePolicyManager.ACTION_CHECK_POLICY_COMPLIANCE; 23 import static android.app.admin.DevicePolicyManager.DELEGATION_APP_RESTRICTIONS; 24 import static android.app.admin.DevicePolicyManager.DELEGATION_CERT_INSTALL; 25 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT; 26 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED; 27 import static android.app.admin.DevicePolicyManager.ID_TYPE_BASE_INFO; 28 import static android.app.admin.DevicePolicyManager.ID_TYPE_IMEI; 29 import static android.app.admin.DevicePolicyManager.ID_TYPE_MEID; 30 import static android.app.admin.DevicePolicyManager.ID_TYPE_SERIAL; 31 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_BLOCK_ACTIVITY_START_IN_TASK; 32 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_GLOBAL_ACTIONS; 33 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_HOME; 34 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_KEYGUARD; 35 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_NONE; 36 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS; 37 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW; 38 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_SYSTEM_INFO; 39 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_HIGH; 40 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_LOW; 41 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_MEDIUM; 42 import static android.app.admin.DevicePolicyManager.PASSWORD_COMPLEXITY_NONE; 43 import static android.app.admin.DevicePolicyManager.PRIVATE_DNS_SET_NO_ERROR; 44 import static android.app.admin.DevicePolicyManager.WIFI_SECURITY_ENTERPRISE_192; 45 import static android.app.admin.DevicePolicyManager.WIFI_SECURITY_ENTERPRISE_EAP; 46 import static android.app.admin.DevicePolicyManager.WIFI_SECURITY_OPEN; 47 import static android.app.admin.DevicePolicyManager.WIFI_SECURITY_PERSONAL; 48 import static android.app.admin.DevicePolicyManager.WIPE_EUICC; 49 import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_DIRECT_BOOT_AWARE; 50 import static android.location.LocationManager.FUSED_PROVIDER; 51 import static android.location.LocationManager.GPS_PROVIDER; 52 import static android.location.LocationManager.NETWORK_PROVIDER; 53 import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT; 54 import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE; 55 import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK; 56 import static android.net.InetAddresses.parseNumericAddress; 57 import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; 58 59 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_NONE; 60 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD; 61 import static com.android.internal.widget.LockPatternUtils.EscrowTokenStateChangeCallback; 62 import static com.android.server.SystemTimeZone.TIME_ZONE_CONFIDENCE_HIGH; 63 import static com.android.server.devicepolicy.DevicePolicyManagerService.ACTION_PROFILE_OFF_DEADLINE; 64 import static com.android.server.devicepolicy.DevicePolicyManagerService.ACTION_TURN_PROFILE_ON_NOTIFICATION; 65 import static com.android.server.devicepolicy.DpmMockContext.CALLER_USER_HANDLE; 66 import static com.android.server.testutils.TestUtils.assertExpectException; 67 68 import static com.google.common.truth.Truth.assertThat; 69 import static com.google.common.truth.Truth.assertWithMessage; 70 71 import static org.junit.Assert.fail; 72 import static org.junit.Assume.assumeTrue; 73 import static org.mockito.Matchers.any; 74 import static org.mockito.Matchers.anyBoolean; 75 import static org.mockito.Matchers.anyInt; 76 import static org.mockito.Matchers.anyLong; 77 import static org.mockito.Matchers.anyObject; 78 import static org.mockito.Matchers.anyString; 79 import static org.mockito.Matchers.eq; 80 import static org.mockito.Matchers.isNull; 81 import static org.mockito.Mockito.clearInvocations; 82 import static org.mockito.Mockito.doAnswer; 83 import static org.mockito.Mockito.doReturn; 84 import static org.mockito.Mockito.never; 85 import static org.mockito.Mockito.nullable; 86 import static org.mockito.Mockito.reset; 87 import static org.mockito.Mockito.timeout; 88 import static org.mockito.Mockito.times; 89 import static org.mockito.Mockito.verify; 90 import static org.mockito.Mockito.verifyNoMoreInteractions; 91 import static org.mockito.Mockito.verifyZeroInteractions; 92 import static org.mockito.Mockito.when; 93 import static org.mockito.hamcrest.MockitoHamcrest.argThat; 94 import static org.testng.Assert.assertThrows; 95 96 import static java.util.Collections.emptyList; 97 98 import android.Manifest.permission; 99 import android.app.Activity; 100 import android.app.AppOpsManager; 101 import android.app.Notification; 102 import android.app.PendingIntent; 103 import android.app.admin.DeviceAdminReceiver; 104 import android.app.admin.DevicePolicyManager; 105 import android.app.admin.DevicePolicyManagerInternal; 106 import android.app.admin.DevicePolicyManagerLiteInternal; 107 import android.app.admin.FactoryResetProtectionPolicy; 108 import android.app.admin.PasswordMetrics; 109 import android.app.admin.PreferentialNetworkServiceConfig; 110 import android.app.admin.SystemUpdatePolicy; 111 import android.app.admin.WifiSsidPolicy; 112 import android.content.BroadcastReceiver; 113 import android.content.ComponentName; 114 import android.content.Intent; 115 import android.content.IntentFilter; 116 import android.content.pm.ApplicationInfo; 117 import android.content.pm.PackageInfo; 118 import android.content.pm.PackageManager; 119 import android.content.pm.ResolveInfo; 120 import android.content.pm.StringParceledListSlice; 121 import android.content.pm.UserInfo; 122 import android.graphics.Color; 123 import android.hardware.usb.UsbManager; 124 import android.net.ProfileNetworkPreference; 125 import android.net.Uri; 126 import android.net.wifi.WifiSsid; 127 import android.os.Build; 128 import android.os.Build.VERSION_CODES; 129 import android.os.Bundle; 130 import android.os.IpcDataCache; 131 import android.os.PersistableBundle; 132 import android.os.Process; 133 import android.os.UserHandle; 134 import android.os.UserManager; 135 import android.platform.test.annotations.Presubmit; 136 import android.provider.DeviceConfig; 137 import android.provider.Settings; 138 import android.security.KeyChain; 139 import android.security.keystore.AttestationUtils; 140 import android.telephony.TelephonyManager; 141 import android.telephony.data.ApnSetting; 142 import android.test.MoreAsserts; 143 import android.util.ArraySet; 144 import android.util.Log; 145 import android.util.Pair; 146 147 import androidx.test.filters.FlakyTest; 148 import androidx.test.filters.SmallTest; 149 150 import com.android.internal.R; 151 import com.android.internal.messages.nano.SystemMessageProto; 152 import com.android.internal.widget.LockPatternUtils; 153 import com.android.internal.widget.LockscreenCredential; 154 import com.android.server.LocalServices; 155 import com.android.server.SystemService; 156 import com.android.server.devicepolicy.DevicePolicyManagerService.RestrictionsListener; 157 import com.android.server.pm.RestrictionsSet; 158 import com.android.server.pm.UserManagerInternal; 159 import com.android.server.pm.UserRestrictionsUtils; 160 161 import org.hamcrest.BaseMatcher; 162 import org.hamcrest.Description; 163 import org.hamcrest.Matcher; 164 import org.junit.After; 165 import org.junit.Before; 166 import org.junit.Ignore; 167 import org.junit.Test; 168 import org.mockito.Mockito; 169 import org.mockito.internal.util.collections.Sets; 170 import org.mockito.stubbing.Answer; 171 172 import java.io.File; 173 import java.net.InetSocketAddress; 174 import java.net.Proxy; 175 import java.nio.charset.StandardCharsets; 176 import java.util.ArrayList; 177 import java.util.Arrays; 178 import java.util.Collections; 179 import java.util.HashMap; 180 import java.util.List; 181 import java.util.Map; 182 import java.util.Set; 183 import java.util.concurrent.TimeUnit; 184 185 /** 186 * Tests for DevicePolicyManager( and DevicePolicyManagerService). 187 * 188 * <p>Run this test with: 189 * 190 * {@code atest FrameworksServicesTests:com.android.server.devicepolicy.DevicePolicyManagerTest} 191 * 192 */ 193 @SmallTest 194 @Presubmit 195 public class DevicePolicyManagerTest extends DpmTestBase { 196 197 private static final String TAG = DevicePolicyManagerTest.class.getSimpleName(); 198 199 private static final List<String> OWNER_SETUP_PERMISSIONS = Arrays.asList( 200 permission.MANAGE_DEVICE_ADMINS, permission.MANAGE_PROFILE_AND_DEVICE_OWNERS, 201 permission.MANAGE_USERS, permission.INTERACT_ACROSS_USERS_FULL); 202 public static final String NOT_DEVICE_OWNER_MSG = "does not own the device"; 203 public static final String NOT_PROFILE_OWNER_MSG = "does not own the profile"; 204 public static final String NOT_ORG_OWNED_PROFILE_OWNER_MSG = 205 "not the profile owner on organization-owned device"; 206 public static final String INVALID_CALLING_IDENTITY_MSG = "Calling identity is not authorized"; 207 public static final String ONGOING_CALL_MSG = "ongoing call on the device"; 208 209 // TODO replace all instances of this with explicit {@link #mServiceContext}. 210 @Deprecated 211 private DpmMockContext mContext; 212 213 private DpmMockContext mServiceContext; 214 private DpmMockContext mAdmin1Context; 215 public DevicePolicyManager dpm; 216 public DevicePolicyManager parentDpm; 217 public DevicePolicyManagerServiceTestable dpms; 218 219 private boolean mIsAutomotive; 220 221 /* 222 * The CA cert below is the content of cacert.pem as generated by: 223 * 224 * openssl req -new -x509 -days 3650 -extensions v3_ca -keyout cakey.pem -out cacert.pem 225 */ 226 private static final String TEST_CA = 227 "-----BEGIN CERTIFICATE-----\n" + 228 "MIIDXTCCAkWgAwIBAgIJAK9Tl/F9V8kSMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n" + 229 "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n" + 230 "aWRnaXRzIFB0eSBMdGQwHhcNMTUwMzA2MTczMjExWhcNMjUwMzAzMTczMjExWjBF\n" + 231 "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n" + 232 "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n" + 233 "CgKCAQEAvItOutsE75WBTgTyNAHt4JXQ3JoseaGqcC3WQij6vhrleWi5KJ0jh1/M\n" + 234 "Rpry7Fajtwwb4t8VZa0NuM2h2YALv52w1xivql88zce/HU1y7XzbXhxis9o6SCI+\n" + 235 "oVQSbPeXRgBPppFzBEh3ZqYTVhAqw451XhwdA4Aqs3wts7ddjwlUzyMdU44osCUg\n" + 236 "kVg7lfPf9sTm5IoHVcfLSCWH5n6Nr9sH3o2ksyTwxuOAvsN11F/a0mmUoPciYPp+\n" + 237 "q7DzQzdi7akRG601DZ4YVOwo6UITGvDyuAAdxl5isovUXqe6Jmz2/myTSpAKxGFs\n" + 238 "jk9oRoG6WXWB1kni490GIPjJ1OceyQIDAQABo1AwTjAdBgNVHQ4EFgQUH1QIlPKL\n" + 239 "p2OQ/AoLOjKvBW4zK3AwHwYDVR0jBBgwFoAUH1QIlPKLp2OQ/AoLOjKvBW4zK3Aw\n" + 240 "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAcMi4voMMJHeQLjtq8Oky\n" + 241 "Azpyk8moDwgCd4llcGj7izOkIIFqq/lyqKdtykVKUWz2bSHO5cLrtaOCiBWVlaCV\n" + 242 "DYAnnVLM8aqaA6hJDIfaGs4zmwz0dY8hVMFCuCBiLWuPfiYtbEmjHGSmpQTG6Qxn\n" + 243 "ZJlaK5CZyt5pgh5EdNdvQmDEbKGmu0wpCq9qjZImwdyAul1t/B0DrsWApZMgZpeI\n" + 244 "d2od0VBrCICB1K4p+C51D93xyQiva7xQcCne+TAnGNy9+gjQ/MyR8MRpwRLv5ikD\n" + 245 "u0anJCN8pXo6IMglfMAsoton1J6o5/ae5uhC6caQU8bNUsCK570gpNfjkzo6rbP0\n" + 246 "wQ==\n" + 247 "-----END CERTIFICATE-----\n"; 248 249 // Constants for testing setManagedProfileMaximumTimeOff: 250 // Profile maximum time off value 251 private static final long PROFILE_OFF_TIMEOUT = TimeUnit.DAYS.toMillis(5); 252 // Synthetic time at the beginning of test. 253 private static final long PROFILE_OFF_START = 1; 254 // Time when warning notification should be posted, 255 private static final long PROFILE_OFF_WARNING_TIME = 256 PROFILE_OFF_START + PROFILE_OFF_TIMEOUT - TimeUnit.DAYS.toMillis(1); 257 // Time when the apps should be suspended 258 private static final long PROFILE_OFF_DEADLINE = PROFILE_OFF_START + PROFILE_OFF_TIMEOUT; 259 // Notification title and text for setManagedProfileMaximumTimeOff tests: 260 private static final String PROFILE_OFF_SUSPENSION_TITLE = "suspension_title"; 261 private static final String PROFILE_OFF_SUSPENSION_TEXT = "suspension_text"; 262 private static final String PROFILE_OFF_SUSPENSION_SOON_TEXT = "suspension_tomorrow_text"; 263 private static final String FLAG_ENABLE_WORK_PROFILE_TELEPHONY = 264 "enable_work_profile_telephony"; 265 266 @Before setUp()267 public void setUp() throws Exception { 268 269 // Disable caches in this test process. This must happen early, since some of the 270 // following initialization steps invalidate caches. 271 IpcDataCache.disableForTestMode(); 272 273 mContext = getContext(); 274 mServiceContext = mContext; 275 mServiceContext.binder.callingUid = DpmMockContext.CALLER_UID; 276 when(getServices().userManagerInternal.getUserIds()).thenReturn(new int[]{0}); 277 when(getServices().packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) 278 .thenReturn(true); 279 doReturn(Collections.singletonList(new ResolveInfo())) 280 .when(getServices().packageManager).queryBroadcastReceiversAsUser( 281 any(Intent.class), 282 anyInt(), 283 any(UserHandle.class)); 284 285 // Make createContextAsUser to work. 286 mContext.packageName = "com.android.frameworks.servicestests"; 287 getServices().addPackageContext(UserHandle.of(0), mContext); 288 getServices().addPackageContext(UserHandle.of(CALLER_USER_HANDLE), mContext); 289 290 // By default, pretend all users are running and unlocked. 291 when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true); 292 293 initializeDpms(); 294 295 Mockito.reset(getServices().usageStatsManagerInternal); 296 Mockito.reset(getServices().networkPolicyManagerInternal); 297 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); 298 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); 299 setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_UID); 300 setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID); 301 302 mAdmin1Context = new DpmMockContext(getServices(), mRealTestContext); 303 mAdmin1Context.packageName = admin1.getPackageName(); 304 mAdmin1Context.applicationInfo = new ApplicationInfo(); 305 mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID; 306 307 setUpUserManager(); 308 309 when(getServices().lockPatternUtils.hasSecureLockScreen()).thenReturn(true); 310 311 mIsAutomotive = mContext.getPackageManager() 312 .hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 313 314 final String TEST_STRING = "{count, plural,\n" 315 + " =1 {Test for exactly 1 cert out of 4}\n" 316 + " other {Test for exactly # certs out of 4}\n" 317 + "}"; 318 doReturn(TEST_STRING) 319 .when(mContext.resources) 320 .getString(R.string.ssl_ca_cert_warning); 321 } 322 getMockTransferMetadataManager()323 private TransferOwnershipMetadataManager getMockTransferMetadataManager() { 324 return dpms.mTransferOwnershipMetadataManager; 325 } 326 327 @After tearDown()328 public void tearDown() throws Exception { 329 flushTasks(dpms); 330 getMockTransferMetadataManager().deleteMetadataFile(); 331 } 332 initializeDpms()333 private void initializeDpms() { 334 // Need clearCallingIdentity() to pass permission checks. 335 final long ident = mContext.binder.clearCallingIdentity(); 336 337 dpms = new DevicePolicyManagerServiceTestable(getServices(), mContext); 338 dpms.systemReady(SystemService.PHASE_LOCK_SETTINGS_READY); 339 dpms.systemReady(SystemService.PHASE_BOOT_COMPLETED); 340 341 dpm = new DevicePolicyManagerTestable(mContext, dpms); 342 343 parentDpm = new DevicePolicyManagerTestable(mServiceContext, dpms, 344 /* parentInstance= */true); 345 346 mContext.binder.restoreCallingIdentity(ident); 347 } 348 setUpUserManager()349 private void setUpUserManager() { 350 // Emulate UserManager.set/getApplicationRestriction(). 351 final Map<Pair<String, UserHandle>, Bundle> appRestrictions = new HashMap<>(); 352 353 // UM.setApplicationRestrictions() will save to appRestrictions. 354 doAnswer((Answer<Void>) invocation -> { 355 String pkg = (String) invocation.getArguments()[0]; 356 Bundle bundle = (Bundle) invocation.getArguments()[1]; 357 UserHandle user = (UserHandle) invocation.getArguments()[2]; 358 359 appRestrictions.put(Pair.create(pkg, user), bundle); 360 361 return null; 362 }).when(getServices().userManager).setApplicationRestrictions( 363 anyString(), nullable(Bundle.class), any(UserHandle.class)); 364 365 // UM.getApplicationRestrictions() will read from appRestrictions. 366 doAnswer((Answer<Bundle>) invocation -> { 367 String pkg = (String) invocation.getArguments()[0]; 368 UserHandle user = (UserHandle) invocation.getArguments()[1]; 369 370 return appRestrictions.get(Pair.create(pkg, user)); 371 }).when(getServices().userManager).getApplicationRestrictions( 372 anyString(), any(UserHandle.class)); 373 374 // Emulate UserManager.setUserRestriction/getUserRestrictions 375 final Map<UserHandle, Bundle> userRestrictions = new HashMap<>(); 376 377 doAnswer((Answer<Void>) invocation -> { 378 String key = (String) invocation.getArguments()[0]; 379 boolean value = (Boolean) invocation.getArguments()[1]; 380 UserHandle user = (UserHandle) invocation.getArguments()[2]; 381 Bundle userBundle = userRestrictions.getOrDefault(user, new Bundle()); 382 userBundle.putBoolean(key, value); 383 384 userRestrictions.put(user, userBundle); 385 return null; 386 }).when(getServices().userManager).setUserRestriction( 387 anyString(), anyBoolean(), any(UserHandle.class)); 388 389 doAnswer((Answer<Boolean>) invocation -> { 390 String key = (String) invocation.getArguments()[0]; 391 UserHandle user = (UserHandle) invocation.getArguments()[1]; 392 Bundle userBundle = userRestrictions.getOrDefault(user, new Bundle()); 393 return userBundle.getBoolean(key); 394 }).when(getServices().userManager).hasUserRestriction( 395 anyString(), any(UserHandle.class)); 396 397 // Add the first secondary user. 398 getServices().addUser(CALLER_USER_HANDLE, 0, UserManager.USER_TYPE_FULL_SECONDARY); 399 } 400 setAsProfileOwner(ComponentName admin)401 private void setAsProfileOwner(ComponentName admin) { 402 final long ident = mServiceContext.binder.clearCallingIdentity(); 403 404 mServiceContext.binder.callingUid = 405 UserHandle.getUid(CALLER_USER_HANDLE, DpmMockContext.SYSTEM_UID); 406 runAsCaller(mServiceContext, dpms, dpm -> { 407 // PO needs to be a DA. 408 dpm.setActiveAdmin(admin, /*replace=*/ false); 409 // Fire! 410 assertThat(dpm.setProfileOwner(admin, CALLER_USER_HANDLE)).isTrue(); 411 // Check 412 assertThat(dpm.getProfileOwnerAsUser(CALLER_USER_HANDLE)).isEqualTo(admin); 413 }); 414 415 mServiceContext.binder.restoreCallingIdentity(ident); 416 } 417 418 @Test testHasNoFeature()419 public void testHasNoFeature() throws Exception { 420 when(getServices().packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) 421 .thenReturn(false); 422 423 new DevicePolicyManagerServiceTestable(getServices(), mContext); 424 425 // If the device has no DPMS feature, it shouldn't register the local service. 426 assertThat(LocalServices.getService(DevicePolicyManagerInternal.class)).isNull(); 427 428 // But should still register the lite one 429 assertThat(LocalServices.getService(DevicePolicyManagerLiteInternal.class)).isNotNull(); 430 } 431 432 @Test testLoadAdminData()433 public void testLoadAdminData() throws Exception { 434 // Device owner in SYSTEM_USER 435 setDeviceOwner(); 436 // Profile owner in CALLER_USER_HANDLE 437 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); 438 setAsProfileOwner(admin2); 439 // Active admin in CALLER_USER_HANDLE 440 final int ANOTHER_UID = UserHandle.getUid(CALLER_USER_HANDLE, 1306); 441 setUpPackageManagerForFakeAdmin(adminAnotherPackage, ANOTHER_UID, admin2); 442 dpm.setActiveAdmin(adminAnotherPackage, /* replace =*/ false, CALLER_USER_HANDLE); 443 assertThat(dpm.isAdminActiveAsUser(adminAnotherPackage, CALLER_USER_HANDLE)).isTrue(); 444 445 initializeDpms(); 446 447 // Verify 448 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 449 MockUtils.checkApps(admin1.getPackageName()), 450 eq(UserHandle.USER_SYSTEM)); 451 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 452 MockUtils.checkApps(admin2.getPackageName(), 453 adminAnotherPackage.getPackageName()), 454 eq(CALLER_USER_HANDLE)); 455 verify(getServices().usageStatsManagerInternal).onAdminDataAvailable(); 456 verify(getServices().networkPolicyManagerInternal).onAdminDataAvailable(); 457 } 458 459 @Test testLoadAdminData_noAdmins()460 public void testLoadAdminData_noAdmins() throws Exception { 461 final int ANOTHER_USER_ID = 15; 462 getServices().addUser(ANOTHER_USER_ID, 0, ""); 463 464 initializeDpms(); 465 466 // Verify 467 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 468 null, CALLER_USER_HANDLE); 469 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 470 null, ANOTHER_USER_ID); 471 verify(getServices().usageStatsManagerInternal).onAdminDataAvailable(); 472 verify(getServices().networkPolicyManagerInternal).onAdminDataAvailable(); 473 } 474 475 /** 476 * Caller doesn't have proper permissions. 477 */ 478 @Test testSetActiveAdmin_SecurityException()479 public void testSetActiveAdmin_SecurityException() { 480 // 1. Failure cases. 481 482 // Caller doesn't have MANAGE_DEVICE_ADMINS. 483 assertExpectException(SecurityException.class, /* messageRegex= */ null, 484 () -> dpm.setActiveAdmin(admin1, false)); 485 486 // Caller has MANAGE_DEVICE_ADMINS, but for different user. 487 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 488 489 assertExpectException(SecurityException.class, /* messageRegex= */ null, 490 () -> dpm.setActiveAdmin(admin1, false, CALLER_USER_HANDLE + 1)); 491 } 492 493 /** 494 * Test for: 495 * {@link DevicePolicyManager#setActiveAdmin} 496 * with replace=false and replace=true 497 * {@link DevicePolicyManager#isAdminActive} 498 * {@link DevicePolicyManager#isAdminActiveAsUser} 499 * {@link DevicePolicyManager#getActiveAdmins} 500 * {@link DevicePolicyManager#getActiveAdminsAsUser} 501 */ 502 @Test testSetActiveAdmin()503 public void testSetActiveAdmin() throws Exception { 504 // 1. Make sure the caller has proper permissions. 505 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 506 507 // 2. Call the API. 508 dpm.setActiveAdmin(admin1, /* replace =*/ false); 509 510 // 3. Verify internal calls. 511 512 // Check if the boradcast is sent. 513 verify(mContext.spiedContext).sendBroadcastAsUser( 514 MockUtils.checkIntentAction( 515 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 516 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 517 eq(null), 518 any(Bundle.class)); 519 verify(mContext.spiedContext).sendBroadcastAsUser( 520 MockUtils.checkIntentAction( 521 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED), 522 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 523 eq(null), 524 any(Bundle.class)); 525 526 verify(getServices().ipackageManager, times(1)).setApplicationEnabledSetting( 527 eq(admin1.getPackageName()), 528 eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT), 529 eq(PackageManager.DONT_KILL_APP), 530 eq(CALLER_USER_HANDLE), 531 anyString()); 532 533 verify(getServices().usageStatsManagerInternal).onActiveAdminAdded( 534 admin1.getPackageName(), CALLER_USER_HANDLE); 535 536 // TODO Verify other calls too. 537 538 // Make sure it's active admin1. 539 assertThat(dpm.isAdminActive(admin1)).isTrue(); 540 assertThat(dpm.isAdminActive(admin2)).isFalse(); 541 assertThat(dpm.isAdminActive(admin3)).isFalse(); 542 543 // But not admin1 for a different user. 544 545 // For this to work, caller needs android.permission.INTERACT_ACROSS_USERS_FULL. 546 // (Because we're checking a different user's status from CALLER_USER_HANDLE.) 547 mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL"); 548 549 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE + 1)).isFalse(); 550 assertThat(dpm.isAdminActiveAsUser(admin2, CALLER_USER_HANDLE + 1)).isFalse(); 551 552 mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL"); 553 554 // Next, add one more admin. 555 // Before doing so, update the application info, now it's enabled. 556 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID, 557 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); 558 559 dpm.setActiveAdmin(admin2, /* replace =*/ false); 560 561 // Now we have two admins. 562 assertThat(dpm.isAdminActive(admin1)).isTrue(); 563 assertThat(dpm.isAdminActive(admin2)).isTrue(); 564 assertThat(dpm.isAdminActive(admin3)).isFalse(); 565 566 // Admin2 was already enabled, so setApplicationEnabledSetting() shouldn't have called 567 // again. (times(1) because it was previously called for admin1) 568 verify(getServices().ipackageManager, times(1)).setApplicationEnabledSetting( 569 eq(admin1.getPackageName()), 570 eq(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT), 571 eq(PackageManager.DONT_KILL_APP), 572 eq(CALLER_USER_HANDLE), 573 anyString()); 574 575 // times(2) because it was previously called for admin1 which is in the same package 576 // as admin2. 577 verify(getServices().usageStatsManagerInternal, times(2)).onActiveAdminAdded( 578 admin2.getPackageName(), CALLER_USER_HANDLE); 579 580 // 4. Add the same admin1 again without replace, which should throw. 581 assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, 582 () -> dpm.setActiveAdmin(admin1, /* replace =*/ false)); 583 584 // 5. Add the same admin1 again with replace, which should succeed. 585 dpm.setActiveAdmin(admin1, /* replace =*/ true); 586 587 // TODO make sure it's replaced. 588 589 // 6. Test getActiveAdmins() 590 List<ComponentName> admins = dpm.getActiveAdmins(); 591 assertThat(admins.size()).isEqualTo(2); 592 assertThat(admins.get(0)).isEqualTo(admin1); 593 assertThat(admins.get(1)).isEqualTo(admin2); 594 595 // There shouldn't be any callback to UsageStatsManagerInternal when the admin is being 596 // replaced 597 verifyNoMoreInteractions(getServices().usageStatsManagerInternal); 598 599 // Another user has no admins. 600 mContext.callerPermissions.add("android.permission.INTERACT_ACROSS_USERS_FULL"); 601 602 assertThat(DpmTestUtils.getListSizeAllowingNull( 603 dpm.getActiveAdminsAsUser(CALLER_USER_HANDLE + 1))).isEqualTo(0); 604 605 mContext.callerPermissions.remove("android.permission.INTERACT_ACROSS_USERS_FULL"); 606 } 607 608 @Test testSetActiveAdmin_multiUsers()609 public void testSetActiveAdmin_multiUsers() throws Exception { 610 611 final int ANOTHER_USER_ID = 100; 612 final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 20456); 613 614 getServices().addUser(ANOTHER_USER_ID, 0, ""); // Add one more user. 615 616 // Set up pacakge manager for the other user. 617 setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID); 618 619 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 620 621 dpm.setActiveAdmin(admin1, /* replace =*/ false); 622 623 mMockContext.binder.callingUid = ANOTHER_ADMIN_UID; 624 dpm.setActiveAdmin(admin2, /* replace =*/ false); 625 626 627 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; 628 assertThat(dpm.isAdminActive(admin1)).isTrue(); 629 assertThat(dpm.isAdminActive(admin2)).isFalse(); 630 631 mMockContext.binder.callingUid = ANOTHER_ADMIN_UID; 632 assertThat(dpm.isAdminActive(admin1)).isFalse(); 633 assertThat(dpm.isAdminActive(admin2)).isTrue(); 634 } 635 636 /** 637 * Test for: 638 * {@link DevicePolicyManager#setActiveAdmin} 639 * with replace=false 640 */ 641 @Test testSetActiveAdmin_twiceWithoutReplace()642 public void testSetActiveAdmin_twiceWithoutReplace() throws Exception { 643 // 1. Make sure the caller has proper permissions. 644 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 645 646 dpm.setActiveAdmin(admin1, /* replace =*/ false); 647 assertThat(dpm.isAdminActive(admin1)).isTrue(); 648 649 // Add the same admin1 again without replace, which should throw. 650 assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, 651 () -> dpm.setActiveAdmin(admin1, /* replace =*/ false)); 652 } 653 654 /** 655 * Test for: 656 * {@link DevicePolicyManager#setActiveAdmin} when the admin isn't protected with 657 * BIND_DEVICE_ADMIN. 658 */ 659 @Test testSetActiveAdmin_permissionCheck()660 public void testSetActiveAdmin_permissionCheck() throws Exception { 661 // 1. Make sure the caller has proper permissions. 662 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 663 664 assertExpectException(IllegalArgumentException.class, 665 /* messageRegex= */ permission.BIND_DEVICE_ADMIN, 666 () -> dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false)); 667 assertThat(dpm.isAdminActive(adminNoPerm)).isFalse(); 668 669 // Change the target API level to MNC. Now it can be set as DA. 670 setUpPackageManagerForAdmin(adminNoPerm, DpmMockContext.CALLER_UID, null, 671 VERSION_CODES.M); 672 dpm.setActiveAdmin(adminNoPerm, /* replace =*/ false); 673 assertThat(dpm.isAdminActive(adminNoPerm)).isTrue(); 674 675 // TODO Test the "load from the file" case where DA will still be loaded even without 676 // BIND_DEVICE_ADMIN and target API is N. 677 } 678 679 /** 680 * Test for: 681 * {@link DevicePolicyManager#removeActiveAdmin} 682 */ 683 @Test testRemoveActiveAdmin_SecurityException()684 public void testRemoveActiveAdmin_SecurityException() { 685 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 686 687 // Add admin. 688 689 dpm.setActiveAdmin(admin1, /* replace =*/ false); 690 691 assertThat(dpm.isAdminActive(admin1)).isTrue(); 692 693 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 694 695 // Directly call the DPMS method with a different userid, which should fail. 696 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 697 () -> dpms.removeActiveAdmin(admin1, CALLER_USER_HANDLE + 1)); 698 699 // Try to remove active admin with a different caller userid should fail too, without 700 // having MANAGE_DEVICE_ADMINS. 701 mContext.callerPermissions.clear(); 702 703 // Change the caller, and call into DPMS directly with a different user-id. 704 705 mContext.binder.callingUid = 1234567; 706 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 707 () -> dpms.removeActiveAdmin(admin1, CALLER_USER_HANDLE)); 708 } 709 710 /** 711 * {@link DevicePolicyManager#removeActiveAdmin} should fail with the user is not unlocked 712 * (because we can't send the remove broadcast). 713 */ 714 @Test testRemoveActiveAdmin_userNotRunningOrLocked()715 public void testRemoveActiveAdmin_userNotRunningOrLocked() { 716 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 717 718 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 719 720 // Add admin. 721 722 dpm.setActiveAdmin(admin1, /* replace =*/ false); 723 724 assertThat(dpm.isAdminActive(admin1)).isTrue(); 725 726 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 727 728 // 1. User not unlocked. 729 setUserUnlocked(CALLER_USER_HANDLE, false); 730 assertExpectException(IllegalStateException.class, 731 /* messageRegex= */ "User must be running and unlocked", 732 () -> dpm.removeActiveAdmin(admin1)); 733 734 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 735 verify(getServices().usageStatsManagerInternal, times(0)).setActiveAdminApps( 736 null, CALLER_USER_HANDLE); 737 738 // 2. User unlocked. 739 setUserUnlocked(CALLER_USER_HANDLE, true); 740 741 dpm.removeActiveAdmin(admin1); 742 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 743 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 744 null, CALLER_USER_HANDLE); 745 } 746 747 /** 748 * Test for: 749 * {@link DevicePolicyManager#removeActiveAdmin} 750 */ 751 @Test testRemoveActiveAdmin_fromDifferentUserWithINTERACT_ACROSS_USERS_FULL()752 public void testRemoveActiveAdmin_fromDifferentUserWithINTERACT_ACROSS_USERS_FULL() { 753 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 754 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 755 756 // Add admin1. 757 758 dpm.setActiveAdmin(admin1, /* replace =*/ false); 759 760 assertThat(dpm.isAdminActive(admin1)).isTrue(); 761 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 762 763 // Different user, but should work, because caller has proper permissions. 764 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 765 766 // Change the caller, and call into DPMS directly with a different user-id. 767 mContext.binder.callingUid = 1234567; 768 769 dpms.removeActiveAdmin(admin1, CALLER_USER_HANDLE); 770 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 771 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 772 null, CALLER_USER_HANDLE); 773 774 // TODO DO Still can't be removed in this case. 775 } 776 777 /** 778 * Test for: 779 * {@link DevicePolicyManager#removeActiveAdmin} 780 */ 781 @Test testRemoveActiveAdmin_sameUserNoMANAGE_DEVICE_ADMINS()782 public void testRemoveActiveAdmin_sameUserNoMANAGE_DEVICE_ADMINS() { 783 // Need MANAGE_DEVICE_ADMINS for setActiveAdmin. We'll remove it later. 784 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 785 786 // Add admin1. 787 788 dpm.setActiveAdmin(admin1, /* replace =*/ false); 789 790 assertThat(dpm.isAdminActive(admin1)).isTrue(); 791 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 792 793 // Broadcast from saveSettingsLocked(). 794 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 795 MockUtils.checkIntentAction( 796 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 797 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 798 eq(null), 799 any(Bundle.class)); 800 801 // Remove. No permissions, but same user, so it'll work. 802 mContext.callerPermissions.clear(); 803 dpm.removeActiveAdmin(admin1); 804 805 verify(mContext.spiedContext).sendOrderedBroadcastAsUser( 806 MockUtils.checkIntentAction( 807 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED), 808 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 809 isNull(String.class), 810 eq(AppOpsManager.OP_NONE), 811 any(Bundle.class), 812 any(BroadcastReceiver.class), 813 eq(dpms.mHandler), 814 eq(Activity.RESULT_OK), 815 isNull(String.class), 816 isNull(Bundle.class)); 817 818 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 819 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 820 null, CALLER_USER_HANDLE); 821 822 // Again broadcast from saveSettingsLocked(). 823 verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( 824 MockUtils.checkIntentAction( 825 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 826 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 827 eq(null), 828 any(Bundle.class)); 829 830 // TODO Check other internal calls. 831 } 832 833 @Test testRemoveActiveAdmin_multipleAdminsInUser()834 public void testRemoveActiveAdmin_multipleAdminsInUser() { 835 // Need MANAGE_DEVICE_ADMINS for setActiveAdmin. We'll remove it later. 836 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 837 838 // Add admin1. 839 dpm.setActiveAdmin(admin1, /* replace =*/ false); 840 841 assertThat(dpm.isAdminActive(admin1)).isTrue(); 842 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 843 844 // Add admin2. 845 dpm.setActiveAdmin(admin2, /* replace =*/ false); 846 847 assertThat(dpm.isAdminActive(admin2)).isTrue(); 848 assertThat(dpm.isRemovingAdmin(admin2, CALLER_USER_HANDLE)).isFalse(); 849 850 // Broadcast from saveSettingsLocked(). 851 verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( 852 MockUtils.checkIntentAction( 853 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 854 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 855 eq(null), 856 any(Bundle.class)); 857 858 // Remove. No permissions, but same user, so it'll work. 859 mContext.callerPermissions.clear(); 860 dpm.removeActiveAdmin(admin1); 861 862 verify(mContext.spiedContext).sendOrderedBroadcastAsUser( 863 MockUtils.checkIntentAction( 864 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED), 865 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 866 isNull(String.class), 867 eq(AppOpsManager.OP_NONE), 868 any(Bundle.class), 869 any(BroadcastReceiver.class), 870 eq(dpms.mHandler), 871 eq(Activity.RESULT_OK), 872 isNull(String.class), 873 isNull(Bundle.class)); 874 875 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 876 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 877 MockUtils.checkApps(admin2.getPackageName()), 878 eq(CALLER_USER_HANDLE)); 879 880 // Again broadcast from saveSettingsLocked(). 881 verify(mContext.spiedContext, times(3)).sendBroadcastAsUser( 882 MockUtils.checkIntentAction( 883 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 884 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 885 eq(null), 886 any(Bundle.class)); 887 } 888 889 /** 890 * Test for: 891 * {@link DevicePolicyManager#forceRemoveActiveAdmin(ComponentName, int)} 892 */ 893 @Test testForceRemoveActiveAdmin_nonShellCaller()894 public void testForceRemoveActiveAdmin_nonShellCaller() throws Exception { 895 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 896 897 // Add admin. 898 setupPackageInPackageManager(admin1.getPackageName(), 899 /* userId= */ CALLER_USER_HANDLE, 900 /* appId= */ 10138, 901 /* flags= */ ApplicationInfo.FLAG_TEST_ONLY); 902 dpm.setActiveAdmin(admin1, /* replace =*/ false); 903 assertThat(dpm.isAdminActive(admin1)).isTrue(); 904 905 // Calling from a non-shell uid should fail with a SecurityException 906 mContext.binder.callingUid = 123456; 907 assertExpectException(SecurityException.class, 908 /* messageRegex = */ null, 909 () -> dpms.forceRemoveActiveAdmin(admin1, CALLER_USER_HANDLE)); 910 } 911 912 /** 913 * Test for: 914 * {@link DevicePolicyManager#forceRemoveActiveAdmin(ComponentName, int)} 915 */ 916 @Test testForceRemoveActiveAdmin_nonShellCallerWithPermission()917 public void testForceRemoveActiveAdmin_nonShellCallerWithPermission() throws Exception { 918 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 919 920 // Add admin. 921 setupPackageInPackageManager(admin1.getPackageName(), 922 /* userId= */ CALLER_USER_HANDLE, 923 /* appId= */ 10138, 924 /* flags= */ ApplicationInfo.FLAG_TEST_ONLY); 925 dpm.setActiveAdmin(admin1, /* replace =*/ false); 926 assertThat(dpm.isAdminActive(admin1)).isTrue(); 927 928 mContext.binder.callingUid = 123456; 929 mContext.callerPermissions.add( 930 android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 931 dpms.forceRemoveActiveAdmin(admin1, CALLER_USER_HANDLE); 932 933 mContext.callerPermissions.add(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); 934 // Verify 935 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 936 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 937 null, CALLER_USER_HANDLE); 938 } 939 940 /** 941 * Test for: 942 * {@link DevicePolicyManager#forceRemoveActiveAdmin(ComponentName, int)} 943 */ 944 @Test testForceRemoveActiveAdmin_ShellCaller()945 public void testForceRemoveActiveAdmin_ShellCaller() throws Exception { 946 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 947 948 // Add admin. 949 setupPackageInPackageManager(admin1.getPackageName(), 950 /* userId= */ CALLER_USER_HANDLE, 951 /* appId= */ 10138, 952 /* flags= */ ApplicationInfo.FLAG_TEST_ONLY); 953 dpm.setActiveAdmin(admin1, /* replace =*/ false); 954 assertThat(dpm.isAdminActive(admin1)).isTrue(); 955 956 mContext.binder.callingUid = Process.SHELL_UID; 957 dpms.forceRemoveActiveAdmin(admin1, CALLER_USER_HANDLE); 958 959 mContext.callerPermissions.add(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); 960 // Verify 961 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 962 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 963 null, CALLER_USER_HANDLE); 964 } 965 966 /** 967 * Test for: {@link DevicePolicyManager#setPasswordHistoryLength(ComponentName, int)} 968 * 969 * Validates that when the password history length is set, it is persisted after rebooting 970 */ 971 @Test testSaveAndLoadPasswordHistoryLength_persistedAfterReboot()972 public void testSaveAndLoadPasswordHistoryLength_persistedAfterReboot() throws Exception { 973 int passwordHistoryLength = 2; 974 975 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 976 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 977 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 978 979 // Install admin1 on system user. 980 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 981 982 // Set admin1 to active admin and device owner 983 dpm.setActiveAdmin(admin1, false); 984 dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM); 985 986 // Save password history length 987 dpm.setPasswordHistoryLength(admin1, passwordHistoryLength); 988 989 assertThat(passwordHistoryLength).isEqualTo(dpm.getPasswordHistoryLength(admin1)); 990 991 initializeDpms(); 992 reset(mContext.spiedContext); 993 994 // Password history length should persist after rebooted 995 assertThat(passwordHistoryLength).isEqualTo(dpm.getPasswordHistoryLength(admin1)); 996 } 997 998 /** 999 * Test for: {@link DevicePolicyManager#reportPasswordChanged} 1000 * 1001 * Validates that when the password for a user changes, the notification broadcast intent 1002 * {@link DeviceAdminReceiver#ACTION_PASSWORD_CHANGED} is sent to managed profile owners, in 1003 * addition to ones in the original user. 1004 */ 1005 @Test testSetActivePasswordState_sendToProfiles()1006 public void testSetActivePasswordState_sendToProfiles() throws Exception { 1007 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 1008 1009 final int MANAGED_PROFILE_USER_ID = 78; 1010 final int MANAGED_PROFILE_ADMIN_UID = 1011 UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); 1012 1013 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 1014 mContext.packageName = admin1.getPackageName(); 1015 1016 // Add a managed profile belonging to the system user. 1017 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 1018 1019 // Change the parent user's password. 1020 dpm.reportPasswordChanged(new PasswordMetrics(CREDENTIAL_TYPE_PASSWORD), 1021 UserHandle.USER_SYSTEM); 1022 1023 // The managed profile owner should receive this broadcast. 1024 final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); 1025 intent.setComponent(admin1); 1026 intent.putExtra(Intent.EXTRA_USER, UserHandle.of(UserHandle.USER_SYSTEM)); 1027 1028 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 1029 MockUtils.checkIntent(intent), 1030 MockUtils.checkUserHandle(MANAGED_PROFILE_USER_ID), 1031 eq(null), 1032 any(Bundle.class)); 1033 } 1034 1035 /** 1036 * Test for: @{link DevicePolicyManager#reportPasswordChanged} 1037 * 1038 * Validates that when the password for a managed profile changes, the notification broadcast 1039 * intent {@link DeviceAdminReceiver#ACTION_PASSWORD_CHANGED} is only sent to the profile, not 1040 * its parent. 1041 */ 1042 @Test testSetActivePasswordState_notSentToParent()1043 public void testSetActivePasswordState_notSentToParent() throws Exception { 1044 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 1045 1046 final int MANAGED_PROFILE_USER_ID = 78; 1047 final int MANAGED_PROFILE_ADMIN_UID = 1048 UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); 1049 1050 // Configure system as having separate profile challenge. 1051 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 1052 mContext.packageName = admin1.getPackageName(); 1053 doReturn(true).when(getServices().lockPatternUtils) 1054 .isSeparateProfileChallengeEnabled(MANAGED_PROFILE_USER_ID); 1055 1056 // Add a managed profile belonging to the system user. 1057 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 1058 1059 // Change the profile's password. 1060 dpm.reportPasswordChanged(new PasswordMetrics(CREDENTIAL_TYPE_PASSWORD), 1061 MANAGED_PROFILE_USER_ID); 1062 1063 // Both the device owner and the managed profile owner should receive this broadcast. 1064 final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); 1065 intent.setComponent(admin1); 1066 intent.putExtra(Intent.EXTRA_USER, UserHandle.of(MANAGED_PROFILE_USER_ID)); 1067 1068 verify(mContext.spiedContext, never()).sendBroadcastAsUser( 1069 MockUtils.checkIntent(intent), 1070 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM), 1071 eq(null), 1072 any(Bundle.class)); 1073 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 1074 MockUtils.checkIntent(intent), 1075 MockUtils.checkUserHandle(MANAGED_PROFILE_USER_ID), 1076 eq(null), 1077 any(Bundle.class)); 1078 } 1079 1080 /** 1081 * Test for: {@link DevicePolicyManager#setDeviceOwner} DO on system user installs successfully. 1082 */ 1083 @Test testSetDeviceOwner()1084 public void testSetDeviceOwner() throws Exception { 1085 setDeviceOwner(); 1086 1087 // Try to set a profile owner on the same user, which should fail. 1088 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_SYSTEM_USER_UID); 1089 dpm.setActiveAdmin(admin2, /* refreshing= */ true, UserHandle.USER_SYSTEM); 1090 assertExpectException(IllegalStateException.class, 1091 /* messageRegex= */ "already has a device owner", 1092 () -> dpm.setProfileOwner(admin2, UserHandle.USER_SYSTEM)); 1093 1094 // DO admin can't be deactivated. 1095 dpm.removeActiveAdmin(admin1); 1096 assertThat(dpm.isAdminActive(admin1)).isTrue(); 1097 1098 // TODO Test getDeviceOwnerName() too. To do so, we need to change 1099 // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable. 1100 } 1101 setDeviceOwner()1102 private void setDeviceOwner() throws Exception { 1103 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1104 mContext.callerPermissions.add(permission.MANAGE_USERS); 1105 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1106 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1107 1108 // In this test, change the caller user to "system". 1109 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1110 1111 // Make sure admin1 is installed on system user. 1112 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1113 1114 // Check various get APIs. 1115 checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ false); 1116 1117 // DO needs to be an DA. 1118 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1119 1120 // Fire! 1121 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 1122 1123 // getDeviceOwnerComponent should return the admin1 component. 1124 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 1125 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1126 1127 // Check various get APIs. 1128 checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ true); 1129 1130 // getDeviceOwnerComponent should *NOT* return the admin1 component for other users. 1131 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1132 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1133 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1134 1135 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1136 1137 // Verify internal calls. 1138 verify(getServices().userManager, times(1)).setUserRestriction( 1139 eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), 1140 eq(true), eq(UserHandle.SYSTEM)); 1141 1142 verify(getServices().userManager, times(1)).setUserRestriction( 1143 eq(UserManager.DISALLOW_ADD_CLONE_PROFILE), 1144 eq(true), eq(UserHandle.SYSTEM)); 1145 1146 verify(getServices().userManager, times(1)).setUserRestriction( 1147 eq(UserManager.DISALLOW_ADD_PRIVATE_PROFILE), 1148 eq(true), eq(UserHandle.SYSTEM)); 1149 1150 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 1151 MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), 1152 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 1153 1154 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1155 } 1156 1157 // TODO(b/174859111): move to automotive-only section setDeviceOwner_headlessSystemUser()1158 private void setDeviceOwner_headlessSystemUser() throws Exception { 1159 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1160 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1161 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1162 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 1163 mContext.callerPermissions.add(permission.MANAGE_USERS); 1164 1165 when(getServices().iactivityManager.getCurrentUser()).thenReturn( 1166 new UserInfo(DpmMockContext.CALLER_USER_HANDLE, "caller", /* flags=*/ 0)); 1167 // Check various get APIs. 1168 checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ false); 1169 1170 final long ident = mServiceContext.binder.clearCallingIdentity(); 1171 1172 mServiceContext.binder.callingUid = DpmMockContext.CALLER_UID; 1173 1174 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1175 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); 1176 1177 // Set device owner 1178 runAsCaller(mServiceContext, dpms, dpm -> { 1179 // DO needs to be a DA 1180 dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); 1181 // DO should be set on headless system user 1182 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 1183 // PO should be set on calling user. 1184 assertThat(dpm.getProfileOwnerAsUser(CALLER_USER_HANDLE)).isEqualTo(admin1); 1185 }); 1186 1187 mServiceContext.binder.restoreCallingIdentity(ident); 1188 1189 // Check various get APIs. 1190 checkGetDeviceOwnerInfoApi(dpm, /* hasDeviceOwner =*/ true); 1191 1192 // Add MANAGE_USERS or test purpose. 1193 mContext.callerPermissions.add(permission.MANAGE_USERS); 1194 // getDeviceOwnerComponent should *NOT* return the admin1 component for calling user. 1195 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isNull(); 1196 // Device owner should be set on system user. 1197 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_SYSTEM); 1198 1199 // Set calling user to be system user. 1200 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1201 1202 // Device owner component should be admin1 1203 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 1204 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1205 1206 // Verify internal calls. 1207 verify(mContext.spiedContext).sendBroadcastAsUser( 1208 MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), 1209 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 1210 } 1211 checkGetDeviceOwnerInfoApi(DevicePolicyManager dpm, boolean hasDeviceOwner)1212 private void checkGetDeviceOwnerInfoApi(DevicePolicyManager dpm, boolean hasDeviceOwner) { 1213 final int origCallingUser = mContext.binder.callingUid; 1214 final List origPermissions = new ArrayList(mContext.callerPermissions); 1215 mContext.callerPermissions.clear(); 1216 1217 mContext.callerPermissions.add(permission.MANAGE_USERS); 1218 1219 mContext.binder.callingUid = Process.SYSTEM_UID; 1220 1221 // TODO Test getDeviceOwnerName() too. To do so, we need to change 1222 // DPMS.getApplicationLabel() because Context.createPackageContextAsUser() is not mockable. 1223 if (hasDeviceOwner) { 1224 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isTrue(); 1225 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isTrue(); 1226 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 1227 1228 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isTrue(); 1229 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1230 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_SYSTEM); 1231 } else { 1232 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isFalse(); 1233 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isFalse(); 1234 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1235 1236 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isFalse(); 1237 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(null); 1238 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_NULL); 1239 } 1240 1241 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1242 if (hasDeviceOwner) { 1243 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isTrue(); 1244 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isTrue(); 1245 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 1246 1247 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isTrue(); 1248 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1249 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_SYSTEM); 1250 } else { 1251 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isFalse(); 1252 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isFalse(); 1253 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1254 1255 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isFalse(); 1256 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(null); 1257 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_NULL); 1258 } 1259 1260 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1261 // Still with MANAGE_USERS. 1262 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isFalse(); 1263 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isFalse(); 1264 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1265 1266 if (hasDeviceOwner) { 1267 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isTrue(); 1268 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1269 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_SYSTEM); 1270 } else { 1271 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isFalse(); 1272 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(null); 1273 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_NULL); 1274 } 1275 1276 mContext.binder.callingUid = Process.SYSTEM_UID; 1277 mContext.callerPermissions.remove(permission.MANAGE_USERS); 1278 // System can still call "OnAnyUser" without MANAGE_USERS. 1279 if (hasDeviceOwner) { 1280 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isTrue(); 1281 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isTrue(); 1282 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 1283 1284 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isTrue(); 1285 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1286 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_SYSTEM); 1287 } else { 1288 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isFalse(); 1289 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isFalse(); 1290 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1291 1292 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isFalse(); 1293 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(null); 1294 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_NULL); 1295 } 1296 1297 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1298 // Still no MANAGE_USERS. 1299 if (hasDeviceOwner) { 1300 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isTrue(); 1301 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isTrue(); 1302 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 1303 } else { 1304 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isFalse(); 1305 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isFalse(); 1306 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1307 } 1308 1309 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1310 () -> dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); 1311 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1312 dpm::getDeviceOwnerComponentOnAnyUser); 1313 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1314 dpm::getDeviceOwnerUserId); 1315 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1316 dpm::getDeviceOwnerNameOnAnyUser); 1317 1318 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1319 // Still no MANAGE_USERS. 1320 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isFalse(); 1321 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isFalse(); 1322 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(null); 1323 1324 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1325 () -> dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())); 1326 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1327 dpm::getDeviceOwnerComponentOnAnyUser); 1328 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1329 dpm::getDeviceOwnerUserId); 1330 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1331 dpm::getDeviceOwnerNameOnAnyUser); 1332 1333 // Restore. 1334 mContext.binder.callingUid = origCallingUser; 1335 mContext.callerPermissions.addAll(origPermissions); 1336 } 1337 1338 1339 /** 1340 * Test for: {@link DevicePolicyManager#setDeviceOwner} Package doesn't exist. 1341 */ 1342 @Test testSetDeviceOwner_noSuchPackage()1343 public void testSetDeviceOwner_noSuchPackage() { 1344 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1345 mContext.callerPermissions.add(permission.MANAGE_USERS); 1346 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1347 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1348 1349 // Call from a process on the system user. 1350 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1351 1352 assertExpectException(IllegalArgumentException.class, 1353 /* messageRegex= */ "Invalid component", 1354 () -> dpm.setDeviceOwner(new ComponentName("a.b.c", ".def"), 1355 UserHandle.USER_SYSTEM)); 1356 } 1357 1358 @Test testSetDeviceOwner_failures()1359 public void testSetDeviceOwner_failures() throws Exception { 1360 // TODO Test more failure cases. Basically test all chacks in enforceCanSetDeviceOwner(). 1361 // Package doesn't exist and caller is not system 1362 assertExpectException(SecurityException.class, 1363 /* messageRegex= */ "Calling identity is not authorized", 1364 () -> dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)); 1365 1366 // Package exists, but caller is not system 1367 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1368 assertExpectException(SecurityException.class, 1369 /* messageRegex= */ "Calling identity is not authorized", 1370 () -> dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)); 1371 } 1372 1373 @Test 1374 @Ignore("b/277916462") testClearDeviceOwner()1375 public void testClearDeviceOwner() throws Exception { 1376 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1377 mContext.callerPermissions.add(permission.MANAGE_USERS); 1378 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1379 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1380 1381 // Set admin1 as a DA to the secondary user. 1382 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); 1383 1384 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1385 1386 // Set admin 1 as the DO to the system user. 1387 1388 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1389 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1390 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1391 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 1392 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1393 1394 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); 1395 when(getServices().userManager.hasUserRestriction(eq(UserManager.DISALLOW_ADD_USER), 1396 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM))).thenReturn(true); 1397 1398 assertThat(dpm.isAdminActive(admin1)).isTrue(); 1399 assertThat(dpm.isRemovingAdmin(admin1, UserHandle.USER_SYSTEM)).isFalse(); 1400 1401 // Set up other mocks. 1402 when(getServices().userManager.getUserRestrictions()).thenReturn(new Bundle()); 1403 1404 // Now call clear. 1405 getServices().addTestPackageUid(admin1.getPackageName(), 1406 DpmMockContext.CALLER_SYSTEM_USER_UID); 1407 1408 // But first pretend the user is locked. Then it should fail. 1409 when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(false); 1410 assertExpectException(IllegalStateException.class, 1411 /* messageRegex= */ "User must be running and unlocked", 1412 () -> dpm.clearDeviceOwnerApp(admin1.getPackageName())); 1413 1414 when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true); 1415 dpm.clearDeviceOwnerApp(admin1.getPackageName()); 1416 1417 // Now DO shouldn't be set. 1418 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isNull(); 1419 1420 verify(getServices().userManager).setUserRestriction(eq(UserManager.DISALLOW_ADD_USER), 1421 eq(false), 1422 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 1423 1424 verify(getServices().userManager) 1425 .setUserRestriction(eq(UserManager.DISALLOW_ADD_CLONE_PROFILE), eq(false), 1426 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 1427 1428 verify(getServices().userManager) 1429 .setUserRestriction(eq(UserManager.DISALLOW_ADD_PRIVATE_PROFILE), eq(false), 1430 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 1431 1432 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 1433 eq(UserHandle.USER_SYSTEM), MockUtils.checkUserRestrictions(), 1434 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 1435 1436 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 1437 null, UserHandle.USER_SYSTEM); 1438 1439 assertThat(dpm.isAdminActiveAsUser(admin1, UserHandle.USER_SYSTEM)).isFalse(); 1440 1441 // ACTION_DEVICE_OWNER_CHANGED should be sent twice, once for setting the device owner 1442 // and once for clearing it. 1443 verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( 1444 MockUtils.checkIntentAction(DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), 1445 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 1446 // TODO Check other calls. 1447 } 1448 1449 @Test testDeviceOwnerBackupActivateDeactivate()1450 public void testDeviceOwnerBackupActivateDeactivate() throws Exception { 1451 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1452 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1453 1454 // Set admin1 as a DA to the secondary user. 1455 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1456 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1457 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1458 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 1459 1460 verify(getServices().ibackupManager, times(1)).setBackupServiceActive( 1461 eq(UserHandle.USER_SYSTEM), eq(false)); 1462 1463 dpm.clearDeviceOwnerApp(admin1.getPackageName()); 1464 1465 verify(getServices().ibackupManager, times(1)).setBackupServiceActive( 1466 eq(UserHandle.USER_SYSTEM), eq(true)); 1467 } 1468 1469 @Test testProfileOwnerBackupActivateDeactivate()1470 public void testProfileOwnerBackupActivateDeactivate() throws Exception { 1471 setAsProfileOwner(admin1); 1472 1473 verify(getServices().ibackupManager, times(1)).setBackupServiceActive( 1474 eq(CALLER_USER_HANDLE), eq(false)); 1475 1476 dpm.clearProfileOwner(admin1); 1477 1478 verify(getServices().ibackupManager, times(1)).setBackupServiceActive( 1479 eq(CALLER_USER_HANDLE), eq(true)); 1480 } 1481 1482 @Test testClearDeviceOwner_fromDifferentUser()1483 public void testClearDeviceOwner_fromDifferentUser() throws Exception { 1484 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1485 mContext.callerPermissions.add(permission.MANAGE_USERS); 1486 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1487 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1488 1489 // Set admin1 as a DA to the secondary user. 1490 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); 1491 1492 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1493 1494 // Set admin 1 as the DO to the system user. 1495 1496 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1497 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1498 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1499 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 1500 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 1501 1502 // Now call clear from the secondary user, which should throw. 1503 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1504 1505 // Now call clear. 1506 getServices().addTestPackageUid(admin1.getPackageName(), DpmMockContext.CALLER_UID); 1507 assertExpectException(SecurityException.class, 1508 /* messageRegex =*/ "clearDeviceOwner can only be called by the device owner", 1509 () -> dpm.clearDeviceOwnerApp(admin1.getPackageName())); 1510 1511 // DO shouldn't be removed. 1512 assertThat(dpm.isDeviceManaged()).isTrue(); 1513 } 1514 1515 /** 1516 * Test for: {@link DevicePolicyManager#clearDeviceOwnerApp(String)} 1517 * 1518 * Validates that when the device owner is removed, the reset password token is cleared 1519 */ 1520 @Test 1521 @Ignore("b/277916462") testClearDeviceOwner_clearResetPasswordToken()1522 public void testClearDeviceOwner_clearResetPasswordToken() throws Exception { 1523 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 1524 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1525 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1526 1527 // Install admin1 on system user 1528 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1529 1530 // Set admin1 to active admin and device owner 1531 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1532 dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM); 1533 1534 // Add reset password token 1535 final long handle = 12000; 1536 final byte[] token = new byte[32]; 1537 when(getServices().lockPatternUtils.addEscrowToken(eq(token), eq(UserHandle.USER_SYSTEM), 1538 nullable(EscrowTokenStateChangeCallback.class))) 1539 .thenReturn(handle); 1540 assertThat(dpm.setResetPasswordToken(admin1, token)).isTrue(); 1541 1542 // Assert reset password token is active 1543 when(getServices().lockPatternUtils.isEscrowTokenActive(eq(handle), 1544 eq(UserHandle.USER_SYSTEM))) 1545 .thenReturn(true); 1546 assertThat(dpm.isResetPasswordTokenActive(admin1)).isTrue(); 1547 1548 // Remove the device owner 1549 dpm.clearDeviceOwnerApp(admin1.getPackageName()); 1550 1551 // Verify password reset password token was removed 1552 verify(getServices().lockPatternUtils).removeEscrowToken(eq(handle), 1553 eq(UserHandle.USER_SYSTEM)); 1554 } 1555 1556 @Test testSetProfileOwner()1557 public void testSetProfileOwner() throws Exception { 1558 setAsProfileOwner(admin1); 1559 1560 // PO admin can't be deactivated. 1561 dpm.removeActiveAdmin(admin1); 1562 assertThat(dpm.isAdminActive(admin1)).isTrue(); 1563 1564 // Try setting DO on the same user, which should fail. 1565 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); 1566 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 1567 runAsCaller(mServiceContext, dpms, dpm -> { 1568 dpm.setActiveAdmin(admin2, /* refreshing= */ true, CALLER_USER_HANDLE); 1569 assertExpectException(IllegalStateException.class, 1570 /* messageRegex= */ "already has a profile owner", 1571 () -> dpm.setDeviceOwner(admin2, CALLER_USER_HANDLE)); 1572 }); 1573 } 1574 1575 @Test testClearProfileOwner()1576 public void testClearProfileOwner() throws Exception { 1577 setAsProfileOwner(admin1); 1578 1579 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1580 1581 assertThat(dpm.isProfileOwnerApp(admin1.getPackageName())).isTrue(); 1582 assertThat(dpm.isRemovingAdmin(admin1, CALLER_USER_HANDLE)).isFalse(); 1583 1584 // First try when the user is locked, which should fail. 1585 when(getServices().userManager.isUserUnlocked(anyInt())) 1586 .thenReturn(false); 1587 assertExpectException(IllegalStateException.class, 1588 /* messageRegex= */ "User must be running and unlocked", 1589 () -> dpm.clearProfileOwner(admin1)); 1590 1591 // Clear, really. 1592 when(getServices().userManager.isUserUnlocked(anyInt())).thenReturn(true); 1593 dpm.clearProfileOwner(admin1); 1594 1595 // Check 1596 assertThat(dpm.isProfileOwnerApp(admin1.getPackageName())).isFalse(); 1597 assertThat(dpm.isAdminActiveAsUser(admin1, CALLER_USER_HANDLE)).isFalse(); 1598 verify(getServices().usageStatsManagerInternal).setActiveAdminApps( 1599 null, CALLER_USER_HANDLE); 1600 } 1601 1602 @Test testSetProfileOwner_failures()1603 public void testSetProfileOwner_failures() throws Exception { 1604 // TODO Test more failure cases. Basically test all chacks in enforceCanSetProfileOwner(). 1605 // Package doesn't exist and caller is not system 1606 assertExpectException(SecurityException.class, 1607 /* messageRegex= */ "Calling identity is not authorized", 1608 () -> dpm.setProfileOwner(admin1, UserHandle.USER_SYSTEM)); 1609 1610 // Package exists, but caller is not system 1611 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1612 assertExpectException(SecurityException.class, 1613 /* messageRegex= */ "Calling identity is not authorized", 1614 () -> dpm.setProfileOwner(admin1, UserHandle.USER_SYSTEM)); 1615 } 1616 1617 @Test testGetDeviceOwnerAdminLocked()1618 public void testGetDeviceOwnerAdminLocked() throws Exception { 1619 checkDeviceOwnerWithMultipleDeviceAdmins(); 1620 } 1621 1622 // This method is used primarily for testDeviceOwnerMigration. checkDeviceOwnerWithMultipleDeviceAdmins()1623 private void checkDeviceOwnerWithMultipleDeviceAdmins() throws Exception { 1624 // In ths test, we use 3 users (system + 2 secondary users), set some device admins to them, 1625 // set admin3 on USER_SYSTEM as DO, then call getDeviceOwnerAdminLocked() to 1626 // make sure it gets the right component from the right user. 1627 1628 final int ANOTHER_USER_ID = 100; 1629 final int ANOTHER_ADMIN_UID = UserHandle.getUid(ANOTHER_USER_ID, 456); 1630 1631 getServices().addUser(ANOTHER_USER_ID, 0, ""); // Add one more user. 1632 1633 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1634 mContext.callerPermissions.add(permission.MANAGE_USERS); 1635 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1636 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1637 1638 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1639 1640 // Make sure the admin package is installed to each user. 1641 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1642 setUpPackageManagerForAdmin(admin3, DpmMockContext.CALLER_SYSTEM_USER_UID); 1643 1644 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); 1645 setUpPackageManagerForAdmin(admin2, DpmMockContext.CALLER_UID); 1646 1647 setUpPackageManagerForAdmin(admin2, ANOTHER_ADMIN_UID); 1648 1649 // Set active admins to the users. 1650 dpm.setActiveAdmin(admin1, /* replace =*/ false); 1651 dpm.setActiveAdmin(admin3, /* replace =*/ false); 1652 1653 dpm.setActiveAdmin(admin1, /* replace =*/ false, CALLER_USER_HANDLE); 1654 dpm.setActiveAdmin(admin2, /* replace =*/ false, CALLER_USER_HANDLE); 1655 1656 dpm.setActiveAdmin(admin2, /* replace =*/ false, ANOTHER_USER_ID); 1657 1658 // Set DO on the system user which is only allowed during first boot. 1659 setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); 1660 assertThat(dpm.setDeviceOwner(admin3, UserHandle.USER_SYSTEM)).isTrue(); 1661 assertThat(dpms.getDeviceOwnerComponent(/* callingUserOnly =*/ false)).isEqualTo(admin3); 1662 1663 // Then check getDeviceOwnerAdminLocked(). 1664 ActiveAdmin deviceOwner = getDeviceOwner(); 1665 assertThat(deviceOwner.info.getComponent()).isEqualTo(admin3); 1666 assertThat(deviceOwner.getUid()).isEqualTo(DpmMockContext.CALLER_SYSTEM_USER_UID); 1667 } 1668 1669 @Test testSetGetApplicationRestriction()1670 public void testSetGetApplicationRestriction() { 1671 setAsProfileOwner(admin1); 1672 mContext.packageName = admin1.getPackageName(); 1673 1674 { 1675 Bundle rest = new Bundle(); 1676 rest.putString("KEY_STRING", "Foo1"); 1677 dpm.setApplicationRestrictions(admin1, "pkg1", rest); 1678 } 1679 1680 { 1681 Bundle rest = new Bundle(); 1682 rest.putString("KEY_STRING", "Foo2"); 1683 dpm.setApplicationRestrictions(admin1, "pkg2", rest); 1684 } 1685 1686 { 1687 Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg1"); 1688 assertThat(returned).isNotNull(); 1689 assertThat(returned.size()).isEqualTo(1); 1690 assertThat("Foo1").isEqualTo(returned.get("KEY_STRING")); 1691 } 1692 1693 { 1694 Bundle returned = dpm.getApplicationRestrictions(admin1, "pkg2"); 1695 assertThat(returned).isNotNull(); 1696 assertThat(returned.size()).isEqualTo(1); 1697 assertThat("Foo2").isEqualTo(returned.get("KEY_STRING")); 1698 } 1699 1700 dpm.setApplicationRestrictions(admin1, "pkg2", new Bundle()); 1701 assertThat(dpm.getApplicationRestrictions(admin1, "pkg2").size()).isEqualTo(0); 1702 } 1703 1704 /** 1705 * Setup a package in the package manager mock for {@link DpmMockContext#CALLER_USER_HANDLE}. 1706 * Useful for faking installed applications. 1707 * 1708 * @param packageName the name of the package to be setup 1709 * @param appId the application ID to be given to the package 1710 * @return the UID of the package as known by the mock package manager 1711 */ setupPackageInPackageManager(final String packageName, final int appId)1712 private int setupPackageInPackageManager(final String packageName, final int appId) 1713 throws Exception { 1714 return setupPackageInPackageManager(packageName, CALLER_USER_HANDLE, appId, 1715 ApplicationInfo.FLAG_HAS_CODE); 1716 } 1717 1718 /** 1719 * Setup a package in the package manager mock. Useful for faking installed applications. 1720 * 1721 * @param packageName the name of the package to be setup 1722 * @param userId the user id where the package will be "installed" 1723 * @param appId the application ID to be given to the package 1724 * @param flags flags to set in the ApplicationInfo for this package 1725 * @return the UID of the package as known by the mock package manager 1726 */ setupPackageInPackageManager(final String packageName, int userId, final int appId, int flags)1727 private int setupPackageInPackageManager(final String packageName, int userId, final int appId, 1728 int flags) throws Exception { 1729 final int uid = UserHandle.getUid(userId, appId); 1730 // Make the PackageManager return the package instead of throwing NameNotFoundException 1731 final PackageInfo pi = new PackageInfo(); 1732 pi.applicationInfo = new ApplicationInfo(); 1733 pi.applicationInfo.flags = flags; 1734 doReturn(pi).when(getServices().ipackageManager).getPackageInfo( 1735 eq(packageName), 1736 anyLong(), 1737 eq(userId)); 1738 doReturn(pi.applicationInfo).when(getServices().ipackageManager).getApplicationInfo( 1739 eq(packageName), 1740 anyLong(), 1741 eq(userId)); 1742 doReturn(true).when(getServices().ipackageManager).isPackageAvailable(packageName, userId); 1743 // Setup application UID with the PackageManager 1744 getServices().addTestPackageUid(packageName, uid); 1745 // Associate packageName to uid 1746 doReturn(packageName).when(getServices().ipackageManager).getNameForUid(eq(uid)); 1747 doReturn(new String[]{packageName}) 1748 .when(getServices().ipackageManager).getPackagesForUid(eq(uid)); 1749 return uid; 1750 } 1751 1752 @Test testCertificateDisclosure()1753 public void testCertificateDisclosure() throws Exception { 1754 final int userId = CALLER_USER_HANDLE; 1755 final UserHandle user = UserHandle.of(userId); 1756 1757 mServiceContext.packageName = mRealTestContext.getPackageName(); 1758 mServiceContext.applicationInfo = new ApplicationInfo(); 1759 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 1760 when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); 1761 1762 StringParceledListSlice oneCert = asSlice(new String[] {"1"}); 1763 StringParceledListSlice fourCerts = asSlice(new String[] {"1", "2", "3", "4"}); 1764 1765 // Given that we have exactly one certificate installed, 1766 when(getServices().keyChainConnection.getService().getUserCaAliases()).thenReturn(oneCert); 1767 // when that certificate is approved, 1768 dpms.approveCaCert(oneCert.getList().get(0), userId, true); 1769 // a notification should not be shown. 1770 verify(getServices().notificationManager, timeout(1000)) 1771 .cancelAsUser(anyString(), anyInt(), eq(user)); 1772 1773 // Given that we have four certificates installed, 1774 when(getServices().keyChainConnection.getService().getUserCaAliases()) 1775 .thenReturn(fourCerts); 1776 // when two of them are approved (one of them approved twice hence no action), 1777 dpms.approveCaCert(fourCerts.getList().get(0), userId, true); 1778 dpms.approveCaCert(fourCerts.getList().get(1), userId, true); 1779 // a notification should be shown saying that there are two certificates left to approve. 1780 final String TEST_STRING_RESULT = "Test for exactly 2 certs out of 4"; 1781 verify(getServices().notificationManager, timeout(1000)) 1782 .notifyAsUser(anyString(), anyInt(), argThat(hasExtra(EXTRA_TITLE, 1783 TEST_STRING_RESULT 1784 )), eq(user)); 1785 } 1786 1787 @Test testRemoveCredentialManagementApp()1788 public void testRemoveCredentialManagementApp() throws Exception { 1789 final String packageName = "com.test.cred.mng"; 1790 Intent intent = new Intent(Intent.ACTION_PACKAGE_REMOVED); 1791 intent.setData(Uri.parse("package:" + packageName)); 1792 dpms.mReceiver.setPendingResult( 1793 new BroadcastReceiver.PendingResult(Activity.RESULT_OK, 1794 "resultData", 1795 /* resultExtras= */ null, 1796 BroadcastReceiver.PendingResult.TYPE_UNREGISTERED, 1797 /* ordered= */ true, 1798 /* sticky= */ false, 1799 /* token= */ null, 1800 CALLER_USER_HANDLE, 1801 /* flags= */ 0)); 1802 when(getServices().keyChainConnection.getService().hasCredentialManagementApp()) 1803 .thenReturn(true); 1804 when(getServices().keyChainConnection.getService().getCredentialManagementAppPackageName()) 1805 .thenReturn(packageName); 1806 1807 dpms.mReceiver.onReceive(mContext, intent); 1808 1809 flushTasks(dpms); 1810 verify(getServices().keyChainConnection.getService()).hasCredentialManagementApp(); 1811 verify(getServices().keyChainConnection.getService()).removeCredentialManagementApp(); 1812 } 1813 1814 /** 1815 * Simple test for delegate set/get and general delegation. Tests verifying that delegated 1816 * privileges can acually be exercised by a delegate are not covered here. 1817 */ 1818 @Test 1819 @Ignore // temp dsiabled - broken with flags testDelegation()1820 public void testDelegation() throws Exception { 1821 setAsProfileOwner(admin1); 1822 1823 final int userHandle = CALLER_USER_HANDLE; 1824 1825 // Given two packages 1826 final String CERT_DELEGATE = "com.delegate.certs"; 1827 final String RESTRICTIONS_DELEGATE = "com.delegate.apprestrictions"; 1828 final int CERT_DELEGATE_UID = setupPackageInPackageManager(CERT_DELEGATE, 20988); 1829 final int RESTRICTIONS_DELEGATE_UID = setupPackageInPackageManager(RESTRICTIONS_DELEGATE, 1830 20989); 1831 1832 // On delegation 1833 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1834 mContext.packageName = admin1.getPackageName(); 1835 dpm.setCertInstallerPackage(admin1, CERT_DELEGATE); 1836 dpm.setApplicationRestrictionsManagingPackage(admin1, RESTRICTIONS_DELEGATE); 1837 1838 // DPMS correctly stores and retrieves the delegates 1839 DevicePolicyData policy = dpms.mUserData.get(userHandle); 1840 assertThat(policy.mDelegationMap.size()).isEqualTo(2); 1841 MoreAsserts.assertContentsInAnyOrder(policy.mDelegationMap.get(CERT_DELEGATE), 1842 DELEGATION_CERT_INSTALL); 1843 MoreAsserts.assertContentsInAnyOrder(dpm.getDelegatedScopes(admin1, CERT_DELEGATE), 1844 DELEGATION_CERT_INSTALL); 1845 assertThat(dpm.getCertInstallerPackage(admin1)).isEqualTo(CERT_DELEGATE); 1846 MoreAsserts.assertContentsInAnyOrder(policy.mDelegationMap.get(RESTRICTIONS_DELEGATE), 1847 DELEGATION_APP_RESTRICTIONS); 1848 MoreAsserts.assertContentsInAnyOrder(dpm.getDelegatedScopes(admin1, RESTRICTIONS_DELEGATE), 1849 DELEGATION_APP_RESTRICTIONS); 1850 assertThat(dpm.getApplicationRestrictionsManagingPackage(admin1)) 1851 .isEqualTo(RESTRICTIONS_DELEGATE); 1852 1853 // On calling install certificate APIs from an unauthorized process 1854 mContext.binder.callingUid = RESTRICTIONS_DELEGATE_UID; 1855 mContext.packageName = RESTRICTIONS_DELEGATE; 1856 1857 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1858 () -> dpm.installCaCert(null, null)); 1859 1860 // On calling install certificate APIs from an authorized process 1861 mContext.binder.callingUid = CERT_DELEGATE_UID; 1862 mContext.packageName = CERT_DELEGATE; 1863 1864 // DPMS executes without a SecurityException 1865 try { 1866 dpm.installCaCert(null, null); 1867 } catch (SecurityException unexpected) { 1868 fail("Threw SecurityException on authorized access"); 1869 } catch (NullPointerException expected) { 1870 } 1871 1872 // On removing a delegate 1873 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1874 mContext.packageName = admin1.getPackageName(); 1875 dpm.setCertInstallerPackage(admin1, null); 1876 1877 // DPMS does not allow access to ex-delegate 1878 mContext.binder.callingUid = CERT_DELEGATE_UID; 1879 mContext.packageName = CERT_DELEGATE; 1880 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 1881 () -> dpm.installCaCert(null, null)); 1882 1883 // But still allows access to other existing delegates 1884 mContext.binder.callingUid = RESTRICTIONS_DELEGATE_UID; 1885 mContext.packageName = RESTRICTIONS_DELEGATE; 1886 try { 1887 dpm.getApplicationRestrictions(null, "pkg"); 1888 } catch (SecurityException expected) { 1889 fail("Threw SecurityException on authorized access"); 1890 } 1891 } 1892 1893 @Test 1894 @Ignore // Temp disabled - broken with flags testApplicationRestrictionsManagingApp()1895 public void testApplicationRestrictionsManagingApp() throws Exception { 1896 setAsProfileOwner(admin1); 1897 1898 final String nonExistAppRestrictionsManagerPackage = "com.google.app.restrictions.manager2"; 1899 final String appRestrictionsManagerPackage = "com.google.app.restrictions.manager"; 1900 final String nonDelegateExceptionMessageRegex = 1901 "Caller with uid \\d+ is not com.google.app.restrictions.manager"; 1902 final int appRestrictionsManagerAppId = 20987; 1903 final int appRestrictionsManagerUid = setupPackageInPackageManager( 1904 appRestrictionsManagerPackage, appRestrictionsManagerAppId); 1905 1906 // appRestrictionsManager package shouldn't be able to manage restrictions as the PO hasn't 1907 // delegated that permission yet. 1908 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1909 mContext.packageName = admin1.getPackageName(); 1910 assertThat(dpm.isCallerApplicationRestrictionsManagingPackage()).isFalse(); 1911 final Bundle rest = new Bundle(); 1912 rest.putString("KEY_STRING", "Foo1"); 1913 assertExpectException(SecurityException.class, INVALID_CALLING_IDENTITY_MSG, 1914 () -> dpm.setApplicationRestrictions(null, "pkg1", rest)); 1915 1916 // Check via the profile owner that no restrictions were set. 1917 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1918 mContext.packageName = admin1.getPackageName(); 1919 assertThat(dpm.getApplicationRestrictions(admin1, "pkg1").size()).isEqualTo(0); 1920 1921 // Check the API does not allow setting a non-existent package 1922 assertExpectException(PackageManager.NameNotFoundException.class, 1923 /* messageRegex= */ nonExistAppRestrictionsManagerPackage, 1924 () -> dpm.setApplicationRestrictionsManagingPackage( 1925 admin1, nonExistAppRestrictionsManagerPackage)); 1926 1927 // Let appRestrictionsManagerPackage manage app restrictions 1928 dpm.setApplicationRestrictionsManagingPackage(admin1, appRestrictionsManagerPackage); 1929 assertThat(dpm.getApplicationRestrictionsManagingPackage(admin1)) 1930 .isEqualTo(appRestrictionsManagerPackage); 1931 1932 // Now that package should be able to set and retrieve app restrictions. 1933 mContext.binder.callingUid = appRestrictionsManagerUid; 1934 mContext.packageName = appRestrictionsManagerPackage; 1935 assertThat(dpm.isCallerApplicationRestrictionsManagingPackage()).isTrue(); 1936 dpm.setApplicationRestrictions(null, "pkg1", rest); 1937 Bundle returned = dpm.getApplicationRestrictions(null, "pkg1"); 1938 assertThat(returned.size()).isEqualTo(1); 1939 assertThat(returned.get("KEY_STRING")).isEqualTo("Foo1"); 1940 1941 // The same app running on a separate user shouldn't be able to manage app restrictions. 1942 mContext.binder.callingUid = UserHandle.getUid( 1943 UserHandle.USER_SYSTEM, appRestrictionsManagerAppId); 1944 assertThat(dpm.isCallerApplicationRestrictionsManagingPackage()).isFalse(); 1945 assertExpectException(SecurityException.class, nonDelegateExceptionMessageRegex, 1946 () -> dpm.setApplicationRestrictions(null, "pkg1", rest)); 1947 1948 // The DPM is still able to manage app restrictions, even if it allowed another app to do it 1949 // too. 1950 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 1951 mContext.packageName = admin1.getPackageName(); 1952 assertThat(dpm.getApplicationRestrictions(admin1, "pkg1")).isEqualTo(returned); 1953 dpm.setApplicationRestrictions(admin1, "pkg1", null); 1954 assertThat(dpm.getApplicationRestrictions(admin1, "pkg1").size()).isEqualTo(0); 1955 1956 // Removing the ability for the package to manage app restrictions. 1957 dpm.setApplicationRestrictionsManagingPackage(admin1, null); 1958 assertThat(dpm.getApplicationRestrictionsManagingPackage(admin1)).isNull(); 1959 mContext.binder.callingUid = appRestrictionsManagerUid; 1960 mContext.packageName = appRestrictionsManagerPackage; 1961 assertThat(dpm.isCallerApplicationRestrictionsManagingPackage()).isFalse(); 1962 assertExpectException(SecurityException.class, INVALID_CALLING_IDENTITY_MSG, 1963 () -> dpm.setApplicationRestrictions(null, "pkg1", null)); 1964 } 1965 1966 @Test 1967 @Ignore("b/277916462") testSetUserRestriction_asDo()1968 public void testSetUserRestriction_asDo() throws Exception { 1969 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 1970 mContext.callerPermissions.add(permission.MANAGE_USERS); 1971 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 1972 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 1973 1974 // First, set DO. 1975 1976 // Call from a process on the system user. 1977 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 1978 1979 // Make sure admin1 is installed on system user. 1980 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 1981 1982 // Call. 1983 dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); 1984 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 1985 1986 assertNoDeviceOwnerRestrictions(); 1987 reset(getServices().userManagerInternal); 1988 1989 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); 1990 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 1991 eq(UserHandle.USER_SYSTEM), 1992 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), 1993 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 1994 reset(getServices().userManagerInternal); 1995 1996 dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); 1997 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 1998 eq(UserHandle.USER_SYSTEM), 1999 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), 2000 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM, 2001 UserManager.DISALLOW_OUTGOING_CALLS), 2002 eq(true)); 2003 reset(getServices().userManagerInternal); 2004 2005 DpmTestUtils.assertRestrictions( 2006 DpmTestUtils.newRestrictions( 2007 UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS), 2008 getDeviceOwner().ensureUserRestrictions() 2009 ); 2010 DpmTestUtils.assertRestrictions( 2011 DpmTestUtils.newRestrictions( 2012 UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_OUTGOING_CALLS), 2013 dpm.getUserRestrictions(admin1) 2014 ); 2015 2016 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); 2017 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2018 eq(UserHandle.USER_SYSTEM), 2019 MockUtils.checkUserRestrictions(), 2020 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM, 2021 UserManager.DISALLOW_OUTGOING_CALLS), 2022 eq(true)); 2023 reset(getServices().userManagerInternal); 2024 2025 DpmTestUtils.assertRestrictions( 2026 DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), 2027 getDeviceOwner().ensureUserRestrictions() 2028 ); 2029 DpmTestUtils.assertRestrictions( 2030 DpmTestUtils.newRestrictions(UserManager.DISALLOW_OUTGOING_CALLS), 2031 dpm.getUserRestrictions(admin1) 2032 ); 2033 2034 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); 2035 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2036 eq(UserHandle.USER_SYSTEM), 2037 MockUtils.checkUserRestrictions(), 2038 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 2039 reset(getServices().userManagerInternal); 2040 2041 assertNoDeviceOwnerRestrictions(); 2042 2043 // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE are PO restrictions, but when 2044 // DO sets them, the scope is global. 2045 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); 2046 reset(getServices().userManagerInternal); 2047 dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); 2048 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2049 eq(UserHandle.USER_SYSTEM), 2050 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADJUST_VOLUME, 2051 UserManager.DISALLOW_UNMUTE_MICROPHONE), 2052 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 2053 reset(getServices().userManagerInternal); 2054 2055 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); 2056 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); 2057 reset(getServices().userManagerInternal); 2058 2059 // More tests. 2060 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER); 2061 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2062 eq(UserHandle.USER_SYSTEM), 2063 MockUtils.checkUserRestrictions(UserManager.DISALLOW_ADD_USER), 2064 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 2065 reset(getServices().userManagerInternal); 2066 2067 dpm.addUserRestriction(admin1, UserManager.DISALLOW_FUN); 2068 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2069 eq(UserHandle.USER_SYSTEM), 2070 MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, 2071 UserManager.DISALLOW_ADD_USER), 2072 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 2073 reset(getServices().userManagerInternal); 2074 2075 dpm.setCameraDisabled(admin1, true); 2076 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2077 eq(UserHandle.USER_SYSTEM), 2078 // DISALLOW_CAMERA will be applied globally. 2079 MockUtils.checkUserRestrictions(UserManager.DISALLOW_FUN, 2080 UserManager.DISALLOW_ADD_USER, UserManager.DISALLOW_CAMERA), 2081 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM), eq(true)); 2082 reset(getServices().userManagerInternal); 2083 } 2084 getDeviceOwner()2085 private ActiveAdmin getDeviceOwner() { 2086 ComponentName component = dpms.mOwners.getDeviceOwnerComponent(); 2087 DevicePolicyData policy = 2088 dpms.getUserData(dpms.mOwners.getDeviceOwnerUserId()); 2089 for (ActiveAdmin admin : policy.mAdminList) { 2090 if (component.equals(admin.info.getComponent())) { 2091 return admin; 2092 } 2093 } 2094 return null; 2095 } 2096 2097 @Test testDaDisallowedPolicies_SecurityException()2098 public void testDaDisallowedPolicies_SecurityException() throws Exception { 2099 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2100 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 2101 2102 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID, null, 2103 Build.VERSION_CODES.Q); 2104 dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); 2105 2106 2107 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2108 boolean originalCameraDisabled = dpm.getCameraDisabled(admin1); 2109 assertExpectException(SecurityException.class, /* messageRegex= */ null, 2110 () -> dpm.setCameraDisabled(admin1, true)); 2111 assertThat(dpm.getCameraDisabled(admin1)).isEqualTo(originalCameraDisabled); 2112 2113 int originalKeyguardDisabledFeatures = dpm.getKeyguardDisabledFeatures(admin1); 2114 assertExpectException(SecurityException.class, /* messageRegex= */ null, 2115 () -> dpm.setKeyguardDisabledFeatures(admin1, 2116 DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_ALL)); 2117 assertThat(dpm.getKeyguardDisabledFeatures(admin1)) 2118 .isEqualTo(originalKeyguardDisabledFeatures); 2119 2120 long originalPasswordExpirationTimeout = dpm.getPasswordExpirationTimeout(admin1); 2121 assertExpectException(SecurityException.class, /* messageRegex= */ null, 2122 () -> dpm.setPasswordExpirationTimeout(admin1, 1234)); 2123 assertThat(dpm.getPasswordExpirationTimeout(admin1)) 2124 .isEqualTo(originalPasswordExpirationTimeout); 2125 2126 if (isDeprecatedPasswordApisSupported()) { 2127 int originalPasswordQuality = dpm.getPasswordQuality(admin1); 2128 assertExpectException(SecurityException.class, /* messageRegex= */ null, 2129 () -> dpm.setPasswordQuality(admin1, 2130 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)); 2131 assertThat(dpm.getPasswordQuality(admin1)).isEqualTo(originalPasswordQuality); 2132 } 2133 } 2134 2135 @Test 2136 @Ignore("b/277916462") testSetUserRestriction_asPo()2137 public void testSetUserRestriction_asPo() { 2138 setAsProfileOwner(admin1); 2139 2140 DpmTestUtils.assertRestrictions( 2141 DpmTestUtils.newRestrictions(), 2142 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE).ensureUserRestrictions() 2143 ); 2144 2145 dpm.addUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); 2146 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2147 eq(CALLER_USER_HANDLE), 2148 MockUtils.checkUserRestrictions(), 2149 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE, 2150 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES), 2151 eq(false)); 2152 2153 dpm.addUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); 2154 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2155 eq(CALLER_USER_HANDLE), 2156 MockUtils.checkUserRestrictions(), 2157 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE, 2158 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, 2159 UserManager.DISALLOW_OUTGOING_CALLS), 2160 eq(false)); 2161 2162 DpmTestUtils.assertRestrictions( 2163 DpmTestUtils.newRestrictions( 2164 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, 2165 UserManager.DISALLOW_OUTGOING_CALLS 2166 ), 2167 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2168 .ensureUserRestrictions() 2169 ); 2170 DpmTestUtils.assertRestrictions( 2171 DpmTestUtils.newRestrictions( 2172 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, 2173 UserManager.DISALLOW_OUTGOING_CALLS 2174 ), 2175 dpm.getUserRestrictions(admin1) 2176 ); 2177 2178 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); 2179 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2180 eq(CALLER_USER_HANDLE), 2181 MockUtils.checkUserRestrictions(), 2182 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE, 2183 UserManager.DISALLOW_OUTGOING_CALLS), 2184 eq(false)); 2185 2186 DpmTestUtils.assertRestrictions( 2187 DpmTestUtils.newRestrictions( 2188 UserManager.DISALLOW_OUTGOING_CALLS 2189 ), 2190 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2191 .ensureUserRestrictions() 2192 ); 2193 DpmTestUtils.assertRestrictions( 2194 DpmTestUtils.newRestrictions( 2195 UserManager.DISALLOW_OUTGOING_CALLS 2196 ), 2197 dpm.getUserRestrictions(admin1) 2198 ); 2199 2200 dpm.clearUserRestriction(admin1, UserManager.DISALLOW_OUTGOING_CALLS); 2201 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2202 eq(CALLER_USER_HANDLE), 2203 MockUtils.checkUserRestrictions(), 2204 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE), eq(false)); 2205 2206 DpmTestUtils.assertRestrictions( 2207 DpmTestUtils.newRestrictions(), 2208 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2209 .ensureUserRestrictions() 2210 ); 2211 DpmTestUtils.assertRestrictions( 2212 DpmTestUtils.newRestrictions(), 2213 dpm.getUserRestrictions(admin1) 2214 ); 2215 2216 // DISALLOW_ADJUST_VOLUME and DISALLOW_UNMUTE_MICROPHONE can be set by PO too, even 2217 // though when DO sets them they'll be applied globally. 2218 dpm.addUserRestriction(admin1, UserManager.DISALLOW_ADJUST_VOLUME); 2219 2220 dpm.addUserRestriction(admin1, UserManager.DISALLOW_UNMUTE_MICROPHONE); 2221 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2222 eq(CALLER_USER_HANDLE), 2223 MockUtils.checkUserRestrictions(), 2224 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE, 2225 UserManager.DISALLOW_ADJUST_VOLUME, 2226 UserManager.DISALLOW_UNMUTE_MICROPHONE), 2227 eq(false)); 2228 2229 dpm.setCameraDisabled(admin1, true); 2230 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2231 eq(CALLER_USER_HANDLE), 2232 MockUtils.checkUserRestrictions(), 2233 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE, 2234 UserManager.DISALLOW_ADJUST_VOLUME, 2235 UserManager.DISALLOW_UNMUTE_MICROPHONE, 2236 UserManager.DISALLOW_CAMERA), 2237 eq(false)); 2238 reset(getServices().userManagerInternal); 2239 2240 // TODO Make sure restrictions are written to the file. 2241 } 2242 2243 private static final Set<String> PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS = 2244 Sets.newSet( 2245 UserManager.DISALLOW_AIRPLANE_MODE, 2246 UserManager.DISALLOW_CONFIG_DATE_TIME, 2247 UserManager.DISALLOW_CONFIG_PRIVATE_DNS 2248 ); 2249 2250 private static final Set<String> PROFILE_OWNER_ORGANIZATION_OWNED_LOCAL_RESTRICTIONS = 2251 Sets.newSet( 2252 UserManager.DISALLOW_CONFIG_BLUETOOTH, 2253 UserManager.DISALLOW_CONFIG_LOCATION, 2254 UserManager.DISALLOW_CONFIG_WIFI, 2255 UserManager.DISALLOW_CONTENT_CAPTURE, 2256 UserManager.DISALLOW_CONTENT_SUGGESTIONS, 2257 UserManager.DISALLOW_DEBUGGING_FEATURES, 2258 UserManager.DISALLOW_SHARE_LOCATION, 2259 UserManager.DISALLOW_OUTGOING_CALLS, 2260 UserManager.DISALLOW_BLUETOOTH_SHARING, 2261 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, 2262 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, 2263 UserManager.DISALLOW_CONFIG_TETHERING, 2264 UserManager.DISALLOW_DATA_ROAMING, 2265 UserManager.DISALLOW_SAFE_BOOT, 2266 UserManager.DISALLOW_SMS, 2267 UserManager.DISALLOW_USB_FILE_TRANSFER, 2268 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, 2269 UserManager.DISALLOW_UNMUTE_MICROPHONE 2270 ); 2271 2272 @Test 2273 @Ignore("b/277916462") testSetUserRestriction_asPoOfOrgOwnedDevice()2274 public void testSetUserRestriction_asPoOfOrgOwnedDevice() throws Exception { 2275 final int MANAGED_PROFILE_ADMIN_UID = 2276 UserHandle.getUid(CALLER_USER_HANDLE, DpmMockContext.SYSTEM_UID); 2277 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 2278 2279 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 2280 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 2281 2282 when(getServices().userManager.getProfileParent(CALLER_USER_HANDLE)) 2283 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 2284 2285 for (String restriction : PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS) { 2286 addAndRemoveGlobalUserRestrictionOnParentDpm(restriction); 2287 } 2288 for (String restriction : PROFILE_OWNER_ORGANIZATION_OWNED_LOCAL_RESTRICTIONS) { 2289 addAndRemoveLocalUserRestrictionOnParentDpm(restriction); 2290 } 2291 2292 parentDpm.setCameraDisabled(admin1, true); 2293 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2294 eq(CALLER_USER_HANDLE), 2295 MockUtils.checkUserRestrictions(), 2296 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM, 2297 UserManager.DISALLOW_CAMERA), 2298 eq(false)); 2299 DpmTestUtils.assertRestrictions( 2300 DpmTestUtils.newRestrictions(UserManager.DISALLOW_CAMERA), 2301 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2302 .getParentActiveAdmin() 2303 .getEffectiveRestrictions() 2304 ); 2305 2306 parentDpm.setCameraDisabled(admin1, false); 2307 DpmTestUtils.assertRestrictions( 2308 DpmTestUtils.newRestrictions(), 2309 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2310 .getParentActiveAdmin() 2311 .getEffectiveRestrictions() 2312 ); 2313 reset(getServices().userManagerInternal); 2314 } 2315 addAndRemoveGlobalUserRestrictionOnParentDpm(String restriction)2316 private void addAndRemoveGlobalUserRestrictionOnParentDpm(String restriction) { 2317 parentDpm.addUserRestriction(admin1, restriction); 2318 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2319 eq(CALLER_USER_HANDLE), 2320 MockUtils.checkUserRestrictions(restriction), 2321 MockUtils.checkUserRestrictions(CALLER_USER_HANDLE), 2322 eq(false)); 2323 parentDpm.clearUserRestriction(admin1, restriction); 2324 DpmTestUtils.assertRestrictions( 2325 DpmTestUtils.newRestrictions(), 2326 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2327 .getParentActiveAdmin() 2328 .getEffectiveRestrictions() 2329 ); 2330 } 2331 addAndRemoveLocalUserRestrictionOnParentDpm(String restriction)2332 private void addAndRemoveLocalUserRestrictionOnParentDpm(String restriction) { 2333 parentDpm.addUserRestriction(admin1, restriction); 2334 verify(getServices().userManagerInternal).setDevicePolicyUserRestrictions( 2335 eq(CALLER_USER_HANDLE), 2336 MockUtils.checkUserRestrictions(), 2337 MockUtils.checkUserRestrictions(UserHandle.USER_SYSTEM, restriction), 2338 eq(false)); 2339 parentDpm.clearUserRestriction(admin1, restriction); 2340 DpmTestUtils.assertRestrictions( 2341 DpmTestUtils.newRestrictions(), 2342 dpms.getProfileOwnerAdminLocked(CALLER_USER_HANDLE) 2343 .getParentActiveAdmin() 2344 .getEffectiveRestrictions() 2345 ); 2346 } 2347 2348 @Test 2349 @Ignore("b/277916462") testNoDefaultEnabledUserRestrictions()2350 public void testNoDefaultEnabledUserRestrictions() throws Exception { 2351 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2352 mContext.callerPermissions.add(permission.MANAGE_USERS); 2353 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 2354 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 2355 2356 // First, set DO. 2357 2358 // Call from a process on the system user. 2359 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2360 2361 // Make sure admin1 is installed on system user. 2362 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 2363 2364 dpm.setActiveAdmin(admin1, /* replace =*/ false, UserHandle.USER_SYSTEM); 2365 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 2366 2367 assertNoDeviceOwnerRestrictions(); 2368 2369 reset(getServices().userManagerInternal); 2370 2371 // Ensure the DISALLOW_REMOVE_MANAGED_PROFILES restriction doesn't show up as a 2372 // restriction to the device owner. 2373 dpm.addUserRestriction(admin1, UserManager.DISALLOW_REMOVE_MANAGED_PROFILE); 2374 assertNoDeviceOwnerRestrictions(); 2375 } 2376 assertNoDeviceOwnerRestrictions()2377 private void assertNoDeviceOwnerRestrictions() { 2378 DpmTestUtils.assertRestrictions( 2379 DpmTestUtils.newRestrictions(), 2380 getDeviceOwner().getEffectiveRestrictions() 2381 ); 2382 } 2383 2384 @Test testSetFactoryResetProtectionPolicyWithDO()2385 public void testSetFactoryResetProtectionPolicyWithDO() throws Exception { 2386 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2387 setupDeviceOwner(); 2388 2389 when(getServices().persistentDataBlockManagerInternal.getAllowedUid()).thenReturn( 2390 DpmMockContext.CALLER_UID); 2391 2392 FactoryResetProtectionPolicy policy = new FactoryResetProtectionPolicy.Builder() 2393 .setFactoryResetProtectionAccounts(new ArrayList<>()) 2394 .setFactoryResetProtectionEnabled(false) 2395 .build(); 2396 dpm.setFactoryResetProtectionPolicy(admin1, policy); 2397 2398 FactoryResetProtectionPolicy result = dpm.getFactoryResetProtectionPolicy(admin1); 2399 assertThat(result).isEqualTo(policy); 2400 assertPoliciesAreEqual(policy, result); 2401 2402 verify(mContext.spiedContext).sendBroadcastAsUser( 2403 MockUtils.checkIntentAction( 2404 DevicePolicyManager.ACTION_RESET_PROTECTION_POLICY_CHANGED), 2405 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 2406 eq(android.Manifest.permission.MANAGE_FACTORY_RESET_PROTECTION)); 2407 } 2408 2409 @Test testSetFactoryResetProtectionPolicyFailWithPO()2410 public void testSetFactoryResetProtectionPolicyFailWithPO() throws Exception { 2411 setupProfileOwner(); 2412 2413 FactoryResetProtectionPolicy policy = new FactoryResetProtectionPolicy.Builder() 2414 .setFactoryResetProtectionEnabled(false) 2415 .build(); 2416 2417 assertExpectException(SecurityException.class, null, 2418 () -> dpm.setFactoryResetProtectionPolicy(admin1, policy)); 2419 } 2420 2421 @Test testSetFactoryResetProtectionPolicyWithPOOfOrganizationOwnedDevice()2422 public void testSetFactoryResetProtectionPolicyWithPOOfOrganizationOwnedDevice() 2423 throws Exception { 2424 setupProfileOwner(); 2425 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 2426 2427 when(getServices().persistentDataBlockManagerInternal.getAllowedUid()).thenReturn( 2428 DpmMockContext.CALLER_UID); 2429 2430 List<String> accounts = new ArrayList<>(); 2431 accounts.add("Account 1"); 2432 accounts.add("Account 2"); 2433 2434 FactoryResetProtectionPolicy policy = new FactoryResetProtectionPolicy.Builder() 2435 .setFactoryResetProtectionAccounts(accounts) 2436 .build(); 2437 2438 dpm.setFactoryResetProtectionPolicy(admin1, policy); 2439 2440 FactoryResetProtectionPolicy result = dpm.getFactoryResetProtectionPolicy(admin1); 2441 assertThat(result).isEqualTo(policy); 2442 assertPoliciesAreEqual(policy, result); 2443 2444 verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( 2445 MockUtils.checkIntentAction( 2446 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 2447 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 2448 eq(null), 2449 any(Bundle.class)); 2450 verify(mContext.spiedContext).sendBroadcastAsUser( 2451 MockUtils.checkIntentAction( 2452 DevicePolicyManager.ACTION_PROFILE_OWNER_CHANGED), 2453 MockUtils.checkUserHandle(CALLER_USER_HANDLE)); 2454 verify(mContext.spiedContext).sendBroadcastAsUser( 2455 MockUtils.checkIntentAction( 2456 DevicePolicyManager.ACTION_RESET_PROTECTION_POLICY_CHANGED), 2457 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 2458 eq(android.Manifest.permission.MANAGE_FACTORY_RESET_PROTECTION)); 2459 } 2460 2461 @Test testGetFactoryResetProtectionPolicyWithFrpManagementAgent()2462 public void testGetFactoryResetProtectionPolicyWithFrpManagementAgent() 2463 throws Exception { 2464 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2465 setupDeviceOwner(); 2466 when(getServices().persistentDataBlockManagerInternal.getAllowedUid()).thenReturn( 2467 DpmMockContext.CALLER_UID); 2468 2469 FactoryResetProtectionPolicy policy = new FactoryResetProtectionPolicy.Builder() 2470 .setFactoryResetProtectionAccounts(new ArrayList<>()) 2471 .setFactoryResetProtectionEnabled(false) 2472 .build(); 2473 2474 dpm.setFactoryResetProtectionPolicy(admin1, policy); 2475 2476 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2477 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 2478 dpm.setActiveAdmin(admin1, /*replace=*/ false); 2479 FactoryResetProtectionPolicy result = dpm.getFactoryResetProtectionPolicy(null); 2480 assertThat(result).isEqualTo(policy); 2481 assertPoliciesAreEqual(policy, result); 2482 2483 verify(mContext.spiedContext).sendBroadcastAsUser( 2484 MockUtils.checkIntentAction( 2485 DevicePolicyManager.ACTION_RESET_PROTECTION_POLICY_CHANGED), 2486 MockUtils.checkUserHandle(CALLER_USER_HANDLE), 2487 eq(android.Manifest.permission.MANAGE_FACTORY_RESET_PROTECTION)); 2488 } 2489 assertPoliciesAreEqual(FactoryResetProtectionPolicy expectedPolicy, FactoryResetProtectionPolicy actualPolicy)2490 private void assertPoliciesAreEqual(FactoryResetProtectionPolicy expectedPolicy, 2491 FactoryResetProtectionPolicy actualPolicy) { 2492 assertThat(actualPolicy.isFactoryResetProtectionEnabled()).isEqualTo( 2493 expectedPolicy.isFactoryResetProtectionEnabled()); 2494 assertAccountsAreEqual(expectedPolicy.getFactoryResetProtectionAccounts(), 2495 actualPolicy.getFactoryResetProtectionAccounts()); 2496 } 2497 assertAccountsAreEqual(List<String> expectedAccounts, List<String> actualAccounts)2498 private void assertAccountsAreEqual(List<String> expectedAccounts, 2499 List<String> actualAccounts) { 2500 assertThat(actualAccounts).containsExactlyElementsIn(expectedAccounts); 2501 } 2502 2503 @Test testSetPermittedInputMethodsWithPOOfOrganizationOwnedDevice()2504 public void testSetPermittedInputMethodsWithPOOfOrganizationOwnedDevice() 2505 throws Exception { 2506 String packageName = "com.google.pkg.one"; 2507 setupProfileOwner(); 2508 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 2509 2510 // Allow all input methods 2511 parentDpm.setPermittedInputMethods(admin1, null); 2512 2513 assertThat(parentDpm.getPermittedInputMethods(admin1)).isNull(); 2514 2515 // Allow only system input methods 2516 parentDpm.setPermittedInputMethods(admin1, new ArrayList<>()); 2517 2518 assertThat(parentDpm.getPermittedInputMethods(admin1)).isEmpty(); 2519 2520 // Don't allow specific third party input methods 2521 final List<String> inputMethods = Collections.singletonList(packageName); 2522 2523 assertExpectException(IllegalArgumentException.class, /* messageRegex= */ "Permitted " 2524 + "input methods must allow all input methods or only system input methods " 2525 + "when called on the parent instance of an organization-owned device", 2526 () -> parentDpm.setPermittedInputMethods(admin1, inputMethods)); 2527 } 2528 2529 @Test testGetProxyParameters()2530 public void testGetProxyParameters() throws Exception { 2531 assertThat(dpm.getProxyParameters(inetAddrProxy("192.0.2.1", 1234), emptyList())) 2532 .isEqualTo(new Pair<>("192.0.2.1:1234", "")); 2533 assertThat(dpm.getProxyParameters(inetAddrProxy("192.0.2.1", 1234), 2534 listOf("one.example.com ", " two.example.com "))) 2535 .isEqualTo(new Pair<>("192.0.2.1:1234", "one.example.com,two.example.com")); 2536 assertThat(dpm.getProxyParameters(hostnameProxy("proxy.example.com", 1234), emptyList())) 2537 .isEqualTo(new Pair<>("proxy.example.com:1234", "")); 2538 assertThat(dpm.getProxyParameters(hostnameProxy("proxy.example.com", 1234), 2539 listOf("excluded.example.com"))) 2540 .isEqualTo(new Pair<>("proxy.example.com:1234", "excluded.example.com")); 2541 2542 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2543 inetAddrProxy("192.0.2.1", 0), emptyList())); 2544 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2545 hostnameProxy("", 1234), emptyList())); 2546 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2547 hostnameProxy("", 0), emptyList())); 2548 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2549 hostnameProxy("invalid! hostname", 1234), emptyList())); 2550 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2551 hostnameProxy("proxy.example.com", 1234), listOf("invalid exclusion"))); 2552 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2553 hostnameProxy("proxy.example.com", -1), emptyList())); 2554 assertThrows(IllegalArgumentException.class, () -> dpm.getProxyParameters( 2555 hostnameProxy("proxy.example.com", 0xFFFF + 1), emptyList())); 2556 } 2557 inetAddrProxy(String inetAddr, int port)2558 private static Proxy inetAddrProxy(String inetAddr, int port) { 2559 return new Proxy( 2560 Proxy.Type.HTTP, new InetSocketAddress(parseNumericAddress(inetAddr), port)); 2561 } 2562 hostnameProxy(String hostname, int port)2563 private static Proxy hostnameProxy(String hostname, int port) { 2564 return new Proxy( 2565 Proxy.Type.HTTP, InetSocketAddress.createUnresolved(hostname, port)); 2566 } 2567 listOf(String... args)2568 private static List<String> listOf(String... args) { 2569 return Arrays.asList(args); 2570 } 2571 2572 @Test testSetKeyguardDisabledFeaturesWithDO()2573 public void testSetKeyguardDisabledFeaturesWithDO() throws Exception { 2574 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2575 setupDeviceOwner(); 2576 2577 dpm.setKeyguardDisabledFeatures(admin1, DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA); 2578 2579 assertThat(dpm.getKeyguardDisabledFeatures(admin1)).isEqualTo( 2580 DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA); 2581 } 2582 2583 @Test testSetKeyguardDisabledFeaturesWithPO()2584 public void testSetKeyguardDisabledFeaturesWithPO() throws Exception { 2585 setupProfileOwner(); 2586 2587 dpm.setKeyguardDisabledFeatures(admin1, DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT); 2588 2589 assertThat(dpm.getKeyguardDisabledFeatures(admin1)).isEqualTo( 2590 DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT); 2591 } 2592 2593 @Test testSetKeyguardDisabledFeaturesWithPOOfOrganizationOwnedDevice()2594 public void testSetKeyguardDisabledFeaturesWithPOOfOrganizationOwnedDevice() 2595 throws Exception { 2596 final int MANAGED_PROFILE_USER_ID = CALLER_USER_HANDLE; 2597 final int MANAGED_PROFILE_ADMIN_UID = 2598 UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); 2599 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 2600 2601 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 2602 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 2603 2604 parentDpm.setKeyguardDisabledFeatures(admin1, 2605 DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA); 2606 2607 assertThat(parentDpm.getKeyguardDisabledFeatures(admin1)).isEqualTo( 2608 DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA); 2609 } 2610 2611 @Test 2612 @Ignore("b/277916462") testSetApplicationHiddenWithDO()2613 public void testSetApplicationHiddenWithDO() throws Exception { 2614 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2615 setupDeviceOwner(); 2616 mContext.packageName = admin1.getPackageName(); 2617 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 2618 mockEmptyPolicyExemptApps(); 2619 2620 String packageName = "com.google.android.test"; 2621 2622 dpm.setApplicationHidden(admin1, packageName, true); 2623 verify(getServices().ipackageManager).setApplicationHiddenSettingAsUser(packageName, 2624 true, UserHandle.USER_SYSTEM); 2625 2626 dpm.setApplicationHidden(admin1, packageName, false); 2627 verify(getServices().ipackageManager).setApplicationHiddenSettingAsUser(packageName, 2628 false, UserHandle.USER_SYSTEM); 2629 2630 verify(getServices().ipackageManager, never()).getPackageInfo(packageName, 2631 PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM); 2632 verify(getServices().ipackageManager, never()).getPackageInfo(packageName, 2633 PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_SYSTEM_ONLY, 2634 UserHandle.USER_SYSTEM); 2635 } 2636 2637 @Test 2638 @Ignore("b/277916462") testSetApplicationHiddenWithPOOfOrganizationOwnedDevice()2639 public void testSetApplicationHiddenWithPOOfOrganizationOwnedDevice() throws Exception { 2640 final int MANAGED_PROFILE_USER_ID = CALLER_USER_HANDLE; 2641 final int MANAGED_PROFILE_ADMIN_UID = 2642 UserHandle.getUid(MANAGED_PROFILE_USER_ID, DpmMockContext.SYSTEM_UID); 2643 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 2644 2645 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 2646 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 2647 mContext.packageName = admin1.getPackageName(); 2648 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 2649 mockEmptyPolicyExemptApps(); 2650 2651 String packageName = "com.google.android.test"; 2652 2653 ApplicationInfo applicationInfo = new ApplicationInfo(); 2654 applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM; 2655 when(getServices().userManager.getProfileParent(MANAGED_PROFILE_USER_ID)) 2656 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 2657 when(getServices().ipackageManager.getApplicationInfo(packageName, 2658 PackageManager.MATCH_UNINSTALLED_PACKAGES, UserHandle.USER_SYSTEM)).thenReturn( 2659 applicationInfo); 2660 2661 parentDpm.setApplicationHidden(admin1, packageName, true); 2662 verify(getServices().ipackageManager).setApplicationHiddenSettingAsUser(packageName, 2663 true, UserHandle.USER_SYSTEM); 2664 2665 parentDpm.setApplicationHidden(admin1, packageName, false); 2666 verify(getServices().ipackageManager).setApplicationHiddenSettingAsUser(packageName, 2667 false, UserHandle.USER_SYSTEM); 2668 } 2669 2670 @Test testGetMacAddress()2671 public void testGetMacAddress() throws Exception { 2672 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2673 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 2674 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 2675 2676 // In this test, change the caller user to "system". 2677 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2678 2679 // Make sure admin1 is installed on system user. 2680 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 2681 2682 // Test 1. Caller doesn't have DO or DA. 2683 assertExpectException(SecurityException.class, /* messageRegex= */ 2684 "does not exist or is not owned by uid", () -> dpm.getWifiMacAddress(admin1)); 2685 2686 // DO needs to be an DA. 2687 dpm.setActiveAdmin(admin1, /* replace =*/ false); 2688 assertThat(dpm.isAdminActive(admin1)).isTrue(); 2689 2690 // Test 2. Caller has DA, but not DO. 2691 assertExpectException(SecurityException.class, 2692 /* messageRegex= */ INVALID_CALLING_IDENTITY_MSG, 2693 () -> dpm.getWifiMacAddress(admin1)); 2694 2695 // Test 3. Caller has PO, but not DO. 2696 assertThat(dpm.setProfileOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 2697 assertExpectException(SecurityException.class, 2698 /* messageRegex= */ INVALID_CALLING_IDENTITY_MSG, 2699 () -> dpm.getWifiMacAddress(admin1)); 2700 2701 // Remove PO. 2702 dpm.clearProfileOwner(admin1); 2703 dpm.setActiveAdmin(admin1, false); 2704 // Test 4, Caller is DO now. 2705 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 2706 2707 // 4-1. But WifiManager is not ready. 2708 assertThat(dpm.getWifiMacAddress(admin1)).isNull(); 2709 2710 // 4-2. When WifiManager returns an empty array, dpm should also output null. 2711 when(getServices().wifiManager.getFactoryMacAddresses()).thenReturn(new String[0]); 2712 assertThat(dpm.getWifiMacAddress(admin1)).isNull(); 2713 2714 // 4-3. With a real MAC address. 2715 final String[] macAddresses = new String[]{"11:22:33:44:55:66"}; 2716 when(getServices().wifiManager.getFactoryMacAddresses()).thenReturn(macAddresses); 2717 assertThat(dpm.getWifiMacAddress(admin1)).isEqualTo("11:22:33:44:55:66"); 2718 } 2719 2720 @Test testGetMacAddressByOrgOwnedPO()2721 public void testGetMacAddressByOrgOwnedPO() throws Exception { 2722 setupProfileOwner(); 2723 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 2724 2725 final String[] macAddresses = new String[]{"11:22:33:44:55:66"}; 2726 when(getServices().wifiManager.getFactoryMacAddresses()).thenReturn(macAddresses); 2727 assertThat(dpm.getWifiMacAddress(admin1)).isEqualTo("11:22:33:44:55:66"); 2728 } 2729 2730 @Test testReboot()2731 public void testReboot() throws Exception { 2732 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2733 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 2734 2735 // In this test, change the caller user to "system". 2736 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2737 2738 // Make sure admin1 is installed on system user. 2739 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 2740 2741 // Set admin1 as DA. 2742 dpm.setActiveAdmin(admin1, false); 2743 assertThat(dpm.isAdminActive(admin1)).isTrue(); 2744 assertExpectException(SecurityException.class, /* messageRegex= */ 2745 INVALID_CALLING_IDENTITY_MSG, () -> dpm.reboot(admin1)); 2746 2747 // Set admin1 as PO. 2748 assertThat(dpm.setProfileOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 2749 assertExpectException(SecurityException.class, /* messageRegex= */ 2750 INVALID_CALLING_IDENTITY_MSG, () -> dpm.reboot(admin1)); 2751 2752 // Remove PO and add DO. 2753 dpm.clearProfileOwner(admin1); 2754 dpm.setActiveAdmin(admin1, false); 2755 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 2756 2757 // admin1 is DO. 2758 // Set current call state of device to ringing. 2759 when(getServices().telephonyManager.getCallState()) 2760 .thenReturn(TelephonyManager.CALL_STATE_RINGING); 2761 assertExpectException(IllegalStateException.class, /* messageRegex= */ ONGOING_CALL_MSG, 2762 () -> dpm.reboot(admin1)); 2763 2764 // Set current call state of device to dialing/active. 2765 when(getServices().telephonyManager.getCallState()) 2766 .thenReturn(TelephonyManager.CALL_STATE_OFFHOOK); 2767 assertExpectException(IllegalStateException.class, /* messageRegex= */ ONGOING_CALL_MSG, 2768 () -> dpm.reboot(admin1)); 2769 2770 // Set current call state of device to idle. 2771 when(getServices().telephonyManager.getCallState()) 2772 .thenReturn(TelephonyManager.CALL_STATE_IDLE); 2773 dpm.reboot(admin1); 2774 } 2775 2776 @Test testSetGetSupportText()2777 public void testSetGetSupportText() { 2778 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2779 dpm.setActiveAdmin(admin1, true); 2780 dpm.setActiveAdmin(admin2, true); 2781 mContext.callerPermissions.remove(permission.MANAGE_DEVICE_ADMINS); 2782 2783 // Null default support messages. 2784 { 2785 assertThat(dpm.getLongSupportMessage(admin1)).isNull(); 2786 assertThat(dpm.getShortSupportMessage(admin1)).isNull(); 2787 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 2788 assertThat(dpm.getShortSupportMessageForUser(admin1, CALLER_USER_HANDLE)).isNull(); 2789 assertThat(dpm.getLongSupportMessageForUser(admin1, CALLER_USER_HANDLE)).isNull(); 2790 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; 2791 } 2792 2793 // Only system can call the per user versions. 2794 { 2795 assertExpectException(SecurityException.class, /* messageRegex= */ "message for user", 2796 () -> dpm.getShortSupportMessageForUser(admin1, CALLER_USER_HANDLE)); 2797 assertExpectException(SecurityException.class, /* messageRegex= */ "message for user", 2798 () -> dpm.getLongSupportMessageForUser(admin1, CALLER_USER_HANDLE)); 2799 } 2800 2801 // Can't set message for admin in another uid. 2802 { 2803 mContext.binder.callingUid = DpmMockContext.CALLER_UID + 1; 2804 assertThrows(SecurityException.class, 2805 () -> dpm.setShortSupportMessage(admin1, "Some text")); 2806 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 2807 } 2808 2809 // Set/Get short returns what it sets and other admins text isn't changed. 2810 { 2811 final String supportText = "Some text to test with."; 2812 dpm.setShortSupportMessage(admin1, supportText); 2813 assertThat(dpm.getShortSupportMessage(admin1)).isEqualTo(supportText); 2814 assertThat(dpm.getLongSupportMessage(admin1)).isNull(); 2815 assertThat(dpm.getShortSupportMessage(admin2)).isNull(); 2816 2817 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 2818 assertThat(dpm.getShortSupportMessageForUser(admin1, 2819 CALLER_USER_HANDLE)).isEqualTo(supportText); 2820 assertThat(dpm.getShortSupportMessageForUser(admin2, CALLER_USER_HANDLE)).isNull(); 2821 assertThat(dpm.getLongSupportMessageForUser(admin1, CALLER_USER_HANDLE)).isNull(); 2822 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; 2823 2824 dpm.setShortSupportMessage(admin1, null); 2825 assertThat(dpm.getShortSupportMessage(admin1)).isNull(); 2826 } 2827 2828 // Set/Get long returns what it sets and other admins text isn't changed. 2829 { 2830 final String supportText = "Some text to test with.\nWith more text."; 2831 dpm.setLongSupportMessage(admin1, supportText); 2832 assertThat(dpm.getLongSupportMessage(admin1)).isEqualTo(supportText); 2833 assertThat(dpm.getShortSupportMessage(admin1)).isNull(); 2834 assertThat(dpm.getLongSupportMessage(admin2)).isNull(); 2835 2836 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 2837 assertThat(dpm.getLongSupportMessageForUser(admin1, 2838 CALLER_USER_HANDLE)).isEqualTo(supportText); 2839 assertThat(dpm.getLongSupportMessageForUser(admin2, CALLER_USER_HANDLE)).isNull(); 2840 assertThat(dpm.getShortSupportMessageForUser(admin1, CALLER_USER_HANDLE)).isNull(); 2841 mMockContext.binder.callingUid = DpmMockContext.CALLER_UID; 2842 2843 dpm.setLongSupportMessage(admin1, null); 2844 assertThat(dpm.getLongSupportMessage(admin1)).isNull(); 2845 } 2846 } 2847 2848 @Test testSetGetMeteredDataDisabledPackages()2849 public void testSetGetMeteredDataDisabledPackages() throws Exception { 2850 setAsProfileOwner(admin1); 2851 2852 assertThat(dpm.getMeteredDataDisabledPackages(admin1)).isEmpty(); 2853 2854 // Setup 2855 final ArrayList<String> pkgsToRestrict = new ArrayList<>(); 2856 final String package1 = "com.example.one"; 2857 final String package2 = "com.example.two"; 2858 pkgsToRestrict.add(package1); 2859 pkgsToRestrict.add(package2); 2860 setupPackageInPackageManager(package1, CALLER_USER_HANDLE, 123, 0); 2861 setupPackageInPackageManager(package2, CALLER_USER_HANDLE, 456, 0); 2862 List<String> excludedPkgs = dpm.setMeteredDataDisabledPackages(admin1, pkgsToRestrict); 2863 2864 // Verify 2865 assertThat(excludedPkgs).isEmpty(); 2866 assertThat(dpm.getMeteredDataDisabledPackages(admin1)).isEqualTo(pkgsToRestrict); 2867 verify(getServices().networkPolicyManagerInternal).setMeteredRestrictedPackages( 2868 MockUtils.checkApps(pkgsToRestrict.toArray(new String[0])), 2869 eq(CALLER_USER_HANDLE)); 2870 2871 // Setup 2872 pkgsToRestrict.remove(package1); 2873 excludedPkgs = dpm.setMeteredDataDisabledPackages(admin1, pkgsToRestrict); 2874 2875 // Verify 2876 assertThat(excludedPkgs).isEmpty(); 2877 assertThat(dpm.getMeteredDataDisabledPackages(admin1)).isEqualTo(pkgsToRestrict); 2878 verify(getServices().networkPolicyManagerInternal).setMeteredRestrictedPackages( 2879 MockUtils.checkApps(pkgsToRestrict.toArray(new String[0])), 2880 eq(CALLER_USER_HANDLE)); 2881 } 2882 2883 @Test testSetGetMeteredDataDisabledPackages_deviceAdmin()2884 public void testSetGetMeteredDataDisabledPackages_deviceAdmin() { 2885 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 2886 dpm.setActiveAdmin(admin1, true); 2887 assertThat(dpm.isAdminActive(admin1)).isTrue(); 2888 mContext.callerPermissions.remove(permission.MANAGE_DEVICE_ADMINS); 2889 2890 assertExpectException(SecurityException.class, /* messageRegex= */ NOT_PROFILE_OWNER_MSG, 2891 () -> dpm.setMeteredDataDisabledPackages(admin1, new ArrayList<>())); 2892 assertExpectException(SecurityException.class, /* messageRegex= */ NOT_PROFILE_OWNER_MSG, 2893 () -> dpm.getMeteredDataDisabledPackages(admin1)); 2894 } 2895 2896 @Test testIsMeteredDataDisabledForUserPackage()2897 public void testIsMeteredDataDisabledForUserPackage() throws Exception { 2898 setAsProfileOwner(admin1); 2899 2900 // Setup 2901 final ArrayList<String> pkgsToRestrict = new ArrayList<>(); 2902 final String package1 = "com.example.one"; 2903 final String package2 = "com.example.two"; 2904 final String package3 = "com.example.three"; 2905 pkgsToRestrict.add(package1); 2906 pkgsToRestrict.add(package2); 2907 setupPackageInPackageManager(package1, CALLER_USER_HANDLE, 123, 0); 2908 setupPackageInPackageManager(package2, CALLER_USER_HANDLE, 456, 0); 2909 List<String> excludedPkgs = dpm.setMeteredDataDisabledPackages(admin1, pkgsToRestrict); 2910 2911 // Verify 2912 assertThat(excludedPkgs).isEmpty(); 2913 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 2914 assertWithMessage("%s should be restricted", package1) 2915 .that(dpm.isMeteredDataDisabledPackageForUser(admin1, package1, CALLER_USER_HANDLE)) 2916 .isTrue(); 2917 assertWithMessage("%s should be restricted", package2) 2918 .that(dpm.isMeteredDataDisabledPackageForUser(admin1, package2, CALLER_USER_HANDLE)) 2919 .isTrue(); 2920 assertWithMessage("%s should not be restricted", package3) 2921 .that(dpm.isMeteredDataDisabledPackageForUser(admin1, package3, CALLER_USER_HANDLE)) 2922 .isFalse(); 2923 } 2924 2925 @Test testIsMeteredDataDisabledForUserPackage_nonSystemUidCaller()2926 public void testIsMeteredDataDisabledForUserPackage_nonSystemUidCaller() throws Exception { 2927 setAsProfileOwner(admin1); 2928 assertExpectException(SecurityException.class, 2929 /* messageRegex= */ "Only the system can query restricted pkgs", 2930 () -> dpm.isMeteredDataDisabledPackageForUser( 2931 admin1, "com.example.one", CALLER_USER_HANDLE)); 2932 dpm.clearProfileOwner(admin1); 2933 2934 setDeviceOwner(); 2935 assertExpectException(SecurityException.class, 2936 /* messageRegex= */ "Only the system can query restricted pkgs", 2937 () -> dpm.isMeteredDataDisabledPackageForUser( 2938 admin1, "com.example.one", CALLER_USER_HANDLE)); 2939 clearDeviceOwner(); 2940 } 2941 2942 @Test 2943 @Ignore("b/277916462") testCreateAdminSupportIntent()2944 public void testCreateAdminSupportIntent() throws Exception { 2945 // Setup device owner. 2946 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 2947 setupDeviceOwner(); 2948 2949 // Nonexisting permission returns null 2950 Intent intent = dpm.createAdminSupportIntent("disallow_nothing"); 2951 assertThat(intent).isNull(); 2952 2953 // Existing permission that is not set returns null 2954 intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); 2955 assertThat(intent).isNull(); 2956 2957 // Existing permission that is not set by device/profile owner returns null 2958 when(getServices().userManager.hasUserRestriction( 2959 eq(UserManager.DISALLOW_ADJUST_VOLUME), 2960 eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) 2961 .thenReturn(true); 2962 intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); 2963 assertThat(intent).isNull(); 2964 2965 // UM.getUserRestrictionSources() will return a list of size 1 with the caller resource. 2966 doAnswer((Answer<List<UserManager.EnforcingUser>>) invocation -> Collections.singletonList( 2967 new UserManager.EnforcingUser( 2968 UserHandle.USER_SYSTEM, 2969 UserManager.RESTRICTION_SOURCE_DEVICE_OWNER)) 2970 ).when(getServices().userManager).getUserRestrictionSources( 2971 eq(UserManager.DISALLOW_ADJUST_VOLUME), 2972 eq(UserHandle.of(UserHandle.USER_SYSTEM))); 2973 intent = dpm.createAdminSupportIntent(UserManager.DISALLOW_ADJUST_VOLUME); 2974 assertThat(intent).isNotNull(); 2975 assertThat(intent.getAction()).isEqualTo(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS); 2976 assertThat(intent.getIntExtra(Intent.EXTRA_USER_ID, -1)) 2977 .isEqualTo(UserHandle.getUserId(DpmMockContext.CALLER_SYSTEM_USER_UID)); 2978 assertThat(intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)) 2979 .isEqualTo(UserManager.DISALLOW_ADJUST_VOLUME); 2980 2981 // Try with POLICY_DISABLE_CAMERA and POLICY_DISABLE_SCREEN_CAPTURE, which are not 2982 // user restrictions 2983 2984 // Camera is not disabled 2985 intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); 2986 assertThat(intent).isNull(); 2987 2988 // Camera is disabled 2989 dpm.setCameraDisabled(admin1, true); 2990 intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); 2991 assertThat(intent).isNotNull(); 2992 assertThat(intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)) 2993 .isEqualTo(DevicePolicyManager.POLICY_DISABLE_CAMERA); 2994 2995 // Screen capture is not disabled 2996 intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); 2997 assertThat(intent).isNull(); 2998 2999 // Screen capture is disabled 3000 dpm.setScreenCaptureDisabled(admin1, true); 3001 intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); 3002 assertThat(intent).isNotNull(); 3003 assertThat(intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)) 3004 .isEqualTo(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); 3005 3006 // Same checks for different user 3007 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 3008 // Camera should be disabled by device owner 3009 intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_CAMERA); 3010 assertThat(intent).isNotNull(); 3011 assertThat(intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION)) 3012 .isEqualTo(DevicePolicyManager.POLICY_DISABLE_CAMERA); 3013 assertThat(intent.getIntExtra(Intent.EXTRA_USER_ID, -1)) 3014 .isEqualTo(UserHandle.getUserId(DpmMockContext.CALLER_UID)); 3015 // ScreenCapture should not be disabled by device owner 3016 intent = dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); 3017 assertThat(intent).isNull(); 3018 } 3019 3020 /** 3021 * Test for: 3022 * {@link DevicePolicyManager#setAffiliationIds} 3023 * {@link DevicePolicyManager#getAffiliationIds} 3024 * {@link DevicePolicyManager#isAffiliatedUser} 3025 */ 3026 @Test testUserAffiliation()3027 public void testUserAffiliation() throws Exception { 3028 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 3029 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3030 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL); 3031 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 3032 3033 // Check that the system user is unaffiliated. 3034 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3035 assertThat(dpm.isAffiliatedUser()).isFalse(); 3036 3037 // Set a device owner on the system user. Check that the system user becomes affiliated. 3038 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 3039 dpm.setActiveAdmin(admin1, /* replace =*/ false); 3040 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 3041 assertThat(dpm.isAffiliatedUser()).isTrue(); 3042 assertThat(dpm.getAffiliationIds(admin1).isEmpty()).isTrue(); 3043 3044 // Install a profile owner. Check that the test user is unaffiliated. 3045 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 3046 setAsProfileOwner(admin2); 3047 assertThat(dpm.isAffiliatedUser()).isFalse(); 3048 assertThat(dpm.getAffiliationIds(admin2).isEmpty()).isTrue(); 3049 3050 // Have the profile owner specify a set of affiliation ids. Check that the test user remains 3051 // unaffiliated. 3052 final Set<String> userAffiliationIds = new ArraySet<>(); 3053 userAffiliationIds.add("red"); 3054 userAffiliationIds.add("green"); 3055 userAffiliationIds.add("blue"); 3056 dpm.setAffiliationIds(admin2, userAffiliationIds); 3057 MoreAsserts.assertContentsInAnyOrder(dpm.getAffiliationIds(admin2), "red", "green", "blue"); 3058 assertThat(dpm.isAffiliatedUser()).isFalse(); 3059 3060 // Have the device owner specify a set of affiliation ids that do not intersect with those 3061 // specified by the profile owner. Check that the test user remains unaffiliated. 3062 final Set<String> deviceAffiliationIds = new ArraySet<>(); 3063 deviceAffiliationIds.add("cyan"); 3064 deviceAffiliationIds.add("yellow"); 3065 deviceAffiliationIds.add("magenta"); 3066 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3067 dpm.setAffiliationIds(admin1, deviceAffiliationIds); 3068 MoreAsserts.assertContentsInAnyOrder( 3069 dpm.getAffiliationIds(admin1), "cyan", "yellow", "magenta"); 3070 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 3071 assertThat(dpm.isAffiliatedUser()).isFalse(); 3072 3073 // Have the profile owner specify a set of affiliation ids that intersect with those 3074 // specified by the device owner. Check that the test user becomes affiliated. 3075 userAffiliationIds.add("yellow"); 3076 dpm.setAffiliationIds(admin2, userAffiliationIds); 3077 MoreAsserts.assertContentsInAnyOrder( 3078 dpm.getAffiliationIds(admin2), "red", "green", "blue", "yellow"); 3079 assertThat(dpm.isAffiliatedUser()).isTrue(); 3080 3081 // Clear affiliation ids for the profile owner. The user becomes unaffiliated. 3082 dpm.setAffiliationIds(admin2, Collections.emptySet()); 3083 assertThat(dpm.getAffiliationIds(admin2).isEmpty()).isTrue(); 3084 assertThat(dpm.isAffiliatedUser()).isFalse(); 3085 3086 // Set affiliation ids again, then clear PO to check that the user becomes unaffiliated 3087 dpm.setAffiliationIds(admin2, userAffiliationIds); 3088 assertThat(dpm.isAffiliatedUser()).isTrue(); 3089 dpm.clearProfileOwner(admin2); 3090 assertThat(dpm.isAffiliatedUser()).isFalse(); 3091 3092 // Check that the system user remains affiliated. 3093 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3094 assertThat(dpm.isAffiliatedUser()).isTrue(); 3095 3096 // Clear the device owner - the user becomes unaffiliated. 3097 clearDeviceOwner(); 3098 assertThat(dpm.isAffiliatedUser()).isFalse(); 3099 } 3100 3101 @Test testGetUserProvisioningState_defaultResult()3102 public void testGetUserProvisioningState_defaultResult() { 3103 mContext.callerPermissions.add(permission.MANAGE_USERS); 3104 assertThat(dpm.getUserProvisioningState()) 3105 .isEqualTo(DevicePolicyManager.STATE_USER_UNMANAGED); 3106 } 3107 3108 @Test testSetUserProvisioningState_permission()3109 public void testSetUserProvisioningState_permission() throws Exception { 3110 setupProfileOwner(); 3111 3112 exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3113 DevicePolicyManager.STATE_USER_SETUP_FINALIZED); 3114 } 3115 3116 @Test testSetUserProvisioningState_unprivileged()3117 public void testSetUserProvisioningState_unprivileged() throws Exception { 3118 setupProfileOwner(); 3119 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 3120 () -> dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED, 3121 CALLER_USER_HANDLE)); 3122 } 3123 3124 @Test testSetUserProvisioningState_noManagement()3125 public void testSetUserProvisioningState_noManagement() { 3126 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3127 mContext.callerPermissions.add(permission.MANAGE_USERS); 3128 assertExpectException(IllegalStateException.class, 3129 /* messageRegex= */ "change provisioning state unless a .* owner is set", 3130 () -> dpm.setUserProvisioningState(DevicePolicyManager.STATE_USER_SETUP_FINALIZED, 3131 CALLER_USER_HANDLE)); 3132 assertThat(dpm.getUserProvisioningState()) 3133 .isEqualTo(DevicePolicyManager.STATE_USER_UNMANAGED); 3134 } 3135 3136 @Test testSetUserProvisioningState_deviceOwnerFromSetupWizard()3137 public void testSetUserProvisioningState_deviceOwnerFromSetupWizard() throws Exception { 3138 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3139 setupDeviceOwner(); 3140 3141 exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, 3142 DevicePolicyManager.STATE_USER_SETUP_COMPLETE, 3143 DevicePolicyManager.STATE_USER_SETUP_FINALIZED); 3144 } 3145 3146 @Test testSetUserProvisioningState_deviceOwnerFromSetupWizardAlternative()3147 public void testSetUserProvisioningState_deviceOwnerFromSetupWizardAlternative() 3148 throws Exception { 3149 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3150 setupDeviceOwner(); 3151 3152 exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, 3153 DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE, 3154 DevicePolicyManager.STATE_USER_SETUP_FINALIZED); 3155 } 3156 3157 @Test testSetUserProvisioningState_deviceOwnerWithoutSetupWizard()3158 public void testSetUserProvisioningState_deviceOwnerWithoutSetupWizard() throws Exception { 3159 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3160 setupDeviceOwner(); 3161 3162 exerciseUserProvisioningTransitions(UserHandle.USER_SYSTEM, 3163 DevicePolicyManager.STATE_USER_SETUP_FINALIZED); 3164 } 3165 3166 @Test testSetUserProvisioningState_managedProfileFromSetupWizard_primaryUser()3167 public void testSetUserProvisioningState_managedProfileFromSetupWizard_primaryUser() 3168 throws Exception { 3169 setupProfileOwner(); 3170 3171 exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3172 DevicePolicyManager.STATE_USER_PROFILE_COMPLETE, 3173 DevicePolicyManager.STATE_USER_PROFILE_FINALIZED); 3174 } 3175 3176 @Test testSetUserProvisioningState_managedProfileFromSetupWizard_managedProfile()3177 public void testSetUserProvisioningState_managedProfileFromSetupWizard_managedProfile() 3178 throws Exception { 3179 setupProfileOwner(); 3180 3181 exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3182 DevicePolicyManager.STATE_USER_SETUP_COMPLETE, 3183 DevicePolicyManager.STATE_USER_SETUP_FINALIZED); 3184 } 3185 3186 @Test testSetUserProvisioningState_managedProfileWithoutSetupWizard()3187 public void testSetUserProvisioningState_managedProfileWithoutSetupWizard() throws Exception { 3188 setupProfileOwner(); 3189 3190 exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3191 DevicePolicyManager.STATE_USER_SETUP_FINALIZED); 3192 } 3193 3194 @Test testSetUserProvisioningState_illegalTransitionOutOfFinalized1()3195 public void testSetUserProvisioningState_illegalTransitionOutOfFinalized1() throws Exception { 3196 setupProfileOwner(); 3197 3198 assertExpectException(IllegalStateException.class, 3199 /* messageRegex= */ "Cannot move to user provisioning state", 3200 () -> exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3201 DevicePolicyManager.STATE_USER_SETUP_FINALIZED, 3202 DevicePolicyManager.STATE_USER_UNMANAGED)); 3203 } 3204 3205 @Test testSetUserProvisioningState_profileFinalized_canTransitionToUserUnmanaged()3206 public void testSetUserProvisioningState_profileFinalized_canTransitionToUserUnmanaged() 3207 throws Exception { 3208 setupProfileOwner(); 3209 3210 exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3211 DevicePolicyManager.STATE_USER_PROFILE_FINALIZED, 3212 DevicePolicyManager.STATE_USER_UNMANAGED); 3213 } 3214 3215 @Test testSetUserProvisioningState_illegalTransitionToAnotherInProgressState()3216 public void testSetUserProvisioningState_illegalTransitionToAnotherInProgressState() 3217 throws Exception { 3218 setupProfileOwner(); 3219 3220 assertExpectException(IllegalStateException.class, 3221 /* messageRegex= */ "Cannot move to user provisioning state", 3222 () -> exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, 3223 DevicePolicyManager.STATE_USER_SETUP_INCOMPLETE, 3224 DevicePolicyManager.STATE_USER_SETUP_COMPLETE)); 3225 } 3226 exerciseUserProvisioningTransitions(int userId, int... states)3227 private void exerciseUserProvisioningTransitions(int userId, int... states) { 3228 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3229 mContext.callerPermissions.add(permission.MANAGE_USERS); 3230 3231 assertThat(dpm.getUserProvisioningState()) 3232 .isEqualTo(DevicePolicyManager.STATE_USER_UNMANAGED); 3233 for (int state : states) { 3234 dpm.setUserProvisioningState(state, userId); 3235 assertThat(dpm.getUserProvisioningState()).isEqualTo(state); 3236 } 3237 } 3238 setupProfileOwner()3239 private void setupProfileOwner() throws Exception { 3240 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); 3241 3242 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_UID); 3243 dpm.setActiveAdmin(admin1, false); 3244 assertThat(dpm.setProfileOwner(admin1, CALLER_USER_HANDLE)).isTrue(); 3245 3246 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); 3247 } 3248 setupProfileOwnerOnUser0()3249 private void setupProfileOwnerOnUser0() throws Exception { 3250 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); 3251 3252 setUpPackageManagerForAdmin(admin1, DpmMockContext.SYSTEM_UID); 3253 dpm.setActiveAdmin(admin1, false); 3254 assertThat(dpm.setProfileOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 3255 3256 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); 3257 } 3258 setupDeviceOwner()3259 private void setupDeviceOwner() throws Exception { 3260 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); 3261 3262 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 3263 dpm.setActiveAdmin(admin1, false); 3264 assertThat(dpm.setDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 3265 3266 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); 3267 } 3268 3269 @Test testSetMaximumTimeToLock()3270 public void testSetMaximumTimeToLock() { 3271 mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); 3272 3273 dpm.setActiveAdmin(admin1, /* replace =*/ false); 3274 dpm.setActiveAdmin(admin2, /* replace =*/ false); 3275 3276 reset(getServices().powerManagerInternal); 3277 reset(getServices().settings); 3278 3279 dpm.setMaximumTimeToLock(admin1, 0); 3280 verifyScreenTimeoutCall(null, CALLER_USER_HANDLE); 3281 verifyStayOnWhilePluggedCleared(false); 3282 reset(getServices().powerManagerInternal); 3283 reset(getServices().settings); 3284 3285 dpm.setMaximumTimeToLock(admin1, 1); 3286 verifyScreenTimeoutCall(1L, CALLER_USER_HANDLE); 3287 verifyStayOnWhilePluggedCleared(true); 3288 reset(getServices().powerManagerInternal); 3289 reset(getServices().settings); 3290 3291 dpm.setMaximumTimeToLock(admin2, 10); 3292 verifyScreenTimeoutCall(null, CALLER_USER_HANDLE); 3293 verifyStayOnWhilePluggedCleared(false); 3294 reset(getServices().powerManagerInternal); 3295 reset(getServices().settings); 3296 3297 dpm.setMaximumTimeToLock(admin1, 5); 3298 verifyScreenTimeoutCall(5L, CALLER_USER_HANDLE); 3299 verifyStayOnWhilePluggedCleared(true); 3300 reset(getServices().powerManagerInternal); 3301 reset(getServices().settings); 3302 3303 dpm.setMaximumTimeToLock(admin2, 4); 3304 verifyScreenTimeoutCall(4L, CALLER_USER_HANDLE); 3305 verifyStayOnWhilePluggedCleared(true); 3306 reset(getServices().powerManagerInternal); 3307 reset(getServices().settings); 3308 3309 dpm.setMaximumTimeToLock(admin1, 0); 3310 reset(getServices().powerManagerInternal); 3311 reset(getServices().settings); 3312 3313 dpm.setMaximumTimeToLock(admin2, Long.MAX_VALUE); 3314 verifyScreenTimeoutCall(Long.MAX_VALUE, CALLER_USER_HANDLE); 3315 verifyStayOnWhilePluggedCleared(true); 3316 reset(getServices().powerManagerInternal); 3317 reset(getServices().settings); 3318 3319 dpm.setMaximumTimeToLock(admin2, 10); 3320 verifyScreenTimeoutCall(10L, CALLER_USER_HANDLE); 3321 verifyStayOnWhilePluggedCleared(true); 3322 reset(getServices().powerManagerInternal); 3323 reset(getServices().settings); 3324 3325 // There's no restriction; should be set to MAX. 3326 dpm.setMaximumTimeToLock(admin2, 0); 3327 verifyScreenTimeoutCall(Long.MAX_VALUE, CALLER_USER_HANDLE); 3328 verifyStayOnWhilePluggedCleared(false); 3329 } 3330 3331 @Test testSupervisionConfig()3332 public void testSupervisionConfig() throws Exception { 3333 final int uid = UserHandle.getUid(15, 19436); 3334 addManagedProfile(admin1, uid, admin1); 3335 mContext.binder.callingUid = uid; 3336 3337 verifySupervisionConfig(uid, null, null); 3338 verifySupervisionConfig(uid, "", null); 3339 verifySupervisionConfig(uid, null, ""); 3340 verifySupervisionConfig(uid, "", ""); 3341 3342 verifySupervisionConfig(uid, admin1.flattenToString(), null); 3343 verifySupervisionConfig(uid, admin1.flattenToString(), ""); 3344 3345 verifySupervisionConfig(uid, null, admin1.getPackageName()); 3346 verifySupervisionConfig(uid, "", admin1.getPackageName()); 3347 } 3348 verifySupervisionConfig( int uid , String configComponentName, String configPackageName)3349 private void verifySupervisionConfig( 3350 int uid , String configComponentName, String configPackageName) { 3351 final boolean isAdmin = admin1.flattenToString().equals(configComponentName) 3352 || admin1.getPackageName().equals(configPackageName); 3353 3354 final UserHandle user = UserHandle.getUserHandleForUid(uid); 3355 final DevicePolicyManagerInternal dpmi = 3356 LocalServices.getService(DevicePolicyManagerInternal.class); 3357 3358 when(mServiceContext.resources 3359 .getString(R.string.config_defaultSupervisionProfileOwnerComponent)) 3360 .thenReturn(configComponentName); 3361 3362 when(mServiceContext.resources 3363 .getString(R.string.config_systemSupervision)) 3364 .thenReturn(configPackageName); 3365 3366 if (isAdmin) { 3367 assertThat(dpmi.isActiveSupervisionApp(uid)).isTrue(); 3368 assertThat(dpm.getProfileOwnerOrDeviceOwnerSupervisionComponent(user)) 3369 .isEqualTo(admin1); 3370 assertThat(dpm.isSupervisionComponent(admin1)).isTrue(); 3371 } else { 3372 assertThat(dpmi.isActiveSupervisionApp(uid)).isFalse(); 3373 assertThat(dpm.getProfileOwnerOrDeviceOwnerSupervisionComponent(user)).isNull(); 3374 assertThat(dpm.isSupervisionComponent(admin1)).isFalse(); 3375 } 3376 } 3377 3378 // Test if lock timeout on managed profile is handled correctly depending on whether profile 3379 // uses separate challenge. 3380 @Test testSetMaximumTimeToLockProfile()3381 public void testSetMaximumTimeToLockProfile() throws Exception { 3382 final int PROFILE_USER = 15; 3383 final int PROFILE_ADMIN = UserHandle.getUid(PROFILE_USER, 19436); 3384 addManagedProfile(admin1, PROFILE_ADMIN, admin1); 3385 mContext.binder.callingUid = PROFILE_ADMIN; 3386 final DevicePolicyManagerInternal dpmi = 3387 LocalServices.getService(DevicePolicyManagerInternal.class); 3388 3389 dpm.setMaximumTimeToLock(admin1, 0); 3390 3391 reset(getServices().powerManagerInternal); 3392 reset(getServices().settings); 3393 3394 // First add timeout for the profile. 3395 dpm.setMaximumTimeToLock(admin1, 10); 3396 verifyScreenTimeoutCall(10L, UserHandle.USER_SYSTEM); 3397 3398 reset(getServices().powerManagerInternal); 3399 reset(getServices().settings); 3400 3401 // Add separate challenge 3402 when(getServices().lockPatternUtils 3403 .isSeparateProfileChallengeEnabled(eq(PROFILE_USER))).thenReturn(true); 3404 dpmi.reportSeparateProfileChallengeChanged(PROFILE_USER); 3405 3406 verifyScreenTimeoutCall(10L, PROFILE_USER); 3407 verifyScreenTimeoutCall(Long.MAX_VALUE, UserHandle.USER_SYSTEM); 3408 3409 reset(getServices().powerManagerInternal); 3410 reset(getServices().settings); 3411 3412 // Remove the timeout. 3413 dpm.setMaximumTimeToLock(admin1, 0); 3414 verifyScreenTimeoutCall(Long.MAX_VALUE, PROFILE_USER); 3415 verifyScreenTimeoutCall(null , UserHandle.USER_SYSTEM); 3416 3417 reset(getServices().powerManagerInternal); 3418 reset(getServices().settings); 3419 3420 // Add it back. 3421 dpm.setMaximumTimeToLock(admin1, 10); 3422 verifyScreenTimeoutCall(10L, PROFILE_USER); 3423 verifyScreenTimeoutCall(null, UserHandle.USER_SYSTEM); 3424 3425 reset(getServices().powerManagerInternal); 3426 reset(getServices().settings); 3427 3428 // Remove separate challenge. 3429 reset(getServices().lockPatternUtils); 3430 when(getServices().lockPatternUtils 3431 .isSeparateProfileChallengeEnabled(eq(PROFILE_USER))).thenReturn(false); 3432 dpmi.reportSeparateProfileChallengeChanged(PROFILE_USER); 3433 when(getServices().lockPatternUtils.hasSecureLockScreen()).thenReturn(true); 3434 3435 verifyScreenTimeoutCall(Long.MAX_VALUE, PROFILE_USER); 3436 verifyScreenTimeoutCall(10L , UserHandle.USER_SYSTEM); 3437 3438 reset(getServices().powerManagerInternal); 3439 reset(getServices().settings); 3440 3441 // Remove the timeout. 3442 dpm.setMaximumTimeToLock(admin1, 0); 3443 verifyScreenTimeoutCall(null, PROFILE_USER); 3444 verifyScreenTimeoutCall(Long.MAX_VALUE, UserHandle.USER_SYSTEM); 3445 } 3446 3447 @Test testSetRequiredStrongAuthTimeout_DeviceOwner()3448 public void testSetRequiredStrongAuthTimeout_DeviceOwner() throws Exception { 3449 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3450 setupDeviceOwner(); 3451 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3452 3453 final long MINIMUM_STRONG_AUTH_TIMEOUT_MS = TimeUnit.HOURS.toMillis(1); 3454 final long ONE_MINUTE = TimeUnit.MINUTES.toMillis(1); 3455 final long MIN_PLUS_ONE_MINUTE = MINIMUM_STRONG_AUTH_TIMEOUT_MS + ONE_MINUTE; 3456 final long MAX_MINUS_ONE_MINUTE = DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS 3457 - ONE_MINUTE; 3458 3459 // verify that the minimum timeout cannot be modified on user builds (system property is 3460 // not being read) 3461 getServices().buildMock.isDebuggable = false; 3462 3463 dpm.setRequiredStrongAuthTimeout(admin1, MAX_MINUS_ONE_MINUTE); 3464 assertThat(MAX_MINUS_ONE_MINUTE).isEqualTo(dpm.getRequiredStrongAuthTimeout(admin1)); 3465 assertThat(MAX_MINUS_ONE_MINUTE).isEqualTo(dpm.getRequiredStrongAuthTimeout(null)); 3466 3467 verify(getServices().systemProperties, never()).getLong(anyString(), anyLong()); 3468 3469 // restore to the debuggable build state 3470 getServices().buildMock.isDebuggable = true; 3471 3472 // reset to default (0 means the admin is not participating, so default should be returned) 3473 dpm.setRequiredStrongAuthTimeout(admin1, 0); 3474 3475 // aggregation should be the default if unset by any admin 3476 assertThat(DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS) 3477 .isEqualTo(dpm.getRequiredStrongAuthTimeout(null)); 3478 3479 // admin not participating by default 3480 assertThat(dpm.getRequiredStrongAuthTimeout(admin1)).isEqualTo(0); 3481 3482 //clamping from the top 3483 dpm.setRequiredStrongAuthTimeout(admin1, 3484 DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS + ONE_MINUTE); 3485 assertThat(DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS) 3486 .isEqualTo(dpm.getRequiredStrongAuthTimeout(admin1)); 3487 assertThat(DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS) 3488 .isEqualTo(dpm.getRequiredStrongAuthTimeout(null)); 3489 3490 // 0 means the admin is not participating, so default should be returned 3491 dpm.setRequiredStrongAuthTimeout(admin1, 0); 3492 assertThat(dpm.getRequiredStrongAuthTimeout(admin1)).isEqualTo(0); 3493 assertThat(DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS) 3494 .isEqualTo(dpm.getRequiredStrongAuthTimeout(null)); 3495 3496 // clamping from the bottom 3497 dpm.setRequiredStrongAuthTimeout(admin1, MINIMUM_STRONG_AUTH_TIMEOUT_MS - ONE_MINUTE); 3498 assertThat(dpm.getRequiredStrongAuthTimeout(admin1)) 3499 .isEqualTo(MINIMUM_STRONG_AUTH_TIMEOUT_MS); 3500 assertThat(dpm.getRequiredStrongAuthTimeout(null)) 3501 .isEqualTo(MINIMUM_STRONG_AUTH_TIMEOUT_MS); 3502 3503 // values within range 3504 dpm.setRequiredStrongAuthTimeout(admin1, MIN_PLUS_ONE_MINUTE); 3505 assertThat(dpm.getRequiredStrongAuthTimeout(admin1)).isEqualTo(MIN_PLUS_ONE_MINUTE); 3506 assertThat(dpm.getRequiredStrongAuthTimeout(null)).isEqualTo(MIN_PLUS_ONE_MINUTE); 3507 3508 dpm.setRequiredStrongAuthTimeout(admin1, MAX_MINUS_ONE_MINUTE); 3509 assertThat(dpm.getRequiredStrongAuthTimeout(admin1)).isEqualTo(MAX_MINUS_ONE_MINUTE); 3510 assertThat(dpm.getRequiredStrongAuthTimeout(null)).isEqualTo(MAX_MINUS_ONE_MINUTE); 3511 3512 // reset to default 3513 dpm.setRequiredStrongAuthTimeout(admin1, 0); 3514 assertThat(dpm.getRequiredStrongAuthTimeout(admin1)).isEqualTo(0); 3515 assertThat(DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS) 3516 .isEqualTo(dpm.getRequiredStrongAuthTimeout(null)); 3517 3518 // negative value 3519 assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, 3520 () -> dpm.setRequiredStrongAuthTimeout(admin1, -ONE_MINUTE)); 3521 } 3522 verifyScreenTimeoutCall(Long expectedTimeout, int userId)3523 private void verifyScreenTimeoutCall(Long expectedTimeout, int userId) { 3524 if (expectedTimeout == null) { 3525 verify(getServices().powerManagerInternal, times(0)) 3526 .setMaximumScreenOffTimeoutFromDeviceAdmin(eq(userId), anyLong()); 3527 } else { 3528 verify(getServices().powerManagerInternal, times(1)) 3529 .setMaximumScreenOffTimeoutFromDeviceAdmin(eq(userId), eq(expectedTimeout)); 3530 } 3531 } 3532 verifyStayOnWhilePluggedCleared(boolean cleared)3533 private void verifyStayOnWhilePluggedCleared(boolean cleared) { 3534 // TODO Verify calls to settingsGlobalPutInt. Tried but somehow mockito threw 3535 // UnfinishedVerificationException. 3536 } 3537 setup_DeviceAdminFeatureOff()3538 private void setup_DeviceAdminFeatureOff() throws Exception { 3539 when(getServices().packageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) 3540 .thenReturn(false); 3541 when(getServices().ipackageManager 3542 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(false); 3543 initializeDpms(); 3544 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(true); 3545 when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) 3546 .thenReturn(true); 3547 setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); 3548 3549 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3550 } 3551 3552 @Test testIsProvisioningAllowed_DeviceAdminFeatureOff()3553 public void testIsProvisioningAllowed_DeviceAdminFeatureOff() throws Exception { 3554 setup_DeviceAdminFeatureOff(); 3555 mContext.packageName = admin1.getPackageName(); 3556 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3557 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); 3558 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, false); 3559 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3560 3561 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(true); 3562 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); 3563 } 3564 3565 @Test testCheckProvisioningPreCondition_DeviceAdminFeatureOff()3566 public void testCheckProvisioningPreCondition_DeviceAdminFeatureOff() throws Exception { 3567 setup_DeviceAdminFeatureOff(); 3568 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3569 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3570 DevicePolicyManager.STATUS_DEVICE_ADMIN_NOT_SUPPORTED); 3571 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, 3572 DevicePolicyManager.STATUS_DEVICE_ADMIN_NOT_SUPPORTED); 3573 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3574 DevicePolicyManager.STATUS_DEVICE_ADMIN_NOT_SUPPORTED); 3575 } 3576 setup_ManagedProfileFeatureOff()3577 private void setup_ManagedProfileFeatureOff() throws Exception { 3578 when(getServices().ipackageManager 3579 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(false); 3580 initializeDpms(); 3581 when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, true)) 3582 .thenReturn(true); 3583 setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); 3584 3585 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3586 } 3587 3588 @Test testIsProvisioningAllowed_ManagedProfileFeatureOff()3589 public void testIsProvisioningAllowed_ManagedProfileFeatureOff() throws Exception { 3590 setup_ManagedProfileFeatureOff(); 3591 mContext.packageName = admin1.getPackageName(); 3592 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3593 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); 3594 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, true); 3595 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3596 3597 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(true); 3598 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3599 } 3600 3601 @Test testCheckProvisioningPreCondition_ManagedProfileFeatureOff()3602 public void testCheckProvisioningPreCondition_ManagedProfileFeatureOff() throws Exception { 3603 setup_ManagedProfileFeatureOff(); 3604 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3605 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3606 DevicePolicyManager.STATUS_OK); 3607 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, 3608 DevicePolicyManager.STATUS_OK); 3609 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3610 DevicePolicyManager.STATUS_MANAGED_USERS_NOT_SUPPORTED); 3611 } 3612 setup_firstBoot_systemUser()3613 private void setup_firstBoot_systemUser() throws Exception { 3614 when(getServices().ipackageManager 3615 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(true); 3616 when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, false)) 3617 .thenReturn(true); 3618 setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); 3619 when(getServices().userManager.getProfileParent(UserHandle.USER_SYSTEM)).thenReturn(null); 3620 3621 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3622 } 3623 3624 /* Tests provisions from system user during first boot. */ 3625 @Test testIsProvisioningAllowed_firstBoot_systemUser()3626 public void testIsProvisioningAllowed_firstBoot_systemUser() throws Exception { 3627 setup_firstBoot_systemUser(); 3628 mContext.packageName = admin1.getPackageName(); 3629 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3630 3631 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(false); 3632 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); 3633 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, true); 3634 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); 3635 3636 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(true); 3637 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); 3638 } 3639 3640 @Test testCheckProvisioningPreCondition_firstBoot_systemUser()3641 public void testCheckProvisioningPreCondition_firstBoot_systemUser() 3642 throws Exception { 3643 setup_firstBoot_systemUser(); 3644 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3645 3646 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(false); 3647 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3648 DevicePolicyManager.STATUS_OK); 3649 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, 3650 DevicePolicyManager.STATUS_OK); 3651 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3652 DevicePolicyManager.STATUS_OK); 3653 3654 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(true); 3655 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3656 DevicePolicyManager.STATUS_OK); 3657 } 3658 setup_systemUserSetupComplete_systemUser()3659 private void setup_systemUserSetupComplete_systemUser() throws Exception { 3660 when(getServices().ipackageManager 3661 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(true); 3662 when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, false)) 3663 .thenReturn(true); 3664 setUserSetupCompleteForUser(true, UserHandle.USER_SYSTEM); 3665 when(getServices().userManager.getProfileParent(UserHandle.USER_SYSTEM)).thenReturn(null); 3666 3667 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3668 } 3669 setup_withDo_systemUser()3670 private void setup_withDo_systemUser() throws Exception { 3671 setDeviceOwner(); 3672 setup_systemUserSetupComplete_systemUser(); 3673 setUpPackageManagerForFakeAdmin(adminAnotherPackage, DpmMockContext.ANOTHER_UID, admin2); 3674 } 3675 setup_withDo_systemUser_ManagedProfile()3676 private void setup_withDo_systemUser_ManagedProfile() throws Exception { 3677 setup_withDo_systemUser(); 3678 final int MANAGED_PROFILE_USER_ID = 18; 3679 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 1308); 3680 when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, 3681 false /* we can't remove a managed profile */)).thenReturn(false); 3682 } 3683 3684 @Test testIsProvisioningAllowed_systemUserSetupComplete_systemUser()3685 public void testIsProvisioningAllowed_systemUserSetupComplete_systemUser() 3686 throws Exception { 3687 setup_systemUserSetupComplete_systemUser(); 3688 mContext.packageName = admin1.getPackageName(); 3689 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3690 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3691 false/* because of completed device setup */); 3692 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, 3693 false/* because of completed device setup */); 3694 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); 3695 } 3696 3697 @Test testCheckProvisioningPreCondition_systemUserSetupComplete_systemUser()3698 public void testCheckProvisioningPreCondition_systemUserSetupComplete_systemUser() 3699 throws Exception { 3700 setup_systemUserSetupComplete_systemUser(); 3701 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3702 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3703 DevicePolicyManager.STATUS_USER_SETUP_COMPLETED); 3704 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, 3705 DevicePolicyManager.STATUS_USER_SETUP_COMPLETED); 3706 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3707 DevicePolicyManager.STATUS_OK); 3708 } 3709 3710 @Test testProvisioning_withDo_systemUser()3711 public void testProvisioning_withDo_systemUser() throws Exception { 3712 setup_withDo_systemUser(); 3713 mContext.packageName = admin1.getPackageName(); 3714 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3715 3716 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, 3717 DevicePolicyManager.STATUS_HAS_DEVICE_OWNER); 3718 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, 3719 DevicePolicyManager.STATUS_HAS_DEVICE_OWNER); 3720 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); 3721 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, false); 3722 3723 // COMP mode NOT is allowed. 3724 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3725 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3726 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3727 3728 // And other DPCs can NOT provision a managed profile. 3729 assertCheckProvisioningPreCondition( 3730 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3731 DpmMockContext.ANOTHER_PACKAGE_NAME, 3732 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3733 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, 3734 DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); 3735 } 3736 3737 @Test testProvisioning_withDo_systemUser_restrictedBySystem()3738 public void testProvisioning_withDo_systemUser_restrictedBySystem() 3739 throws Exception { 3740 setup_withDo_systemUser(); 3741 mContext.packageName = admin1.getPackageName(); 3742 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3743 // The DO should not be allowed to initiate provisioning if the restriction is set by 3744 // another entity. 3745 when(getServices().userManager.hasUserRestriction( 3746 eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), 3747 eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) 3748 .thenReturn(true); 3749 when(getServices().userManager.getUserRestrictionSource( 3750 eq(UserManager.DISALLOW_ADD_MANAGED_PROFILE), 3751 eq(UserHandle.getUserHandleForUid(mContext.binder.callingUid)))) 3752 .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); 3753 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3754 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3755 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3756 3757 assertCheckProvisioningPreCondition( 3758 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3759 DpmMockContext.ANOTHER_PACKAGE_NAME, 3760 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3761 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, 3762 DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); 3763 } 3764 3765 @Test testCheckCannotSetProfileOwnerWithDeviceOwner()3766 public void testCheckCannotSetProfileOwnerWithDeviceOwner() throws Exception { 3767 setup_withDo_systemUser(); 3768 final int managedProfileUserId = 18; 3769 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 1308); 3770 3771 final int userId = UserHandle.getUserId(managedProfileAdminUid); 3772 getServices().addUser(userId, 0, UserManager.USER_TYPE_PROFILE_MANAGED, 3773 UserHandle.USER_SYSTEM); 3774 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); 3775 setUpPackageManagerForFakeAdmin(admin1, managedProfileAdminUid, admin1); 3776 dpm.setActiveAdmin(admin1, false, userId); 3777 assertThat(dpm.setProfileOwner(admin1, userId)).isFalse(); 3778 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); 3779 } 3780 3781 @Test testCheckProvisioningPreCondition_attemptingComp()3782 public void testCheckProvisioningPreCondition_attemptingComp() throws Exception { 3783 setup_withDo_systemUser_ManagedProfile(); 3784 mContext.packageName = admin1.getPackageName(); 3785 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3786 3787 // We can delete the managed profile to create a new one, so provisioning is allowed. 3788 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3789 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3790 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3791 assertCheckProvisioningPreCondition( 3792 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3793 DpmMockContext.ANOTHER_PACKAGE_NAME, 3794 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3795 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, 3796 DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); 3797 } 3798 3799 @Test testCheckProvisioningPreCondition_comp_cannot_remove_profile()3800 public void testCheckProvisioningPreCondition_comp_cannot_remove_profile() 3801 throws Exception { 3802 setup_withDo_systemUser_ManagedProfile(); 3803 mContext.packageName = admin1.getPackageName(); 3804 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3805 when(getServices().userManager.hasUserRestriction( 3806 eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), 3807 eq(UserHandle.SYSTEM))) 3808 .thenReturn(true); 3809 when(getServices().userManager.getUserRestrictionSource( 3810 eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), 3811 eq(UserHandle.SYSTEM))) 3812 .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); 3813 3814 // We can't remove the profile to create a new one. 3815 assertCheckProvisioningPreCondition( 3816 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3817 DpmMockContext.ANOTHER_PACKAGE_NAME, 3818 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3819 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false, 3820 DpmMockContext.ANOTHER_PACKAGE_NAME, DpmMockContext.ANOTHER_UID); 3821 3822 // But the device owner can still do it because it has set the restriction itself. 3823 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3824 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3825 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3826 } 3827 3828 // TODO(b/174859111): move to automotive-only section setup_firstBoot_headlessSystemUserMode()3829 private void setup_firstBoot_headlessSystemUserMode() throws Exception { 3830 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 3831 when(getServices().userManager.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM, false)) 3832 .thenReturn(true); 3833 setUserSetupCompleteForUser(false, UserHandle.USER_SYSTEM); 3834 when(getServices().userManager.getProfileParent(UserHandle.USER_SYSTEM)).thenReturn(null); 3835 } 3836 3837 /** 3838 * TODO(b/174859111): move to automotive-only section 3839 * Tests provision from secondary user during first boot. 3840 **/ 3841 @Test testIsProvisioningAllowed_firstBoot_secondaryUser()3842 public void testIsProvisioningAllowed_firstBoot_secondaryUser() throws Exception { 3843 setup_firstBoot_headlessSystemUserMode(); 3844 mContext.packageName = admin1.getPackageName(); 3845 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3846 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 3847 // Provisioning device from secondary user should fail in non-headless system user mode. 3848 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(false); 3849 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, false); 3850 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE, false); 3851 3852 // Required for ACTION_PROVISION_MANAGED_PROFILE if allowed to add managed profile from 3853 // secondary user 3854 when(getServices().userManager.canAddMoreManagedProfiles(CALLER_USER_HANDLE, false)) 3855 .thenReturn(true); 3856 when(getServices().ipackageManager 3857 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(true); 3858 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, true); 3859 3860 // Provisioning device from secondary user should be allowed in headless system user mode. 3861 when(getServices().userManagerForMock.isHeadlessSystemUserMode()).thenReturn(true); 3862 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, true); 3863 } 3864 setup_provisionManagedProfileWithDeviceOwner_primaryUser()3865 private void setup_provisionManagedProfileWithDeviceOwner_primaryUser() throws Exception { 3866 setDeviceOwner(); 3867 3868 when(getServices().ipackageManager 3869 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(true); 3870 when(getServices().userManager.getProfileParent(CALLER_USER_HANDLE)) 3871 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 3872 when(getServices().userManager.canAddMoreManagedProfiles(CALLER_USER_HANDLE, 3873 false)).thenReturn(true); 3874 setUserSetupCompleteForUser(false, CALLER_USER_HANDLE); 3875 3876 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 3877 } 3878 3879 @Test testIsProvisioningAllowed_provisionManagedProfileWithDeviceOwner_primaryUser()3880 public void testIsProvisioningAllowed_provisionManagedProfileWithDeviceOwner_primaryUser() 3881 throws Exception { 3882 setup_provisionManagedProfileWithDeviceOwner_primaryUser(); 3883 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3884 mContext.packageName = admin1.getPackageName(); 3885 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3886 } 3887 3888 @Test testCheckProvisioningPreCondition_provisionManagedProfileWithDeviceOwner_primaryUser()3889 public void testCheckProvisioningPreCondition_provisionManagedProfileWithDeviceOwner_primaryUser() 3890 throws Exception { 3891 setup_provisionManagedProfileWithDeviceOwner_primaryUser(); 3892 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3893 3894 // COMP mode is NOT allowed. 3895 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3896 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3897 } 3898 setup_provisionManagedProfileOneAlreadyExist_primaryUser()3899 private void setup_provisionManagedProfileOneAlreadyExist_primaryUser() throws Exception { 3900 setDeviceOwner(); 3901 3902 when(getServices().ipackageManager 3903 .hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS, 0)).thenReturn(true); 3904 when(getServices().userManager.hasUserRestriction( 3905 eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), 3906 eq(UserHandle.of(CALLER_USER_HANDLE)))) 3907 .thenReturn(true); 3908 when(getServices().userManager.canAddMoreManagedProfiles(CALLER_USER_HANDLE, 3909 false /* we can't remove a managed profile */)).thenReturn(false); 3910 setUserSetupCompleteForUser(false, CALLER_USER_HANDLE); 3911 3912 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 3913 } 3914 3915 @Test testIsProvisioningAllowed_provisionManagedProfile_oneAlreadyExists_primaryUser()3916 public void testIsProvisioningAllowed_provisionManagedProfile_oneAlreadyExists_primaryUser() 3917 throws Exception { 3918 setup_provisionManagedProfileOneAlreadyExist_primaryUser(); 3919 mContext.packageName = admin1.getPackageName(); 3920 setUpPackageManagerForAdmin(admin1, mContext.binder.callingUid); 3921 assertProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, false); 3922 } 3923 3924 @Test testCheckProvisioningPreCondition_provisionManagedProfile_oneAlreadyExists_primaryUser()3925 public void testCheckProvisioningPreCondition_provisionManagedProfile_oneAlreadyExists_primaryUser() 3926 throws Exception { 3927 setup_provisionManagedProfileOneAlreadyExist_primaryUser(); 3928 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3929 assertCheckProvisioningPreCondition(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, 3930 DevicePolicyManager.STATUS_CANNOT_ADD_MANAGED_PROFILE); 3931 } 3932 3933 @Test testCheckProvisioningPreCondition_permission()3934 public void testCheckProvisioningPreCondition_permission() { 3935 // GIVEN the permission MANAGE_PROFILE_AND_DEVICE_OWNERS is not granted 3936 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 3937 () -> dpm.checkProvisioningPrecondition( 3938 DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE, "some.package")); 3939 } 3940 3941 @Test testForceUpdateUserSetupComplete_permission()3942 public void testForceUpdateUserSetupComplete_permission() { 3943 // GIVEN the permission MANAGE_PROFILE_AND_DEVICE_OWNERS is not granted 3944 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 3945 () -> dpm.forceUpdateUserSetupComplete(UserHandle.USER_SYSTEM)); 3946 } 3947 3948 @Test testForceUpdateUserSetupComplete_forcesUpdate()3949 public void testForceUpdateUserSetupComplete_forcesUpdate() { 3950 mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); 3951 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3952 final int userId = UserHandle.getUserId(mContext.binder.callingUid); 3953 3954 // GIVEN userComplete is false in SettingsProvider 3955 setUserSetupCompleteForUser(false, userId); 3956 3957 // GIVEN userComplete is true in DPM 3958 DevicePolicyData userData = new DevicePolicyData(userId); 3959 userData.mUserSetupComplete = true; 3960 dpms.mUserData.put(userId, userData); 3961 3962 assertThat(dpms.hasUserSetupCompleted()).isTrue(); 3963 3964 dpm.forceUpdateUserSetupComplete(userId); 3965 3966 // THEN the state in dpms is changed 3967 assertThat(dpms.hasUserSetupCompleted()).isFalse(); 3968 } 3969 clearDeviceOwner()3970 private void clearDeviceOwner() { 3971 getServices().addTestPackageUid(admin1.getPackageName(), 3972 DpmMockContext.CALLER_SYSTEM_USER_UID); 3973 3974 mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3975 runAsCaller(mAdmin1Context, dpms, dpm -> { 3976 dpm.clearDeviceOwnerApp(admin1.getPackageName()); 3977 }); 3978 } 3979 3980 @Test testGetLastSecurityLogRetrievalTime()3981 public void testGetLastSecurityLogRetrievalTime() throws Exception { 3982 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 3983 setupDeviceOwner(); 3984 3985 // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the 3986 // feature is disabled because there are non-affiliated secondary users. 3987 getServices().removeUser(CALLER_USER_HANDLE); 3988 when(mContext.resources.getBoolean(R.bool.config_supportPreRebootSecurityLogs)) 3989 .thenReturn(true); 3990 3991 // No logs were retrieved so far. 3992 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(-1); 3993 3994 // Enabling logging should not change the timestamp. 3995 dpm.setSecurityLoggingEnabled(admin1, true); 3996 verify(getServices().settings).securityLogSetLoggingEnabledProperty(true); 3997 3998 when(getServices().settings.securityLogGetLoggingEnabledProperty()).thenReturn(true); 3999 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(-1); 4000 4001 // Retrieving the logs should update the timestamp. 4002 final long beforeRetrieval = System.currentTimeMillis(); 4003 dpm.retrieveSecurityLogs(admin1); 4004 final long firstSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); 4005 final long afterRetrieval = System.currentTimeMillis(); 4006 assertThat(firstSecurityLogRetrievalTime >= beforeRetrieval).isTrue(); 4007 assertThat(firstSecurityLogRetrievalTime <= afterRetrieval).isTrue(); 4008 4009 // Retrieving the pre-boot logs should update the timestamp. 4010 Thread.sleep(2); 4011 dpm.retrievePreRebootSecurityLogs(admin1); 4012 final long secondSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); 4013 assertThat(secondSecurityLogRetrievalTime > firstSecurityLogRetrievalTime).isTrue(); 4014 4015 // Checking the timestamp again should not change it. 4016 Thread.sleep(2); 4017 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(secondSecurityLogRetrievalTime); 4018 4019 // Retrieving the logs again should update the timestamp. 4020 dpm.retrieveSecurityLogs(admin1); 4021 final long thirdSecurityLogRetrievalTime = dpm.getLastSecurityLogRetrievalTime(); 4022 assertThat(thirdSecurityLogRetrievalTime > secondSecurityLogRetrievalTime).isTrue(); 4023 4024 // Disabling logging should not change the timestamp. 4025 Thread.sleep(2); 4026 dpm.setSecurityLoggingEnabled(admin1, false); 4027 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(thirdSecurityLogRetrievalTime); 4028 4029 // Restarting the DPMS should not lose the timestamp. 4030 initializeDpms(); 4031 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(thirdSecurityLogRetrievalTime); 4032 4033 // Any uid holding MANAGE_USERS permission can retrieve the timestamp. 4034 mContext.binder.callingUid = 1234567; 4035 mContext.callerPermissions.add(permission.MANAGE_USERS); 4036 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(thirdSecurityLogRetrievalTime); 4037 mContext.callerPermissions.remove(permission.MANAGE_USERS); 4038 4039 // System can retrieve the timestamp. 4040 mContext.binder.clearCallingIdentity(); 4041 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(thirdSecurityLogRetrievalTime); 4042 4043 // Removing the device owner should clear the timestamp. 4044 clearDeviceOwner(); 4045 assertThat(dpm.getLastSecurityLogRetrievalTime()).isEqualTo(-1); 4046 } 4047 4048 @Test testSetConfiguredNetworksLockdownStateWithDO()4049 public void testSetConfiguredNetworksLockdownStateWithDO() throws Exception { 4050 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4051 setupDeviceOwner(); 4052 dpm.setConfiguredNetworksLockdownState(admin1, true); 4053 verify(getServices().settings).settingsGlobalPutInt( 4054 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 1); 4055 4056 dpm.setConfiguredNetworksLockdownState(admin1, false); 4057 verify(getServices().settings).settingsGlobalPutInt( 4058 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0); 4059 } 4060 4061 @Test testSetConfiguredNetworksLockdownStateWithPO()4062 public void testSetConfiguredNetworksLockdownStateWithPO() throws Exception { 4063 setupProfileOwner(); 4064 assertExpectException(SecurityException.class, null, 4065 () -> dpm.setConfiguredNetworksLockdownState(admin1, false)); 4066 verify(getServices().settings, never()).settingsGlobalPutInt( 4067 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0); 4068 } 4069 4070 @Test testSetConfiguredNetworksLockdownStateWithPOOfOrganizationOwnedDevice()4071 public void testSetConfiguredNetworksLockdownStateWithPOOfOrganizationOwnedDevice() 4072 throws Exception { 4073 setupProfileOwner(); 4074 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4075 dpm.setConfiguredNetworksLockdownState(admin1, true); 4076 verify(getServices().settings).settingsGlobalPutInt( 4077 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 1); 4078 4079 dpm.setConfiguredNetworksLockdownState(admin1, false); 4080 verify(getServices().settings).settingsGlobalPutInt( 4081 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0); 4082 } 4083 4084 @Test testUpdateNetworkPreferenceOnStartUser()4085 public void testUpdateNetworkPreferenceOnStartUser() throws Exception { 4086 final int managedProfileUserId = 15; 4087 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4088 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4089 mContext.binder.callingUid = managedProfileAdminUid; 4090 mServiceContext.permissions.add(permission.INTERACT_ACROSS_USERS_FULL); 4091 4092 dpms.handleStartUser(managedProfileUserId); 4093 ProfileNetworkPreference preferenceDetails = 4094 new ProfileNetworkPreference.Builder() 4095 .setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT) 4096 .build(); 4097 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4098 preferences.add(preferenceDetails); 4099 verify(getServices().connectivityManager, times(1)) 4100 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4101 null, null); 4102 } 4103 4104 @Test testUpdateNetworkPreferenceOnStopUser()4105 public void testUpdateNetworkPreferenceOnStopUser() throws Exception { 4106 final int managedProfileUserId = 15; 4107 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4108 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4109 mContext.binder.callingUid = managedProfileAdminUid; 4110 mServiceContext.permissions.add(permission.INTERACT_ACROSS_USERS_FULL); 4111 4112 dpms.handleStopUser(managedProfileUserId); 4113 ProfileNetworkPreference preferenceDetails = 4114 new ProfileNetworkPreference.Builder() 4115 .setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT) 4116 .build(); 4117 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4118 preferences.add(preferenceDetails); 4119 verify(getServices().connectivityManager, times(1)) 4120 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4121 null, null); 4122 } 4123 4124 @Test testGetSetPreferentialNetworkService()4125 public void testGetSetPreferentialNetworkService() throws Exception { 4126 assertExpectException(SecurityException.class, null, 4127 () -> dpm.setPreferentialNetworkServiceEnabled(false)); 4128 4129 assertExpectException(SecurityException.class, null, 4130 () -> dpm.isPreferentialNetworkServiceEnabled()); 4131 4132 final int managedProfileUserId = 15; 4133 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4134 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4135 mContext.binder.callingUid = managedProfileAdminUid; 4136 4137 dpm.setPreferentialNetworkServiceEnabled(false); 4138 assertThat(dpm.isPreferentialNetworkServiceEnabled()).isFalse(); 4139 4140 ProfileNetworkPreference preferenceDetails = 4141 new ProfileNetworkPreference.Builder() 4142 .setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT) 4143 .build(); 4144 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4145 preferences.add(preferenceDetails); 4146 verify(getServices().connectivityManager, times(1)) 4147 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4148 null, null); 4149 4150 dpm.setPreferentialNetworkServiceEnabled(true); 4151 assertThat(dpm.isPreferentialNetworkServiceEnabled()).isTrue(); 4152 4153 ProfileNetworkPreference preferenceDetails2 = 4154 new ProfileNetworkPreference.Builder() 4155 .setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE) 4156 .setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1) 4157 .build(); 4158 List<ProfileNetworkPreference> preferences2 = new ArrayList<>(); 4159 preferences2.add(preferenceDetails); 4160 verify(getServices().connectivityManager, times(1)) 4161 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences2, 4162 null, null); 4163 } 4164 4165 @Test testSetPreferentialNetworkServiceConfig_noProfileOwner()4166 public void testSetPreferentialNetworkServiceConfig_noProfileOwner() throws Exception { 4167 assertExpectException(SecurityException.class, null, 4168 () -> dpm.setPreferentialNetworkServiceConfigs( 4169 List.of(PreferentialNetworkServiceConfig.DEFAULT))); 4170 } 4171 4172 @Test testIsPreferentialNetworkServiceEnabled_noProfileOwner()4173 public void testIsPreferentialNetworkServiceEnabled_noProfileOwner() throws Exception { 4174 assertExpectException(SecurityException.class, null, 4175 () -> dpm.isPreferentialNetworkServiceEnabled()); 4176 } 4177 4178 @Test testSetPreferentialNetworkServiceConfig_invalidConfig()4179 public void testSetPreferentialNetworkServiceConfig_invalidConfig() throws Exception { 4180 final int managedProfileUserId = 15; 4181 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4182 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4183 mContext.binder.callingUid = managedProfileAdminUid; 4184 4185 PreferentialNetworkServiceConfig.Builder preferentialNetworkServiceConfigBuilder = 4186 new PreferentialNetworkServiceConfig.Builder(); 4187 assertExpectException(NullPointerException.class, null, 4188 () -> preferentialNetworkServiceConfigBuilder.setIncludedUids(null)); 4189 assertExpectException(NullPointerException.class, null, 4190 () -> preferentialNetworkServiceConfigBuilder.setExcludedUids(null)); 4191 assertExpectException(IllegalArgumentException.class, null, 4192 () -> preferentialNetworkServiceConfigBuilder.setNetworkId(6)); 4193 int[] includedUids = new int[]{1, 2}; 4194 int[] excludedUids = new int[]{3, 4}; 4195 preferentialNetworkServiceConfigBuilder.setIncludedUids(includedUids); 4196 preferentialNetworkServiceConfigBuilder.setExcludedUids(excludedUids); 4197 4198 assertExpectException(IllegalStateException.class, null, 4199 () -> preferentialNetworkServiceConfigBuilder.build()); 4200 } 4201 4202 @Test testSetPreferentialNetworkServiceConfig_defaultPreference()4203 public void testSetPreferentialNetworkServiceConfig_defaultPreference() throws Exception { 4204 final int managedProfileUserId = 15; 4205 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4206 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4207 mContext.binder.callingUid = managedProfileAdminUid; 4208 4209 dpm.setPreferentialNetworkServiceConfigs( 4210 List.of(PreferentialNetworkServiceConfig.DEFAULT)); 4211 assertThat(dpm.isPreferentialNetworkServiceEnabled()).isFalse(); 4212 assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0).isEnabled()).isFalse(); 4213 4214 ProfileNetworkPreference preferenceDetails = 4215 new ProfileNetworkPreference.Builder() 4216 .setPreference(PROFILE_NETWORK_PREFERENCE_DEFAULT) 4217 .build(); 4218 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4219 preferences.add(preferenceDetails); 4220 verify(getServices().connectivityManager, times(1)) 4221 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4222 null, null); 4223 } 4224 4225 @Test testSetPreferentialNetworkServiceConfig_enterprisePreference()4226 public void testSetPreferentialNetworkServiceConfig_enterprisePreference() throws Exception { 4227 PreferentialNetworkServiceConfig preferentialNetworkServiceConfigEnabled = 4228 (new PreferentialNetworkServiceConfig.Builder()) 4229 .setEnabled(true) 4230 .setNetworkId(NET_ENTERPRISE_ID_1) 4231 .build(); 4232 4233 final int managedProfileUserId = 15; 4234 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4235 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4236 mContext.binder.callingUid = managedProfileAdminUid; 4237 4238 dpm.setPreferentialNetworkServiceConfigs(List.of(preferentialNetworkServiceConfigEnabled)); 4239 assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0) 4240 .isEnabled()).isTrue(); 4241 ProfileNetworkPreference preferenceDetails = 4242 new ProfileNetworkPreference.Builder() 4243 .setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE) 4244 .setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1) 4245 .build(); 4246 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4247 preferences.add(preferenceDetails); 4248 verify(getServices().connectivityManager, times(1)) 4249 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4250 null, null); 4251 } 4252 4253 @Test testSetPreferentialNetworkServiceConfig_enterprisePreferenceIncludedUids()4254 public void testSetPreferentialNetworkServiceConfig_enterprisePreferenceIncludedUids() 4255 throws Exception { 4256 final int managedProfileUserId = 15; 4257 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4258 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4259 mContext.binder.callingUid = managedProfileAdminUid; 4260 4261 PreferentialNetworkServiceConfig preferentialNetworkServiceConfigEnabled = 4262 (new PreferentialNetworkServiceConfig.Builder()) 4263 .setEnabled(true) 4264 .setNetworkId(NET_ENTERPRISE_ID_1) 4265 .setFallbackToDefaultConnectionAllowed(false) 4266 .setIncludedUids(new int[]{1, 2}) 4267 .build(); 4268 dpm.setPreferentialNetworkServiceConfigs(List.of(preferentialNetworkServiceConfigEnabled)); 4269 assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0) 4270 .isEnabled()).isTrue(); 4271 ProfileNetworkPreference preferenceDetails = 4272 new ProfileNetworkPreference.Builder() 4273 .setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK) 4274 .setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1) 4275 .setIncludedUids(new int[]{1, 2}) 4276 .build(); 4277 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4278 preferences.add(preferenceDetails); 4279 verify(getServices().connectivityManager, times(1)) 4280 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4281 null, null); 4282 } 4283 4284 @Test testSetPreferentialNetworkServiceConfig_enterprisePreferenceExcludedUids()4285 public void testSetPreferentialNetworkServiceConfig_enterprisePreferenceExcludedUids() 4286 throws Exception { 4287 final int managedProfileUserId = 15; 4288 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 4289 addManagedProfile(admin1, managedProfileAdminUid, admin1); 4290 mContext.binder.callingUid = managedProfileAdminUid; 4291 4292 PreferentialNetworkServiceConfig preferentialNetworkServiceConfigEnabled = 4293 (new PreferentialNetworkServiceConfig.Builder()) 4294 .setEnabled(true) 4295 .setNetworkId(NET_ENTERPRISE_ID_1) 4296 .setFallbackToDefaultConnectionAllowed(false) 4297 .setExcludedUids(new int[]{1, 2}) 4298 .build(); 4299 4300 dpm.setPreferentialNetworkServiceConfigs(List.of(preferentialNetworkServiceConfigEnabled)); 4301 assertThat(dpm.getPreferentialNetworkServiceConfigs().get(0) 4302 .isEnabled()).isTrue(); 4303 ProfileNetworkPreference preferenceDetails = 4304 new ProfileNetworkPreference.Builder() 4305 .setPreference(PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK) 4306 .setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1) 4307 .setExcludedUids(new int[]{1, 2}) 4308 .build(); 4309 List<ProfileNetworkPreference> preferences = new ArrayList<>(); 4310 preferences.clear(); 4311 preferences.add(preferenceDetails); 4312 verify(getServices().connectivityManager, times(1)) 4313 .setProfileNetworkPreferences(UserHandle.of(managedProfileUserId), preferences, 4314 null, null); 4315 } 4316 4317 @Test testSetSystemSettingFailWithNonWhitelistedSettings()4318 public void testSetSystemSettingFailWithNonWhitelistedSettings() throws Exception { 4319 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4320 setupDeviceOwner(); 4321 assertExpectException(SecurityException.class, null, () -> 4322 dpm.setSystemSetting(admin1, Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, "0")); 4323 } 4324 4325 @Test testSetSystemSettingWithDO()4326 public void testSetSystemSettingWithDO() throws Exception { 4327 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4328 setupDeviceOwner(); 4329 dpm.setSystemSetting(admin1, Settings.System.SCREEN_BRIGHTNESS, "0"); 4330 verify(getServices().settings).settingsSystemPutStringForUser( 4331 Settings.System.SCREEN_BRIGHTNESS, "0", UserHandle.USER_SYSTEM); 4332 } 4333 4334 @Test testSetSystemSettingWithPO()4335 public void testSetSystemSettingWithPO() throws Exception { 4336 setupProfileOwner(); 4337 dpm.setSystemSetting(admin1, Settings.System.SCREEN_BRIGHTNESS, "0"); 4338 verify(getServices().settings).settingsSystemPutStringForUser( 4339 Settings.System.SCREEN_BRIGHTNESS, "0", CALLER_USER_HANDLE); 4340 } 4341 4342 @Test testSetAutoTimeEnabledModifiesSetting()4343 public void testSetAutoTimeEnabledModifiesSetting() throws Exception { 4344 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4345 setupDeviceOwner(); 4346 dpm.setAutoTimeEnabled(admin1, true); 4347 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 1); 4348 4349 dpm.setAutoTimeEnabled(admin1, false); 4350 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 0); 4351 } 4352 4353 @Test testSetAutoTimeEnabledWithPOOnUser0()4354 public void testSetAutoTimeEnabledWithPOOnUser0() throws Exception { 4355 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 4356 setupProfileOwnerOnUser0(); 4357 dpm.setAutoTimeEnabled(admin1, true); 4358 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 1); 4359 4360 dpm.setAutoTimeEnabled(admin1, false); 4361 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 0); 4362 } 4363 4364 @Test testSetAutoTimeEnabledFailWithPONotOnUser0()4365 public void testSetAutoTimeEnabledFailWithPONotOnUser0() throws Exception { 4366 setupProfileOwner(); 4367 assertExpectException(SecurityException.class, null, 4368 () -> dpm.setAutoTimeEnabled(admin1, false)); 4369 verify(getServices().settings, never()).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 0); 4370 } 4371 4372 @Test testSetAutoTimeEnabledWithPOOfOrganizationOwnedDevice()4373 public void testSetAutoTimeEnabledWithPOOfOrganizationOwnedDevice() throws Exception { 4374 setupProfileOwner(); 4375 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4376 4377 dpm.setAutoTimeEnabled(admin1, true); 4378 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 1); 4379 4380 dpm.setAutoTimeEnabled(admin1, false); 4381 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME, 0); 4382 } 4383 4384 @Test 4385 @Ignore("b/277916462") testSetAutoTimeZoneEnabledModifiesSetting()4386 public void testSetAutoTimeZoneEnabledModifiesSetting() throws Exception { 4387 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4388 setupDeviceOwner(); 4389 dpm.setAutoTimeZoneEnabled(admin1, true); 4390 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 1); 4391 4392 dpm.setAutoTimeZoneEnabled(admin1, false); 4393 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 0); 4394 } 4395 4396 @Test 4397 @Ignore("b/277916462") testSetAutoTimeZoneEnabledWithPOOnUser0()4398 public void testSetAutoTimeZoneEnabledWithPOOnUser0() throws Exception { 4399 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 4400 setupProfileOwnerOnUser0(); 4401 dpm.setAutoTimeZoneEnabled(admin1, true); 4402 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 1); 4403 4404 dpm.setAutoTimeZoneEnabled(admin1, false); 4405 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 0); 4406 } 4407 4408 @Test 4409 @Ignore("b/277916462") testSetAutoTimeZoneEnabledFailWithPONotOnUser0()4410 public void testSetAutoTimeZoneEnabledFailWithPONotOnUser0() throws Exception { 4411 setupProfileOwner(); 4412 assertExpectException(SecurityException.class, null, 4413 () -> dpm.setAutoTimeZoneEnabled(admin1, false)); 4414 verify(getServices().settings, never()).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 4415 0); 4416 } 4417 4418 @Test 4419 @Ignore("b/277916462") testSetAutoTimeZoneEnabledWithPOOfOrganizationOwnedDevice()4420 public void testSetAutoTimeZoneEnabledWithPOOfOrganizationOwnedDevice() throws Exception { 4421 setupProfileOwner(); 4422 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4423 4424 dpm.setAutoTimeZoneEnabled(admin1, true); 4425 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 1); 4426 4427 dpm.setAutoTimeZoneEnabled(admin1, false); 4428 verify(getServices().settings).settingsGlobalPutInt(Settings.Global.AUTO_TIME_ZONE, 0); 4429 } 4430 4431 @Test testIsOrganizationOwnedDevice()4432 public void testIsOrganizationOwnedDevice() throws Exception { 4433 // Set up the user manager to return correct user info 4434 addManagedProfile(admin1, DpmMockContext.CALLER_UID, admin1); 4435 4436 // Any caller should be able to call this method. 4437 assertThat(dpm.isOrganizationOwnedDeviceWithManagedProfile()).isFalse(); 4438 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4439 4440 verify(getServices().userManager).setUserRestriction( 4441 eq(UserManager.DISALLOW_ADD_USER), 4442 eq(true), 4443 eq(UserHandle.of(UserHandle.USER_SYSTEM))); 4444 4445 assertThat(dpm.isOrganizationOwnedDeviceWithManagedProfile()).isTrue(); 4446 4447 // A random caller from another user should also be able to get the right result. 4448 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 4449 assertThat(dpm.isOrganizationOwnedDeviceWithManagedProfile()).isTrue(); 4450 } 4451 4452 @Test testMarkOrganizationOwnedDevice_baseRestrictionsAdded()4453 public void testMarkOrganizationOwnedDevice_baseRestrictionsAdded() throws Exception { 4454 addManagedProfile(admin1, DpmMockContext.CALLER_UID, admin1); 4455 4456 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4457 4458 // Base restriction DISALLOW_REMOVE_MANAGED_PROFILE added 4459 verify(getServices().userManager).setUserRestriction( 4460 eq(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE), 4461 eq(true), 4462 eq(UserHandle.of(UserHandle.USER_SYSTEM))); 4463 4464 // Base restriction DISALLOW_ADD_USER added 4465 verify(getServices().userManager).setUserRestriction( 4466 eq(UserManager.DISALLOW_ADD_USER), 4467 eq(true), 4468 eq(UserHandle.of(UserHandle.USER_SYSTEM))); 4469 4470 // Assert base restrictions cannot be added or removed by admin 4471 assertExpectException(SecurityException.class, null, () -> 4472 parentDpm.addUserRestriction(admin1, UserManager.DISALLOW_REMOVE_MANAGED_PROFILE)); 4473 assertExpectException(SecurityException.class, null, () -> 4474 parentDpm.clearUserRestriction(admin1, 4475 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE)); 4476 assertExpectException(SecurityException.class, null, () -> 4477 parentDpm.addUserRestriction(admin1, UserManager.DISALLOW_ADD_USER)); 4478 assertExpectException(SecurityException.class, null, () -> 4479 parentDpm.clearUserRestriction(admin1, UserManager.DISALLOW_ADD_USER)); 4480 } 4481 4482 @Test testSetTime()4483 public void testSetTime() throws Exception { 4484 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4485 setupDeviceOwner(); 4486 dpm.setTime(admin1, 0); 4487 verify(getServices().alarmManager).setTime(0); 4488 } 4489 4490 @Test testSetTimeFailWithPO()4491 public void testSetTimeFailWithPO() throws Exception { 4492 setupProfileOwner(); 4493 assertExpectException(SecurityException.class, null, () -> dpm.setTime(admin1, 0)); 4494 } 4495 4496 @Test testSetTimeWithPOOfOrganizationOwnedDevice()4497 public void testSetTimeWithPOOfOrganizationOwnedDevice() throws Exception { 4498 setupProfileOwner(); 4499 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4500 dpm.setTime(admin1, 0); 4501 verify(getServices().alarmManager).setTime(0); 4502 } 4503 4504 @Test testSetTimeWithAutoTimeOn()4505 public void testSetTimeWithAutoTimeOn() throws Exception { 4506 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4507 setupDeviceOwner(); 4508 when(getServices().settings.settingsGlobalGetInt(Settings.Global.AUTO_TIME, 0)) 4509 .thenReturn(1); 4510 assertThat(dpm.setTime(admin1, 0)).isFalse(); 4511 } 4512 4513 @Test testSetTimeZone()4514 public void testSetTimeZone() throws Exception { 4515 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4516 setupDeviceOwner(); 4517 dpm.setTimeZone(admin1, "Asia/Shanghai"); 4518 verify(getServices().alarmManagerInternal) 4519 .setTimeZone(eq("Asia/Shanghai"), eq(TIME_ZONE_CONFIDENCE_HIGH), anyString()); 4520 } 4521 4522 @Test testSetTimeZoneFailWithPO()4523 public void testSetTimeZoneFailWithPO() throws Exception { 4524 setupProfileOwner(); 4525 assertExpectException(SecurityException.class, null, 4526 () -> dpm.setTimeZone(admin1, "Asia/Shanghai")); 4527 } 4528 4529 @Test testSetTimeZoneWithPOOfOrganizationOwnedDevice()4530 public void testSetTimeZoneWithPOOfOrganizationOwnedDevice() throws Exception { 4531 setupProfileOwner(); 4532 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4533 dpm.setTimeZone(admin1, "Asia/Shanghai"); 4534 verify(getServices().alarmManagerInternal) 4535 .setTimeZone(eq("Asia/Shanghai"), eq(TIME_ZONE_CONFIDENCE_HIGH), anyString()); 4536 } 4537 4538 @Test testSetTimeZoneWithAutoTimeZoneOn()4539 public void testSetTimeZoneWithAutoTimeZoneOn() throws Exception { 4540 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4541 setupDeviceOwner(); 4542 when(getServices().settings.settingsGlobalGetInt(Settings.Global.AUTO_TIME_ZONE, 0)) 4543 .thenReturn(1); 4544 assertThat(dpm.setTimeZone(admin1, "Asia/Shanghai")).isFalse(); 4545 } 4546 4547 @Test testGetLastBugReportRequestTime()4548 public void testGetLastBugReportRequestTime() throws Exception { 4549 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4550 setupDeviceOwner(); 4551 4552 mContext.packageName = admin1.getPackageName(); 4553 mContext.applicationInfo = new ApplicationInfo(); 4554 when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); 4555 4556 // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the 4557 // feature is disabled because there are non-affiliated secondary users. 4558 getServices().removeUser(CALLER_USER_HANDLE); 4559 4560 // No bug reports were requested so far. 4561 assertThat(dpm.getLastBugReportRequestTime()).isEqualTo(-1); 4562 4563 // Requesting a bug report should update the timestamp. 4564 final long beforeRequest = System.currentTimeMillis(); 4565 dpm.requestBugreport(admin1); 4566 final long bugReportRequestTime = dpm.getLastBugReportRequestTime(); 4567 final long afterRequest = System.currentTimeMillis(); 4568 assertThat(bugReportRequestTime).isAtLeast(beforeRequest); 4569 assertThat(bugReportRequestTime).isAtMost(afterRequest); 4570 4571 // Checking the timestamp again should not change it. 4572 Thread.sleep(2); 4573 assertThat(dpm.getLastBugReportRequestTime()).isEqualTo(bugReportRequestTime); 4574 4575 // Restarting the DPMS should not lose the timestamp. 4576 initializeDpms(); 4577 assertThat(dpm.getLastBugReportRequestTime()).isEqualTo(bugReportRequestTime); 4578 4579 // Any uid holding MANAGE_USERS permission can retrieve the timestamp. 4580 mContext.binder.callingUid = 1234567; 4581 mContext.callerPermissions.add(permission.MANAGE_USERS); 4582 assertThat(dpm.getLastBugReportRequestTime()).isEqualTo(bugReportRequestTime); 4583 mContext.callerPermissions.remove(permission.MANAGE_USERS); 4584 4585 // System can retrieve the timestamp. 4586 mContext.binder.clearCallingIdentity(); 4587 assertThat(dpm.getLastBugReportRequestTime()).isEqualTo(bugReportRequestTime); 4588 4589 // Removing the device owner should clear the timestamp. 4590 clearDeviceOwner(); 4591 assertThat(dpm.getLastBugReportRequestTime()).isEqualTo(-1); 4592 } 4593 4594 @Test testGetLastNetworkLogRetrievalTime()4595 public void testGetLastNetworkLogRetrievalTime() throws Exception { 4596 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4597 setupDeviceOwner(); 4598 mContext.packageName = admin1.getPackageName(); 4599 mContext.applicationInfo = new ApplicationInfo(); 4600 when(mContext.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); 4601 4602 // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the 4603 // feature is disabled because there are non-affiliated secondary users. 4604 getServices().removeUser(CALLER_USER_HANDLE); 4605 when(getServices().iipConnectivityMetrics.addNetdEventCallback(anyInt(), anyObject())) 4606 .thenReturn(true); 4607 4608 // No logs were retrieved so far. 4609 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4610 4611 // Attempting to retrieve logs without enabling logging should not change the timestamp. 4612 dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); 4613 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4614 4615 // Enabling logging should not change the timestamp. 4616 dpm.setNetworkLoggingEnabled(admin1, true); 4617 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4618 4619 // Retrieving the logs should update the timestamp. 4620 final long beforeRetrieval = System.currentTimeMillis(); 4621 dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); 4622 final long firstNetworkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); 4623 final long afterRetrieval = System.currentTimeMillis(); 4624 assertThat(firstNetworkLogRetrievalTime >= beforeRetrieval).isTrue(); 4625 assertThat(firstNetworkLogRetrievalTime <= afterRetrieval).isTrue(); 4626 4627 // Checking the timestamp again should not change it. 4628 Thread.sleep(2); 4629 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(firstNetworkLogRetrievalTime); 4630 4631 // Retrieving the logs again should update the timestamp. 4632 dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); 4633 final long secondNetworkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); 4634 assertThat(secondNetworkLogRetrievalTime > firstNetworkLogRetrievalTime).isTrue(); 4635 4636 // Disabling logging should not change the timestamp. 4637 Thread.sleep(2); 4638 dpm.setNetworkLoggingEnabled(admin1, false); 4639 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(secondNetworkLogRetrievalTime); 4640 4641 // Restarting the DPMS should not lose the timestamp. 4642 initializeDpms(); 4643 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(secondNetworkLogRetrievalTime); 4644 4645 // Any uid holding MANAGE_USERS permission can retrieve the timestamp. 4646 mContext.binder.callingUid = 1234567; 4647 mContext.callerPermissions.add(permission.MANAGE_USERS); 4648 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(secondNetworkLogRetrievalTime); 4649 mContext.callerPermissions.remove(permission.MANAGE_USERS); 4650 4651 // System can retrieve the timestamp. 4652 mContext.binder.clearCallingIdentity(); 4653 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(secondNetworkLogRetrievalTime); 4654 4655 // Removing the device owner should clear the timestamp. 4656 clearDeviceOwner(); 4657 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4658 } 4659 4660 @Test testSetNetworkLoggingEnabled_asPo()4661 public void testSetNetworkLoggingEnabled_asPo() throws Exception { 4662 final int managedProfileUserId = CALLER_USER_HANDLE; 4663 final int managedProfileAdminUid = 4664 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 4665 mContext.binder.callingUid = managedProfileAdminUid; 4666 mContext.applicationInfo = new ApplicationInfo(); 4667 mContext.packageName = admin1.getPackageName(); 4668 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.S); 4669 when(getServices().iipConnectivityMetrics 4670 .addNetdEventCallback(anyInt(), anyObject())).thenReturn(true); 4671 4672 // Check no logs have been retrieved so far. 4673 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4674 4675 // Enable network logging 4676 dpm.setNetworkLoggingEnabled(admin1, true); 4677 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4678 4679 // Retrieve the network logs and verify timestamp has been updated. 4680 final long beforeRetrieval = System.currentTimeMillis(); 4681 4682 dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); 4683 4684 final long networkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); 4685 final long afterRetrieval = System.currentTimeMillis(); 4686 assertThat(networkLogRetrievalTime >= beforeRetrieval).isTrue(); 4687 assertThat(networkLogRetrievalTime <= afterRetrieval).isTrue(); 4688 } 4689 4690 @Test testSetNetworkLoggingEnabled_asPoOfOrgOwnedDevice()4691 public void testSetNetworkLoggingEnabled_asPoOfOrgOwnedDevice() throws Exception { 4692 // Setup profile owner on organization-owned device 4693 final int MANAGED_PROFILE_ADMIN_UID = 4694 UserHandle.getUid(CALLER_USER_HANDLE, DpmMockContext.SYSTEM_UID); 4695 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 4696 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 4697 4698 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 4699 mContext.packageName = admin1.getPackageName(); 4700 mContext.applicationInfo = new ApplicationInfo(); 4701 when(getServices().iipConnectivityMetrics 4702 .addNetdEventCallback(anyInt(), anyObject())).thenReturn(true); 4703 4704 // Check no logs have been retrieved so far. 4705 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4706 4707 // Enable network logging 4708 dpm.setNetworkLoggingEnabled(admin1, true); 4709 assertThat(dpm.getLastNetworkLogRetrievalTime()).isEqualTo(-1); 4710 4711 // Retrieve the network logs and verify timestamp has been updated. 4712 final long beforeRetrieval = System.currentTimeMillis(); 4713 4714 dpm.retrieveNetworkLogs(admin1, 0 /* batchToken */); 4715 4716 final long networkLogRetrievalTime = dpm.getLastNetworkLogRetrievalTime(); 4717 final long afterRetrieval = System.currentTimeMillis(); 4718 assertThat(networkLogRetrievalTime >= beforeRetrieval).isTrue(); 4719 assertThat(networkLogRetrievalTime <= afterRetrieval).isTrue(); 4720 } 4721 4722 @Test testGetBindDeviceAdminTargetUsers()4723 public void testGetBindDeviceAdminTargetUsers() throws Exception { 4724 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 4725 4726 // Setup device owner. 4727 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4728 setupDeviceOwner(); 4729 4730 // Only device owner is setup, the result list should be empty. 4731 List<UserHandle> targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); 4732 MoreAsserts.assertEmpty(targetUsers); 4733 4734 // Add a secondary user, it should never talk with. 4735 final int ANOTHER_USER_ID = 36; 4736 getServices().addUser(ANOTHER_USER_ID, 0, UserManager.USER_TYPE_FULL_SECONDARY); 4737 4738 // Since the managed profile is not affiliated, they should not be allowed to talk to each 4739 // other. 4740 targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); 4741 MoreAsserts.assertEmpty(targetUsers); 4742 4743 // Setting affiliation ids 4744 final Set<String> userAffiliationIds = Collections.singleton("some.affiliation-id"); 4745 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4746 dpm.setAffiliationIds(admin1, userAffiliationIds); 4747 4748 // Changing affiliation ids in one 4749 dpm.setAffiliationIds(admin1, Collections.singleton("some-different-affiliation-id")); 4750 4751 // Since the managed profile is not affiliated any more, they should not be allowed to talk 4752 // to each other. 4753 targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); 4754 MoreAsserts.assertEmpty(targetUsers); 4755 4756 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4757 targetUsers = dpm.getBindDeviceAdminTargetUsers(admin1); 4758 MoreAsserts.assertEmpty(targetUsers); 4759 } 4760 verifyLockTaskState(int userId)4761 private void verifyLockTaskState(int userId) throws Exception { 4762 verifyLockTaskState(userId, new String[0], 4763 DevicePolicyManager.LOCK_TASK_FEATURE_GLOBAL_ACTIONS); 4764 } 4765 verifyLockTaskState(int userId, String[] packages, int flags)4766 private void verifyLockTaskState(int userId, String[] packages, int flags) throws Exception { 4767 verify(getServices().iactivityManager).updateLockTaskPackages(userId, packages); 4768 verify(getServices().iactivityTaskManager).updateLockTaskFeatures(userId, flags); 4769 } 4770 verifyCanSetLockTask(int uid, int userId, ComponentName who, String[] packages, int flags)4771 private void verifyCanSetLockTask(int uid, int userId, ComponentName who, String[] packages, 4772 int flags) throws Exception { 4773 mContext.binder.callingUid = uid; 4774 dpm.setLockTaskPackages(who, packages); 4775 MoreAsserts.assertEquals(packages, dpm.getLockTaskPackages(who)); 4776 for (String p : packages) { 4777 assertThat(dpm.isLockTaskPermitted(p)).isTrue(); 4778 } 4779 assertThat(dpm.isLockTaskPermitted("anotherPackage")).isFalse(); 4780 // Test to see if set lock task features can be set 4781 dpm.setLockTaskFeatures(who, flags); 4782 verifyLockTaskState(userId, packages, flags); 4783 } 4784 verifyCanNotSetLockTask(int uid, ComponentName who, String[] packages, int flags)4785 private void verifyCanNotSetLockTask(int uid, ComponentName who, String[] packages, 4786 int flags) throws Exception { 4787 mContext.binder.callingUid = uid; 4788 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 4789 () -> dpm.setLockTaskPackages(who, packages)); 4790 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 4791 () -> dpm.getLockTaskPackages(who)); 4792 assertThat(dpm.isLockTaskPermitted("doPackage1")).isFalse(); 4793 assertExpectException(SecurityException.class, /* messageRegex =*/ null, 4794 () -> dpm.setLockTaskFeatures(who, flags)); 4795 } 4796 4797 @Test 4798 @FlakyTest(bugId = 260145949) testLockTaskPolicyForProfileOwner()4799 public void testLockTaskPolicyForProfileOwner() throws Exception { 4800 mockPolicyExemptApps(); 4801 mockVendorPolicyExemptApps(); 4802 4803 // Setup a PO 4804 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 4805 setAsProfileOwner(admin1); 4806 verifyLockTaskState(CALLER_USER_HANDLE); 4807 4808 final String[] poPackages = {"poPackage1", "poPackage2"}; 4809 final int poFlags = DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS 4810 | DevicePolicyManager.LOCK_TASK_FEATURE_HOME 4811 | DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW; 4812 verifyCanSetLockTask(DpmMockContext.CALLER_UID, CALLER_USER_HANDLE, admin1, 4813 poPackages, poFlags); 4814 4815 // Set up a managed profile managed by different package (package name shouldn't matter) 4816 final int MANAGED_PROFILE_USER_ID = 15; 4817 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 20456); 4818 final ComponentName adminDifferentPackage = 4819 new ComponentName("another.package", "whatever.class"); 4820 addManagedProfile(adminDifferentPackage, MANAGED_PROFILE_ADMIN_UID, admin2); 4821 verifyLockTaskState(MANAGED_PROFILE_USER_ID); 4822 4823 // Managed profile is unaffiliated - shouldn't be able to setLockTaskPackages. 4824 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 4825 final String[] mpoPackages = {"poPackage1", "poPackage2"}; 4826 final int mpoFlags = DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS 4827 | DevicePolicyManager.LOCK_TASK_FEATURE_HOME 4828 | DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW; 4829 verifyCanNotSetLockTask(MANAGED_PROFILE_ADMIN_UID, adminDifferentPackage, mpoPackages, 4830 mpoFlags); 4831 } 4832 4833 @Test 4834 @FlakyTest(bugId = 260145949) testLockTaskFeatures_IllegalArgumentException()4835 public void testLockTaskFeatures_IllegalArgumentException() throws Exception { 4836 // Setup a device owner. 4837 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4838 setupDeviceOwner(); 4839 // Lock task policy is updated when loading user data. 4840 verifyLockTaskState(UserHandle.USER_SYSTEM); 4841 4842 final int flags = DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS 4843 | DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW; 4844 assertExpectException(IllegalArgumentException.class, 4845 "Cannot use LOCK_TASK_FEATURE_OVERVIEW without LOCK_TASK_FEATURE_HOME", 4846 () -> dpm.setLockTaskFeatures(admin1, flags)); 4847 } 4848 4849 @Test testSecondaryLockscreen_profileOwner()4850 public void testSecondaryLockscreen_profileOwner() throws Exception { 4851 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 4852 4853 // Initial state is disabled. 4854 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of( 4855 CALLER_USER_HANDLE))).isFalse(); 4856 4857 // Profile owner can set enabled state. 4858 setAsProfileOwner(admin1); 4859 when(mServiceContext.resources 4860 .getString(R.string.config_defaultSupervisionProfileOwnerComponent)) 4861 .thenReturn(admin1.flattenToString()); 4862 dpm.setSecondaryLockscreenEnabled(admin1, true); 4863 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of( 4864 CALLER_USER_HANDLE))).isTrue(); 4865 4866 // Managed profile managed by different package is unaffiliated - cannot set enabled. 4867 final int managedProfileUserId = 15; 4868 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 20456); 4869 final ComponentName adminDifferentPackage = 4870 new ComponentName("another.package", "whatever.class"); 4871 addManagedProfile(adminDifferentPackage, managedProfileAdminUid, admin2); 4872 mContext.binder.callingUid = managedProfileAdminUid; 4873 assertExpectException(SecurityException.class, /* messageRegex= */ null, 4874 () -> dpm.setSecondaryLockscreenEnabled(adminDifferentPackage, false)); 4875 } 4876 4877 @Test testSecondaryLockscreen_deviceOwner()4878 public void testSecondaryLockscreen_deviceOwner() throws Exception { 4879 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4880 4881 // Initial state is disabled. 4882 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(UserHandle.USER_SYSTEM))) 4883 .isFalse(); 4884 4885 // Device owners can set enabled state. 4886 setupDeviceOwner(); 4887 when(mServiceContext.resources 4888 .getString(R.string.config_defaultSupervisionProfileOwnerComponent)) 4889 .thenReturn(admin1.flattenToString()); 4890 dpm.setSecondaryLockscreenEnabled(admin1, true); 4891 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(UserHandle.USER_SYSTEM))) 4892 .isTrue(); 4893 } 4894 4895 @Test testSecondaryLockscreen_nonOwner()4896 public void testSecondaryLockscreen_nonOwner() throws Exception { 4897 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 4898 4899 // Initial state is disabled. 4900 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(CALLER_USER_HANDLE))).isFalse(); 4901 4902 // Non-DO/PO cannot set enabled state. 4903 when(mServiceContext.resources 4904 .getString(R.string.config_defaultSupervisionProfileOwnerComponent)) 4905 .thenReturn(admin1.flattenToString()); 4906 assertExpectException(SecurityException.class, /* messageRegex= */ null, 4907 () -> dpm.setSecondaryLockscreenEnabled(admin1, true)); 4908 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(CALLER_USER_HANDLE))).isFalse(); 4909 } 4910 4911 @Test testSecondaryLockscreen_nonSupervisionApp()4912 public void testSecondaryLockscreen_nonSupervisionApp() throws Exception { 4913 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 4914 4915 // Ensure packages are *not* flagged as test_only. 4916 doReturn(new ApplicationInfo()).when(getServices().ipackageManager).getApplicationInfo( 4917 eq(admin1.getPackageName()), 4918 anyLong(), 4919 eq(CALLER_USER_HANDLE)); 4920 doReturn(new ApplicationInfo()).when(getServices().ipackageManager).getApplicationInfo( 4921 eq(admin2.getPackageName()), 4922 anyLong(), 4923 eq(CALLER_USER_HANDLE)); 4924 4925 // Initial state is disabled. 4926 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(CALLER_USER_HANDLE))).isFalse(); 4927 4928 // Caller is Profile Owner, but no supervision app is configured. 4929 setAsProfileOwner(admin1); 4930 assertExpectException(SecurityException.class, "is not the default supervision component", 4931 () -> dpm.setSecondaryLockscreenEnabled(admin1, true)); 4932 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(CALLER_USER_HANDLE))).isFalse(); 4933 4934 // Caller is Profile Owner, but is not the default configured supervision app. 4935 when(mServiceContext.resources 4936 .getString(R.string.config_defaultSupervisionProfileOwnerComponent)) 4937 .thenReturn(admin2.flattenToString()); 4938 assertExpectException(SecurityException.class, "is not the default supervision component", 4939 () -> dpm.setSecondaryLockscreenEnabled(admin1, true)); 4940 assertThat(dpm.isSecondaryLockscreenEnabled(UserHandle.of(CALLER_USER_HANDLE))).isFalse(); 4941 } 4942 4943 @Test testIsDeviceManaged()4944 public void testIsDeviceManaged() throws Exception { 4945 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4946 setupDeviceOwner(); 4947 4948 // The device owner itself, any uid holding MANAGE_USERS permission and the system can 4949 // find out that the device has a device owner. 4950 assertThat(dpm.isDeviceManaged()).isTrue(); 4951 mContext.binder.callingUid = 1234567; 4952 mContext.callerPermissions.add(permission.MANAGE_USERS); 4953 assertThat(dpm.isDeviceManaged()).isTrue(); 4954 mContext.callerPermissions.remove(permission.MANAGE_USERS); 4955 mContext.binder.clearCallingIdentity(); 4956 assertThat(dpm.isDeviceManaged()).isTrue(); 4957 4958 clearDeviceOwner(); 4959 4960 // Any uid holding MANAGE_USERS permission and the system can find out that the device does 4961 // not have a device owner. 4962 mContext.binder.callingUid = 1234567; 4963 mContext.callerPermissions.add(permission.MANAGE_USERS); 4964 assertThat(dpm.isDeviceManaged()).isFalse(); 4965 mContext.callerPermissions.remove(permission.MANAGE_USERS); 4966 mContext.binder.clearCallingIdentity(); 4967 assertThat(dpm.isDeviceManaged()).isFalse(); 4968 } 4969 4970 @Test testDeviceOwnerOrganizationName()4971 public void testDeviceOwnerOrganizationName() throws Exception { 4972 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 4973 setupDeviceOwner(); 4974 4975 dpm.setOrganizationName(admin1, "organization"); 4976 4977 // Device owner can retrieve organization managing the device. 4978 assertThat(dpm.getDeviceOwnerOrganizationName()).isEqualTo("organization"); 4979 4980 // Any uid holding MANAGE_USERS permission can retrieve organization managing the device. 4981 mContext.binder.callingUid = 1234567; 4982 mContext.callerPermissions.add(permission.MANAGE_USERS); 4983 assertThat(dpm.getDeviceOwnerOrganizationName()).isEqualTo("organization"); 4984 mContext.callerPermissions.remove(permission.MANAGE_USERS); 4985 4986 // System can retrieve organization managing the device. 4987 mContext.binder.clearCallingIdentity(); 4988 assertThat(dpm.getDeviceOwnerOrganizationName()).isEqualTo("organization"); 4989 4990 // Removing the device owner clears the organization managing the device. 4991 clearDeviceOwner(); 4992 assertThat(dpm.getDeviceOwnerOrganizationName()).isNull(); 4993 } 4994 4995 @Test testWipeDataManagedProfile()4996 public void testWipeDataManagedProfile() throws Exception { 4997 final int MANAGED_PROFILE_USER_ID = 15; 4998 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 4999 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 5000 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 5001 mServiceContext.permissions.add(permission.INTERACT_ACROSS_USERS_FULL); 5002 5003 // Even if the caller is the managed profile, the current user is the user 0 5004 when(getServices().iactivityManager.getCurrentUser()) 5005 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5006 // Get mock reason string since we throw an IAE with empty string input. 5007 when(mContext.getResources().getString(R.string.work_profile_deleted_description_dpm_wipe)) 5008 .thenReturn("Just a test string."); 5009 5010 dpm.wipeData(0); 5011 verify(getServices().userManagerInternal).removeUserEvenWhenDisallowed( 5012 MANAGED_PROFILE_USER_ID); 5013 } 5014 5015 @Test 5016 @Ignore("b/277916462") testWipeDataManagedProfileOnOrganizationOwnedDevice()5017 public void testWipeDataManagedProfileOnOrganizationOwnedDevice() throws Exception { 5018 setupProfileOwner(); 5019 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 5020 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_DEVICE_POLICY_MANAGER, 5021 FLAG_ENABLE_WORK_PROFILE_TELEPHONY, "true", false); 5022 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY, 5023 FLAG_ENABLE_WORK_PROFILE_TELEPHONY, "true", false); 5024 // Even if the caller is the managed profile, the current user is the user 0 5025 when(getServices().iactivityManager.getCurrentUser()) 5026 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5027 // Get mock reason string since we throw an IAE with empty string input. 5028 when(mContext.getResources().getString(R.string.work_profile_deleted_description_dpm_wipe)) 5029 .thenReturn("Just a test string."); 5030 when(getServices().userManager.getProfileParent(CALLER_USER_HANDLE)) 5031 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5032 when(getServices().userManager.getPrimaryUser()) 5033 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5034 when(getServices().subscriptionManager.getActiveSubscriptionIdList(false)).thenReturn( 5035 new int[1]); 5036 // Set some device-wide policies: 5037 // Security logging 5038 when(getServices().settings.securityLogGetLoggingEnabledProperty()).thenReturn(true); 5039 // System update policy 5040 dpms.mOwners.setSystemUpdatePolicy(SystemUpdatePolicy.createAutomaticInstallPolicy()); 5041 // Make it look as if FRP agent is present. 5042 when(dpms.mMockInjector.getPersistentDataBlockManagerInternal().getAllowedUid()) 5043 .thenReturn(12345 /* some UID in user 0 */); 5044 // Make personal apps look suspended 5045 dpms.getUserData(UserHandle.USER_SYSTEM).mAppsSuspended = true; 5046 // Screen capture 5047 dpm.setScreenCaptureDisabled(admin1, true); 5048 5049 dpm.wipeData(0); 5050 verify(getServices().userManagerInternal).removeUserEvenWhenDisallowed(CALLER_USER_HANDLE); 5051 5052 // Make sure COPE restrictions are lifted: 5053 verify(getServices().userManager).setUserRestriction( 5054 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, false, UserHandle.SYSTEM); 5055 verify(getServices().userManager).setUserRestriction( 5056 UserManager.DISALLOW_ADD_USER, false, UserHandle.SYSTEM); 5057 5058 clearInvocations(getServices().iwindowManager); 5059 5060 // Some device-wide policies are getting cleaned-up after the user is removed. 5061 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5062 sendBroadcastWithUser(dpms, Intent.ACTION_USER_REMOVED, CALLER_USER_HANDLE); 5063 5064 // Screenlock info should be removed 5065 verify(getServices().lockPatternUtils).setDeviceOwnerInfo(null); 5066 // Wifi config lockdown should be lifted 5067 verify(getServices().settings).settingsGlobalPutInt( 5068 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0); 5069 // System update policy should be removed 5070 assertThat(dpms.mOwners.getSystemUpdatePolicy()).isNull(); 5071 // FRP agent should be notified 5072 verify(mContext.spiedContext, times(0)).sendBroadcastAsUser( 5073 MockUtils.checkIntentAction( 5074 DevicePolicyManager.ACTION_RESET_PROTECTION_POLICY_CHANGED), 5075 MockUtils.checkUserHandle(UserHandle.USER_SYSTEM)); 5076 // Refresh strong auth timeout 5077 verify(getServices().lockSettingsInternal).refreshStrongAuthTimeout(UserHandle.USER_SYSTEM); 5078 // Refresh screen capture 5079 verify(getServices().iwindowManager).refreshScreenCaptureDisabled(); 5080 // Unsuspend personal apps 5081 verify(getServices().packageManagerInternal) 5082 .unsuspendAdminSuspendedPackages(UserHandle.USER_SYSTEM); 5083 verify(getServices().subscriptionManager).setSubscriptionUserHandle(0, null); 5084 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_DEVICE_POLICY_MANAGER, 5085 FLAG_ENABLE_WORK_PROFILE_TELEPHONY, "false", false); 5086 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_TELEPHONY, 5087 FLAG_ENABLE_WORK_PROFILE_TELEPHONY, "false", false); 5088 } 5089 5090 @Test testWipeDataManagedProfileDisallowed()5091 public void testWipeDataManagedProfileDisallowed() throws Exception { 5092 final int MANAGED_PROFILE_USER_ID = 15; 5093 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 5094 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 5095 5096 // Even if the caller is the managed profile, the current user is the user 0 5097 when(getServices().iactivityManager.getCurrentUser()) 5098 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5099 5100 when(getServices().userManager.getUserRestrictionSource( 5101 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, 5102 UserHandle.of(MANAGED_PROFILE_USER_ID))) 5103 .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); 5104 when(mContext.getResources().getString(R.string.work_profile_deleted_description_dpm_wipe)). 5105 thenReturn("Just a test string."); 5106 5107 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 5108 // The PO is not allowed to remove the profile if the user restriction was set on the 5109 // profile by the system 5110 assertExpectException(SecurityException.class, /* messageRegex= */ null, 5111 () -> dpm.wipeData(0)); 5112 } 5113 5114 @Test testWipeDevice_DeviceOwner()5115 public void testWipeDevice_DeviceOwner() throws Exception { 5116 setDeviceOwner(); 5117 when(getServices().userManager.getUserRestrictionSource( 5118 UserManager.DISALLOW_FACTORY_RESET, 5119 UserHandle.SYSTEM)) 5120 .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); 5121 when(mContext.getResources().getString(R.string.work_profile_deleted_description_dpm_wipe)). 5122 thenReturn("Just a test string."); 5123 5124 dpm.wipeDevice(0); 5125 5126 verifyRebootWipeUserData(/* wipeEuicc= */ false); 5127 } 5128 5129 @Test testWipeEuiccDataEnabled()5130 public void testWipeEuiccDataEnabled() throws Exception { 5131 setDeviceOwner(); 5132 when(getServices().userManager.getUserRestrictionSource( 5133 UserManager.DISALLOW_FACTORY_RESET, 5134 UserHandle.SYSTEM)) 5135 .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); 5136 when(mContext.getResources().getString(R.string.work_profile_deleted_description_dpm_wipe)). 5137 thenReturn("Just a test string."); 5138 5139 dpm.wipeDevice(WIPE_EUICC); 5140 5141 verifyRebootWipeUserData(/* wipeEuicc= */ true); 5142 } 5143 5144 @Test testWipeDevice_DeviceOwnerDisallowed()5145 public void testWipeDevice_DeviceOwnerDisallowed() throws Exception { 5146 setDeviceOwner(); 5147 when(getServices().userManager.getUserRestrictionSource( 5148 UserManager.DISALLOW_FACTORY_RESET, 5149 UserHandle.SYSTEM)) 5150 .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); 5151 when(mContext.getResources().getString(R.string.work_profile_deleted_description_dpm_wipe)). 5152 thenReturn("Just a test string."); 5153 // The DO is not allowed to wipe the device if the user restriction was set 5154 // by the system 5155 assertExpectException(SecurityException.class, /* messageRegex= */ null, 5156 () -> dpm.wipeDevice(0)); 5157 } 5158 5159 @Test testMaximumFailedPasswordAttemptsReachedManagedProfile()5160 public void testMaximumFailedPasswordAttemptsReachedManagedProfile() throws Exception { 5161 final int MANAGED_PROFILE_USER_ID = 15; 5162 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 5163 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 5164 5165 // Even if the caller is the managed profile, the current user is the user 0 5166 when(getServices().iactivityManager.getCurrentUser()) 5167 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5168 5169 when(getServices().userManager.getUserRestrictionSource( 5170 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, 5171 UserHandle.of(MANAGED_PROFILE_USER_ID))) 5172 .thenReturn(UserManager.RESTRICTION_SOURCE_PROFILE_OWNER); 5173 5174 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 5175 dpm.setMaximumFailedPasswordsForWipe(admin1, 3); 5176 5177 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5178 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 5179 // Failed password attempts on the parent user are taken into account, as there isn't a 5180 // separate work challenge. 5181 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5182 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5183 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5184 5185 // The profile should be wiped even if DISALLOW_REMOVE_MANAGED_PROFILE is enabled, because 5186 // both the user restriction and the policy were set by the PO. 5187 verify(getServices().userManagerInternal).removeUserEvenWhenDisallowed( 5188 MANAGED_PROFILE_USER_ID); 5189 verifyZeroInteractions(getServices().recoverySystem); 5190 } 5191 5192 @Test testMaximumFailedPasswordAttemptsReachedManagedProfileDisallowed()5193 public void testMaximumFailedPasswordAttemptsReachedManagedProfileDisallowed() 5194 throws Exception { 5195 final int MANAGED_PROFILE_USER_ID = 15; 5196 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 5197 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 5198 5199 // Even if the caller is the managed profile, the current user is the user 0 5200 when(getServices().iactivityManager.getCurrentUser()) 5201 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5202 5203 when(getServices().userManager.getUserRestrictionSource( 5204 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, 5205 UserHandle.of(MANAGED_PROFILE_USER_ID))) 5206 .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); 5207 5208 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 5209 dpm.setMaximumFailedPasswordsForWipe(admin1, 3); 5210 5211 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5212 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 5213 // Failed password attempts on the parent user are taken into account, as there isn't a 5214 // separate work challenge. 5215 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5216 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5217 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5218 5219 // DISALLOW_REMOVE_MANAGED_PROFILE was set by the system, not the PO, so the profile is 5220 // not wiped. 5221 verify(getServices().userManagerInternal, never()) 5222 .removeUserEvenWhenDisallowed(anyInt()); 5223 verifyZeroInteractions(getServices().recoverySystem); 5224 } 5225 5226 @Test testMaximumFailedPasswordAttemptsReachedDeviceOwner()5227 public void testMaximumFailedPasswordAttemptsReachedDeviceOwner() throws Exception { 5228 setDeviceOwner(); 5229 when(getServices().userManager.getUserRestrictionSource( 5230 UserManager.DISALLOW_FACTORY_RESET, 5231 UserHandle.SYSTEM)) 5232 .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); 5233 5234 dpm.setMaximumFailedPasswordsForWipe(admin1, 3); 5235 5236 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5237 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 5238 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5239 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5240 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5241 5242 // The device should be wiped even if DISALLOW_FACTORY_RESET is enabled, because both the 5243 // user restriction and the policy were set by the DO. 5244 verifyRebootWipeUserData(/* wipeEuicc= */ false); 5245 } 5246 5247 @Test testMaximumFailedPasswordAttemptsReachedDeviceOwnerDisallowed()5248 public void testMaximumFailedPasswordAttemptsReachedDeviceOwnerDisallowed() throws Exception { 5249 setDeviceOwner(); 5250 when(getServices().userManager.getUserRestrictionSource( 5251 UserManager.DISALLOW_FACTORY_RESET, 5252 UserHandle.SYSTEM)) 5253 .thenReturn(UserManager.RESTRICTION_SOURCE_SYSTEM); 5254 5255 dpm.setMaximumFailedPasswordsForWipe(admin1, 3); 5256 5257 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5258 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 5259 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5260 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5261 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5262 5263 // DISALLOW_FACTORY_RESET was set by the system, not the DO, so the device is not wiped. 5264 verifyZeroInteractions(getServices().recoverySystem); 5265 verify(getServices().userManagerInternal, never()) 5266 .removeUserEvenWhenDisallowed(anyInt()); 5267 } 5268 5269 @Test testMaximumFailedDevicePasswordAttemptsReachedOrgOwnedManagedProfile()5270 public void testMaximumFailedDevicePasswordAttemptsReachedOrgOwnedManagedProfile() 5271 throws Exception { 5272 final int MANAGED_PROFILE_USER_ID = 15; 5273 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 5274 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 5275 5276 // Even if the caller is the managed profile, the current user is the user 0 5277 when(getServices().iactivityManager.getCurrentUser()) 5278 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5279 5280 configureProfileOwnerOfOrgOwnedDevice(admin1, MANAGED_PROFILE_USER_ID); 5281 5282 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 5283 dpm.setMaximumFailedPasswordsForWipe(admin1, 3); 5284 5285 assertThat(dpm.getMaximumFailedPasswordsForWipe(admin1)).isEqualTo(3); 5286 assertThat(dpm.getMaximumFailedPasswordsForWipe(null)).isEqualTo(3); 5287 5288 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5289 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 5290 5291 assertThat(dpm.getMaximumFailedPasswordsForWipe(null, UserHandle.USER_SYSTEM)).isEqualTo(3); 5292 // Check that primary will be wiped as a result of failed primary user unlock attempts. 5293 assertThat(dpm.getProfileWithMinimumFailedPasswordsForWipe(UserHandle.USER_SYSTEM)) 5294 .isEqualTo(UserHandle.USER_SYSTEM); 5295 5296 // Failed password attempts on the parent user are taken into account, as there isn't a 5297 // separate work challenge. 5298 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5299 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5300 dpm.reportFailedPasswordAttempt(UserHandle.USER_SYSTEM); 5301 5302 // For managed profile on an organization owned device, the whole device should be wiped. 5303 verifyRebootWipeUserData(/* wipeEuicc= */ false); 5304 } 5305 5306 @Test testMaximumFailedProfilePasswordAttemptsReachedOrgOwnedManagedProfile()5307 public void testMaximumFailedProfilePasswordAttemptsReachedOrgOwnedManagedProfile() 5308 throws Exception { 5309 final int MANAGED_PROFILE_USER_ID = 15; 5310 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 5311 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 5312 5313 // Even if the caller is the managed profile, the current user is the user 0 5314 when(getServices().iactivityManager.getCurrentUser()) 5315 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5316 5317 doReturn(true).when(getServices().lockPatternUtils) 5318 .isSeparateProfileChallengeEnabled(MANAGED_PROFILE_USER_ID); 5319 5320 // Configure separate challenge. 5321 configureProfileOwnerOfOrgOwnedDevice(admin1, MANAGED_PROFILE_USER_ID); 5322 5323 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 5324 dpm.setMaximumFailedPasswordsForWipe(admin1, 3); 5325 5326 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5327 mContext.callerPermissions.add(permission.BIND_DEVICE_ADMIN); 5328 5329 assertThat(dpm.getMaximumFailedPasswordsForWipe(null, UserHandle.USER_SYSTEM)).isEqualTo(0); 5330 assertThat(dpm.getMaximumFailedPasswordsForWipe(null, MANAGED_PROFILE_USER_ID)) 5331 .isEqualTo(3); 5332 // Check that the policy is not affecting primary profile challenge. 5333 assertThat(dpm.getProfileWithMinimumFailedPasswordsForWipe(UserHandle.USER_SYSTEM)) 5334 .isEqualTo(UserHandle.USER_NULL); 5335 // Check that primary will be wiped as a result of failed profile unlock attempts. 5336 assertThat(dpm.getProfileWithMinimumFailedPasswordsForWipe(MANAGED_PROFILE_USER_ID)) 5337 .isEqualTo(UserHandle.USER_SYSTEM); 5338 5339 // Simulate three failed attempts at solving the separate challenge. 5340 dpm.reportFailedPasswordAttempt(MANAGED_PROFILE_USER_ID); 5341 dpm.reportFailedPasswordAttempt(MANAGED_PROFILE_USER_ID); 5342 dpm.reportFailedPasswordAttempt(MANAGED_PROFILE_USER_ID); 5343 5344 // For managed profile on an organization owned device, the whole device should be wiped. 5345 verifyRebootWipeUserData(/* wipeEuicc= */ false); 5346 } 5347 5348 @Test testGetPermissionGrantState()5349 public void testGetPermissionGrantState() throws Exception { 5350 final String permission = "some.permission"; 5351 final String app1 = "com.example.app1"; 5352 final String app2 = "com.example.app2"; 5353 5354 when(getServices().ipackageManager.checkPermission(eq(permission), eq(app1), anyInt())) 5355 .thenReturn(PackageManager.PERMISSION_GRANTED); 5356 doReturn(PackageManager.FLAG_PERMISSION_POLICY_FIXED).when(getServices().packageManager) 5357 .getPermissionFlags(permission, app1, UserHandle.SYSTEM); 5358 when(getServices().packageManager.getPermissionFlags(permission, app1, 5359 UserHandle.of(CALLER_USER_HANDLE))) 5360 .thenReturn(PackageManager.FLAG_PERMISSION_POLICY_FIXED); 5361 when(getServices().ipackageManager.checkPermission(eq(permission), eq(app2), anyInt())) 5362 .thenReturn(PackageManager.PERMISSION_DENIED); 5363 doReturn(0).when(getServices().packageManager).getPermissionFlags(permission, app2, 5364 UserHandle.SYSTEM); 5365 when(getServices().packageManager.getPermissionFlags(permission, app2, 5366 UserHandle.of(CALLER_USER_HANDLE))).thenReturn(0); 5367 5368 // System can retrieve permission grant state. 5369 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 5370 mContext.packageName = "android"; 5371 assertThat(dpm.getPermissionGrantState(null, app1, permission)) 5372 .isEqualTo(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED); 5373 assertThat(dpm.getPermissionGrantState(null, app2, permission)) 5374 .isEqualTo(DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT); 5375 5376 // A regular app cannot retrieve permission grant state. 5377 mContext.binder.callingUid = setupPackageInPackageManager(app1, 1); 5378 mContext.packageName = app1; 5379 assertExpectException(SecurityException.class, /* messageRegex= */ null, 5380 () -> dpm.getPermissionGrantState(null, app1, permission)); 5381 5382 // Profile owner can retrieve permission grant state. 5383 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 5384 mContext.packageName = admin1.getPackageName(); 5385 setAsProfileOwner(admin1); 5386 assertThat(dpm.getPermissionGrantState(admin1, app1, permission)) 5387 .isEqualTo(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED); 5388 assertThat(dpm.getPermissionGrantState(admin1, app2, permission)) 5389 .isEqualTo(DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT); 5390 } 5391 5392 @Test 5393 @Ignore("b/277916462") testResetPasswordWithToken()5394 public void testResetPasswordWithToken() throws Exception { 5395 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 5396 setupDeviceOwner(); 5397 // test token validation 5398 assertExpectException(IllegalArgumentException.class, /* messageRegex= */ null, 5399 () -> dpm.setResetPasswordToken(admin1, new byte[31])); 5400 5401 // test adding a token 5402 final byte[] token = new byte[32]; 5403 final long handle = 123456; 5404 final String password = "password"; 5405 when(getServices().lockPatternUtils.addEscrowToken(eq(token), eq(UserHandle.USER_SYSTEM), 5406 nullable(EscrowTokenStateChangeCallback.class))) 5407 .thenReturn(handle); 5408 assertThat(dpm.setResetPasswordToken(admin1, token)).isTrue(); 5409 5410 // test password activation 5411 when(getServices().lockPatternUtils.isEscrowTokenActive(handle, UserHandle.USER_SYSTEM)) 5412 .thenReturn(true); 5413 assertThat(dpm.isResetPasswordTokenActive(admin1)).isTrue(); 5414 5415 // test reset password with token 5416 when(getServices().lockPatternUtils.setLockCredentialWithToken( 5417 LockscreenCredential.createPassword(password), handle, token, 5418 UserHandle.USER_SYSTEM)).thenReturn(true); 5419 assertThat(dpm.resetPasswordWithToken(admin1, password, token, 0)).isTrue(); 5420 5421 // test removing a token 5422 when(getServices().lockPatternUtils.removeEscrowToken(handle, UserHandle.USER_SYSTEM)) 5423 .thenReturn(true); 5424 assertThat(dpm.clearResetPasswordToken(admin1)).isTrue(); 5425 } 5426 5427 @Test 5428 @Ignore("b/277916462") resetPasswordWithToken_NumericPin()5429 public void resetPasswordWithToken_NumericPin() throws Exception { 5430 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 5431 setupDeviceOwner(); 5432 // adding a token 5433 final byte[] token = new byte[32]; 5434 final long handle = 123456; 5435 when(getServices().lockPatternUtils.addEscrowToken(eq(token), eq(UserHandle.USER_SYSTEM), 5436 nullable(EscrowTokenStateChangeCallback.class))) 5437 .thenReturn(handle); 5438 assertThat(dpm.setResetPasswordToken(admin1, token)).isTrue(); 5439 5440 // Test resetting with a numeric pin 5441 final String pin = "123456"; 5442 when(getServices().lockPatternUtils.setLockCredentialWithToken( 5443 LockscreenCredential.createPin(pin), handle, token, 5444 UserHandle.USER_SYSTEM)).thenReturn(true); 5445 assertThat(dpm.resetPasswordWithToken(admin1, pin, token, 0)).isTrue(); 5446 } 5447 5448 @Test 5449 @Ignore("b/277916462") resetPasswordWithToken_EmptyPassword()5450 public void resetPasswordWithToken_EmptyPassword() throws Exception { 5451 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 5452 setupDeviceOwner(); 5453 // adding a token 5454 final byte[] token = new byte[32]; 5455 final long handle = 123456; 5456 when(getServices().lockPatternUtils.addEscrowToken(eq(token), eq(UserHandle.USER_SYSTEM), 5457 nullable(EscrowTokenStateChangeCallback.class))) 5458 .thenReturn(handle); 5459 assertThat(dpm.setResetPasswordToken(admin1, token)).isTrue(); 5460 5461 // Test resetting with an empty password 5462 final String password = ""; 5463 when(getServices().lockPatternUtils.setLockCredentialWithToken( 5464 LockscreenCredential.createNone(), handle, token, 5465 UserHandle.USER_SYSTEM)).thenReturn(true); 5466 assertThat(dpm.resetPasswordWithToken(admin1, password, token, 0)).isTrue(); 5467 } 5468 5469 @Test testIsActivePasswordSufficient()5470 public void testIsActivePasswordSufficient() throws Exception { 5471 assumeDeprecatedPasswordApisSupported(); 5472 5473 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 5474 mContext.packageName = admin1.getPackageName(); 5475 setupDeviceOwner(); 5476 5477 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5478 dpm.setPasswordMinimumLength(admin1, 8); 5479 dpm.setPasswordMinimumLetters(admin1, 6); 5480 dpm.setPasswordMinimumLowerCase(admin1, 3); 5481 dpm.setPasswordMinimumUpperCase(admin1, 1); 5482 dpm.setPasswordMinimumNonLetter(admin1, 1); 5483 dpm.setPasswordMinimumNumeric(admin1, 1); 5484 dpm.setPasswordMinimumSymbols(admin1, 0); 5485 5486 reset(mContext.spiedContext); 5487 5488 PasswordMetrics passwordMetricsNoSymbols = metricsForPassword("abcdXYZ5"); 5489 5490 setActivePasswordState(passwordMetricsNoSymbols); 5491 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5492 5493 initializeDpms(); 5494 reset(mContext.spiedContext); 5495 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5496 5497 // This call simulates the user entering the password for the first time after a reboot. 5498 // This causes password metrics to be reloaded into memory. Until this happens, 5499 // dpm.isActivePasswordSufficient() will continue to return its last checkpointed value, 5500 // even if the DPC changes password requirements so that the password no longer meets the 5501 // requirements. This is a known limitation of the current implementation of 5502 // isActivePasswordSufficient() - see b/34218769. 5503 setActivePasswordState(passwordMetricsNoSymbols); 5504 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5505 5506 dpm.setPasswordMinimumSymbols(admin1, 1); 5507 // This assertion would fail if we had not called setActivePasswordState() again after 5508 // initializeDpms() - see previous comment. 5509 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5510 5511 initializeDpms(); 5512 reset(mContext.spiedContext); 5513 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5514 5515 PasswordMetrics passwordMetricsWithSymbols = metricsForPassword("abcd.XY5"); 5516 5517 setActivePasswordState(passwordMetricsWithSymbols); 5518 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5519 } 5520 5521 @Test testIsActivePasswordSufficient_noLockScreen()5522 public void testIsActivePasswordSufficient_noLockScreen() throws Exception { 5523 assumeDeprecatedPasswordApisSupported(); 5524 5525 // If there is no lock screen, the password is considered empty no matter what, because 5526 // it provides no security. 5527 when(getServices().lockPatternUtils.hasSecureLockScreen()).thenReturn(false); 5528 5529 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 5530 mContext.packageName = admin1.getPackageName(); 5531 setupDeviceOwner(); 5532 final int userHandle = UserHandle.getUserId(mContext.binder.callingUid); 5533 // When there is no lockscreen, user password metrics is always empty. 5534 when(getServices().lockSettingsInternal.getUserPasswordMetrics(userHandle)) 5535 .thenReturn(new PasswordMetrics(CREDENTIAL_TYPE_NONE)); 5536 5537 // If no password requirements are set, isActivePasswordSufficient should succeed. 5538 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5539 5540 // Now set some password quality requirements. 5541 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); 5542 5543 reset(mContext.spiedContext); 5544 // This should be ignored, as there is no lock screen. 5545 dpm.reportPasswordChanged(new PasswordMetrics(CREDENTIAL_TYPE_NONE), userHandle); 5546 5547 // No broadcast should be sent. 5548 verify(mContext.spiedContext, times(0)).sendBroadcastAsUser( 5549 MockUtils.checkIntentAction(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED), 5550 MockUtils.checkUserHandle(userHandle)); 5551 5552 // The active (nonexistent) password doesn't comply with the requirements. 5553 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5554 } 5555 5556 @Test testIsPasswordSufficientAfterProfileUnification()5557 public void testIsPasswordSufficientAfterProfileUnification() throws Exception { 5558 final int managedProfileUserId = CALLER_USER_HANDLE; 5559 final int managedProfileAdminUid = 5560 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5561 mContext.binder.callingUid = managedProfileAdminUid; 5562 5563 addManagedProfile(admin1, managedProfileAdminUid, admin1); 5564 doReturn(true).when(getServices().lockPatternUtils) 5565 .isSeparateProfileChallengeEnabled(managedProfileUserId); 5566 5567 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 5568 parentDpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_MEDIUM); 5569 5570 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5571 .thenReturn(metricsForPin("184342")); 5572 5573 // Numeric password is compliant with current requirement (QUALITY_NUMERIC set explicitly 5574 // on the parent admin) 5575 assertThat(dpm.isPasswordSufficientAfterProfileUnification(UserHandle.USER_SYSTEM, 5576 UserHandle.USER_NULL)).isTrue(); 5577 // Numeric password is not compliant if profile is to be unified: the profile has a 5578 // QUALITY_ALPHABETIC policy on itself which will be enforced on the password after 5579 // unification. 5580 assertThat(dpm.isPasswordSufficientAfterProfileUnification(UserHandle.USER_SYSTEM, 5581 managedProfileUserId)).isFalse(); 5582 } 5583 5584 @Test testGetAggregatedPasswordComplexity_IgnoreProfileRequirement()5585 public void testGetAggregatedPasswordComplexity_IgnoreProfileRequirement() 5586 throws Exception { 5587 final int managedProfileUserId = CALLER_USER_HANDLE; 5588 final int managedProfileAdminUid = 5589 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5590 mContext.binder.callingUid = managedProfileAdminUid; 5591 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 5592 5593 // Profile has a unified challenge 5594 doReturn(false).when(getServices().lockPatternUtils) 5595 .isSeparateProfileChallengeEnabled(managedProfileUserId); 5596 doReturn(true).when(getServices().lockPatternUtils) 5597 .isProfileWithUnifiedChallenge(managedProfileUserId); 5598 5599 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 5600 parentDpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW); 5601 5602 assertThat(dpms.getAggregatedPasswordComplexityForUser(UserHandle.USER_SYSTEM, true)) 5603 .isEqualTo(PASSWORD_COMPLEXITY_LOW); 5604 assertThat(dpms.getAggregatedPasswordComplexityForUser(UserHandle.USER_SYSTEM, false)) 5605 .isEqualTo(PASSWORD_COMPLEXITY_HIGH); 5606 } 5607 5608 @Test testGetAggregatedPasswordMetrics_IgnoreProfileRequirement()5609 public void testGetAggregatedPasswordMetrics_IgnoreProfileRequirement() 5610 throws Exception { 5611 assumeDeprecatedPasswordApisSupported(); 5612 5613 final int managedProfileUserId = CALLER_USER_HANDLE; 5614 final int managedProfileAdminUid = 5615 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5616 mContext.binder.callingUid = managedProfileAdminUid; 5617 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 5618 5619 // Profile has a unified challenge 5620 doReturn(false).when(getServices().lockPatternUtils) 5621 .isSeparateProfileChallengeEnabled(managedProfileUserId); 5622 doReturn(true).when(getServices().lockPatternUtils) 5623 .isProfileWithUnifiedChallenge(managedProfileUserId); 5624 5625 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5626 dpm.setPasswordMinimumLength(admin1, 8); 5627 dpm.setPasswordMinimumLetters(admin1, 1); 5628 dpm.setPasswordMinimumNumeric(admin1, 2); 5629 dpm.setPasswordMinimumSymbols(admin1, 3); 5630 5631 parentDpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); 5632 5633 PasswordMetrics deviceMetrics = 5634 dpms.getPasswordMinimumMetrics(UserHandle.USER_SYSTEM, true); 5635 assertThat(deviceMetrics.credType).isEqualTo(LockPatternUtils.CREDENTIAL_TYPE_PATTERN); 5636 5637 PasswordMetrics allMetrics = 5638 dpms.getPasswordMinimumMetrics(UserHandle.USER_SYSTEM, false); 5639 assertThat(allMetrics.credType).isEqualTo(LockPatternUtils.CREDENTIAL_TYPE_PASSWORD); 5640 assertThat(allMetrics.length).isEqualTo(8); 5641 assertThat(allMetrics.letters).isEqualTo(1); 5642 assertThat(allMetrics.numeric).isEqualTo(2); 5643 assertThat(allMetrics.symbols).isEqualTo(3); 5644 } 5645 5646 @Test testCanSetPasswordRequirementOnParentPreS()5647 public void testCanSetPasswordRequirementOnParentPreS() throws Exception { 5648 assumeDeprecatedPasswordApisSupported(); 5649 5650 final int managedProfileUserId = CALLER_USER_HANDLE; 5651 final int managedProfileAdminUid = 5652 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5653 mContext.binder.callingUid = managedProfileAdminUid; 5654 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 5655 dpms.mMockInjector.setChangeEnabledForPackage(165573442L, false, 5656 admin1.getPackageName(), managedProfileUserId); 5657 5658 parentDpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5659 assertThat(parentDpm.getPasswordQuality(admin1)) 5660 .isEqualTo(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5661 } 5662 5663 @Test testCannotSetPasswordRequirementOnParent()5664 public void testCannotSetPasswordRequirementOnParent() throws Exception { 5665 assumeDeprecatedPasswordApisSupported(); 5666 5667 final int managedProfileUserId = CALLER_USER_HANDLE; 5668 final int managedProfileAdminUid = 5669 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5670 mContext.binder.callingUid = managedProfileAdminUid; 5671 addManagedProfile(admin1, managedProfileAdminUid, admin1); 5672 dpms.mMockInjector.setChangeEnabledForPackage(165573442L, true, 5673 admin1.getPackageName(), managedProfileUserId); 5674 5675 try { 5676 assertExpectException(SecurityException.class, null, () -> 5677 parentDpm.setPasswordQuality( 5678 admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX)); 5679 } finally { 5680 dpms.mMockInjector.clearEnabledChanges(); 5681 } 5682 } 5683 5684 @Test isActivePasswordSufficient_SeparateWorkChallenge_ProfileQualityRequirementMet()5685 public void isActivePasswordSufficient_SeparateWorkChallenge_ProfileQualityRequirementMet() 5686 throws Exception { 5687 assumeDeprecatedPasswordApisSupported(); 5688 5689 // Create work profile with empty separate challenge 5690 final int managedProfileUserId = 15; 5691 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5692 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5693 /* separateChallenge */ true); 5694 5695 // Set profile password quality requirement. No password added yet so 5696 // profile.isActivePasswordSufficient should return false 5697 mContext.binder.callingUid = managedProfileAdminUid; 5698 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC); 5699 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5700 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5701 5702 // Set a work challenge and verify profile.isActivePasswordSufficient is now true 5703 when(getServices().lockSettingsInternal.getUserPasswordMetrics(managedProfileUserId)) 5704 .thenReturn(metricsForPassword("abcdXYZ5")); 5705 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5706 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5707 } 5708 5709 @Test isActivePasswordSufficient_SeparateWorkChallenge_ProfileComplexityRequirementMet()5710 public void isActivePasswordSufficient_SeparateWorkChallenge_ProfileComplexityRequirementMet() 5711 throws Exception { 5712 assumeDeprecatedPasswordApisSupported(); 5713 5714 // Create work profile with empty separate challenge 5715 final int managedProfileUserId = 15; 5716 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5717 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5718 /* separateChallenge */ true); 5719 5720 // Set profile password complexity requirement. No password added yet so 5721 // profile.isActivePasswordSufficient should return false 5722 mContext.binder.callingUid = managedProfileAdminUid; 5723 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_MEDIUM); 5724 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5725 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5726 5727 // Set a work challenge and verify profile.isActivePasswordSufficient is now true 5728 when(getServices().lockSettingsInternal.getUserPasswordMetrics(managedProfileUserId)) 5729 .thenReturn(metricsForPin("5156")); 5730 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5731 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5732 } 5733 5734 @Test isActivePasswordSufficient_SeparateWorkChallenge_ParentQualityRequirementMet()5735 public void isActivePasswordSufficient_SeparateWorkChallenge_ParentQualityRequirementMet() 5736 throws Exception { 5737 assumeDeprecatedPasswordApisSupported(); 5738 5739 // Create work profile with empty separate challenge 5740 final int managedProfileUserId = 15; 5741 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5742 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5743 /* separateChallenge */ true); 5744 5745 // Set parent password quality requirement. No password added yet so 5746 // parent.isActivePasswordSufficient should return false 5747 mContext.binder.callingUid = managedProfileAdminUid; 5748 parentDpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX); 5749 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5750 assertThat(parentDpm.isActivePasswordSufficient()).isFalse(); 5751 5752 // Set a device lockscreen and verify parent.isActivePasswordSufficient is now true 5753 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5754 .thenReturn(metricsForPassword("acbdXYZ5")); 5755 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5756 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5757 } 5758 5759 @Test isActivePasswordSufficient_SeparateWorkChallenge_ParentComplexityRequirementMet()5760 public void isActivePasswordSufficient_SeparateWorkChallenge_ParentComplexityRequirementMet() 5761 throws Exception { 5762 // Create work profile with empty separate challenge 5763 final int managedProfileUserId = 15; 5764 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5765 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5766 /* separateChallenge */ true); 5767 5768 // Set parent password complexity requirement. No password added yet so 5769 // parent.isActivePasswordSufficient should return false 5770 mContext.binder.callingUid = managedProfileAdminUid; 5771 parentDpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW); 5772 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5773 assertThat(parentDpm.isActivePasswordSufficient()).isFalse(); 5774 5775 // Set a device lockscreen and verify parent.isActivePasswordSufficient is now true 5776 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5777 .thenReturn(metricsForPin("1234")); 5778 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5779 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5780 } 5781 5782 @Test isActivePasswordSufficient_UnifiedWorkChallenge_ProfileQualityRequirementMet()5783 public void isActivePasswordSufficient_UnifiedWorkChallenge_ProfileQualityRequirementMet() 5784 throws Exception { 5785 assumeDeprecatedPasswordApisSupported(); 5786 5787 // Create work profile with unified challenge 5788 final int managedProfileUserId = 15; 5789 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5790 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5791 /* separateChallenge */ false); 5792 5793 // Set profile password quality requirement. No password added yet so 5794 // {profile, parent}.isActivePasswordSufficient should return false 5795 mContext.binder.callingUid = managedProfileAdminUid; 5796 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC); 5797 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5798 assertThat(parentDpm.isActivePasswordSufficient()).isFalse(); 5799 5800 // Set a device lockscreen and verify {profile, parent}.isActivePasswordSufficient is true 5801 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5802 .thenReturn(metricsForPassword("abcdXYZ5")); 5803 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5804 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5805 } 5806 5807 @Test isActivePasswordSufficient_UnifiedWorkChallenge_ProfileComplexityRequirementMet()5808 public void isActivePasswordSufficient_UnifiedWorkChallenge_ProfileComplexityRequirementMet() 5809 throws Exception { 5810 // Create work profile with unified challenge 5811 final int managedProfileUserId = 15; 5812 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5813 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5814 /* separateChallenge */ false); 5815 5816 // Set profile password complexity requirement. No password added yet so 5817 // {profile, parent}.isActivePasswordSufficient should return false 5818 mContext.binder.callingUid = managedProfileAdminUid; 5819 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 5820 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5821 assertThat(parentDpm.isActivePasswordSufficient()).isFalse(); 5822 5823 // Set a device lockscreen and verify {profile, parent}.isActivePasswordSufficient is true 5824 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5825 .thenReturn(metricsForPin("51567548")); 5826 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5827 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5828 } 5829 5830 @Test isActivePasswordSufficient_UnifiedWorkChallenge_ParentQualityRequirementMet()5831 public void isActivePasswordSufficient_UnifiedWorkChallenge_ParentQualityRequirementMet() 5832 throws Exception { 5833 assumeDeprecatedPasswordApisSupported(); 5834 5835 // Create work profile with unified challenge 5836 final int managedProfileUserId = 15; 5837 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5838 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5839 /* separateChallenge */ false); 5840 5841 // Set parent password quality requirement. No password added yet so 5842 // {profile, parent}.isActivePasswordSufficient should return false 5843 mContext.binder.callingUid = managedProfileAdminUid; 5844 parentDpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC); 5845 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5846 assertThat(parentDpm.isActivePasswordSufficient()).isFalse(); 5847 5848 // Set a device lockscreen and verify {profile, parent}.isActivePasswordSufficient is true 5849 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5850 .thenReturn(metricsForPassword("abcdXYZ5")); 5851 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5852 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5853 } 5854 5855 @Test isActivePasswordSufficient_UnifiedWorkChallenge_ParentComplexityRequirementMet()5856 public void isActivePasswordSufficient_UnifiedWorkChallenge_ParentComplexityRequirementMet() 5857 throws Exception { 5858 // Create work profile with unified challenge 5859 final int managedProfileUserId = 15; 5860 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 5861 addManagedProfileForPasswordTests(managedProfileUserId, managedProfileAdminUid, 5862 /* separateChallenge */ false); 5863 5864 // Set parent password complexity requirement. No password added yet so 5865 // {profile, parent}.isActivePasswordSufficient should return false 5866 mContext.binder.callingUid = managedProfileAdminUid; 5867 parentDpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_MEDIUM); 5868 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 5869 assertThat(parentDpm.isActivePasswordSufficient()).isFalse(); 5870 5871 // Set a device lockscreen and verify {profile, parent}.isActivePasswordSufficient is true 5872 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5873 .thenReturn(metricsForPin("5156")); 5874 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 5875 assertThat(parentDpm.isActivePasswordSufficient()).isTrue(); 5876 } 5877 addManagedProfileForPasswordTests(int userId, int adminUid, boolean separateChallenge)5878 private void addManagedProfileForPasswordTests(int userId, int adminUid, 5879 boolean separateChallenge) throws Exception { 5880 addManagedProfile(admin1, adminUid, admin1); 5881 when(getServices().userManager.getProfileParent(userId)) 5882 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5883 doReturn(separateChallenge).when(getServices().lockPatternUtils) 5884 .isSeparateProfileChallengeEnabled(userId); 5885 doReturn(!separateChallenge).when(getServices().lockPatternUtils) 5886 .isProfileWithUnifiedChallenge(userId); 5887 when(getServices().userManager.getCredentialOwnerProfile(userId)) 5888 .thenReturn(separateChallenge ? userId : UserHandle.USER_SYSTEM); 5889 when(getServices().lockSettingsInternal.getUserPasswordMetrics(userId)) 5890 .thenReturn(new PasswordMetrics(CREDENTIAL_TYPE_NONE)); 5891 when(getServices().lockSettingsInternal.getUserPasswordMetrics(UserHandle.USER_SYSTEM)) 5892 .thenReturn(new PasswordMetrics(CREDENTIAL_TYPE_NONE)); 5893 } 5894 5895 @Test testPasswordQualityAppliesToParentPreS()5896 public void testPasswordQualityAppliesToParentPreS() throws Exception { 5897 assumeDeprecatedPasswordApisSupported(); 5898 5899 final int managedProfileUserId = CALLER_USER_HANDLE; 5900 final int managedProfileAdminUid = 5901 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5902 mContext.binder.callingUid = managedProfileAdminUid; 5903 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 5904 when(getServices().userManager.getProfileParent(CALLER_USER_HANDLE)) 5905 .thenReturn(new UserInfo(UserHandle.USER_SYSTEM, "user system", 0)); 5906 5907 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5908 assertThat(parentDpm.getPasswordQuality(null)) 5909 .isEqualTo(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5910 } 5911 5912 @Test testPasswordQualityDoesNotApplyToParentPostS()5913 public void testPasswordQualityDoesNotApplyToParentPostS() throws Exception { 5914 final int managedProfileUserId = CALLER_USER_HANDLE; 5915 final int managedProfileAdminUid = 5916 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 5917 mContext.binder.callingUid = managedProfileAdminUid; 5918 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 5919 5920 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 5921 assertThat(parentDpm.getPasswordQuality(admin1)) 5922 .isEqualTo(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); 5923 } 5924 setActivePasswordState(PasswordMetrics passwordMetrics)5925 private void setActivePasswordState(PasswordMetrics passwordMetrics) 5926 throws Exception { 5927 final int userHandle = UserHandle.getUserId(mContext.binder.callingUid); 5928 final long ident = mContext.binder.clearCallingIdentity(); 5929 5930 when(getServices().lockSettingsInternal.getUserPasswordMetrics(userHandle)) 5931 .thenReturn(passwordMetrics); 5932 dpm.reportPasswordChanged(passwordMetrics, userHandle); 5933 5934 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 5935 MockUtils.checkIntentAction( 5936 DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED), 5937 MockUtils.checkUserHandle(userHandle), 5938 eq(null), 5939 any(Bundle.class)); 5940 5941 final Intent intent = new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED); 5942 intent.setComponent(admin1); 5943 intent.putExtra(Intent.EXTRA_USER, UserHandle.of(userHandle)); 5944 5945 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 5946 MockUtils.checkIntent(intent), 5947 MockUtils.checkUserHandle(userHandle), 5948 eq(null), 5949 any()); 5950 5951 // CertificateMonitor.updateInstalledCertificates is called on the background thread, 5952 // let it finish with system uid, otherwise it will throw and crash. 5953 flushTasks(dpms); 5954 5955 mContext.binder.restoreCallingIdentity(ident); 5956 } 5957 5958 @Test testIsCurrentInputMethodSetByOwnerForDeviceOwner()5959 public void testIsCurrentInputMethodSetByOwnerForDeviceOwner() throws Exception { 5960 final String currentIme = Settings.Secure.DEFAULT_INPUT_METHOD; 5961 final Uri currentImeUri = Settings.Secure.getUriFor(currentIme); 5962 final int deviceOwnerUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 5963 final int firstUserSystemUid = UserHandle.getUid(UserHandle.USER_SYSTEM, 5964 DpmMockContext.SYSTEM_UID); 5965 final int secondUserSystemUid = UserHandle.getUid(CALLER_USER_HANDLE, 5966 DpmMockContext.SYSTEM_UID); 5967 5968 // Set up a device owner. 5969 mContext.binder.callingUid = deviceOwnerUid; 5970 setupDeviceOwner(); 5971 5972 // First and second user set IMEs manually. 5973 mContext.binder.callingUid = firstUserSystemUid; 5974 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 5975 mContext.binder.callingUid = secondUserSystemUid; 5976 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 5977 5978 // Device owner changes IME for first user. 5979 mContext.binder.callingUid = deviceOwnerUid; 5980 when(getServices().settings.settingsSecureGetStringForUser(currentIme, 5981 UserHandle.USER_SYSTEM)).thenReturn("ime1"); 5982 dpm.setSecureSetting(admin1, currentIme, "ime2"); 5983 verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime2", 5984 UserHandle.USER_SYSTEM); 5985 reset(getServices().settings); 5986 dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); 5987 mContext.binder.callingUid = firstUserSystemUid; 5988 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 5989 mContext.binder.callingUid = secondUserSystemUid; 5990 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 5991 5992 // Second user changes IME manually. 5993 dpms.notifyChangeToContentObserver(currentImeUri, CALLER_USER_HANDLE); 5994 mContext.binder.callingUid = firstUserSystemUid; 5995 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 5996 mContext.binder.callingUid = secondUserSystemUid; 5997 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 5998 5999 // First user changes IME manually. 6000 dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); 6001 mContext.binder.callingUid = firstUserSystemUid; 6002 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6003 mContext.binder.callingUid = secondUserSystemUid; 6004 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6005 6006 // Device owner changes IME for first user again. 6007 mContext.binder.callingUid = deviceOwnerUid; 6008 when(getServices().settings.settingsSecureGetStringForUser(currentIme, 6009 UserHandle.USER_SYSTEM)).thenReturn("ime2"); 6010 dpm.setSecureSetting(admin1, currentIme, "ime3"); 6011 verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime3", 6012 UserHandle.USER_SYSTEM); 6013 dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); 6014 mContext.binder.callingUid = firstUserSystemUid; 6015 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6016 mContext.binder.callingUid = secondUserSystemUid; 6017 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6018 6019 // Restarting the DPMS should not lose information. 6020 initializeDpms(); 6021 mContext.binder.callingUid = firstUserSystemUid; 6022 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6023 mContext.binder.callingUid = secondUserSystemUid; 6024 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6025 6026 // Device owner can find out whether it set the current IME itself. 6027 mContext.binder.callingUid = deviceOwnerUid; 6028 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6029 6030 // Removing the device owner should clear the information that it set the current IME. 6031 clearDeviceOwner(); 6032 mContext.binder.callingUid = firstUserSystemUid; 6033 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6034 mContext.binder.callingUid = secondUserSystemUid; 6035 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6036 } 6037 6038 @Test testIsCurrentInputMethodSetByOwnerForProfileOwner()6039 public void testIsCurrentInputMethodSetByOwnerForProfileOwner() throws Exception { 6040 final String currentIme = Settings.Secure.DEFAULT_INPUT_METHOD; 6041 final Uri currentImeUri = Settings.Secure.getUriFor(currentIme); 6042 final int profileOwnerUid = DpmMockContext.CALLER_UID; 6043 final int firstUserSystemUid = UserHandle.getUid(UserHandle.USER_SYSTEM, 6044 DpmMockContext.SYSTEM_UID); 6045 final int secondUserSystemUid = UserHandle.getUid(CALLER_USER_HANDLE, 6046 DpmMockContext.SYSTEM_UID); 6047 6048 // Set up a profile owner. 6049 mContext.binder.callingUid = profileOwnerUid; 6050 setupProfileOwner(); 6051 6052 // First and second user set IMEs manually. 6053 mContext.binder.callingUid = firstUserSystemUid; 6054 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6055 mContext.binder.callingUid = secondUserSystemUid; 6056 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6057 6058 // Profile owner changes IME for second user. 6059 mContext.binder.callingUid = profileOwnerUid; 6060 when(getServices().settings.settingsSecureGetStringForUser(currentIme, 6061 CALLER_USER_HANDLE)).thenReturn("ime1"); 6062 dpm.setSecureSetting(admin1, currentIme, "ime2"); 6063 verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime2", 6064 CALLER_USER_HANDLE); 6065 reset(getServices().settings); 6066 dpms.notifyChangeToContentObserver(currentImeUri, CALLER_USER_HANDLE); 6067 mContext.binder.callingUid = firstUserSystemUid; 6068 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6069 mContext.binder.callingUid = secondUserSystemUid; 6070 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6071 6072 // First user changes IME manually. 6073 dpms.notifyChangeToContentObserver(currentImeUri, UserHandle.USER_SYSTEM); 6074 mContext.binder.callingUid = firstUserSystemUid; 6075 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6076 mContext.binder.callingUid = secondUserSystemUid; 6077 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6078 6079 // Second user changes IME manually. 6080 dpms.notifyChangeToContentObserver(currentImeUri, CALLER_USER_HANDLE); 6081 mContext.binder.callingUid = firstUserSystemUid; 6082 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6083 mContext.binder.callingUid = secondUserSystemUid; 6084 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6085 6086 // Profile owner changes IME for second user again. 6087 mContext.binder.callingUid = profileOwnerUid; 6088 when(getServices().settings.settingsSecureGetStringForUser(currentIme, 6089 CALLER_USER_HANDLE)).thenReturn("ime2"); 6090 dpm.setSecureSetting(admin1, currentIme, "ime3"); 6091 verify(getServices().settings).settingsSecurePutStringForUser(currentIme, "ime3", 6092 CALLER_USER_HANDLE); 6093 dpms.notifyChangeToContentObserver(currentImeUri, CALLER_USER_HANDLE); 6094 mContext.binder.callingUid = firstUserSystemUid; 6095 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6096 mContext.binder.callingUid = secondUserSystemUid; 6097 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6098 6099 // Restarting the DPMS should not lose information. 6100 initializeDpms(); 6101 mContext.binder.callingUid = firstUserSystemUid; 6102 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6103 mContext.binder.callingUid = secondUserSystemUid; 6104 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6105 6106 // Profile owner can find out whether it set the current IME itself. 6107 mContext.binder.callingUid = profileOwnerUid; 6108 assertThat(dpm.isCurrentInputMethodSetByOwner()).isTrue(); 6109 6110 // Removing the profile owner should clear the information that it set the current IME. 6111 dpm.clearProfileOwner(admin1); 6112 mContext.binder.callingUid = firstUserSystemUid; 6113 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6114 mContext.binder.callingUid = secondUserSystemUid; 6115 assertThat(dpm.isCurrentInputMethodSetByOwner()).isFalse(); 6116 } 6117 6118 @Test testSetPermittedCrossProfileNotificationListeners_unavailableForDo()6119 public void testSetPermittedCrossProfileNotificationListeners_unavailableForDo() 6120 throws Exception { 6121 // Set up a device owner. 6122 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 6123 setupDeviceOwner(); 6124 assertSetPermittedCrossProfileNotificationListenersUnavailable(mContext.binder.callingUid); 6125 } 6126 6127 @Test testSetPermittedCrossProfileNotificationListeners_unavailableForPoOnUser()6128 public void testSetPermittedCrossProfileNotificationListeners_unavailableForPoOnUser() 6129 throws Exception { 6130 // Set up a profile owner. 6131 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6132 setupProfileOwner(); 6133 assertSetPermittedCrossProfileNotificationListenersUnavailable(mContext.binder.callingUid); 6134 } 6135 assertSetPermittedCrossProfileNotificationListenersUnavailable( int adminUid)6136 private void assertSetPermittedCrossProfileNotificationListenersUnavailable( 6137 int adminUid) throws Exception { 6138 mContext.binder.callingUid = adminUid; 6139 final int userId = UserHandle.getUserId(adminUid); 6140 6141 final String packageName = "some.package"; 6142 assertThat(dpms.setPermittedCrossProfileNotificationListeners( 6143 admin1, Collections.singletonList(packageName))).isFalse(); 6144 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1)).isNull(); 6145 6146 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6147 assertThat(dpms.isNotificationListenerServicePermitted(packageName, userId)).isTrue(); 6148 6149 // Attempt to set to empty list (which means no listener is allowlisted) 6150 mContext.binder.callingUid = adminUid; 6151 assertThat(dpms.setPermittedCrossProfileNotificationListeners( 6152 admin1, emptyList())).isFalse(); 6153 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1)).isNull(); 6154 6155 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6156 assertThat(dpms.isNotificationListenerServicePermitted(packageName, userId)).isTrue(); 6157 } 6158 6159 @Test testIsNotificationListenerServicePermitted_onlySystemCanCall()6160 public void testIsNotificationListenerServicePermitted_onlySystemCanCall() throws Exception { 6161 // Set up a managed profile 6162 final int MANAGED_PROFILE_USER_ID = 15; 6163 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 6164 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 6165 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6166 6167 final String permittedListener = "some.package"; 6168 setupPackageInPackageManager( 6169 permittedListener, 6170 UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. 6171 /*appId=*/ 12345, /*flags=*/ 0); 6172 6173 assertThat(dpms.setPermittedCrossProfileNotificationListeners( 6174 admin1, Collections.singletonList(permittedListener))).isTrue(); 6175 6176 // isNotificationListenerServicePermitted should throw if not called from System. 6177 assertExpectException(SecurityException.class, /* messageRegex= */ null, 6178 () -> dpms.isNotificationListenerServicePermitted( 6179 permittedListener, MANAGED_PROFILE_USER_ID)); 6180 6181 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6182 assertThat(dpms.isNotificationListenerServicePermitted( 6183 permittedListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6184 } 6185 6186 @Test testSetPermittedCrossProfileNotificationListeners_managedProfile()6187 public void testSetPermittedCrossProfileNotificationListeners_managedProfile() 6188 throws Exception { 6189 // Set up a managed profile 6190 final int MANAGED_PROFILE_USER_ID = 15; 6191 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 6192 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 6193 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6194 6195 final String permittedListener = "permitted.package"; 6196 int appId = 12345; 6197 setupPackageInPackageManager( 6198 permittedListener, 6199 UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. 6200 appId, /*flags=*/ 0); 6201 6202 final String notPermittedListener = "not.permitted.package"; 6203 setupPackageInPackageManager( 6204 notPermittedListener, 6205 UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. 6206 ++appId, /*flags=*/ 0); 6207 6208 final String systemListener = "system.package"; 6209 setupPackageInPackageManager( 6210 systemListener, 6211 UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. 6212 ++appId, ApplicationInfo.FLAG_SYSTEM); 6213 6214 // By default all packages are allowed 6215 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1)).isNull(); 6216 6217 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6218 assertThat(dpms.isNotificationListenerServicePermitted( 6219 permittedListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6220 assertThat(dpms.isNotificationListenerServicePermitted( 6221 notPermittedListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6222 assertThat(dpms.isNotificationListenerServicePermitted( 6223 systemListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6224 6225 // Setting only one package in the allowlist 6226 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6227 assertThat(dpms.setPermittedCrossProfileNotificationListeners( 6228 admin1, Collections.singletonList(permittedListener))).isTrue(); 6229 final List<String> permittedListeners = 6230 dpms.getPermittedCrossProfileNotificationListeners(admin1); 6231 assertThat(permittedListeners.size()).isEqualTo(1); 6232 assertThat(permittedListeners.get(0)).isEqualTo(permittedListener); 6233 6234 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6235 assertThat(dpms.isNotificationListenerServicePermitted( 6236 permittedListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6237 assertThat(dpms.isNotificationListenerServicePermitted( 6238 notPermittedListener, MANAGED_PROFILE_USER_ID)).isFalse(); 6239 // System packages are always allowed (even if not in the allowlist) 6240 assertThat(dpms.isNotificationListenerServicePermitted( 6241 systemListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6242 6243 // Setting an empty allowlist - only system listeners allowed 6244 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6245 assertThat(dpms.setPermittedCrossProfileNotificationListeners( 6246 admin1, emptyList())).isTrue(); 6247 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1).size()).isEqualTo(0); 6248 6249 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6250 assertThat(dpms.isNotificationListenerServicePermitted( 6251 permittedListener, MANAGED_PROFILE_USER_ID)).isFalse(); 6252 assertThat(dpms.isNotificationListenerServicePermitted( 6253 notPermittedListener, MANAGED_PROFILE_USER_ID)).isFalse(); 6254 // System packages are always allowed (even if not in the allowlist) 6255 assertThat(dpms.isNotificationListenerServicePermitted( 6256 systemListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6257 6258 // Setting a null allowlist - all listeners allowed 6259 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6260 assertThat(dpms.setPermittedCrossProfileNotificationListeners(admin1, null)).isTrue(); 6261 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1)).isNull(); 6262 6263 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6264 assertThat(dpms.isNotificationListenerServicePermitted( 6265 permittedListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6266 assertThat(dpms.isNotificationListenerServicePermitted( 6267 notPermittedListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6268 assertThat(dpms.isNotificationListenerServicePermitted( 6269 systemListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6270 } 6271 6272 @Test testSetPermittedCrossProfileNotificationListeners_doesNotAffectPrimaryProfile()6273 public void testSetPermittedCrossProfileNotificationListeners_doesNotAffectPrimaryProfile() 6274 throws Exception { 6275 // Set up a managed profile 6276 final int MANAGED_PROFILE_USER_ID = 15; 6277 final int MANAGED_PROFILE_ADMIN_UID = UserHandle.getUid(MANAGED_PROFILE_USER_ID, 19436); 6278 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 6279 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6280 6281 final String nonSystemPackage = "non.system.package"; 6282 int appId = 12345; 6283 setupPackageInPackageManager( 6284 nonSystemPackage, 6285 UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. 6286 appId, /*flags=*/ 0); 6287 6288 final String systemListener = "system.package"; 6289 setupPackageInPackageManager( 6290 systemListener, 6291 UserHandle.USER_SYSTEM, // We check the packageInfo from the primary user. 6292 ++appId, ApplicationInfo.FLAG_SYSTEM); 6293 6294 // By default all packages are allowed (for all profiles) 6295 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1)).isNull(); 6296 6297 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6298 assertThat(dpms.isNotificationListenerServicePermitted( 6299 nonSystemPackage, MANAGED_PROFILE_USER_ID)).isTrue(); 6300 assertThat(dpms.isNotificationListenerServicePermitted( 6301 systemListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6302 assertThat(dpms.isNotificationListenerServicePermitted( 6303 nonSystemPackage, UserHandle.USER_SYSTEM)).isTrue(); 6304 assertThat(dpms.isNotificationListenerServicePermitted( 6305 systemListener, UserHandle.USER_SYSTEM)).isTrue(); 6306 6307 // Setting an empty allowlist - only system listeners allowed in managed profile, but 6308 // all allowed in primary profile 6309 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 6310 assertThat(dpms.setPermittedCrossProfileNotificationListeners( 6311 admin1, emptyList())).isTrue(); 6312 assertThat(dpms.getPermittedCrossProfileNotificationListeners(admin1).size()).isEqualTo(0); 6313 6314 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6315 assertThat(dpms.isNotificationListenerServicePermitted( 6316 nonSystemPackage, MANAGED_PROFILE_USER_ID)).isFalse(); 6317 assertThat(dpms.isNotificationListenerServicePermitted( 6318 systemListener, MANAGED_PROFILE_USER_ID)).isTrue(); 6319 assertThat(dpms.isNotificationListenerServicePermitted( 6320 nonSystemPackage, UserHandle.USER_SYSTEM)).isTrue(); 6321 assertThat(dpms.isNotificationListenerServicePermitted( 6322 systemListener, UserHandle.USER_SYSTEM)).isTrue(); 6323 } 6324 6325 @Test testGetOwnerInstalledCaCertsForDeviceOwner()6326 public void testGetOwnerInstalledCaCertsForDeviceOwner() throws Exception { 6327 mServiceContext.packageName = mRealTestContext.getPackageName(); 6328 mServiceContext.applicationInfo = new ApplicationInfo(); 6329 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6330 mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 6331 setDeviceOwner(); 6332 6333 verifyCanGetOwnerInstalledCaCerts(admin1, mAdmin1Context); 6334 } 6335 6336 @Test testGetOwnerInstalledCaCertsForProfileOwner()6337 public void testGetOwnerInstalledCaCertsForProfileOwner() throws Exception { 6338 mServiceContext.packageName = mRealTestContext.getPackageName(); 6339 mServiceContext.applicationInfo = new ApplicationInfo(); 6340 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6341 mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID; 6342 setAsProfileOwner(admin1); 6343 6344 verifyCanGetOwnerInstalledCaCerts(admin1, mAdmin1Context); 6345 verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval(admin1, mAdmin1Context); 6346 } 6347 6348 @Test testGetOwnerInstalledCaCertsForDelegate()6349 public void testGetOwnerInstalledCaCertsForDelegate() throws Exception { 6350 mServiceContext.packageName = mRealTestContext.getPackageName(); 6351 mServiceContext.applicationInfo = new ApplicationInfo(); 6352 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6353 mAdmin1Context.binder.callingUid = DpmMockContext.CALLER_UID; 6354 setAsProfileOwner(admin1); 6355 6356 var caller = new DpmMockContext(getServices(), mRealTestContext); 6357 caller.packageName = "com.example.delegate"; 6358 caller.binder.callingUid = setupPackageInPackageManager(caller.packageName, 6359 CALLER_USER_HANDLE, 20988, ApplicationInfo.FLAG_HAS_CODE); 6360 6361 // Make caller a delegated cert installer. 6362 runAsCaller(mAdmin1Context, dpms, 6363 dpm -> dpm.setCertInstallerPackage(admin1, caller.packageName)); 6364 6365 verifyCanGetOwnerInstalledCaCerts(null, caller); 6366 verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval(null, caller); 6367 } 6368 6369 @Test testDisallowSharingIntoProfileSetRestriction()6370 public void testDisallowSharingIntoProfileSetRestriction() { 6371 when(mServiceContext.resources.getString(R.string.config_managed_provisioning_package)) 6372 .thenReturn("com.android.managedprovisioning"); 6373 when(getServices().userManagerInternal.getProfileParentId(anyInt())) 6374 .thenReturn(UserHandle.USER_SYSTEM); 6375 mServiceContext.binder.callingPid = DpmMockContext.SYSTEM_PID; 6376 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6377 Bundle restriction = new Bundle(); 6378 restriction.putBoolean(UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE, true); 6379 6380 RestrictionsListener listener = new RestrictionsListener( 6381 mServiceContext, getServices().userManagerInternal, dpms); 6382 listener.onUserRestrictionsChanged(CALLER_USER_HANDLE, restriction, new Bundle()); 6383 6384 verifyDataSharingAppliedBroadcast(); 6385 } 6386 6387 @Test testDisallowSharingIntoProfileClearRestriction()6388 public void testDisallowSharingIntoProfileClearRestriction() { 6389 when(mServiceContext.resources.getString(R.string.config_managed_provisioning_package)) 6390 .thenReturn("com.android.managedprovisioning"); 6391 when(getServices().userManagerInternal.getProfileParentId(anyInt())) 6392 .thenReturn(UserHandle.USER_SYSTEM); 6393 mServiceContext.binder.callingPid = DpmMockContext.SYSTEM_PID; 6394 mServiceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6395 Bundle restriction = new Bundle(); 6396 restriction.putBoolean(UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE, true); 6397 6398 RestrictionsListener listener = new RestrictionsListener( 6399 mServiceContext, getServices().userManagerInternal, dpms); 6400 listener.onUserRestrictionsChanged(CALLER_USER_HANDLE, new Bundle(), restriction); 6401 6402 verifyDataSharingAppliedBroadcast(); 6403 } 6404 6405 @Test testDisallowSharingIntoProfileUnchanged()6406 public void testDisallowSharingIntoProfileUnchanged() { 6407 RestrictionsListener listener = new RestrictionsListener( 6408 mContext, getServices().userManagerInternal, dpms); 6409 listener.onUserRestrictionsChanged(CALLER_USER_HANDLE, new Bundle(), new Bundle()); 6410 verify(mContext.spiedContext, never()).sendBroadcastAsUser(any(), any()); 6411 } 6412 verifyDataSharingAppliedBroadcast()6413 private void verifyDataSharingAppliedBroadcast() { 6414 Intent expectedIntent = new Intent( 6415 DevicePolicyManager.ACTION_DATA_SHARING_RESTRICTION_APPLIED); 6416 verify(mContext.spiedContext, times(1)).sendBroadcastAsUser( 6417 MockUtils.checkIntent(expectedIntent), 6418 MockUtils.checkUserHandle(CALLER_USER_HANDLE)); 6419 } 6420 6421 @Test testOverrideApnAPIsFailWithPO()6422 public void testOverrideApnAPIsFailWithPO() throws Exception { 6423 when(getServices().packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) 6424 .thenReturn(true); 6425 // FEATURE_TELEPHONY is set in DPMS's constructor and therefore a new DPMS instance 6426 // is created after turning on the feature. 6427 initializeDpms(); 6428 setupProfileOwner(); 6429 ApnSetting apn = (new ApnSetting.Builder()) 6430 .setApnName("test") 6431 .setEntryName("test") 6432 .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT) 6433 .build(); 6434 assertExpectException(SecurityException.class, null, () -> 6435 dpm.addOverrideApn(admin1, apn)); 6436 assertExpectException(SecurityException.class, null, () -> 6437 dpm.updateOverrideApn(admin1, 0, apn)); 6438 assertExpectException(SecurityException.class, null, () -> 6439 dpm.removeOverrideApn(admin1, 0)); 6440 assertExpectException(SecurityException.class, null, () -> 6441 dpm.getOverrideApns(admin1)); 6442 assertExpectException(SecurityException.class, null, () -> 6443 dpm.setOverrideApnsEnabled(admin1, false)); 6444 assertExpectException(SecurityException.class, null, () -> 6445 dpm.isOverrideApnEnabled(admin1)); 6446 } 6447 verifyCanGetOwnerInstalledCaCerts( final ComponentName caller, final DpmMockContext callerContext)6448 private void verifyCanGetOwnerInstalledCaCerts( 6449 final ComponentName caller, final DpmMockContext callerContext) throws Exception { 6450 final String alias = "cert"; 6451 final byte[] caCert = TEST_CA.getBytes(); 6452 6453 // device admin (used for posting the tls notification) 6454 DpmMockContext admin1Context = mAdmin1Context; 6455 if (admin1.getPackageName().equals(callerContext.getPackageName())) { 6456 admin1Context = callerContext; 6457 } 6458 when(admin1Context.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); 6459 6460 // caller: device admin or delegated certificate installer 6461 callerContext.applicationInfo = new ApplicationInfo(); 6462 final UserHandle callerUser = callerContext.binder.getCallingUserHandle(); 6463 6464 // system_server 6465 final DpmMockContext serviceContext = mContext; 6466 serviceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6467 getServices().addPackageContext(callerUser, admin1Context); 6468 getServices().addPackageContext(callerUser, callerContext); 6469 6470 // Install a CA cert. 6471 runAsCaller(callerContext, dpms, (dpm) -> { 6472 when(getServices().keyChainConnection.getService().installCaCertificate(caCert)) 6473 .thenReturn(alias); 6474 assertThat(dpm.installCaCert(caller, caCert)).isTrue(); 6475 when(getServices().keyChainConnection.getService().getUserCaAliases()) 6476 .thenReturn(asSlice(new String[] {alias})); 6477 }); 6478 6479 getServices().injectBroadcast(mServiceContext, 6480 new Intent(KeyChain.ACTION_TRUST_STORE_CHANGED) 6481 .putExtra(Intent.EXTRA_USER_HANDLE, callerUser.getIdentifier()), 6482 callerUser.getIdentifier()); 6483 flushTasks(dpms); 6484 6485 final List<String> ownerInstalledCaCerts = new ArrayList<>(); 6486 6487 // Device Owner / Profile Owner can find out which CA certs were installed by itself. 6488 runAsCaller(admin1Context, dpms, (dpm) -> { 6489 final List<String> installedCaCerts = dpm.getOwnerInstalledCaCerts(callerUser); 6490 assertThat(installedCaCerts).isEqualTo(Collections.singletonList(alias)); 6491 ownerInstalledCaCerts.addAll(installedCaCerts); 6492 }); 6493 6494 // Restarting the DPMS should not lose information. 6495 initializeDpms(); 6496 runAsCaller(admin1Context, dpms, 6497 (dpm) -> assertThat(dpm.getOwnerInstalledCaCerts(callerUser)) 6498 .isEqualTo(ownerInstalledCaCerts)); 6499 6500 // System can find out which CA certs were installed by the Device Owner / Profile Owner. 6501 runAsCaller(serviceContext, dpms, (dpm) -> { 6502 assertThat(dpm.getOwnerInstalledCaCerts(callerUser)).isEqualTo(ownerInstalledCaCerts); 6503 6504 // Remove the CA cert. 6505 reset(getServices().keyChainConnection.getService()); 6506 }); 6507 6508 getServices().injectBroadcast(mServiceContext, 6509 new Intent(KeyChain.ACTION_TRUST_STORE_CHANGED) 6510 .putExtra(Intent.EXTRA_USER_HANDLE, callerUser.getIdentifier()), 6511 callerUser.getIdentifier()); 6512 flushTasks(dpms); 6513 6514 // Verify that the CA cert is no longer reported as installed by the Device Owner / Profile 6515 // Owner. 6516 runAsCaller(admin1Context, dpms, (dpm) -> { 6517 MoreAsserts.assertEmpty(dpm.getOwnerInstalledCaCerts(callerUser)); 6518 }); 6519 } 6520 verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval( final ComponentName callerName, final DpmMockContext callerContext)6521 private void verifyCantGetOwnerInstalledCaCertsProfileOwnerRemoval( 6522 final ComponentName callerName, final DpmMockContext callerContext) throws Exception { 6523 final String alias = "cert"; 6524 final byte[] caCert = TEST_CA.getBytes(); 6525 6526 // device admin (used for posting the tls notification) 6527 DpmMockContext admin1Context = mAdmin1Context; 6528 if (admin1.getPackageName().equals(callerContext.getPackageName())) { 6529 admin1Context = callerContext; 6530 } 6531 when(admin1Context.resources.getColor(anyInt(), anyObject())).thenReturn(Color.WHITE); 6532 6533 // caller: device admin or delegated certificate installer 6534 callerContext.applicationInfo = new ApplicationInfo(); 6535 final UserHandle callerUser = callerContext.binder.getCallingUserHandle(); 6536 6537 // system_server 6538 final DpmMockContext serviceContext = mContext; 6539 serviceContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 6540 getServices().addPackageContext(callerUser, admin1Context); 6541 getServices().addPackageContext(callerUser, callerContext); 6542 6543 // Install a CA cert as caller 6544 runAsCaller(callerContext, dpms, (dpm) -> { 6545 when(getServices().keyChainConnection.getService().installCaCertificate(caCert)) 6546 .thenReturn(alias); 6547 assertThat(dpm.installCaCert(callerName, caCert)).isTrue(); 6548 }); 6549 6550 // Fake the CA cert as having been installed 6551 when(getServices().keyChainConnection.getService().getUserCaAliases()) 6552 .thenReturn(asSlice(new String[] {alias})); 6553 getServices().injectBroadcast(mServiceContext, 6554 new Intent(KeyChain.ACTION_TRUST_STORE_CHANGED) 6555 .putExtra(Intent.EXTRA_USER_HANDLE, callerUser.getIdentifier()), 6556 callerUser.getIdentifier()); 6557 flushTasks(dpms); 6558 6559 // Removing the Profile Owner should clear the information on which CA certs were installed 6560 runAsCaller(admin1Context, dpms, dpm -> dpm.clearProfileOwner(admin1)); 6561 6562 runAsCaller(serviceContext, dpms, (dpm) -> { 6563 final List<String> ownerInstalledCaCerts = dpm.getOwnerInstalledCaCerts(callerUser); 6564 assertThat(ownerInstalledCaCerts).isNotNull(); 6565 assertThat(ownerInstalledCaCerts.isEmpty()).isTrue(); 6566 }); 6567 } 6568 verifyRebootWipeUserData(boolean wipeEuicc)6569 private void verifyRebootWipeUserData(boolean wipeEuicc) throws Exception { 6570 verify(getServices().recoverySystem).rebootWipeUserData(/*shutdown=*/ eq(false), 6571 /* reason= */ anyString(), /*force=*/ eq(true), eq(wipeEuicc), 6572 /* wipeAdoptableStorage= */ eq(false), /* wipeFactoryResetProtection= */ eq(false)); 6573 } 6574 assertAttestationFlags(int attestationFlags, int[] expectedFlags)6575 private void assertAttestationFlags(int attestationFlags, int[] expectedFlags) { 6576 int[] gotFlags = DevicePolicyManagerService.translateIdAttestationFlags(attestationFlags); 6577 Arrays.sort(gotFlags); 6578 Arrays.sort(expectedFlags); 6579 assertThat(Arrays.equals(expectedFlags, gotFlags)).isTrue(); 6580 } 6581 6582 @Test testTranslationOfIdAttestationFlag()6583 public void testTranslationOfIdAttestationFlag() { 6584 int[] allIdTypes = new int[]{ID_TYPE_SERIAL, ID_TYPE_IMEI, ID_TYPE_MEID}; 6585 int[] correspondingAttUtilsTypes = new int[]{ 6586 AttestationUtils.ID_TYPE_SERIAL, AttestationUtils.ID_TYPE_IMEI, 6587 AttestationUtils.ID_TYPE_MEID}; 6588 6589 // Test translation of zero flags 6590 assertThat(DevicePolicyManagerService.translateIdAttestationFlags(0)).isNull(); 6591 6592 // Test translation of the ID_TYPE_BASE_INFO flag, which should yield an empty, but 6593 // non-null array 6594 assertAttestationFlags(ID_TYPE_BASE_INFO, new int[] {}); 6595 6596 // Test translation of a single flag 6597 assertAttestationFlags(ID_TYPE_BASE_INFO | ID_TYPE_SERIAL, 6598 new int[] {AttestationUtils.ID_TYPE_SERIAL}); 6599 assertAttestationFlags(ID_TYPE_SERIAL, new int[] {AttestationUtils.ID_TYPE_SERIAL}); 6600 6601 // Test translation of two flags 6602 assertAttestationFlags(ID_TYPE_SERIAL | ID_TYPE_IMEI, 6603 new int[] {AttestationUtils.ID_TYPE_IMEI, AttestationUtils.ID_TYPE_SERIAL}); 6604 assertAttestationFlags(ID_TYPE_BASE_INFO | ID_TYPE_MEID | ID_TYPE_SERIAL, 6605 new int[] {AttestationUtils.ID_TYPE_MEID, AttestationUtils.ID_TYPE_SERIAL}); 6606 6607 // Test translation of all three flags 6608 assertAttestationFlags(ID_TYPE_SERIAL | ID_TYPE_IMEI | ID_TYPE_MEID, 6609 new int[] {AttestationUtils.ID_TYPE_IMEI, AttestationUtils.ID_TYPE_SERIAL, 6610 AttestationUtils.ID_TYPE_MEID}); 6611 // Test translation of all three flags 6612 assertAttestationFlags(ID_TYPE_SERIAL | ID_TYPE_IMEI | ID_TYPE_MEID | ID_TYPE_BASE_INFO, 6613 new int[] {AttestationUtils.ID_TYPE_IMEI, AttestationUtils.ID_TYPE_SERIAL, 6614 AttestationUtils.ID_TYPE_MEID}); 6615 } 6616 6617 @Test testRevertDeviceOwnership_noMetadataFile()6618 public void testRevertDeviceOwnership_noMetadataFile() throws Exception { 6619 setDeviceOwner(); 6620 initializeDpms(); 6621 assertThat(getMockTransferMetadataManager().metadataFileExists()).isFalse(); 6622 assertThat(dpms.isDeviceOwner(admin1, UserHandle.USER_SYSTEM)).isTrue(); 6623 assertThat(dpms.isAdminActive(admin1, UserHandle.USER_SYSTEM)).isTrue(); 6624 } 6625 6626 @FlakyTest(bugId = 148934649) 6627 @Test testRevertDeviceOwnership_adminAndDeviceMigrated()6628 public void testRevertDeviceOwnership_adminAndDeviceMigrated() throws Exception { 6629 DpmTestUtils.writeInputStreamToFile( 6630 getRawStream(com.android.frameworks.servicestests.R.raw.active_admin_migrated), 6631 getDeviceOwnerPoliciesFile()); 6632 DpmTestUtils.writeInputStreamToFile( 6633 getRawStream(com.android.frameworks.servicestests.R.raw.device_owner_migrated), 6634 getDeviceOwnerFile()); 6635 assertDeviceOwnershipRevertedWithFakeTransferMetadata(); 6636 } 6637 6638 @FlakyTest(bugId = 148934649) 6639 @Test testRevertDeviceOwnership_deviceNotMigrated()6640 public void testRevertDeviceOwnership_deviceNotMigrated() throws Exception { 6641 DpmTestUtils.writeInputStreamToFile( 6642 getRawStream(com.android.frameworks.servicestests.R.raw.active_admin_migrated), 6643 getDeviceOwnerPoliciesFile()); 6644 DpmTestUtils.writeInputStreamToFile( 6645 getRawStream(com.android.frameworks.servicestests.R.raw.device_owner_not_migrated), 6646 getDeviceOwnerFile()); 6647 assertDeviceOwnershipRevertedWithFakeTransferMetadata(); 6648 } 6649 6650 @Test testRevertDeviceOwnership_adminAndDeviceNotMigrated()6651 public void testRevertDeviceOwnership_adminAndDeviceNotMigrated() throws Exception { 6652 DpmTestUtils.writeInputStreamToFile( 6653 getRawStream(com.android.frameworks.servicestests.R.raw.active_admin_not_migrated), 6654 getDeviceOwnerPoliciesFile()); 6655 DpmTestUtils.writeInputStreamToFile( 6656 getRawStream(com.android.frameworks.servicestests.R.raw.device_owner_not_migrated), 6657 getDeviceOwnerFile()); 6658 assertDeviceOwnershipRevertedWithFakeTransferMetadata(); 6659 } 6660 6661 @Test testRevertProfileOwnership_noMetadataFile()6662 public void testRevertProfileOwnership_noMetadataFile() throws Exception { 6663 setupProfileOwner(); 6664 initializeDpms(); 6665 assertThat(getMockTransferMetadataManager().metadataFileExists()).isFalse(); 6666 assertThat(dpms.isProfileOwner(admin1, CALLER_USER_HANDLE)).isTrue(); 6667 assertThat(dpms.isAdminActive(admin1, CALLER_USER_HANDLE)).isTrue(); 6668 } 6669 6670 @FlakyTest(bugId = 148934649) 6671 @Test testRevertProfileOwnership_adminAndProfileMigrated()6672 public void testRevertProfileOwnership_adminAndProfileMigrated() throws Exception { 6673 getServices().addUser(DpmMockContext.CALLER_USER_HANDLE, 0, 6674 UserManager.USER_TYPE_PROFILE_MANAGED, UserHandle.USER_SYSTEM); 6675 DpmTestUtils.writeInputStreamToFile( 6676 getRawStream(com.android.frameworks.servicestests.R.raw.active_admin_migrated), 6677 getProfileOwnerPoliciesFile()); 6678 DpmTestUtils.writeInputStreamToFile( 6679 getRawStream(com.android.frameworks.servicestests.R.raw.profile_owner_migrated), 6680 getProfileOwnerFile()); 6681 assertProfileOwnershipRevertedWithFakeTransferMetadata(); 6682 } 6683 6684 @FlakyTest(bugId = 148934649) 6685 @Test testRevertProfileOwnership_profileNotMigrated()6686 public void testRevertProfileOwnership_profileNotMigrated() throws Exception { 6687 getServices().addUser(DpmMockContext.CALLER_USER_HANDLE, 0, 6688 UserManager.USER_TYPE_PROFILE_MANAGED, UserHandle.USER_SYSTEM); 6689 DpmTestUtils.writeInputStreamToFile( 6690 getRawStream(com.android.frameworks.servicestests.R.raw.active_admin_migrated), 6691 getProfileOwnerPoliciesFile()); 6692 DpmTestUtils.writeInputStreamToFile( 6693 getRawStream(com.android.frameworks.servicestests.R.raw.profile_owner_not_migrated), 6694 getProfileOwnerFile()); 6695 assertProfileOwnershipRevertedWithFakeTransferMetadata(); 6696 } 6697 6698 @Test testRevertProfileOwnership_adminAndProfileNotMigrated()6699 public void testRevertProfileOwnership_adminAndProfileNotMigrated() throws Exception { 6700 getServices().addUser(CALLER_USER_HANDLE, 0, 6701 UserManager.USER_TYPE_PROFILE_MANAGED, UserHandle.USER_SYSTEM); 6702 DpmTestUtils.writeInputStreamToFile( 6703 getRawStream(com.android.frameworks.servicestests.R.raw.active_admin_not_migrated), 6704 getProfileOwnerPoliciesFile()); 6705 DpmTestUtils.writeInputStreamToFile( 6706 getRawStream(com.android.frameworks.servicestests.R.raw.profile_owner_not_migrated), 6707 getProfileOwnerFile()); 6708 assertProfileOwnershipRevertedWithFakeTransferMetadata(); 6709 } 6710 6711 @Test testGrantDeviceIdsAccess_notToProfileOwner()6712 public void testGrantDeviceIdsAccess_notToProfileOwner() throws Exception { 6713 setupProfileOwner(); 6714 configureContextForAccess(mContext, false); 6715 6716 assertExpectException(SecurityException.class, /* messageRegex= */ null, 6717 () -> dpm.setProfileOwnerOnOrganizationOwnedDevice(admin2, true)); 6718 } 6719 6720 @Test testGrantDeviceIdsAccess_notByAuthorizedCaller()6721 public void testGrantDeviceIdsAccess_notByAuthorizedCaller() throws Exception { 6722 setupProfileOwner(); 6723 configureContextForAccess(mContext, false); 6724 6725 assertExpectException(SecurityException.class, /* messageRegex= */ null, 6726 () -> dpm.setProfileOwnerOnOrganizationOwnedDevice(admin1, true)); 6727 } 6728 6729 @Test testGrantDeviceIdsAccess_byAuthorizedSystemCaller()6730 public void testGrantDeviceIdsAccess_byAuthorizedSystemCaller() throws Exception { 6731 setupProfileOwner(); 6732 6733 // This method will throw if the system context could not call 6734 // markProfileOwnerOfOrganizationOwnedDevice successfully. 6735 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 6736 } 6737 configureContextForAccess(DpmMockContext context, boolean granted)6738 private void configureContextForAccess(DpmMockContext context, boolean granted) { 6739 when(context.spiedContext.checkCallingPermission( 6740 permission.MARK_DEVICE_ORGANIZATION_OWNED)) 6741 .thenReturn(granted ? PackageManager.PERMISSION_GRANTED 6742 : PackageManager.PERMISSION_DENIED); 6743 6744 when(getServices().userManager.getProfileParent(any())) 6745 .thenReturn(UserHandle.SYSTEM); 6746 } 6747 6748 @Test testGrantDeviceIdsAccess_byAuthorizedManagedProvisioning()6749 public void testGrantDeviceIdsAccess_byAuthorizedManagedProvisioning() throws Exception { 6750 setupProfileOwner(); 6751 6752 final long ident = mServiceContext.binder.clearCallingIdentity(); 6753 configureContextForAccess(mServiceContext, true); 6754 mServiceContext.permissions.add(permission.MARK_DEVICE_ORGANIZATION_OWNED); 6755 6756 mServiceContext.binder.callingUid = 6757 UserHandle.getUid(CALLER_USER_HANDLE, 6758 DpmMockContext.CALLER_MANAGED_PROVISIONING_UID); 6759 try { 6760 runAsCaller(mServiceContext, dpms, dpm -> { 6761 dpm.setProfileOwnerOnOrganizationOwnedDevice(admin1, true); 6762 }); 6763 } finally { 6764 mServiceContext.binder.restoreCallingIdentity(ident); 6765 } 6766 } 6767 6768 @Test testHasDeviceIdAccessUnchecked_deviceOwnerCaller()6769 public void testHasDeviceIdAccessUnchecked_deviceOwnerCaller() 6770 throws Exception { 6771 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 6772 setupDeviceOwner(); 6773 configureContextForAccess(mContext, false); 6774 6775 // Device owner should be allowed to request Device ID attestation. 6776 assertThat(dpms.hasDeviceIdAccessUnchecked( 6777 admin1.getPackageName(), 6778 DpmMockContext.CALLER_SYSTEM_USER_UID)).isTrue(); 6779 6780 // Another package must not be allowed to request Device ID attestation. 6781 assertThat(dpms.hasDeviceIdAccessUnchecked( 6782 DpmMockContext.ANOTHER_PACKAGE_NAME, 6783 DpmMockContext.CALLER_SYSTEM_USER_UID)).isFalse(); 6784 } 6785 6786 @Test testHasDeviceIdAccessUnchecked_profileOwnerCaller()6787 public void testHasDeviceIdAccessUnchecked_profileOwnerCaller() 6788 throws Exception { 6789 configureContextForAccess(mContext, false); 6790 6791 // Make sure a security exception is thrown if the device has no profile owner. 6792 assertThat(dpms.hasDeviceIdAccessUnchecked( 6793 admin1.getPackageName(), 6794 DpmMockContext.CALLER_UID)).isFalse(); 6795 6796 setupProfileOwner(); 6797 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 6798 6799 // The profile owner is allowed to request Device ID attestation. 6800 assertThat(dpms.hasDeviceIdAccessUnchecked( 6801 admin1.getPackageName(), 6802 DpmMockContext.CALLER_UID)).isTrue(); 6803 6804 // But not another package. 6805 assertThat(dpms.hasDeviceIdAccessUnchecked( 6806 DpmMockContext.ANOTHER_PACKAGE_NAME, 6807 DpmMockContext.CALLER_UID)).isFalse(); 6808 6809 // Or another component which is not the admin. 6810 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 6811 assertThat(dpms.hasDeviceIdAccessUnchecked( 6812 admin1.getPackageName(), 6813 DpmMockContext.ANOTHER_UID)).isFalse(); 6814 } 6815 runAsDelegatedCertInstaller(DpmRunnable action)6816 public void runAsDelegatedCertInstaller(DpmRunnable action) throws Exception { 6817 final long ident = mServiceContext.binder.clearCallingIdentity(); 6818 6819 mServiceContext.binder.callingUid = UserHandle.getUid(CALLER_USER_HANDLE, 6820 DpmMockContext.DELEGATE_CERT_INSTALLER_UID); 6821 try { 6822 runAsCaller(mServiceContext, dpms, action); 6823 } finally { 6824 mServiceContext.binder.restoreCallingIdentity(ident); 6825 } 6826 } 6827 6828 @Test testHasDeviceIdAccessUnchecked_delegateCaller()6829 public void testHasDeviceIdAccessUnchecked_delegateCaller() throws Exception { 6830 setupProfileOwner(); 6831 markDelegatedCertInstallerAsInstalled(); 6832 6833 // Configure a delegated cert installer. 6834 runAsCaller(mServiceContext, dpms, 6835 dpm -> dpm.setDelegatedScopes(admin1, DpmMockContext.DELEGATE_PACKAGE_NAME, 6836 Arrays.asList(DELEGATION_CERT_INSTALL))); 6837 6838 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 6839 6840 // Make sure that the profile owner can still request Device ID attestation. 6841 assertThat(dpms.hasDeviceIdAccessUnchecked( 6842 admin1.getPackageName(), 6843 DpmMockContext.CALLER_UID)).isTrue(); 6844 6845 runAsDelegatedCertInstaller(dpm -> assertThat( 6846 dpms.hasDeviceIdAccessUnchecked( 6847 DpmMockContext.DELEGATE_PACKAGE_NAME, 6848 UserHandle.getUid(CALLER_USER_HANDLE, 6849 DpmMockContext.DELEGATE_CERT_INSTALLER_UID))).isTrue()); 6850 } 6851 6852 @Test testHasDeviceIdAccessUnchecked_delegateCallerWithoutPermissions()6853 public void testHasDeviceIdAccessUnchecked_delegateCallerWithoutPermissions() 6854 throws Exception { 6855 setupProfileOwner(); 6856 markDelegatedCertInstallerAsInstalled(); 6857 6858 // Configure a delegated cert installer. 6859 runAsCaller(mServiceContext, dpms, 6860 dpm -> dpm.setDelegatedScopes(admin1, DpmMockContext.DELEGATE_PACKAGE_NAME, 6861 Arrays.asList(DELEGATION_CERT_INSTALL))); 6862 6863 assertThat(dpms.hasDeviceIdAccessUnchecked( 6864 admin1.getPackageName(), 6865 DpmMockContext.CALLER_UID)).isFalse(); 6866 6867 runAsDelegatedCertInstaller(dpm -> { 6868 assertThat(dpms.hasDeviceIdAccessUnchecked( 6869 DpmMockContext.DELEGATE_PACKAGE_NAME, 6870 DpmMockContext.CALLER_UID)).isFalse(); 6871 }); 6872 } 6873 6874 @Test testGetPasswordComplexity_securityExceptionNotThrownForParentInstance()6875 public void testGetPasswordComplexity_securityExceptionNotThrownForParentInstance() { 6876 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6877 when(getServices().packageManager.getPackagesForUid(DpmMockContext.CALLER_UID)).thenReturn( 6878 new String[0]); 6879 mServiceContext.permissions.add(permission.REQUEST_PASSWORD_COMPLEXITY); 6880 setAsProfileOwner(admin1); 6881 6882 parentDpm.getPasswordComplexity(); 6883 6884 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_NONE); 6885 } 6886 6887 @Test testGetPasswordComplexity_illegalStateExceptionIfLocked()6888 public void testGetPasswordComplexity_illegalStateExceptionIfLocked() { 6889 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6890 when(getServices().packageManager.getPackagesForUid(DpmMockContext.CALLER_UID)).thenReturn( 6891 new String[0]); 6892 when(getServices().userManager.isUserUnlocked(CALLER_USER_HANDLE)).thenReturn(false); 6893 assertThrows(IllegalStateException.class, () -> dpm.getPasswordComplexity()); 6894 } 6895 6896 @Test testGetPasswordComplexity_securityExceptionWithoutPermissions()6897 public void testGetPasswordComplexity_securityExceptionWithoutPermissions() { 6898 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6899 when(getServices().packageManager.getPackagesForUid(DpmMockContext.CALLER_UID)).thenReturn( 6900 new String[0]); 6901 when(getServices().userManager.isUserUnlocked(CALLER_USER_HANDLE)).thenReturn(true); 6902 assertThrows(SecurityException.class, () -> dpm.getPasswordComplexity()); 6903 } 6904 6905 6906 @Test testGetPasswordComplexity_currentUserNoPassword()6907 public void testGetPasswordComplexity_currentUserNoPassword() { 6908 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6909 when(getServices().packageManager.getPackagesForUid(DpmMockContext.CALLER_UID)).thenReturn( 6910 new String[0]); 6911 when(getServices().userManager.isUserUnlocked(CALLER_USER_HANDLE)).thenReturn(true); 6912 mServiceContext.permissions.add(permission.REQUEST_PASSWORD_COMPLEXITY); 6913 when(getServices().userManager.getCredentialOwnerProfile(CALLER_USER_HANDLE)) 6914 .thenReturn(CALLER_USER_HANDLE); 6915 6916 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_NONE); 6917 } 6918 6919 @Test testGetPasswordComplexity_currentUserHasPassword()6920 public void testGetPasswordComplexity_currentUserHasPassword() { 6921 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6922 when(getServices().packageManager.getPackagesForUid(DpmMockContext.CALLER_UID)).thenReturn( 6923 new String[0]); 6924 when(getServices().userManager.isUserUnlocked(CALLER_USER_HANDLE)).thenReturn(true); 6925 mServiceContext.permissions.add(permission.REQUEST_PASSWORD_COMPLEXITY); 6926 when(getServices().userManager.getCredentialOwnerProfile(CALLER_USER_HANDLE)) 6927 .thenReturn(CALLER_USER_HANDLE); 6928 when(getServices().lockSettingsInternal 6929 .getUserPasswordMetrics(CALLER_USER_HANDLE)) 6930 .thenReturn(metricsForPassword("asdf")); 6931 6932 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_MEDIUM); 6933 } 6934 6935 @Test testGetPasswordComplexity_unifiedChallengeReturnsParentUserPassword()6936 public void testGetPasswordComplexity_unifiedChallengeReturnsParentUserPassword() { 6937 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 6938 when(getServices().packageManager.getPackagesForUid(DpmMockContext.CALLER_UID)).thenReturn( 6939 new String[0]); 6940 when(getServices().userManager.isUserUnlocked(CALLER_USER_HANDLE)).thenReturn(true); 6941 mServiceContext.permissions.add(permission.REQUEST_PASSWORD_COMPLEXITY); 6942 6943 UserInfo parentUser = new UserInfo(); 6944 parentUser.id = CALLER_USER_HANDLE + 10; 6945 when(getServices().userManager.getCredentialOwnerProfile(CALLER_USER_HANDLE)) 6946 .thenReturn(parentUser.id); 6947 6948 when(getServices().lockSettingsInternal 6949 .getUserPasswordMetrics(CALLER_USER_HANDLE)) 6950 .thenReturn(metricsForPassword("asdf")); 6951 when(getServices().lockSettingsInternal 6952 .getUserPasswordMetrics(parentUser.id)) 6953 .thenReturn(metricsForPassword("parentUser")); 6954 6955 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_HIGH); 6956 } 6957 6958 @Test testCrossProfileCalendarPackages_initiallyEmpty()6959 public void testCrossProfileCalendarPackages_initiallyEmpty() { 6960 setAsProfileOwner(admin1); 6961 final Set<String> packages = dpm.getCrossProfileCalendarPackages(admin1); 6962 assertCrossProfileCalendarPackagesEqual(packages, Collections.emptySet()); 6963 } 6964 6965 @Test testCrossProfileCalendarPackages_reopenDpms()6966 public void testCrossProfileCalendarPackages_reopenDpms() { 6967 setAsProfileOwner(admin1); 6968 dpm.setCrossProfileCalendarPackages(admin1, null); 6969 Set<String> packages = dpm.getCrossProfileCalendarPackages(admin1); 6970 assertThat(packages == null).isTrue(); 6971 initializeDpms(); 6972 packages = dpm.getCrossProfileCalendarPackages(admin1); 6973 assertThat(packages == null).isTrue(); 6974 6975 dpm.setCrossProfileCalendarPackages(admin1, Collections.emptySet()); 6976 packages = dpm.getCrossProfileCalendarPackages(admin1); 6977 assertCrossProfileCalendarPackagesEqual(packages, Collections.emptySet()); 6978 initializeDpms(); 6979 packages = dpm.getCrossProfileCalendarPackages(admin1); 6980 assertCrossProfileCalendarPackagesEqual(packages, Collections.emptySet()); 6981 6982 final String dummyPackageName = "test"; 6983 final Set<String> testPackages = new ArraySet<String>(Arrays.asList(dummyPackageName)); 6984 dpm.setCrossProfileCalendarPackages(admin1, testPackages); 6985 packages = dpm.getCrossProfileCalendarPackages(admin1); 6986 assertCrossProfileCalendarPackagesEqual(packages, testPackages); 6987 initializeDpms(); 6988 packages = dpm.getCrossProfileCalendarPackages(admin1); 6989 assertCrossProfileCalendarPackagesEqual(packages, testPackages); 6990 } 6991 assertCrossProfileCalendarPackagesEqual(Set<String> expected, Set<String> actual)6992 private void assertCrossProfileCalendarPackagesEqual(Set<String> expected, Set<String> actual) { 6993 assertThat(expected).isNotNull(); 6994 assertThat(actual).isNotNull(); 6995 assertThat(actual).containsExactlyElementsIn(expected); 6996 } 6997 6998 @Test testIsPackageAllowedToAccessCalendar_adminNotAllowed()6999 public void testIsPackageAllowedToAccessCalendar_adminNotAllowed() { 7000 final String testPackage = "TEST_PACKAGE"; 7001 setAsProfileOwner(admin1); 7002 dpm.setCrossProfileCalendarPackages(admin1, Collections.emptySet()); 7003 when(getServices().settings.settingsSecureGetIntForUser( 7004 Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED, 7005 0, CALLER_USER_HANDLE)).thenReturn(1); 7006 mContext.permissions.add(permission.INTERACT_ACROSS_USERS); 7007 7008 assertThat(dpm.isPackageAllowedToAccessCalendar(testPackage)).isFalse(); 7009 } 7010 7011 @Test testIsPackageAllowedToAccessCalendar_settingOff()7012 public void testIsPackageAllowedToAccessCalendar_settingOff() { 7013 final String testPackage = "TEST_PACKAGE"; 7014 setAsProfileOwner(admin1); 7015 dpm.setCrossProfileCalendarPackages(admin1, Collections.singleton(testPackage)); 7016 when(getServices().settings.settingsSecureGetIntForUser( 7017 Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED, 7018 0, CALLER_USER_HANDLE)).thenReturn(0); 7019 mContext.permissions.add(permission.INTERACT_ACROSS_USERS); 7020 7021 assertThat(dpm.isPackageAllowedToAccessCalendar(testPackage)).isFalse(); 7022 } 7023 7024 @Test testIsPackageAllowedToAccessCalendar_bothAllowed()7025 public void testIsPackageAllowedToAccessCalendar_bothAllowed() { 7026 final String testPackage = "TEST_PACKAGE"; 7027 getServices().addTestPackageUid(testPackage, DpmMockContext.ANOTHER_UID); 7028 setAsProfileOwner(admin1); 7029 dpm.setCrossProfileCalendarPackages(admin1, null); 7030 when(getServices().settings.settingsSecureGetIntForUser( 7031 Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED, 7032 0, CALLER_USER_HANDLE)).thenReturn(1); 7033 mContext.permissions.add(permission.INTERACT_ACROSS_USERS); 7034 7035 assertThat(dpm.isPackageAllowedToAccessCalendar(testPackage)).isTrue(); 7036 } 7037 7038 @Test testIsPackageAllowedToAccessCalendar_requiresPermission()7039 public void testIsPackageAllowedToAccessCalendar_requiresPermission() { 7040 final String testPackage = "TEST_PACKAGE"; 7041 getServices().addTestPackageUid(testPackage, DpmMockContext.ANOTHER_UID); 7042 7043 assertExpectException(SecurityException.class, /* messageRegex= */ null, 7044 () -> dpm.isPackageAllowedToAccessCalendar(testPackage)); 7045 } 7046 7047 @Test testIsPackageAllowedToAccessCalendar_samePackageAndSameUser_noPermissionRequired()7048 public void testIsPackageAllowedToAccessCalendar_samePackageAndSameUser_noPermissionRequired() { 7049 final String testPackage = "TEST_PACKAGE"; 7050 setAsProfileOwner(admin1); 7051 dpm.setCrossProfileCalendarPackages(admin1, null); 7052 when(getServices().settings.settingsSecureGetIntForUser( 7053 Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED, 7054 0, CALLER_USER_HANDLE)).thenReturn(1); 7055 7056 getServices().addTestPackageUid(testPackage, mContext.binder.callingUid); 7057 7058 assertThat(dpm.isPackageAllowedToAccessCalendar(testPackage)).isTrue(); 7059 } 7060 7061 @Test 7062 @Ignore("b/277916462") testSetUserControlDisabledPackages_asDO()7063 public void testSetUserControlDisabledPackages_asDO() throws Exception { 7064 final List<String> testPackages = new ArrayList<>(); 7065 testPackages.add("package_1"); 7066 testPackages.add("package_2"); 7067 mServiceContext.permissions.add(permission.MANAGE_DEVICE_ADMINS); 7068 setDeviceOwner(); 7069 7070 dpm.setUserControlDisabledPackages(admin1, testPackages); 7071 7072 verify(getServices().packageManagerInternal) 7073 .setOwnerProtectedPackages(UserHandle.USER_ALL, testPackages); 7074 assertThat(dpm.getUserControlDisabledPackages(admin1)).isEqualTo(testPackages); 7075 } 7076 7077 @Test 7078 @Ignore("b/277916462") testSetUserControlDisabledPackages_asPO()7079 public void testSetUserControlDisabledPackages_asPO() { 7080 final List<String> testPackages = new ArrayList<>(); 7081 testPackages.add("package_1"); 7082 testPackages.add("package_2"); 7083 mServiceContext.permissions.add(permission.MANAGE_DEVICE_ADMINS); 7084 setAsProfileOwner(admin1); 7085 7086 dpm.setUserControlDisabledPackages(admin1, testPackages); 7087 7088 verify(getServices().packageManagerInternal) 7089 .setOwnerProtectedPackages(CALLER_USER_HANDLE, testPackages); 7090 assertThat(dpm.getUserControlDisabledPackages(admin1)).isEqualTo(testPackages); 7091 } 7092 configureProfileOwnerOfOrgOwnedDevice(ComponentName who, int userId)7093 private void configureProfileOwnerOfOrgOwnedDevice(ComponentName who, int userId) { 7094 final long ident = mServiceContext.binder.clearCallingIdentity(); 7095 mServiceContext.binder.callingUid = UserHandle.getUid(userId, DpmMockContext.SYSTEM_UID); 7096 7097 configureContextForAccess(mServiceContext, true); 7098 runAsCaller(mServiceContext, dpms, dpm -> { 7099 dpm.setProfileOwnerOnOrganizationOwnedDevice(who, true); 7100 }); 7101 mServiceContext.binder.restoreCallingIdentity(ident); 7102 } 7103 7104 @Test testGetCrossProfilePackages_notSet_returnsEmpty()7105 public void testGetCrossProfilePackages_notSet_returnsEmpty() { 7106 setAsProfileOwner(admin1); 7107 assertThat(dpm.getCrossProfilePackages(admin1).isEmpty()).isTrue(); 7108 } 7109 7110 @Test testGetCrossProfilePackages_notSet_dpmsReinitialized_returnsEmpty()7111 public void testGetCrossProfilePackages_notSet_dpmsReinitialized_returnsEmpty() { 7112 setAsProfileOwner(admin1); 7113 7114 initializeDpms(); 7115 7116 assertThat(dpm.getCrossProfilePackages(admin1).isEmpty()).isTrue(); 7117 } 7118 7119 @Test testGetCrossProfilePackages_whenSet_returnsEqual()7120 public void testGetCrossProfilePackages_whenSet_returnsEqual() { 7121 setAsProfileOwner(admin1); 7122 Set<String> packages = Collections.singleton("TEST_PACKAGE"); 7123 7124 dpm.setCrossProfilePackages(admin1, packages); 7125 7126 assertThat(dpm.getCrossProfilePackages(admin1)).isEqualTo(packages); 7127 } 7128 7129 @Test testGetCrossProfilePackages_whenSet_dpmsReinitialized_returnsEqual()7130 public void testGetCrossProfilePackages_whenSet_dpmsReinitialized_returnsEqual() { 7131 setAsProfileOwner(admin1); 7132 Set<String> packages = Collections.singleton("TEST_PACKAGE"); 7133 7134 dpm.setCrossProfilePackages(admin1, packages); 7135 initializeDpms(); 7136 7137 assertThat(dpm.getCrossProfilePackages(admin1)).isEqualTo(packages); 7138 } 7139 7140 @Test testGetAllCrossProfilePackages_notSet_returnsEmpty()7141 public void testGetAllCrossProfilePackages_notSet_returnsEmpty() throws Exception { 7142 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 7143 addManagedProfile(admin1, mServiceContext.binder.callingUid, admin1); 7144 mContext.packageName = admin1.getPackageName(); 7145 7146 setCrossProfileAppsList(); 7147 setVendorCrossProfileAppsList(); 7148 7149 assertThat(dpm.getAllCrossProfilePackages().isEmpty()).isTrue(); 7150 } 7151 7152 @Test testGetAllCrossProfilePackages_notSet_dpmsReinitialized_returnsEmpty()7153 public void testGetAllCrossProfilePackages_notSet_dpmsReinitialized_returnsEmpty() 7154 throws Exception { 7155 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 7156 addManagedProfile(admin1, mServiceContext.binder.callingUid, admin1); 7157 mContext.packageName = admin1.getPackageName(); 7158 7159 setCrossProfileAppsList(); 7160 setVendorCrossProfileAppsList(); 7161 initializeDpms(); 7162 7163 assertThat(dpm.getAllCrossProfilePackages().isEmpty()).isTrue(); 7164 } 7165 7166 @Test testGetAllCrossProfilePackages_whenSet_returnsCombinedSet()7167 public void testGetAllCrossProfilePackages_whenSet_returnsCombinedSet() throws Exception { 7168 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 7169 addManagedProfile(admin1, mServiceContext.binder.callingUid, admin1); 7170 final Set<String> packages = Sets.newSet("TEST_PACKAGE", "TEST_COMMON_PACKAGE"); 7171 mContext.packageName = admin1.getPackageName(); 7172 7173 dpm.setCrossProfilePackages(admin1, packages); 7174 setCrossProfileAppsList("TEST_DEFAULT_PACKAGE", "TEST_COMMON_PACKAGE"); 7175 setVendorCrossProfileAppsList("TEST_VENDOR_DEFAULT_PACKAGE"); 7176 7177 assertThat(dpm.getAllCrossProfilePackages()).containsExactly( 7178 "TEST_PACKAGE", "TEST_DEFAULT_PACKAGE", "TEST_COMMON_PACKAGE", 7179 "TEST_VENDOR_DEFAULT_PACKAGE"); 7180 } 7181 7182 @Test testGetAllCrossProfilePackages_whenSet_dpmsReinitialized_returnsCombinedSet()7183 public void testGetAllCrossProfilePackages_whenSet_dpmsReinitialized_returnsCombinedSet() 7184 throws Exception { 7185 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 7186 addManagedProfile(admin1, mServiceContext.binder.callingUid, admin1); 7187 final Set<String> packages = Sets.newSet("TEST_PACKAGE", "TEST_COMMON_PACKAGE"); 7188 mContext.packageName = admin1.getPackageName(); 7189 7190 dpm.setCrossProfilePackages(admin1, packages); 7191 setCrossProfileAppsList("TEST_DEFAULT_PACKAGE", "TEST_COMMON_PACKAGE"); 7192 setVendorCrossProfileAppsList("TEST_VENDOR_DEFAULT_PACKAGE"); 7193 initializeDpms(); 7194 7195 assertThat(dpm.getAllCrossProfilePackages()).containsExactly( 7196 "TEST_PACKAGE", "TEST_DEFAULT_PACKAGE", "TEST_COMMON_PACKAGE", 7197 "TEST_VENDOR_DEFAULT_PACKAGE"); 7198 } 7199 7200 @Test testGetDefaultCrossProfilePackages_noPackagesSet_returnsEmpty()7201 public void testGetDefaultCrossProfilePackages_noPackagesSet_returnsEmpty() { 7202 setCrossProfileAppsList(); 7203 setVendorCrossProfileAppsList(); 7204 7205 assertThat(dpm.getDefaultCrossProfilePackages()).isEmpty(); 7206 } 7207 7208 @Test testGetDefaultCrossProfilePackages_packagesSet_returnsCombinedSet()7209 public void testGetDefaultCrossProfilePackages_packagesSet_returnsCombinedSet() { 7210 setCrossProfileAppsList("TEST_DEFAULT_PACKAGE", "TEST_COMMON_PACKAGE"); 7211 setVendorCrossProfileAppsList("TEST_VENDOR_DEFAULT_PACKAGE"); 7212 7213 assertThat(dpm.getDefaultCrossProfilePackages()).containsExactly( 7214 "TEST_DEFAULT_PACKAGE", "TEST_COMMON_PACKAGE", "TEST_VENDOR_DEFAULT_PACKAGE"); 7215 } 7216 7217 @Test testSetCommonCriteriaMode_asDeviceOwner()7218 public void testSetCommonCriteriaMode_asDeviceOwner() throws Exception { 7219 setDeviceOwner(); 7220 7221 assertThat(dpm.isCommonCriteriaModeEnabled(admin1)).isFalse(); 7222 assertThat(dpm.isCommonCriteriaModeEnabled(null)).isFalse(); 7223 7224 dpm.setCommonCriteriaModeEnabled(admin1, true); 7225 7226 assertThat(dpm.isCommonCriteriaModeEnabled(admin1)).isTrue(); 7227 assertThat(dpm.isCommonCriteriaModeEnabled(null)).isTrue(); 7228 } 7229 7230 @Test testSetCommonCriteriaMode_asPoOfOrgOwnedDevice()7231 public void testSetCommonCriteriaMode_asPoOfOrgOwnedDevice() throws Exception { 7232 final int managedProfileUserId = 15; 7233 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 7234 addManagedProfile(admin1, managedProfileAdminUid, admin1); 7235 configureProfileOwnerOfOrgOwnedDevice(admin1, managedProfileUserId); 7236 mContext.binder.callingUid = managedProfileAdminUid; 7237 7238 assertThat(dpm.isCommonCriteriaModeEnabled(admin1)).isFalse(); 7239 assertThat(dpm.isCommonCriteriaModeEnabled(null)).isFalse(); 7240 7241 dpm.setCommonCriteriaModeEnabled(admin1, true); 7242 7243 assertThat(dpm.isCommonCriteriaModeEnabled(admin1)).isTrue(); 7244 assertThat(dpm.isCommonCriteriaModeEnabled(null)).isTrue(); 7245 } 7246 7247 @Test testCanProfileOwnerResetPasswordWhenLocked_nonDirectBootAwarePo()7248 public void testCanProfileOwnerResetPasswordWhenLocked_nonDirectBootAwarePo() 7249 throws Exception { 7250 setDeviceEncryptionPerUser(); 7251 setupProfileOwner(); 7252 setupPasswordResetToken(); 7253 7254 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7255 assertWithMessage("po is not direct boot aware") 7256 .that(dpm.canProfileOwnerResetPasswordWhenLocked(CALLER_USER_HANDLE)).isFalse(); 7257 } 7258 7259 @Test testCanProfileOwnerResetPasswordWhenLocked_noActiveToken()7260 public void testCanProfileOwnerResetPasswordWhenLocked_noActiveToken() throws Exception { 7261 setDeviceEncryptionPerUser(); 7262 setupProfileOwner(); 7263 makeAdmin1DirectBootAware(); 7264 7265 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7266 assertWithMessage("po doesn't have an active password reset token") 7267 .that(dpm.canProfileOwnerResetPasswordWhenLocked(CALLER_USER_HANDLE)).isFalse(); 7268 } 7269 7270 @Test testCanProfileOwnerResetPasswordWhenLocked_nonFbeDevice()7271 public void testCanProfileOwnerResetPasswordWhenLocked_nonFbeDevice() throws Exception { 7272 setupProfileOwner(); 7273 makeAdmin1DirectBootAware(); 7274 setupPasswordResetToken(); 7275 7276 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7277 assertWithMessage("device is not FBE") 7278 .that(dpm.canProfileOwnerResetPasswordWhenLocked(CALLER_USER_HANDLE)).isFalse(); 7279 } 7280 7281 @Test 7282 @Ignore("b/277916462") testCanProfileOwnerResetPasswordWhenLocked()7283 public void testCanProfileOwnerResetPasswordWhenLocked() throws Exception { 7284 setDeviceEncryptionPerUser(); 7285 setupProfileOwner(); 7286 makeAdmin1DirectBootAware(); 7287 setupPasswordResetToken(); 7288 7289 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7290 assertWithMessage("direct boot aware po with active password reset token") 7291 .that(dpm.canProfileOwnerResetPasswordWhenLocked(CALLER_USER_HANDLE)).isTrue(); 7292 } 7293 setupPasswordResetToken()7294 private void setupPasswordResetToken() { 7295 final byte[] token = new byte[32]; 7296 final long handle = 123456; 7297 7298 when(getServices().lockPatternUtils 7299 .addEscrowToken(eq(token), eq(CALLER_USER_HANDLE), 7300 nullable(EscrowTokenStateChangeCallback.class))) 7301 .thenReturn(handle); 7302 7303 dpm.setResetPasswordToken(admin1, token); 7304 7305 when(getServices().lockPatternUtils 7306 .isEscrowTokenActive(eq(handle), eq(CALLER_USER_HANDLE))) 7307 .thenReturn(true); 7308 7309 assertWithMessage("failed to activate token").that(dpm.isResetPasswordTokenActive(admin1)) 7310 .isTrue(); 7311 } 7312 makeAdmin1DirectBootAware()7313 private void makeAdmin1DirectBootAware() 7314 throws PackageManager.NameNotFoundException, android.os.RemoteException { 7315 Mockito.reset(getServices().ipackageManager); 7316 7317 final ApplicationInfo ai = DpmTestUtils.cloneParcelable( 7318 mRealTestContext.getPackageManager().getApplicationInfo( 7319 admin1.getPackageName(), 7320 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS)); 7321 ai.privateFlags = PRIVATE_FLAG_DIRECT_BOOT_AWARE; 7322 7323 doReturn(ai).when(getServices().ipackageManager).getApplicationInfo( 7324 eq(admin1.getPackageName()), 7325 anyLong(), 7326 eq(CALLER_USER_HANDLE)); 7327 } 7328 setDeviceEncryptionPerUser()7329 private void setDeviceEncryptionPerUser() { 7330 when(getServices().storageManager.isFileBasedEncryptionEnabled()).thenReturn(true); 7331 } 7332 setCrossProfileAppsList(String... packages)7333 private void setCrossProfileAppsList(String... packages) { 7334 when(mContext.getResources() 7335 .getStringArray(eq(R.array.cross_profile_apps))) 7336 .thenReturn(packages); 7337 } 7338 setVendorCrossProfileAppsList(String... packages)7339 private void setVendorCrossProfileAppsList(String... packages) { 7340 when(mContext.getResources() 7341 .getStringArray(eq(R.array.vendor_cross_profile_apps))) 7342 .thenReturn(packages); 7343 } 7344 7345 @Test 7346 @Ignore("b/277916462") testSetAccountTypesWithManagementDisabledOnManagedProfile()7347 public void testSetAccountTypesWithManagementDisabledOnManagedProfile() throws Exception { 7348 setupProfileOwner(); 7349 7350 final String accountType = "com.example.account.type"; 7351 int originalUid = mContext.binder.callingUid; 7352 dpm.setAccountManagementDisabled(admin1, accountType, true); 7353 assertThat(dpm.getAccountTypesWithManagementDisabled()).asList().containsExactly( 7354 accountType); 7355 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 7356 assertThat(dpm.getAccountTypesWithManagementDisabled()).isEmpty(); 7357 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7358 assertThat(dpm.getAccountTypesWithManagementDisabled()).isEmpty(); 7359 7360 mContext.binder.callingUid = originalUid; 7361 dpm.setAccountManagementDisabled(admin1, accountType, false); 7362 assertThat(dpm.getAccountTypesWithManagementDisabled()).isEmpty(); 7363 } 7364 7365 @Test 7366 @Ignore("b/277916462") testSetAccountTypesWithManagementDisabledOnOrgOwnedManagedProfile()7367 public void testSetAccountTypesWithManagementDisabledOnOrgOwnedManagedProfile() 7368 throws Exception { 7369 mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS); 7370 7371 final int managedProfileUserId = 15; 7372 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 7373 7374 addManagedProfile(admin1, managedProfileAdminUid, admin1); 7375 mContext.binder.callingUid = managedProfileAdminUid; 7376 7377 configureProfileOwnerOfOrgOwnedDevice(admin1, managedProfileUserId); 7378 7379 int originalUid = mContext.binder.callingUid; 7380 final String accountType = "com.example.account.type"; 7381 dpm.getParentProfileInstance(admin1).setAccountManagementDisabled(admin1, accountType, 7382 true); 7383 assertThat(dpm.getAccountTypesWithManagementDisabled()).isEmpty(); 7384 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 7385 assertThat(dpm.getAccountTypesWithManagementDisabled()).asList().containsExactly( 7386 accountType); 7387 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7388 assertThat(dpm.getAccountTypesWithManagementDisabled()).asList().containsExactly( 7389 accountType); 7390 7391 mContext.binder.callingUid = originalUid; 7392 dpm.getParentProfileInstance(admin1).setAccountManagementDisabled(admin1, accountType, 7393 false); 7394 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 7395 assertThat(dpm.getAccountTypesWithManagementDisabled()).isEmpty(); 7396 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7397 assertThat(dpm.getAccountTypesWithManagementDisabled()).isEmpty(); 7398 } 7399 7400 /** 7401 * Tests the case when the user doesn't turn the profile on in time, verifies that the user is 7402 * warned with a notification and then the apps get suspended. 7403 */ 7404 @Test 7405 @Ignore // Temp disabled - broken with flags testMaximumProfileTimeOff_profileOffTimeExceeded()7406 public void testMaximumProfileTimeOff_profileOffTimeExceeded() throws Exception { 7407 prepareMocksForSetMaximumProfileTimeOff(); 7408 7409 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7410 dpm.setManagedProfileMaximumTimeOff(admin1, PROFILE_OFF_TIMEOUT); 7411 7412 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7413 // The profile is running, neither alarm nor notification should be posted. 7414 verify(getServices().alarmManager, never()) 7415 .set(anyInt(), anyLong(), any(PendingIntent.class)); 7416 verify(getServices().notificationManager, never()) 7417 .notify(anyInt(), any(Notification.class)); 7418 // Apps shouldn't be suspended. 7419 verifyZeroInteractions(getServices().ipackageManager); 7420 clearInvocations(getServices().alarmManager); 7421 7422 setUserUnlocked(CALLER_USER_HANDLE, false); 7423 sendBroadcastWithUser(dpms, Intent.ACTION_USER_STOPPED, CALLER_USER_HANDLE); 7424 7425 // Verify the alarm was scheduled for time when the warning should be shown. 7426 verify(getServices().alarmManager, times(1)) 7427 .set(anyInt(), eq(PROFILE_OFF_WARNING_TIME), any()); 7428 // But still no notification should be posted at this point. 7429 verify(getServices().notificationManager, never()) 7430 .notify(anyInt(), any(Notification.class)); 7431 // Apps shouldn't be suspended. 7432 verifyZeroInteractions(getServices().ipackageManager); 7433 clearInvocations(getServices().alarmManager); 7434 7435 // Pretend the alarm went off. 7436 dpms.mMockInjector.setSystemCurrentTimeMillis(PROFILE_OFF_WARNING_TIME + 10); 7437 sendBroadcastWithUser(dpms, ACTION_PROFILE_OFF_DEADLINE, CALLER_USER_HANDLE); 7438 7439 // Verify the alarm was scheduled for the actual deadline this time. 7440 verify(getServices().alarmManager, times(1)).set(anyInt(), eq(PROFILE_OFF_DEADLINE), any()); 7441 // Now the user should see a warning notification. 7442 verify(getServices().notificationManager, times(1)) 7443 .notifyAsUser(any(), anyInt(), any(), any()); 7444 // Apps shouldn't be suspended yet. 7445 verifyZeroInteractions(getServices().ipackageManager); 7446 clearInvocations(getServices().alarmManager); 7447 clearInvocations(getServices().notificationManager); 7448 7449 // Pretend the alarm went off. 7450 dpms.mMockInjector.setSystemCurrentTimeMillis(PROFILE_OFF_DEADLINE + 10); 7451 sendBroadcastWithUser(dpms, ACTION_PROFILE_OFF_DEADLINE, CALLER_USER_HANDLE); 7452 7453 // Verify the alarm was not set. 7454 verifyZeroInteractions(getServices().alarmManager); 7455 // Now the user should see a notification about suspended apps. 7456 verify(getServices().notificationManager, times(1)) 7457 .notifyAsUser(any(), anyInt(), any(), any()); 7458 // Verify that the apps are suspended. 7459 verify(getServices().packageManagerInternal, times(1)) 7460 .setPackagesSuspendedByAdmin(anyInt(), any(), eq(true)); 7461 } 7462 7463 /** 7464 * Tests the case when the user turns the profile back on long before the deadline (> 1 day). 7465 */ 7466 @Test testMaximumProfileTimeOff_turnOnBeforeWarning()7467 public void testMaximumProfileTimeOff_turnOnBeforeWarning() throws Exception { 7468 prepareMocksForSetMaximumProfileTimeOff(); 7469 7470 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7471 dpm.setManagedProfileMaximumTimeOff(admin1, PROFILE_OFF_TIMEOUT); 7472 7473 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7474 setUserUnlocked(CALLER_USER_HANDLE, false); 7475 sendBroadcastWithUser(dpms, Intent.ACTION_USER_STOPPED, CALLER_USER_HANDLE); 7476 clearInvocations(getServices().alarmManager); 7477 setUserUnlocked(CALLER_USER_HANDLE, true); 7478 sendBroadcastWithUser(dpms, Intent.ACTION_USER_UNLOCKED, CALLER_USER_HANDLE); 7479 7480 // Verify that the alarm got discharged. 7481 verify(getServices().alarmManager, times(1)).cancel((PendingIntent) null); 7482 } 7483 7484 /** 7485 * Tests the case when the user turns the profile back on after the warning notification. 7486 */ 7487 @Test testMaximumProfileTimeOff_turnOnAfterWarning()7488 public void testMaximumProfileTimeOff_turnOnAfterWarning() throws Exception { 7489 prepareMocksForSetMaximumProfileTimeOff(); 7490 7491 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7492 dpm.setManagedProfileMaximumTimeOff(admin1, PROFILE_OFF_TIMEOUT); 7493 7494 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7495 setUserUnlocked(CALLER_USER_HANDLE, false); 7496 sendBroadcastWithUser(dpms, Intent.ACTION_USER_STOPPED, CALLER_USER_HANDLE); 7497 7498 // Pretend the alarm went off. 7499 dpms.mMockInjector.setSystemCurrentTimeMillis(PROFILE_OFF_WARNING_TIME + 10); 7500 sendBroadcastWithUser(dpms, ACTION_PROFILE_OFF_DEADLINE, CALLER_USER_HANDLE); 7501 7502 clearInvocations(getServices().alarmManager); 7503 clearInvocations(getServices().notificationManager); 7504 setUserUnlocked(CALLER_USER_HANDLE, true); 7505 sendBroadcastWithUser(dpms, Intent.ACTION_USER_UNLOCKED, CALLER_USER_HANDLE); 7506 7507 // Verify that the alarm got discharged. 7508 verify(getServices().alarmManager, times(1)).cancel((PendingIntent) null); 7509 // Verify that the notification is removed. 7510 verify(getServices().notificationManager, times(1)) 7511 .cancel(eq(SystemMessageProto.SystemMessage.NOTE_PERSONAL_APPS_SUSPENDED)); 7512 } 7513 7514 /** 7515 * Tests the case when the user turns the profile back on when the apps are already suspended. 7516 */ 7517 @Test 7518 @Ignore("b/277916462") testMaximumProfileTimeOff_turnOnAfterDeadline()7519 public void testMaximumProfileTimeOff_turnOnAfterDeadline() throws Exception { 7520 prepareMocksForSetMaximumProfileTimeOff(); 7521 7522 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7523 dpm.setManagedProfileMaximumTimeOff(admin1, PROFILE_OFF_TIMEOUT); 7524 7525 mContext.binder.callingUid = DpmMockContext.SYSTEM_UID; 7526 setUserUnlocked(CALLER_USER_HANDLE, false); 7527 sendBroadcastWithUser(dpms, Intent.ACTION_USER_STOPPED, CALLER_USER_HANDLE); 7528 7529 // Pretend the alarm went off after the deadline. 7530 dpms.mMockInjector.setSystemCurrentTimeMillis(PROFILE_OFF_DEADLINE + 10); 7531 sendBroadcastWithUser(dpms, ACTION_PROFILE_OFF_DEADLINE, CALLER_USER_HANDLE); 7532 7533 clearInvocations(getServices().alarmManager); 7534 clearInvocations(getServices().notificationManager); 7535 clearInvocations(getServices().ipackageManager); 7536 7537 // Pretend the user clicked on the "apps suspended" notification to turn the profile on. 7538 sendBroadcastWithUser(dpms, ACTION_TURN_PROFILE_ON_NOTIFICATION, CALLER_USER_HANDLE); 7539 // Verify that the profile is turned on. 7540 verify(getServices().userManager, times(1)) 7541 .requestQuietModeEnabled(eq(false), eq(UserHandle.of(CALLER_USER_HANDLE))); 7542 7543 setUserUnlocked(CALLER_USER_HANDLE, true); 7544 sendBroadcastWithUser(dpms, Intent.ACTION_USER_UNLOCKED, CALLER_USER_HANDLE); 7545 7546 // Verify that the notification is removed (at this point DPC should show it). 7547 verify(getServices().notificationManager, times(1)) 7548 .cancel(eq(SystemMessageProto.SystemMessage.NOTE_PERSONAL_APPS_SUSPENDED)); 7549 // Verify that the apps are NOT unsuspeded. 7550 verify(getServices().ipackageManager, never()).setPackagesSuspendedAsUser( 7551 any(), eq(false), any(), any(), any(), anyInt(), any(), anyInt(), anyInt()); 7552 // Verify that DPC is invoked to check policy compliance. 7553 verify(mContext.spiedContext).startActivityAsUser( 7554 MockUtils.checkIntentAction(ACTION_CHECK_POLICY_COMPLIANCE), 7555 MockUtils.checkUserHandle(CALLER_USER_HANDLE)); 7556 7557 // Verify that correct suspension reason is reported to the DPC. 7558 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7559 assertThat(dpm.getPersonalAppsSuspendedReasons(admin1)) 7560 .isEqualTo(DevicePolicyManager.PERSONAL_APPS_SUSPENDED_PROFILE_TIMEOUT); 7561 7562 // Verify that rolling time back doesn't change the status. 7563 dpms.mMockInjector.setSystemCurrentTimeMillis(PROFILE_OFF_START); 7564 assertThat(dpm.getPersonalAppsSuspendedReasons(admin1)) 7565 .isEqualTo(DevicePolicyManager.PERSONAL_APPS_SUSPENDED_PROFILE_TIMEOUT); 7566 } 7567 7568 @Test testSetRequiredPasswordComplexity_UnauthorizedCallersOnDO()7569 public void testSetRequiredPasswordComplexity_UnauthorizedCallersOnDO() throws Exception { 7570 assumeDeprecatedPasswordApisSupported(); 7571 7572 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 7573 setupDeviceOwner(); 7574 // DO must be able to set it. 7575 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW); 7576 // But not on the parent DPM. 7577 assertExpectException(IllegalArgumentException.class, null, 7578 () -> parentDpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW)); 7579 // Another package must not be allowed to set password complexity. 7580 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 7581 assertExpectException(SecurityException.class, null, 7582 () -> dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW)); 7583 } 7584 7585 @Test testSetRequiredPasswordComplexity_UnauthorizedCallersOnPO()7586 public void testSetRequiredPasswordComplexity_UnauthorizedCallersOnPO() throws Exception { 7587 assumeDeprecatedPasswordApisSupported(); 7588 7589 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7590 setupProfileOwner(); 7591 // PO must be able to set it. 7592 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW); 7593 // And on the parent profile DPM instance. 7594 parentDpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW); 7595 // Another package must not be allowed to set password complexity. 7596 mContext.binder.callingUid = DpmMockContext.ANOTHER_UID; 7597 assertExpectException(SecurityException.class, null, 7598 () -> dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_LOW)); 7599 } 7600 7601 @Test testSetRequiredPasswordComplexity_validValuesOnly()7602 public void testSetRequiredPasswordComplexity_validValuesOnly() throws Exception { 7603 assumeDeprecatedPasswordApisSupported(); 7604 7605 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7606 setupProfileOwner(); 7607 7608 // Cannot set value other than password_complexity none/low/medium/high 7609 assertExpectException(IllegalArgumentException.class, null, () -> 7610 dpm.setRequiredPasswordComplexity(-1)); 7611 assertExpectException(IllegalArgumentException.class, null, () -> 7612 dpm.setRequiredPasswordComplexity(7)); 7613 assertExpectException(IllegalArgumentException.class, null, () -> 7614 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH + 1)); 7615 7616 final Set<Integer> allowedModes = Set.of(PASSWORD_COMPLEXITY_NONE, PASSWORD_COMPLEXITY_LOW, 7617 PASSWORD_COMPLEXITY_MEDIUM, PASSWORD_COMPLEXITY_HIGH); 7618 for (int complexity : allowedModes) { 7619 // Ensure exception is not thrown. 7620 dpm.setRequiredPasswordComplexity(complexity); 7621 } 7622 } 7623 7624 @Test testSetRequiredPasswordComplexity_setAndGet()7625 public void testSetRequiredPasswordComplexity_setAndGet() throws Exception { 7626 assumeDeprecatedPasswordApisSupported(); 7627 7628 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7629 setupProfileOwner(); 7630 7631 final Set<Integer> allowedModes = Set.of(PASSWORD_COMPLEXITY_NONE, PASSWORD_COMPLEXITY_LOW, 7632 PASSWORD_COMPLEXITY_MEDIUM, PASSWORD_COMPLEXITY_HIGH); 7633 for (int complexity : allowedModes) { 7634 dpm.setRequiredPasswordComplexity(complexity); 7635 assertThat(dpm.getRequiredPasswordComplexity()).isEqualTo(complexity); 7636 } 7637 } 7638 7639 @Test testSetRequiredPasswordComplexityOnParent_setAndGet()7640 public void testSetRequiredPasswordComplexityOnParent_setAndGet() throws Exception { 7641 assumeDeprecatedPasswordApisSupported(); 7642 7643 final int managedProfileUserId = 15; 7644 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 7645 7646 addManagedProfile(admin1, managedProfileAdminUid, admin1); 7647 mContext.binder.callingUid = managedProfileAdminUid; 7648 when(getServices().userManager.isManagedProfile()).thenReturn(true); 7649 7650 final Set<Integer> allowedModes = Set.of(PASSWORD_COMPLEXITY_NONE, PASSWORD_COMPLEXITY_LOW, 7651 PASSWORD_COMPLEXITY_MEDIUM, PASSWORD_COMPLEXITY_HIGH); 7652 for (int complexity : allowedModes) { 7653 dpm.getParentProfileInstance(admin1).setRequiredPasswordComplexity(complexity); 7654 assertThat(dpm.getParentProfileInstance(admin1).getRequiredPasswordComplexity()) 7655 .isEqualTo(complexity); 7656 assertThat(dpm.getRequiredPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_NONE); 7657 } 7658 } 7659 7660 @Test testSetRequiredPasswordComplexity_isSufficient()7661 public void testSetRequiredPasswordComplexity_isSufficient() throws Exception { 7662 assumeDeprecatedPasswordApisSupported(); 7663 7664 mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; 7665 mContext.packageName = admin1.getPackageName(); 7666 setupDeviceOwner(); 7667 7668 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 7669 assertThat(dpm.getRequiredPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_HIGH); 7670 when(getServices().packageManager.getPackagesForUid( 7671 DpmMockContext.CALLER_SYSTEM_USER_UID)).thenReturn(new String[0]); 7672 mServiceContext.permissions.add(permission.REQUEST_PASSWORD_COMPLEXITY); 7673 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_NONE); 7674 7675 reset(mContext.spiedContext); 7676 PasswordMetrics passwordMetricsNoSymbols = metricsForPin("1234"); 7677 setActivePasswordState(passwordMetricsNoSymbols); 7678 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_LOW); 7679 assertThat(dpm.isActivePasswordSufficient()).isFalse(); 7680 7681 reset(mContext.spiedContext); 7682 passwordMetricsNoSymbols = metricsForPassword("84125312943a"); 7683 setActivePasswordState(passwordMetricsNoSymbols); 7684 assertThat(dpm.getPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_HIGH); 7685 // using isActivePasswordSufficient 7686 assertThat(dpm.isActivePasswordSufficient()).isTrue(); 7687 } 7688 7689 @Test testSetRequiredPasswordComplexity_resetBySettingQuality()7690 public void testSetRequiredPasswordComplexity_resetBySettingQuality() throws Exception { 7691 assumeDeprecatedPasswordApisSupported(); 7692 7693 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7694 setupProfileOwner(); 7695 7696 // Test that calling setPasswordQuality resets complexity to none. 7697 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 7698 assertThat(dpm.getRequiredPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_HIGH); 7699 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX); 7700 assertThat(dpm.getRequiredPasswordComplexity()).isEqualTo(PASSWORD_COMPLEXITY_NONE); 7701 } 7702 7703 @Test testSetRequiredPasswordComplexity_overridesQuality()7704 public void testSetRequiredPasswordComplexity_overridesQuality() throws Exception { 7705 assumeDeprecatedPasswordApisSupported(); 7706 7707 mContext.binder.callingUid = DpmMockContext.CALLER_UID; 7708 setupProfileOwner(); 7709 7710 // Test that calling setRequiredPasswordComplexity resets password quality. 7711 dpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX); 7712 assertThat(dpm.getPasswordQuality(admin1)).isEqualTo( 7713 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX); 7714 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 7715 assertThat(dpm.getPasswordQuality(admin1)).isEqualTo( 7716 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED); 7717 } 7718 7719 @Test testSetRequiredPasswordComplexityFailsWithQualityOnParent()7720 public void testSetRequiredPasswordComplexityFailsWithQualityOnParent() throws Exception { 7721 assumeDeprecatedPasswordApisSupported(); 7722 7723 final int managedProfileUserId = CALLER_USER_HANDLE; 7724 final int managedProfileAdminUid = 7725 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 7726 mContext.binder.callingUid = managedProfileAdminUid; 7727 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 7728 7729 parentDpm.setPasswordQuality(admin1, DevicePolicyManager.PASSWORD_QUALITY_COMPLEX); 7730 7731 assertThrows(IllegalStateException.class, 7732 () -> dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH)); 7733 } 7734 7735 @Test testSetQualityOnParentFailsWithComplexityOnProfile()7736 public void testSetQualityOnParentFailsWithComplexityOnProfile() throws Exception { 7737 assumeDeprecatedPasswordApisSupported(); 7738 7739 final int managedProfileUserId = CALLER_USER_HANDLE; 7740 final int managedProfileAdminUid = 7741 UserHandle.getUid(managedProfileUserId, DpmMockContext.SYSTEM_UID); 7742 mContext.binder.callingUid = managedProfileAdminUid; 7743 addManagedProfile(admin1, managedProfileAdminUid, admin1, VERSION_CODES.R); 7744 7745 dpm.setRequiredPasswordComplexity(PASSWORD_COMPLEXITY_HIGH); 7746 7747 assertThrows(IllegalStateException.class, 7748 () -> parentDpm.setPasswordQuality(admin1, 7749 DevicePolicyManager.PASSWORD_QUALITY_COMPLEX)); 7750 } 7751 7752 @Test testSetDeviceOwnerType_throwsExceptionWhenCallerNotAuthorized()7753 public void testSetDeviceOwnerType_throwsExceptionWhenCallerNotAuthorized() { 7754 assertThrows(SecurityException.class, 7755 () -> dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT)); 7756 } 7757 7758 @Test testSetDeviceOwnerType_throwsExceptionWhenThereIsNoDeviceOwner()7759 public void testSetDeviceOwnerType_throwsExceptionWhenThereIsNoDeviceOwner() { 7760 mContext.binder.clearCallingIdentity(); 7761 assertThrows(IllegalStateException.class, 7762 () -> dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT)); 7763 } 7764 7765 @Test testSetDeviceOwnerType_throwsExceptionWhenNotAsDeviceOwnerAdmin()7766 public void testSetDeviceOwnerType_throwsExceptionWhenNotAsDeviceOwnerAdmin() throws Exception { 7767 setDeviceOwner(); 7768 7769 assertThrows(IllegalStateException.class, 7770 () -> dpm.setDeviceOwnerType(admin2, DEVICE_OWNER_TYPE_FINANCED)); 7771 } 7772 7773 @Test testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice()7774 public void testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice() throws Exception { 7775 setDeviceOwner(); 7776 7777 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7778 7779 assertThat(dpm.isFinancedDevice()).isTrue(); 7780 initializeDpms(); 7781 assertThat(dpm.isFinancedDevice()).isTrue(); 7782 } 7783 7784 @Test testSetDeviceOwnerType_asDeviceOwner_setDeviceOwnerTypeTwice_success()7785 public void testSetDeviceOwnerType_asDeviceOwner_setDeviceOwnerTypeTwice_success() 7786 throws Exception { 7787 setDeviceOwner(); 7788 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT); 7789 7790 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7791 7792 assertThat(dpm.isFinancedDevice()).isTrue(); 7793 } 7794 7795 @Test testIsFinancedDevice_throwsExceptionWhenThereIsNoDeviceOwner()7796 public void testIsFinancedDevice_throwsExceptionWhenThereIsNoDeviceOwner() { 7797 assertThrows(SecurityException.class, () -> dpm.isFinancedDevice()); 7798 } 7799 7800 @Test testSetUserRestriction_financeDo_invalidRestrictions_restrictionNotSet()7801 public void testSetUserRestriction_financeDo_invalidRestrictions_restrictionNotSet() 7802 throws Exception { 7803 setDeviceOwner(); 7804 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7805 7806 for (String restriction : UserRestrictionsUtils.USER_RESTRICTIONS) { 7807 if (!UserRestrictionsUtils.canFinancedDeviceOwnerChange(restriction)) { 7808 assertNoDeviceOwnerRestrictions(); 7809 assertExpectException(SecurityException.class, /* messageRegex= */ null, 7810 () -> dpm.addUserRestriction(admin1, restriction)); 7811 7812 verify(getServices().userManagerInternal, never()) 7813 .setDevicePolicyUserRestrictions(anyInt(), any(), any(), anyBoolean()); 7814 DpmTestUtils.assertRestrictions(new Bundle(), dpm.getUserRestrictions(admin1)); 7815 } 7816 } 7817 } 7818 7819 @Test 7820 @Ignore("b/277916462") testSetUserRestriction_financeDo_validRestrictions_setsRestriction()7821 public void testSetUserRestriction_financeDo_validRestrictions_setsRestriction() 7822 throws Exception { 7823 setDeviceOwner(); 7824 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7825 7826 for (String restriction : UserRestrictionsUtils.USER_RESTRICTIONS) { 7827 if (UserRestrictionsUtils.canFinancedDeviceOwnerChange(restriction)) { 7828 assertNoDeviceOwnerRestrictions(); 7829 dpm.addUserRestriction(admin1, restriction); 7830 7831 Bundle globalRestrictions = 7832 dpms.getDeviceOwnerAdminLocked().getGlobalUserRestrictions( 7833 UserManagerInternal.OWNER_TYPE_DEVICE_OWNER); 7834 RestrictionsSet localRestrictions = new RestrictionsSet(); 7835 localRestrictions.updateRestrictions( 7836 UserHandle.USER_SYSTEM, 7837 dpms.getDeviceOwnerAdminLocked().getLocalUserRestrictions( 7838 UserManagerInternal.OWNER_TYPE_DEVICE_OWNER)); 7839 verify(getServices().userManagerInternal) 7840 .setDevicePolicyUserRestrictions(eq(UserHandle.USER_SYSTEM), 7841 MockUtils.checkUserRestrictions(globalRestrictions), 7842 MockUtils.checkUserRestrictions( 7843 UserHandle.USER_SYSTEM, localRestrictions), 7844 eq(true)); 7845 reset(getServices().userManagerInternal); 7846 7847 DpmTestUtils.assertRestrictions(DpmTestUtils.newRestrictions(restriction), 7848 dpm.getUserRestrictions(admin1)); 7849 7850 dpm.clearUserRestriction(admin1, restriction); 7851 reset(getServices().userManagerInternal); 7852 } 7853 } 7854 } 7855 7856 @Test 7857 @FlakyTest(bugId = 260145949) testSetLockTaskFeatures_financeDo_validLockTaskFeatures_lockTaskFeaturesSet()7858 public void testSetLockTaskFeatures_financeDo_validLockTaskFeatures_lockTaskFeaturesSet() 7859 throws Exception { 7860 int validLockTaskFeatures = LOCK_TASK_FEATURE_SYSTEM_INFO | LOCK_TASK_FEATURE_KEYGUARD 7861 | LOCK_TASK_FEATURE_HOME | LOCK_TASK_FEATURE_GLOBAL_ACTIONS 7862 | LOCK_TASK_FEATURE_NOTIFICATIONS | LOCK_TASK_FEATURE_BLOCK_ACTIVITY_START_IN_TASK; 7863 setDeviceOwner(); 7864 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7865 7866 dpm.setLockTaskFeatures(admin1, validLockTaskFeatures); 7867 7868 verify(getServices().iactivityTaskManager) 7869 .updateLockTaskFeatures(eq(UserHandle.USER_SYSTEM), eq(validLockTaskFeatures)); 7870 } 7871 7872 @Test 7873 @FlakyTest(bugId = 260145949) testSetLockTaskFeatures_financeDo_invalidLockTaskFeatures_throwsException()7874 public void testSetLockTaskFeatures_financeDo_invalidLockTaskFeatures_throwsException() 7875 throws Exception { 7876 int invalidLockTaskFeatures = LOCK_TASK_FEATURE_NONE | LOCK_TASK_FEATURE_OVERVIEW 7877 | LOCK_TASK_FEATURE_HOME; 7878 setDeviceOwner(); 7879 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7880 // Called during setup. 7881 verify(getServices().iactivityTaskManager).updateLockTaskFeatures(anyInt(), anyInt()); 7882 7883 assertExpectException(SecurityException.class, /* messageRegex= */ null, 7884 () -> dpm.setLockTaskFeatures(admin1, invalidLockTaskFeatures)); 7885 7886 verifyNoMoreInteractions(getServices().iactivityTaskManager); 7887 } 7888 7889 @Test 7890 @FlakyTest(bugId = 260145949) testIsUninstallBlocked_financeDo_success()7891 public void testIsUninstallBlocked_financeDo_success() throws Exception { 7892 String packageName = "com.android.foo.package"; 7893 setDeviceOwner(); 7894 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7895 when(getServices().ipackageManager.getBlockUninstallForUser( 7896 eq(packageName), eq(UserHandle.getCallingUserId()))) 7897 .thenReturn(true); 7898 7899 assertThat(dpm.isUninstallBlocked(admin1, packageName)).isTrue(); 7900 } 7901 7902 @Test 7903 @Ignore("b/277916462") testSetUninstallBlocked_financeDo_success()7904 public void testSetUninstallBlocked_financeDo_success() throws Exception { 7905 String packageName = "com.android.foo.package"; 7906 setDeviceOwner(); 7907 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7908 7909 dpm.setUninstallBlocked(admin1, packageName, false); 7910 7911 verify(getServices().ipackageManager) 7912 .setBlockUninstallForUser(eq(packageName), eq(false), 7913 eq(UserHandle.USER_SYSTEM)); 7914 } 7915 7916 @Test 7917 @Ignore("b/277916462") testSetUserControlDisabledPackages_financeDo_success()7918 public void testSetUserControlDisabledPackages_financeDo_success() throws Exception { 7919 List<String> packages = new ArrayList<>(); 7920 packages.add("com.android.foo.package"); 7921 setDeviceOwner(); 7922 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7923 7924 dpm.setUserControlDisabledPackages(admin1, packages); 7925 7926 verify(getServices().packageManagerInternal) 7927 .setOwnerProtectedPackages(eq(UserHandle.USER_ALL), eq(packages)); 7928 } 7929 7930 @Test testGetUserControlDisabledPackages_financeDo_success()7931 public void testGetUserControlDisabledPackages_financeDo_success() throws Exception { 7932 setDeviceOwner(); 7933 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7934 7935 assertThat(dpm.getUserControlDisabledPackages(admin1)).isEmpty(); 7936 } 7937 7938 @Test testSetOrganizationName_financeDo_success()7939 public void testSetOrganizationName_financeDo_success() throws Exception { 7940 String organizationName = "Test Organization"; 7941 setDeviceOwner(); 7942 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7943 7944 dpm.setOrganizationName(admin1, organizationName); 7945 7946 assertThat(dpm.getDeviceOwnerOrganizationName()).isEqualTo(organizationName); 7947 } 7948 7949 @Test testSetShortSupportMessage_financeDo_success()7950 public void testSetShortSupportMessage_financeDo_success() throws Exception { 7951 String supportMessage = "Test short support message"; 7952 setDeviceOwner(); 7953 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7954 7955 dpm.setShortSupportMessage(admin1, supportMessage); 7956 7957 assertThat(dpm.getShortSupportMessage(admin1)).isEqualTo(supportMessage); 7958 } 7959 7960 @Test testIsBackupServiceEnabled_financeDo_success()7961 public void testIsBackupServiceEnabled_financeDo_success() throws Exception { 7962 setDeviceOwner(); 7963 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7964 when(getServices().ibackupManager.isBackupServiceActive(eq(UserHandle.USER_SYSTEM))) 7965 .thenReturn(true); 7966 7967 assertThat(dpm.isBackupServiceEnabled(admin1)).isTrue(); 7968 } 7969 7970 @Test testSetBackupServiceEnabled_financeDo_success()7971 public void testSetBackupServiceEnabled_financeDo_success() throws Exception { 7972 setDeviceOwner(); 7973 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7974 7975 dpm.setBackupServiceEnabled(admin1, true); 7976 7977 verify(getServices().ibackupManager) 7978 .setBackupServiceActive(eq(UserHandle.USER_SYSTEM), eq(true)); 7979 } 7980 7981 @Test testIsLockTaskPermitted_financeDo_success()7982 public void testIsLockTaskPermitted_financeDo_success() throws Exception { 7983 String packageName = "com.android.foo.package"; 7984 mockPolicyExemptApps(packageName); 7985 mockVendorPolicyExemptApps(); 7986 setDeviceOwner(); 7987 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7988 7989 assertThat(dpm.isLockTaskPermitted(packageName)).isTrue(); 7990 } 7991 7992 @Test 7993 @FlakyTest(bugId = 260145949) testSetLockTaskPackages_financeDo_success()7994 public void testSetLockTaskPackages_financeDo_success() throws Exception { 7995 String[] packages = {"com.android.foo.package"}; 7996 mockEmptyPolicyExemptApps(); 7997 setDeviceOwner(); 7998 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 7999 8000 dpm.setLockTaskPackages(admin1, packages); 8001 8002 verify(getServices().iactivityManager) 8003 .updateLockTaskPackages(eq(UserHandle.USER_SYSTEM), eq(packages)); 8004 } 8005 8006 @Test 8007 @Ignore("b/277916462") testAddPersistentPreferredActivity_financeDo_success()8008 public void testAddPersistentPreferredActivity_financeDo_success() throws Exception { 8009 IntentFilter filter = new IntentFilter(); 8010 ComponentName target = new ComponentName(admin2.getPackageName(), "test.class"); 8011 setDeviceOwner(); 8012 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8013 8014 dpm.addPersistentPreferredActivity(admin1, filter, target); 8015 8016 verify(getServices().ipackageManager) 8017 .addPersistentPreferredActivity(eq(filter), eq(target), eq(UserHandle.USER_SYSTEM)); 8018 verify(getServices().ipackageManager) 8019 .flushPackageRestrictionsAsUser(eq(UserHandle.USER_SYSTEM)); 8020 } 8021 8022 @Test 8023 @Ignore("b/277916462") testClearPackagePersistentPreferredActvities_financeDo_success()8024 public void testClearPackagePersistentPreferredActvities_financeDo_success() throws Exception { 8025 String packageName = admin2.getPackageName(); 8026 setDeviceOwner(); 8027 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8028 8029 dpm.clearPackagePersistentPreferredActivities(admin1, packageName); 8030 8031 verify(getServices().ipackageManager) 8032 .clearPackagePersistentPreferredActivities( 8033 eq(packageName), eq(UserHandle.USER_SYSTEM)); 8034 verify(getServices().ipackageManager) 8035 .flushPackageRestrictionsAsUser(eq(UserHandle.USER_SYSTEM)); 8036 } 8037 8038 @Test testWipeDevice_financeDo_success()8039 public void testWipeDevice_financeDo_success() throws Exception { 8040 setDeviceOwner(); 8041 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8042 when(getServices().userManager.getUserRestrictionSource( 8043 UserManager.DISALLOW_FACTORY_RESET, 8044 UserHandle.SYSTEM)) 8045 .thenReturn(UserManager.RESTRICTION_SOURCE_DEVICE_OWNER); 8046 when(mMockContext.getResources() 8047 .getString(R.string.work_profile_deleted_description_dpm_wipe)) 8048 .thenReturn("Test string"); 8049 8050 dpm.wipeDevice(0); 8051 8052 verifyRebootWipeUserData(/* wipeEuicc= */ false); 8053 } 8054 8055 @Test testIsDeviceOwnerApp_financeDo_success()8056 public void testIsDeviceOwnerApp_financeDo_success() throws Exception { 8057 setDeviceOwner(); 8058 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8059 8060 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isTrue(); 8061 } 8062 8063 @Test testClearDeviceOwnerApp_financeDo_success()8064 public void testClearDeviceOwnerApp_financeDo_success() throws Exception { 8065 setDeviceOwner(); 8066 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8067 8068 dpm.clearDeviceOwnerApp(admin1.getPackageName()); 8069 8070 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isNull(); 8071 assertThat(dpm.isAdminActiveAsUser(admin1, UserHandle.USER_SYSTEM)).isFalse(); 8072 verify(mMockContext.spiedContext, times(2)) 8073 .sendBroadcastAsUser( 8074 MockUtils.checkIntentAction( 8075 DevicePolicyManager.ACTION_DEVICE_OWNER_CHANGED), 8076 eq(UserHandle.SYSTEM)); 8077 } 8078 8079 @Test testSetPermissionGrantState_financeDo_notReadPhoneStatePermission_throwsException()8080 public void testSetPermissionGrantState_financeDo_notReadPhoneStatePermission_throwsException() 8081 throws Exception { 8082 setDeviceOwner(); 8083 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8084 8085 assertExpectException(SecurityException.class, /* messageRegex= */ null, 8086 () -> dpm.setPermissionGrantState(admin1, admin1.getPackageName(), 8087 permission.READ_CALENDAR, 8088 DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED)); 8089 } 8090 8091 @Test testSetPermissionGrantState_financeDo_grantPermissionToNonDeviceOwnerPackage_throwsException()8092 public void testSetPermissionGrantState_financeDo_grantPermissionToNonDeviceOwnerPackage_throwsException() 8093 throws Exception { 8094 setDeviceOwner(); 8095 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8096 8097 assertExpectException(SecurityException.class, /* messageRegex= */ null, 8098 () -> dpm.setPermissionGrantState(admin1, "com.android.foo.package", 8099 permission.READ_PHONE_STATE, 8100 DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED)); 8101 } 8102 8103 @Test testGetPermissionGrantState_financeDo_notReadPhoneStatePermission_throwsException()8104 public void testGetPermissionGrantState_financeDo_notReadPhoneStatePermission_throwsException() 8105 throws Exception { 8106 setDeviceOwner(); 8107 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8108 8109 assertExpectException(SecurityException.class, /* messageRegex= */ null, 8110 () -> dpm.getPermissionGrantState(admin1, admin1.getPackageName(), 8111 permission.READ_CALENDAR)); 8112 } 8113 8114 @Test testGetPermissionGrantState_financeDo_notDeviceOwnerPackage_throwsException()8115 public void testGetPermissionGrantState_financeDo_notDeviceOwnerPackage_throwsException() 8116 throws Exception { 8117 setDeviceOwner(); 8118 dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); 8119 8120 assertExpectException(SecurityException.class, /* messageRegex= */ null, 8121 () -> dpm.getPermissionGrantState(admin1, "com.android.foo.package", 8122 permission.READ_PHONE_STATE)); 8123 } 8124 8125 @Test testSetUsbDataSignalingEnabled_noDeviceOwnerOrPoOfOrgOwnedDevice()8126 public void testSetUsbDataSignalingEnabled_noDeviceOwnerOrPoOfOrgOwnedDevice() { 8127 assertThrows(SecurityException.class, 8128 () -> dpm.setUsbDataSignalingEnabled(true)); 8129 } 8130 8131 @Test testSetUsbDataSignalingEnabled_asDeviceOwner()8132 public void testSetUsbDataSignalingEnabled_asDeviceOwner() throws Exception { 8133 setDeviceOwner(); 8134 when(getServices().usbManager.enableUsbDataSignal(false)).thenReturn(true); 8135 when(getServices().usbManager.getUsbHalVersion()).thenReturn(UsbManager.USB_HAL_V1_3); 8136 8137 assertThat(dpm.isUsbDataSignalingEnabled()).isTrue(); 8138 8139 dpm.setUsbDataSignalingEnabled(false); 8140 8141 assertThat(dpm.isUsbDataSignalingEnabled()).isFalse(); 8142 } 8143 8144 @Test testIsUsbDataSignalingEnabledForUser()8145 public void testIsUsbDataSignalingEnabledForUser() throws Exception { 8146 when(getServices().usbManager.enableUsbDataSignal(false)).thenReturn(true); 8147 when(getServices().usbManager.getUsbHalVersion()).thenReturn(UsbManager.USB_HAL_V1_3); 8148 setDeviceOwner(); 8149 dpm.setUsbDataSignalingEnabled(false); 8150 8151 assertThat(dpm.isUsbDataSignalingEnabled()).isFalse(); 8152 } 8153 8154 @Test testSetUsbDataSignalingEnabled_asPoOfOrgOwnedDevice()8155 public void testSetUsbDataSignalingEnabled_asPoOfOrgOwnedDevice() throws Exception { 8156 final int managedProfileUserId = 15; 8157 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 8158 addManagedProfile(admin1, managedProfileAdminUid, admin1); 8159 configureProfileOwnerOfOrgOwnedDevice(admin1, managedProfileUserId); 8160 mContext.binder.callingUid = managedProfileAdminUid; 8161 when(getServices().usbManager.enableUsbDataSignal(false)).thenReturn(true); 8162 when(getServices().usbManager.getUsbHalVersion()).thenReturn(UsbManager.USB_HAL_V1_3); 8163 8164 assertThat(dpm.isUsbDataSignalingEnabled()).isTrue(); 8165 8166 dpm.setUsbDataSignalingEnabled(false); 8167 8168 assertThat(dpm.isUsbDataSignalingEnabled()).isFalse(); 8169 } 8170 8171 @Test testCanUsbDataSignalingBeDisabled_canBeDisabled()8172 public void testCanUsbDataSignalingBeDisabled_canBeDisabled() throws Exception { 8173 when(getServices().usbManager.getUsbHalVersion()).thenReturn(UsbManager.USB_HAL_V1_3); 8174 8175 assertThat(dpm.canUsbDataSignalingBeDisabled()).isTrue(); 8176 } 8177 8178 @Test testCanUsbDataSignalingBeDisabled_cannotBeDisabled()8179 public void testCanUsbDataSignalingBeDisabled_cannotBeDisabled() throws Exception { 8180 setDeviceOwner(); 8181 when(getServices().usbManager.getUsbHalVersion()).thenReturn(UsbManager.USB_HAL_V1_2); 8182 8183 assertThat(dpm.canUsbDataSignalingBeDisabled()).isFalse(); 8184 assertThrows(IllegalStateException.class, 8185 () -> dpm.setUsbDataSignalingEnabled(true)); 8186 } 8187 8188 @Test testSetUsbDataSignalingEnabled_noChangeToActiveAdmin()8189 public void testSetUsbDataSignalingEnabled_noChangeToActiveAdmin() 8190 throws Exception { 8191 setDeviceOwner(); 8192 when(getServices().usbManager.getUsbHalVersion()).thenReturn(UsbManager.USB_HAL_V1_3); 8193 boolean enabled = dpm.isUsbDataSignalingEnabled(); 8194 8195 dpm.setUsbDataSignalingEnabled(true); 8196 8197 assertThat(dpm.isUsbDataSignalingEnabled()).isEqualTo(enabled); 8198 } 8199 8200 @Test testGetPolicyExemptApps_noPermission()8201 public void testGetPolicyExemptApps_noPermission() { 8202 assertThrows(SecurityException.class, () -> dpm.getPolicyExemptApps()); 8203 } 8204 8205 @Test testGetPolicyExemptApps_empty()8206 public void testGetPolicyExemptApps_empty() { 8207 grantManageDeviceAdmins(); 8208 mockPolicyExemptApps(); 8209 mockVendorPolicyExemptApps(); 8210 8211 assertThat(dpm.getPolicyExemptApps()).isEmpty(); 8212 } 8213 8214 @Test testGetPolicyExemptApps_baseOnly()8215 public void testGetPolicyExemptApps_baseOnly() { 8216 grantManageDeviceAdmins(); 8217 mockPolicyExemptApps("foo"); 8218 mockVendorPolicyExemptApps(); 8219 8220 assertThat(dpm.getPolicyExemptApps()).containsExactly("foo"); 8221 } 8222 8223 @Test testGetPolicyExemptApps_vendorOnly()8224 public void testGetPolicyExemptApps_vendorOnly() { 8225 grantManageDeviceAdmins(); 8226 mockPolicyExemptApps(); 8227 mockVendorPolicyExemptApps("bar"); 8228 8229 assertThat(dpm.getPolicyExemptApps()).containsExactly("bar"); 8230 } 8231 8232 @Test testGetPolicyExemptApps_baseAndVendor()8233 public void testGetPolicyExemptApps_baseAndVendor() { 8234 grantManageDeviceAdmins(); 8235 mockPolicyExemptApps("4", "23", "15", "42", "8"); 8236 mockVendorPolicyExemptApps("16", "15", "4"); 8237 8238 assertThat(dpm.getPolicyExemptApps()).containsExactly("4", "8", "15", "16", "23", "42"); 8239 } 8240 8241 @Test testSetGlobalPrivateDnsModeOpportunistic_asDeviceOwner()8242 public void testSetGlobalPrivateDnsModeOpportunistic_asDeviceOwner() throws Exception { 8243 setDeviceOwner(); 8244 // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the 8245 // feature is disabled because there are non-affiliated secondary users. 8246 getServices().removeUser(CALLER_USER_HANDLE); 8247 clearInvocations(getServices().settings); 8248 8249 int result = dpm.setGlobalPrivateDnsModeOpportunistic(admin1); 8250 8251 assertThat(result).isEqualTo(PRIVATE_DNS_SET_NO_ERROR); 8252 } 8253 8254 @Test testSetGlobalPrivateDnsModeOpportunistic_hasUnaffiliatedUsers()8255 public void testSetGlobalPrivateDnsModeOpportunistic_hasUnaffiliatedUsers() throws Exception { 8256 setDeviceOwner(); 8257 setAsProfileOwner(admin2); 8258 8259 assertThrows(SecurityException.class, 8260 () -> dpm.setGlobalPrivateDnsModeOpportunistic(admin1)); 8261 } 8262 8263 @Test testSetRecommendedGlobalProxy_asDeviceOwner()8264 public void testSetRecommendedGlobalProxy_asDeviceOwner() throws Exception { 8265 setDeviceOwner(); 8266 // setUp() adds a secondary user for CALLER_USER_HANDLE. Remove it as otherwise the 8267 // feature is disabled because there are non-affiliated secondary users. 8268 getServices().removeUser(CALLER_USER_HANDLE); 8269 8270 dpm.setRecommendedGlobalProxy(admin1, null); 8271 8272 verify(getServices().connectivityManager).setGlobalProxy(null); 8273 } 8274 8275 @Test testSetRecommendedGlobalProxy_hasUnaffiliatedUsers()8276 public void testSetRecommendedGlobalProxy_hasUnaffiliatedUsers() throws Exception { 8277 setDeviceOwner(); 8278 setAsProfileOwner(admin2); 8279 8280 assertThrows(SecurityException.class, () -> dpm.setRecommendedGlobalProxy(admin1, null)); 8281 } 8282 8283 @Test testSetAlwaysOnVpnPackage_clearsAdminVpn()8284 public void testSetAlwaysOnVpnPackage_clearsAdminVpn() throws Exception { 8285 setDeviceOwner(); 8286 8287 when(getServices().vpnManager 8288 .setAlwaysOnVpnPackageForUser(anyInt(), any(), anyBoolean(), any())) 8289 .thenReturn(true); 8290 8291 // Set VPN package to admin package. 8292 dpm.setAlwaysOnVpnPackage(admin1, admin1.getPackageName(), false, null); 8293 8294 verify(getServices().vpnManager).setAlwaysOnVpnPackageForUser( 8295 UserHandle.USER_SYSTEM, admin1.getPackageName(), false, null); 8296 8297 // Clear VPN package. 8298 dpm.setAlwaysOnVpnPackage(admin1, null, false, null); 8299 8300 // Change should be propagated to VpnManager 8301 verify(getServices().vpnManager).setAlwaysOnVpnPackageForUser( 8302 UserHandle.USER_SYSTEM, null, false, null); 8303 // The package should lose authorization to start VPN. 8304 verify(getServices().appOpsManager).setMode(OP_ACTIVATE_VPN, 8305 DpmMockContext.CALLER_SYSTEM_USER_UID, admin1.getPackageName(), MODE_DEFAULT); 8306 } 8307 8308 @Test testSetAlwaysOnVpnPackage_doesntKillUserVpn()8309 public void testSetAlwaysOnVpnPackage_doesntKillUserVpn() throws Exception { 8310 setDeviceOwner(); 8311 8312 when(getServices().vpnManager 8313 .setAlwaysOnVpnPackageForUser(anyInt(), any(), anyBoolean(), any())) 8314 .thenReturn(true); 8315 8316 // this time it shouldn't go into VpnManager anymore. 8317 dpm.setAlwaysOnVpnPackage(admin1, null, false, null); 8318 8319 verifyNoMoreInteractions(getServices().vpnManager); 8320 verifyNoMoreInteractions(getServices().appOpsManager); 8321 } 8322 8323 @Test testDisallowConfigVpn_clearsUserVpn()8324 public void testDisallowConfigVpn_clearsUserVpn() throws Exception { 8325 final String userVpnPackage = "org.some.vpn.servcie"; 8326 final int userVpnUid = 20374; 8327 8328 setDeviceOwner(); 8329 8330 setupVpnAuthorization(userVpnPackage, userVpnUid); 8331 8332 simulateRestrictionAdded(UserManager.DISALLOW_CONFIG_VPN); 8333 8334 verify(getServices().vpnManager).setAlwaysOnVpnPackageForUser( 8335 UserHandle.USER_SYSTEM, null, false, null); 8336 verify(getServices().appOpsManager).setMode(OP_ACTIVATE_VPN, 8337 userVpnUid, userVpnPackage, MODE_DEFAULT); 8338 } 8339 8340 @Test testDisallowConfigVpn_doesntKillAdminVpn()8341 public void testDisallowConfigVpn_doesntKillAdminVpn() throws Exception { 8342 setDeviceOwner(); 8343 8344 when(getServices().vpnManager 8345 .setAlwaysOnVpnPackageForUser(anyInt(), any(), anyBoolean(), any())) 8346 .thenReturn(true); 8347 8348 // Set VPN package to admin package. 8349 dpm.setAlwaysOnVpnPackage(admin1, admin1.getPackageName(), false, null); 8350 setupVpnAuthorization(admin1.getPackageName(), DpmMockContext.CALLER_SYSTEM_USER_UID); 8351 clearInvocations(getServices().vpnManager); 8352 8353 simulateRestrictionAdded(UserManager.DISALLOW_CONFIG_VPN); 8354 8355 // Admin-set package should remain always-on and should retain its authorization. 8356 verifyNoMoreInteractions(getServices().vpnManager); 8357 verify(getServices().appOpsManager, never()).setMode(OP_ACTIVATE_VPN, 8358 DpmMockContext.CALLER_SYSTEM_USER_UID, admin1.getPackageName(), MODE_DEFAULT); 8359 } 8360 8361 @Test testGetOrganizationNameForUser_calledByNonPrivilegedApp_throwsException()8362 public void testGetOrganizationNameForUser_calledByNonPrivilegedApp_throwsException() { 8363 assertExpectException(SecurityException.class, "Calling identity is not authorized", 8364 () -> dpm.getOrganizationNameForUser(UserHandle.USER_SYSTEM)); 8365 } 8366 8367 @Test testSetWifiMinimumSecurity_noDeviceOwnerOrPoOfOrgOwnedDevice()8368 public void testSetWifiMinimumSecurity_noDeviceOwnerOrPoOfOrgOwnedDevice() { 8369 assertThrows(SecurityException.class, () -> dpm.setMinimumRequiredWifiSecurityLevel( 8370 DevicePolicyManager.WIFI_SECURITY_PERSONAL)); 8371 } 8372 8373 @Test testSetWifiMinimumSecurity_asDeviceOwner()8374 public void testSetWifiMinimumSecurity_asDeviceOwner() throws Exception { 8375 setDeviceOwner(); 8376 8377 final Set<Integer> allowedLevels = Set.of(WIFI_SECURITY_OPEN, WIFI_SECURITY_PERSONAL, 8378 WIFI_SECURITY_ENTERPRISE_EAP, WIFI_SECURITY_ENTERPRISE_192); 8379 for (int level : allowedLevels) { 8380 dpm.setMinimumRequiredWifiSecurityLevel(level); 8381 assertThat(dpm.getMinimumRequiredWifiSecurityLevel()).isEqualTo(level); 8382 } 8383 } 8384 8385 @Test testSetWifiMinimumSecurity_asPoOfOrgOwnedDevice()8386 public void testSetWifiMinimumSecurity_asPoOfOrgOwnedDevice() throws Exception { 8387 final int managedProfileUserId = 15; 8388 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 8389 addManagedProfile(admin1, managedProfileAdminUid, admin1); 8390 configureProfileOwnerOfOrgOwnedDevice(admin1, managedProfileUserId); 8391 mContext.binder.callingUid = managedProfileAdminUid; 8392 8393 final Set<Integer> allowedLevels = Set.of(WIFI_SECURITY_OPEN, WIFI_SECURITY_PERSONAL, 8394 WIFI_SECURITY_ENTERPRISE_EAP, WIFI_SECURITY_ENTERPRISE_192); 8395 for (int level : allowedLevels) { 8396 dpm.setMinimumRequiredWifiSecurityLevel(level); 8397 assertThat(dpm.getMinimumRequiredWifiSecurityLevel()).isEqualTo(level); 8398 } 8399 } 8400 8401 @Test testSetSsidAllowlist_noDeviceOwnerOrPoOfOrgOwnedDevice()8402 public void testSetSsidAllowlist_noDeviceOwnerOrPoOfOrgOwnedDevice() { 8403 final Set<WifiSsid> ssids = new ArraySet<>( 8404 Arrays.asList(WifiSsid.fromBytes("ssid1".getBytes(StandardCharsets.UTF_8)))); 8405 WifiSsidPolicy policy = new WifiSsidPolicy( 8406 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_ALLOWLIST, ssids); 8407 assertThrows(SecurityException.class, () -> dpm.setWifiSsidPolicy(policy)); 8408 } 8409 8410 @Test testSetSsidAllowlist_asDeviceOwner()8411 public void testSetSsidAllowlist_asDeviceOwner() throws Exception { 8412 setDeviceOwner(); 8413 8414 final Set<WifiSsid> ssids = new ArraySet<>( 8415 Arrays.asList(WifiSsid.fromBytes("ssid1".getBytes(StandardCharsets.UTF_8)))); 8416 WifiSsidPolicy policy = new WifiSsidPolicy( 8417 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_ALLOWLIST, ssids); 8418 dpm.setWifiSsidPolicy(policy); 8419 assertThat(dpm.getWifiSsidPolicy().getSsids()).isEqualTo(ssids); 8420 assertThat(dpm.getWifiSsidPolicy().getPolicyType()).isEqualTo( 8421 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_ALLOWLIST); 8422 } 8423 8424 @Test testSetSsidAllowlist_asPoOfOrgOwnedDevice()8425 public void testSetSsidAllowlist_asPoOfOrgOwnedDevice() throws Exception { 8426 final int managedProfileUserId = 15; 8427 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 8428 addManagedProfile(admin1, managedProfileAdminUid, admin1); 8429 configureProfileOwnerOfOrgOwnedDevice(admin1, managedProfileUserId); 8430 mContext.binder.callingUid = managedProfileAdminUid; 8431 8432 final Set<WifiSsid> ssids = new ArraySet<>( 8433 Arrays.asList(WifiSsid.fromBytes("ssid1".getBytes(StandardCharsets.UTF_8)), 8434 WifiSsid.fromBytes("ssid2".getBytes(StandardCharsets.UTF_8)), 8435 WifiSsid.fromBytes("ssid3".getBytes(StandardCharsets.UTF_8)))); 8436 WifiSsidPolicy policy = new WifiSsidPolicy( 8437 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_ALLOWLIST, ssids); 8438 dpm.setWifiSsidPolicy(policy); 8439 assertThat(dpm.getWifiSsidPolicy().getSsids()).isEqualTo(ssids); 8440 assertThat(dpm.getWifiSsidPolicy().getPolicyType()).isEqualTo( 8441 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_ALLOWLIST); 8442 } 8443 8444 @Test testSetSsidAllowlist_emptyList()8445 public void testSetSsidAllowlist_emptyList() throws Exception { 8446 setDeviceOwner(); 8447 8448 final Set<WifiSsid> ssids = new ArraySet<>(); 8449 assertThrows(IllegalArgumentException.class, 8450 () -> new WifiSsidPolicy(WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_ALLOWLIST, ssids)); 8451 } 8452 8453 @Test testSetSsidDenylist_noDeviceOwnerOrPoOfOrgOwnedDevice()8454 public void testSetSsidDenylist_noDeviceOwnerOrPoOfOrgOwnedDevice() { 8455 final Set<WifiSsid> ssids = new ArraySet<>( 8456 Arrays.asList(WifiSsid.fromBytes("ssid1".getBytes(StandardCharsets.UTF_8)))); 8457 WifiSsidPolicy policy = new WifiSsidPolicy( 8458 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_DENYLIST, ssids); 8459 assertThrows(SecurityException.class, () -> dpm.setWifiSsidPolicy(policy)); 8460 } 8461 8462 @Test testSetSsidDenylist_asDeviceOwner()8463 public void testSetSsidDenylist_asDeviceOwner() throws Exception { 8464 setDeviceOwner(); 8465 8466 final Set<WifiSsid> ssids = new ArraySet<>( 8467 Arrays.asList(WifiSsid.fromBytes("ssid1".getBytes(StandardCharsets.UTF_8)))); 8468 WifiSsidPolicy policy = new WifiSsidPolicy( 8469 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_DENYLIST, ssids); 8470 dpm.setWifiSsidPolicy(policy); 8471 assertThat(dpm.getWifiSsidPolicy().getSsids()).isEqualTo(ssids); 8472 assertThat(dpm.getWifiSsidPolicy().getPolicyType()).isEqualTo( 8473 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_DENYLIST); 8474 } 8475 8476 @Test testSetSsidDenylist_asPoOfOrgOwnedDevice()8477 public void testSetSsidDenylist_asPoOfOrgOwnedDevice() throws Exception { 8478 final int managedProfileUserId = 15; 8479 final int managedProfileAdminUid = UserHandle.getUid(managedProfileUserId, 19436); 8480 addManagedProfile(admin1, managedProfileAdminUid, admin1); 8481 configureProfileOwnerOfOrgOwnedDevice(admin1, managedProfileUserId); 8482 mContext.binder.callingUid = managedProfileAdminUid; 8483 8484 final Set<WifiSsid> ssids = new ArraySet<>( 8485 Arrays.asList(WifiSsid.fromBytes("ssid1".getBytes(StandardCharsets.UTF_8)), 8486 WifiSsid.fromBytes("ssid2".getBytes(StandardCharsets.UTF_8)), 8487 WifiSsid.fromBytes("ssid3".getBytes(StandardCharsets.UTF_8)))); 8488 WifiSsidPolicy policy = new WifiSsidPolicy( 8489 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_DENYLIST, ssids); 8490 dpm.setWifiSsidPolicy(policy); 8491 assertThat(dpm.getWifiSsidPolicy().getSsids()).isEqualTo(ssids); 8492 assertThat(dpm.getWifiSsidPolicy().getPolicyType()).isEqualTo( 8493 WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_DENYLIST); 8494 } 8495 8496 @Test testSetSsidDenylist_emptyList()8497 public void testSetSsidDenylist_emptyList() throws Exception { 8498 setDeviceOwner(); 8499 8500 final Set<WifiSsid> ssids = new ArraySet<>(); 8501 assertThrows(IllegalArgumentException.class, 8502 () -> new WifiSsidPolicy(WifiSsidPolicy.WIFI_SSID_POLICY_TYPE_DENYLIST, ssids)); 8503 } 8504 8505 @Test testSendLostModeLocationUpdate_noPermission()8506 public void testSendLostModeLocationUpdate_noPermission() { 8507 assertThrows(SecurityException.class, () -> dpm.sendLostModeLocationUpdate( 8508 getServices().executor, /* empty callback */ result -> {})); 8509 } 8510 8511 @Test testSendLostModeLocationUpdate_notOrganizationOwnedDevice()8512 public void testSendLostModeLocationUpdate_notOrganizationOwnedDevice() { 8513 mContext.callerPermissions.add(permission.TRIGGER_LOST_MODE); 8514 assertThrows(IllegalStateException.class, () -> dpm.sendLostModeLocationUpdate( 8515 getServices().executor, /* empty callback */ result -> {})); 8516 } 8517 8518 @Test testSendLostModeLocationUpdate_asDeviceOwner()8519 public void testSendLostModeLocationUpdate_asDeviceOwner() throws Exception { 8520 mContext.callerPermissions.add(permission.TRIGGER_LOST_MODE); 8521 setDeviceOwner(); 8522 when(getServices().locationManager.isProviderEnabled(FUSED_PROVIDER)).thenReturn(true); 8523 8524 dpm.sendLostModeLocationUpdate(getServices().executor, /* empty callback */ result -> {}); 8525 8526 verify(getServices().locationManager, times(1)).getCurrentLocation( 8527 eq(FUSED_PROVIDER), any(), eq(getServices().executor), any()); 8528 } 8529 8530 @Test testSendLostModeLocationUpdate_asProfileOwnerOfOrgOwnedDevice()8531 public void testSendLostModeLocationUpdate_asProfileOwnerOfOrgOwnedDevice() throws Exception { 8532 final int MANAGED_PROFILE_ADMIN_UID = 8533 UserHandle.getUid(CALLER_USER_HANDLE, DpmMockContext.SYSTEM_UID); 8534 mContext.binder.callingUid = MANAGED_PROFILE_ADMIN_UID; 8535 mContext.callerPermissions.add(permission.TRIGGER_LOST_MODE); 8536 addManagedProfile(admin1, MANAGED_PROFILE_ADMIN_UID, admin1); 8537 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 8538 when(getServices().locationManager.isProviderEnabled(FUSED_PROVIDER)).thenReturn(true); 8539 8540 dpm.sendLostModeLocationUpdate(getServices().executor, /* empty callback */ result -> {}); 8541 8542 verify(getServices().locationManager, times(1)).getCurrentLocation( 8543 eq(FUSED_PROVIDER), any(), eq(getServices().executor), any()); 8544 } 8545 8546 @Test testSendLostModeLocationUpdate_noProviderIsEnabled()8547 public void testSendLostModeLocationUpdate_noProviderIsEnabled() throws Exception { 8548 mContext.callerPermissions.add(permission.TRIGGER_LOST_MODE); 8549 setDeviceOwner(); 8550 when(getServices().locationManager.isProviderEnabled(FUSED_PROVIDER)).thenReturn(false); 8551 when(getServices().locationManager.isProviderEnabled(NETWORK_PROVIDER)).thenReturn(false); 8552 when(getServices().locationManager.isProviderEnabled(GPS_PROVIDER)).thenReturn(false); 8553 8554 dpm.sendLostModeLocationUpdate(getServices().executor, /* empty callback */ result -> {}); 8555 8556 verify(getServices().locationManager, never()).getCurrentLocation( 8557 eq(FUSED_PROVIDER), any(), eq(getServices().executor), any()); 8558 } 8559 8560 /** 8561 * Verifies that bundles with tons of moderately long strings are persisted correctly. 8562 * 8563 * Policy is serialized into binary XML and there is a limit on the max string length: 65535. 8564 * This test ensures that as long as each string in the trust agent configuration is below this 8565 * limit, the policy can be serialized and deserialized correctly, even when the total length 8566 * of the configuration is above that limit. This should be the case because PersistableBundle 8567 * contents are stored as XML subtrees rather than as strings. 8568 */ 8569 @Test testSetTrustAgentConfiguration_largeBundlePersisted()8570 public void testSetTrustAgentConfiguration_largeBundlePersisted() { 8571 setAsProfileOwner(admin1); 8572 8573 ComponentName agent = new ComponentName("some.trust.agent", "some.trust.agent.Agent"); 8574 PersistableBundle configIn = new PersistableBundle(); 8575 String kilobyteString = new String(new char[1024]).replace('\0', 'A'); 8576 for (int i = 0; i < 1024; i++) { 8577 configIn.putString("key-" + i, kilobyteString); 8578 } 8579 8580 runAsCaller(mAdmin1Context, dpms, dpm -> { 8581 dpm.setTrustAgentConfiguration(admin1, agent, configIn); 8582 }); 8583 8584 // Re-read policies to see if they were serialized/deserialized correctly. 8585 initializeDpms(); 8586 8587 List<PersistableBundle> configsOut = new ArrayList<>(); 8588 runAsCaller(mAdmin1Context, dpms, dpm -> { 8589 configsOut.addAll(dpm.getTrustAgentConfiguration(admin1, agent)); 8590 }); 8591 8592 assertThat(configsOut.size()).isEqualTo(1); 8593 PersistableBundle configOut = configsOut.get(0); 8594 assertThat(configOut.size()).isEqualTo(1024); 8595 for (int i = 0; i < 1024; i++) { 8596 assertThat(configOut.getString("key-" + i, null)).isEqualTo(kilobyteString); 8597 } 8598 } 8599 setupVpnAuthorization(String userVpnPackage, int userVpnUid)8600 private void setupVpnAuthorization(String userVpnPackage, int userVpnUid) { 8601 final AppOpsManager.PackageOps vpnOp = new AppOpsManager.PackageOps(userVpnPackage, 8602 userVpnUid, List.of(new AppOpsManager.OpEntry( 8603 OP_ACTIVATE_VPN, MODE_ALLOWED, Collections.emptyMap()))); 8604 when(getServices().appOpsManager.getPackagesForOps(any(int[].class))) 8605 .thenReturn(List.of(vpnOp)); 8606 } 8607 simulateRestrictionAdded(String restriction)8608 private void simulateRestrictionAdded(String restriction) { 8609 RestrictionsListener listener = new RestrictionsListener( 8610 mServiceContext, getServices().userManagerInternal, dpms); 8611 8612 final Bundle newRestrictions = new Bundle(); 8613 newRestrictions.putBoolean(restriction, true); 8614 listener.onUserRestrictionsChanged(UserHandle.USER_SYSTEM, newRestrictions, new Bundle()); 8615 } 8616 setUserUnlocked(int userHandle, boolean unlocked)8617 private void setUserUnlocked(int userHandle, boolean unlocked) { 8618 when(getServices().userManager.isUserUnlocked(eq(userHandle))).thenReturn(unlocked); 8619 when(getServices().userManagerInternal.isUserUnlockingOrUnlocked(eq(userHandle))) 8620 .thenReturn(unlocked); 8621 } 8622 prepareMocksForSetMaximumProfileTimeOff()8623 private void prepareMocksForSetMaximumProfileTimeOff() throws Exception { 8624 addManagedProfile(admin1, DpmMockContext.CALLER_UID, admin1); 8625 configureProfileOwnerOfOrgOwnedDevice(admin1, CALLER_USER_HANDLE); 8626 8627 when(getServices().userManager.isUserUnlocked()).thenReturn(true); 8628 8629 doReturn(Collections.singletonList(new ResolveInfo())) 8630 .when(getServices().packageManager).queryIntentActivitiesAsUser( 8631 any(Intent.class), anyInt(), eq(CALLER_USER_HANDLE)); 8632 8633 dpms.mMockInjector.setSystemCurrentTimeMillis(PROFILE_OFF_START); 8634 // To allow creation of Notification via Notification.Builder 8635 mContext.applicationInfo = mRealTestContext.getApplicationInfo(); 8636 8637 // Make locale available for date formatting: 8638 when(mServiceContext.resources.getConfiguration()) 8639 .thenReturn(mRealTestContext.getResources().getConfiguration()); 8640 8641 clearInvocations(getServices().ipackageManager); 8642 } 8643 hasExtra(String... extras)8644 private static Matcher<Notification> hasExtra(String... extras) { 8645 assertWithMessage("Odd number of extra key-values").that(extras.length % 2).isEqualTo(0); 8646 return new BaseMatcher<Notification>() { 8647 @Override 8648 public boolean matches(Object item) { 8649 final Notification notification = (Notification) item; 8650 for (int i = 0; i < extras.length / 2; i++) { 8651 if (!extras[i * 2 + 1].equals(notification.extras.getString(extras[i * 2]))) { 8652 return false; 8653 } 8654 } 8655 return true; 8656 } 8657 @Override 8658 public void describeTo(Description description) { 8659 description.appendText("Notification{"); 8660 for (int i = 0; i < extras.length / 2; i++) { 8661 if (i > 0) { 8662 description.appendText(","); 8663 } 8664 description.appendText(extras[i * 2] + "=\"" + extras[i * 2 + 1] + "\""); 8665 } 8666 description.appendText("}"); 8667 } 8668 }; 8669 } 8670 8671 // admin1 is the outgoing DPC, adminAnotherPackage is the incoming one. 8672 private void assertDeviceOwnershipRevertedWithFakeTransferMetadata() throws Exception { 8673 writeFakeTransferMetadataFile(UserHandle.USER_SYSTEM, 8674 TransferOwnershipMetadataManager.ADMIN_TYPE_DEVICE_OWNER); 8675 8676 final long ident = mServiceContext.binder.clearCallingIdentity(); 8677 setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); 8678 setUpPackageManagerForFakeAdmin(adminAnotherPackage, 8679 DpmMockContext.CALLER_SYSTEM_USER_UID, admin1); 8680 // To simulate a reboot, we just reinitialize dpms and call systemReady 8681 initializeDpms(); 8682 8683 assertThat(dpm.isDeviceOwnerApp(admin1.getPackageName())).isTrue(); 8684 assertThat(dpm.isDeviceOwnerApp(adminAnotherPackage.getPackageName())).isFalse(); 8685 assertThat(dpm.isAdminActive(adminAnotherPackage)).isFalse(); 8686 assertThat(dpm.isAdminActive(admin1)).isTrue(); 8687 assertThat(dpm.isDeviceOwnerAppOnCallingUser(admin1.getPackageName())).isTrue(); 8688 assertThat(dpm.getDeviceOwnerComponentOnCallingUser()).isEqualTo(admin1); 8689 8690 assertThat(dpm.isDeviceOwnerAppOnAnyUser(admin1.getPackageName())).isTrue(); 8691 assertThat(dpm.getDeviceOwnerComponentOnAnyUser()).isEqualTo(admin1); 8692 assertThat(dpm.getDeviceOwnerUserId()).isEqualTo(UserHandle.USER_SYSTEM); 8693 assertThat(getMockTransferMetadataManager().metadataFileExists()).isFalse(); 8694 8695 mServiceContext.binder.restoreCallingIdentity(ident); 8696 } 8697 8698 // admin1 is the outgoing DPC, adminAnotherPackage is the incoming one. 8699 private void assertProfileOwnershipRevertedWithFakeTransferMetadata() throws Exception { 8700 writeFakeTransferMetadataFile(CALLER_USER_HANDLE, 8701 TransferOwnershipMetadataManager.ADMIN_TYPE_PROFILE_OWNER); 8702 8703 int uid = UserHandle.getUid(CALLER_USER_HANDLE, 8704 DpmMockContext.CALLER_SYSTEM_USER_UID); 8705 setUpPackageManagerForAdmin(admin1, uid); 8706 setUpPackageManagerForFakeAdmin(adminAnotherPackage, uid, admin1); 8707 // To simulate a reboot, we just reinitialize dpms and call systemReady 8708 initializeDpms(); 8709 8710 assertThat(dpm.isProfileOwnerApp(admin1.getPackageName())).isTrue(); 8711 assertThat(dpm.isAdminActive(admin1)).isTrue(); 8712 assertThat(dpm.isProfileOwnerApp(adminAnotherPackage.getPackageName())).isFalse(); 8713 assertThat(dpm.isAdminActive(adminAnotherPackage)).isFalse(); 8714 assertThat(admin1).isEqualTo(dpm.getProfileOwnerAsUser(CALLER_USER_HANDLE)); 8715 assertThat(getMockTransferMetadataManager().metadataFileExists()).isFalse(); 8716 } 8717 8718 private void writeFakeTransferMetadataFile(int callerUserHandle, String adminType) { 8719 TransferOwnershipMetadataManager metadataManager = getMockTransferMetadataManager(); 8720 metadataManager.deleteMetadataFile(); 8721 8722 final TransferOwnershipMetadataManager.Metadata metadata = 8723 new TransferOwnershipMetadataManager.Metadata( 8724 admin1.flattenToString(), adminAnotherPackage.flattenToString(), 8725 callerUserHandle, 8726 adminType); 8727 metadataManager.saveMetadataFile(metadata); 8728 } 8729 8730 private File getDeviceOwnerFile() { 8731 return dpms.mOwners.getDeviceOwnerFile(); 8732 } 8733 8734 private File getProfileOwnerFile() { 8735 return dpms.mOwners.getProfileOwnerFile(CALLER_USER_HANDLE); 8736 } 8737 8738 private File getProfileOwnerPoliciesFile() { 8739 File parentDir = getServices().pathProvider.getUserSystemDirectory(CALLER_USER_HANDLE); 8740 return getPoliciesFile(parentDir); 8741 } 8742 8743 private File getDeviceOwnerPoliciesFile() { 8744 return getPoliciesFile(getServices().systemUserDataDir); 8745 } 8746 8747 private File getPoliciesFile(File parentDir) { 8748 return new File(parentDir, "device_policies.xml"); 8749 } 8750 8751 private void setUserSetupCompleteForUser(boolean isUserSetupComplete, int userhandle) { 8752 when(getServices().settings.settingsSecureGetIntForUser(Settings.Secure.USER_SETUP_COMPLETE, 0, 8753 userhandle)).thenReturn(isUserSetupComplete ? 1 : 0); 8754 dpms.notifyChangeToContentObserver( 8755 Settings.Secure.getUriFor(Settings.Secure.USER_SETUP_COMPLETE), userhandle); 8756 } 8757 8758 private void assertProvisioningAllowed(String action, boolean expected) { 8759 assertWithMessage("isProvisioningAllowed(%s) returning unexpected result", action) 8760 .that(dpm.isProvisioningAllowed(action)).isEqualTo(expected); 8761 } 8762 8763 private void assertProvisioningAllowed(String action, boolean expected, String packageName, 8764 int uid) { 8765 final String previousPackageName = mContext.packageName; 8766 final int previousUid = mMockContext.binder.callingUid; 8767 8768 // Call assertProvisioningAllowed with the packageName / uid passed as arguments. 8769 mContext.packageName = packageName; 8770 mMockContext.binder.callingUid = uid; 8771 assertProvisioningAllowed(action, expected); 8772 8773 // Set the previous package name / calling uid to go back to the initial state. 8774 mContext.packageName = previousPackageName; 8775 mMockContext.binder.callingUid = previousUid; 8776 } 8777 8778 private void assertCheckProvisioningPreCondition(String action, int provisioningCondition) { 8779 assertCheckProvisioningPreCondition(action, admin1.getPackageName(), provisioningCondition); 8780 } 8781 8782 private void assertCheckProvisioningPreCondition( 8783 String action, String packageName, int provisioningCondition) { 8784 assertWithMessage("checkProvisioningPreCondition(%s, %s) returning unexpected result", 8785 action, packageName).that(dpm.checkProvisioningPrecondition(action, packageName)) 8786 .isEqualTo(provisioningCondition); 8787 } 8788 8789 /** 8790 * Setup a managed profile with the specified admin and its uid. 8791 * @param admin ComponentName that's visible to the test code, which doesn't have to exist. 8792 * @param adminUid uid of the admin package. 8793 * @param copyFromAdmin package information for {@code admin} will be built based on this 8794 * component's information. 8795 * @param appTargetSdk admin's target SDK level 8796 */ 8797 private void addManagedProfile( 8798 ComponentName admin, int adminUid, ComponentName copyFromAdmin, int appTargetSdk) 8799 throws Exception { 8800 final int userId = UserHandle.getUserId(adminUid); 8801 getServices().addUser(userId, 0, UserManager.USER_TYPE_PROFILE_MANAGED, 8802 UserHandle.USER_SYSTEM); 8803 mContext.callerPermissions.addAll(OWNER_SETUP_PERMISSIONS); 8804 setUpPackageManagerForFakeAdmin(admin, adminUid, /* enabledSetting= */ null, 8805 appTargetSdk, copyFromAdmin); 8806 dpm.setActiveAdmin(admin, false, userId); 8807 assertThat(dpm.setProfileOwner(admin, userId)).isTrue(); 8808 mContext.callerPermissions.removeAll(OWNER_SETUP_PERMISSIONS); 8809 } 8810 8811 /** 8812 * Same as {@code addManagedProfile} above, except using development API level as the API 8813 * level of the admin. 8814 */ 8815 private void addManagedProfile( 8816 ComponentName admin, int adminUid, ComponentName copyFromAdmin) throws Exception { 8817 addManagedProfile(admin, adminUid, copyFromAdmin, VERSION_CODES.CUR_DEVELOPMENT); 8818 } 8819 8820 /** 8821 * Convert String[] to StringParceledListSlice. 8822 */ 8823 private static StringParceledListSlice asSlice(String[] s) { 8824 return new StringParceledListSlice(Arrays.asList(s)); 8825 } 8826 8827 private void grantManageDeviceAdmins() { 8828 Log.d(TAG, "Granting " + permission.MANAGE_DEVICE_ADMINS); 8829 mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS); 8830 } 8831 8832 private void mockPolicyExemptApps(String... apps) { 8833 Log.d(TAG, "Mocking R.array.policy_exempt_apps to return " + Arrays.toString(apps)); 8834 when(mContext.resources.getStringArray(R.array.policy_exempt_apps)).thenReturn(apps); 8835 } 8836 8837 private void mockVendorPolicyExemptApps(String... apps) { 8838 Log.d(TAG, "Mocking R.array.vendor_policy_exempt_apps to return " + Arrays.toString(apps)); 8839 when(mContext.resources.getStringArray(R.array.vendor_policy_exempt_apps)).thenReturn(apps); 8840 } 8841 8842 private void mockEmptyPolicyExemptApps() { 8843 when(mContext.getResources().getStringArray(R.array.policy_exempt_apps)) 8844 .thenReturn(new String[0]); 8845 when(mContext.getResources().getStringArray(R.array.vendor_policy_exempt_apps)) 8846 .thenReturn(new String[0]); 8847 } 8848 8849 private boolean isDeprecatedPasswordApisSupported() { 8850 return !mIsAutomotive; 8851 } 8852 8853 private void assumeDeprecatedPasswordApisSupported() { 8854 assumeTrue("device doesn't support deprecated password APIs", 8855 isDeprecatedPasswordApisSupported()); 8856 } 8857 8858 private static PasswordMetrics metricsForPassword(String password) { 8859 return PasswordMetrics.computeForCredential(LockscreenCredential.createPassword(password)); 8860 } 8861 8862 private static PasswordMetrics metricsForPin(String pin) { 8863 return PasswordMetrics.computeForCredential(LockscreenCredential.createPin(pin)); 8864 } 8865 } 8866