1 /* 2 * Copyright (C) 2021 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.bedstead.remotedpc; 18 19 import static android.os.Build.VERSION_CODES.Q; 20 import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.testng.Assert.assertThrows; 25 26 import android.content.ComponentName; 27 import android.os.UserHandle; 28 29 import com.android.bedstead.harrier.BedsteadJUnit4; 30 import com.android.bedstead.harrier.DeviceState; 31 import com.android.bedstead.harrier.annotations.AfterClass; 32 import com.android.bedstead.harrier.annotations.BeforeClass; 33 import com.android.bedstead.harrier.annotations.EnsureHasNoSecondaryUser; 34 import com.android.bedstead.enterprise.annotations.EnsureHasNoWorkProfile; 35 import com.android.bedstead.harrier.annotations.RequireRunOnInitialUser; 36 import com.android.bedstead.harrier.annotations.RequireRunOnSystemUser; 37 import com.android.bedstead.harrier.annotations.RequireSdkVersion; 38 import com.android.bedstead.enterprise.annotations.EnsureHasDeviceOwner; 39 import com.android.bedstead.enterprise.annotations.EnsureHasNoDeviceOwner; 40 import com.android.bedstead.enterprise.annotations.EnsureHasNoProfileOwner; 41 import com.android.bedstead.enterprise.annotations.EnsureHasProfileOwner; 42 import com.android.bedstead.nene.TestApis; 43 import com.android.bedstead.nene.devicepolicy.DeviceOwner; 44 import com.android.bedstead.nene.devicepolicy.ProfileOwner; 45 import com.android.bedstead.nene.exceptions.NeneException; 46 import com.android.bedstead.nene.users.UserReference; 47 import com.android.bedstead.nene.users.UserType; 48 import com.android.bedstead.testapp.TestApp; 49 50 import org.junit.ClassRule; 51 import org.junit.Rule; 52 import org.junit.Test; 53 import org.junit.runner.RunWith; 54 55 @RunWith(BedsteadJUnit4.class) 56 public class RemoteDpcTest { 57 // TODO(180478924): We shouldn't need to hardcode this 58 private static final String DEVICE_ADMIN_TESTAPP_PACKAGE_NAME = 59 "com.android.bedstead.testapp.DeviceAdminTestApp"; 60 private static final ComponentName NON_REMOTE_DPC_COMPONENT = 61 new ComponentName(DEVICE_ADMIN_TESTAPP_PACKAGE_NAME, 62 "com.android.bedstead.testapp.DeviceAdminTestApp.DeviceAdminReceiver"); 63 64 @ClassRule @Rule 65 public static DeviceState sDeviceState = new DeviceState(); 66 67 private static TestApp sNonRemoteDpcTestApp = sDeviceState.testApps().query() 68 // TODO(180478924): Query by feature not package name 69 .wherePackageName().isEqualTo(DEVICE_ADMIN_TESTAPP_PACKAGE_NAME) 70 .get(); 71 private static final UserReference sUser = TestApis.users().instrumented(); 72 private static final UserReference NON_EXISTING_USER_REFERENCE = 73 TestApis.users().find(99999); 74 private static final UserHandle NON_EXISTING_USER_HANDLE = 75 NON_EXISTING_USER_REFERENCE.userHandle(); 76 77 @BeforeClass setupClass()78 public static void setupClass() { 79 sNonRemoteDpcTestApp.install(); 80 sNonRemoteDpcTestApp.install(TestApis.users().system()); 81 } 82 83 @AfterClass teardownClass()84 public static void teardownClass() { 85 sNonRemoteDpcTestApp.uninstallFromAllUsers(); 86 } 87 88 @Test 89 @EnsureHasNoDeviceOwner deviceOwner_noDeviceOwner_returnsNull()90 public void deviceOwner_noDeviceOwner_returnsNull() { 91 assertThat(RemoteDpc.deviceOwner()).isNull(); 92 } 93 94 @Test 95 @RequireRunOnSystemUser 96 @EnsureHasNoDeviceOwner 97 @EnsureHasNoProfileOwner 98 @EnsureHasNoSecondaryUser // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser 99 @EnsureHasNoWorkProfile // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser deviceOwner_nonRemoteDpcDeviceOwner_returnsNull()100 public void deviceOwner_nonRemoteDpcDeviceOwner_returnsNull() { 101 DeviceOwner deviceOwner = 102 TestApis.devicePolicy().setDeviceOwner(NON_REMOTE_DPC_COMPONENT); 103 try { 104 assertThat(RemoteDpc.deviceOwner()).isNull(); 105 } finally { 106 deviceOwner.remove(); 107 } 108 } 109 110 @Test 111 @EnsureHasDeviceOwner deviceOwner_remoteDpcDeviceOwner_returnsInstance()112 public void deviceOwner_remoteDpcDeviceOwner_returnsInstance() { 113 assertThat(RemoteDpc.deviceOwner()).isNotNull(); 114 } 115 116 @Test 117 @EnsureHasNoProfileOwner profileOwner_noProfileOwner_returnsNull()118 public void profileOwner_noProfileOwner_returnsNull() { 119 assertThat(RemoteDpc.profileOwner()).isNull(); 120 } 121 122 @Test 123 @EnsureHasNoDeviceOwner 124 @EnsureHasNoProfileOwner profileOwner_nonRemoteDpcProfileOwner_returnsNull()125 public void profileOwner_nonRemoteDpcProfileOwner_returnsNull() { 126 ProfileOwner profileOwner = 127 TestApis.devicePolicy().setProfileOwner(sUser, NON_REMOTE_DPC_COMPONENT); 128 try { 129 assertThat(RemoteDpc.profileOwner()).isNull(); 130 } finally { 131 profileOwner.remove(); 132 } 133 } 134 135 @Test 136 @EnsureHasProfileOwner profileOwner_remoteDpcProfileOwner_returnsInstance()137 public void profileOwner_remoteDpcProfileOwner_returnsInstance() { 138 assertThat(RemoteDpc.profileOwner()).isNotNull(); 139 } 140 141 @Test profileOwner_userHandle_null_throwsException()142 public void profileOwner_userHandle_null_throwsException() { 143 assertThrows(NullPointerException.class, () -> RemoteDpc.profileOwner((UserHandle) null)); 144 } 145 146 @Test 147 @EnsureHasNoDeviceOwner 148 @EnsureHasNoWorkProfile 149 @RequireRunOnInitialUser profileOwner_userHandle_noProfileOwner_returnsNull()150 public void profileOwner_userHandle_noProfileOwner_returnsNull() { 151 UserReference profile = TestApis.users().createUser() 152 .parent(sUser) 153 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 154 .createAndStart(); 155 try { 156 assertThat(RemoteDpc.profileOwner(profile.userHandle())).isNull(); 157 } finally { 158 profile.remove(); 159 } 160 } 161 162 @Test 163 @EnsureHasNoDeviceOwner 164 @EnsureHasNoWorkProfile 165 @RequireRunOnInitialUser 166 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") profileOwner_userHandle_nonRemoteDpcProfileOwner_returnsNull()167 public void profileOwner_userHandle_nonRemoteDpcProfileOwner_returnsNull() { 168 UserReference profile = TestApis.users().createUser() 169 .parent(sUser) 170 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 171 .createAndStart(); 172 sNonRemoteDpcTestApp.install(profile); 173 try { 174 TestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 175 176 assertThat(RemoteDpc.profileOwner(profile.userHandle())).isNull(); 177 } finally { 178 profile.remove(); 179 } 180 } 181 182 @Test 183 @EnsureHasNoDeviceOwner 184 @EnsureHasNoWorkProfile 185 @RequireRunOnInitialUser 186 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") profileOwner_userHandle_remoteDpcProfileOwner_returnsInstance()187 public void profileOwner_userHandle_remoteDpcProfileOwner_returnsInstance() { 188 UserReference profile = TestApis.users().createUser() 189 .parent(sUser) 190 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 191 .createAndStart(); 192 RemoteDpc.setAsProfileOwner(profile); 193 try { 194 assertThat(RemoteDpc.profileOwner(profile.userHandle())).isNotNull(); 195 } finally { 196 profile.remove(); 197 } 198 } 199 200 @Test profileOwner_userReference_null_throwsException()201 public void profileOwner_userReference_null_throwsException() { 202 assertThrows(NullPointerException.class, 203 () -> RemoteDpc.profileOwner((UserReference) null)); 204 } 205 206 @Test 207 @EnsureHasNoDeviceOwner 208 @EnsureHasNoWorkProfile 209 @RequireRunOnInitialUser profileOwner_userReference_noProfileOwner_returnsNull()210 public void profileOwner_userReference_noProfileOwner_returnsNull() { 211 UserReference profile = TestApis.users().createUser() 212 .parent(sUser) 213 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 214 .createAndStart(); 215 try { 216 assertThat(RemoteDpc.profileOwner(profile)).isNull(); 217 } finally { 218 profile.remove(); 219 } 220 } 221 222 @Test 223 @EnsureHasNoDeviceOwner 224 @RequireRunOnInitialUser 225 @EnsureHasNoWorkProfile profileOwner_userReference_nonRemoteDpcProfileOwner_returnsNull()226 public void profileOwner_userReference_nonRemoteDpcProfileOwner_returnsNull() { 227 UserReference profile = TestApis.users().createUser() 228 .parent(sUser) 229 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 230 .createAndStart(); 231 sNonRemoteDpcTestApp.install(profile); 232 try { 233 TestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 234 235 assertThat(RemoteDpc.profileOwner(profile)).isNull(); 236 } finally { 237 profile.remove(); 238 } 239 } 240 241 @Test 242 @EnsureHasNoDeviceOwner 243 @EnsureHasNoWorkProfile 244 @RequireRunOnInitialUser 245 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") profileOwner_userReference_remoteDpcProfileOwner_returnsInstance()246 public void profileOwner_userReference_remoteDpcProfileOwner_returnsInstance() { 247 UserReference profile = TestApis.users().createUser() 248 .parent(sUser) 249 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 250 .createAndStart(); 251 RemoteDpc.setAsProfileOwner(profile); 252 try { 253 assertThat(RemoteDpc.profileOwner(profile)).isNotNull(); 254 } finally { 255 profile.remove(); 256 } 257 } 258 259 @Test 260 @EnsureHasNoDeviceOwner 261 @EnsureHasNoProfileOwner any_noDeviceOwner_noProfileOwner_returnsNull()262 public void any_noDeviceOwner_noProfileOwner_returnsNull() { 263 assertThat(RemoteDpc.any()).isNull(); 264 } 265 266 @Test 267 @EnsureHasNoDeviceOwner 268 @EnsureHasNoProfileOwner any_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull()269 public void any_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull() { 270 ProfileOwner profileOwner = TestApis.devicePolicy().setProfileOwner(sUser, 271 NON_REMOTE_DPC_COMPONENT); 272 273 try { 274 assertThat(RemoteDpc.any()).isNull(); 275 } finally { 276 profileOwner.remove(); 277 } 278 } 279 280 @Test 281 @EnsureHasNoDeviceOwner 282 @EnsureHasNoProfileOwner 283 @RequireRunOnSystemUser 284 @EnsureHasNoSecondaryUser // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser 285 @EnsureHasNoWorkProfile // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser any_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull()286 public void any_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull() { 287 DeviceOwner deviceOwner = TestApis.devicePolicy().setDeviceOwner( 288 NON_REMOTE_DPC_COMPONENT); 289 290 try { 291 assertThat(RemoteDpc.any()).isNull(); 292 } finally { 293 deviceOwner.remove(); 294 } 295 } 296 297 @Test 298 @EnsureHasDeviceOwner any_remoteDpcDeviceOwner_returnsDeviceOwner()299 public void any_remoteDpcDeviceOwner_returnsDeviceOwner() { 300 assertThat(RemoteDpc.any()).isNotNull(); 301 } 302 303 @Test 304 @EnsureHasProfileOwner any_remoteDpcProfileOwner_returnsProfileOwner()305 public void any_remoteDpcProfileOwner_returnsProfileOwner() { 306 assertThat(RemoteDpc.any()).isNotNull(); 307 } 308 309 @Test any_userHandle_null_throwsException()310 public void any_userHandle_null_throwsException() { 311 assertThrows(NullPointerException.class, () -> RemoteDpc.any((UserHandle) null)); 312 } 313 314 @Test 315 @EnsureHasNoDeviceOwner 316 @EnsureHasNoProfileOwner any_userHandle_noDeviceOwner_noProfileOwner_returnsNull()317 public void any_userHandle_noDeviceOwner_noProfileOwner_returnsNull() { 318 assertThat(RemoteDpc.any(sUser.userHandle())).isNull(); 319 } 320 321 @Test 322 @EnsureHasNoDeviceOwner 323 @EnsureHasNoWorkProfile 324 @RequireRunOnInitialUser any_userHandle_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull()325 public void any_userHandle_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull() { 326 UserReference profile = TestApis.users().createUser() 327 .parent(sUser) 328 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 329 .createAndStart(); 330 sNonRemoteDpcTestApp.install(profile); 331 try { 332 TestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 333 334 assertThat(RemoteDpc.any(profile.userHandle())).isNull(); 335 } finally { 336 profile.remove(); 337 } 338 } 339 340 @Test 341 @EnsureHasNoDeviceOwner 342 @EnsureHasNoProfileOwner 343 @RequireRunOnSystemUser 344 @EnsureHasNoSecondaryUser // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser 345 @EnsureHasNoWorkProfile // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser any_userHandle_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull()346 public void any_userHandle_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull() { 347 DeviceOwner deviceOwner = TestApis.devicePolicy().setDeviceOwner( 348 NON_REMOTE_DPC_COMPONENT); 349 350 try { 351 assertThat(RemoteDpc.any(sUser.userHandle())).isNull(); 352 } finally { 353 deviceOwner.remove(); 354 } 355 } 356 357 @Test 358 @EnsureHasDeviceOwner 359 @EnsureHasNoProfileOwner any_userHandle_remoteDpcDeviceOwner_returnsDeviceOwner()360 public void any_userHandle_remoteDpcDeviceOwner_returnsDeviceOwner() { 361 RemoteDpc deviceOwner = RemoteDpc.deviceOwner(); 362 363 assertThat(RemoteDpc.any(sUser.userHandle())).isEqualTo(deviceOwner); 364 } 365 366 @Test 367 @EnsureHasNoDeviceOwner 368 @EnsureHasNoWorkProfile 369 @RequireRunOnInitialUser 370 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") any_userHandle_remoteDpcProfileOwner_returnsProfileOwner()371 public void any_userHandle_remoteDpcProfileOwner_returnsProfileOwner() { 372 UserReference profile = TestApis.users().createUser() 373 .parent(sUser) 374 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 375 .createAndStart(); 376 try { 377 RemoteDpc profileOwner = RemoteDpc.setAsProfileOwner(profile); 378 379 assertThat(RemoteDpc.any(profile.userHandle())).isEqualTo(profileOwner); 380 } finally { 381 profile.remove(); 382 } 383 } 384 385 @Test any_userReference_null_throwsException()386 public void any_userReference_null_throwsException() { 387 assertThrows(NullPointerException.class, () -> RemoteDpc.any((UserReference) null)); 388 } 389 390 @Test any_userReference_noDeviceOwner_noProfileOwner_returnsNull()391 public void any_userReference_noDeviceOwner_noProfileOwner_returnsNull() { 392 assertThat(RemoteDpc.any(sUser)).isNull(); 393 } 394 395 @Test 396 @EnsureHasNoDeviceOwner 397 @EnsureHasNoWorkProfile 398 @RequireRunOnInitialUser any_userReference_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull()399 public void any_userReference_noDeviceOwner_nonRemoteDpcProfileOwner_returnsNull() { 400 UserReference profile = TestApis.users().createUser() 401 .parent(sUser) 402 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 403 .createAndStart(); 404 sNonRemoteDpcTestApp.install(profile); 405 try { 406 TestApis.devicePolicy().setProfileOwner(profile, NON_REMOTE_DPC_COMPONENT); 407 408 assertThat(RemoteDpc.any(profile)).isNull(); 409 } finally { 410 profile.remove(); 411 } 412 } 413 414 @Test 415 @EnsureHasNoDeviceOwner 416 @EnsureHasNoProfileOwner 417 @RequireRunOnSystemUser 418 @EnsureHasNoSecondaryUser // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser 419 @EnsureHasNoWorkProfile // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser any_userReference_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull()420 public void any_userReference_nonRemoteDpcDeviceOwner_noProfileOwner_returnsNull() { 421 DeviceOwner deviceOwner = TestApis.devicePolicy().setDeviceOwner( 422 NON_REMOTE_DPC_COMPONENT); 423 424 try { 425 assertThat(RemoteDpc.any(sUser)).isNull(); 426 } finally { 427 deviceOwner.remove(); 428 } 429 } 430 431 @Test 432 @EnsureHasDeviceOwner 433 @EnsureHasNoProfileOwner any_userReference_remoteDpcDeviceOwner_returnsDeviceOwner()434 public void any_userReference_remoteDpcDeviceOwner_returnsDeviceOwner() { 435 RemoteDpc deviceOwner = RemoteDpc.deviceOwner(); 436 437 assertThat(RemoteDpc.any(sUser)).isEqualTo(deviceOwner); 438 } 439 440 @Test 441 @EnsureHasNoDeviceOwner 442 @EnsureHasNoWorkProfile 443 @RequireRunOnInitialUser 444 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") any_userReference_remoteDpcProfileOwner_returnsProfileOwner()445 public void any_userReference_remoteDpcProfileOwner_returnsProfileOwner() { 446 UserReference profile = TestApis.users().createUser() 447 .parent(sUser) 448 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 449 .createAndStart(); 450 try { 451 RemoteDpc profileOwner = RemoteDpc.setAsProfileOwner(profile); 452 453 assertThat(RemoteDpc.any(profile)).isEqualTo(profileOwner); 454 } finally { 455 profile.remove(); 456 } 457 } 458 459 @Test setAsDeviceOwner_withoutTestAppQuery_setsDefault()460 public void setAsDeviceOwner_withoutTestAppQuery_setsDefault() { 461 RemoteDpc.setAsDeviceOwner(); 462 463 DeviceOwner deviceOwner = TestApis.devicePolicy().getDeviceOwner(); 464 assertThat(deviceOwner.pkg().packageName()).isEqualTo("com.android.cts.RemoteDPC"); 465 } 466 467 @Test 468 @EnsureHasDeviceOwner setAsDeviceOwner_alreadySet_doesNothing()469 public void setAsDeviceOwner_alreadySet_doesNothing() { 470 RemoteDpc.setAsDeviceOwner(); 471 472 DeviceOwner deviceOwner = TestApis.devicePolicy().getDeviceOwner(); 473 assertThat(deviceOwner).isNotNull(); 474 } 475 476 @Test 477 @EnsureHasNoDeviceOwner 478 @EnsureHasNoProfileOwner 479 @RequireRunOnSystemUser 480 @EnsureHasNoSecondaryUser // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser 481 @EnsureHasNoWorkProfile // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser setAsDeviceOwner_alreadyHasDeviceOwner_replacesDeviceOwner()482 public void setAsDeviceOwner_alreadyHasDeviceOwner_replacesDeviceOwner() { 483 TestApis.devicePolicy().setDeviceOwner(NON_REMOTE_DPC_COMPONENT); 484 485 try { 486 RemoteDpc remoteDPC = RemoteDpc.setAsDeviceOwner(); 487 488 DeviceOwner deviceOwner = TestApis.devicePolicy().getDeviceOwner(); 489 assertThat(deviceOwner).isEqualTo(remoteDPC.devicePolicyController()); 490 } finally { 491 TestApis.devicePolicy().getDeviceOwner().remove(); 492 } 493 } 494 495 @Test 496 @EnsureHasNoDeviceOwner 497 @EnsureHasNoProfileOwner setAsDeviceOwner_doesNotHaveDeviceOwner_setsDeviceOwner()498 public void setAsDeviceOwner_doesNotHaveDeviceOwner_setsDeviceOwner() { 499 RemoteDpc.setAsDeviceOwner(); 500 501 DeviceOwner deviceOwner = TestApis.devicePolicy().getDeviceOwner(); 502 try { 503 assertThat(deviceOwner).isNotNull(); 504 } finally { 505 if (deviceOwner != null) { 506 deviceOwner.remove(); 507 } 508 } 509 } 510 511 @Test setAsProfileOwner_userHandle_null_throwsException()512 public void setAsProfileOwner_userHandle_null_throwsException() { 513 assertThrows(NullPointerException.class, 514 () -> RemoteDpc.setAsProfileOwner((UserHandle) null)); 515 } 516 517 @Test setAsProfileOwner_userHandle_nonExistingUser_throwsException()518 public void setAsProfileOwner_userHandle_nonExistingUser_throwsException() { 519 assertThrows(NeneException.class, 520 () -> RemoteDpc.setAsProfileOwner(NON_EXISTING_USER_HANDLE)); 521 } 522 523 @Test 524 @EnsureHasNoDeviceOwner 525 @EnsureHasNoWorkProfile 526 @RequireRunOnInitialUser 527 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") setAsProfileOwner_userHandle_alreadySet_doesNothing()528 public void setAsProfileOwner_userHandle_alreadySet_doesNothing() { 529 UserReference profile = TestApis.users().createUser() 530 .parent(sUser) 531 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 532 .createAndStart(); 533 try { 534 RemoteDpc.setAsProfileOwner(profile.userHandle()); 535 536 RemoteDpc.setAsProfileOwner(profile.userHandle()); 537 538 assertThat(TestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 539 } finally { 540 profile.remove(); 541 } 542 } 543 544 @Test 545 @EnsureHasNoDeviceOwner 546 @EnsureHasNoWorkProfile 547 @RequireRunOnInitialUser setAsProfileOwner_userHandle_alreadyHasProfileOwner_replacesProfileOwner()548 public void setAsProfileOwner_userHandle_alreadyHasProfileOwner_replacesProfileOwner() { 549 sNonRemoteDpcTestApp.install(); 550 RemoteDpc remoteDPC = null; 551 try { 552 TestApis.devicePolicy() 553 .setProfileOwner(TestApis.users().instrumented(), NON_REMOTE_DPC_COMPONENT); 554 555 remoteDPC = RemoteDpc.setAsProfileOwner(TestApis.users().instrumented().userHandle()); 556 557 assertThat(TestApis.devicePolicy().getProfileOwner(TestApis.users().instrumented())) 558 .isEqualTo(remoteDPC.devicePolicyController()); 559 } finally { 560 if (remoteDPC != null) { 561 remoteDPC.remove(); 562 } 563 } 564 } 565 566 @Test 567 @EnsureHasNoDeviceOwner 568 @EnsureHasNoWorkProfile 569 @RequireRunOnInitialUser 570 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") setAsProfileOwner_userHandle_doesNotHaveProfileOwner_setsProfileOwner()571 public void setAsProfileOwner_userHandle_doesNotHaveProfileOwner_setsProfileOwner() { 572 UserReference profile = TestApis.users().createUser() 573 .parent(sUser) 574 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 575 .createAndStart(); 576 try { 577 RemoteDpc.setAsProfileOwner(profile.userHandle()); 578 579 assertThat(TestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 580 } finally { 581 profile.remove(); 582 } 583 } 584 585 @Test 586 @EnsureHasNoDeviceOwner 587 @EnsureHasNoWorkProfile 588 @RequireRunOnInitialUser 589 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") setAsProfileOwner_disallowInstallUnknownSourcesIsDisabled()590 public void setAsProfileOwner_disallowInstallUnknownSourcesIsDisabled() { 591 // This is a temp fix for an issue where DISALLOW_INSTALL_UNKNOWN_SOURCES causes 592 // verification failures 593 UserReference profile = TestApis.users().createUser() 594 .parent(sUser) 595 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 596 .createAndStart(); 597 try { 598 RemoteDpc profileOwner = RemoteDpc.setAsProfileOwner(profile.userHandle()); 599 600 assertThat(profileOwner.userManager() 601 .hasUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES)) 602 .isFalse(); 603 } finally { 604 profile.remove(); 605 } 606 } 607 608 @Test setAsProfileOwner_userReference_null_throwsException()609 public void setAsProfileOwner_userReference_null_throwsException() { 610 assertThrows(NullPointerException.class, 611 () -> RemoteDpc.setAsProfileOwner((UserReference) null)); 612 } 613 614 @Test setAsProfileOwner_userReference_nonExistingUser_throwsException()615 public void setAsProfileOwner_userReference_nonExistingUser_throwsException() { 616 assertThrows(NeneException.class, 617 () -> RemoteDpc.setAsProfileOwner(NON_EXISTING_USER_REFERENCE)); 618 } 619 620 @Test 621 @EnsureHasNoDeviceOwner 622 @EnsureHasNoWorkProfile 623 @RequireRunOnInitialUser 624 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") setAsProfileOwner_userReference_alreadySet_doesNothing()625 public void setAsProfileOwner_userReference_alreadySet_doesNothing() { 626 UserReference profile = TestApis.users().createUser() 627 .parent(sUser) 628 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 629 .createAndStart(); 630 try { 631 RemoteDpc.setAsProfileOwner(profile); 632 633 RemoteDpc.setAsProfileOwner(profile); 634 635 assertThat(TestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 636 } finally { 637 profile.remove(); 638 } 639 } 640 641 @Test 642 @EnsureHasNoDeviceOwner 643 @EnsureHasNoWorkProfile 644 @RequireRunOnInitialUser setAsProfileOwner_userReference_alreadyHasProfileOwner_replacesProfileOwner()645 public void setAsProfileOwner_userReference_alreadyHasProfileOwner_replacesProfileOwner() { 646 sNonRemoteDpcTestApp.install(); 647 RemoteDpc remoteDPC = null; 648 try { 649 TestApis.devicePolicy() 650 .setProfileOwner(TestApis.users().instrumented(), NON_REMOTE_DPC_COMPONENT); 651 652 remoteDPC = RemoteDpc.setAsProfileOwner(TestApis.users().instrumented()); 653 654 assertThat(TestApis.devicePolicy().getProfileOwner(TestApis.users().instrumented())) 655 .isEqualTo(remoteDPC.devicePolicyController()); 656 } finally { 657 if (remoteDPC != null) { 658 remoteDPC.remove(); 659 } 660 } 661 } 662 663 @Test 664 @EnsureHasNoDeviceOwner 665 @EnsureHasNoWorkProfile 666 @RequireRunOnInitialUser 667 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") setAsProfileOwner_userReference_doesNotHaveProfileOwner_setsProfileOwner()668 public void setAsProfileOwner_userReference_doesNotHaveProfileOwner_setsProfileOwner() { 669 UserReference profile = TestApis.users().createUser() 670 .parent(sUser) 671 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 672 .createAndStart(); 673 try { 674 RemoteDpc.setAsProfileOwner(profile); 675 676 assertThat(TestApis.devicePolicy().getProfileOwner(profile)).isNotNull(); 677 } finally { 678 profile.remove(); 679 } 680 } 681 682 @Test 683 @EnsureHasDeviceOwner devicePolicyController_returnsDevicePolicyController()684 public void devicePolicyController_returnsDevicePolicyController() { 685 RemoteDpc remoteDPC = RemoteDpc.deviceOwner(); 686 687 try { 688 assertThat(remoteDPC.devicePolicyController()) 689 .isEqualTo(TestApis.devicePolicy().getDeviceOwner()); 690 } finally { 691 remoteDPC.remove(); 692 } 693 } 694 695 @Test 696 @EnsureHasDeviceOwner remove_deviceOwner_removes()697 public void remove_deviceOwner_removes() { 698 RemoteDpc remoteDPC = RemoteDpc.deviceOwner(); 699 700 remoteDPC.remove(); 701 702 assertThat(TestApis.devicePolicy().getDeviceOwner()).isNull(); 703 } 704 705 @Test 706 @EnsureHasNoDeviceOwner 707 @EnsureHasNoWorkProfile 708 @RequireRunOnInitialUser 709 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") remove_profileOwner_removes()710 public void remove_profileOwner_removes() { 711 try (UserReference profile = TestApis.users().createUser() 712 .parent(sUser) 713 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 714 .createAndStart()) { 715 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(profile); 716 717 remoteDPC.remove(); 718 719 assertThat(TestApis.devicePolicy().getProfileOwner(profile)).isNull(); 720 } 721 } 722 723 // TODO(scottjonathan): Do we need to support the case where there is both a DO and a PO on 724 // older versions of Android? 725 726 @Test 727 @EnsureHasDeviceOwner frameworkCall_makesCall()728 public void frameworkCall_makesCall() { 729 sDeviceState.dpc().devicePolicyManager().getCurrentFailedPasswordAttempts(); 730 } 731 732 @Test 733 @EnsureHasNoDeviceOwner 734 @EnsureHasNoWorkProfile 735 @RequireRunOnInitialUser 736 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") frameworkCall_onProfile_makesCall()737 public void frameworkCall_onProfile_makesCall() { 738 try (UserReference profile = TestApis.users().createUser() 739 .parent(sUser) 740 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 741 .createAndStart()) { 742 RemoteDpc remoteDPC = RemoteDpc.setAsProfileOwner(profile); 743 744 // Checking that the call succeeds 745 remoteDPC.devicePolicyManager().isUsingUnifiedPassword(remoteDPC.componentName()); 746 } 747 } 748 749 @Test 750 @EnsureHasDeviceOwner frameworkCallRequiresProfileOwner_notProfileOwner_throwsSecurityException()751 public void frameworkCallRequiresProfileOwner_notProfileOwner_throwsSecurityException() { 752 RemoteDpc remoteDPC = RemoteDpc.deviceOwner(); 753 754 assertThrows(SecurityException.class, () -> 755 remoteDPC.devicePolicyManager().isUsingUnifiedPassword(remoteDPC.componentName())); 756 } 757 758 @Test forDevicePolicyController_nullDevicePolicyController_throwsException()759 public void forDevicePolicyController_nullDevicePolicyController_throwsException() { 760 assertThrows(NullPointerException.class, () -> RemoteDpc.forDevicePolicyController(null)); 761 } 762 763 @Test 764 @RequireRunOnSystemUser 765 @EnsureHasNoDeviceOwner 766 @EnsureHasNoProfileOwner 767 @EnsureHasNoSecondaryUser // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser 768 @EnsureHasNoWorkProfile // TODO(b/202832720): Replace with @EnsureHasOnlyInstrumentedUser forDevicePolicyController_nonRemoteDpcDevicePolicyController_throwsException()769 public void forDevicePolicyController_nonRemoteDpcDevicePolicyController_throwsException() { 770 DeviceOwner deviceOwner = TestApis.devicePolicy().setDeviceOwner(NON_REMOTE_DPC_COMPONENT); 771 772 try { 773 assertThrows(IllegalStateException.class, 774 () -> RemoteDpc.forDevicePolicyController(deviceOwner)); 775 } finally { 776 deviceOwner.remove(); 777 } 778 } 779 780 @Test 781 @EnsureHasDeviceOwner forDevicePolicyController_remoteDpcDevicePolicyController_returnsRemoteDpc()782 public void forDevicePolicyController_remoteDpcDevicePolicyController_returnsRemoteDpc() { 783 RemoteDpc remoteDPC = RemoteDpc.deviceOwner(); 784 785 assertThat(RemoteDpc.forDevicePolicyController(remoteDPC.devicePolicyController())) 786 .isNotNull(); 787 } 788 789 @Test 790 @EnsureHasNoWorkProfile 791 @EnsureHasNoDeviceOwner 792 @RequireRunOnInitialUser 793 @RequireSdkVersion(min = Q, reason = "Cannot use RemoteDPC cross-user prior to Q") getParentProfileInstance_returnsUsableInstance()794 public void getParentProfileInstance_returnsUsableInstance() { 795 try (UserReference profile = TestApis.users().createUser() 796 .parent(sUser) 797 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME)) 798 .createAndStart()) { 799 RemoteDpc remoteDpc = RemoteDpc.setAsProfileOwner(profile); 800 801 // Confirm that we can call methods on the RemoteDevicePolicyManager which comes from 802 // getParentProfileInstance 803 remoteDpc.devicePolicyManager() 804 .getParentProfileInstance(remoteDpc.componentName()) 805 .getPasswordQuality(remoteDpc.componentName()); 806 807 // APIs which are not supported on parent instances should throw SecurityException 808 assertThrows(SecurityException.class, () -> 809 remoteDpc.devicePolicyManager() 810 .getParentProfileInstance(remoteDpc.componentName()) 811 .getParentProfileInstance(remoteDpc.componentName())); 812 } 813 } 814 815 @Test setAsProfileOwner_alreadySetToDifferentRemoteDpc_replacesRemoteDpc()816 public void setAsProfileOwner_alreadySetToDifferentRemoteDpc_replacesRemoteDpc() { 817 try { 818 RemoteDpc.setAsProfileOwner(sUser, 819 sDeviceState.testApps().query().whereTargetSdkVersion().isLessThan(28)); 820 RemoteDpc.setAsProfileOwner(sUser, 821 sDeviceState.testApps().query().whereTargetSdkVersion().isGreaterThan(28)); 822 823 assertThat(TestApis.devicePolicy().getProfileOwner() 824 .pkg().targetSdkVersion()).isGreaterThan(28); 825 } finally { 826 ProfileOwner profileOwner = TestApis.devicePolicy().getProfileOwner(sUser); 827 if (profileOwner != null) { 828 profileOwner.remove(); 829 } 830 } 831 } 832 833 @Test setAsDeviceOwner_alreadySetToDifferentRemoteDpc_replacesRemoteDpc()834 public void setAsDeviceOwner_alreadySetToDifferentRemoteDpc_replacesRemoteDpc() { 835 try { 836 RemoteDpc.setAsDeviceOwner( 837 sDeviceState.testApps().query().whereTargetSdkVersion().isLessThan(28)); 838 RemoteDpc.setAsDeviceOwner( 839 sDeviceState.testApps().query().whereTargetSdkVersion().isGreaterThan(28)); 840 841 assertThat(TestApis.devicePolicy().getDeviceOwner() 842 .pkg().targetSdkVersion()).isGreaterThan(28); 843 } finally { 844 DeviceOwner deviceOwner = TestApis.devicePolicy().getDeviceOwner(); 845 if (deviceOwner != null) { 846 deviceOwner.remove(); 847 } 848 } 849 } 850 851 @Test setAsProfileOwner_matchesExistingRemoteDpc_doesNotReplace()852 public void setAsProfileOwner_matchesExistingRemoteDpc_doesNotReplace() { 853 try { 854 RemoteDpc.setAsProfileOwner(sUser, 855 sDeviceState.testApps().query().whereTargetSdkVersion().isEqualTo(28)); 856 RemoteDpc.setAsProfileOwner(sUser, 857 sDeviceState.testApps().query().whereTargetSdkVersion().isEqualTo(28)); 858 859 assertThat(TestApis.devicePolicy().getProfileOwner() 860 .pkg().targetSdkVersion()).isEqualTo(28); 861 } finally { 862 ProfileOwner profileOwner = TestApis.devicePolicy().getProfileOwner(sUser); 863 if (profileOwner != null) { 864 profileOwner.remove(); 865 } 866 } 867 } 868 869 @Test setAsDeviceOwner_matchesExistingRemoteDpc_doesNotReplace()870 public void setAsDeviceOwner_matchesExistingRemoteDpc_doesNotReplace() { 871 try { 872 RemoteDpc.setAsDeviceOwner( 873 sDeviceState.testApps().query().whereTargetSdkVersion().isEqualTo(28)); 874 RemoteDpc.setAsDeviceOwner( 875 sDeviceState.testApps().query().whereTargetSdkVersion().isEqualTo(28)); 876 877 assertThat(TestApis.devicePolicy().getDeviceOwner() 878 .pkg().targetSdkVersion()).isEqualTo(28); 879 } finally { 880 DeviceOwner deviceOwner = TestApis.devicePolicy().getDeviceOwner(); 881 if (deviceOwner != null) { 882 deviceOwner.remove(); 883 } 884 } 885 } 886 887 } 888