diff options
author | Ruslan Tkhakokhov <rthakohov@google.com> | 2018-12-03 17:28:39 +0000 |
---|---|---|
committer | Ruslan Tkhakokhov <rthakohov@google.com> | 2018-12-17 16:48:13 +0000 |
commit | a2fe6c5bb914bb3374a13d7c718614dd98698b71 (patch) | |
tree | b9151fb44e4b8d3df789158e9c590bdcadded722 /services/robotests/src | |
parent | 1982ca78e30ae156d77d8a3216593f6272fc0ec9 (diff) |
[Multi-user] Add -user param to adb backup/restore
Add an optional parameter -user to provide ID of the user for which to
run backup/restore operation. Add robolectric test to verify the
new parameter is proccessed correctly.
Bug: 119908153
Test: 1) atest BackupTest
2) atest BackupManagerServiceTest
3) atest TrampolineTest
4) atest GtsBackupTestCases
5) atest GtsBackupHostTestCases
6) Manual:
- Run "adb backup -all" and verify that backup is successfull
- Run "adb restore" and verify that restore is successfull
- Run "adb backup -all -user 10" and verify that backup faield as
it's only currently supported for system user
- Run "adb restore -user 10" and verify that restore failed as it's
only currently supported for system user
Change-Id: I6dbf9c87eedd5a72da0446beff7d2551f98f2654
Diffstat (limited to 'services/robotests/src')
-rw-r--r-- | services/robotests/src/com/android/commands/bu/AdbBackupTest.java | 61 | ||||
-rw-r--r-- | services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java | 127 |
2 files changed, 176 insertions, 12 deletions
diff --git a/services/robotests/src/com/android/commands/bu/AdbBackupTest.java b/services/robotests/src/com/android/commands/bu/AdbBackupTest.java new file mode 100644 index 000000000000..6869f5612c1d --- /dev/null +++ b/services/robotests/src/com/android/commands/bu/AdbBackupTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.commands.bu; + +import static org.mockito.Mockito.verify; + +import android.app.backup.IBackupManager; +import android.os.UserHandle; +import android.platform.test.annotations.Presubmit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowParcelFileDescriptor; + +/** Unit tests for {@link com.android.commands.bu.Backup}. */ +@RunWith(RobolectricTestRunner.class) +@Config(shadows = {ShadowParcelFileDescriptor.class}) +@Presubmit +public class AdbBackupTest { + @Mock private IBackupManager mBackupManager; + private Backup mBackup; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mBackup = new Backup(mBackupManager); + } + + @Test + public void testRun_whenUserNotSpecified_callsAdbBackupAsSystemUser() throws Exception { + mBackup.run(new String[] {"backup", "-all"}); + + verify(mBackupManager).isBackupServiceActive(UserHandle.USER_SYSTEM); + } + + @Test + public void testRun_whenUserSpecified_callsBackupManagerAsSpecifiedUser() throws Exception { + mBackup.run(new String[] {"backup", "-user", "10", "-all"}); + + verify(mBackupManager).isBackupServiceActive(10); + } +} diff --git a/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java b/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java index 96ef0ce45001..58bce1cdfbf1 100644 --- a/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java +++ b/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java @@ -65,6 +65,8 @@ public class BackupManagerServiceTest { private static final String TEST_PACKAGE = "package"; private static final String TEST_TRANSPORT = "transport"; + private static final String[] ADB_TEST_PACKAGES = {TEST_PACKAGE}; + private static final int NON_USER_SYSTEM = UserHandle.USER_SYSTEM + 1; private ShadowContextWrapper mShadowContext; @@ -555,16 +557,81 @@ public class BackupManagerServiceTest { verify(mUserBackupManagerService).hasBackupPassword(); } + /** + * Test verifying that {@link BackupManagerService#adbBackup(ParcelFileDescriptor, int, boolean, + * boolean, boolean, boolean, boolean, boolean, boolean, boolean, String[])} throws a + * {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. + */ + @Test + public void testAdbBackup_withoutPermission_throwsSecurityException() { + mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + + expectThrows(SecurityException.class, + () -> + mBackupManagerService.adbBackup( + /* userId */ mUserId, + /* parcelFileDescriptor*/ null, + /* includeApks */ true, + /* includeObbs */ true, + /* includeShared */ true, + /* doWidgets */ true, + /* doAllApps */ true, + /* includeSystem */ true, + /* doCompress */ true, + /* doKeyValue */ true, + null)); + + } + + /** + * Test verifying that {@link BackupManagerService#adbBackup(ParcelFileDescriptor, int, boolean, + * boolean, boolean, boolean, boolean, boolean, boolean, boolean, String[])} does not require + * the caller to have INTERACT_ACROSS_USERS_FULL permission when the calling user id is the + * same as the target user id. + */ + @Test + public void testAdbBackup_whenCallingUserIsTargetUser_doesntNeedPermission() throws Exception { + ShadowBinder.setCallingUserHandle(UserHandle.of(mUserId)); + mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + + mBackupManagerService.adbBackup( + /* userId */ mUserId, + parcelFileDescriptor, + /* includeApks */ true, + /* includeObbs */ true, + /* includeShared */ true, + /* doWidgets */ true, + /* doAllApps */ true, + /* includeSystem */ true, + /* doCompress */ true, + /* doKeyValue */ true, + ADB_TEST_PACKAGES); + + verify(mUserBackupManagerService) + .adbBackup( + parcelFileDescriptor, + /* includeApks */ true, + /* includeObbs */ true, + /* includeShared */ true, + /* doWidgets */ true, + /* doAllApps */ true, + /* includeSystem */ true, + /* doCompress */ true, + /* doKeyValue */ true, + ADB_TEST_PACKAGES); + } + /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testAdbBackup_callsAdbBackupForUser() throws Exception { - File testFile = new File(mContext.getFilesDir(), "test"); - testFile.createNewFile(); - ParcelFileDescriptor parcelFileDescriptor = - ParcelFileDescriptor.open(testFile, ParcelFileDescriptor.MODE_READ_WRITE); - String[] packages = {TEST_PACKAGE}; + mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); mBackupManagerService.adbBackup( + /* userId */ mUserId, parcelFileDescriptor, /* includeApks */ true, /* includeObbs */ true, @@ -574,7 +641,7 @@ public class BackupManagerServiceTest { /* includeSystem */ true, /* doCompress */ true, /* doKeyValue */ true, - packages); + ADB_TEST_PACKAGES); verify(mUserBackupManagerService) .adbBackup( @@ -587,18 +654,48 @@ public class BackupManagerServiceTest { /* includeSystem */ true, /* doCompress */ true, /* doKeyValue */ true, - packages); + ADB_TEST_PACKAGES); + } + + /** + * Test verifying that {@link BackupManagerService#adbRestore(ParcelFileDescriptor, int)} throws + * a {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL + * permission. + */ + @Test + public void testAdbRestore_withoutPermission_throwsSecurityException() { + mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + + expectThrows(SecurityException.class, + () -> mBackupManagerService.adbRestore(mUserId, null)); + + } + + /** + * Test verifying that {@link BackupManagerService#adbRestore(ParcelFileDescriptor, int)} does + * not require the caller to have INTERACT_ACROSS_USERS_FULL permission when the calling user id + * is the same as the target user id. + */ + @Test + public void testAdbRestore_whenCallingUserIsTargetUser_doesntNeedPermission() throws Exception { + ShadowBinder.setCallingUserHandle(UserHandle.of(mUserId)); + mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + + mBackupManagerService.adbRestore(mUserId, parcelFileDescriptor); + + verify(mUserBackupManagerService).adbRestore(parcelFileDescriptor); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testAdbRestore_callsAdbRestoreForUser() throws Exception { - File testFile = new File(mContext.getFilesDir(), "test"); - testFile.createNewFile(); - ParcelFileDescriptor parcelFileDescriptor = - ParcelFileDescriptor.open(testFile, ParcelFileDescriptor.MODE_READ_WRITE); + mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); - mBackupManagerService.adbRestore(parcelFileDescriptor); + mBackupManagerService.adbRestore(mUserId, parcelFileDescriptor); verify(mUserBackupManagerService).adbRestore(parcelFileDescriptor); } @@ -638,4 +735,10 @@ public class BackupManagerServiceTest { verify(mUserBackupManagerService).dump(fileDescriptor, printWriter, args); } + + private ParcelFileDescriptor getFileDescriptorForAdbTest() throws Exception { + File testFile = new File(mContext.getFilesDir(), "test"); + testFile.createNewFile(); + return ParcelFileDescriptor.open(testFile, ParcelFileDescriptor.MODE_READ_WRITE); + } } |