1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.devicepolicy; 18 19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 22 import java.io.File; 23 import java.util.Arrays; 24 import java.util.Collections; 25 import java.util.List; 26 import java.util.Map; 27 28 /** 29 * Set of tests for Device Owner use cases. 30 */ 31 public class DeviceOwnerTest extends BaseDevicePolicyTest { 32 33 private static final String DEVICE_OWNER_PKG = "com.android.cts.deviceowner"; 34 private static final String DEVICE_OWNER_APK = "CtsDeviceOwnerApp.apk"; 35 36 private static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile"; 37 private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk"; 38 private static final String MANAGED_PROFILE_ADMIN = 39 MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver"; 40 41 private static final String FEATURE_BACKUP = "android.software.backup"; 42 43 private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver"; 44 private static final String INTENT_RECEIVER_APK = "CtsIntentReceiverApp.apk"; 45 46 private static final String WIFI_CONFIG_CREATOR_PKG = 47 "com.android.cts.deviceowner.wificonfigcreator"; 48 private static final String WIFI_CONFIG_CREATOR_APK = "CtsWifiConfigCreator.apk"; 49 50 private static final String ADMIN_RECEIVER_TEST_CLASS = 51 DEVICE_OWNER_PKG + ".BasicAdminReceiver"; 52 private static final String DEVICE_OWNER_COMPONENT = DEVICE_OWNER_PKG + "/" 53 + ADMIN_RECEIVER_TEST_CLASS; 54 55 private static final String TEST_APP_APK = "CtsEmptyTestApp.apk"; 56 private static final String TEST_APP_PKG = "android.packageinstaller.emptytestapp.cts"; 57 private static final String TEST_APP_LOCATION = "/data/local/tmp/cts/packageinstaller/"; 58 59 private static final String ARG_SECURITY_LOGGING_BATCH_NUMBER = "batchNumber"; 60 private static final int SECURITY_EVENTS_BATCH_SIZE = 100; 61 62 private static final String ARG_NETWORK_LOGGING_BATCH_COUNT = "batchCount"; 63 64 private final List<String> NO_SETUP_WIZARD_PROVISIONING_MODE = 65 Arrays.asList("DISABLED", "EMULATOR"); 66 67 /** CreateAndManageUser is available and an additional user can be created. */ 68 private boolean mHasCreateAndManageUserFeature; 69 70 /** 71 * Whether Setup Wizard is disabled. 72 */ 73 private boolean mSetupWizardDisabled; 74 75 @Override 76 protected void setUp() throws Exception { 77 super.setUp(); 78 if (mHasFeature) { 79 installAppAsUser(DEVICE_OWNER_APK, mPrimaryUserId); 80 if (!setDeviceOwner(DEVICE_OWNER_COMPONENT, mPrimaryUserId, 81 /*expectFailure*/ false)) { 82 removeAdmin(DEVICE_OWNER_COMPONENT, mPrimaryUserId); 83 getDevice().uninstallPackage(DEVICE_OWNER_PKG); 84 fail("Failed to set device owner"); 85 } 86 } 87 mHasCreateAndManageUserFeature = mHasFeature && canCreateAdditionalUsers(1) 88 && hasDeviceFeature("android.software.managed_users"); 89 mSetupWizardDisabled = NO_SETUP_WIZARD_PROVISIONING_MODE.contains( 90 getDevice().getProperty("ro.setupwizard.mode")); 91 } 92 93 @Override 94 protected void tearDown() throws Exception { 95 if (mHasFeature) { 96 assertTrue("Failed to remove device owner.", 97 removeAdmin(DEVICE_OWNER_COMPONENT, mPrimaryUserId)); 98 getDevice().uninstallPackage(DEVICE_OWNER_PKG); 99 switchUser(USER_SYSTEM); 100 removeTestUsers(); 101 } 102 103 super.tearDown(); 104 } 105 106 public void testDeviceOwnerSetup() throws Exception { 107 executeDeviceOwnerTest("DeviceOwnerSetupTest"); 108 } 109 110 public void testLockScreenInfo() throws Exception { 111 executeDeviceOwnerTest("LockScreenInfoTest"); 112 } 113 114 public void testWifi() throws Exception { 115 if (!hasDeviceFeature("android.hardware.wifi")) { 116 return; 117 } 118 executeDeviceOwnerTest("WifiTest"); 119 } 120 121 public void testRemoteBugreportWithTwoUsers() throws Exception { 122 if (!mHasFeature || !canCreateAdditionalUsers(1)) { 123 return; 124 } 125 final int userId = createUser(); 126 try { 127 executeDeviceTestMethod(".RemoteBugreportTest", 128 "testRequestBugreportThrowsSecurityException"); 129 } finally { 130 removeUser(userId); 131 } 132 } 133 134 public void testCreateAndManageUser_LowStorage() throws Exception { 135 if (!mHasCreateAndManageUserFeature) { 136 return; 137 } 138 139 try { 140 // Force low storage 141 getDevice().setSetting("global", "sys_storage_threshold_percentage", "100"); 142 getDevice().setSetting("global", "sys_storage_threshold_max_bytes", 143 String.valueOf(Long.MAX_VALUE)); 144 145 // The next createAndManageUser should return USER_OPERATION_ERROR_LOW_STORAGE. 146 executeDeviceTestMethod(".CreateAndManageUserTest", 147 "testCreateAndManageUser_LowStorage"); 148 } finally { 149 getDevice().executeShellCommand( 150 "settings delete global sys_storage_threshold_percentage"); 151 getDevice().executeShellCommand( 152 "settings delete global sys_storage_threshold_max_bytes"); 153 } 154 } 155 156 public void testCreateAndManageUser_MaxUsers() throws Exception { 157 if (!mHasCreateAndManageUserFeature) { 158 return; 159 } 160 161 int maxUsers = getDevice().getMaxNumberOfUsersSupported(); 162 // Primary user is already there, so we can create up to maxUsers -1. 163 for (int i = 0; i < maxUsers - 1; i++) { 164 executeDeviceTestMethod(".CreateAndManageUserTest", 165 "testCreateAndManageUser"); 166 } 167 // The next createAndManageUser should return USER_OPERATION_ERROR_MAX_USERS. 168 executeDeviceTestMethod(".CreateAndManageUserTest", 169 "testCreateAndManageUser_MaxUsers"); 170 } 171 172 /** 173 * Test creating an user using the DevicePolicyManager's createAndManageUser. 174 * {@link android.app.admin.DevicePolicyManager#getSecondaryUsers} is tested. 175 */ 176 public void testCreateAndManageUser_GetSecondaryUsers() throws Exception { 177 if (!mHasCreateAndManageUserFeature) { 178 return; 179 } 180 181 executeDeviceTestMethod(".CreateAndManageUserTest", 182 "testCreateAndManageUser_GetSecondaryUsers"); 183 } 184 185 /** 186 * Test creating an user using the DevicePolicyManager's createAndManageUser method and switch 187 * to the user. 188 * {@link android.app.admin.DevicePolicyManager#switchUser} is tested. 189 */ 190 public void testCreateAndManageUser_SwitchUser() throws Exception { 191 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 192 return; 193 } 194 195 executeDeviceTestMethod(".CreateAndManageUserTest", 196 "testCreateAndManageUser_SwitchUser"); 197 } 198 199 /** 200 * Test creating an user using the DevicePolicyManager's createAndManageUser method and switch 201 * to the user to test stop user while target user is in foreground. 202 * {@link android.app.admin.DevicePolicyManager#stopUser} is tested. 203 */ 204 public void testCreateAndManageUser_CannotStopCurrentUser() throws Exception { 205 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 206 return; 207 } 208 209 executeDeviceTestMethod(".CreateAndManageUserTest", 210 "testCreateAndManageUser_CannotStopCurrentUser"); 211 } 212 213 /** 214 * Test creating an user using the DevicePolicyManager's createAndManageUser method and start 215 * the user in background to test APIs on that user. 216 * {@link android.app.admin.DevicePolicyManager#startUserInBackground} is tested. 217 */ 218 public void testCreateAndManageUser_StartInBackground() throws Exception { 219 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 220 return; 221 } 222 223 executeDeviceTestMethod(".CreateAndManageUserTest", 224 "testCreateAndManageUser_StartInBackground"); 225 } 226 227 /** 228 * Test creating an user using the DevicePolicyManager's createAndManageUser method and start 229 * the user in background to test APIs on that user. 230 * {@link android.app.admin.DevicePolicyManager#startUserInBackground} is tested. 231 */ 232 public void testCreateAndManageUser_StartInBackground_MaxRunningUsers() throws Exception { 233 if (!mHasCreateAndManageUserFeature) { 234 return; 235 } 236 237 int maxRunningUsers = getDevice().getMaxNumberOfRunningUsersSupported(); 238 // Primary user is already running, so we can start up to maxRunningUsers -1. 239 for (int i = 0; i < maxRunningUsers - 1; i++) { 240 executeDeviceTestMethod(".CreateAndManageUserTest", 241 "testCreateAndManageUser_StartInBackground"); 242 } 243 // The next startUserInBackground should return USER_OPERATION_ERROR_MAX_RUNNING_USERS. 244 executeDeviceTestMethod(".CreateAndManageUserTest", 245 "testCreateAndManageUser_StartInBackground_MaxRunningUsers"); 246 } 247 248 /** 249 * Test creating an user using the DevicePolicyManager's createAndManageUser method and start 250 * the user in background to test APIs on that user. 251 * {@link android.app.admin.DevicePolicyManager#stopUser} is tested. 252 */ 253 public void testCreateAndManageUser_StopUser() throws Exception { 254 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 255 return; 256 } 257 258 executeDeviceTestMethod(".CreateAndManageUserTest", 259 "testCreateAndManageUser_StopUser"); 260 assertNewUserStopped(); 261 } 262 263 /** 264 * Test creating an ephemeral user using the DevicePolicyManager's createAndManageUser method 265 * and start the user in background, user is then stopped. The user should be removed 266 * automatically even when DISALLOW_REMOVE_USER is set. 267 */ 268 public void testCreateAndManageUser_StopEphemeralUser_DisallowRemoveUser() throws Exception { 269 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 270 return; 271 } 272 273 executeDeviceTestMethod(".CreateAndManageUserTest", 274 "testCreateAndManageUser_StopEphemeralUser_DisallowRemoveUser"); 275 assertEquals(0, getUsersCreatedByTests().size()); 276 } 277 278 /** 279 * Test creating an user using the DevicePolicyManager's createAndManageUser method, affiliate 280 * the user and start the user in background to test APIs on that user. 281 * {@link android.app.admin.DevicePolicyManager#logoutUser} is tested. 282 */ 283 public void testCreateAndManageUser_LogoutUser() throws Exception { 284 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 285 return; 286 } 287 288 executeDeviceTestMethod(".CreateAndManageUserTest", 289 "testCreateAndManageUser_LogoutUser"); 290 assertNewUserStopped(); 291 } 292 293 /** 294 * Test creating an user using the DevicePolicyManager's createAndManageUser method, affiliate 295 * the user and start the user in background to test APIs on that user. 296 * {@link android.app.admin.DevicePolicyManager#isAffiliatedUser} is tested. 297 */ 298 public void testCreateAndManageUser_Affiliated() throws Exception { 299 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 300 return; 301 } 302 303 executeDeviceTestMethod(".CreateAndManageUserTest", 304 "testCreateAndManageUser_Affiliated"); 305 } 306 307 /** 308 * Test creating an ephemeral user using the DevicePolicyManager's createAndManageUser method, 309 * affiliate the user and start the user in background to test APIs on that user. 310 * {@link android.app.admin.DevicePolicyManager#isEphemeralUser} is tested. 311 */ 312 public void testCreateAndManageUser_Ephemeral() throws Exception { 313 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 314 return; 315 } 316 317 executeDeviceTestMethod(".CreateAndManageUserTest", 318 "testCreateAndManageUser_Ephemeral"); 319 320 List<Integer> newUsers = getUsersCreatedByTests(); 321 assertEquals(1, newUsers.size()); 322 int newUserId = newUsers.get(0); 323 324 // Get the flags of the new user and check the user is ephemeral. 325 int flags = getUserFlags(newUserId); 326 assertEquals("Ephemeral flag must be set", FLAG_EPHEMERAL, flags & FLAG_EPHEMERAL); 327 } 328 329 /** 330 * Test creating an user using the DevicePolicyManager's createAndManageUser method, affiliate 331 * the user and start the user in background to test APIs on that user. 332 * {@link android.app.admin.DevicePolicyManager#LEAVE_ALL_SYSTEM_APPS_ENABLED} is tested. 333 */ 334 public void testCreateAndManageUser_LeaveAllSystemApps() throws Exception { 335 if (!mHasCreateAndManageUserFeature || !canStartAdditionalUsers(1)) { 336 return; 337 } 338 339 executeDeviceTestMethod(".CreateAndManageUserTest", 340 "testCreateAndManageUser_LeaveAllSystemApps"); 341 } 342 343 public void testCreateAndManageUser_SkipSetupWizard() throws Exception { 344 if (mSetupWizardDisabled || !mHasCreateAndManageUserFeature) { 345 return; 346 } 347 executeDeviceTestMethod(".CreateAndManageUserTest", 348 "testCreateAndManageUser_SkipSetupWizard"); 349 } 350 351 public void testCreateAndManageUser_DontSkipSetupWizard() throws Exception { 352 if (mSetupWizardDisabled || !mHasCreateAndManageUserFeature) { 353 return; 354 } 355 executeDeviceTestMethod(".CreateAndManageUserTest", 356 "testCreateAndManageUser_DontSkipSetupWizard"); 357 } 358 359 public void testCreateAndManageUser_AddRestrictionSet() throws Exception { 360 if (mHasCreateAndManageUserFeature) { 361 executeDeviceTestMethod(".CreateAndManageUserTest", 362 "testCreateAndManageUser_AddRestrictionSet"); 363 } 364 } 365 366 public void testCreateAndManageUser_RemoveRestrictionSet() throws Exception { 367 if (mHasCreateAndManageUserFeature) { 368 executeDeviceTestMethod(".CreateAndManageUserTest", 369 "testCreateAndManageUser_RemoveRestrictionSet"); 370 } 371 } 372 373 public void testUserAddedOrRemovedBroadcasts() throws Exception { 374 if (mHasCreateAndManageUserFeature) { 375 executeDeviceTestMethod(".CreateAndManageUserTest", 376 "testUserAddedOrRemovedBroadcasts"); 377 } 378 } 379 380 public void testUserSession() throws Exception { 381 if (!mHasFeature) { 382 return; 383 } 384 executeDeviceOwnerTest("UserSessionTest"); 385 } 386 387 public void testSecurityLoggingWithTwoUsers() throws Exception { 388 if (!mHasFeature || !canCreateAdditionalUsers(1)) { 389 return; 390 } 391 392 final int userId = createUser(); 393 try { 394 // The feature can be enabled, but in a "paused" state. Attempting to retrieve logs 395 // should throw security exception. 396 executeDeviceTestMethod(".SecurityLoggingTest", "testEnablingSecurityLogging"); 397 executeDeviceTestMethod(".SecurityLoggingTest", 398 "testRetrievingSecurityLogsThrowsSecurityException"); 399 executeDeviceTestMethod(".SecurityLoggingTest", 400 "testRetrievingPreviousSecurityLogsThrowsSecurityException"); 401 } finally { 402 removeUser(userId); 403 executeDeviceTestMethod(".SecurityLoggingTest", "testDisablingSecurityLogging"); 404 } 405 } 406 407 public void testSecurityLoggingWithSingleUser() throws Exception { 408 if (!mHasFeature) { 409 return; 410 } 411 412 // Turn logging on. 413 executeDeviceTestMethod(".SecurityLoggingTest", "testEnablingSecurityLogging"); 414 // Reboot to ensure ro.device_owner is set to true in logd and logging is on. 415 rebootAndWaitUntilReady(); 416 417 // Generate various types of events on device side and check that they are logged. 418 executeDeviceTestMethod(".SecurityLoggingTest", "testGenerateLogs"); 419 getDevice().executeShellCommand("dpm force-security-logs"); 420 executeDeviceTestMethod(".SecurityLoggingTest", "testVerifyGeneratedLogs"); 421 422 // Reboot the device, so the security event ids are reset. 423 rebootAndWaitUntilReady(); 424 425 // Verify event ids are consistent across a consecutive batch. 426 for (int batchNumber = 0; batchNumber < 3; batchNumber++) { 427 generateDummySecurityLogs(); 428 getDevice().executeShellCommand("dpm force-security-logs"); 429 executeDeviceTestMethod(".SecurityLoggingTest", "testVerifyLogIds", 430 Collections.singletonMap(ARG_SECURITY_LOGGING_BATCH_NUMBER, 431 Integer.toString(batchNumber))); 432 } 433 434 // Immediately attempting to fetch events again should fail. 435 executeDeviceTestMethod(".SecurityLoggingTest", 436 "testSecurityLoggingRetrievalRateLimited"); 437 // Turn logging off. 438 executeDeviceTestMethod(".SecurityLoggingTest", "testDisablingSecurityLogging"); 439 } 440 441 private void generateDummySecurityLogs() throws DeviceNotAvailableException { 442 // Trigger security events of type TAG_ADB_SHELL_CMD. 443 for (int i = 0; i < SECURITY_EVENTS_BATCH_SIZE; i++) { 444 getDevice().executeShellCommand("echo just_testing_" + i); 445 } 446 } 447 448 public void testNetworkLoggingWithTwoUsers() throws Exception { 449 if (!mHasFeature || !canCreateAdditionalUsers(1)) { 450 return; 451 } 452 453 final int userId = createUser(); 454 try { 455 // The feature can be enabled, but in a "paused" state. Attempting to retrieve logs 456 // should throw security exception. 457 executeDeviceTestMethod(".NetworkLoggingTest", 458 "testRetrievingNetworkLogsThrowsSecurityException"); 459 } finally { 460 removeUser(userId); 461 } 462 } 463 464 public void testNetworkLoggingWithSingleUser() throws Exception { 465 if (!mHasFeature) { 466 return; 467 } 468 executeDeviceTestMethod(".NetworkLoggingTest", "testProvidingWrongBatchTokenReturnsNull"); 469 executeDeviceTestMethod(".NetworkLoggingTest", "testNetworkLoggingAndRetrieval", 470 Collections.singletonMap(ARG_NETWORK_LOGGING_BATCH_COUNT, Integer.toString(1))); 471 } 472 473 public void testNetworkLogging_multipleBatches() throws Exception { 474 if (!mHasFeature) { 475 return; 476 } 477 executeDeviceTestMethod(".NetworkLoggingTest", "testNetworkLoggingAndRetrieval", 478 Collections.singletonMap(ARG_NETWORK_LOGGING_BATCH_COUNT, Integer.toString(2))); 479 } 480 481 public void testNetworkLogging_rebootResetsId() throws Exception { 482 if (!mHasFeature) { 483 return; 484 } 485 // First batch: retrieve and verify the events. 486 executeDeviceTestMethod(".NetworkLoggingTest", "testNetworkLoggingAndRetrieval", 487 Collections.singletonMap(ARG_NETWORK_LOGGING_BATCH_COUNT, Integer.toString(1))); 488 // Reboot the device, so the security event IDs are re-set. 489 rebootAndWaitUntilReady(); 490 // First batch after reboot: retrieve and verify the events. 491 executeDeviceTestMethod(".NetworkLoggingTest", "testNetworkLoggingAndRetrieval", 492 Collections.singletonMap(ARG_NETWORK_LOGGING_BATCH_COUNT, Integer.toString(1))); 493 } 494 495 496 public void testSetAffiliationId_IllegalArgumentException() throws Exception { 497 if (!mHasFeature) { 498 return; 499 } 500 executeDeviceTestMethod(".AffiliationTest", "testSetAffiliationId_null"); 501 executeDeviceTestMethod(".AffiliationTest", "testSetAffiliationId_containsEmptyString"); 502 } 503 504 public void testLockTask_deviceOwnerUser() throws Exception { 505 if (!mHasFeature) { 506 return; 507 } 508 try { 509 installAppAsUser(INTENT_RECEIVER_APK, mPrimaryUserId); 510 executeDeviceOwnerTest("LockTaskTest"); 511 } catch (AssertionError ex) { 512 // STOPSHIP(b/32771855), remove this once we fixed the bug. 513 executeShellCommand("dumpsys activity activities"); 514 executeShellCommand("dumpsys window -a"); 515 executeShellCommand("dumpsys activity service com.android.systemui"); 516 throw ex; 517 } finally { 518 getDevice().uninstallPackage(INTENT_RECEIVER_PKG); 519 } 520 } 521 522 public void testLockTaskAfterReboot_deviceOwnerUser() throws Exception { 523 if (!mHasFeature) { 524 return; 525 } 526 527 try { 528 // Just start kiosk mode 529 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest", "startLockTask", 530 mPrimaryUserId); 531 532 // Reboot while in kiosk mode and then unlock the device 533 getDevice().reboot(); 534 535 // Check that kiosk mode is working and can't be interrupted 536 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest", 537 "testLockTaskIsActiveAndCantBeInterrupted", mPrimaryUserId); 538 539 // Try to open settings via adb 540 executeShellCommand("am start -a android.settings.SETTINGS"); 541 542 // Check again 543 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest", 544 "testLockTaskIsActiveAndCantBeInterrupted", mPrimaryUserId); 545 } finally { 546 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest", 547 "clearDefaultHomeIntentReceiver", mPrimaryUserId); 548 } 549 } 550 551 public void testLockTask_unaffiliatedUser() throws Exception { 552 if (!mHasFeature || !canCreateAdditionalUsers(1)) { 553 return; 554 } 555 556 final int userId = createUser(); 557 installAppAsUser(DEVICE_OWNER_APK, userId); 558 setProfileOwnerOrFail(DEVICE_OWNER_COMPONENT, userId); 559 560 runDeviceTestsAsUser( 561 DEVICE_OWNER_PKG, 562 ".AffiliationTest", 563 "testLockTaskMethodsThrowExceptionIfUnaffiliated", 564 userId); 565 566 runDeviceTestsAsUser( 567 DEVICE_OWNER_PKG, ".AffiliationTest", "testSetAffiliationId1", mPrimaryUserId); 568 runDeviceTestsAsUser( 569 DEVICE_OWNER_PKG, ".AffiliationTest", "testSetAffiliationId1", userId); 570 runDeviceTestsAsUser( 571 DEVICE_OWNER_PKG, 572 ".AffiliationTest", 573 "testSetLockTaskPackagesClearedIfUserBecomesUnaffiliated", 574 userId); 575 } 576 577 public void testLockTask_affiliatedSecondaryUser() throws Exception { 578 if (!mHasFeature || !canCreateAdditionalUsers(1)) { 579 return; 580 } 581 final int userId = createAffiliatedSecondaryUser(); 582 executeAffiliatedProfileOwnerTest("LockTaskTest", userId); 583 } 584 585 public void testSystemUpdatePolicy() throws Exception { 586 if (!mHasFeature) { 587 return; 588 } 589 executeDeviceOwnerTest("SystemUpdatePolicyTest"); 590 } 591 592 public void testWifiConfigLockdown() throws Exception { 593 final boolean hasWifi = hasDeviceFeature("android.hardware.wifi"); 594 if (hasWifi && mHasFeature) { 595 try { 596 installAppAsUser(WIFI_CONFIG_CREATOR_APK, mPrimaryUserId); 597 executeDeviceOwnerTest("WifiConfigLockdownTest"); 598 } finally { 599 getDevice().uninstallPackage(WIFI_CONFIG_CREATOR_PKG); 600 } 601 } 602 } 603 604 /** 605 * Execute WifiSetHttpProxyTest as device owner. 606 */ 607 public void testWifiSetHttpProxyTest() throws Exception { 608 final boolean hasWifi = hasDeviceFeature("android.hardware.wifi"); 609 if (hasWifi && mHasFeature) { 610 executeDeviceOwnerTest("WifiSetHttpProxyTest"); 611 } 612 } 613 614 public void testCannotSetDeviceOwnerAgain() throws Exception { 615 if (!mHasFeature) { 616 return; 617 } 618 // verify that we can't set the same admin receiver as device owner again 619 assertFalse(setDeviceOwner( 620 DEVICE_OWNER_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mPrimaryUserId, 621 /*expectFailure*/ true)); 622 623 // verify that we can't set a different admin receiver as device owner 624 try { 625 installAppAsUser(MANAGED_PROFILE_APK, mPrimaryUserId); 626 assertFalse(setDeviceOwner( 627 MANAGED_PROFILE_PKG + "/" + MANAGED_PROFILE_ADMIN, mPrimaryUserId, 628 /*expectFailure*/ true)); 629 } finally { 630 // Remove the device owner in case the test fails. 631 removeAdmin(MANAGED_PROFILE_PKG + "/" + MANAGED_PROFILE_ADMIN, mPrimaryUserId); 632 getDevice().uninstallPackage(MANAGED_PROFILE_PKG); 633 } 634 } 635 636 // Execute HardwarePropertiesManagerTest as a device owner. 637 public void testHardwarePropertiesManagerAsDeviceOwner() throws Exception { 638 if (!mHasFeature) { 639 return; 640 } 641 642 executeDeviceTestMethod(".HardwarePropertiesManagerTest", "testHardwarePropertiesManager"); 643 } 644 645 // Execute VrTemperatureTest as a device owner. 646 public void testVrTemperaturesAsDeviceOwner() throws Exception { 647 if (!mHasFeature) { 648 return; 649 } 650 651 executeDeviceTestMethod(".VrTemperatureTest", "testVrTemperatures"); 652 } 653 654 public void testIsManagedDeviceProvisioningAllowed() throws Exception { 655 if (!mHasFeature) { 656 return; 657 } 658 // This case runs when DO is provisioned 659 // mHasFeature == true and provisioned, can't provision DO again. 660 executeDeviceTestMethod(".PreDeviceOwnerTest", "testIsProvisioningAllowedFalse"); 661 } 662 663 /** 664 * Can provision Managed Profile when DO is set by default if they are the same admin. 665 */ 666 public void testIsManagedProfileProvisioningAllowed_deviceOwnerIsSet() throws Exception { 667 if (!mHasFeature) { 668 return; 669 } 670 if (!hasDeviceFeature("android.software.managed_users")) { 671 return; 672 } 673 executeDeviceTestMethod(".PreDeviceOwnerTest", 674 "testIsProvisioningAllowedTrueForManagedProfileAction"); 675 } 676 677 public void testAdminActionBookkeeping() throws Exception { 678 if (!mHasFeature) { 679 return; 680 } 681 682 executeDeviceOwnerTest("AdminActionBookkeepingTest"); 683 } 684 685 public void testBluetoothRestriction() throws Exception { 686 if (!mHasFeature) { 687 return; 688 } 689 executeDeviceOwnerTest("BluetoothRestrictionTest"); 690 } 691 692 public void testSetTime() throws Exception { 693 if (!mHasFeature) { 694 return; 695 } 696 executeDeviceOwnerTest("SetTimeTest"); 697 } 698 699 public void testDeviceOwnerProvisioning() throws Exception { 700 if (!mHasFeature) { 701 return; 702 } 703 executeDeviceOwnerTest("DeviceOwnerProvisioningTest"); 704 } 705 706 public void testDisallowFactoryReset() throws Exception { 707 if (!mHasFeature) { 708 return; 709 } 710 int adminVersion = 24; 711 changeUserRestrictionOrFail("no_factory_reset", true, mPrimaryUserId, 712 DEVICE_OWNER_PKG); 713 try { 714 installAppAsUser(DeviceAdminHelper.getDeviceAdminApkFileName(adminVersion), 715 mPrimaryUserId); 716 setDeviceAdmin(DeviceAdminHelper.getAdminReceiverComponent(adminVersion), 717 mPrimaryUserId); 718 runDeviceTestsAsUser( 719 DeviceAdminHelper.getDeviceAdminApkPackage(adminVersion), 720 DeviceAdminHelper.getDeviceAdminJavaPackage() + ".WipeDataTest", 721 "testWipeDataThrowsSecurityException", mPrimaryUserId); 722 } finally { 723 removeAdmin(DeviceAdminHelper.getAdminReceiverComponent(adminVersion), mPrimaryUserId); 724 getDevice().uninstallPackage(DeviceAdminHelper.getDeviceAdminApkPackage(adminVersion)); 725 } 726 } 727 728 public void testBackupServiceEnabling() throws Exception { 729 final boolean hasBackupService = getDevice().hasFeature(FEATURE_BACKUP); 730 // The backup service cannot be enabled if the backup feature is not supported. 731 if (!mHasFeature || !hasBackupService) { 732 return; 733 } 734 executeDeviceTestMethod(".BackupServicePoliciesTest", 735 "testEnablingAndDisablingBackupService"); 736 } 737 738 public void testPackageInstallCache() throws Exception { 739 if (!mHasFeature) { 740 return; 741 } 742 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild); 743 final File apk = buildHelper.getTestFile(TEST_APP_APK); 744 try { 745 getDevice().uninstallPackage(TEST_APP_PKG); 746 assertTrue(getDevice().pushFile(apk, TEST_APP_LOCATION + apk.getName())); 747 748 // Install the package in primary user 749 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 750 "testPackageInstall", mPrimaryUserId); 751 752 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 753 "testKeepPackageCache", mPrimaryUserId); 754 755 // Remove the package in primary user 756 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 757 "testPackageUninstall", mPrimaryUserId); 758 759 // Should be able to enable the cached package in primary user 760 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 761 "testInstallExistingPackage", mPrimaryUserId); 762 } finally { 763 String command = "rm " + TEST_APP_LOCATION + apk.getName(); 764 getDevice().executeShellCommand(command); 765 getDevice().uninstallPackage(TEST_APP_PKG); 766 } 767 } 768 769 public void testPackageInstallCache_multiUser() throws Exception { 770 if (!mHasFeature || !canCreateAdditionalUsers(1)) { 771 return; 772 } 773 final int userId = createAffiliatedSecondaryUser(); 774 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild); 775 final File apk = buildHelper.getTestFile(TEST_APP_APK); 776 try { 777 getDevice().uninstallPackage(TEST_APP_PKG); 778 assertTrue(getDevice().pushFile(apk, TEST_APP_LOCATION + apk.getName())); 779 780 // Install the package in primary user 781 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 782 "testPackageInstall", mPrimaryUserId); 783 784 // Should be able to enable the package in secondary user 785 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 786 "testInstallExistingPackage", userId); 787 788 // Remove the package in both user 789 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 790 "testPackageUninstall", mPrimaryUserId); 791 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 792 "testPackageUninstall", userId); 793 794 // Install the package in secondary user 795 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 796 "testPackageInstall", userId); 797 798 // Should be able to enable the package in primary user 799 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 800 "testInstallExistingPackage", mPrimaryUserId); 801 802 // Keep the package in cache 803 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 804 "testKeepPackageCache", mPrimaryUserId); 805 806 // Remove the package in both user 807 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 808 "testPackageUninstall", mPrimaryUserId); 809 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 810 "testPackageUninstall", userId); 811 812 // Should be able to enable the cached package in both users 813 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 814 "testInstallExistingPackage", userId); 815 runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".PackageInstallTest", 816 "testInstallExistingPackage", mPrimaryUserId); 817 } finally { 818 String command = "rm " + TEST_APP_LOCATION + apk.getName(); 819 getDevice().executeShellCommand(command); 820 getDevice().uninstallPackage(TEST_APP_PKG); 821 } 822 } 823 824 public void testAirplaneModeRestriction() throws Exception { 825 if (!mHasFeature) { 826 return; 827 } 828 executeDeviceOwnerTest("AirplaneModeRestrictionTest"); 829 } 830 831 public void testOverrideApn() throws Exception { 832 if (!mHasFeature) { 833 return; 834 } 835 executeDeviceOwnerTest("OverrideApnTest"); 836 } 837 838 private void executeDeviceOwnerTest(String testClassName) throws Exception { 839 if (!mHasFeature) { 840 return; 841 } 842 String testClass = DEVICE_OWNER_PKG + "." + testClassName; 843 runDeviceTestsAsUser(DEVICE_OWNER_PKG, testClass, mPrimaryUserId); 844 } 845 846 private void executeAffiliatedProfileOwnerTest(String testClassName, int userId) 847 throws Exception { 848 if (!mHasFeature) { 849 return; 850 } 851 String testClass = DEVICE_OWNER_PKG + "." + testClassName; 852 runDeviceTestsAsUser(DEVICE_OWNER_PKG, testClass, userId); 853 } 854 855 private void executeDeviceTestMethod(String className, String testName) throws Exception { 856 if (!mHasFeature) { 857 return; 858 } 859 runDeviceTestsAsUser(DEVICE_OWNER_PKG, className, testName, 860 /* deviceOwnerUserId */ mPrimaryUserId); 861 } 862 863 private int createAffiliatedSecondaryUser() throws Exception { 864 final int userId = createUser(); 865 installAppAsUser(INTENT_RECEIVER_APK, userId); 866 installAppAsUser(DEVICE_OWNER_APK, userId); 867 setProfileOwnerOrFail(DEVICE_OWNER_COMPONENT, userId); 868 869 switchUser(userId); 870 waitForBroadcastIdle(); 871 wakeupAndDismissKeyguard(); 872 873 // Setting the same affiliation ids on both users and running the lock task tests. 874 runDeviceTestsAsUser( 875 DEVICE_OWNER_PKG, ".AffiliationTest", "testSetAffiliationId1", mPrimaryUserId); 876 runDeviceTestsAsUser( 877 DEVICE_OWNER_PKG, ".AffiliationTest", "testSetAffiliationId1", userId); 878 return userId; 879 } 880 881 private void executeDeviceTestMethod(String className, String testName, 882 Map<String, String> params) throws Exception { 883 if (!mHasFeature) { 884 return; 885 } 886 runDeviceTestsAsUser(DEVICE_OWNER_PKG, className, testName, 887 /* deviceOwnerUserId */ mPrimaryUserId, params); 888 } 889 890 private void assertNewUserStopped() throws Exception { 891 List<Integer> newUsers = getUsersCreatedByTests(); 892 assertEquals(1, newUsers.size()); 893 int newUserId = newUsers.get(0); 894 895 assertFalse(getDevice().isUserRunning(newUserId)); 896 } 897 } 898