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