AccountManagerServiceTest.java revision 01985ff6d249e434b900cd33718d830a948e3a86
1/* 2 * Copyright (C) 2009 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 17package com.android.server.accounts; 18 19import static android.database.sqlite.SQLiteDatabase.deleteDatabase; 20import static org.mockito.Matchers.any; 21import static org.mockito.Matchers.anyBoolean; 22import static org.mockito.Matchers.anyInt; 23import static org.mockito.Matchers.anyString; 24import static org.mockito.Matchers.eq; 25import static org.mockito.Mockito.mock; 26import static org.mockito.Mockito.never; 27import static org.mockito.Mockito.verify; 28import static org.mockito.Mockito.when; 29 30import android.accounts.Account; 31import android.accounts.AccountManager; 32import android.accounts.AccountManagerInternal; 33import android.accounts.AuthenticatorDescription; 34import android.accounts.CantAddAccountActivity; 35import android.accounts.IAccountManagerResponse; 36import android.app.AppOpsManager; 37import android.app.admin.DevicePolicyManager; 38import android.app.admin.DevicePolicyManagerInternal; 39import android.app.INotificationManager; 40import android.content.BroadcastReceiver; 41import android.content.ComponentName; 42import android.content.Context; 43import android.content.Intent; 44import android.content.IntentFilter; 45import android.content.ServiceConnection; 46import android.content.pm.ActivityInfo; 47import android.content.pm.ApplicationInfo; 48import android.content.pm.PackageInfo; 49import android.content.pm.PackageManager; 50import android.content.pm.RegisteredServicesCacheListener; 51import android.content.pm.ResolveInfo; 52import android.content.pm.Signature; 53import android.content.pm.UserInfo; 54import android.content.pm.RegisteredServicesCache.ServiceInfo; 55import android.database.Cursor; 56import android.database.DatabaseErrorHandler; 57import android.database.sqlite.SQLiteDatabase; 58import android.os.Bundle; 59import android.os.Handler; 60import android.os.IBinder; 61import android.os.Looper; 62import android.os.RemoteException; 63import android.os.UserHandle; 64import android.os.UserManager; 65import android.test.AndroidTestCase; 66import android.test.mock.MockContext; 67import android.test.suitebuilder.annotation.SmallTest; 68import android.util.Log; 69 70import com.android.frameworks.servicestests.R; 71import com.android.server.LocalServices; 72 73import org.mockito.ArgumentCaptor; 74import org.mockito.Captor; 75import org.mockito.Mock; 76import org.mockito.MockitoAnnotations; 77 78import java.io.File; 79import java.io.FileDescriptor; 80import java.io.PrintWriter; 81import java.security.GeneralSecurityException; 82import java.util.ArrayList; 83import java.util.Arrays; 84import java.util.Collection; 85import java.util.Comparator; 86import java.util.List; 87import java.util.concurrent.CountDownLatch; 88import java.util.concurrent.TimeUnit; 89 90public class AccountManagerServiceTest extends AndroidTestCase { 91 private static final String TAG = AccountManagerServiceTest.class.getSimpleName(); 92 private static final long ONE_DAY_IN_MILLISECOND = 86400000; 93 94 @Mock private Context mMockContext; 95 @Mock private AppOpsManager mMockAppOpsManager; 96 @Mock private UserManager mMockUserManager; 97 @Mock private PackageManager mMockPackageManager; 98 @Mock private DevicePolicyManagerInternal mMockDevicePolicyManagerInternal; 99 @Mock private DevicePolicyManager mMockDevicePolicyManager; 100 @Mock private IAccountManagerResponse mMockAccountManagerResponse; 101 @Mock private IBinder mMockBinder; 102 @Mock private INotificationManager mMockNotificationManager; 103 104 @Captor private ArgumentCaptor<Intent> mIntentCaptor; 105 @Captor private ArgumentCaptor<Bundle> mBundleCaptor; 106 107 private static final int LATCH_TIMEOUT_MS = 500; 108 private static final String PREN_DB = "pren.db"; 109 private static final String DE_DB = "de.db"; 110 private static final String CE_DB = "ce.db"; 111 private PackageInfo mPackageInfo; 112 private AccountManagerService mAms; 113 private TestInjector mTestInjector; 114 115 @Override 116 protected void setUp() throws Exception { 117 MockitoAnnotations.initMocks(this); 118 119 when(mMockPackageManager.checkSignatures(anyInt(), anyInt())) 120 .thenReturn(PackageManager.SIGNATURE_MATCH); 121 final UserInfo ui = new UserInfo(UserHandle.USER_SYSTEM, "user0", 0); 122 when(mMockUserManager.getUserInfo(eq(ui.id))).thenReturn(ui); 123 when(mMockContext.createPackageContextAsUser( 124 anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext); 125 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); 126 127 mPackageInfo = new PackageInfo(); 128 mPackageInfo.signatures = new Signature[1]; 129 mPackageInfo.signatures[0] = new Signature(new byte[] {'a', 'b', 'c', 'd'}); 130 mPackageInfo.applicationInfo = new ApplicationInfo(); 131 mPackageInfo.applicationInfo.privateFlags = ApplicationInfo.PRIVATE_FLAG_PRIVILEGED; 132 when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo); 133 when(mMockContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mMockAppOpsManager); 134 when(mMockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager); 135 when(mMockContext.getSystemServiceName(AppOpsManager.class)).thenReturn( 136 Context.APP_OPS_SERVICE); 137 when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn( 138 PackageManager.PERMISSION_GRANTED); 139 Bundle bundle = new Bundle(); 140 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 141 when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn( 142 mMockDevicePolicyManager); 143 when(mMockAccountManagerResponse.asBinder()).thenReturn(mMockBinder); 144 145 Context realTestContext = getContext(); 146 MyMockContext mockContext = new MyMockContext(realTestContext, mMockContext); 147 setContext(mockContext); 148 mTestInjector = new TestInjector(realTestContext, mockContext, mMockNotificationManager); 149 mAms = new AccountManagerService(mTestInjector); 150 } 151 152 @Override 153 protected void tearDown() throws Exception { 154 // Let async logging tasks finish, otherwise they may crash due to db being removed 155 CountDownLatch cdl = new CountDownLatch(1); 156 mAms.mHandler.post(() -> { 157 deleteDatabase(new File(mTestInjector.getCeDatabaseName(UserHandle.USER_SYSTEM))); 158 deleteDatabase(new File(mTestInjector.getDeDatabaseName(UserHandle.USER_SYSTEM))); 159 deleteDatabase(new File(mTestInjector.getPreNDatabaseName(UserHandle.USER_SYSTEM))); 160 cdl.countDown(); 161 }); 162 cdl.await(1, TimeUnit.SECONDS); 163 super.tearDown(); 164 } 165 166 class AccountSorter implements Comparator<Account> { 167 public int compare(Account object1, Account object2) { 168 if (object1 == object2) return 0; 169 if (object1 == null) return 1; 170 if (object2 == null) return -1; 171 int result = object1.type.compareTo(object2.type); 172 if (result != 0) return result; 173 return object1.name.compareTo(object2.name); 174 } 175 } 176 177 @SmallTest 178 public void testCheckAddAccount() throws Exception { 179 unlockSystemUser(); 180 Account a11 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 181 Account a21 = new Account("account2", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 182 Account a31 = new Account("account3", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 183 Account a12 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 184 Account a22 = new Account("account2", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 185 Account a32 = new Account("account3", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 186 mAms.addAccountExplicitly(a11, "p11", null); 187 mAms.addAccountExplicitly(a12, "p12", null); 188 mAms.addAccountExplicitly(a21, "p21", null); 189 mAms.addAccountExplicitly(a22, "p22", null); 190 mAms.addAccountExplicitly(a31, "p31", null); 191 mAms.addAccountExplicitly(a32, "p32", null); 192 193 Account[] accounts = mAms.getAccounts(null, mContext.getOpPackageName()); 194 Arrays.sort(accounts, new AccountSorter()); 195 assertEquals(6, accounts.length); 196 assertEquals(a11, accounts[0]); 197 assertEquals(a21, accounts[1]); 198 assertEquals(a31, accounts[2]); 199 assertEquals(a12, accounts[3]); 200 assertEquals(a22, accounts[4]); 201 assertEquals(a32, accounts[5]); 202 203 accounts = mAms.getAccounts(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 204 mContext.getOpPackageName()); 205 Arrays.sort(accounts, new AccountSorter()); 206 assertEquals(3, accounts.length); 207 assertEquals(a11, accounts[0]); 208 assertEquals(a21, accounts[1]); 209 assertEquals(a31, accounts[2]); 210 211 mAms.removeAccountInternal(a21); 212 213 accounts = mAms.getAccounts(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 214 mContext.getOpPackageName()); 215 Arrays.sort(accounts, new AccountSorter()); 216 assertEquals(2, accounts.length); 217 assertEquals(a11, accounts[0]); 218 assertEquals(a31, accounts[1]); 219 } 220 221 @SmallTest 222 public void testPasswords() throws Exception { 223 unlockSystemUser(); 224 Account a11 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 225 Account a12 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 226 mAms.addAccountExplicitly(a11, "p11", null); 227 mAms.addAccountExplicitly(a12, "p12", null); 228 229 assertEquals("p11", mAms.getPassword(a11)); 230 assertEquals("p12", mAms.getPassword(a12)); 231 232 mAms.setPassword(a11, "p11b"); 233 234 assertEquals("p11b", mAms.getPassword(a11)); 235 assertEquals("p12", mAms.getPassword(a12)); 236 } 237 238 @SmallTest 239 public void testUserdata() throws Exception { 240 unlockSystemUser(); 241 Account a11 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 242 Bundle u11 = new Bundle(); 243 u11.putString("a", "a_a11"); 244 u11.putString("b", "b_a11"); 245 u11.putString("c", "c_a11"); 246 Account a12 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 247 Bundle u12 = new Bundle(); 248 u12.putString("a", "a_a12"); 249 u12.putString("b", "b_a12"); 250 u12.putString("c", "c_a12"); 251 mAms.addAccountExplicitly(a11, "p11", u11); 252 mAms.addAccountExplicitly(a12, "p12", u12); 253 254 assertEquals("a_a11", mAms.getUserData(a11, "a")); 255 assertEquals("b_a11", mAms.getUserData(a11, "b")); 256 assertEquals("c_a11", mAms.getUserData(a11, "c")); 257 assertEquals("a_a12", mAms.getUserData(a12, "a")); 258 assertEquals("b_a12", mAms.getUserData(a12, "b")); 259 assertEquals("c_a12", mAms.getUserData(a12, "c")); 260 261 mAms.setUserData(a11, "b", "b_a11b"); 262 mAms.setUserData(a12, "c", null); 263 264 assertEquals("a_a11", mAms.getUserData(a11, "a")); 265 assertEquals("b_a11b", mAms.getUserData(a11, "b")); 266 assertEquals("c_a11", mAms.getUserData(a11, "c")); 267 assertEquals("a_a12", mAms.getUserData(a12, "a")); 268 assertEquals("b_a12", mAms.getUserData(a12, "b")); 269 assertNull(mAms.getUserData(a12, "c")); 270 } 271 272 @SmallTest 273 public void testAuthtokens() throws Exception { 274 unlockSystemUser(); 275 Account a11 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 276 Account a12 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 277 mAms.addAccountExplicitly(a11, "p11", null); 278 mAms.addAccountExplicitly(a12, "p12", null); 279 280 mAms.setAuthToken(a11, "att1", "a11_att1"); 281 mAms.setAuthToken(a11, "att2", "a11_att2"); 282 mAms.setAuthToken(a11, "att3", "a11_att3"); 283 mAms.setAuthToken(a12, "att1", "a12_att1"); 284 mAms.setAuthToken(a12, "att2", "a12_att2"); 285 mAms.setAuthToken(a12, "att3", "a12_att3"); 286 287 assertEquals("a11_att1", mAms.peekAuthToken(a11, "att1")); 288 assertEquals("a11_att2", mAms.peekAuthToken(a11, "att2")); 289 assertEquals("a11_att3", mAms.peekAuthToken(a11, "att3")); 290 assertEquals("a12_att1", mAms.peekAuthToken(a12, "att1")); 291 assertEquals("a12_att2", mAms.peekAuthToken(a12, "att2")); 292 assertEquals("a12_att3", mAms.peekAuthToken(a12, "att3")); 293 294 mAms.setAuthToken(a11, "att3", "a11_att3b"); 295 mAms.invalidateAuthToken(a12.type, "a12_att2"); 296 297 assertEquals("a11_att1", mAms.peekAuthToken(a11, "att1")); 298 assertEquals("a11_att2", mAms.peekAuthToken(a11, "att2")); 299 assertEquals("a11_att3b", mAms.peekAuthToken(a11, "att3")); 300 assertEquals("a12_att1", mAms.peekAuthToken(a12, "att1")); 301 assertNull(mAms.peekAuthToken(a12, "att2")); 302 assertEquals("a12_att3", mAms.peekAuthToken(a12, "att3")); 303 304 assertNull(mAms.peekAuthToken(a12, "att2")); 305 } 306 307 @SmallTest 308 public void testRemovedAccountSync() throws Exception { 309 unlockSystemUser(); 310 Account a1 = new Account("account1", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 311 Account a2 = new Account("account2", AccountManagerServiceTestFixtures.ACCOUNT_TYPE_2); 312 mAms.addAccountExplicitly(a1, "p1", null); 313 mAms.addAccountExplicitly(a2, "p2", null); 314 315 Context originalContext = ((MyMockContext)getContext()).mTestContext; 316 // create a separate instance of AMS. It initially assumes that user0 is locked 317 AccountManagerService ams2 = new AccountManagerService(mTestInjector); 318 319 // Verify that account can be removed when user is locked 320 ams2.removeAccountInternal(a1); 321 Account[] accounts = ams2.getAccounts(UserHandle.USER_SYSTEM, mContext.getOpPackageName()); 322 assertEquals(1, accounts.length); 323 assertEquals("Only a2 should be returned", a2, accounts[0]); 324 325 // Verify that CE db file is unchanged and still has 2 accounts 326 String ceDatabaseName = mTestInjector.getCeDatabaseName(UserHandle.USER_SYSTEM); 327 int accountsNumber = readNumberOfAccountsFromDbFile(originalContext, ceDatabaseName); 328 assertEquals("CE database should still have 2 accounts", 2, accountsNumber); 329 330 // Unlock the user and verify that db has been updated 331 ams2.onUserUnlocked(newIntentForUser(UserHandle.USER_SYSTEM)); 332 accounts = ams2.getAccounts(UserHandle.USER_SYSTEM, mContext.getOpPackageName()); 333 assertEquals(1, accounts.length); 334 assertEquals("Only a2 should be returned", a2, accounts[0]); 335 accountsNumber = readNumberOfAccountsFromDbFile(originalContext, ceDatabaseName); 336 assertEquals("CE database should now have 1 account", 1, accountsNumber); 337 } 338 339 @SmallTest 340 public void testPreNDatabaseMigration() throws Exception { 341 String preNDatabaseName = mTestInjector.getPreNDatabaseName(UserHandle.USER_SYSTEM); 342 Context originalContext = ((MyMockContext) getContext()).mTestContext; 343 PreNTestDatabaseHelper.createV4Database(originalContext, preNDatabaseName); 344 // Assert that database was created with 1 account 345 int n = readNumberOfAccountsFromDbFile(originalContext, preNDatabaseName); 346 assertEquals("pre-N database should have 1 account", 1, n); 347 348 // Start testing 349 unlockSystemUser(); 350 Account[] accounts = mAms.getAccounts(null, mContext.getOpPackageName()); 351 assertEquals("1 account should be migrated", 1, accounts.length); 352 assertEquals(PreNTestDatabaseHelper.ACCOUNT_NAME, accounts[0].name); 353 assertEquals(PreNTestDatabaseHelper.ACCOUNT_PASSWORD, mAms.getPassword(accounts[0])); 354 assertEquals("Authtoken should be migrated", 355 PreNTestDatabaseHelper.TOKEN_STRING, 356 mAms.peekAuthToken(accounts[0], PreNTestDatabaseHelper.TOKEN_TYPE)); 357 358 assertFalse("pre-N database file should be removed but was found at " + preNDatabaseName, 359 new File(preNDatabaseName).exists()); 360 361 // Verify that ce/de files are present 362 String deDatabaseName = mTestInjector.getDeDatabaseName(UserHandle.USER_SYSTEM); 363 String ceDatabaseName = mTestInjector.getCeDatabaseName(UserHandle.USER_SYSTEM); 364 assertTrue("DE database file should be created at " + deDatabaseName, 365 new File(deDatabaseName).exists()); 366 assertTrue("CE database file should be created at " + ceDatabaseName, 367 new File(ceDatabaseName).exists()); 368 } 369 370 @SmallTest 371 public void testStartAddAccountSessionWithNullResponse() throws Exception { 372 unlockSystemUser(); 373 try { 374 mAms.startAddAccountSession( 375 null, // response 376 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 377 "authTokenType", 378 null, // requiredFeatures 379 true, // expectActivityLaunch 380 null); // optionsIn 381 fail("IllegalArgumentException expected. But no exception was thrown."); 382 } catch (IllegalArgumentException e) { 383 // IllegalArgumentException is expected. 384 } 385 } 386 387 @SmallTest 388 public void testStartAddAccountSessionWithNullAccountType() throws Exception { 389 unlockSystemUser(); 390 try { 391 mAms.startAddAccountSession( 392 mMockAccountManagerResponse, // response 393 null, // accountType 394 "authTokenType", 395 null, // requiredFeatures 396 true, // expectActivityLaunch 397 null); // optionsIn 398 fail("IllegalArgumentException expected. But no exception was thrown."); 399 } catch (IllegalArgumentException e) { 400 // IllegalArgumentException is expected. 401 } 402 } 403 404 @SmallTest 405 public void testStartAddAccountSessionUserCannotModifyAccountNoDPM() throws Exception { 406 unlockSystemUser(); 407 Bundle bundle = new Bundle(); 408 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 409 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 410 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 411 412 mAms.startAddAccountSession( 413 mMockAccountManagerResponse, // response 414 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 415 "authTokenType", 416 null, // requiredFeatures 417 true, // expectActivityLaunch 418 null); // optionsIn 419 verify(mMockAccountManagerResponse).onError( 420 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 421 verify(mMockContext).startActivityAsUser(mIntentCaptor.capture(), eq(UserHandle.SYSTEM)); 422 423 // verify the intent for default CantAddAccountActivity is sent. 424 Intent intent = mIntentCaptor.getValue(); 425 assertEquals(intent.getComponent().getClassName(), CantAddAccountActivity.class.getName()); 426 assertEquals(intent.getIntExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, 0), 427 AccountManager.ERROR_CODE_USER_RESTRICTED); 428 } 429 430 @SmallTest 431 public void testStartAddAccountSessionUserCannotModifyAccountWithDPM() throws Exception { 432 unlockSystemUser(); 433 Bundle bundle = new Bundle(); 434 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 435 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 436 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 437 LocalServices.addService( 438 DevicePolicyManagerInternal.class, mMockDevicePolicyManagerInternal); 439 when(mMockDevicePolicyManagerInternal.createUserRestrictionSupportIntent( 440 anyInt(), anyString())).thenReturn(new Intent()); 441 when(mMockDevicePolicyManagerInternal.createShowAdminSupportIntent( 442 anyInt(), anyBoolean())).thenReturn(new Intent()); 443 444 mAms.startAddAccountSession( 445 mMockAccountManagerResponse, // response 446 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 447 "authTokenType", 448 null, // requiredFeatures 449 true, // expectActivityLaunch 450 null); // optionsIn 451 452 verify(mMockAccountManagerResponse).onError( 453 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 454 verify(mMockContext).startActivityAsUser(any(Intent.class), eq(UserHandle.SYSTEM)); 455 verify(mMockDevicePolicyManagerInternal).createUserRestrictionSupportIntent( 456 anyInt(), anyString()); 457 } 458 459 @SmallTest 460 public void testStartAddAccountSessionUserCannotModifyAccountForTypeNoDPM() throws Exception { 461 unlockSystemUser(); 462 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 463 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 464 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 465 466 mAms.startAddAccountSession( 467 mMockAccountManagerResponse, // response 468 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 469 "authTokenType", 470 null, // requiredFeatures 471 true, // expectActivityLaunch 472 null); // optionsIn 473 474 verify(mMockAccountManagerResponse).onError( 475 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 476 verify(mMockContext).startActivityAsUser(mIntentCaptor.capture(), eq(UserHandle.SYSTEM)); 477 478 // verify the intent for default CantAddAccountActivity is sent. 479 Intent intent = mIntentCaptor.getValue(); 480 assertEquals(intent.getComponent().getClassName(), CantAddAccountActivity.class.getName()); 481 assertEquals(intent.getIntExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, 0), 482 AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE); 483 } 484 485 @SmallTest 486 public void testStartAddAccountSessionUserCannotModifyAccountForTypeWithDPM() throws Exception { 487 unlockSystemUser(); 488 when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn( 489 mMockDevicePolicyManager); 490 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 491 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 492 493 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 494 LocalServices.addService( 495 DevicePolicyManagerInternal.class, mMockDevicePolicyManagerInternal); 496 when(mMockDevicePolicyManagerInternal.createUserRestrictionSupportIntent( 497 anyInt(), anyString())).thenReturn(new Intent()); 498 when(mMockDevicePolicyManagerInternal.createShowAdminSupportIntent( 499 anyInt(), anyBoolean())).thenReturn(new Intent()); 500 501 mAms.startAddAccountSession( 502 mMockAccountManagerResponse, // response 503 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 504 "authTokenType", 505 null, // requiredFeatures 506 true, // expectActivityLaunch 507 null); // optionsIn 508 509 verify(mMockAccountManagerResponse).onError( 510 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 511 verify(mMockContext).startActivityAsUser(any(Intent.class), eq(UserHandle.SYSTEM)); 512 verify(mMockDevicePolicyManagerInternal).createShowAdminSupportIntent( 513 anyInt(), anyBoolean()); 514 } 515 516 @SmallTest 517 public void testStartAddAccountSessionSuccessWithoutPasswordForwarding() throws Exception { 518 unlockSystemUser(); 519 when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn( 520 PackageManager.PERMISSION_DENIED); 521 522 final CountDownLatch latch = new CountDownLatch(1); 523 Response response = new Response(latch, mMockAccountManagerResponse); 524 Bundle options = createOptionsWithAccountName( 525 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 526 mAms.startAddAccountSession( 527 response, // response 528 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 529 "authTokenType", 530 null, // requiredFeatures 531 false, // expectActivityLaunch 532 options); // optionsIn 533 waitForLatch(latch); 534 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 535 Bundle result = mBundleCaptor.getValue(); 536 Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); 537 assertNotNull(sessionBundle); 538 // Assert that session bundle is encrypted and hence data not visible. 539 assertNull(sessionBundle.getString(AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1)); 540 // Assert password is not returned 541 assertNull(result.getString(AccountManager.KEY_PASSWORD)); 542 assertNull(result.getString(AccountManager.KEY_AUTHTOKEN, null)); 543 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN, 544 result.getString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN)); 545 } 546 547 @SmallTest 548 public void testStartAddAccountSessionSuccessWithPasswordForwarding() throws Exception { 549 unlockSystemUser(); 550 when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn( 551 PackageManager.PERMISSION_GRANTED); 552 553 final CountDownLatch latch = new CountDownLatch(1); 554 Response response = new Response(latch, mMockAccountManagerResponse); 555 Bundle options = createOptionsWithAccountName( 556 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 557 mAms.startAddAccountSession( 558 response, // response 559 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 560 "authTokenType", 561 null, // requiredFeatures 562 false, // expectActivityLaunch 563 options); // optionsIn 564 565 waitForLatch(latch); 566 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 567 Bundle result = mBundleCaptor.getValue(); 568 Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); 569 assertNotNull(sessionBundle); 570 // Assert that session bundle is encrypted and hence data not visible. 571 assertNull(sessionBundle.getString(AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1)); 572 // Assert password is returned 573 assertEquals(result.getString(AccountManager.KEY_PASSWORD), 574 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 575 assertNull(result.getString(AccountManager.KEY_AUTHTOKEN)); 576 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN, 577 result.getString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN)); 578 } 579 580 @SmallTest 581 public void testStartAddAccountSessionReturnWithInvalidIntent() throws Exception { 582 unlockSystemUser(); 583 ResolveInfo resolveInfo = new ResolveInfo(); 584 resolveInfo.activityInfo = new ActivityInfo(); 585 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 586 when(mMockPackageManager.resolveActivityAsUser( 587 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 588 when(mMockPackageManager.checkSignatures( 589 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 590 591 final CountDownLatch latch = new CountDownLatch(1); 592 Response response = new Response(latch, mMockAccountManagerResponse); 593 Bundle options = createOptionsWithAccountName( 594 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE); 595 596 mAms.startAddAccountSession( 597 response, // response 598 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 599 "authTokenType", 600 null, // requiredFeatures 601 true, // expectActivityLaunch 602 options); // optionsIn 603 waitForLatch(latch); 604 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 605 verify(mMockAccountManagerResponse).onError( 606 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 607 } 608 609 @SmallTest 610 public void testStartAddAccountSessionReturnWithValidIntent() throws Exception { 611 unlockSystemUser(); 612 ResolveInfo resolveInfo = new ResolveInfo(); 613 resolveInfo.activityInfo = new ActivityInfo(); 614 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 615 when(mMockPackageManager.resolveActivityAsUser( 616 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 617 when(mMockPackageManager.checkSignatures( 618 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 619 620 final CountDownLatch latch = new CountDownLatch(1); 621 Response response = new Response(latch, mMockAccountManagerResponse); 622 Bundle options = createOptionsWithAccountName( 623 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE); 624 625 mAms.startAddAccountSession( 626 response, // response 627 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 628 "authTokenType", 629 null, // requiredFeatures 630 true, // expectActivityLaunch 631 options); // optionsIn 632 waitForLatch(latch); 633 634 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 635 Bundle result = mBundleCaptor.getValue(); 636 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 637 assertNotNull(intent); 638 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 639 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 640 } 641 642 @SmallTest 643 public void testStartAddAccountSessionError() throws Exception { 644 unlockSystemUser(); 645 Bundle options = createOptionsWithAccountName( 646 AccountManagerServiceTestFixtures.ACCOUNT_NAME_ERROR); 647 options.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_INVALID_RESPONSE); 648 options.putString(AccountManager.KEY_ERROR_MESSAGE, 649 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 650 651 final CountDownLatch latch = new CountDownLatch(1); 652 Response response = new Response(latch, mMockAccountManagerResponse); 653 mAms.startAddAccountSession( 654 response, // response 655 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 656 "authTokenType", 657 null, // requiredFeatures 658 false, // expectActivityLaunch 659 options); // optionsIn 660 661 waitForLatch(latch); 662 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 663 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 664 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 665 } 666 667 @SmallTest 668 public void testStartUpdateCredentialsSessionWithNullResponse() throws Exception { 669 unlockSystemUser(); 670 try { 671 mAms.startUpdateCredentialsSession( 672 null, // response 673 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 674 "authTokenType", 675 true, // expectActivityLaunch 676 null); // optionsIn 677 fail("IllegalArgumentException expected. But no exception was thrown."); 678 } catch (IllegalArgumentException e) { 679 // IllegalArgumentException is expected. 680 } 681 } 682 683 @SmallTest 684 public void testStartUpdateCredentialsSessionWithNullAccount() throws Exception { 685 unlockSystemUser(); 686 try { 687 mAms.startUpdateCredentialsSession( 688 mMockAccountManagerResponse, // response 689 null, 690 "authTokenType", 691 true, // expectActivityLaunch 692 null); // optionsIn 693 fail("IllegalArgumentException expected. But no exception was thrown."); 694 } catch (IllegalArgumentException e) { 695 // IllegalArgumentException is expected. 696 } 697 } 698 699 @SmallTest 700 public void testStartUpdateCredentialsSessionSuccessWithoutPasswordForwarding() 701 throws Exception { 702 unlockSystemUser(); 703 when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn( 704 PackageManager.PERMISSION_DENIED); 705 706 final CountDownLatch latch = new CountDownLatch(1); 707 Response response = new Response(latch, mMockAccountManagerResponse); 708 Bundle options = createOptionsWithAccountName( 709 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 710 mAms.startUpdateCredentialsSession( 711 response, // response 712 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 713 "authTokenType", 714 false, // expectActivityLaunch 715 options); // optionsIn 716 waitForLatch(latch); 717 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 718 Bundle result = mBundleCaptor.getValue(); 719 Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); 720 assertNotNull(sessionBundle); 721 // Assert that session bundle is encrypted and hence data not visible. 722 assertNull(sessionBundle.getString(AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1)); 723 // Assert password is not returned 724 assertNull(result.getString(AccountManager.KEY_PASSWORD)); 725 assertNull(result.getString(AccountManager.KEY_AUTHTOKEN, null)); 726 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN, 727 result.getString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN)); 728 } 729 730 @SmallTest 731 public void testStartUpdateCredentialsSessionSuccessWithPasswordForwarding() throws Exception { 732 unlockSystemUser(); 733 when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn( 734 PackageManager.PERMISSION_GRANTED); 735 736 final CountDownLatch latch = new CountDownLatch(1); 737 Response response = new Response(latch, mMockAccountManagerResponse); 738 Bundle options = createOptionsWithAccountName( 739 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 740 mAms.startUpdateCredentialsSession( 741 response, // response 742 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 743 "authTokenType", 744 false, // expectActivityLaunch 745 options); // optionsIn 746 747 waitForLatch(latch); 748 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 749 Bundle result = mBundleCaptor.getValue(); 750 Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); 751 assertNotNull(sessionBundle); 752 // Assert that session bundle is encrypted and hence data not visible. 753 assertNull(sessionBundle.getString(AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1)); 754 // Assert password is returned 755 assertEquals(result.getString(AccountManager.KEY_PASSWORD), 756 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 757 assertNull(result.getString(AccountManager.KEY_AUTHTOKEN)); 758 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN, 759 result.getString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN)); 760 } 761 762 @SmallTest 763 public void testStartUpdateCredentialsSessionReturnWithInvalidIntent() throws Exception { 764 unlockSystemUser(); 765 ResolveInfo resolveInfo = new ResolveInfo(); 766 resolveInfo.activityInfo = new ActivityInfo(); 767 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 768 when(mMockPackageManager.resolveActivityAsUser( 769 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 770 when(mMockPackageManager.checkSignatures( 771 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 772 773 final CountDownLatch latch = new CountDownLatch(1); 774 Response response = new Response(latch, mMockAccountManagerResponse); 775 Bundle options = createOptionsWithAccountName( 776 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE); 777 778 mAms.startUpdateCredentialsSession( 779 response, // response 780 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 781 "authTokenType", 782 true, // expectActivityLaunch 783 options); // optionsIn 784 785 waitForLatch(latch); 786 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 787 verify(mMockAccountManagerResponse).onError( 788 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 789 } 790 791 @SmallTest 792 public void testStartUpdateCredentialsSessionReturnWithValidIntent() throws Exception { 793 unlockSystemUser(); 794 ResolveInfo resolveInfo = new ResolveInfo(); 795 resolveInfo.activityInfo = new ActivityInfo(); 796 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 797 when(mMockPackageManager.resolveActivityAsUser( 798 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 799 when(mMockPackageManager.checkSignatures( 800 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 801 802 final CountDownLatch latch = new CountDownLatch(1); 803 Response response = new Response(latch, mMockAccountManagerResponse); 804 Bundle options = createOptionsWithAccountName( 805 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE); 806 807 mAms.startUpdateCredentialsSession( 808 response, // response 809 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 810 "authTokenType", 811 true, // expectActivityLaunch 812 options); // optionsIn 813 814 waitForLatch(latch); 815 816 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 817 Bundle result = mBundleCaptor.getValue(); 818 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 819 assertNotNull(intent); 820 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 821 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 822 } 823 824 @SmallTest 825 public void testStartUpdateCredentialsSessionError() throws Exception { 826 unlockSystemUser(); 827 Bundle options = createOptionsWithAccountName( 828 AccountManagerServiceTestFixtures.ACCOUNT_NAME_ERROR); 829 options.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_INVALID_RESPONSE); 830 options.putString(AccountManager.KEY_ERROR_MESSAGE, 831 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 832 833 final CountDownLatch latch = new CountDownLatch(1); 834 Response response = new Response(latch, mMockAccountManagerResponse); 835 836 mAms.startUpdateCredentialsSession( 837 response, // response 838 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, 839 "authTokenType", 840 true, // expectActivityLaunch 841 options); // optionsIn 842 843 waitForLatch(latch); 844 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 845 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 846 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 847 } 848 849 @SmallTest 850 public void testFinishSessionAsUserWithNullResponse() throws Exception { 851 unlockSystemUser(); 852 try { 853 mAms.finishSessionAsUser( 854 null, // response 855 createEncryptedSessionBundle( 856 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 857 false, // expectActivityLaunch 858 createAppBundle(), // appInfo 859 UserHandle.USER_SYSTEM); 860 fail("IllegalArgumentException expected. But no exception was thrown."); 861 } catch (IllegalArgumentException e) { 862 // IllegalArgumentException is expected. 863 } 864 } 865 866 @SmallTest 867 public void testFinishSessionAsUserWithNullSessionBundle() throws Exception { 868 unlockSystemUser(); 869 try { 870 mAms.finishSessionAsUser( 871 mMockAccountManagerResponse, // response 872 null, // sessionBundle 873 false, // expectActivityLaunch 874 createAppBundle(), // appInfo 875 UserHandle.USER_SYSTEM); 876 fail("IllegalArgumentException expected. But no exception was thrown."); 877 } catch (IllegalArgumentException e) { 878 // IllegalArgumentException is expected. 879 } 880 } 881 882 @SmallTest 883 public void testFinishSessionAsUserUserCannotModifyAccountNoDPM() throws Exception { 884 unlockSystemUser(); 885 Bundle bundle = new Bundle(); 886 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 887 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 888 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 889 890 mAms.finishSessionAsUser( 891 mMockAccountManagerResponse, // response 892 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 893 false, // expectActivityLaunch 894 createAppBundle(), // appInfo 895 2); // fake user id 896 897 verify(mMockAccountManagerResponse).onError( 898 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 899 verify(mMockContext).startActivityAsUser(mIntentCaptor.capture(), eq(UserHandle.of(2))); 900 901 // verify the intent for default CantAddAccountActivity is sent. 902 Intent intent = mIntentCaptor.getValue(); 903 assertEquals(intent.getComponent().getClassName(), CantAddAccountActivity.class.getName()); 904 assertEquals(intent.getIntExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, 0), 905 AccountManager.ERROR_CODE_USER_RESTRICTED); 906 } 907 908 @SmallTest 909 public void testFinishSessionAsUserUserCannotModifyAccountWithDPM() throws Exception { 910 unlockSystemUser(); 911 Bundle bundle = new Bundle(); 912 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 913 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 914 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 915 LocalServices.addService( 916 DevicePolicyManagerInternal.class, mMockDevicePolicyManagerInternal); 917 when(mMockDevicePolicyManagerInternal.createUserRestrictionSupportIntent( 918 anyInt(), anyString())).thenReturn(new Intent()); 919 when(mMockDevicePolicyManagerInternal.createShowAdminSupportIntent( 920 anyInt(), anyBoolean())).thenReturn(new Intent()); 921 922 mAms.finishSessionAsUser( 923 mMockAccountManagerResponse, // response 924 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 925 false, // expectActivityLaunch 926 createAppBundle(), // appInfo 927 2); // fake user id 928 929 verify(mMockAccountManagerResponse).onError( 930 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 931 verify(mMockContext).startActivityAsUser(any(Intent.class), eq(UserHandle.of(2))); 932 verify(mMockDevicePolicyManagerInternal).createUserRestrictionSupportIntent( 933 anyInt(), anyString()); 934 } 935 936 @SmallTest 937 public void testFinishSessionAsUserWithBadSessionBundle() throws Exception { 938 unlockSystemUser(); 939 940 Bundle badSessionBundle = new Bundle(); 941 badSessionBundle.putString("any", "any"); 942 mAms.finishSessionAsUser( 943 mMockAccountManagerResponse, // response 944 badSessionBundle, // sessionBundle 945 false, // expectActivityLaunch 946 createAppBundle(), // appInfo 947 2); // fake user id 948 949 verify(mMockAccountManagerResponse).onError( 950 eq(AccountManager.ERROR_CODE_BAD_REQUEST), anyString()); 951 } 952 953 @SmallTest 954 public void testFinishSessionAsUserWithBadAccountType() throws Exception { 955 unlockSystemUser(); 956 957 mAms.finishSessionAsUser( 958 mMockAccountManagerResponse, // response 959 createEncryptedSessionBundleWithNoAccountType( 960 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 961 false, // expectActivityLaunch 962 createAppBundle(), // appInfo 963 2); // fake user id 964 965 verify(mMockAccountManagerResponse).onError( 966 eq(AccountManager.ERROR_CODE_BAD_ARGUMENTS), anyString()); 967 } 968 969 @SmallTest 970 public void testFinishSessionAsUserUserCannotModifyAccountForTypeNoDPM() throws Exception { 971 unlockSystemUser(); 972 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 973 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 974 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 975 976 mAms.finishSessionAsUser( 977 mMockAccountManagerResponse, // response 978 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 979 false, // expectActivityLaunch 980 createAppBundle(), // appInfo 981 2); // fake user id 982 983 verify(mMockAccountManagerResponse).onError( 984 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 985 verify(mMockContext).startActivityAsUser(mIntentCaptor.capture(), eq(UserHandle.of(2))); 986 987 // verify the intent for default CantAddAccountActivity is sent. 988 Intent intent = mIntentCaptor.getValue(); 989 assertEquals(intent.getComponent().getClassName(), CantAddAccountActivity.class.getName()); 990 assertEquals(intent.getIntExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, 0), 991 AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE); 992 } 993 994 @SmallTest 995 public void testFinishSessionAsUserUserCannotModifyAccountForTypeWithDPM() throws Exception { 996 unlockSystemUser(); 997 when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn( 998 mMockDevicePolicyManager); 999 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 1000 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 1001 1002 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 1003 LocalServices.addService( 1004 DevicePolicyManagerInternal.class, mMockDevicePolicyManagerInternal); 1005 when(mMockDevicePolicyManagerInternal.createUserRestrictionSupportIntent( 1006 anyInt(), anyString())).thenReturn(new Intent()); 1007 when(mMockDevicePolicyManagerInternal.createShowAdminSupportIntent( 1008 anyInt(), anyBoolean())).thenReturn(new Intent()); 1009 1010 mAms.finishSessionAsUser( 1011 mMockAccountManagerResponse, // response 1012 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 1013 false, // expectActivityLaunch 1014 createAppBundle(), // appInfo 1015 2); // fake user id 1016 1017 verify(mMockAccountManagerResponse).onError( 1018 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 1019 verify(mMockContext).startActivityAsUser(any(Intent.class), eq(UserHandle.of(2))); 1020 verify(mMockDevicePolicyManagerInternal).createShowAdminSupportIntent( 1021 anyInt(), anyBoolean()); 1022 } 1023 1024 @SmallTest 1025 public void testFinishSessionAsUserSuccess() throws Exception { 1026 unlockSystemUser(); 1027 final CountDownLatch latch = new CountDownLatch(1); 1028 Response response = new Response(latch, mMockAccountManagerResponse); 1029 mAms.finishSessionAsUser( 1030 response, // response 1031 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 1032 false, // expectActivityLaunch 1033 createAppBundle(), // appInfo 1034 UserHandle.USER_SYSTEM); 1035 1036 waitForLatch(latch); 1037 // Verify notification is cancelled 1038 verify(mMockNotificationManager).cancelNotificationWithTag( 1039 anyString(), anyString(), anyInt(), anyInt()); 1040 1041 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1042 Bundle result = mBundleCaptor.getValue(); 1043 Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); 1044 assertNotNull(sessionBundle); 1045 // Assert that session bundle is decrypted and hence data is visible. 1046 assertEquals(AccountManagerServiceTestFixtures.SESSION_DATA_VALUE_1, 1047 sessionBundle.getString(AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1)); 1048 // Assert finishSessionAsUser added calling uid and pid into the sessionBundle 1049 assertTrue(sessionBundle.containsKey(AccountManager.KEY_CALLER_UID)); 1050 assertTrue(sessionBundle.containsKey(AccountManager.KEY_CALLER_PID)); 1051 // Assert App bundle data overrides sessionBundle data 1052 assertEquals(sessionBundle.getString( 1053 AccountManager.KEY_ANDROID_PACKAGE_NAME), "APCT.package"); 1054 1055 // Verify response data 1056 assertNull(result.getString(AccountManager.KEY_AUTHTOKEN, null)); 1057 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_NAME, 1058 result.getString(AccountManager.KEY_ACCOUNT_NAME)); 1059 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 1060 result.getString(AccountManager.KEY_ACCOUNT_TYPE)); 1061 } 1062 1063 @SmallTest 1064 public void testFinishSessionAsUserReturnWithInvalidIntent() throws Exception { 1065 unlockSystemUser(); 1066 ResolveInfo resolveInfo = new ResolveInfo(); 1067 resolveInfo.activityInfo = new ActivityInfo(); 1068 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1069 when(mMockPackageManager.resolveActivityAsUser( 1070 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1071 when(mMockPackageManager.checkSignatures( 1072 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 1073 1074 final CountDownLatch latch = new CountDownLatch(1); 1075 Response response = new Response(latch, mMockAccountManagerResponse); 1076 1077 mAms.finishSessionAsUser( 1078 response, // response 1079 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE), 1080 true, // expectActivityLaunch 1081 createAppBundle(), // appInfo 1082 UserHandle.USER_SYSTEM); 1083 1084 waitForLatch(latch); 1085 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1086 verify(mMockAccountManagerResponse).onError( 1087 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 1088 } 1089 1090 @SmallTest 1091 public void testFinishSessionAsUserReturnWithValidIntent() throws Exception { 1092 unlockSystemUser(); 1093 ResolveInfo resolveInfo = new ResolveInfo(); 1094 resolveInfo.activityInfo = new ActivityInfo(); 1095 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1096 when(mMockPackageManager.resolveActivityAsUser( 1097 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1098 when(mMockPackageManager.checkSignatures( 1099 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 1100 1101 final CountDownLatch latch = new CountDownLatch(1); 1102 Response response = new Response(latch, mMockAccountManagerResponse); 1103 1104 mAms.finishSessionAsUser( 1105 response, // response 1106 createEncryptedSessionBundle(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE), 1107 true, // expectActivityLaunch 1108 createAppBundle(), // appInfo 1109 UserHandle.USER_SYSTEM); 1110 1111 waitForLatch(latch); 1112 1113 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1114 Bundle result = mBundleCaptor.getValue(); 1115 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 1116 assertNotNull(intent); 1117 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 1118 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 1119 } 1120 1121 @SmallTest 1122 public void testFinishSessionAsUserError() throws Exception { 1123 unlockSystemUser(); 1124 Bundle sessionBundle = createEncryptedSessionBundleWithError( 1125 AccountManagerServiceTestFixtures.ACCOUNT_NAME_ERROR); 1126 1127 final CountDownLatch latch = new CountDownLatch(1); 1128 Response response = new Response(latch, mMockAccountManagerResponse); 1129 1130 mAms.finishSessionAsUser( 1131 response, // response 1132 sessionBundle, 1133 false, // expectActivityLaunch 1134 createAppBundle(), // appInfo 1135 UserHandle.USER_SYSTEM); 1136 1137 waitForLatch(latch); 1138 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 1139 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 1140 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1141 } 1142 1143 @SmallTest 1144 public void testIsCredentialsUpdatedSuggestedWithNullResponse() throws Exception { 1145 unlockSystemUser(); 1146 try { 1147 mAms.isCredentialsUpdateSuggested( 1148 null, // response 1149 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1150 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 1151 fail("IllegalArgumentException expected. But no exception was thrown."); 1152 } catch (IllegalArgumentException e) { 1153 // IllegalArgumentException is expected. 1154 } 1155 } 1156 1157 @SmallTest 1158 public void testIsCredentialsUpdatedSuggestedWithNullAccount() throws Exception { 1159 unlockSystemUser(); 1160 try { 1161 mAms.isCredentialsUpdateSuggested( 1162 mMockAccountManagerResponse, 1163 null, // account 1164 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 1165 fail("IllegalArgumentException expected. But no exception was thrown."); 1166 } catch (IllegalArgumentException e) { 1167 // IllegalArgumentException is expected. 1168 } 1169 } 1170 1171 @SmallTest 1172 public void testIsCredentialsUpdatedSuggestedWithEmptyStatusToken() throws Exception { 1173 unlockSystemUser(); 1174 try { 1175 mAms.isCredentialsUpdateSuggested( 1176 mMockAccountManagerResponse, 1177 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1178 null); 1179 fail("IllegalArgumentException expected. But no exception was thrown."); 1180 } catch (IllegalArgumentException e) { 1181 // IllegalArgumentException is expected. 1182 } 1183 } 1184 1185 @SmallTest 1186 public void testIsCredentialsUpdatedSuggestedError() throws Exception { 1187 unlockSystemUser(); 1188 final CountDownLatch latch = new CountDownLatch(1); 1189 Response response = new Response(latch, mMockAccountManagerResponse); 1190 1191 mAms.isCredentialsUpdateSuggested( 1192 response, 1193 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, 1194 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 1195 1196 waitForLatch(latch); 1197 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 1198 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 1199 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1200 } 1201 1202 @SmallTest 1203 public void testIsCredentialsUpdatedSuggestedSuccess() throws Exception { 1204 unlockSystemUser(); 1205 final CountDownLatch latch = new CountDownLatch(1); 1206 Response response = new Response(latch, mMockAccountManagerResponse); 1207 1208 mAms.isCredentialsUpdateSuggested( 1209 response, 1210 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1211 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 1212 1213 waitForLatch(latch); 1214 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1215 Bundle result = mBundleCaptor.getValue(); 1216 boolean needUpdate = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); 1217 assertTrue(needUpdate); 1218 } 1219 1220 @SmallTest 1221 public void testHasFeaturesWithNullResponse() throws Exception { 1222 unlockSystemUser(); 1223 try { 1224 mAms.hasFeatures( 1225 null, // response 1226 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1227 new String[] {"feature1", "feature2"}, // features 1228 "testPackage"); // opPackageName 1229 fail("IllegalArgumentException expected. But no exception was thrown."); 1230 } catch (IllegalArgumentException e) { 1231 // IllegalArgumentException is expected. 1232 } 1233 } 1234 1235 @SmallTest 1236 public void testHasFeaturesWithNullAccount() throws Exception { 1237 unlockSystemUser(); 1238 try { 1239 mAms.hasFeatures( 1240 mMockAccountManagerResponse, // response 1241 null, // account 1242 new String[] {"feature1", "feature2"}, // features 1243 "testPackage"); // opPackageName 1244 fail("IllegalArgumentException expected. But no exception was thrown."); 1245 } catch (IllegalArgumentException e) { 1246 // IllegalArgumentException is expected. 1247 } 1248 } 1249 1250 @SmallTest 1251 public void testHasFeaturesWithNullFeature() throws Exception { 1252 unlockSystemUser(); 1253 try { 1254 mAms.hasFeatures( 1255 mMockAccountManagerResponse, // response 1256 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, // account 1257 null, // features 1258 "testPackage"); // opPackageName 1259 fail("IllegalArgumentException expected. But no exception was thrown."); 1260 } catch (IllegalArgumentException e) { 1261 // IllegalArgumentException is expected. 1262 } 1263 } 1264 1265 @SmallTest 1266 public void testHasFeaturesReturnNullResult() throws Exception { 1267 unlockSystemUser(); 1268 final CountDownLatch latch = new CountDownLatch(1); 1269 Response response = new Response(latch, mMockAccountManagerResponse); 1270 mAms.hasFeatures( 1271 response, // response 1272 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, // account 1273 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, // features 1274 "testPackage"); // opPackageName 1275 waitForLatch(latch); 1276 verify(mMockAccountManagerResponse).onError( 1277 eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString()); 1278 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1279 } 1280 1281 @SmallTest 1282 public void testHasFeaturesSuccess() throws Exception { 1283 unlockSystemUser(); 1284 final CountDownLatch latch = new CountDownLatch(1); 1285 Response response = new Response(latch, mMockAccountManagerResponse); 1286 mAms.hasFeatures( 1287 response, // response 1288 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, // account 1289 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, // features 1290 "testPackage"); // opPackageName 1291 waitForLatch(latch); 1292 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1293 Bundle result = mBundleCaptor.getValue(); 1294 boolean hasFeatures = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); 1295 assertTrue(hasFeatures); 1296 } 1297 1298 @SmallTest 1299 public void testRemoveAccountAsUserWithNullResponse() throws Exception { 1300 unlockSystemUser(); 1301 try { 1302 mAms.removeAccountAsUser( 1303 null, // response 1304 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1305 true, // expectActivityLaunch 1306 UserHandle.USER_SYSTEM); 1307 fail("IllegalArgumentException expected. But no exception was thrown."); 1308 } catch (IllegalArgumentException e) { 1309 // IllegalArgumentException is expected. 1310 } 1311 } 1312 1313 @SmallTest 1314 public void testRemoveAccountAsUserWithNullAccount() throws Exception { 1315 unlockSystemUser(); 1316 try { 1317 mAms.removeAccountAsUser( 1318 mMockAccountManagerResponse, // response 1319 null, // account 1320 true, // expectActivityLaunch 1321 UserHandle.USER_SYSTEM); 1322 fail("IllegalArgumentException expected. But no exception was thrown."); 1323 } catch (IllegalArgumentException e) { 1324 // IllegalArgumentException is expected. 1325 } 1326 } 1327 1328 @SmallTest 1329 public void testRemoveAccountAsUserAccountNotManagedByCaller() throws Exception { 1330 unlockSystemUser(); 1331 when(mMockPackageManager.checkSignatures(anyInt(), anyInt())) 1332 .thenReturn(PackageManager.SIGNATURE_NO_MATCH); 1333 try { 1334 mAms.removeAccountAsUser( 1335 mMockAccountManagerResponse, // response 1336 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1337 true, // expectActivityLaunch 1338 UserHandle.USER_SYSTEM); 1339 fail("SecurityException expected. But no exception was thrown."); 1340 } catch (SecurityException e) { 1341 // SecurityException is expected. 1342 } 1343 } 1344 1345 @SmallTest 1346 public void testRemoveAccountAsUserUserCannotModifyAccount() throws Exception { 1347 unlockSystemUser(); 1348 Bundle bundle = new Bundle(); 1349 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 1350 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 1351 1352 final CountDownLatch latch = new CountDownLatch(1); 1353 Response response = new Response(latch, mMockAccountManagerResponse); 1354 1355 mAms.removeAccountAsUser( 1356 response, // response 1357 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1358 true, // expectActivityLaunch 1359 UserHandle.USER_SYSTEM); 1360 waitForLatch(latch); 1361 verify(mMockAccountManagerResponse).onError( 1362 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 1363 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1364 } 1365 1366 @SmallTest 1367 public void testRemoveAccountAsUserUserCannotModifyAccountType() throws Exception { 1368 unlockSystemUser(); 1369 when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn( 1370 mMockDevicePolicyManager); 1371 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 1372 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 1373 1374 final CountDownLatch latch = new CountDownLatch(1); 1375 Response response = new Response(latch, mMockAccountManagerResponse); 1376 1377 mAms.removeAccountAsUser( 1378 response, // response 1379 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1380 true, // expectActivityLaunch 1381 UserHandle.USER_SYSTEM); 1382 waitForLatch(latch); 1383 verify(mMockAccountManagerResponse).onError( 1384 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 1385 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1386 } 1387 1388 @SmallTest 1389 public void testRemoveAccountAsUserRemovalAllowed() throws Exception { 1390 unlockSystemUser(); 1391 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p1", null); 1392 Account[] addedAccounts = 1393 mAms.getAccounts(UserHandle.USER_SYSTEM, mContext.getOpPackageName()); 1394 assertEquals(1, addedAccounts.length); 1395 1396 final CountDownLatch latch = new CountDownLatch(1); 1397 Response response = new Response(latch, mMockAccountManagerResponse); 1398 1399 mAms.removeAccountAsUser( 1400 response, // response 1401 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1402 true, // expectActivityLaunch 1403 UserHandle.USER_SYSTEM); 1404 waitForLatch(latch); 1405 1406 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1407 Bundle result = mBundleCaptor.getValue(); 1408 boolean allowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); 1409 assertTrue(allowed); 1410 Account[] accounts = mAms.getAccounts(UserHandle.USER_SYSTEM, mContext.getOpPackageName()); 1411 assertEquals(0, accounts.length); 1412 } 1413 1414 @SmallTest 1415 public void testRemoveAccountAsUserRemovalNotAllowed() throws Exception { 1416 unlockSystemUser(); 1417 1418 final CountDownLatch latch = new CountDownLatch(1); 1419 Response response = new Response(latch, mMockAccountManagerResponse); 1420 1421 mAms.removeAccountAsUser( 1422 response, // response 1423 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, 1424 true, // expectActivityLaunch 1425 UserHandle.USER_SYSTEM); 1426 waitForLatch(latch); 1427 1428 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1429 Bundle result = mBundleCaptor.getValue(); 1430 boolean allowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); 1431 assertFalse(allowed); 1432 } 1433 1434 @SmallTest 1435 public void testRemoveAccountAsUserReturnWithValidIntent() throws Exception { 1436 unlockSystemUser(); 1437 ResolveInfo resolveInfo = new ResolveInfo(); 1438 resolveInfo.activityInfo = new ActivityInfo(); 1439 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1440 when(mMockPackageManager.resolveActivityAsUser( 1441 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1442 when(mMockPackageManager.checkSignatures( 1443 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 1444 1445 final CountDownLatch latch = new CountDownLatch(1); 1446 Response response = new Response(latch, mMockAccountManagerResponse); 1447 1448 mAms.removeAccountAsUser( 1449 response, // response 1450 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 1451 true, // expectActivityLaunch 1452 UserHandle.USER_SYSTEM); 1453 waitForLatch(latch); 1454 1455 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1456 Bundle result = mBundleCaptor.getValue(); 1457 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 1458 assertNotNull(intent); 1459 } 1460 1461 @SmallTest 1462 public void testGetAuthTokenLabelWithNullAccountType() throws Exception { 1463 unlockSystemUser(); 1464 try { 1465 mAms.getAuthTokenLabel( 1466 mMockAccountManagerResponse, // response 1467 null, // accountType 1468 "authTokenType"); 1469 fail("IllegalArgumentException expected. But no exception was thrown."); 1470 } catch (IllegalArgumentException e) { 1471 // IllegalArgumentException is expected. 1472 } 1473 } 1474 1475 @SmallTest 1476 public void testGetAuthTokenLabelWithNullAuthTokenType() throws Exception { 1477 unlockSystemUser(); 1478 try { 1479 mAms.getAuthTokenLabel( 1480 mMockAccountManagerResponse, // response 1481 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1482 null); // authTokenType 1483 fail("IllegalArgumentException expected. But no exception was thrown."); 1484 } catch (IllegalArgumentException e) { 1485 // IllegalArgumentException is expected. 1486 } 1487 } 1488 1489 @SmallTest 1490 public void testGetAuthTokenWithNullResponse() throws Exception { 1491 unlockSystemUser(); 1492 try { 1493 mAms.getAuthToken( 1494 null, // response 1495 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1496 "authTokenType", // authTokenType 1497 true, // notifyOnAuthFailure 1498 true, // expectActivityLaunch 1499 createGetAuthTokenOptions()); 1500 fail("IllegalArgumentException expected. But no exception was thrown."); 1501 } catch (IllegalArgumentException e) { 1502 // IllegalArgumentException is expected. 1503 } 1504 } 1505 1506 @SmallTest 1507 public void testGetAuthTokenWithNullAccount() throws Exception { 1508 unlockSystemUser(); 1509 final CountDownLatch latch = new CountDownLatch(1); 1510 Response response = new Response(latch, mMockAccountManagerResponse); 1511 mAms.getAuthToken( 1512 response, // response 1513 null, // account 1514 "authTokenType", // authTokenType 1515 true, // notifyOnAuthFailure 1516 true, // expectActivityLaunch 1517 createGetAuthTokenOptions()); 1518 waitForLatch(latch); 1519 1520 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1521 verify(mMockAccountManagerResponse).onError( 1522 eq(AccountManager.ERROR_CODE_BAD_ARGUMENTS), anyString()); 1523 } 1524 1525 @SmallTest 1526 public void testGetAuthTokenWithNullAuthTokenType() throws Exception { 1527 unlockSystemUser(); 1528 final CountDownLatch latch = new CountDownLatch(1); 1529 Response response = new Response(latch, mMockAccountManagerResponse); 1530 mAms.getAuthToken( 1531 response, // response 1532 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1533 null, // authTokenType 1534 true, // notifyOnAuthFailure 1535 true, // expectActivityLaunch 1536 createGetAuthTokenOptions()); 1537 waitForLatch(latch); 1538 1539 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1540 verify(mMockAccountManagerResponse).onError( 1541 eq(AccountManager.ERROR_CODE_BAD_ARGUMENTS), anyString()); 1542 } 1543 1544 @SmallTest 1545 public void testGetAuthTokenWithInvalidPackage() throws Exception { 1546 unlockSystemUser(); 1547 String[] list = new String[]{"test"}; 1548 when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list); 1549 try { 1550 mAms.getAuthToken( 1551 mMockAccountManagerResponse, // response 1552 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1553 "authTokenType", // authTokenType 1554 true, // notifyOnAuthFailure 1555 true, // expectActivityLaunch 1556 createGetAuthTokenOptions()); 1557 fail("SecurityException expected. But no exception was thrown."); 1558 } catch (SecurityException e) { 1559 // SecurityException is expected. 1560 } 1561 } 1562 1563 @SmallTest 1564 public void testGetAuthTokenFromInternal() throws Exception { 1565 unlockSystemUser(); 1566 when(mMockContext.createPackageContextAsUser( 1567 anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext); 1568 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); 1569 String[] list = new String[]{AccountManagerServiceTestFixtures.CALLER_PACKAGE}; 1570 when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list); 1571 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null); 1572 1573 mAms.setAuthToken(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1574 "authTokenType", AccountManagerServiceTestFixtures.AUTH_TOKEN); 1575 final CountDownLatch latch = new CountDownLatch(1); 1576 Response response = new Response(latch, mMockAccountManagerResponse); 1577 mAms.getAuthToken( 1578 response, // response 1579 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1580 "authTokenType", // authTokenType 1581 true, // notifyOnAuthFailure 1582 true, // expectActivityLaunch 1583 createGetAuthTokenOptions()); 1584 waitForLatch(latch); 1585 1586 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1587 Bundle result = mBundleCaptor.getValue(); 1588 assertEquals(result.getString(AccountManager.KEY_AUTHTOKEN), 1589 AccountManagerServiceTestFixtures.AUTH_TOKEN); 1590 assertEquals(result.getString(AccountManager.KEY_ACCOUNT_NAME), 1591 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 1592 assertEquals(result.getString(AccountManager.KEY_ACCOUNT_TYPE), 1593 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 1594 } 1595 1596 @SmallTest 1597 public void testGetAuthTokenSuccess() throws Exception { 1598 unlockSystemUser(); 1599 when(mMockContext.createPackageContextAsUser( 1600 anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext); 1601 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); 1602 String[] list = new String[]{AccountManagerServiceTestFixtures.CALLER_PACKAGE}; 1603 when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list); 1604 1605 final CountDownLatch latch = new CountDownLatch(1); 1606 Response response = new Response(latch, mMockAccountManagerResponse); 1607 mAms.getAuthToken( 1608 response, // response 1609 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1610 "authTokenType", // authTokenType 1611 true, // notifyOnAuthFailure 1612 false, // expectActivityLaunch 1613 createGetAuthTokenOptions()); 1614 waitForLatch(latch); 1615 1616 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1617 Bundle result = mBundleCaptor.getValue(); 1618 assertEquals(result.getString(AccountManager.KEY_AUTHTOKEN), 1619 AccountManagerServiceTestFixtures.AUTH_TOKEN); 1620 assertEquals(result.getString(AccountManager.KEY_ACCOUNT_NAME), 1621 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 1622 assertEquals(result.getString(AccountManager.KEY_ACCOUNT_TYPE), 1623 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 1624 } 1625 1626 @SmallTest 1627 public void testGetAuthTokenReturnWithInvalidIntent() throws Exception { 1628 unlockSystemUser(); 1629 when(mMockContext.createPackageContextAsUser( 1630 anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext); 1631 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); 1632 String[] list = new String[]{AccountManagerServiceTestFixtures.CALLER_PACKAGE}; 1633 when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list); 1634 ResolveInfo resolveInfo = new ResolveInfo(); 1635 resolveInfo.activityInfo = new ActivityInfo(); 1636 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1637 when(mMockPackageManager.resolveActivityAsUser( 1638 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1639 when(mMockPackageManager.checkSignatures( 1640 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 1641 1642 final CountDownLatch latch = new CountDownLatch(1); 1643 Response response = new Response(latch, mMockAccountManagerResponse); 1644 mAms.getAuthToken( 1645 response, // response 1646 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 1647 "authTokenType", // authTokenType 1648 true, // notifyOnAuthFailure 1649 false, // expectActivityLaunch 1650 createGetAuthTokenOptions()); 1651 waitForLatch(latch); 1652 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1653 verify(mMockAccountManagerResponse).onError( 1654 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 1655 } 1656 1657 @SmallTest 1658 public void testGetAuthTokenReturnWithValidIntent() throws Exception { 1659 unlockSystemUser(); 1660 when(mMockContext.createPackageContextAsUser( 1661 anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext); 1662 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); 1663 String[] list = new String[]{AccountManagerServiceTestFixtures.CALLER_PACKAGE}; 1664 when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list); 1665 1666 ResolveInfo resolveInfo = new ResolveInfo(); 1667 resolveInfo.activityInfo = new ActivityInfo(); 1668 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1669 when(mMockPackageManager.resolveActivityAsUser( 1670 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1671 when(mMockPackageManager.checkSignatures( 1672 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 1673 1674 final CountDownLatch latch = new CountDownLatch(1); 1675 Response response = new Response(latch, mMockAccountManagerResponse); 1676 mAms.getAuthToken( 1677 response, // response 1678 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 1679 "authTokenType", // authTokenType 1680 false, // notifyOnAuthFailure 1681 true, // expectActivityLaunch 1682 createGetAuthTokenOptions()); 1683 waitForLatch(latch); 1684 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1685 Bundle result = mBundleCaptor.getValue(); 1686 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 1687 assertNotNull(intent); 1688 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 1689 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 1690 } 1691 1692 @SmallTest 1693 public void testGetAuthTokenError() throws Exception { 1694 unlockSystemUser(); 1695 when(mMockContext.createPackageContextAsUser( 1696 anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext); 1697 when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); 1698 String[] list = new String[]{AccountManagerServiceTestFixtures.CALLER_PACKAGE}; 1699 when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list); 1700 final CountDownLatch latch = new CountDownLatch(1); 1701 Response response = new Response(latch, mMockAccountManagerResponse); 1702 mAms.getAuthToken( 1703 response, // response 1704 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, 1705 "authTokenType", // authTokenType 1706 true, // notifyOnAuthFailure 1707 false, // expectActivityLaunch 1708 createGetAuthTokenOptions()); 1709 waitForLatch(latch); 1710 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 1711 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 1712 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1713 1714 } 1715 1716 @SmallTest 1717 public void testAddAccountAsUserWithNullResponse() throws Exception { 1718 unlockSystemUser(); 1719 try { 1720 mAms.addAccountAsUser( 1721 null, // response 1722 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 1723 "authTokenType", 1724 null, // requiredFeatures 1725 true, // expectActivityLaunch 1726 null, // optionsIn 1727 UserHandle.USER_SYSTEM); 1728 fail("IllegalArgumentException expected. But no exception was thrown."); 1729 } catch (IllegalArgumentException e) { 1730 // IllegalArgumentException is expected. 1731 } 1732 } 1733 1734 @SmallTest 1735 public void testAddAccountAsUserWithNullAccountType() throws Exception { 1736 unlockSystemUser(); 1737 try { 1738 mAms.addAccountAsUser( 1739 mMockAccountManagerResponse, // response 1740 null, // accountType 1741 "authTokenType", 1742 null, // requiredFeatures 1743 true, // expectActivityLaunch 1744 null, // optionsIn 1745 UserHandle.USER_SYSTEM); 1746 fail("IllegalArgumentException expected. But no exception was thrown."); 1747 } catch (IllegalArgumentException e) { 1748 // IllegalArgumentException is expected. 1749 } 1750 } 1751 1752 @SmallTest 1753 public void testAddAccountAsUserUserCannotModifyAccountNoDPM() throws Exception { 1754 unlockSystemUser(); 1755 Bundle bundle = new Bundle(); 1756 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 1757 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 1758 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 1759 1760 mAms.addAccountAsUser( 1761 mMockAccountManagerResponse, // response 1762 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1763 "authTokenType", 1764 null, // requiredFeatures 1765 true, // expectActivityLaunch 1766 null, // optionsIn 1767 UserHandle.USER_SYSTEM); 1768 verify(mMockAccountManagerResponse).onError( 1769 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 1770 verify(mMockContext).startActivityAsUser(mIntentCaptor.capture(), eq(UserHandle.SYSTEM)); 1771 1772 // verify the intent for default CantAddAccountActivity is sent. 1773 Intent intent = mIntentCaptor.getValue(); 1774 assertEquals(intent.getComponent().getClassName(), CantAddAccountActivity.class.getName()); 1775 assertEquals(intent.getIntExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, 0), 1776 AccountManager.ERROR_CODE_USER_RESTRICTED); 1777 } 1778 1779 @SmallTest 1780 public void testAddAccountAsUserUserCannotModifyAccountWithDPM() throws Exception { 1781 unlockSystemUser(); 1782 Bundle bundle = new Bundle(); 1783 bundle.putBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, true); 1784 when(mMockUserManager.getUserRestrictions(any(UserHandle.class))).thenReturn(bundle); 1785 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 1786 LocalServices.addService( 1787 DevicePolicyManagerInternal.class, mMockDevicePolicyManagerInternal); 1788 when(mMockDevicePolicyManagerInternal.createUserRestrictionSupportIntent( 1789 anyInt(), anyString())).thenReturn(new Intent()); 1790 when(mMockDevicePolicyManagerInternal.createShowAdminSupportIntent( 1791 anyInt(), anyBoolean())).thenReturn(new Intent()); 1792 1793 mAms.addAccountAsUser( 1794 mMockAccountManagerResponse, // response 1795 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1796 "authTokenType", 1797 null, // requiredFeatures 1798 true, // expectActivityLaunch 1799 null, // optionsIn 1800 UserHandle.USER_SYSTEM); 1801 1802 verify(mMockAccountManagerResponse).onError( 1803 eq(AccountManager.ERROR_CODE_USER_RESTRICTED), anyString()); 1804 verify(mMockContext).startActivityAsUser(any(Intent.class), eq(UserHandle.SYSTEM)); 1805 verify(mMockDevicePolicyManagerInternal).createUserRestrictionSupportIntent( 1806 anyInt(), anyString()); 1807 } 1808 1809 @SmallTest 1810 public void testAddAccountAsUserUserCannotModifyAccountForTypeNoDPM() throws Exception { 1811 unlockSystemUser(); 1812 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 1813 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 1814 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 1815 1816 mAms.addAccountAsUser( 1817 mMockAccountManagerResponse, // response 1818 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1819 "authTokenType", 1820 null, // requiredFeatures 1821 true, // expectActivityLaunch 1822 null, // optionsIn 1823 UserHandle.USER_SYSTEM); 1824 1825 verify(mMockAccountManagerResponse).onError( 1826 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 1827 verify(mMockContext).startActivityAsUser(mIntentCaptor.capture(), eq(UserHandle.SYSTEM)); 1828 1829 // verify the intent for default CantAddAccountActivity is sent. 1830 Intent intent = mIntentCaptor.getValue(); 1831 assertEquals(intent.getComponent().getClassName(), CantAddAccountActivity.class.getName()); 1832 assertEquals(intent.getIntExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, 0), 1833 AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE); 1834 } 1835 1836 @SmallTest 1837 public void testAddAccountAsUserUserCannotModifyAccountForTypeWithDPM() throws Exception { 1838 unlockSystemUser(); 1839 when(mMockContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn( 1840 mMockDevicePolicyManager); 1841 when(mMockDevicePolicyManager.getAccountTypesWithManagementDisabledAsUser(anyInt())) 1842 .thenReturn(new String[]{AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, "BBB"}); 1843 1844 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); 1845 LocalServices.addService( 1846 DevicePolicyManagerInternal.class, mMockDevicePolicyManagerInternal); 1847 when(mMockDevicePolicyManagerInternal.createUserRestrictionSupportIntent( 1848 anyInt(), anyString())).thenReturn(new Intent()); 1849 when(mMockDevicePolicyManagerInternal.createShowAdminSupportIntent( 1850 anyInt(), anyBoolean())).thenReturn(new Intent()); 1851 1852 mAms.addAccountAsUser( 1853 mMockAccountManagerResponse, // response 1854 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1855 "authTokenType", 1856 null, // requiredFeatures 1857 true, // expectActivityLaunch 1858 null, // optionsIn 1859 UserHandle.USER_SYSTEM); 1860 1861 verify(mMockAccountManagerResponse).onError( 1862 eq(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE), anyString()); 1863 verify(mMockContext).startActivityAsUser(any(Intent.class), eq(UserHandle.SYSTEM)); 1864 verify(mMockDevicePolicyManagerInternal).createShowAdminSupportIntent( 1865 anyInt(), anyBoolean()); 1866 } 1867 1868 @SmallTest 1869 public void testAddAccountAsUserSuccess() throws Exception { 1870 unlockSystemUser(); 1871 final CountDownLatch latch = new CountDownLatch(1); 1872 Response response = new Response(latch, mMockAccountManagerResponse); 1873 mAms.addAccountAsUser( 1874 response, // response 1875 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1876 "authTokenType", 1877 null, // requiredFeatures 1878 true, // expectActivityLaunch 1879 createAddAccountOptions(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS), 1880 UserHandle.USER_SYSTEM); 1881 waitForLatch(latch); 1882 // Verify notification is cancelled 1883 verify(mMockNotificationManager).cancelNotificationWithTag( 1884 anyString(), anyString(), anyInt(), anyInt()); 1885 1886 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1887 Bundle result = mBundleCaptor.getValue(); 1888 // Verify response data 1889 assertNull(result.getString(AccountManager.KEY_AUTHTOKEN, null)); 1890 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS, 1891 result.getString(AccountManager.KEY_ACCOUNT_NAME)); 1892 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 1893 result.getString(AccountManager.KEY_ACCOUNT_TYPE)); 1894 1895 Bundle optionBundle = result.getParcelable( 1896 AccountManagerServiceTestFixtures.KEY_OPTIONS_BUNDLE); 1897 // Assert addAccountAsUser added calling uid and pid into the option bundle 1898 assertTrue(optionBundle.containsKey(AccountManager.KEY_CALLER_UID)); 1899 assertTrue(optionBundle.containsKey(AccountManager.KEY_CALLER_PID)); 1900 } 1901 1902 @SmallTest 1903 public void testAddAccountAsUserReturnWithInvalidIntent() throws Exception { 1904 unlockSystemUser(); 1905 ResolveInfo resolveInfo = new ResolveInfo(); 1906 resolveInfo.activityInfo = new ActivityInfo(); 1907 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1908 when(mMockPackageManager.resolveActivityAsUser( 1909 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1910 when(mMockPackageManager.checkSignatures( 1911 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 1912 1913 final CountDownLatch latch = new CountDownLatch(1); 1914 Response response = new Response(latch, mMockAccountManagerResponse); 1915 mAms.addAccountAsUser( 1916 response, // response 1917 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1918 "authTokenType", 1919 null, // requiredFeatures 1920 true, // expectActivityLaunch 1921 createAddAccountOptions(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE), 1922 UserHandle.USER_SYSTEM); 1923 1924 waitForLatch(latch); 1925 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1926 verify(mMockAccountManagerResponse).onError( 1927 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 1928 } 1929 1930 @SmallTest 1931 public void testAddAccountAsUserReturnWithValidIntent() throws Exception { 1932 unlockSystemUser(); 1933 ResolveInfo resolveInfo = new ResolveInfo(); 1934 resolveInfo.activityInfo = new ActivityInfo(); 1935 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 1936 when(mMockPackageManager.resolveActivityAsUser( 1937 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 1938 when(mMockPackageManager.checkSignatures( 1939 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 1940 1941 final CountDownLatch latch = new CountDownLatch(1); 1942 Response response = new Response(latch, mMockAccountManagerResponse); 1943 1944 mAms.addAccountAsUser( 1945 response, // response 1946 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1947 "authTokenType", 1948 null, // requiredFeatures 1949 true, // expectActivityLaunch 1950 createAddAccountOptions(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE), 1951 UserHandle.USER_SYSTEM); 1952 1953 waitForLatch(latch); 1954 1955 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 1956 Bundle result = mBundleCaptor.getValue(); 1957 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 1958 assertNotNull(intent); 1959 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 1960 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 1961 } 1962 1963 @SmallTest 1964 public void testAddAccountAsUserError() throws Exception { 1965 unlockSystemUser(); 1966 1967 final CountDownLatch latch = new CountDownLatch(1); 1968 Response response = new Response(latch, mMockAccountManagerResponse); 1969 1970 mAms.addAccountAsUser( 1971 response, // response 1972 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 1973 "authTokenType", 1974 null, // requiredFeatures 1975 true, // expectActivityLaunch 1976 createAddAccountOptions(AccountManagerServiceTestFixtures.ACCOUNT_NAME_ERROR), 1977 UserHandle.USER_SYSTEM); 1978 1979 waitForLatch(latch); 1980 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 1981 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 1982 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 1983 } 1984 1985 @SmallTest 1986 public void testConfirmCredentialsAsUserWithNullResponse() throws Exception { 1987 unlockSystemUser(); 1988 try { 1989 mAms.confirmCredentialsAsUser( 1990 null, // response 1991 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 1992 new Bundle(), // options 1993 false, // expectActivityLaunch 1994 UserHandle.USER_SYSTEM); 1995 fail("IllegalArgumentException expected. But no exception was thrown."); 1996 } catch (IllegalArgumentException e) { 1997 // IllegalArgumentException is expected. 1998 } 1999 } 2000 2001 @SmallTest 2002 public void testConfirmCredentialsAsUserWithNullAccount() throws Exception { 2003 unlockSystemUser(); 2004 try { 2005 mAms.confirmCredentialsAsUser( 2006 mMockAccountManagerResponse, // response 2007 null, // account 2008 new Bundle(), // options 2009 false, // expectActivityLaunch 2010 UserHandle.USER_SYSTEM); 2011 fail("IllegalArgumentException expected. But no exception was thrown."); 2012 } catch (IllegalArgumentException e) { 2013 // IllegalArgumentException is expected. 2014 } 2015 } 2016 2017 @SmallTest 2018 public void testConfirmCredentialsAsUserSuccess() throws Exception { 2019 unlockSystemUser(); 2020 final CountDownLatch latch = new CountDownLatch(1); 2021 Response response = new Response(latch, mMockAccountManagerResponse); 2022 mAms.confirmCredentialsAsUser( 2023 response, // response 2024 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 2025 new Bundle(), // options 2026 true, // expectActivityLaunch 2027 UserHandle.USER_SYSTEM); 2028 waitForLatch(latch); 2029 2030 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2031 Bundle result = mBundleCaptor.getValue(); 2032 // Verify response data 2033 assertTrue(result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)); 2034 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS, 2035 result.getString(AccountManager.KEY_ACCOUNT_NAME)); 2036 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2037 result.getString(AccountManager.KEY_ACCOUNT_TYPE)); 2038 } 2039 2040 @SmallTest 2041 public void testConfirmCredentialsAsUserReturnWithInvalidIntent() throws Exception { 2042 unlockSystemUser(); 2043 ResolveInfo resolveInfo = new ResolveInfo(); 2044 resolveInfo.activityInfo = new ActivityInfo(); 2045 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 2046 when(mMockPackageManager.resolveActivityAsUser( 2047 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 2048 when(mMockPackageManager.checkSignatures( 2049 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 2050 2051 final CountDownLatch latch = new CountDownLatch(1); 2052 Response response = new Response(latch, mMockAccountManagerResponse); 2053 mAms.confirmCredentialsAsUser( 2054 response, // response 2055 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 2056 new Bundle(), // options 2057 true, // expectActivityLaunch 2058 UserHandle.USER_SYSTEM); 2059 waitForLatch(latch); 2060 2061 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 2062 verify(mMockAccountManagerResponse).onError( 2063 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 2064 } 2065 2066 @SmallTest 2067 public void testConfirmCredentialsAsUserReturnWithValidIntent() throws Exception { 2068 unlockSystemUser(); 2069 ResolveInfo resolveInfo = new ResolveInfo(); 2070 resolveInfo.activityInfo = new ActivityInfo(); 2071 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 2072 when(mMockPackageManager.resolveActivityAsUser( 2073 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 2074 when(mMockPackageManager.checkSignatures( 2075 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 2076 2077 final CountDownLatch latch = new CountDownLatch(1); 2078 Response response = new Response(latch, mMockAccountManagerResponse); 2079 2080 mAms.confirmCredentialsAsUser( 2081 response, // response 2082 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 2083 new Bundle(), // options 2084 true, // expectActivityLaunch 2085 UserHandle.USER_SYSTEM); 2086 2087 waitForLatch(latch); 2088 2089 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2090 Bundle result = mBundleCaptor.getValue(); 2091 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 2092 assertNotNull(intent); 2093 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 2094 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 2095 } 2096 2097 @SmallTest 2098 public void testConfirmCredentialsAsUserError() throws Exception { 2099 unlockSystemUser(); 2100 2101 final CountDownLatch latch = new CountDownLatch(1); 2102 Response response = new Response(latch, mMockAccountManagerResponse); 2103 2104 mAms.confirmCredentialsAsUser( 2105 response, // response 2106 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, 2107 new Bundle(), // options 2108 true, // expectActivityLaunch 2109 UserHandle.USER_SYSTEM); 2110 2111 waitForLatch(latch); 2112 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 2113 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 2114 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 2115 } 2116 2117 @SmallTest 2118 public void testUpdateCredentialsWithNullResponse() throws Exception { 2119 unlockSystemUser(); 2120 try { 2121 mAms.updateCredentials( 2122 null, // response 2123 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 2124 "authTokenType", 2125 false, // expectActivityLaunch 2126 new Bundle()); // options 2127 fail("IllegalArgumentException expected. But no exception was thrown."); 2128 } catch (IllegalArgumentException e) { 2129 // IllegalArgumentException is expected. 2130 } 2131 } 2132 2133 @SmallTest 2134 public void testUpdateCredentialsWithNullAccount() throws Exception { 2135 unlockSystemUser(); 2136 try { 2137 mAms.updateCredentials( 2138 mMockAccountManagerResponse, // response 2139 null, // account 2140 "authTokenType", 2141 false, // expectActivityLaunch 2142 new Bundle()); // options 2143 fail("IllegalArgumentException expected. But no exception was thrown."); 2144 } catch (IllegalArgumentException e) { 2145 // IllegalArgumentException is expected. 2146 } 2147 } 2148 2149 @SmallTest 2150 public void testUpdateCredentialsSuccess() throws Exception { 2151 unlockSystemUser(); 2152 final CountDownLatch latch = new CountDownLatch(1); 2153 Response response = new Response(latch, mMockAccountManagerResponse); 2154 2155 mAms.updateCredentials( 2156 response, // response 2157 AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, 2158 "authTokenType", 2159 false, // expectActivityLaunch 2160 new Bundle()); // options 2161 2162 waitForLatch(latch); 2163 2164 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2165 Bundle result = mBundleCaptor.getValue(); 2166 // Verify response data 2167 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS, 2168 result.getString(AccountManager.KEY_ACCOUNT_NAME)); 2169 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2170 result.getString(AccountManager.KEY_ACCOUNT_TYPE)); 2171 } 2172 2173 @SmallTest 2174 public void testUpdateCredentialsReturnWithInvalidIntent() throws Exception { 2175 unlockSystemUser(); 2176 ResolveInfo resolveInfo = new ResolveInfo(); 2177 resolveInfo.activityInfo = new ActivityInfo(); 2178 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 2179 when(mMockPackageManager.resolveActivityAsUser( 2180 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 2181 when(mMockPackageManager.checkSignatures( 2182 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_NO_MATCH); 2183 2184 final CountDownLatch latch = new CountDownLatch(1); 2185 Response response = new Response(latch, mMockAccountManagerResponse); 2186 2187 mAms.updateCredentials( 2188 response, // response 2189 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 2190 "authTokenType", 2191 true, // expectActivityLaunch 2192 new Bundle()); // options 2193 2194 waitForLatch(latch); 2195 2196 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 2197 verify(mMockAccountManagerResponse).onError( 2198 eq(AccountManager.ERROR_CODE_REMOTE_EXCEPTION), anyString()); 2199 } 2200 2201 @SmallTest 2202 public void testUpdateCredentialsReturnWithValidIntent() throws Exception { 2203 unlockSystemUser(); 2204 ResolveInfo resolveInfo = new ResolveInfo(); 2205 resolveInfo.activityInfo = new ActivityInfo(); 2206 resolveInfo.activityInfo.applicationInfo = new ApplicationInfo(); 2207 when(mMockPackageManager.resolveActivityAsUser( 2208 any(Intent.class), anyInt(), anyInt())).thenReturn(resolveInfo); 2209 when(mMockPackageManager.checkSignatures( 2210 anyInt(), anyInt())).thenReturn(PackageManager.SIGNATURE_MATCH); 2211 2212 final CountDownLatch latch = new CountDownLatch(1); 2213 Response response = new Response(latch, mMockAccountManagerResponse); 2214 2215 mAms.updateCredentials( 2216 response, // response 2217 AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, 2218 "authTokenType", 2219 true, // expectActivityLaunch 2220 new Bundle()); // options 2221 2222 waitForLatch(latch); 2223 2224 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2225 Bundle result = mBundleCaptor.getValue(); 2226 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 2227 assertNotNull(intent); 2228 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_RESULT)); 2229 assertNotNull(intent.getParcelableExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK)); 2230 } 2231 2232 @SmallTest 2233 public void testUpdateCredentialsError() throws Exception { 2234 unlockSystemUser(); 2235 2236 final CountDownLatch latch = new CountDownLatch(1); 2237 Response response = new Response(latch, mMockAccountManagerResponse); 2238 2239 mAms.updateCredentials( 2240 response, // response 2241 AccountManagerServiceTestFixtures.ACCOUNT_ERROR, 2242 "authTokenType", 2243 false, // expectActivityLaunch 2244 new Bundle()); // options 2245 2246 waitForLatch(latch); 2247 verify(mMockAccountManagerResponse).onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, 2248 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 2249 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 2250 } 2251 2252 @SmallTest 2253 public void testEditPropertiesWithNullResponse() throws Exception { 2254 unlockSystemUser(); 2255 try { 2256 mAms.editProperties( 2257 null, // response 2258 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2259 false); // expectActivityLaunch 2260 fail("IllegalArgumentException expected. But no exception was thrown."); 2261 } catch (IllegalArgumentException e) { 2262 // IllegalArgumentException is expected. 2263 } 2264 } 2265 2266 @SmallTest 2267 public void testEditPropertiesWithNullAccountType() throws Exception { 2268 unlockSystemUser(); 2269 try { 2270 mAms.editProperties( 2271 mMockAccountManagerResponse, // response 2272 null, // accountType 2273 false); // expectActivityLaunch 2274 fail("IllegalArgumentException expected. But no exception was thrown."); 2275 } catch (IllegalArgumentException e) { 2276 // IllegalArgumentException is expected. 2277 } 2278 } 2279 2280 @SmallTest 2281 public void testEditPropertiesAccountNotManagedByCaller() throws Exception { 2282 unlockSystemUser(); 2283 when(mMockPackageManager.checkSignatures(anyInt(), anyInt())) 2284 .thenReturn(PackageManager.SIGNATURE_NO_MATCH); 2285 try { 2286 mAms.editProperties( 2287 mMockAccountManagerResponse, // response 2288 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2289 false); // expectActivityLaunch 2290 fail("SecurityException expected. But no exception was thrown."); 2291 } catch (SecurityException e) { 2292 // SecurityException is expected. 2293 } 2294 } 2295 2296 @SmallTest 2297 public void testEditPropertiesSuccess() throws Exception { 2298 unlockSystemUser(); 2299 final CountDownLatch latch = new CountDownLatch(1); 2300 Response response = new Response(latch, mMockAccountManagerResponse); 2301 2302 mAms.editProperties( 2303 response, // response 2304 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2305 false); // expectActivityLaunch 2306 2307 waitForLatch(latch); 2308 2309 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2310 Bundle result = mBundleCaptor.getValue(); 2311 // Verify response data 2312 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS, 2313 result.getString(AccountManager.KEY_ACCOUNT_NAME)); 2314 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2315 result.getString(AccountManager.KEY_ACCOUNT_TYPE)); 2316 } 2317 2318 @SmallTest 2319 public void testGetAccountsByFeaturesWithNullResponse() throws Exception { 2320 unlockSystemUser(); 2321 try { 2322 mAms.getAccountsByFeatures( 2323 null, // response 2324 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, 2325 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, 2326 "testpackage"); // opPackageName 2327 fail("IllegalArgumentException expected. But no exception was thrown."); 2328 } catch (IllegalArgumentException e) { 2329 // IllegalArgumentException is expected. 2330 } 2331 } 2332 2333 @SmallTest 2334 public void testGetAccountsByFeaturesWithNullAccountType() throws Exception { 2335 unlockSystemUser(); 2336 try { 2337 mAms.getAccountsByFeatures( 2338 mMockAccountManagerResponse, // response 2339 null, // accountType 2340 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, 2341 "testpackage"); // opPackageName 2342 fail("IllegalArgumentException expected. But no exception was thrown."); 2343 } catch (IllegalArgumentException e) { 2344 // IllegalArgumentException is expected. 2345 } 2346 } 2347 2348 @SmallTest 2349 public void testGetAccountsByFeaturesAccountNotVisible() throws Exception { 2350 unlockSystemUser(); 2351 2352 when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn( 2353 PackageManager.PERMISSION_DENIED); 2354 when(mMockPackageManager.checkSignatures(anyInt(), anyInt())) 2355 .thenReturn(PackageManager.SIGNATURE_NO_MATCH); 2356 2357 final CountDownLatch latch = new CountDownLatch(1); 2358 Response response = new Response(latch, mMockAccountManagerResponse); 2359 mAms.getAccountsByFeatures( 2360 response, // response 2361 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 2362 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, 2363 "testpackage"); // opPackageName 2364 waitForLatch(latch); 2365 2366 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2367 Bundle result = mBundleCaptor.getValue(); 2368 Account[] accounts = (Account[]) result.getParcelableArray(AccountManager.KEY_ACCOUNTS); 2369 assertTrue(accounts.length == 0); 2370 } 2371 2372 @SmallTest 2373 public void testGetAccountsByFeaturesNullFeatureReturnsAllAccounts() throws Exception { 2374 unlockSystemUser(); 2375 2376 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null); 2377 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, "p12", null); 2378 2379 final CountDownLatch latch = new CountDownLatch(1); 2380 Response response = new Response(latch, mMockAccountManagerResponse); 2381 mAms.getAccountsByFeatures( 2382 response, // response 2383 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 2384 null, // features 2385 "testpackage"); // opPackageName 2386 waitForLatch(latch); 2387 2388 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2389 Bundle result = mBundleCaptor.getValue(); 2390 Account[] accounts = (Account[]) result.getParcelableArray(AccountManager.KEY_ACCOUNTS); 2391 Arrays.sort(accounts, new AccountSorter()); 2392 assertEquals(2, accounts.length); 2393 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, accounts[0]); 2394 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, accounts[1]); 2395 } 2396 2397 @SmallTest 2398 public void testGetAccountsByFeaturesReturnsAccountsWithFeaturesOnly() throws Exception { 2399 unlockSystemUser(); 2400 2401 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null); 2402 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, "p12", null); 2403 2404 final CountDownLatch latch = new CountDownLatch(1); 2405 Response response = new Response(latch, mMockAccountManagerResponse); 2406 mAms.getAccountsByFeatures( 2407 response, // response 2408 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 2409 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, 2410 "testpackage"); // opPackageName 2411 waitForLatch(latch); 2412 2413 verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture()); 2414 Bundle result = mBundleCaptor.getValue(); 2415 Account[] accounts = (Account[]) result.getParcelableArray(AccountManager.KEY_ACCOUNTS); 2416 assertEquals(1, accounts.length); 2417 assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, accounts[0]); 2418 } 2419 2420 @SmallTest 2421 public void testGetAccountsByFeaturesError() throws Exception { 2422 unlockSystemUser(); 2423 2424 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null); 2425 mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_ERROR, "p12", null); 2426 2427 final CountDownLatch latch = new CountDownLatch(1); 2428 Response response = new Response(latch, mMockAccountManagerResponse); 2429 mAms.getAccountsByFeatures( 2430 response, // response 2431 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType 2432 AccountManagerServiceTestFixtures.ACCOUNT_FEATURES, 2433 "testpackage"); // opPackageName 2434 waitForLatch(latch); 2435 2436 verify(mMockAccountManagerResponse).onError( 2437 eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString()); 2438 verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class)); 2439 } 2440 2441 private void waitForLatch(CountDownLatch latch) { 2442 try { 2443 latch.await(LATCH_TIMEOUT_MS, TimeUnit.MILLISECONDS); 2444 } catch (InterruptedException e) { 2445 throw new IllegalStateException("Should not throw an InterruptedException", e); 2446 } 2447 } 2448 2449 private Bundle createAddAccountOptions(String accountName) { 2450 Bundle options = new Bundle(); 2451 options.putString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME, accountName); 2452 return options; 2453 } 2454 2455 private Bundle createGetAuthTokenOptions() { 2456 Bundle options = new Bundle(); 2457 options.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME, 2458 AccountManagerServiceTestFixtures.CALLER_PACKAGE); 2459 options.putLong(AccountManagerServiceTestFixtures.KEY_TOKEN_EXPIRY, 2460 System.currentTimeMillis() + ONE_DAY_IN_MILLISECOND); 2461 return options; 2462 } 2463 2464 private Bundle encryptBundleWithCryptoHelper(Bundle sessionBundle) { 2465 Bundle encryptedBundle = null; 2466 try { 2467 CryptoHelper cryptoHelper = CryptoHelper.getInstance(); 2468 encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); 2469 } catch (GeneralSecurityException e) { 2470 throw new IllegalStateException("Failed to encrypt session bundle.", e); 2471 } 2472 return encryptedBundle; 2473 } 2474 2475 private Bundle createEncryptedSessionBundle(final String accountName) { 2476 Bundle sessionBundle = new Bundle(); 2477 sessionBundle.putString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME, accountName); 2478 sessionBundle.putString( 2479 AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1, 2480 AccountManagerServiceTestFixtures.SESSION_DATA_VALUE_1); 2481 sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, 2482 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 2483 sessionBundle.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME, "APCT.session.package"); 2484 return encryptBundleWithCryptoHelper(sessionBundle); 2485 } 2486 2487 private Bundle createEncryptedSessionBundleWithError(final String accountName) { 2488 Bundle sessionBundle = new Bundle(); 2489 sessionBundle.putString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME, accountName); 2490 sessionBundle.putString( 2491 AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1, 2492 AccountManagerServiceTestFixtures.SESSION_DATA_VALUE_1); 2493 sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, 2494 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 2495 sessionBundle.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME, "APCT.session.package"); 2496 sessionBundle.putInt( 2497 AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_INVALID_RESPONSE); 2498 sessionBundle.putString(AccountManager.KEY_ERROR_MESSAGE, 2499 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 2500 return encryptBundleWithCryptoHelper(sessionBundle); 2501 } 2502 2503 private Bundle createEncryptedSessionBundleWithNoAccountType(final String accountName) { 2504 Bundle sessionBundle = new Bundle(); 2505 sessionBundle.putString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME, accountName); 2506 sessionBundle.putString( 2507 AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1, 2508 AccountManagerServiceTestFixtures.SESSION_DATA_VALUE_1); 2509 sessionBundle.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME, "APCT.session.package"); 2510 return encryptBundleWithCryptoHelper(sessionBundle); 2511 } 2512 2513 private Bundle createAppBundle() { 2514 Bundle appBundle = new Bundle(); 2515 appBundle.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME, "APCT.package"); 2516 return appBundle; 2517 } 2518 2519 private Bundle createOptionsWithAccountName(final String accountName) { 2520 Bundle sessionBundle = new Bundle(); 2521 sessionBundle.putString( 2522 AccountManagerServiceTestFixtures.SESSION_DATA_NAME_1, 2523 AccountManagerServiceTestFixtures.SESSION_DATA_VALUE_1); 2524 sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, 2525 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 2526 Bundle options = new Bundle(); 2527 options.putString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME, accountName); 2528 options.putBundle(AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE, 2529 sessionBundle); 2530 options.putString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_PASSWORD, 2531 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 2532 return options; 2533 } 2534 2535 private int readNumberOfAccountsFromDbFile(Context context, String dbName) { 2536 SQLiteDatabase ceDb = context.openOrCreateDatabase(dbName, 0, null); 2537 try (Cursor cursor = ceDb.rawQuery("SELECT count(*) FROM accounts", null)) { 2538 assertTrue(cursor.moveToNext()); 2539 return cursor.getInt(0); 2540 } 2541 } 2542 2543 private void unlockSystemUser() { 2544 mAms.onUserUnlocked(newIntentForUser(UserHandle.USER_SYSTEM)); 2545 } 2546 2547 private static Intent newIntentForUser(int userId) { 2548 Intent intent = new Intent(); 2549 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); 2550 return intent; 2551 } 2552 2553 static class MyMockContext extends MockContext { 2554 private Context mTestContext; 2555 private Context mMockContext; 2556 2557 MyMockContext(Context testContext, Context mockContext) { 2558 this.mTestContext = testContext; 2559 this.mMockContext = mockContext; 2560 } 2561 2562 @Override 2563 public int checkCallingOrSelfPermission(final String permission) { 2564 return mMockContext.checkCallingOrSelfPermission(permission); 2565 } 2566 2567 @Override 2568 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, 2569 UserHandle user) { 2570 return mTestContext.bindServiceAsUser(service, conn, flags, user); 2571 } 2572 2573 @Override 2574 public void unbindService(ServiceConnection conn) { 2575 mTestContext.unbindService(conn); 2576 } 2577 2578 @Override 2579 public PackageManager getPackageManager() { 2580 return mMockContext.getPackageManager(); 2581 } 2582 2583 @Override 2584 public String getPackageName() { 2585 return mTestContext.getPackageName(); 2586 } 2587 2588 @Override 2589 public Object getSystemService(String name) { 2590 return mMockContext.getSystemService(name); 2591 } 2592 2593 @Override 2594 public String getSystemServiceName(Class<?> serviceClass) { 2595 return mMockContext.getSystemServiceName(serviceClass); 2596 } 2597 2598 @Override 2599 public void startActivityAsUser(Intent intent, UserHandle user) { 2600 mMockContext.startActivityAsUser(intent, user); 2601 } 2602 2603 @Override 2604 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 2605 return mMockContext.registerReceiver(receiver, filter); 2606 } 2607 2608 @Override 2609 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, 2610 IntentFilter filter, String broadcastPermission, Handler scheduler) { 2611 return mMockContext.registerReceiverAsUser( 2612 receiver, user, filter, broadcastPermission, scheduler); 2613 } 2614 2615 @Override 2616 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 2617 SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { 2618 return mTestContext.openOrCreateDatabase(file, mode, factory,errorHandler); 2619 } 2620 2621 @Override 2622 public void sendBroadcastAsUser(Intent intent, UserHandle user) { 2623 mMockContext.sendBroadcastAsUser(intent, user); 2624 } 2625 2626 @Override 2627 public String getOpPackageName() { 2628 return mMockContext.getOpPackageName(); 2629 } 2630 2631 @Override 2632 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) 2633 throws PackageManager.NameNotFoundException { 2634 return mMockContext.createPackageContextAsUser(packageName, flags, user); 2635 } 2636 } 2637 2638 static class TestAccountAuthenticatorCache extends AccountAuthenticatorCache { 2639 public TestAccountAuthenticatorCache(Context realContext) { 2640 super(realContext); 2641 } 2642 2643 @Override 2644 protected File getUserSystemDirectory(int userId) { 2645 return new File(mContext.getCacheDir(), "authenticator"); 2646 } 2647 } 2648 2649 static class TestInjector extends AccountManagerService.Injector { 2650 private Context mRealContext; 2651 private INotificationManager mMockNotificationManager; 2652 TestInjector(Context realContext, 2653 Context mockContext, 2654 INotificationManager mockNotificationManager) { 2655 super(mockContext); 2656 mRealContext = realContext; 2657 mMockNotificationManager = mockNotificationManager; 2658 } 2659 2660 @Override 2661 Looper getMessageHandlerLooper() { 2662 return Looper.getMainLooper(); 2663 } 2664 2665 @Override 2666 void addLocalService(AccountManagerInternal service) { 2667 } 2668 2669 @Override 2670 IAccountAuthenticatorCache getAccountAuthenticatorCache() { 2671 return new TestAccountAuthenticatorCache(mRealContext); 2672 } 2673 2674 @Override 2675 protected String getCeDatabaseName(int userId) { 2676 return new File(mRealContext.getCacheDir(), CE_DB).getPath(); 2677 } 2678 2679 @Override 2680 protected String getDeDatabaseName(int userId) { 2681 return new File(mRealContext.getCacheDir(), DE_DB).getPath(); 2682 } 2683 2684 @Override 2685 String getPreNDatabaseName(int userId) { 2686 return new File(mRealContext.getCacheDir(), PREN_DB).getPath(); 2687 } 2688 2689 @Override 2690 INotificationManager getNotificationManager() { 2691 return mMockNotificationManager; 2692 } 2693 } 2694 2695 class Response extends IAccountManagerResponse.Stub { 2696 private CountDownLatch mLatch; 2697 private IAccountManagerResponse mMockResponse; 2698 public Response(CountDownLatch latch, IAccountManagerResponse mockResponse) { 2699 mLatch = latch; 2700 mMockResponse = mockResponse; 2701 } 2702 2703 @Override 2704 public void onResult(Bundle bundle) { 2705 try { 2706 mMockResponse.onResult(bundle); 2707 } catch (RemoteException e) { 2708 } 2709 mLatch.countDown(); 2710 } 2711 2712 @Override 2713 public void onError(int code, String message) { 2714 try { 2715 mMockResponse.onError(code, message); 2716 } catch (RemoteException e) { 2717 } 2718 mLatch.countDown(); 2719 } 2720 } 2721} 2722