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;
21 import static junit.framework.Assert.assertNotNull;
22 import static junit.framework.Assert.assertNull;
23 import static junit.framework.Assert.assertTrue;
24 import static junit.framework.Assert.fail;
25 
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoMoreInteractions;
33 import static org.mockito.Mockito.when;
34 
35 import android.Manifest;
36 import android.annotation.UserIdInt;
37 import android.app.backup.BackupManager;
38 import android.app.backup.IBackupManagerMonitor;
39 import android.app.backup.IBackupObserver;
40 import android.app.backup.IFullBackupRestoreObserver;
41 import android.app.backup.ISelectBackupTransportCallback;
42 import android.content.ComponentName;
43 import android.content.Context;
44 import android.content.Intent;
45 import android.content.pm.PackageManager;
46 import android.content.pm.UserInfo;
47 import android.os.IBinder;
48 import android.os.ParcelFileDescriptor;
49 import android.os.Process;
50 import android.os.RemoteException;
51 import android.os.UserHandle;
52 import android.os.UserManager;
53 import android.platform.test.annotations.Presubmit;
54 import android.util.SparseArray;
55 
56 import androidx.test.InstrumentationRegistry;
57 import androidx.test.filters.SmallTest;
58 import androidx.test.runner.AndroidJUnit4;
59 
60 import com.android.server.backup.utils.RandomAccessFileUtils;
61 
62 import org.junit.After;
63 import org.junit.Before;
64 import org.junit.Test;
65 import org.junit.runner.RunWith;
66 import org.mockito.Mock;
67 import org.mockito.MockitoAnnotations;
68 
69 import java.io.File;
70 import java.io.FileDescriptor;
71 import java.io.IOException;
72 import java.io.PrintWriter;
73 import java.util.concurrent.LinkedBlockingQueue;
74 import java.util.concurrent.TimeUnit;
75 
76 @SmallTest
77 @Presubmit
78 @RunWith(AndroidJUnit4.class)
79 public class TrampolineTest {
80     private static final String PACKAGE_NAME = "some.package.name";
81     private static final String TRANSPORT_NAME = "some.transport.name";
82     private static final String CURRENT_PASSWORD = "current_password";
83     private static final String NEW_PASSWORD = "new_password";
84     private static final String ENCRYPTION_PASSWORD = "encryption_password";
85     private static final CharSequence DATA_MANAGEMENT_LABEL = "data_management_label";
86     private static final String DESTINATION_STRING = "destination_string";
87     private static final String[] PACKAGE_NAMES =
88             new String[]{"some.package.name._1", "some.package.name._2"};
89     private static final String[] TRANSPORTS =
90             new String[]{"some.transport.name._1", "some.transport.name._2"};
91     private static final ComponentName TRANSPORT_COMPONENT_NAME = new ComponentName("package",
92             "class");
93     private static final ComponentName[] TRANSPORT_COMPONENTS = new ComponentName[]{
94             new ComponentName("package1", "class1"),
95             new ComponentName("package2", "class2")
96     };
97     private static final int NON_USER_SYSTEM = UserHandle.USER_SYSTEM + 1;
98     private static final int UNSTARTED_NON_USER_SYSTEM = UserHandle.USER_SYSTEM + 2;
99 
100     @UserIdInt
101     private int mUserId;
102     @Mock
103     private BackupManagerService mBackupManagerServiceMock;
104     @Mock
105     private UserBackupManagerService mUserBackupManagerService;
106     @Mock
107     private Context mContextMock;
108     @Mock
109     private IBinder mAgentMock;
110     @Mock
111     private ParcelFileDescriptor mParcelFileDescriptorMock;
112     @Mock
113     private IFullBackupRestoreObserver mFullBackupRestoreObserverMock;
114     @Mock
115     private IBackupObserver mBackupObserverMock;
116     @Mock
117     private IBackupManagerMonitor mBackupManagerMonitorMock;
118     @Mock
119     private PrintWriter mPrintWriterMock;
120     @Mock
121     private UserManager mUserManagerMock;
122     @Mock
123     private UserInfo mUserInfoMock;
124 
125     private FileDescriptor mFileDescriptorStub = new FileDescriptor();
126 
127     private TrampolineTestable mTrampoline;
128     private File mTestDir;
129     private File mSuppressFile;
130 
131     @Before
setUp()132     public void setUp() throws Exception {
133         MockitoAnnotations.initMocks(this);
134         mUserId = UserHandle.USER_SYSTEM;
135 
136         SparseArray<UserBackupManagerService> serviceUsers = new SparseArray<>();
137         serviceUsers.append(UserHandle.USER_SYSTEM, mUserBackupManagerService);
138         serviceUsers.append(NON_USER_SYSTEM, mUserBackupManagerService);
139         when(mBackupManagerServiceMock.getServiceUsers()).thenReturn(serviceUsers);
140 
141         when(mUserManagerMock.getUserInfo(UserHandle.USER_SYSTEM)).thenReturn(mUserInfoMock);
142         when(mUserManagerMock.getUserInfo(NON_USER_SYSTEM)).thenReturn(mUserInfoMock);
143         when(mUserManagerMock.getUserInfo(UNSTARTED_NON_USER_SYSTEM)).thenReturn(mUserInfoMock);
144 
145         TrampolineTestable.sBackupManagerServiceMock = mBackupManagerServiceMock;
146         TrampolineTestable.sCallingUserId = UserHandle.USER_SYSTEM;
147         TrampolineTestable.sCallingUid = Process.SYSTEM_UID;
148         TrampolineTestable.sBackupDisabled = false;
149         TrampolineTestable.sUserManagerMock = mUserManagerMock;
150 
151         mTestDir = InstrumentationRegistry.getContext().getFilesDir();
152         mTestDir.mkdirs();
153 
154         mSuppressFile = new File(mTestDir, "suppress");
155         TrampolineTestable.sSuppressFile = mSuppressFile;
156 
157         setUpStateFilesForNonSystemUser(NON_USER_SYSTEM);
158         setUpStateFilesForNonSystemUser(UNSTARTED_NON_USER_SYSTEM);
159 
160         mTrampoline = new TrampolineTestable(mContextMock);
161     }
162 
setUpStateFilesForNonSystemUser(int userId)163     private void setUpStateFilesForNonSystemUser(int userId) {
164         File activatedFile = new File(mTestDir, "activate-" + userId);
165         TrampolineTestable.sActivatedFiles.append(userId, activatedFile);
166         File rememberActivatedFile = new File(mTestDir, "rem-activate-" + userId);
167         TrampolineTestable.sRememberActivatedFiles.append(userId, rememberActivatedFile);
168     }
169 
170     @After
tearDown()171     public void tearDown() throws Exception {
172         mSuppressFile.delete();
173         deleteFiles(TrampolineTestable.sActivatedFiles);
174         deleteFiles(TrampolineTestable.sRememberActivatedFiles);
175     }
176 
deleteFiles(SparseArray<File> files)177     private void deleteFiles(SparseArray<File> files) {
178         int numFiles = files.size();
179         for (int i = 0; i < numFiles; i++) {
180             files.valueAt(i).delete();
181         }
182     }
183 
184     @Test
initializeService_successfullyInitializesBackupService()185     public void initializeService_successfullyInitializesBackupService() {
186         mTrampoline.initializeService();
187 
188         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
189     }
190 
191     @Test
initializeService_globallyDisabled_nonInitialized()192     public void initializeService_globallyDisabled_nonInitialized() {
193         TrampolineTestable.sBackupDisabled = true;
194         TrampolineTestable trampoline = new TrampolineTestable(mContextMock);
195 
196         trampoline.initializeService();
197 
198         assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
199     }
200 
201     @Test
initializeService_doesNotStartServiceForUsers()202     public void initializeService_doesNotStartServiceForUsers() {
203         mTrampoline.initializeService();
204 
205         verify(mBackupManagerServiceMock, never()).startServiceForUser(anyInt());
206     }
207 
208     @Test
isBackupServiceActive_calledBeforeInitialize_returnsFalse()209     public void isBackupServiceActive_calledBeforeInitialize_returnsFalse() {
210         assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
211     }
212 
213     @Test
isBackupServiceActive_forSystemUser_returnsTrueWhenActivated()214     public void isBackupServiceActive_forSystemUser_returnsTrueWhenActivated() throws Exception {
215         mTrampoline.initializeService();
216         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
217 
218         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
219     }
220 
221     @Test
isBackupServiceActive_forSystemUser_returnsFalseWhenDeactivated()222     public void isBackupServiceActive_forSystemUser_returnsFalseWhenDeactivated() throws Exception {
223         mTrampoline.initializeService();
224         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false);
225 
226         assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
227     }
228 
229     @Test
isBackupServiceActive_forNonSystemUser_returnsFalseWhenSystemUserDeactivated()230     public void isBackupServiceActive_forNonSystemUser_returnsFalseWhenSystemUserDeactivated()
231             throws Exception {
232         mTrampoline.initializeService();
233         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false);
234         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
235 
236         assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
237     }
238 
239     @Test
isBackupServiceActive_forNonSystemUser_returnsFalseWhenNonSystemUserDeactivated()240     public void isBackupServiceActive_forNonSystemUser_returnsFalseWhenNonSystemUserDeactivated()
241             throws Exception {
242         mTrampoline.initializeService();
243         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
244         // Don't activate non-system user.
245 
246         assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
247     }
248 
249     @Test
250     public void
isBackupServiceActive_forNonSystemUser_returnsTrueWhenSystemAndNonSystemUserActivated()251             isBackupServiceActive_forNonSystemUser_returnsTrueWhenSystemAndNonSystemUserActivated()
252                 throws Exception {
253         mTrampoline.initializeService();
254         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
255         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
256 
257         assertTrue(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
258     }
259 
260     @Test
261     public void
isBackupServiceActive_forUnstartedNonSystemUser_returnsTrueWhenSystemAndUserActivated()262             isBackupServiceActive_forUnstartedNonSystemUser_returnsTrueWhenSystemAndUserActivated()
263             throws Exception {
264         mTrampoline.initializeService();
265         mTrampoline.setBackupServiceActive(UNSTARTED_NON_USER_SYSTEM, true);
266 
267         assertTrue(mTrampoline.isBackupServiceActive(UNSTARTED_NON_USER_SYSTEM));
268     }
269 
270     @Test
setBackupServiceActive_forSystemUserAndCallerSystemUid_serviceCreated()271     public void setBackupServiceActive_forSystemUserAndCallerSystemUid_serviceCreated() {
272         mTrampoline.initializeService();
273         TrampolineTestable.sCallingUid = Process.SYSTEM_UID;
274 
275         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
276 
277         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
278     }
279 
280     @Test
setBackupServiceActive_forSystemUserAndCallerRootUid_serviceCreated()281     public void setBackupServiceActive_forSystemUserAndCallerRootUid_serviceCreated() {
282         mTrampoline.initializeService();
283         TrampolineTestable.sCallingUid = Process.ROOT_UID;
284 
285         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
286 
287         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
288     }
289 
290     @Test
setBackupServiceActive_forSystemUserAndCallerNonRootNonSystem_throws()291     public void setBackupServiceActive_forSystemUserAndCallerNonRootNonSystem_throws() {
292         mTrampoline.initializeService();
293         TrampolineTestable.sCallingUid = Process.FIRST_APPLICATION_UID;
294 
295         try {
296             mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
297             fail();
298         } catch (SecurityException expected) {
299         }
300     }
301 
302     @Test
setBackupServiceActive_forManagedProfileAndCallerSystemUid_serviceCreated()303     public void setBackupServiceActive_forManagedProfileAndCallerSystemUid_serviceCreated() {
304         when(mUserInfoMock.isManagedProfile()).thenReturn(true);
305         mTrampoline.initializeService();
306         TrampolineTestable.sCallingUid = Process.SYSTEM_UID;
307 
308         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
309 
310         assertTrue(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
311     }
312 
313     @Test
setBackupServiceActive_forManagedProfileAndCallerRootUid_serviceCreated()314     public void setBackupServiceActive_forManagedProfileAndCallerRootUid_serviceCreated() {
315         when(mUserInfoMock.isManagedProfile()).thenReturn(true);
316         mTrampoline.initializeService();
317         TrampolineTestable.sCallingUid = Process.ROOT_UID;
318 
319         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
320 
321         assertTrue(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
322     }
323 
324     @Test
setBackupServiceActive_forManagedProfileAndCallerNonRootNonSystem_throws()325     public void setBackupServiceActive_forManagedProfileAndCallerNonRootNonSystem_throws() {
326         when(mUserInfoMock.isManagedProfile()).thenReturn(true);
327         mTrampoline.initializeService();
328         TrampolineTestable.sCallingUid = Process.FIRST_APPLICATION_UID;
329 
330         try {
331             mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
332             fail();
333         } catch (SecurityException expected) {
334         }
335     }
336 
337     @Test
setBackupServiceActive_forNonSystemUserAndCallerWithoutBackupPermission_throws()338     public void setBackupServiceActive_forNonSystemUserAndCallerWithoutBackupPermission_throws() {
339         doThrow(new SecurityException())
340                 .when(mContextMock)
341                 .enforceCallingOrSelfPermission(eq(Manifest.permission.BACKUP), anyString());
342         mTrampoline.initializeService();
343 
344         try {
345             mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
346             fail();
347         } catch (SecurityException expected) {
348         }
349     }
350 
351     @Test
setBackupServiceActive_forNonSystemUserAndCallerWithoutUserPermission_throws()352     public void setBackupServiceActive_forNonSystemUserAndCallerWithoutUserPermission_throws() {
353         doThrow(new SecurityException())
354                 .when(mContextMock)
355                 .enforceCallingOrSelfPermission(
356                         eq(Manifest.permission.INTERACT_ACROSS_USERS_FULL), anyString());
357         mTrampoline.initializeService();
358 
359         try {
360             mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
361             fail();
362         } catch (SecurityException expected) {
363         }
364     }
365 
366     @Test
setBackupServiceActive_backupDisabled_ignored()367     public void setBackupServiceActive_backupDisabled_ignored() {
368         TrampolineTestable.sBackupDisabled = true;
369         TrampolineTestable trampoline = new TrampolineTestable(mContextMock);
370         trampoline.initializeService();
371 
372         trampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
373 
374         assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
375     }
376 
377     @Test
setBackupServiceActive_alreadyActive_ignored()378     public void setBackupServiceActive_alreadyActive_ignored() {
379         mTrampoline.initializeService();
380         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
381         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
382         assertEquals(1, mTrampoline.getCreateServiceCallsCount());
383 
384         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
385         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
386         assertEquals(1, mTrampoline.getCreateServiceCallsCount());
387     }
388 
389     @Test
setBackupServiceActive_makeNonActive_alreadyNonActive_ignored()390     public void setBackupServiceActive_makeNonActive_alreadyNonActive_ignored() {
391         mTrampoline.initializeService();
392         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false);
393         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false);
394 
395         assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
396     }
397 
398     @Test
setBackupServiceActive_makeActive_serviceCreatedAndSuppressFileDeleted()399     public void setBackupServiceActive_makeActive_serviceCreatedAndSuppressFileDeleted() {
400         mTrampoline.initializeService();
401         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
402 
403         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
404     }
405 
406     @Test
setBackupServiceActive_makeNonActive_serviceDeletedAndSuppressFileCreated()407     public void setBackupServiceActive_makeNonActive_serviceDeletedAndSuppressFileCreated()
408             throws IOException {
409         mTrampoline.initializeService();
410         assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
411 
412         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false);
413 
414         assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
415     }
416 
417     @Test
setBackupActive_nonSystemUser_disabledForSystemUser_ignored()418     public void setBackupActive_nonSystemUser_disabledForSystemUser_ignored() {
419         mTrampoline.initializeService();
420         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false);
421         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
422 
423         assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
424     }
425 
426     @Test
setBackupServiceActive_forOneNonSystemUser_doesNotActivateForAllNonSystemUsers()427     public void setBackupServiceActive_forOneNonSystemUser_doesNotActivateForAllNonSystemUsers() {
428         mTrampoline.initializeService();
429         int otherUser = NON_USER_SYSTEM + 1;
430         File activateFile = new File(mTestDir, "activate-" + otherUser);
431         TrampolineTestable.sActivatedFiles.append(otherUser, activateFile);
432         mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true);
433 
434         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
435 
436         assertTrue(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM));
437         assertFalse(mTrampoline.isBackupServiceActive(otherUser));
438         activateFile.delete();
439     }
440 
441     @Test
setBackupServiceActive_forNonSystemUser_remembersActivated()442     public void setBackupServiceActive_forNonSystemUser_remembersActivated() {
443         mTrampoline.initializeService();
444 
445         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
446 
447         assertTrue(RandomAccessFileUtils.readBoolean(
448                 TrampolineTestable.sRememberActivatedFiles.get(NON_USER_SYSTEM), false));
449     }
450 
451     @Test
setBackupServiceActiveFalse_forNonSystemUser_remembersActivated()452     public void setBackupServiceActiveFalse_forNonSystemUser_remembersActivated() {
453         mTrampoline.initializeService();
454 
455         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, false);
456 
457         assertFalse(RandomAccessFileUtils.readBoolean(
458                 TrampolineTestable.sRememberActivatedFiles.get(NON_USER_SYSTEM), true));
459     }
460 
461     @Test
setBackupServiceActiveTwice_forNonSystemUser_remembersLastActivated()462     public void setBackupServiceActiveTwice_forNonSystemUser_remembersLastActivated() {
463         mTrampoline.initializeService();
464 
465         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true);
466         mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, false);
467 
468         assertFalse(RandomAccessFileUtils.readBoolean(
469                 TrampolineTestable.sRememberActivatedFiles.get(NON_USER_SYSTEM), true));
470     }
471 
472     @Test
dataChanged_calledBeforeInitialize_ignored()473     public void dataChanged_calledBeforeInitialize_ignored() throws Exception {
474         mTrampoline.dataChanged(PACKAGE_NAME);
475         verifyNoMoreInteractions(mBackupManagerServiceMock);
476     }
477 
478     @Test
dataChangedForUser_forwarded()479     public void dataChangedForUser_forwarded() throws Exception {
480         mTrampoline.initializeService();
481 
482         mTrampoline.dataChangedForUser(mUserId, PACKAGE_NAME);
483 
484         verify(mBackupManagerServiceMock).dataChanged(mUserId, PACKAGE_NAME);
485     }
486 
487     @Test
dataChanged_forwarded()488     public void dataChanged_forwarded() throws Exception {
489         TrampolineTestable.sCallingUserId = mUserId;
490         mTrampoline.initializeService();
491 
492         mTrampoline.dataChanged(PACKAGE_NAME);
493 
494         verify(mBackupManagerServiceMock).dataChanged(mUserId, PACKAGE_NAME);
495     }
496 
497     @Test
clearBackupData_calledBeforeInitialize_ignored()498     public void clearBackupData_calledBeforeInitialize_ignored() throws Exception {
499         mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME);
500         verifyNoMoreInteractions(mBackupManagerServiceMock);
501     }
502 
503     @Test
clearBackupDataForUser_forwarded()504     public void clearBackupDataForUser_forwarded() throws Exception {
505         mTrampoline.initializeService();
506 
507         mTrampoline.clearBackupDataForUser(mUserId, TRANSPORT_NAME, PACKAGE_NAME);
508 
509         verify(mBackupManagerServiceMock).clearBackupData(mUserId, TRANSPORT_NAME, PACKAGE_NAME);
510     }
511 
512     @Test
clearBackupData_forwarded()513     public void clearBackupData_forwarded() throws Exception {
514         TrampolineTestable.sCallingUserId = mUserId;
515         mTrampoline.initializeService();
516 
517         mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME);
518 
519         verify(mBackupManagerServiceMock).clearBackupData(mUserId, TRANSPORT_NAME, PACKAGE_NAME);
520     }
521 
522     @Test
agentConnected_calledBeforeInitialize_ignored()523     public void agentConnected_calledBeforeInitialize_ignored() throws Exception {
524         mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock);
525         verifyNoMoreInteractions(mBackupManagerServiceMock);
526     }
527 
528     @Test
agentConnectedForUser_forwarded()529     public void agentConnectedForUser_forwarded() throws Exception {
530         mTrampoline.initializeService();
531 
532         mTrampoline.agentConnectedForUser(mUserId, PACKAGE_NAME, mAgentMock);
533 
534         verify(mBackupManagerServiceMock).agentConnected(mUserId, PACKAGE_NAME, mAgentMock);
535     }
536 
537     @Test
agentConnected_forwarded()538     public void agentConnected_forwarded() throws Exception {
539         TrampolineTestable.sCallingUserId = mUserId;
540         mTrampoline.initializeService();
541 
542         mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock);
543 
544         verify(mBackupManagerServiceMock).agentConnected(mUserId, PACKAGE_NAME, mAgentMock);
545     }
546 
547     @Test
agentDisconnected_calledBeforeInitialize_ignored()548     public void agentDisconnected_calledBeforeInitialize_ignored() throws Exception {
549         mTrampoline.agentDisconnected(PACKAGE_NAME);
550         verifyNoMoreInteractions(mBackupManagerServiceMock);
551     }
552 
553     @Test
agentDisconnectedForUser_forwarded()554     public void agentDisconnectedForUser_forwarded() throws Exception {
555         mTrampoline.initializeService();
556 
557         mTrampoline.agentDisconnectedForUser(mUserId, PACKAGE_NAME);
558 
559         verify(mBackupManagerServiceMock).agentDisconnected(mUserId, PACKAGE_NAME);
560     }
561 
562     @Test
agentDisconnected_forwarded()563     public void agentDisconnected_forwarded() throws Exception {
564         TrampolineTestable.sCallingUserId = mUserId;
565         mTrampoline.initializeService();
566 
567         mTrampoline.agentDisconnected(PACKAGE_NAME);
568 
569         verify(mBackupManagerServiceMock).agentDisconnected(mUserId, PACKAGE_NAME);
570     }
571 
572     @Test
restoreAtInstall_calledBeforeInitialize_ignored()573     public void restoreAtInstall_calledBeforeInitialize_ignored() throws Exception {
574         mTrampoline.restoreAtInstall(PACKAGE_NAME, 123);
575         verifyNoMoreInteractions(mBackupManagerServiceMock);
576     }
577 
578     @Test
restoreAtInstallForUser_forwarded()579     public void restoreAtInstallForUser_forwarded() throws Exception {
580         mTrampoline.initializeService();
581 
582         mTrampoline.restoreAtInstallForUser(mUserId, PACKAGE_NAME, 123);
583 
584         verify(mBackupManagerServiceMock).restoreAtInstall(mUserId, PACKAGE_NAME, 123);
585     }
586 
587     @Test
restoreAtInstall_forwarded()588     public void restoreAtInstall_forwarded() throws Exception {
589         TrampolineTestable.sCallingUserId = mUserId;
590         mTrampoline.initializeService();
591 
592         mTrampoline.restoreAtInstall(PACKAGE_NAME, 123);
593 
594         verify(mBackupManagerServiceMock).restoreAtInstall(mUserId, PACKAGE_NAME, 123);
595     }
596 
597     @Test
setBackupEnabled_calledBeforeInitialize_ignored()598     public void setBackupEnabled_calledBeforeInitialize_ignored() throws Exception {
599         mTrampoline.setBackupEnabled(true);
600         verifyNoMoreInteractions(mBackupManagerServiceMock);
601     }
602 
603     @Test
setBackupEnabledForUser_forwarded()604     public void setBackupEnabledForUser_forwarded() throws Exception {
605         mTrampoline.initializeService();
606 
607         mTrampoline.setBackupEnabledForUser(mUserId, true);
608 
609         verify(mBackupManagerServiceMock).setBackupEnabled(mUserId, true);
610     }
611 
612     @Test
setBackupEnabled_forwardedToCallingUserId()613     public void setBackupEnabled_forwardedToCallingUserId() throws Exception {
614         TrampolineTestable.sCallingUserId = mUserId;
615         mTrampoline.initializeService();
616 
617         mTrampoline.setBackupEnabled(true);
618 
619         verify(mBackupManagerServiceMock).setBackupEnabled(mUserId, true);
620     }
621 
622     @Test
setAutoRestore_calledBeforeInitialize_ignored()623     public void setAutoRestore_calledBeforeInitialize_ignored() throws Exception {
624         mTrampoline.setAutoRestore(true);
625         verifyNoMoreInteractions(mBackupManagerServiceMock);
626     }
627 
628     @Test
setAutoRestoreForUser_forwarded()629     public void setAutoRestoreForUser_forwarded() throws Exception {
630         mTrampoline.initializeService();
631 
632         mTrampoline.setAutoRestoreForUser(mUserId, true);
633 
634         verify(mBackupManagerServiceMock).setAutoRestore(mUserId, true);
635     }
636 
637     @Test
setAutoRestore_forwarded()638     public void setAutoRestore_forwarded() throws Exception {
639         TrampolineTestable.sCallingUserId = mUserId;
640         mTrampoline.initializeService();
641 
642         mTrampoline.setAutoRestore(true);
643 
644         verify(mBackupManagerServiceMock).setAutoRestore(mUserId, true);
645     }
646 
647     @Test
isBackupEnabled_calledBeforeInitialize_ignored()648     public void isBackupEnabled_calledBeforeInitialize_ignored() throws Exception {
649         assertFalse(mTrampoline.isBackupEnabled());
650         verifyNoMoreInteractions(mBackupManagerServiceMock);
651     }
652 
653     @Test
isBackupEnabledForUser_forwarded()654     public void isBackupEnabledForUser_forwarded() throws Exception {
655         mTrampoline.initializeService();
656 
657         mTrampoline.isBackupEnabledForUser(mUserId);
658 
659         verify(mBackupManagerServiceMock).isBackupEnabled(mUserId);
660     }
661 
662     @Test
isBackupEnabled_forwardedToCallingUserId()663     public void isBackupEnabled_forwardedToCallingUserId() throws Exception {
664         TrampolineTestable.sCallingUserId = mUserId;
665         mTrampoline.initializeService();
666 
667         mTrampoline.isBackupEnabled();
668 
669         verify(mBackupManagerServiceMock).isBackupEnabled(mUserId);
670     }
671 
672     @Test
setBackupPassword_calledBeforeInitialize_ignored()673     public void setBackupPassword_calledBeforeInitialize_ignored() throws Exception {
674         mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
675         verifyNoMoreInteractions(mBackupManagerServiceMock);
676     }
677 
678     @Test
setBackupPassword_forwarded()679     public void setBackupPassword_forwarded() throws Exception {
680         mTrampoline.initializeService();
681         mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
682         verify(mBackupManagerServiceMock).setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
683     }
684 
685     @Test
hasBackupPassword_calledBeforeInitialize_ignored()686     public void hasBackupPassword_calledBeforeInitialize_ignored() throws Exception {
687         assertFalse(mTrampoline.hasBackupPassword());
688         verifyNoMoreInteractions(mBackupManagerServiceMock);
689     }
690 
691     @Test
hasBackupPassword_forwarded()692     public void hasBackupPassword_forwarded() throws Exception {
693         mTrampoline.initializeService();
694         mTrampoline.hasBackupPassword();
695         verify(mBackupManagerServiceMock).hasBackupPassword();
696     }
697 
698     @Test
backupNow_calledBeforeInitialize_ignored()699     public void backupNow_calledBeforeInitialize_ignored() throws Exception {
700         mTrampoline.backupNow();
701         verifyNoMoreInteractions(mBackupManagerServiceMock);
702     }
703 
704     @Test
backupNowForUser_forwarded()705     public void backupNowForUser_forwarded() throws Exception {
706         mTrampoline.initializeService();
707 
708         mTrampoline.backupNowForUser(mUserId);
709 
710         verify(mBackupManagerServiceMock).backupNow(mUserId);
711     }
712 
713     @Test
backupNow_forwardedToCallingUserId()714     public void backupNow_forwardedToCallingUserId() throws Exception {
715         TrampolineTestable.sCallingUserId = mUserId;
716         mTrampoline.initializeService();
717 
718         mTrampoline.backupNow();
719 
720         verify(mBackupManagerServiceMock).backupNow(mUserId);
721     }
722 
723     @Test
adbBackup_calledBeforeInitialize_ignored()724     public void adbBackup_calledBeforeInitialize_ignored() throws Exception {
725         mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true,
726                 true, true, true, true, true, true,
727                 PACKAGE_NAMES);
728         verifyNoMoreInteractions(mBackupManagerServiceMock);
729     }
730 
731     @Test
adbBackup_forwarded()732     public void adbBackup_forwarded() throws Exception {
733         mTrampoline.initializeService();
734         mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true,
735                 true, true, true, true, true, true,
736                 PACKAGE_NAMES);
737         verify(mBackupManagerServiceMock).adbBackup(mUserId, mParcelFileDescriptorMock, true,
738                 true, true, true, true, true, true, true, PACKAGE_NAMES);
739     }
740 
741     @Test
fullTransportBackup_calledBeforeInitialize_ignored()742     public void fullTransportBackup_calledBeforeInitialize_ignored() throws Exception {
743         mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES);
744         verifyNoMoreInteractions(mBackupManagerServiceMock);
745     }
746 
747     @Test
fullTransportBackupForUser_forwarded()748     public void fullTransportBackupForUser_forwarded() throws Exception {
749         mTrampoline.initializeService();
750 
751         mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES);
752 
753         verify(mBackupManagerServiceMock).fullTransportBackup(mUserId, PACKAGE_NAMES);
754     }
755 
756     @Test
adbRestore_calledBeforeInitialize_ignored()757     public void adbRestore_calledBeforeInitialize_ignored() throws Exception {
758         mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock);
759         verifyNoMoreInteractions(mBackupManagerServiceMock);
760     }
761 
762     @Test
adbRestore_forwarded()763     public void adbRestore_forwarded() throws Exception {
764         mTrampoline.initializeService();
765         mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock);
766         verify(mBackupManagerServiceMock).adbRestore(mUserId, mParcelFileDescriptorMock);
767     }
768 
769     @Test
acknowledgeFullBackupOrRestore_calledBeforeInitialize_ignored()770     public void acknowledgeFullBackupOrRestore_calledBeforeInitialize_ignored()
771             throws Exception {
772         mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD,
773                 mFullBackupRestoreObserverMock);
774         verifyNoMoreInteractions(mBackupManagerServiceMock);
775     }
776 
777     @Test
acknowledgeFullBackupOrRestoreForUser_forwarded()778     public void acknowledgeFullBackupOrRestoreForUser_forwarded() throws Exception {
779         mTrampoline.initializeService();
780 
781         mTrampoline.acknowledgeFullBackupOrRestoreForUser(
782                 mUserId,
783                 123,
784                 true,
785                 CURRENT_PASSWORD,
786                 ENCRYPTION_PASSWORD,
787                 mFullBackupRestoreObserverMock);
788 
789         verify(mBackupManagerServiceMock)
790                 .acknowledgeAdbBackupOrRestore(
791                         mUserId,
792                         123,
793                         true,
794                         CURRENT_PASSWORD,
795                         ENCRYPTION_PASSWORD,
796                         mFullBackupRestoreObserverMock);
797     }
798 
799     @Test
acknowledgeFullBackupOrRestore_forwarded()800     public void acknowledgeFullBackupOrRestore_forwarded() throws Exception {
801         TrampolineTestable.sCallingUserId = mUserId;
802         mTrampoline.initializeService();
803 
804         mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD,
805                 mFullBackupRestoreObserverMock);
806 
807         verify(mBackupManagerServiceMock)
808                 .acknowledgeAdbBackupOrRestore(
809                         mUserId,
810                         123,
811                         true,
812                         CURRENT_PASSWORD,
813                         ENCRYPTION_PASSWORD,
814                         mFullBackupRestoreObserverMock);
815     }
816 
817     @Test
getCurrentTransport_calledBeforeInitialize_ignored()818     public void getCurrentTransport_calledBeforeInitialize_ignored() throws Exception {
819         assertNull(mTrampoline.getCurrentTransport());
820         verifyNoMoreInteractions(mBackupManagerServiceMock);
821     }
822 
823     @Test
getCurrentTransportForUser_forwarded()824     public void getCurrentTransportForUser_forwarded() throws Exception {
825         when(mBackupManagerServiceMock.getCurrentTransport(mUserId)).thenReturn(TRANSPORT_NAME);
826         mTrampoline.initializeService();
827 
828         assertEquals(TRANSPORT_NAME, mTrampoline.getCurrentTransportForUser(mUserId));
829         verify(mBackupManagerServiceMock).getCurrentTransport(mUserId);
830     }
831 
832     @Test
getCurrentTransport_forwarded()833     public void getCurrentTransport_forwarded() throws Exception {
834         TrampolineTestable.sCallingUserId = mUserId;
835         when(mBackupManagerServiceMock.getCurrentTransport(mUserId)).thenReturn(TRANSPORT_NAME);
836         mTrampoline.initializeService();
837 
838         assertEquals(TRANSPORT_NAME, mTrampoline.getCurrentTransport());
839         verify(mBackupManagerServiceMock).getCurrentTransport(mUserId);
840     }
841 
842     @Test
listAllTransports_calledBeforeInitialize_ignored()843     public void listAllTransports_calledBeforeInitialize_ignored() throws Exception {
844         assertNull(mTrampoline.listAllTransports());
845         verifyNoMoreInteractions(mBackupManagerServiceMock);
846     }
847 
848     @Test
listAllTransportsForUser_forwarded()849     public void listAllTransportsForUser_forwarded() throws Exception {
850         when(mBackupManagerServiceMock.listAllTransports(mUserId)).thenReturn(TRANSPORTS);
851         mTrampoline.initializeService();
852 
853         assertEquals(TRANSPORTS, mTrampoline.listAllTransportsForUser(mUserId));
854         verify(mBackupManagerServiceMock).listAllTransports(mUserId);
855     }
856 
857 
858     @Test
listAllTransports_forwarded()859     public void listAllTransports_forwarded() throws Exception {
860         TrampolineTestable.sCallingUserId = mUserId;
861         when(mBackupManagerServiceMock.listAllTransports(mUserId)).thenReturn(TRANSPORTS);
862         mTrampoline.initializeService();
863 
864         assertEquals(TRANSPORTS, mTrampoline.listAllTransports());
865         verify(mBackupManagerServiceMock).listAllTransports(mUserId);
866     }
867 
868     @Test
listAllTransportComponentsForUser_calledBeforeInitialize_ignored()869     public void listAllTransportComponentsForUser_calledBeforeInitialize_ignored()
870             throws Exception {
871         assertNull(mTrampoline.listAllTransportComponentsForUser(mUserId));
872         verifyNoMoreInteractions(mBackupManagerServiceMock);
873     }
874 
875     @Test
listAllTransportComponentsForUser_forwarded()876     public void listAllTransportComponentsForUser_forwarded() throws Exception {
877         when(mBackupManagerServiceMock.listAllTransportComponents(mUserId)).thenReturn(
878                 TRANSPORT_COMPONENTS);
879         mTrampoline.initializeService();
880 
881         assertEquals(TRANSPORT_COMPONENTS, mTrampoline.listAllTransportComponentsForUser(mUserId));
882         verify(mBackupManagerServiceMock).listAllTransportComponents(mUserId);
883     }
884 
885     @Test
getTransportWhitelist_calledBeforeInitialize_ignored()886     public void getTransportWhitelist_calledBeforeInitialize_ignored() throws Exception {
887         assertNull(mTrampoline.getTransportWhitelist());
888         verifyNoMoreInteractions(mBackupManagerServiceMock);
889     }
890 
891     @Test
getTransportWhitelist_forwarded()892     public void getTransportWhitelist_forwarded() {
893         when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS);
894         mTrampoline.initializeService();
895 
896         assertEquals(TRANSPORTS, mTrampoline.getTransportWhitelist());
897         verify(mBackupManagerServiceMock).getTransportWhitelist();
898     }
899 
900     @Test
updateTransportAttributesForUser_calledBeforeInitialize_ignored()901     public void updateTransportAttributesForUser_calledBeforeInitialize_ignored() {
902         mTrampoline.updateTransportAttributesForUser(
903                 mUserId,
904                 TRANSPORT_COMPONENT_NAME,
905                 TRANSPORT_NAME,
906                 null,
907                 "Transport Destination",
908                 null,
909                 "Data Management");
910 
911         verifyNoMoreInteractions(mBackupManagerServiceMock);
912     }
913 
914     @Test
updateTransportAttributesForUser_forwarded()915     public void updateTransportAttributesForUser_forwarded() {
916         when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS);
917         mTrampoline.initializeService();
918 
919         mTrampoline.updateTransportAttributesForUser(
920                 mUserId,
921                 TRANSPORT_COMPONENT_NAME,
922                 TRANSPORT_NAME,
923                 null,
924                 "Transport Destination",
925                 null,
926                 "Data Management");
927 
928         verify(mBackupManagerServiceMock)
929                 .updateTransportAttributes(
930                         mUserId,
931                         TRANSPORT_COMPONENT_NAME,
932                         TRANSPORT_NAME,
933                         null,
934                         "Transport Destination",
935                         null,
936                         "Data Management");
937     }
938 
939     @Test
selectBackupTransport_calledBeforeInitialize_ignored()940     public void selectBackupTransport_calledBeforeInitialize_ignored() throws RemoteException {
941         mTrampoline.selectBackupTransport(TRANSPORT_NAME);
942         verifyNoMoreInteractions(mBackupManagerServiceMock);
943     }
944 
945     @Test
selectBackupTransportForUser_forwarded()946     public void selectBackupTransportForUser_forwarded() throws Exception {
947         mTrampoline.initializeService();
948 
949         mTrampoline.selectBackupTransportForUser(mUserId, TRANSPORT_NAME);
950 
951         verify(mBackupManagerServiceMock).selectBackupTransport(mUserId, TRANSPORT_NAME);
952     }
953 
954     @Test
selectBackupTransport_forwarded()955     public void selectBackupTransport_forwarded() throws Exception {
956         TrampolineTestable.sCallingUserId = mUserId;
957         mTrampoline.initializeService();
958 
959         mTrampoline.selectBackupTransport(TRANSPORT_NAME);
960 
961         verify(mBackupManagerServiceMock).selectBackupTransport(mUserId, TRANSPORT_NAME);
962     }
963 
964     @Test
selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored()965     public void selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored()
966             throws Exception {
967         LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue();
968 
969         mTrampoline.selectBackupTransportAsyncForUser(
970                 mUserId,
971                 TRANSPORT_COMPONENT_NAME,
972                 new ISelectBackupTransportCallback() {
973                     @Override
974                     public void onSuccess(String transportName) throws RemoteException {
975 
976                     }
977 
978                     @Override
979                     public void onFailure(int reason) throws RemoteException {
980                         q.offer(reason);
981                     }
982 
983                     @Override
984                     public IBinder asBinder() {
985                         return null;
986                     }
987                 });
988 
989         verifyNoMoreInteractions(mBackupManagerServiceMock);
990         Integer errorCode = q.poll(5, TimeUnit.SECONDS);
991         assertNotNull(errorCode);
992         assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, (int) errorCode);
993     }
994 
995     @Test
selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored_nullListener()996     public void selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored_nullListener()
997             throws Exception {
998         mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, null);
999 
1000         verifyNoMoreInteractions(mBackupManagerServiceMock);
1001         // No crash.
1002     }
1003 
1004     @Test
selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored_listenerThrows()1005     public void selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored_listenerThrows()
1006             throws Exception {
1007         mTrampoline.selectBackupTransportAsyncForUser(
1008                 mUserId,
1009                 TRANSPORT_COMPONENT_NAME,
1010                 new ISelectBackupTransportCallback() {
1011                     @Override
1012                     public void onSuccess(String transportName) throws RemoteException {
1013 
1014                     }
1015 
1016                     @Override
1017                     public void onFailure(int reason) throws RemoteException {
1018                         throw new RemoteException("Crash");
1019                     }
1020 
1021                     @Override
1022                     public IBinder asBinder() {
1023                         return null;
1024                     }
1025                 });
1026 
1027         verifyNoMoreInteractions(mBackupManagerServiceMock);
1028         // No crash.
1029     }
1030 
1031     @Test
selectBackupTransportAsyncForUser_forwarded()1032     public void selectBackupTransportAsyncForUser_forwarded() throws Exception {
1033         mTrampoline.initializeService();
1034 
1035         mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, null);
1036 
1037         verify(mBackupManagerServiceMock)
1038                 .selectBackupTransportAsync(mUserId, TRANSPORT_COMPONENT_NAME, null);
1039     }
1040 
1041     @Test
getConfigurationIntent_calledBeforeInitialize_ignored()1042     public void getConfigurationIntent_calledBeforeInitialize_ignored() throws Exception {
1043         mTrampoline.getConfigurationIntent(TRANSPORT_NAME);
1044         verifyNoMoreInteractions(mBackupManagerServiceMock);
1045     }
1046 
1047     @Test
getConfigurationIntentForUser_forwarded()1048     public void getConfigurationIntentForUser_forwarded() throws Exception {
1049         Intent configurationIntentStub = new Intent();
1050         when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn(
1051                 configurationIntentStub);
1052         mTrampoline.initializeService();
1053 
1054         assertEquals(
1055                 configurationIntentStub,
1056                 mTrampoline.getConfigurationIntentForUser(mUserId, TRANSPORT_NAME));
1057         verify(mBackupManagerServiceMock).getConfigurationIntent(mUserId, TRANSPORT_NAME);
1058     }
1059 
1060     @Test
getConfigurationIntent_forwarded()1061     public void getConfigurationIntent_forwarded() throws Exception {
1062         TrampolineTestable.sCallingUserId = mUserId;
1063         Intent configurationIntentStub = new Intent();
1064         when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn(
1065                 configurationIntentStub);
1066         mTrampoline.initializeService();
1067 
1068         assertEquals(configurationIntentStub, mTrampoline.getConfigurationIntent(TRANSPORT_NAME));
1069         verify(mBackupManagerServiceMock).getConfigurationIntent(mUserId, TRANSPORT_NAME);
1070     }
1071 
1072     @Test
getDestinationString_calledBeforeInitialize_ignored()1073     public void getDestinationString_calledBeforeInitialize_ignored() throws Exception {
1074         assertNull(mTrampoline.getDestinationString(TRANSPORT_NAME));
1075         verifyNoMoreInteractions(mBackupManagerServiceMock);
1076     }
1077 
1078     @Test
getDestinationStringForUser_forwarded()1079     public void getDestinationStringForUser_forwarded() throws Exception {
1080         when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn(
1081                 DESTINATION_STRING);
1082         mTrampoline.initializeService();
1083 
1084         assertEquals(
1085                 DESTINATION_STRING,
1086                 mTrampoline.getDestinationStringForUser(mUserId, TRANSPORT_NAME));
1087         verify(mBackupManagerServiceMock).getDestinationString(mUserId, TRANSPORT_NAME);
1088     }
1089 
1090     @Test
getDestinationString_forwarded()1091     public void getDestinationString_forwarded() throws Exception {
1092         TrampolineTestable.sCallingUserId = mUserId;
1093         when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn(
1094                 DESTINATION_STRING);
1095 
1096         mTrampoline.initializeService();
1097         assertEquals(DESTINATION_STRING, mTrampoline.getDestinationString(TRANSPORT_NAME));
1098         verify(mBackupManagerServiceMock).getDestinationString(mUserId, TRANSPORT_NAME);
1099     }
1100 
1101     @Test
getDataManagementIntent_calledBeforeInitialize_ignored()1102     public void getDataManagementIntent_calledBeforeInitialize_ignored() throws Exception {
1103         assertNull(mTrampoline.getDataManagementIntent(TRANSPORT_NAME));
1104         verifyNoMoreInteractions(mBackupManagerServiceMock);
1105     }
1106 
1107     @Test
getDataManagementIntentForUser_forwarded()1108     public void getDataManagementIntentForUser_forwarded() throws Exception {
1109         Intent dataManagementIntent = new Intent();
1110         when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn(
1111                 dataManagementIntent);
1112         mTrampoline.initializeService();
1113 
1114         assertEquals(
1115                 dataManagementIntent,
1116                 mTrampoline.getDataManagementIntentForUser(mUserId, TRANSPORT_NAME));
1117         verify(mBackupManagerServiceMock).getDataManagementIntent(mUserId, TRANSPORT_NAME);
1118     }
1119 
1120     @Test
getDataManagementIntent_forwarded()1121     public void getDataManagementIntent_forwarded() throws Exception {
1122         TrampolineTestable.sCallingUserId = mUserId;
1123         Intent dataManagementIntent = new Intent();
1124         when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn(
1125                 dataManagementIntent);
1126         mTrampoline.initializeService();
1127 
1128         assertEquals(dataManagementIntent, mTrampoline.getDataManagementIntent(TRANSPORT_NAME));
1129         verify(mBackupManagerServiceMock).getDataManagementIntent(mUserId, TRANSPORT_NAME);
1130     }
1131 
1132     @Test
getDataManagementLabelForUser_calledBeforeInitialize_ignored()1133     public void getDataManagementLabelForUser_calledBeforeInitialize_ignored() throws Exception {
1134         assertNull(mTrampoline.getDataManagementLabelForUser(mUserId, TRANSPORT_NAME));
1135         verifyNoMoreInteractions(mBackupManagerServiceMock);
1136     }
1137 
1138     @Test
getDataManagementLabelForUser_forwarded()1139     public void getDataManagementLabelForUser_forwarded() throws Exception {
1140         when(mBackupManagerServiceMock.getDataManagementLabel(mUserId, TRANSPORT_NAME)).thenReturn(
1141                 DATA_MANAGEMENT_LABEL);
1142         mTrampoline.initializeService();
1143 
1144         assertEquals(
1145                 DATA_MANAGEMENT_LABEL,
1146                 mTrampoline.getDataManagementLabelForUser(mUserId, TRANSPORT_NAME));
1147         verify(mBackupManagerServiceMock).getDataManagementLabel(mUserId, TRANSPORT_NAME);
1148     }
1149 
1150     @Test
beginRestoreSession_calledBeforeInitialize_ignored()1151     public void beginRestoreSession_calledBeforeInitialize_ignored() throws Exception {
1152         mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME);
1153         verifyNoMoreInteractions(mBackupManagerServiceMock);
1154     }
1155 
1156     @Test
beginRestoreSessionForUser_forwarded()1157     public void beginRestoreSessionForUser_forwarded() throws Exception {
1158         mTrampoline.initializeService();
1159 
1160         mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME);
1161 
1162         verify(mBackupManagerServiceMock)
1163                 .beginRestoreSession(mUserId, PACKAGE_NAME, TRANSPORT_NAME);
1164     }
1165 
1166     @Test
opComplete_calledBeforeInitialize_ignored()1167     public void opComplete_calledBeforeInitialize_ignored() throws Exception {
1168         mTrampoline.opComplete(1, 2);
1169         verifyNoMoreInteractions(mBackupManagerServiceMock);
1170     }
1171 
1172     @Test
opComplete_forwarded()1173     public void opComplete_forwarded() throws Exception {
1174         TrampolineTestable.sCallingUserId = mUserId;
1175         mTrampoline.initializeService();
1176 
1177         mTrampoline.opComplete(1, 2);
1178 
1179         verify(mBackupManagerServiceMock).opComplete(mUserId, 1, 2);
1180     }
1181 
1182     @Test
getAvailableRestoreTokenForUser_calledBeforeInitialize_ignored()1183     public void getAvailableRestoreTokenForUser_calledBeforeInitialize_ignored() {
1184         assertEquals(0, mTrampoline.getAvailableRestoreTokenForUser(mUserId, PACKAGE_NAME));
1185         verifyNoMoreInteractions(mBackupManagerServiceMock);
1186     }
1187 
1188     @Test
getAvailableRestoreTokenForUser_forwarded()1189     public void getAvailableRestoreTokenForUser_forwarded() {
1190         when(mBackupManagerServiceMock.getAvailableRestoreToken(mUserId, PACKAGE_NAME))
1191                 .thenReturn(123L);
1192         mTrampoline.initializeService();
1193 
1194         assertEquals(123, mTrampoline.getAvailableRestoreTokenForUser(mUserId, PACKAGE_NAME));
1195         verify(mBackupManagerServiceMock).getAvailableRestoreToken(mUserId, PACKAGE_NAME);
1196     }
1197 
1198     @Test
isAppEligibleForBackupForUser_calledBeforeInitialize_ignored()1199     public void isAppEligibleForBackupForUser_calledBeforeInitialize_ignored() {
1200         assertFalse(mTrampoline.isAppEligibleForBackupForUser(mUserId, PACKAGE_NAME));
1201         verifyNoMoreInteractions(mBackupManagerServiceMock);
1202     }
1203 
1204     @Test
isAppEligibleForBackupForUser_forwarded()1205     public void isAppEligibleForBackupForUser_forwarded() {
1206         when(mBackupManagerServiceMock.isAppEligibleForBackup(mUserId, PACKAGE_NAME))
1207                 .thenReturn(true);
1208         mTrampoline.initializeService();
1209 
1210         assertTrue(mTrampoline.isAppEligibleForBackupForUser(mUserId, PACKAGE_NAME));
1211         verify(mBackupManagerServiceMock).isAppEligibleForBackup(mUserId, PACKAGE_NAME);
1212     }
1213 
1214     @Test
requestBackup_calledBeforeInitialize_ignored()1215     public void requestBackup_calledBeforeInitialize_ignored() throws RemoteException {
1216         assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, mTrampoline.requestBackup(
1217                 PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123));
1218         verifyNoMoreInteractions(mBackupManagerServiceMock);
1219     }
1220 
1221     @Test
requestBackupForUser_forwarded()1222     public void requestBackupForUser_forwarded() throws Exception {
1223         when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES,
1224                 mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456);
1225         mTrampoline.initializeService();
1226 
1227         assertEquals(456, mTrampoline.requestBackupForUser(mUserId, PACKAGE_NAMES,
1228                 mBackupObserverMock, mBackupManagerMonitorMock, 123));
1229         verify(mBackupManagerServiceMock).requestBackup(mUserId, PACKAGE_NAMES,
1230                 mBackupObserverMock, mBackupManagerMonitorMock, 123);
1231     }
1232 
1233     @Test
requestBackup_forwardedToCallingUserId()1234     public void requestBackup_forwardedToCallingUserId() throws Exception {
1235         TrampolineTestable.sCallingUserId = mUserId;
1236         when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES,
1237                 mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456);
1238         mTrampoline.initializeService();
1239 
1240         assertEquals(456, mTrampoline.requestBackup(PACKAGE_NAMES,
1241                 mBackupObserverMock, mBackupManagerMonitorMock, 123));
1242         verify(mBackupManagerServiceMock).requestBackup(mUserId, PACKAGE_NAMES,
1243                 mBackupObserverMock, mBackupManagerMonitorMock, 123);
1244     }
1245 
1246     @Test
cancelBackups_calledBeforeInitialize_ignored()1247     public void cancelBackups_calledBeforeInitialize_ignored() throws Exception {
1248         mTrampoline.cancelBackups();
1249         verifyNoMoreInteractions(mBackupManagerServiceMock);
1250     }
1251 
1252     @Test
cancelBackupsForUser_forwarded()1253     public void cancelBackupsForUser_forwarded() throws Exception {
1254         mTrampoline.initializeService();
1255 
1256         mTrampoline.cancelBackupsForUser(mUserId);
1257 
1258         verify(mBackupManagerServiceMock).cancelBackups(mUserId);
1259     }
1260 
1261     @Test
cancelBackups_forwardedToCallingUserId()1262     public void cancelBackups_forwardedToCallingUserId() throws Exception {
1263         TrampolineTestable.sCallingUserId = mUserId;
1264         mTrampoline.initializeService();
1265 
1266         mTrampoline.cancelBackups();
1267 
1268         verify(mBackupManagerServiceMock).cancelBackups(mUserId);
1269     }
1270 
1271     @Test
beginFullBackup_calledBeforeInitialize_ignored()1272     public void beginFullBackup_calledBeforeInitialize_ignored() throws Exception {
1273         mTrampoline.beginFullBackup(mUserId, new FullBackupJob());
1274         verifyNoMoreInteractions(mBackupManagerServiceMock);
1275     }
1276 
1277     @Test
beginFullBackup_forwarded()1278     public void beginFullBackup_forwarded() throws Exception {
1279         FullBackupJob fullBackupJob = new FullBackupJob();
1280         when(mBackupManagerServiceMock.beginFullBackup(mUserId, fullBackupJob)).thenReturn(true);
1281 
1282         mTrampoline.initializeService();
1283         assertTrue(mTrampoline.beginFullBackup(mUserId, fullBackupJob));
1284         verify(mBackupManagerServiceMock).beginFullBackup(mUserId, fullBackupJob);
1285     }
1286 
1287     @Test
endFullBackup_calledBeforeInitialize_ignored()1288     public void endFullBackup_calledBeforeInitialize_ignored() {
1289         mTrampoline.endFullBackup(mUserId);
1290         verifyNoMoreInteractions(mBackupManagerServiceMock);
1291     }
1292 
1293     @Test
endFullBackup_forwarded()1294     public void endFullBackup_forwarded() {
1295         mTrampoline.initializeService();
1296         mTrampoline.endFullBackup(mUserId);
1297         verify(mBackupManagerServiceMock).endFullBackup(mUserId);
1298     }
1299 
1300     @Test
dump_callerDoesNotHavePermission_ignored()1301     public void dump_callerDoesNotHavePermission_ignored() {
1302         when(mContextMock.checkCallingOrSelfPermission(
1303                 android.Manifest.permission.DUMP)).thenReturn(
1304                 PackageManager.PERMISSION_DENIED);
1305         mTrampoline.initializeService();
1306 
1307         mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, new String[0]);
1308 
1309         verifyNoMoreInteractions(mBackupManagerServiceMock);
1310     }
1311 
1312     @Test
dump_calledBeforeInitialize_ignored()1313     public void dump_calledBeforeInitialize_ignored() {
1314         when(mContextMock.checkCallingOrSelfPermission(
1315                 android.Manifest.permission.DUMP)).thenReturn(
1316                 PackageManager.PERMISSION_GRANTED);
1317 
1318         mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, new String[0]);
1319 
1320         verifyNoMoreInteractions(mBackupManagerServiceMock);
1321     }
1322 
1323     @Test
dump_callerHasPermission_forwarded()1324     public void dump_callerHasPermission_forwarded() {
1325         when(mContextMock.checkCallingOrSelfPermission(
1326                 android.Manifest.permission.DUMP)).thenReturn(
1327                 PackageManager.PERMISSION_GRANTED);
1328         mTrampoline.initializeService();
1329 
1330         mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, null);
1331 
1332         verify(mBackupManagerServiceMock).dump(mFileDescriptorStub, mPrintWriterMock, null);
1333     }
1334 
1335     private static class TrampolineTestable extends Trampoline {
1336         static boolean sBackupDisabled = false;
1337         static int sCallingUserId = -1;
1338         static int sCallingUid = -1;
1339         static BackupManagerService sBackupManagerServiceMock = null;
1340         static File sSuppressFile = null;
1341         static SparseArray<File> sActivatedFiles = new SparseArray<>();
1342         static SparseArray<File> sRememberActivatedFiles = new SparseArray<>();
1343         static UserManager sUserManagerMock = null;
1344         private int mCreateServiceCallsCount = 0;
1345 
TrampolineTestable(Context context)1346         TrampolineTestable(Context context) {
1347             super(context);
1348         }
1349 
1350         @Override
getUserManager()1351         protected UserManager getUserManager() {
1352             return sUserManagerMock;
1353         }
1354 
1355         @Override
isBackupDisabled()1356         public boolean isBackupDisabled() {
1357             return sBackupDisabled;
1358         }
1359 
1360         @Override
getSuppressFileForSystemUser()1361         protected File getSuppressFileForSystemUser() {
1362             return sSuppressFile;
1363         }
1364 
1365         @Override
getRememberActivatedFileForNonSystemUser(int userId)1366         protected File getRememberActivatedFileForNonSystemUser(int userId) {
1367             return sRememberActivatedFiles.get(userId);
1368         }
1369 
1370         @Override
getActivatedFileForNonSystemUser(int userId)1371         protected File getActivatedFileForNonSystemUser(int userId) {
1372             return sActivatedFiles.get(userId);
1373         }
1374 
binderGetCallingUserId()1375         protected int binderGetCallingUserId() {
1376             return sCallingUserId;
1377         }
1378 
1379         @Override
binderGetCallingUid()1380         protected int binderGetCallingUid() {
1381             return sCallingUid;
1382         }
1383 
1384         @Override
createBackupManagerService()1385         protected BackupManagerService createBackupManagerService() {
1386             mCreateServiceCallsCount++;
1387             return sBackupManagerServiceMock;
1388         }
1389 
1390         @Override
postToHandler(Runnable runnable)1391         protected void postToHandler(Runnable runnable) {
1392             runnable.run();
1393         }
1394 
getCreateServiceCallsCount()1395         int getCreateServiceCallsCount() {
1396             return mCreateServiceCallsCount;
1397         }
1398     }
1399 }
1400