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 |
| 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 |
Tony Mak | 8b20b2c | 2017-05-02 14:15:10 +0100 | [diff] [blame] | 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 | }); |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 595 | verifyNoMoreInteractions(mBackupManagerServiceMock); |
Tony Mak | 8b20b2c | 2017-05-02 14:15:10 +0100 | [diff] [blame] | 596 | Integer errorCode = q.poll(5, TimeUnit.SECONDS); |
| 597 | assertNotNull(errorCode); |
| 598 | assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, (int) errorCode); |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | @Test |
Tony Mak | 2dd109d | 2017-05-04 11:42:31 +0100 | [diff] [blame] | 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 |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 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 | |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 764 | @Test |
| 765 | public void requestBackup_calledBeforeInitialize_ignored() throws RemoteException { |
Artem Iglikov | c92eb46 | 2017-04-21 09:56:35 +0100 | [diff] [blame] | 766 | assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, mTrampoline.requestBackup( |
| 767 | PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)); |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 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 | |
Artem Iglikov | e5263c6 | 2017-04-21 13:23:16 +0100 | [diff] [blame] | 825 | @Test |
| 826 | public void dump_callerDoesNotHavePermission_ignored() throws RemoteException { |
| 827 | when(mContextMock.checkCallingOrSelfPermission( |
| 828 | android.Manifest.permission.DUMP)).thenReturn( |
| 829 | PackageManager.PERMISSION_DENIED); |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 830 | |
Artem Iglikov | e5263c6 | 2017-04-21 13:23:16 +0100 | [diff] [blame] | 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 boolean sRefactoredServiceEnabled = false; |
| 865 | static File sSuppressFile = null; |
| 866 | static int sCallingUid = -1; |
| 867 | static BackupManagerService sBackupManagerServiceMock = null; |
| 868 | static RefactoredBackupManagerService sRefactoredBackupManagerServiceMock = null; |
| 869 | private int mCreateServiceCallsCount = 0; |
| 870 | |
| 871 | TrampolineTestable(Context context) { |
| 872 | super(context); |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | @Override |
| 876 | protected BackupManagerServiceInterface createService() { |
Artem Iglikov | e5263c6 | 2017-04-21 13:23:16 +0100 | [diff] [blame] | 877 | mCreateServiceCallsCount++; |
| 878 | return super.createService(); |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public boolean isBackupDisabled() { |
| 883 | return sBackupDisabled; |
| 884 | } |
| 885 | |
| 886 | @Override |
| 887 | public File getSuppressFile() { |
| 888 | return sSuppressFile; |
| 889 | } |
| 890 | |
| 891 | @Override |
| 892 | protected int binderGetCallingUid() { |
| 893 | return sCallingUid; |
| 894 | } |
| 895 | |
| 896 | @Override |
| 897 | protected boolean isRefactoredServiceEnabled() { |
| 898 | return sRefactoredServiceEnabled; |
| 899 | } |
| 900 | |
| 901 | @Override |
| 902 | protected BackupManagerServiceInterface createRefactoredBackupManagerService() { |
| 903 | return sRefactoredBackupManagerServiceMock; |
| 904 | } |
| 905 | |
| 906 | @Override |
| 907 | protected BackupManagerServiceInterface createBackupManagerService() { |
| 908 | return sBackupManagerServiceMock; |
| 909 | } |
| 910 | |
| 911 | int getCreateServiceCallsCount() { |
| 912 | return mCreateServiceCallsCount; |
Artem Iglikov | 6052ef5 | 2017-04-20 17:23:39 +0100 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | } |