diff options
author | Annie Meng <anniemeng@google.com> | 2018-12-11 18:24:03 +0000 |
---|---|---|
committer | Annie Meng <anniemeng@google.com> | 2018-12-17 22:00:51 +0000 |
commit | 6a93c233fe851c5629aedf388d485c752d5a6ffe (patch) | |
tree | 87795b9bad8da680e7497b8ee6e502afe0dad4cc /services/robotests/src | |
parent | feb504cb6626aab7b957ddee1c13f9cbadd7314c (diff) |
[Multi-user] Decouple UserBMS and BMS creation
The BMS constructor is now only responsible for setting up the system
service. User registration logic is moved to its own helper that
Trampoline calls during a user unlock callback.
** Logic **
All IBackupManager methods that BMS implements now use bookkeeping to
get the UserBMS instance to act on. Currently the system user instance
is hardcoded in most methods as we don't pass in the user id parameter
(except for a few added in ag/5667585).
If these methods are called on a non-registered user, we log and return
a default value.
** Tests **
Each IBackupManager method now has four corresponding tests:
1) Permission denial test: No INTERACT_ACROSS_USERS_FULL permission +
non-calling user id = security exception (added in ag/5667585, only for
methods that take in an user id param currently).
2) Permission grant test: INTERACT_ACROSS_USERS_FULL permission +
non-calling user id = call forwarded.
3) Registered user test: Passing an user id that has a corresponding
UserBMS instance -> delegate call to that UserBMS instance.
4) Unknown user test: Passing an user id that has no corresponding
UserBMS instance -> no call forwarded.
These tests will be updated when more methods take in an user id param.
Bug: 120212806
Test: 1) atest RunFrameworksServicesRoboTests
2) Unlock system user -> starts service, registers transports
3) adb shell bmgr flows
4) atest TrampolineTest
5) CTS + GTS Backup test cases
6) SUW + Cloud restore; D2D
Change-Id: Ic04f754c75af905ee34c609063c08406e23671d5
Diffstat (limited to 'services/robotests/src')
-rw-r--r-- | services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java | 1180 |
1 files changed, 958 insertions, 222 deletions
diff --git a/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java b/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java index 58bce1cdfbf1..83f66c5258b2 100644 --- a/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java +++ b/services/robotests/src/com/android/server/backup/BackupManagerServiceTest.java @@ -16,11 +16,15 @@ package com.android.server.backup; +import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; + +import static com.android.server.backup.testing.BackupManagerServiceTestUtils.startBackupThread; import static com.android.server.backup.testing.TransportData.backupTransport; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.robolectric.Shadows.shadowOf; import static org.testng.Assert.expectThrows; @@ -37,8 +41,8 @@ import android.os.IBinder; import android.os.ParcelFileDescriptor; import android.os.UserHandle; import android.platform.test.annotations.Presubmit; +import android.util.SparseArray; -import com.android.server.backup.testing.BackupManagerServiceTestUtils; import com.android.server.backup.testing.TransportData; import com.android.server.testing.shadows.ShadowBinder; @@ -64,16 +68,14 @@ import java.io.PrintWriter; 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; - @Mock private UserBackupManagerService mUserBackupManagerService; - private BackupManagerService mBackupManagerService; private Context mContext; - @UserIdInt private int mUserId; + @UserIdInt private int mUserOneId; + @UserIdInt private int mUserTwoId; + @Mock private UserBackupManagerService mUserOneService; + @Mock private UserBackupManagerService mUserTwoService; /** Initialize {@link BackupManagerService}. */ @Before @@ -83,13 +85,11 @@ public class BackupManagerServiceTest { Application application = RuntimeEnvironment.application; mContext = application; mShadowContext = shadowOf(application); - mUserId = NON_USER_SYSTEM; - mBackupManagerService = - new BackupManagerService( - application, - new Trampoline(application), - BackupManagerServiceTestUtils.startBackupThread(null)); - mBackupManagerService.setUserBackupManagerService(mUserBackupManagerService); + + // TODO(b/120212806): Hardcoding system user for now since most methods in BMS don't yet + // take an user parameter (and instead hardcode the system user). + mUserOneId = UserHandle.USER_SYSTEM; + mUserTwoId = mUserOneId + 1; } /** @@ -102,8 +102,8 @@ public class BackupManagerServiceTest { } /** - * Test verifying that {@link BackupManagerService#MORE_DEBUG} is set to {@code false}. - * This is specifically to prevent overloading the logs in production. + * Test verifying that {@link BackupManagerService#MORE_DEBUG} is set to {@code false}. This is + * specifically to prevent overloading the logs in production. */ @Test public void testMoreDebug_isFalse() throws Exception { @@ -112,9 +112,73 @@ public class BackupManagerServiceTest { assertThat(moreDebug).isFalse(); } - // TODO(b/118520567): Change the following tests to use the per-user instance of - // UserBackupManagerService once it's implemented. Currently these tests only test the straight - // forward redirection. + /** Test that the constructor does not create {@link UserBackupManagerService} instances. */ + @Test + public void testConstructor_doesNotRegisterUsers() throws Exception { + BackupManagerService backupManagerService = createService(); + + assertThat(backupManagerService.getServiceUsers().size()).isEqualTo(0); + } + + /** Test that the constructor handles {@code null} parameters. */ + @Test + public void testConstructor_withNullContext_throws() throws Exception { + expectThrows( + NullPointerException.class, + () -> + new BackupManagerService( + /* context */ null, + new Trampoline(mContext), + startBackupThread(null))); + } + + /** Test that the constructor handles {@code null} parameters. */ + @Test + public void testConstructor_withNullTrampoline_throws() throws Exception { + expectThrows( + NullPointerException.class, + () -> + new BackupManagerService( + mContext, /* trampoline */ null, startBackupThread(null))); + } + + /** Test that the constructor handles {@code null} parameters. */ + @Test + public void testConstructor_withNullBackupThread_throws() throws Exception { + expectThrows( + NullPointerException.class, + () -> + new BackupManagerService( + mContext, new Trampoline(mContext), /* backupThread */ null)); + } + + /** Test that the service registers users. */ + @Test + public void testStartServiceForUser_registersUser() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.startServiceForUser(mUserOneId); + + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getServiceUsers(); + assertThat(serviceUsers.size()).isEqualTo(1); + assertThat(serviceUsers.get(mUserOneId)).isNotNull(); + } + + /** Test that the service registers users. */ + @Test + public void testStartServiceForUser_withServiceInstance_registersUser() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.startServiceForUser(mUserOneId, mUserOneService); + + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getServiceUsers(); + assertThat(serviceUsers.size()).isEqualTo(1); + assertThat(serviceUsers.get(mUserOneId)).isEqualTo(mUserOneService); + } + + // TODO(b/120212806): When BMS methods take in a user parameter, modify unknown user tests to + // check that that we don't call the method on another registered user. Currently these tests + // have no registered users since we hardcode the system user in BMS. // --------------------------------------------- // Backup agent tests @@ -122,36 +186,88 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testDataChanged_callsDataChangedForUser() throws Exception { - mBackupManagerService.dataChanged(TEST_PACKAGE); + public void testDataChanged_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); - verify(mUserBackupManagerService).dataChanged(TEST_PACKAGE); + backupManagerService.dataChanged(TEST_PACKAGE); + + verify(mUserOneService).dataChanged(TEST_PACKAGE); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testDataChanged_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.dataChanged(TEST_PACKAGE); + + verify(mUserOneService, never()).dataChanged(TEST_PACKAGE); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testAgentConnected_callsAgentConnectedForUser() throws Exception { + public void testAgentConnected_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); IBinder agentBinder = mock(IBinder.class); - mBackupManagerService.agentConnected(TEST_PACKAGE, agentBinder); + backupManagerService.agentConnected(TEST_PACKAGE, agentBinder); - verify(mUserBackupManagerService).agentConnected(TEST_PACKAGE, agentBinder); + verify(mUserOneService).agentConnected(TEST_PACKAGE, agentBinder); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testAgentConnected_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + IBinder agentBinder = mock(IBinder.class); + + backupManagerService.agentConnected(TEST_PACKAGE, agentBinder); + + verify(mUserOneService, never()).agentConnected(TEST_PACKAGE, agentBinder); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testAgentDisconnected_callsAgentDisconnectedForUser() throws Exception { - mBackupManagerService.agentDisconnected(TEST_PACKAGE); + public void testAgentDisconnected_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.agentDisconnected(TEST_PACKAGE); + + verify(mUserOneService).agentDisconnected(TEST_PACKAGE); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testAgentDisconnected_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); - verify(mUserBackupManagerService).agentDisconnected(TEST_PACKAGE); + backupManagerService.agentDisconnected(TEST_PACKAGE); + + verify(mUserOneService, never()).agentDisconnected(TEST_PACKAGE); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testOpComplete_callsOpCompleteForUser() throws Exception { - mBackupManagerService.opComplete(/* token */ 0, /* result */ 0L); + public void testOpComplete_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.opComplete(/* token */ 0, /* result */ 0L); + + verify(mUserOneService).opComplete(/* token */ 0, /* result */ 0L); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testOpComplete_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); - verify(mUserBackupManagerService).opComplete(/* token */ 0, /* result */ 0L); + backupManagerService.opComplete(/* token */ 0, /* result */ 0L); + + verify(mUserOneService, never()).opComplete(/* token */ 0, /* result */ 0L); } // --------------------------------------------- @@ -160,73 +276,168 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testInitializeTransports_callsInitializeTransportsForUser() throws Exception { + public void testInitializeTransports_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + String[] transports = {TEST_TRANSPORT}; + + backupManagerService.initializeTransports(transports, /* observer */ null); + + verify(mUserOneService).initializeTransports(transports, /* observer */ null); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testInitializeTransports_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); String[] transports = {TEST_TRANSPORT}; - mBackupManagerService.initializeTransports(transports, /* observer */ null); + backupManagerService.initializeTransports(transports, /* observer */ null); - verify(mUserBackupManagerService).initializeTransports(transports, /* observer */ null); + verify(mUserOneService, never()).initializeTransports(transports, /* observer */ null); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testClearBackupData_callsClearBackupDataForUser() throws Exception { - mBackupManagerService.clearBackupData(TEST_TRANSPORT, TEST_PACKAGE); + public void testClearBackupData_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.clearBackupData(TEST_TRANSPORT, TEST_PACKAGE); + + verify(mUserOneService).clearBackupData(TEST_TRANSPORT, TEST_PACKAGE); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testClearBackupData_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); - verify(mUserBackupManagerService).clearBackupData(TEST_TRANSPORT, TEST_PACKAGE); + backupManagerService.clearBackupData(TEST_TRANSPORT, TEST_PACKAGE); + + verify(mUserOneService, never()).clearBackupData(TEST_TRANSPORT, TEST_PACKAGE); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetCurrentTransport_callsGetCurrentTransportForUser() throws Exception { - mBackupManagerService.getCurrentTransport(); + public void testGetCurrentTransport_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getCurrentTransport(); - verify(mUserBackupManagerService).getCurrentTransport(); + verify(mUserOneService).getCurrentTransport(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetCurrentTransport_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.getCurrentTransport(); + + verify(mUserOneService, never()).getCurrentTransport(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetCurrentTransportComponent_callsGetCurrentTransportComponentForUser() + public void testGetCurrentTransportComponent_onRegisteredUser_callsMethodForUser() + throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getCurrentTransportComponent(); + + verify(mUserOneService).getCurrentTransportComponent(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetCurrentTransportComponent_onUnknownUser_doesNotPropagateCall() throws Exception { - mBackupManagerService.getCurrentTransportComponent(); + BackupManagerService backupManagerService = createService(); + + backupManagerService.getCurrentTransportComponent(); - verify(mUserBackupManagerService).getCurrentTransportComponent(); + verify(mUserOneService, never()).getCurrentTransportComponent(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testListAllTransports_callsListAllTransportsForUser() throws Exception { - mBackupManagerService.listAllTransports(); + public void testListAllTransports_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.listAllTransports(); - verify(mUserBackupManagerService).listAllTransports(); + verify(mUserOneService).listAllTransports(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testListAllTransports_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.listAllTransports(); + + verify(mUserOneService, never()).listAllTransports(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testListAllTransportComponents_callsListAllTransportComponentsForUser() + public void testListAllTransportComponents_onRegisteredUser_callsMethodForUser() throws Exception { - mBackupManagerService.listAllTransportComponents(); + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.listAllTransportComponents(); - verify(mUserBackupManagerService).listAllTransportComponents(); + verify(mUserOneService).listAllTransportComponents(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testListAllTransportComponents_onUnknownUser_doesNotPropagateCall() + throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.listAllTransportComponents(); + + verify(mUserOneService, never()).listAllTransportComponents(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetTransportWhitelist_callsGetTransportWhitelistForUser() throws Exception { - mBackupManagerService.getTransportWhitelist(); + public void testGetTransportWhitelist_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getTransportWhitelist(); + + verify(mUserOneService).getTransportWhitelist(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetTransportWhitelist_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); - verify(mUserBackupManagerService).getTransportWhitelist(); + backupManagerService.getTransportWhitelist(); + + verify(mUserOneService, never()).getTransportWhitelist(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testUpdateTransportAttributes_callsUpdateTransportAttributesForUser() + public void testUpdateTransportAttributes_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); TransportData transport = backupTransport(); Intent configurationIntent = new Intent(); Intent dataManagementIntent = new Intent(); - mBackupManagerService.updateTransportAttributes( + backupManagerService.updateTransportAttributes( transport.getTransportComponent(), transport.transportName, configurationIntent, @@ -234,7 +445,34 @@ public class BackupManagerServiceTest { dataManagementIntent, "dataManagementLabel"); - verify(mUserBackupManagerService) + verify(mUserOneService) + .updateTransportAttributes( + transport.getTransportComponent(), + transport.transportName, + configurationIntent, + "currentDestinationString", + dataManagementIntent, + "dataManagementLabel"); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testUpdateTransportAttributes_onUnknownUser_doesNotPropagateCall() + throws Exception { + BackupManagerService backupManagerService = createService(); + TransportData transport = backupTransport(); + Intent configurationIntent = new Intent(); + Intent dataManagementIntent = new Intent(); + + backupManagerService.updateTransportAttributes( + transport.getTransportComponent(), + transport.transportName, + configurationIntent, + "currentDestinationString", + dataManagementIntent, + "dataManagementLabel"); + + verify(mUserOneService, never()) .updateTransportAttributes( transport.getTransportComponent(), transport.transportName, @@ -246,136 +484,292 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testSelectBackupTransport_callsSelectBackupTransportForUser() throws Exception { - mBackupManagerService.selectBackupTransport(TEST_TRANSPORT); + public void testSelectBackupTransport_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.selectBackupTransport(TEST_TRANSPORT); + + verify(mUserOneService).selectBackupTransport(TEST_TRANSPORT); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testSelectBackupTransport_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.selectBackupTransport(TEST_TRANSPORT); - verify(mUserBackupManagerService).selectBackupTransport(TEST_TRANSPORT); + verify(mUserOneService, never()).selectBackupTransport(TEST_TRANSPORT); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testSelectTransportAsync_callsSelectTransportAsyncForUser() throws Exception { + public void testSelectTransportAsync_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); TransportData transport = backupTransport(); ISelectBackupTransportCallback callback = mock(ISelectBackupTransportCallback.class); - mBackupManagerService.selectBackupTransportAsync( + backupManagerService.selectBackupTransportAsync( transport.getTransportComponent(), callback); - verify(mUserBackupManagerService) + verify(mUserOneService) + .selectBackupTransportAsync(transport.getTransportComponent(), callback); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testSelectBackupTransportAsync_onUnknownUser_doesNotPropagateCall() + throws Exception { + BackupManagerService backupManagerService = createService(); + TransportData transport = backupTransport(); + ISelectBackupTransportCallback callback = mock(ISelectBackupTransportCallback.class); + + backupManagerService.selectBackupTransportAsync( + transport.getTransportComponent(), callback); + + verify(mUserOneService, never()) .selectBackupTransportAsync(transport.getTransportComponent(), callback); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetConfigurationIntent_callsGetConfigurationIntentForUser() throws Exception { - mBackupManagerService.getConfigurationIntent(TEST_TRANSPORT); + public void testGetConfigurationIntent_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); - verify(mUserBackupManagerService).getConfigurationIntent(TEST_TRANSPORT); + backupManagerService.getConfigurationIntent(TEST_TRANSPORT); + + verify(mUserOneService).getConfigurationIntent(TEST_TRANSPORT); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetConfigurationIntent_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.getConfigurationIntent(TEST_TRANSPORT); + + verify(mUserOneService, never()).getConfigurationIntent(TEST_TRANSPORT); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetDestinationString_callsGetDestinationStringForUser() throws Exception { - mBackupManagerService.getDestinationString(TEST_TRANSPORT); + public void testGetDestinationString_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getDestinationString(TEST_TRANSPORT); + + verify(mUserOneService).getDestinationString(TEST_TRANSPORT); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetDestinationString_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); - verify(mUserBackupManagerService).getDestinationString(TEST_TRANSPORT); + backupManagerService.getDestinationString(TEST_TRANSPORT); + + verify(mUserOneService, never()).getDestinationString(TEST_TRANSPORT); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetDataManagementIntent_callsGetDataManagementIntentForUser() throws Exception { - mBackupManagerService.getDataManagementIntent(TEST_TRANSPORT); + public void testGetDataManagementIntent_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getDataManagementIntent(TEST_TRANSPORT); - verify(mUserBackupManagerService).getDataManagementIntent(TEST_TRANSPORT); + verify(mUserOneService).getDataManagementIntent(TEST_TRANSPORT); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetDataManagementIntent_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.getDataManagementIntent(TEST_TRANSPORT); + + verify(mUserOneService, never()).getDataManagementIntent(TEST_TRANSPORT); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetDataManagementLabel_callsGetDataManagementLabelForUser() throws Exception { - mBackupManagerService.getDataManagementLabel(TEST_TRANSPORT); + public void testGetDataManagementLabel_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getDataManagementLabel(TEST_TRANSPORT); - verify(mUserBackupManagerService).getDataManagementLabel(TEST_TRANSPORT); + verify(mUserOneService).getDataManagementLabel(TEST_TRANSPORT); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetDataManagementLabel_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.getDataManagementLabel(TEST_TRANSPORT); + + verify(mUserOneService, never()).getDataManagementLabel(TEST_TRANSPORT); } // --------------------------------------------- // Settings tests // --------------------------------------------- + /** - * Test verifying that {@link BackupManagerService#setBackupEnabled(int, boolean)} throws a - * {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. + * Test that the backup services throws a {@link SecurityException} if the caller does not have + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void setBackupEnabled_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testSetBackupEnabled_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows( SecurityException.class, - () -> mBackupManagerService.setBackupEnabled(mUserId, true)); + () -> backupManagerService.setBackupEnabled(mUserTwoId, true)); } /** - * Test verifying that {@link BackupManagerService#setBackupEnabled(int, boolean)} 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 that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void setBackupEnabled_whenCallingUserIsTargetUser_doesntNeedPermission() { - ShadowBinder.setCallingUserHandle(UserHandle.of(mUserId)); - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testSetBackupEnabled_withPermission_propagatesForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); - mBackupManagerService.setBackupEnabled(mUserId, true); + backupManagerService.setBackupEnabled(mUserTwoId, true); - verify(mUserBackupManagerService).setBackupEnabled(true); + verify(mUserTwoService).setBackupEnabled(true); } - /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void setBackupEnabled_callsSetBackupEnabledForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testSetBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + + backupManagerService.setBackupEnabled(mUserOneId, true); + + verify(mUserOneService).setBackupEnabled(true); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testSetBackupEnabled_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); - mBackupManagerService.setBackupEnabled(mUserId, true); + backupManagerService.setBackupEnabled(mUserTwoId, true); - verify(mUserBackupManagerService).setBackupEnabled(true); + verify(mUserOneService, never()).setBackupEnabled(true); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void setAutoRestore_callsSetAutoRestoreForUser() throws Exception { - mBackupManagerService.setAutoRestore(true); + public void testSetAutoRestore_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.setAutoRestore(true); + + verify(mUserOneService).setAutoRestore(true); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testSetAutoRestore_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.setAutoRestore(true); - verify(mUserBackupManagerService).setAutoRestore(true); + verify(mUserOneService, never()).setAutoRestore(true); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testSetBackupProvisioned_callsSetBackupProvisionedForUser() throws Exception { - mBackupManagerService.setBackupProvisioned(true); + public void testSetBackupProvisioned_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); - verify(mUserBackupManagerService).setBackupProvisioned(true); + backupManagerService.setBackupProvisioned(true); + + verify(mUserOneService).setBackupProvisioned(true); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testSetBackupProvisioned_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.setBackupProvisioned(true); + + verify(mUserOneService, never()).setBackupProvisioned(true); } /** - * Test verifying that {@link BackupManagerService#isBackupEnabled(int)} throws a - * {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. + * Test that the backup services throws a {@link SecurityException} if the caller does not have + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void testIsBackupEnabled_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testIsBackupEnabled_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows( - SecurityException.class, - () -> mBackupManagerService.isBackupEnabled(mUserId)); + SecurityException.class, () -> backupManagerService.isBackupEnabled(mUserTwoId)); + } + + /** + * Test that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. + */ + @Test + public void testIsBackupEnabled_withPermission_propagatesForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); + + backupManagerService.isBackupEnabled(mUserTwoId); + + verify(mUserTwoService).isBackupEnabled(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testIsBackupEnabled_callsIsBackupEnabledForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testIsBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + + backupManagerService.isBackupEnabled(mUserOneId); + + verify(mUserOneService).isBackupEnabled(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testIsBackupEnabled_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); - mBackupManagerService.isBackupEnabled(mUserId); + backupManagerService.isBackupEnabled(mUserTwoId); - verify(mUserBackupManagerService).isBackupEnabled(); + verify(mUserOneService, never()).isBackupEnabled(); } // --------------------------------------------- @@ -384,128 +778,290 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testIsAppEligibleForBackup_callsIsAppEligibleForBackupForUser() throws Exception { - mBackupManagerService.isAppEligibleForBackup(TEST_PACKAGE); + public void testIsAppEligibleForBackup_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.isAppEligibleForBackup(TEST_PACKAGE); + + verify(mUserOneService).isAppEligibleForBackup(TEST_PACKAGE); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testIsAppEligibleForBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.isAppEligibleForBackup(TEST_PACKAGE); - verify(mUserBackupManagerService).isAppEligibleForBackup(TEST_PACKAGE); + verify(mUserOneService, never()).isAppEligibleForBackup(TEST_PACKAGE); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testFilterAppsEligibleForBackup_callsFilterAppsEligibleForBackupForUser() + public void testFilterAppsEligibleForBackup_onRegisteredUser_callsMethodForUser() + throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + String[] packages = {TEST_PACKAGE}; + + backupManagerService.filterAppsEligibleForBackup(packages); + + verify(mUserOneService).filterAppsEligibleForBackup(packages); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testFilterAppsEligibleForBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); String[] packages = {TEST_PACKAGE}; - mBackupManagerService.filterAppsEligibleForBackup(packages); + backupManagerService.filterAppsEligibleForBackup(packages); - verify(mUserBackupManagerService).filterAppsEligibleForBackup(packages); + verify(mUserOneService, never()).filterAppsEligibleForBackup(packages); } /** - * Test verifying that {@link BackupManagerService#backupNow(int)} throws a - * {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. + * Test verifying that {@link BackupManagerService#backupNow(int)} throws a {@link + * SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. */ @Test - public void testBackupNow_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testBackupNow_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); - expectThrows( - SecurityException.class, - () -> mBackupManagerService.backupNow(mUserId)); + expectThrows(SecurityException.class, () -> backupManagerService.backupNow(mUserTwoId)); + } + + /** + * Test that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. + */ + @Test + public void testBackupNow_withPermission_propagatesForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); + + backupManagerService.backupNow(mUserTwoId); + + verify(mUserTwoService).backupNow(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testBackupNow_callsBackupNowForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testBackupNow_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); - mBackupManagerService.backupNow(mUserId); + backupManagerService.backupNow(mUserOneId); - verify(mUserBackupManagerService).backupNow(); + verify(mUserOneService).backupNow(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testBackupNow_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); + + backupManagerService.backupNow(mUserTwoId); + + verify(mUserOneService, never()).backupNow(); } /** - * Test verifying that {@link BackupManagerService#requestBackup(int, String[], IBackupObserver, - * IBackupManagerMonitor, int)} throws a {@link SecurityException} if the caller does not have - * INTERACT_ACROSS_USERS_FULL permission. + * Test that the backup services throws a {@link SecurityException} if the caller does not have + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void testRequestBackup_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testRequestBackup_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); String[] packages = {TEST_PACKAGE}; IBackupObserver observer = mock(IBackupObserver.class); IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); expectThrows( SecurityException.class, - () -> mBackupManagerService.requestBackup(mUserId, packages, observer, monitor, 0)); + () -> + backupManagerService.requestBackup( + mUserTwoId, packages, observer, monitor, 0)); } + /** + * Test that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. + */ + @Test + public void testRequestBackup_withPermission_propagatesForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + String[] packages = {TEST_PACKAGE}; + IBackupObserver observer = mock(IBackupObserver.class); + IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); + + backupManagerService.requestBackup(mUserTwoId, packages, observer, monitor, /* flags */ 0); + + verify(mUserTwoService).requestBackup(packages, observer, monitor, /* flags */ 0); + } + + /** Test that the backup service routes methods correctly to the user that requests it. */ + @Test + public void testRequestBackup_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + String[] packages = {TEST_PACKAGE}; + IBackupObserver observer = mock(IBackupObserver.class); + IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + + backupManagerService.requestBackup(mUserOneId, packages, observer, monitor, /* flags */ 0); + + verify(mUserOneService).requestBackup(packages, observer, monitor, /* flags */ 0); + } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testRequestBackup_callsRequestBackupForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testRequestBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); String[] packages = {TEST_PACKAGE}; IBackupObserver observer = mock(IBackupObserver.class); IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); - mBackupManagerService.requestBackup(mUserId, packages, observer, monitor, - /* flags */ 0); + backupManagerService.requestBackup(mUserTwoId, packages, observer, monitor, /* flags */ 0); - verify(mUserBackupManagerService).requestBackup(packages, observer, monitor, /* flags */ 0); + verify(mUserOneService, never()).requestBackup(packages, observer, monitor, /* flags */ 0); } /** - * Test verifying that {@link BackupManagerService#cancelBackups(int)} throws a - * {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. + * Test verifying that {@link BackupManagerService#cancelBackups(int)} throws a {@link + * SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL permission. */ @Test - public void testCancelBackups_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testCancelBackups_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); - expectThrows( - SecurityException.class, - () -> mBackupManagerService.cancelBackups(mUserId)); + expectThrows(SecurityException.class, () -> backupManagerService.cancelBackups(mUserTwoId)); } + /** + * Test that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. + */ + @Test + public void testCancelBackups_withPermission_propagatesForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); + + backupManagerService.cancelBackups(mUserTwoId); + + verify(mUserTwoService).cancelBackups(); + } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testCancelBackups_callsCancelBackupsForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testCancelBackups_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + + backupManagerService.cancelBackups(mUserOneId); + + verify(mUserOneService).cancelBackups(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testCancelBackups_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); - mBackupManagerService.cancelBackups(mUserId); + backupManagerService.cancelBackups(mUserTwoId); - verify(mUserBackupManagerService).cancelBackups(); + verify(mUserOneService, never()).cancelBackups(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testBeginFullBackup_callsBeginFullBackupForUser() throws Exception { + public void testBeginFullBackup_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); FullBackupJob job = new FullBackupJob(); - mBackupManagerService.beginFullBackup(job); + backupManagerService.beginFullBackup(job); - verify(mUserBackupManagerService).beginFullBackup(job); + verify(mUserOneService).beginFullBackup(job); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testBeginFullBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + FullBackupJob job = new FullBackupJob(); + + backupManagerService.beginFullBackup(job); + + verify(mUserOneService, never()).beginFullBackup(job); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testEndFullBackup_callsEndFullBackupForUser() throws Exception { - mBackupManagerService.endFullBackup(); + public void testEndFullBackup_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.endFullBackup(); + + verify(mUserOneService).endFullBackup(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testEndFullBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.endFullBackup(); - verify(mUserBackupManagerService).endFullBackup(); + verify(mUserOneService, never()).endFullBackup(); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testFullTransportBackup_callsFullTransportBackupForUser() throws Exception { + public void testFullTransportBackup_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); String[] packages = {TEST_PACKAGE}; - mBackupManagerService.fullTransportBackup(packages); + backupManagerService.fullTransportBackup(packages); - verify(mUserBackupManagerService).fullTransportBackup(packages); + verify(mUserOneService).fullTransportBackup(packages); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testFullTransportBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + String[] packages = {TEST_PACKAGE}; + + backupManagerService.fullTransportBackup(packages); + + verify(mUserOneService, never()).fullTransportBackup(packages); } // --------------------------------------------- @@ -514,27 +1070,66 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testRestoreAtInstall_callsRestoreAtInstallForUser() throws Exception { - mBackupManagerService.restoreAtInstall(TEST_PACKAGE, /* token */ 0); + public void testRestoreAtInstall_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.restoreAtInstall(TEST_PACKAGE, /* token */ 0); - verify(mUserBackupManagerService).restoreAtInstall(TEST_PACKAGE, /* token */ 0); + verify(mUserOneService).restoreAtInstall(TEST_PACKAGE, /* token */ 0); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testRestoreAtInstall_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.restoreAtInstall(TEST_PACKAGE, /* token */ 0); + + verify(mUserOneService, never()).restoreAtInstall(TEST_PACKAGE, /* token */ 0); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testBeginRestoreSession_callsBeginRestoreSessionForUser() throws Exception { - mBackupManagerService.beginRestoreSession(TEST_PACKAGE, TEST_TRANSPORT); + public void testBeginRestoreSession_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.beginRestoreSession(TEST_PACKAGE, TEST_TRANSPORT); - verify(mUserBackupManagerService).beginRestoreSession(TEST_PACKAGE, TEST_TRANSPORT); + verify(mUserOneService).beginRestoreSession(TEST_PACKAGE, TEST_TRANSPORT); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testBeginRestoreSession_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.beginRestoreSession(TEST_PACKAGE, TEST_TRANSPORT); + + verify(mUserOneService, never()).beginRestoreSession(TEST_PACKAGE, TEST_TRANSPORT); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testGetAvailableRestoreToken_callsGetAvailableRestoreTokenForUser() + public void testGetAvailableRestoreToken_onRegisteredUser_callsMethodForUser() throws Exception { - mBackupManagerService.getAvailableRestoreToken(TEST_PACKAGE); + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.getAvailableRestoreToken(TEST_PACKAGE); + + verify(mUserOneService).getAvailableRestoreToken(TEST_PACKAGE); + } - verify(mUserBackupManagerService).getAvailableRestoreToken(TEST_PACKAGE); + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testGetAvailableRestoreToken_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.getAvailableRestoreToken(TEST_PACKAGE); + + verify(mUserOneService, never()).getAvailableRestoreToken(TEST_PACKAGE); } // --------------------------------------------- @@ -543,33 +1138,61 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testSetBackupPassword_callsSetBackupPasswordForUser() throws Exception { - mBackupManagerService.setBackupPassword("currentPassword", "newPassword"); + public void testSetBackupPassword_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.setBackupPassword("currentPassword", "newPassword"); + + verify(mUserOneService).setBackupPassword("currentPassword", "newPassword"); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testSetBackupPassword_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.setBackupPassword("currentPassword", "newPassword"); - verify(mUserBackupManagerService).setBackupPassword("currentPassword", "newPassword"); + verify(mUserOneService, never()).setBackupPassword("currentPassword", "newPassword"); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testHasBackupPassword_callsHasBackupPasswordForUser() throws Exception { - mBackupManagerService.hasBackupPassword(); + public void testHasBackupPassword_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + + backupManagerService.hasBackupPassword(); + + verify(mUserOneService).hasBackupPassword(); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testHasBackupPassword_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + + backupManagerService.hasBackupPassword(); - verify(mUserBackupManagerService).hasBackupPassword(); + verify(mUserOneService, never()).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 that the backup services throws a {@link SecurityException} if the caller does not have + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void testAdbBackup_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testAdbBackup_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); - expectThrows(SecurityException.class, + expectThrows( + SecurityException.class, () -> - mBackupManagerService.adbBackup( - /* userId */ mUserId, + backupManagerService.adbBackup( + mUserTwoId, /* parcelFileDescriptor*/ null, /* includeApks */ true, /* includeObbs */ true, @@ -580,24 +1203,22 @@ public class BackupManagerServiceTest { /* 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 that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void testAdbBackup_whenCallingUserIsTargetUser_doesntNeedPermission() throws Exception { - ShadowBinder.setCallingUserHandle(UserHandle.of(mUserId)); - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); - + public void testAdbBackup_withPermission_propagatesForNonCallingUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); - mBackupManagerService.adbBackup( - /* userId */ mUserId, + backupManagerService.adbBackup( + mUserTwoId, parcelFileDescriptor, /* includeApks */ true, /* includeObbs */ true, @@ -609,7 +1230,7 @@ public class BackupManagerServiceTest { /* doKeyValue */ true, ADB_TEST_PACKAGES); - verify(mUserBackupManagerService) + verify(mUserTwoService) .adbBackup( parcelFileDescriptor, /* includeApks */ true, @@ -625,13 +1246,49 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testAdbBackup_callsAdbBackupForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testAdbBackup_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + backupManagerService.adbBackup( + mUserOneId, + parcelFileDescriptor, + /* includeApks */ true, + /* includeObbs */ true, + /* includeShared */ true, + /* doWidgets */ true, + /* doAllApps */ true, + /* includeSystem */ true, + /* doCompress */ true, + /* doKeyValue */ true, + ADB_TEST_PACKAGES); + + verify(mUserOneService) + .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 does not route methods for non-registered users. */ + @Test + public void testAdbBackup_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); - mBackupManagerService.adbBackup( - /* userId */ mUserId, + backupManagerService.adbBackup( + mUserTwoId, parcelFileDescriptor, /* includeApks */ true, /* includeObbs */ true, @@ -643,7 +1300,7 @@ public class BackupManagerServiceTest { /* doKeyValue */ true, ADB_TEST_PACKAGES); - verify(mUserBackupManagerService) + verify(mUserOneService, never()) .adbBackup( parcelFileDescriptor, /* includeApks */ true, @@ -658,58 +1315,93 @@ public class BackupManagerServiceTest { } /** - * Test verifying that {@link BackupManagerService#adbRestore(ParcelFileDescriptor, int)} throws - * a {@link SecurityException} if the caller does not have INTERACT_ACROSS_USERS_FULL - * permission. + * Test that the backup services throws a {@link SecurityException} if the caller does not have + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void testAdbRestore_withoutPermission_throwsSecurityException() { - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); - - expectThrows(SecurityException.class, - () -> mBackupManagerService.adbRestore(mUserId, null)); + public void testAdbRestore_withoutPermission_throwsSecurityExceptionForNonCallingUser() { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + expectThrows( + SecurityException.class, () -> backupManagerService.adbRestore(mUserTwoId, 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 that the backup service does not throw a {@link SecurityException} if the caller has + * INTERACT_ACROSS_USERS_FULL permission and passes a different user id. */ @Test - public void testAdbRestore_whenCallingUserIsTargetUser_doesntNeedPermission() throws Exception { - ShadowBinder.setCallingUserHandle(UserHandle.of(mUserId)); - mShadowContext.denyPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); - + public void testAdbRestore_withPermission_propagatesForNonCallingUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); - mBackupManagerService.adbRestore(mUserId, parcelFileDescriptor); + backupManagerService.adbRestore(mUserTwoId, parcelFileDescriptor); - verify(mUserBackupManagerService).adbRestore(parcelFileDescriptor); + verify(mUserTwoService).adbRestore(parcelFileDescriptor); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testAdbRestore_callsAdbRestoreForUser() throws Exception { - mShadowContext.grantPermissions(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); + public void testAdbRestore_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); + + backupManagerService.adbRestore(mUserOneId, parcelFileDescriptor); + + verify(mUserOneService).adbRestore(parcelFileDescriptor); + } + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testAdbRestore_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); + setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); - mBackupManagerService.adbRestore(mUserId, parcelFileDescriptor); + backupManagerService.adbRestore(mUserTwoId, parcelFileDescriptor); - verify(mUserBackupManagerService).adbRestore(parcelFileDescriptor); + verify(mUserOneService, never()).adbRestore(parcelFileDescriptor); } /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testAcknowledgeAdbBackupOrRestore_callsAcknowledgeAdbBackupOrRestoreForUser() + public void testAcknowledgeAdbBackupOrRestore_onRegisteredUser_callsMethodForUser() + throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + IFullBackupRestoreObserver observer = mock(IFullBackupRestoreObserver.class); + + backupManagerService.acknowledgeAdbBackupOrRestore( + /* token */ 0, /* allow */ true, "currentPassword", "encryptionPassword", observer); + + verify(mUserOneService) + .acknowledgeAdbBackupOrRestore( + /* token */ 0, + /* allow */ true, + "currentPassword", + "encryptionPassword", + observer); + } + + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testAcknowledgeAdbBackupOrRestore_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); IFullBackupRestoreObserver observer = mock(IFullBackupRestoreObserver.class); - mBackupManagerService.acknowledgeAdbBackupOrRestore( + backupManagerService.acknowledgeAdbBackupOrRestore( /* token */ 0, /* allow */ true, "currentPassword", "encryptionPassword", observer); - verify(mUserBackupManagerService) + verify(mUserOneService, never()) .acknowledgeAdbBackupOrRestore( /* token */ 0, /* allow */ true, @@ -724,16 +1416,60 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test - public void testDump_callsDumpForUser() throws Exception { + public void testDump_onRegisteredUser_callsMethodForUser() throws Exception { + BackupManagerService backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); File testFile = new File(mContext.getFilesDir(), "test"); testFile.createNewFile(); FileDescriptor fileDescriptor = new FileDescriptor(); PrintWriter printWriter = new PrintWriter(testFile); String[] args = {"1", "2"}; - mBackupManagerService.dump(fileDescriptor, printWriter, args); + backupManagerService.dump(fileDescriptor, printWriter, args); + + verify(mUserOneService).dump(fileDescriptor, printWriter, args); + } - verify(mUserBackupManagerService).dump(fileDescriptor, printWriter, args); + /** Test that the backup service does not route methods for non-registered users. */ + @Test + public void testDump_onUnknownUser_doesNotPropagateCall() throws Exception { + BackupManagerService backupManagerService = createService(); + File testFile = new File(mContext.getFilesDir(), "test"); + testFile.createNewFile(); + FileDescriptor fileDescriptor = new FileDescriptor(); + PrintWriter printWriter = new PrintWriter(testFile); + String[] args = {"1", "2"}; + + backupManagerService.dump(fileDescriptor, printWriter, args); + + verify(mUserOneService, never()).dump(fileDescriptor, printWriter, args); + } + + private BackupManagerService createService() { + return new BackupManagerService( + mContext, new Trampoline(mContext), startBackupThread(null)); + } + + private BackupManagerService createServiceAndRegisterUser( + int userId, UserBackupManagerService userBackupManagerService) { + BackupManagerService backupManagerService = createService(); + backupManagerService.startServiceForUser(userId, userBackupManagerService); + return backupManagerService; + } + + /** + * Sets the calling user to {@code userId} and grants the permission INTERACT_ACROSS_USERS_FULL + * to the caller if {@code shouldGrantPermission} is {@code true}, else it denies the + * permission. + */ + private void setCallerAndGrantInteractUserPermission( + @UserIdInt int userId, boolean shouldGrantPermission) { + ShadowBinder.setCallingUserHandle(UserHandle.of(userId)); + if (shouldGrantPermission) { + mShadowContext.grantPermissions(INTERACT_ACROSS_USERS_FULL); + } else { + mShadowContext.denyPermissions(INTERACT_ACROSS_USERS_FULL); + } } private ParcelFileDescriptor getFileDescriptorForAdbTest() throws Exception { |