TestAccountType1Authenticator.java revision 8c505dbacb65ac8bd11b1061657c1a3b5abe1be2
1/* 2 * Copyright (C) 2017 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 */ 16package com.android.server.accounts; 17 18import android.accounts.AbstractAccountAuthenticator; 19import android.accounts.Account; 20import android.accounts.AccountAuthenticatorResponse; 21import android.accounts.AccountManager; 22import android.accounts.NetworkErrorException; 23import android.content.Context; 24import android.content.Intent; 25import android.os.Bundle; 26 27import com.android.frameworks.servicestests.R; 28 29import java.util.concurrent.atomic.AtomicInteger; 30 31/** 32 * This authenticator is to mock account authenticator to test AccountManagerService. 33 */ 34public class TestAccountType1Authenticator extends AbstractAccountAuthenticator { 35 private final AtomicInteger mTokenCounter = new AtomicInteger(0); 36 37 private final String mAccountType; 38 private final Context mContext; 39 40 public TestAccountType1Authenticator(Context context, String accountType) { 41 super(context); 42 mAccountType = accountType; 43 mContext = context; 44 } 45 46 @Override 47 public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { 48 Bundle result = new Bundle(); 49 result.putString(AccountManager.KEY_ACCOUNT_NAME, 50 AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS); 51 result.putString(AccountManager.KEY_ACCOUNT_TYPE, 52 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 53 result.putString( 54 AccountManager.KEY_AUTHTOKEN, 55 Integer.toString(mTokenCounter.incrementAndGet())); 56 return result; 57 } 58 59 @Override 60 public Bundle addAccount( 61 AccountAuthenticatorResponse response, 62 String accountType, 63 String authTokenType, 64 String[] requiredFeatures, 65 Bundle options) throws NetworkErrorException { 66 if (!mAccountType.equals(accountType)) { 67 throw new IllegalArgumentException("Request to the wrong authenticator!"); 68 } 69 String accountName = null; 70 71 if (options != null) { 72 accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 73 } 74 75 Bundle result = new Bundle(); 76 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 77 // fill bundle with a success result. 78 result.putString(AccountManager.KEY_ACCOUNT_NAME, accountName); 79 result.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); 80 result.putString(AccountManager.KEY_AUTHTOKEN, 81 Integer.toString(mTokenCounter.incrementAndGet())); 82 result.putParcelable(AccountManagerServiceTestFixtures.KEY_OPTIONS_BUNDLE, options); 83 } else if (accountName.equals( 84 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 85 // Specify data to be returned by the eventual activity. 86 Intent eventualActivityResultData = new Intent(); 87 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName); 88 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType); 89 // Fill result with Intent. 90 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 91 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, eventualActivityResultData); 92 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 93 94 result.putParcelable(AccountManager.KEY_INTENT, intent); 95 } else { 96 fillResultWithError( 97 result, 98 AccountManager.ERROR_CODE_INVALID_RESPONSE, 99 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 100 } 101 return result; 102 } 103 104 @Override 105 public Bundle confirmCredentials( 106 AccountAuthenticatorResponse response, 107 Account account, 108 Bundle options) throws NetworkErrorException { 109 if (!mAccountType.equals(account.type)) { 110 throw new IllegalArgumentException("Request to the wrong authenticator!"); 111 } 112 Bundle result = new Bundle(); 113 114 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 115 // fill bundle with a success result. 116 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 117 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 118 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 119 } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 120 // Specify data to be returned by the eventual activity. 121 Intent eventualActivityResultData = new Intent(); 122 eventualActivityResultData.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true); 123 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); 124 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type); 125 126 // Fill result with Intent. 127 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 128 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 129 eventualActivityResultData); 130 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 131 132 result.putParcelable(AccountManager.KEY_INTENT, intent); 133 } else { 134 // fill with error 135 fillResultWithError( 136 result, 137 AccountManager.ERROR_CODE_INVALID_RESPONSE, 138 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 139 } 140 return result; 141 } 142 143 @Override 144 public Bundle getAuthToken( 145 AccountAuthenticatorResponse response, 146 Account account, 147 String authTokenType, 148 Bundle options) throws NetworkErrorException { 149 if (!mAccountType.equals(account.type)) { 150 throw new IllegalArgumentException("Request to the wrong authenticator!"); 151 } 152 Bundle result = new Bundle(); 153 154 long expiryMillis = (options == null) 155 ? 0 : options.getLong(AccountManagerServiceTestFixtures.KEY_TOKEN_EXPIRY); 156 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 157 // fill bundle with a success result. 158 result.putString( 159 AccountManager.KEY_AUTHTOKEN, AccountManagerServiceTestFixtures.AUTH_TOKEN); 160 result.putLong( 161 AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 162 expiryMillis); 163 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 164 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 165 } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 166 // Specify data to be returned by the eventual activity. 167 Intent eventualActivityResultData = new Intent(); 168 eventualActivityResultData.putExtra( 169 AccountManager.KEY_AUTHTOKEN, AccountManagerServiceTestFixtures.AUTH_TOKEN); 170 eventualActivityResultData.putExtra( 171 AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 172 expiryMillis); 173 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); 174 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type); 175 176 // Fill result with Intent. 177 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 178 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 179 eventualActivityResultData); 180 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 181 182 result.putParcelable(AccountManager.KEY_INTENT, intent); 183 184 } else { 185 fillResultWithError( 186 result, 187 AccountManager.ERROR_CODE_INVALID_RESPONSE, 188 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 189 } 190 return result; 191 } 192 193 @Override 194 public String getAuthTokenLabel(String authTokenType) { 195 return AccountManagerServiceTestFixtures.AUTH_TOKEN_LABEL; 196 } 197 198 @Override 199 public Bundle updateCredentials( 200 AccountAuthenticatorResponse response, 201 Account account, 202 String authTokenType, 203 Bundle options) throws NetworkErrorException { 204 if (!mAccountType.equals(account.type)) { 205 throw new IllegalArgumentException("Request to the wrong authenticator!"); 206 } 207 Bundle result = new Bundle(); 208 209 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 210 // fill bundle with a success result. 211 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 212 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 213 } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 214 // Specify data to be returned by the eventual activity. 215 Intent eventualActivityResultData = new Intent(); 216 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); 217 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type); 218 219 // Fill result with Intent. 220 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 221 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 222 eventualActivityResultData); 223 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 224 225 result.putParcelable(AccountManager.KEY_INTENT, intent); 226 } else { 227 // fill with error 228 fillResultWithError( 229 result, 230 AccountManager.ERROR_CODE_INVALID_RESPONSE, 231 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 232 } 233 return result; 234 } 235 236 @Override 237 public Bundle hasFeatures( 238 AccountAuthenticatorResponse response, 239 Account account, 240 String[] features) throws NetworkErrorException { 241 Bundle result = new Bundle(); 242 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 243 // fill bundle with true. 244 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 245 } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 246 // fill bundle with false. 247 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); 248 } else { 249 // return null for error 250 result = null; 251 } 252 253 response.onResult(result); 254 return null; 255 } 256 257 @Override 258 public Bundle startAddAccountSession( 259 AccountAuthenticatorResponse response, 260 String accountType, 261 String authTokenType, 262 String[] requiredFeatures, 263 Bundle options) throws NetworkErrorException { 264 if (!mAccountType.equals(accountType)) { 265 throw new IllegalArgumentException("Request to the wrong authenticator!"); 266 } 267 268 String accountName = null; 269 Bundle sessionBundle = null; 270 String password = null; 271 if (options != null) { 272 accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 273 sessionBundle = options.getBundle( 274 AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE); 275 password = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_PASSWORD); 276 } 277 278 Bundle result = new Bundle(); 279 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 280 // fill bundle with a success result. 281 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle); 282 result.putString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 283 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 284 result.putString(AccountManager.KEY_PASSWORD, password); 285 result.putString(AccountManager.KEY_AUTHTOKEN, 286 Integer.toString(mTokenCounter.incrementAndGet())); 287 } else if (accountName.equals( 288 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 289 // Specify data to be returned by the eventual activity. 290 Intent eventualActivityResultData = new Intent(); 291 eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN, 292 Integer.toString(mTokenCounter.incrementAndGet())); 293 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 294 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 295 eventualActivityResultData.putExtra(AccountManager.KEY_PASSWORD, password); 296 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, 297 sessionBundle); 298 // Fill result with Intent. 299 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 300 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 301 eventualActivityResultData); 302 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 303 304 result.putParcelable(AccountManager.KEY_INTENT, intent); 305 } else { 306 // fill with error 307 fillResultWithError(result, options); 308 } 309 310 return result; 311 } 312 313 @Override 314 public Bundle startUpdateCredentialsSession( 315 AccountAuthenticatorResponse response, 316 Account account, 317 String authTokenType, 318 Bundle options) 319 throws NetworkErrorException { 320 321 if (!mAccountType.equals(account.type)) { 322 throw new IllegalArgumentException("Request to the wrong authenticator!"); 323 } 324 325 String accountName = null; 326 Bundle sessionBundle = null; 327 if (options != null) { 328 accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 329 sessionBundle = options.getBundle( 330 AccountManagerServiceTestFixtures.KEY_ACCOUNT_SESSION_BUNDLE); 331 } 332 333 Bundle result = new Bundle(); 334 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 335 // fill bundle with a success result. 336 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle); 337 result.putString(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 338 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 339 result.putString(AccountManager.KEY_PASSWORD, 340 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 341 result.putString(AccountManager.KEY_AUTHTOKEN, 342 Integer.toString(mTokenCounter.incrementAndGet())); 343 } else if (accountName.equals( 344 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 345 // Specify data to be returned by the eventual activity. 346 Intent eventualActivityResultData = new Intent(); 347 eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN, 348 Integer.toString(mTokenCounter.incrementAndGet())); 349 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_STATUS_TOKEN, 350 AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN); 351 eventualActivityResultData.putExtra(AccountManager.KEY_PASSWORD, 352 AccountManagerServiceTestFixtures.ACCOUNT_PASSWORD); 353 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, 354 sessionBundle); 355 // Fill result with Intent. 356 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 357 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 358 eventualActivityResultData); 359 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 360 361 result.putParcelable(AccountManager.KEY_INTENT, intent); 362 } else { 363 // fill with error 364 fillResultWithError(result, options); 365 } 366 return result; 367 } 368 369 @Override 370 public Bundle finishSession(AccountAuthenticatorResponse response, 371 String accountType, 372 Bundle sessionBundle) throws NetworkErrorException { 373 374 if (!mAccountType.equals(accountType)) { 375 throw new IllegalArgumentException("Request to the wrong authenticator!"); 376 } 377 378 String accountName = null; 379 if (sessionBundle != null) { 380 accountName = sessionBundle.getString( 381 AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME); 382 } 383 384 Bundle result = new Bundle(); 385 if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 386 // add sessionBundle into result for verification purpose 387 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle); 388 // fill bundle with a success result. 389 result.putString(AccountManager.KEY_ACCOUNT_NAME, 390 AccountManagerServiceTestFixtures.ACCOUNT_NAME); 391 result.putString(AccountManager.KEY_ACCOUNT_TYPE, 392 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 393 result.putString(AccountManager.KEY_AUTHTOKEN, 394 Integer.toString(mTokenCounter.incrementAndGet())); 395 } else if (accountName.equals( 396 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 397 // Specify data to be returned by the eventual activity. 398 Intent eventualActivityResultData = new Intent(); 399 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, 400 AccountManagerServiceTestFixtures.ACCOUNT_NAME); 401 eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, 402 AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1); 403 eventualActivityResultData.putExtra(AccountManager.KEY_AUTHTOKEN, 404 Integer.toString(mTokenCounter.incrementAndGet())); 405 406 // Fill result with Intent. 407 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 408 intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, 409 eventualActivityResultData); 410 intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response); 411 412 result.putParcelable(AccountManager.KEY_INTENT, intent); 413 } else { 414 // fill with error 415 fillResultWithError(result, sessionBundle); 416 } 417 return result; 418 } 419 420 @Override 421 public Bundle isCredentialsUpdateSuggested( 422 final AccountAuthenticatorResponse response, 423 Account account, 424 String statusToken) throws NetworkErrorException { 425 426 Bundle result = new Bundle(); 427 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 428 // fill bundle with a success result. 429 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 430 } else { 431 // fill with error 432 fillResultWithError( 433 result, 434 AccountManager.ERROR_CODE_INVALID_RESPONSE, 435 AccountManagerServiceTestFixtures.ERROR_MESSAGE); 436 } 437 438 response.onResult(result); 439 return null; 440 } 441 442 @Override 443 public Bundle getAccountRemovalAllowed( 444 AccountAuthenticatorResponse response, Account account) throws NetworkErrorException { 445 Bundle result = new Bundle(); 446 if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) { 447 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 448 } else if (account.name.equals( 449 AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) { 450 Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class); 451 result.putParcelable(AccountManager.KEY_INTENT, intent); 452 } else { 453 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); 454 } 455 return result; 456 } 457 458 private void fillResultWithError(Bundle result, Bundle options) { 459 int errorCode = AccountManager.ERROR_CODE_INVALID_RESPONSE; 460 String errorMsg = "Default Error Message"; 461 if (options != null) { 462 errorCode = options.getInt(AccountManager.KEY_ERROR_CODE); 463 errorMsg = options.getString(AccountManager.KEY_ERROR_MESSAGE); 464 } 465 fillResultWithError(result, errorCode, errorMsg); 466 } 467 468 private void fillResultWithError(Bundle result, int errorCode, String errorMsg) { 469 result.putInt(AccountManager.KEY_ERROR_CODE, errorCode); 470 result.putString(AccountManager.KEY_ERROR_MESSAGE, errorMsg); 471 } 472}