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 */ 16 17package com.android.server.backup; 18 19import static junit.framework.Assert.assertEquals; 20import static junit.framework.Assert.assertFalse; 21import static junit.framework.Assert.assertNotNull; 22import static junit.framework.Assert.assertNull; 23import static junit.framework.Assert.assertTrue; 24import static junit.framework.Assert.fail; 25 26import static org.mockito.Mockito.reset; 27import static org.mockito.Mockito.verify; 28import static org.mockito.Mockito.verifyNoMoreInteractions; 29import static org.mockito.Mockito.when; 30 31import android.app.backup.BackupManager; 32import android.app.backup.IBackupManagerMonitor; 33import android.app.backup.IBackupObserver; 34import android.app.backup.IFullBackupRestoreObserver; 35import android.app.backup.ISelectBackupTransportCallback; 36import android.content.ComponentName; 37import android.content.Context; 38import android.content.Intent; 39import android.content.pm.PackageManager; 40import android.os.IBinder; 41import android.os.ParcelFileDescriptor; 42import android.os.Process; 43import android.os.RemoteException; 44import android.os.UserHandle; 45import android.platform.test.annotations.Presubmit; 46import android.support.test.filters.SmallTest; 47import android.support.test.runner.AndroidJUnit4; 48 49import org.junit.Before; 50import org.junit.Test; 51import org.junit.runner.RunWith; 52import org.mockito.Mock; 53import org.mockito.MockitoAnnotations; 54 55import java.io.File; 56import java.io.FileDescriptor; 57import java.io.IOException; 58import java.io.PrintWriter; 59import java.util.concurrent.LinkedBlockingQueue; 60import java.util.concurrent.TimeUnit; 61 62@SmallTest 63@Presubmit 64@RunWith(AndroidJUnit4.class) 65public class TrampolineTest { 66 private static final String PACKAGE_NAME = "some.package.name"; 67 private static final String TRANSPORT_NAME = "some.transport.name"; 68 private static final String CURRENT_PASSWORD = "current_password"; 69 private static final String NEW_PASSWORD = "new_password"; 70 private static final String ENCRYPTION_PASSWORD = "encryption_password"; 71 private static final String DATA_MANAGEMENT_LABEL = "data_management_label"; 72 private static final String DESTINATION_STRING = "destination_string"; 73 private static final String[] PACKAGE_NAMES = 74 new String[]{"some.package.name._1", "some.package.name._2"}; 75 private static final String[] TRANSPORTS = 76 new String[]{"some.transport.name._1", "some.transport.name._2"}; 77 private static final ComponentName TRANSPORT_COMPONENT_NAME = new ComponentName("package", 78 "class"); 79 private static final ComponentName[] TRANSPORT_COMPONENTS = new ComponentName[]{ 80 new ComponentName("package1", "class1"), 81 new ComponentName("package2", "class2") 82 }; 83 private final int NON_USER_SYSTEM = UserHandle.USER_SYSTEM + 1; 84 85 @Mock private BackupManagerService mBackupManagerServiceMock; 86 @Mock private Context mContextMock; 87 @Mock private File mSuppressFileMock; 88 @Mock private File mSuppressFileParentMock; 89 @Mock private IBinder mAgentMock; 90 @Mock private ParcelFileDescriptor mParcelFileDescriptorMock; 91 @Mock private IFullBackupRestoreObserver mFullBackupRestoreObserverMock; 92 @Mock private IBackupObserver mBackupObserverMock; 93 @Mock private IBackupManagerMonitor mBackupManagerMonitorMock; 94 @Mock private PrintWriter mPrintWriterMock; 95 96 private FileDescriptor mFileDescriptorStub = new FileDescriptor(); 97 98 private TrampolineTestable mTrampoline; 99 100 @Before 101 public void setUp() { 102 MockitoAnnotations.initMocks(this); 103 104 TrampolineTestable.sBackupManagerServiceMock = mBackupManagerServiceMock; 105 TrampolineTestable.sSuppressFile = mSuppressFileMock; 106 TrampolineTestable.sCallingUid = Process.SYSTEM_UID; 107 TrampolineTestable.sBackupDisabled = false; 108 109 when(mSuppressFileMock.getParentFile()).thenReturn(mSuppressFileParentMock); 110 111 mTrampoline = new TrampolineTestable(mContextMock); 112 } 113 114 @Test 115 public void constructor_createsSuppressFileDirectory() { 116 verify(mSuppressFileParentMock).mkdirs(); 117 } 118 119 @Test 120 public void initialize_forUserSystem_successfullyInitialized() { 121 mTrampoline.initialize(UserHandle.USER_SYSTEM); 122 123 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 124 } 125 126 // The BackupManagerService can only be initialized by USER_SYSTEM, so we check that if any 127 // other user trying to initialize it leaves it non-active. 128 @Test 129 public void initialize_forNonUserSystem_nonInitialized() { 130 mTrampoline.initialize(NON_USER_SYSTEM); 131 132 assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM)); 133 } 134 135 @Test 136 public void initialize_globallyDisabled_nonInitialized() { 137 TrampolineTestable.sBackupDisabled = true; 138 139 TrampolineTestable trampoline = new TrampolineTestable(mContextMock); 140 trampoline.initialize(UserHandle.USER_SYSTEM); 141 142 assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 143 } 144 145 // Verify that BackupManagerService is not initialized if suppress file exists. 146 @Test 147 public void initialize_suppressFileExists_nonInitialized() { 148 when(mSuppressFileMock.exists()).thenReturn(true); 149 150 TrampolineTestable trampoline = new TrampolineTestable(mContextMock); 151 trampoline.initialize(UserHandle.USER_SYSTEM); 152 153 assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 154 } 155 156 @Test 157 public void isBackupServiceActive_calledBeforeInitialize_returnsFalse() { 158 assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 159 } 160 161 @Test 162 public void setBackupServiceActive_callerSystemUid_serviceCreated() { 163 TrampolineTestable.sCallingUid = Process.SYSTEM_UID; 164 165 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 166 167 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 168 } 169 170 @Test 171 public void setBackupServiceActive_callerRootUid_serviceCreated() { 172 TrampolineTestable.sCallingUid = Process.ROOT_UID; 173 174 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 175 176 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 177 } 178 179 @Test 180 public void setBackupServiceActive_callerNonRootNonSystem_securityExceptionThrown() { 181 TrampolineTestable.sCallingUid = Process.FIRST_APPLICATION_UID; 182 183 try { 184 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 185 fail(); 186 } catch (SecurityException expected) { 187 } 188 189 assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 190 } 191 192 @Test 193 public void setBackupServiceActive_backupDisabled_ignored() { 194 TrampolineTestable.sBackupDisabled = true; 195 TrampolineTestable trampoline = new TrampolineTestable(mContextMock); 196 197 trampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 198 199 assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 200 } 201 202 @Test 203 public void setBackupServiceActive_nonUserSystem_ignored() { 204 mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); 205 206 assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM)); 207 } 208 209 @Test 210 public void setBackupServiceActive_alreadyActive_ignored() { 211 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 212 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 213 assertEquals(1, mTrampoline.getCreateServiceCallsCount()); 214 215 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 216 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 217 assertEquals(1, mTrampoline.getCreateServiceCallsCount()); 218 } 219 220 @Test 221 public void setBackupServiceActive_makeActive_serviceCreatedAndSuppressFileDeleted() { 222 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); 223 224 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 225 verify(mSuppressFileMock).delete(); 226 } 227 228 @Test 229 public void setBackupServiceActive_makeNonActive_serviceDeletedAndSuppressFileCreated() 230 throws IOException { 231 mTrampoline.initialize(UserHandle.USER_SYSTEM); 232 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 233 234 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); 235 236 assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 237 verify(mSuppressFileMock).createNewFile(); 238 } 239 240 @Test 241 public void 242 setBackupServiceActive_makeNonActive_serviceDeletedAndSuppressFileCreated_ioExceptionHandled() 243 throws IOException { 244 when(mSuppressFileMock.createNewFile()).thenThrow(new IOException()); 245 mTrampoline.initialize(UserHandle.USER_SYSTEM); 246 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 247 248 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); 249 250 assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); 251 verify(mSuppressFileMock).createNewFile(); 252 } 253 254 @Test 255 public void setBackupServiceActive_makeNonActive_alreadyNonActive_ignored() throws IOException { 256 reset(mSuppressFileMock); 257 258 mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); 259 260 verifyNoMoreInteractions(mSuppressFileMock); 261 } 262 263 @Test 264 public void dataChanged_calledBeforeInitialize_ignored() throws RemoteException { 265 mTrampoline.dataChanged(PACKAGE_NAME); 266 verifyNoMoreInteractions(mBackupManagerServiceMock); 267 } 268 269 @Test 270 public void dataChanged_forwarded() throws RemoteException { 271 mTrampoline.initialize(UserHandle.USER_SYSTEM); 272 mTrampoline.dataChanged(PACKAGE_NAME); 273 verify(mBackupManagerServiceMock).dataChanged(PACKAGE_NAME); 274 } 275 276 @Test 277 public void clearBackupData_calledBeforeInitialize_ignored() throws RemoteException { 278 mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME); 279 verifyNoMoreInteractions(mBackupManagerServiceMock); 280 } 281 282 @Test 283 public void clearBackupData_forwarded() throws RemoteException { 284 mTrampoline.initialize(UserHandle.USER_SYSTEM); 285 mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME); 286 verify(mBackupManagerServiceMock).clearBackupData(TRANSPORT_NAME, PACKAGE_NAME); 287 } 288 289 @Test 290 public void agentConnected_calledBeforeInitialize_ignored() throws RemoteException { 291 mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock); 292 verifyNoMoreInteractions(mBackupManagerServiceMock); 293 } 294 295 @Test 296 public void agentConnected_forwarded() throws RemoteException { 297 mTrampoline.initialize(UserHandle.USER_SYSTEM); 298 mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock); 299 verify(mBackupManagerServiceMock).agentConnected(PACKAGE_NAME, mAgentMock); 300 } 301 302 @Test 303 public void agentDisconnected_calledBeforeInitialize_ignored() throws RemoteException { 304 mTrampoline.agentDisconnected(PACKAGE_NAME); 305 verifyNoMoreInteractions(mBackupManagerServiceMock); 306 } 307 308 @Test 309 public void agentDisconnected_forwarded() throws RemoteException { 310 mTrampoline.initialize(UserHandle.USER_SYSTEM); 311 mTrampoline.agentDisconnected(PACKAGE_NAME); 312 verify(mBackupManagerServiceMock).agentDisconnected(PACKAGE_NAME); 313 } 314 315 @Test 316 public void restoreAtInstall_calledBeforeInitialize_ignored() throws RemoteException { 317 mTrampoline.restoreAtInstall(PACKAGE_NAME, 123); 318 verifyNoMoreInteractions(mBackupManagerServiceMock); 319 } 320 321 @Test 322 public void restoreAtInstall_forwarded() throws RemoteException { 323 mTrampoline.initialize(UserHandle.USER_SYSTEM); 324 mTrampoline.restoreAtInstall(PACKAGE_NAME, 123); 325 verify(mBackupManagerServiceMock).restoreAtInstall(PACKAGE_NAME, 123); 326 } 327 328 @Test 329 public void setBackupEnabled_calledBeforeInitialize_ignored() throws RemoteException { 330 mTrampoline.setBackupEnabled(true); 331 verifyNoMoreInteractions(mBackupManagerServiceMock); 332 } 333 334 @Test 335 public void setBackupEnabled_forwarded() throws RemoteException { 336 mTrampoline.initialize(UserHandle.USER_SYSTEM); 337 mTrampoline.setBackupEnabled(true); 338 verify(mBackupManagerServiceMock).setBackupEnabled(true); 339 } 340 341 @Test 342 public void setAutoRestore_calledBeforeInitialize_ignored() throws RemoteException { 343 mTrampoline.setAutoRestore(true); 344 verifyNoMoreInteractions(mBackupManagerServiceMock); 345 } 346 347 @Test 348 public void setAutoRestore_forwarded() throws RemoteException { 349 mTrampoline.initialize(UserHandle.USER_SYSTEM); 350 mTrampoline.setAutoRestore(true); 351 verify(mBackupManagerServiceMock).setAutoRestore(true); 352 } 353 354 @Test 355 public void setBackupProvisioned_calledBeforeInitialize_ignored() throws RemoteException { 356 mTrampoline.setBackupProvisioned(true); 357 verifyNoMoreInteractions(mBackupManagerServiceMock); 358 } 359 360 @Test 361 public void setBackupProvisioned_forwarded() throws RemoteException { 362 mTrampoline.initialize(UserHandle.USER_SYSTEM); 363 mTrampoline.setBackupProvisioned(true); 364 verify(mBackupManagerServiceMock).setBackupProvisioned(true); 365 } 366 367 @Test 368 public void isBackupEnabled_calledBeforeInitialize_ignored() throws RemoteException { 369 assertFalse(mTrampoline.isBackupEnabled()); 370 verifyNoMoreInteractions(mBackupManagerServiceMock); 371 } 372 373 @Test 374 public void isBackupEnabled_forwarded() throws RemoteException { 375 mTrampoline.initialize(UserHandle.USER_SYSTEM); 376 mTrampoline.isBackupEnabled(); 377 verify(mBackupManagerServiceMock).isBackupEnabled(); 378 } 379 380 @Test 381 public void setBackupPassword_calledBeforeInitialize_ignored() throws RemoteException { 382 mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); 383 verifyNoMoreInteractions(mBackupManagerServiceMock); 384 } 385 386 @Test 387 public void setBackupPassword_forwarded() throws RemoteException { 388 mTrampoline.initialize(UserHandle.USER_SYSTEM); 389 mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); 390 verify(mBackupManagerServiceMock).setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); 391 } 392 393 @Test 394 public void hasBackupPassword_calledBeforeInitialize_ignored() throws RemoteException { 395 assertFalse(mTrampoline.hasBackupPassword()); 396 verifyNoMoreInteractions(mBackupManagerServiceMock); 397 } 398 399 @Test 400 public void hasBackupPassword_forwarded() throws RemoteException { 401 mTrampoline.initialize(UserHandle.USER_SYSTEM); 402 mTrampoline.hasBackupPassword(); 403 verify(mBackupManagerServiceMock).hasBackupPassword(); 404 } 405 406 @Test 407 public void backupNow_calledBeforeInitialize_ignored() throws RemoteException { 408 mTrampoline.backupNow(); 409 verifyNoMoreInteractions(mBackupManagerServiceMock); 410 } 411 412 @Test 413 public void backupNow_forwarded() throws RemoteException { 414 mTrampoline.initialize(UserHandle.USER_SYSTEM); 415 mTrampoline.backupNow(); 416 verify(mBackupManagerServiceMock).backupNow(); 417 } 418 419 @Test 420 public void adbBackup_calledBeforeInitialize_ignored() throws RemoteException { 421 mTrampoline.adbBackup(mParcelFileDescriptorMock, true, true, true, true, true, true, true, 422 true, 423 PACKAGE_NAMES); 424 verifyNoMoreInteractions(mBackupManagerServiceMock); 425 } 426 427 @Test 428 public void adbBackup_forwarded() throws RemoteException { 429 mTrampoline.initialize(UserHandle.USER_SYSTEM); 430 mTrampoline.adbBackup(mParcelFileDescriptorMock, true, true, true, true, true, true, true, 431 true, 432 PACKAGE_NAMES); 433 verify(mBackupManagerServiceMock).adbBackup(mParcelFileDescriptorMock, true, true, true, 434 true, 435 true, true, true, true, PACKAGE_NAMES); 436 } 437 438 @Test 439 public void fullTransportBackup_calledBeforeInitialize_ignored() throws RemoteException { 440 mTrampoline.fullTransportBackup(PACKAGE_NAMES); 441 verifyNoMoreInteractions(mBackupManagerServiceMock); 442 } 443 444 @Test 445 public void fullTransportBackup_forwarded() throws RemoteException { 446 mTrampoline.initialize(UserHandle.USER_SYSTEM); 447 mTrampoline.fullTransportBackup(PACKAGE_NAMES); 448 verify(mBackupManagerServiceMock).fullTransportBackup(PACKAGE_NAMES); 449 } 450 451 @Test 452 public void adbRestore_calledBeforeInitialize_ignored() throws RemoteException { 453 mTrampoline.adbRestore(mParcelFileDescriptorMock); 454 verifyNoMoreInteractions(mBackupManagerServiceMock); 455 } 456 457 @Test 458 public void adbRestore_forwarded() throws RemoteException { 459 mTrampoline.initialize(UserHandle.USER_SYSTEM); 460 mTrampoline.adbRestore(mParcelFileDescriptorMock); 461 verify(mBackupManagerServiceMock).adbRestore(mParcelFileDescriptorMock); 462 } 463 464 @Test 465 public void acknowledgeFullBackupOrRestore_calledBeforeInitialize_ignored() 466 throws RemoteException { 467 mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD, 468 mFullBackupRestoreObserverMock); 469 verifyNoMoreInteractions(mBackupManagerServiceMock); 470 } 471 472 @Test 473 public void acknowledgeFullBackupOrRestore_forwarded() throws RemoteException { 474 mTrampoline.initialize(UserHandle.USER_SYSTEM); 475 mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD, 476 mFullBackupRestoreObserverMock); 477 verify(mBackupManagerServiceMock).acknowledgeAdbBackupOrRestore(123, true, CURRENT_PASSWORD, 478 ENCRYPTION_PASSWORD, mFullBackupRestoreObserverMock); 479 } 480 481 @Test 482 public void getCurrentTransport_calledBeforeInitialize_ignored() throws RemoteException { 483 assertNull(mTrampoline.getCurrentTransport()); 484 verifyNoMoreInteractions(mBackupManagerServiceMock); 485 } 486 487 @Test 488 public void getCurrentTransport_forwarded() throws RemoteException { 489 when(mBackupManagerServiceMock.getCurrentTransport()).thenReturn(TRANSPORT_NAME); 490 491 mTrampoline.initialize(UserHandle.USER_SYSTEM); 492 493 assertEquals(TRANSPORT_NAME, mTrampoline.getCurrentTransport()); 494 verify(mBackupManagerServiceMock).getCurrentTransport(); 495 } 496 497 @Test 498 public void listAllTransports_calledBeforeInitialize_ignored() throws RemoteException { 499 assertNull(mTrampoline.listAllTransports()); 500 verifyNoMoreInteractions(mBackupManagerServiceMock); 501 } 502 503 @Test 504 public void listAllTransports_forwarded() throws RemoteException { 505 when(mBackupManagerServiceMock.listAllTransports()).thenReturn(TRANSPORTS); 506 507 mTrampoline.initialize(UserHandle.USER_SYSTEM); 508 assertEquals(TRANSPORTS, mTrampoline.listAllTransports()); 509 verify(mBackupManagerServiceMock).listAllTransports(); 510 } 511 512 @Test 513 public void listAllTransportComponents_calledBeforeInitialize_ignored() throws RemoteException { 514 assertNull(mTrampoline.listAllTransportComponents()); 515 verifyNoMoreInteractions(mBackupManagerServiceMock); 516 } 517 518 @Test 519 public void listAllTransportComponents_forwarded() throws RemoteException { 520 when(mBackupManagerServiceMock.listAllTransportComponents()).thenReturn( 521 TRANSPORT_COMPONENTS); 522 523 mTrampoline.initialize(UserHandle.USER_SYSTEM); 524 assertEquals(TRANSPORT_COMPONENTS, mTrampoline.listAllTransportComponents()); 525 verify(mBackupManagerServiceMock).listAllTransportComponents(); 526 } 527 528 @Test 529 public void getTransportWhitelist_calledBeforeInitialize_ignored() throws RemoteException { 530 assertNull(mTrampoline.getTransportWhitelist()); 531 verifyNoMoreInteractions(mBackupManagerServiceMock); 532 } 533 534 @Test 535 public void getTransportWhitelist_forwarded() throws RemoteException { 536 when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS); 537 538 mTrampoline.initialize(UserHandle.USER_SYSTEM); 539 assertEquals(TRANSPORTS, mTrampoline.getTransportWhitelist()); 540 verify(mBackupManagerServiceMock).getTransportWhitelist(); 541 } 542 543 @Test 544 public void describeTransport_calledBeforeInitialize_ignored() throws RemoteException { 545 mTrampoline.updateTransportAttributes(TRANSPORT_COMPONENT_NAME, TRANSPORT_NAME, null, 546 "Transport Destination", null, "Data Management"); 547 verifyNoMoreInteractions(mBackupManagerServiceMock); 548 } 549 550 @Test 551 public void describeTransport_forwarded() throws RemoteException { 552 when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS); 553 554 mTrampoline.initialize(UserHandle.USER_SYSTEM); 555 mTrampoline.updateTransportAttributes(TRANSPORT_COMPONENT_NAME, TRANSPORT_NAME, null, 556 "Transport Destination", null, "Data Management"); 557 verify(mBackupManagerServiceMock).updateTransportAttributes(TRANSPORT_COMPONENT_NAME, 558 TRANSPORT_NAME, null, "Transport Destination", null, "Data Management"); 559 } 560 561 @Test 562 public void selectBackupTransport_calledBeforeInitialize_ignored() throws RemoteException { 563 mTrampoline.selectBackupTransport(TRANSPORT_NAME); 564 verifyNoMoreInteractions(mBackupManagerServiceMock); 565 } 566 567 @Test 568 public void selectBackupTransport_forwarded() throws RemoteException { 569 mTrampoline.initialize(UserHandle.USER_SYSTEM); 570 mTrampoline.selectBackupTransport(TRANSPORT_NAME); 571 verify(mBackupManagerServiceMock).selectBackupTransport(TRANSPORT_NAME); 572 } 573 574 @Test 575 public void selectBackupTransportAsync_calledBeforeInitialize_ignored() throws Exception { 576 LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue(); 577 mTrampoline.selectBackupTransportAsync( 578 TRANSPORT_COMPONENT_NAME, 579 new ISelectBackupTransportCallback() { 580 @Override 581 public void onSuccess(String transportName) throws RemoteException { 582 583 } 584 585 @Override 586 public void onFailure(int reason) throws RemoteException { 587 q.offer(reason); 588 } 589 590 @Override 591 public IBinder asBinder() { 592 return null; 593 } 594 }); 595 verifyNoMoreInteractions(mBackupManagerServiceMock); 596 Integer errorCode = q.poll(5, TimeUnit.SECONDS); 597 assertNotNull(errorCode); 598 assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, (int) errorCode); 599 } 600 601 @Test 602 public void selectBackupTransportAsync_calledBeforeInitialize_ignored_nullListener() 603 throws Exception { 604 mTrampoline.selectBackupTransportAsync(TRANSPORT_COMPONENT_NAME, null); 605 verifyNoMoreInteractions(mBackupManagerServiceMock); 606 // No crash. 607 } 608 609 @Test 610 public void selectBackupTransportAsync_calledBeforeInitialize_ignored_listenerThrowException() 611 throws Exception { 612 mTrampoline.selectBackupTransportAsync( 613 TRANSPORT_COMPONENT_NAME, 614 new ISelectBackupTransportCallback() { 615 @Override 616 public void onSuccess(String transportName) throws RemoteException { 617 618 } 619 620 @Override 621 public void onFailure(int reason) throws RemoteException { 622 throw new RemoteException("Crash"); 623 } 624 625 @Override 626 public IBinder asBinder() { 627 return null; 628 } 629 }); 630 verifyNoMoreInteractions(mBackupManagerServiceMock); 631 // No crash. 632 } 633 634 @Test 635 public void selectBackupTransportAsync_forwarded() throws RemoteException { 636 mTrampoline.initialize(UserHandle.USER_SYSTEM); 637 mTrampoline.selectBackupTransportAsync(TRANSPORT_COMPONENT_NAME, null); 638 verify(mBackupManagerServiceMock).selectBackupTransportAsync(TRANSPORT_COMPONENT_NAME, 639 null); 640 } 641 642 @Test 643 public void getConfigurationIntent_calledBeforeInitialize_ignored() throws RemoteException { 644 mTrampoline.getConfigurationIntent(TRANSPORT_NAME); 645 verifyNoMoreInteractions(mBackupManagerServiceMock); 646 } 647 648 @Test 649 public void getConfigurationIntent_forwarded() throws RemoteException { 650 Intent configurationIntentStub = new Intent(); 651 when(mBackupManagerServiceMock.getConfigurationIntent(TRANSPORT_NAME)).thenReturn( 652 configurationIntentStub); 653 654 mTrampoline.initialize(UserHandle.USER_SYSTEM); 655 assertEquals(configurationIntentStub, mTrampoline.getConfigurationIntent(TRANSPORT_NAME)); 656 verify(mBackupManagerServiceMock).getConfigurationIntent(TRANSPORT_NAME); 657 } 658 659 @Test 660 public void getDestinationString_calledBeforeInitialize_ignored() throws RemoteException { 661 assertNull(mTrampoline.getDestinationString(TRANSPORT_NAME)); 662 verifyNoMoreInteractions(mBackupManagerServiceMock); 663 } 664 665 @Test 666 public void getDestinationString_forwarded() throws RemoteException { 667 when(mBackupManagerServiceMock.getDestinationString(TRANSPORT_NAME)).thenReturn( 668 DESTINATION_STRING); 669 670 mTrampoline.initialize(UserHandle.USER_SYSTEM); 671 assertEquals(DESTINATION_STRING, mTrampoline.getDestinationString(TRANSPORT_NAME)); 672 verify(mBackupManagerServiceMock).getDestinationString(TRANSPORT_NAME); 673 } 674 675 @Test 676 public void getDataManagementIntent_calledBeforeInitialize_ignored() throws RemoteException { 677 assertNull(mTrampoline.getDataManagementIntent(TRANSPORT_NAME)); 678 verifyNoMoreInteractions(mBackupManagerServiceMock); 679 } 680 681 @Test 682 public void getDataManagementIntent_forwarded() throws RemoteException { 683 Intent dataManagementIntent = new Intent(); 684 when(mBackupManagerServiceMock.getDataManagementIntent(TRANSPORT_NAME)).thenReturn( 685 dataManagementIntent); 686 687 mTrampoline.initialize(UserHandle.USER_SYSTEM); 688 assertEquals(dataManagementIntent, mTrampoline.getDataManagementIntent(TRANSPORT_NAME)); 689 verify(mBackupManagerServiceMock).getDataManagementIntent(TRANSPORT_NAME); 690 } 691 692 @Test 693 public void getDataManagementLabel_calledBeforeInitialize_ignored() throws RemoteException { 694 assertNull(mTrampoline.getDataManagementLabel(TRANSPORT_NAME)); 695 verifyNoMoreInteractions(mBackupManagerServiceMock); 696 } 697 698 @Test 699 public void getDataManagementLabel_forwarded() throws RemoteException { 700 when(mBackupManagerServiceMock.getDataManagementLabel(TRANSPORT_NAME)).thenReturn( 701 DATA_MANAGEMENT_LABEL); 702 703 mTrampoline.initialize(UserHandle.USER_SYSTEM); 704 assertEquals(DATA_MANAGEMENT_LABEL, mTrampoline.getDataManagementLabel(TRANSPORT_NAME)); 705 verify(mBackupManagerServiceMock).getDataManagementLabel(TRANSPORT_NAME); 706 } 707 708 @Test 709 public void beginRestoreSession_calledBeforeInitialize_ignored() throws RemoteException { 710 mTrampoline.beginRestoreSession(PACKAGE_NAME, TRANSPORT_NAME); 711 verifyNoMoreInteractions(mBackupManagerServiceMock); 712 } 713 714 @Test 715 public void beginRestoreSession_forwarded() throws RemoteException { 716 mTrampoline.initialize(UserHandle.USER_SYSTEM); 717 mTrampoline.beginRestoreSession(PACKAGE_NAME, TRANSPORT_NAME); 718 verify(mBackupManagerServiceMock).beginRestoreSession(PACKAGE_NAME, TRANSPORT_NAME); 719 } 720 721 @Test 722 public void opComplete_calledBeforeInitialize_ignored() throws RemoteException { 723 mTrampoline.opComplete(1, 2); 724 verifyNoMoreInteractions(mBackupManagerServiceMock); 725 } 726 727 @Test 728 public void opComplete_forwarded() throws RemoteException { 729 mTrampoline.initialize(UserHandle.USER_SYSTEM); 730 mTrampoline.opComplete(1, 2); 731 verify(mBackupManagerServiceMock).opComplete(1, 2); 732 } 733 734 @Test 735 public void getAvailableRestoreToken_calledBeforeInitialize_ignored() throws RemoteException { 736 assertEquals(0, mTrampoline.getAvailableRestoreToken(PACKAGE_NAME)); 737 verifyNoMoreInteractions(mBackupManagerServiceMock); 738 } 739 740 @Test 741 public void getAvailableRestoreToken_forwarded() throws RemoteException { 742 when(mBackupManagerServiceMock.getAvailableRestoreToken(PACKAGE_NAME)).thenReturn(123L); 743 744 mTrampoline.initialize(UserHandle.USER_SYSTEM); 745 assertEquals(123, mTrampoline.getAvailableRestoreToken(PACKAGE_NAME)); 746 verify(mBackupManagerServiceMock).getAvailableRestoreToken(PACKAGE_NAME); 747 } 748 749 @Test 750 public void isAppEligibleForBackup_calledBeforeInitialize_ignored() throws RemoteException { 751 assertFalse(mTrampoline.isAppEligibleForBackup(PACKAGE_NAME)); 752 verifyNoMoreInteractions(mBackupManagerServiceMock); 753 } 754 755 @Test 756 public void isAppEligibleForBackup_forwarded() throws RemoteException { 757 when(mBackupManagerServiceMock.isAppEligibleForBackup(PACKAGE_NAME)).thenReturn(true); 758 759 mTrampoline.initialize(UserHandle.USER_SYSTEM); 760 assertTrue(mTrampoline.isAppEligibleForBackup(PACKAGE_NAME)); 761 verify(mBackupManagerServiceMock).isAppEligibleForBackup(PACKAGE_NAME); 762 } 763 764 @Test 765 public void requestBackup_calledBeforeInitialize_ignored() throws RemoteException { 766 assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, mTrampoline.requestBackup( 767 PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)); 768 verifyNoMoreInteractions(mBackupManagerServiceMock); 769 } 770 771 @Test 772 public void requestBackup_forwarded() throws RemoteException { 773 when(mBackupManagerServiceMock.requestBackup(PACKAGE_NAMES, mBackupObserverMock, 774 mBackupManagerMonitorMock, 123)).thenReturn(456); 775 776 mTrampoline.initialize(UserHandle.USER_SYSTEM); 777 assertEquals(456, mTrampoline.requestBackup(PACKAGE_NAMES, mBackupObserverMock, 778 mBackupManagerMonitorMock, 123)); 779 verify(mBackupManagerServiceMock).requestBackup(PACKAGE_NAMES, mBackupObserverMock, 780 mBackupManagerMonitorMock, 123); 781 } 782 783 @Test 784 public void cancelBackups_calledBeforeInitialize_ignored() throws RemoteException { 785 mTrampoline.cancelBackups(); 786 verifyNoMoreInteractions(mBackupManagerServiceMock); 787 } 788 789 @Test 790 public void cancelBackups_forwarded() throws RemoteException { 791 mTrampoline.initialize(UserHandle.USER_SYSTEM); 792 mTrampoline.cancelBackups(); 793 verify(mBackupManagerServiceMock).cancelBackups(); 794 } 795 796 @Test 797 public void beginFullBackup_calledBeforeInitialize_ignored() throws RemoteException { 798 mTrampoline.beginFullBackup(new FullBackupJob()); 799 verifyNoMoreInteractions(mBackupManagerServiceMock); 800 } 801 802 @Test 803 public void beginFullBackup_forwarded() throws RemoteException { 804 FullBackupJob fullBackupJob = new FullBackupJob(); 805 when(mBackupManagerServiceMock.beginFullBackup(fullBackupJob)).thenReturn(true); 806 807 mTrampoline.initialize(UserHandle.USER_SYSTEM); 808 assertTrue(mTrampoline.beginFullBackup(fullBackupJob)); 809 verify(mBackupManagerServiceMock).beginFullBackup(fullBackupJob); 810 } 811 812 @Test 813 public void endFullBackup_calledBeforeInitialize_ignored() throws RemoteException { 814 mTrampoline.endFullBackup(); 815 verifyNoMoreInteractions(mBackupManagerServiceMock); 816 } 817 818 @Test 819 public void endFullBackup_forwarded() throws RemoteException { 820 mTrampoline.initialize(UserHandle.USER_SYSTEM); 821 mTrampoline.endFullBackup(); 822 verify(mBackupManagerServiceMock).endFullBackup(); 823 } 824 825 @Test 826 public void dump_callerDoesNotHavePermission_ignored() throws RemoteException { 827 when(mContextMock.checkCallingOrSelfPermission( 828 android.Manifest.permission.DUMP)).thenReturn( 829 PackageManager.PERMISSION_DENIED); 830 831 mTrampoline.initialize(UserHandle.USER_SYSTEM); 832 833 mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, new String[0]); 834 835 verifyNoMoreInteractions(mBackupManagerServiceMock); 836 } 837 838 @Test 839 public void dump_calledBeforeInitialize_ignored() throws RemoteException { 840 when(mContextMock.checkCallingOrSelfPermission( 841 android.Manifest.permission.DUMP)).thenReturn( 842 PackageManager.PERMISSION_GRANTED); 843 844 mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, new String[0]); 845 846 verifyNoMoreInteractions(mBackupManagerServiceMock); 847 } 848 849 @Test 850 public void dump_callerHasPermission_forwarded() throws RemoteException { 851 when(mContextMock.checkCallingOrSelfPermission( 852 android.Manifest.permission.DUMP)).thenReturn( 853 PackageManager.PERMISSION_GRANTED); 854 855 mTrampoline.initialize(UserHandle.USER_SYSTEM); 856 857 mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, null); 858 859 verify(mBackupManagerServiceMock).dump(mFileDescriptorStub, mPrintWriterMock, null); 860 } 861 862 private static class TrampolineTestable extends Trampoline { 863 static boolean sBackupDisabled = false; 864 static File sSuppressFile = null; 865 static int sCallingUid = -1; 866 static BackupManagerService sBackupManagerServiceMock = null; 867 private int mCreateServiceCallsCount = 0; 868 869 TrampolineTestable(Context context) { 870 super(context); 871 } 872 873 @Override 874 public boolean isBackupDisabled() { 875 return sBackupDisabled; 876 } 877 878 @Override 879 public File getSuppressFile() { 880 return sSuppressFile; 881 } 882 883 @Override 884 protected int binderGetCallingUid() { 885 return sCallingUid; 886 } 887 888 @Override 889 protected BackupManagerServiceInterface createBackupManagerService() { 890 mCreateServiceCallsCount++; 891 return sBackupManagerServiceMock; 892 } 893 894 int getCreateServiceCallsCount() { 895 return mCreateServiceCallsCount; 896 } 897 } 898} 899