diff options
| author | 2019-08-12 15:40:49 +0100 | |
|---|---|---|
| committer | 2019-08-13 18:36:24 +0100 | |
| commit | 9b55bfbd17b6b6e44aea328a83dce7a32aee18fb (patch) | |
| tree | 0239b94f2830d5efbf86ef60339c07b6c854e90f | |
| parent | ac81c6a08f46623b355dcbb48f91a75f6c049c4f (diff) | |
Move start and stop of sub-systems to Trampoline
From BMS. Moved mUserServices list creation from BMS to Trampoline.
BMS still uses it but the plan is to move gradually in multiple
CLs to eventually delete it. Both are dealing with the same instance
because of this. Start and stop of sub-system (uBMS) is now handled in
Trampoline. getUserServices() was also moved. BMS robolectric tests were moved
to Trampoline *robolectric* tests to simplify this CL. After we merge BMS and
Trampoline, we can look into moving tests away from Robolectric.
Test: atest BackupManagerServiceTest TrampolineRoboTest TrampolineTest
Bug: 135661048
Change-Id: I4e612d06fe006c12f215a94f210450a5e6316b75
8 files changed, 562 insertions, 330 deletions
diff --git a/services/backup/java/com/android/server/backup/BackupManagerService.java b/services/backup/java/com/android/server/backup/BackupManagerService.java index dc0bdb325f24..fffdba70c944 100644 --- a/services/backup/java/com/android/server/backup/BackupManagerService.java +++ b/services/backup/java/com/android/server/backup/BackupManagerService.java @@ -38,7 +38,6 @@ import android.content.pm.PackageManager; import android.os.Binder; import android.os.IBinder; import android.os.ParcelFileDescriptor; -import android.os.Trace; import android.os.UserHandle; import android.os.UserManager; import android.util.Slog; @@ -50,7 +49,6 @@ import com.android.server.SystemService; import java.io.FileDescriptor; import java.io.PrintWriter; -import java.util.Set; /** * Definition of the system service that performs backup/restore operations. @@ -70,14 +68,17 @@ public class BackupManagerService { private final Context mContext; private final Trampoline mTrampoline; - - // Keeps track of all unlocked users registered with this service. Indexed by user id. - private final SparseArray<UserBackupManagerService> mServiceUsers = new SparseArray<>(); + private final SparseArray<UserBackupManagerService> mServiceUsers; /** Instantiate a new instance of {@link BackupManagerService}. */ - public BackupManagerService(Context context, Trampoline trampoline) { + public BackupManagerService( + Context context, + Trampoline trampoline, + SparseArray<UserBackupManagerService> userServices) { mContext = checkNotNull(context); mTrampoline = checkNotNull(trampoline); + // TODO(b/135661048): Remove + mServiceUsers = userServices; } /** @@ -98,48 +99,6 @@ public class BackupManagerService { // USER LIFECYCLE CALLBACKS // --------------------------------------------- - /** - * Starts the backup service for user {@code userId} by creating a new instance of {@link - * UserBackupManagerService} and registering it with this service. - */ - @VisibleForTesting - protected void startServiceForUser(int userId, Set<ComponentName> transportWhitelist) { - if (mServiceUsers.get(userId) != null) { - Slog.i(TAG, "userId " + userId + " already started, so not starting again"); - return; - } - - UserBackupManagerService userBackupManagerService = - UserBackupManagerService.createAndInitializeService( - userId, mContext, mTrampoline, transportWhitelist); - startServiceForUser(userId, userBackupManagerService); - } - - /** - * Starts the backup service for user {@code userId} by registering its instance of {@link - * UserBackupManagerService} with this service and setting enabled state. - */ - void startServiceForUser(int userId, UserBackupManagerService userBackupManagerService) { - mServiceUsers.put(userId, userBackupManagerService); - - Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup enable"); - userBackupManagerService.initializeBackupEnableState(); - Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); - } - - /** Stops the backup service for user {@code userId} when the user is stopped. */ - @VisibleForTesting - protected void stopServiceForUser(int userId) { - UserBackupManagerService userBackupManagerService = mServiceUsers.removeReturnOld(userId); - - if (userBackupManagerService != null) { - userBackupManagerService.tearDownService(); - - KeyValueBackupJob.cancel(userId, mContext); - FullBackupJob.cancel(userId, mContext); - } - } - boolean isAbleToServeUser(int userId) { return getUserServices().get(UserHandle.USER_SYSTEM) != null && getUserServices().get(userId) != null; @@ -453,7 +412,7 @@ public class BackupManagerService { } for (int userId : userIds) { - UserBackupManagerService userBackupManagerService = getUserServices().get(userId); + UserBackupManagerService userBackupManagerService = mServiceUsers.get(userId); if (userBackupManagerService != null) { if (userBackupManagerService.getAncestralSerialNumber() == ancestralSerialNumber) { return UserHandle.of(userId); diff --git a/services/backup/java/com/android/server/backup/Trampoline.java b/services/backup/java/com/android/server/backup/Trampoline.java index 59d9c0eb3219..cd2fb03af4f9 100644 --- a/services/backup/java/com/android/server/backup/Trampoline.java +++ b/services/backup/java/com/android/server/backup/Trampoline.java @@ -46,9 +46,11 @@ import android.os.ParcelFileDescriptor; import android.os.Process; import android.os.RemoteException; import android.os.SystemProperties; +import android.os.Trace; import android.os.UserHandle; import android.os.UserManager; import android.util.Slog; +import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; @@ -127,6 +129,9 @@ public class Trampoline extends IBackupManager.Stub { private final Handler mHandler; private final Set<ComponentName> mTransportWhitelist; + /** Keeps track of all unlocked users registered with this service. Indexed by user id. */ + private final SparseArray<UserBackupManagerService> mUserServices; + private final BroadcastReceiver mUserRemovedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -140,6 +145,11 @@ public class Trampoline extends IBackupManager.Stub { }; public Trampoline(Context context) { + this(context, new SparseArray<>()); + } + + @VisibleForTesting + Trampoline(Context context, SparseArray<UserBackupManagerService> userServices) { mContext = context; mGlobalDisable = isBackupDisabled(); HandlerThread handlerThread = @@ -147,7 +157,8 @@ public class Trampoline extends IBackupManager.Stub { handlerThread.start(); mHandler = new Handler(handlerThread.getLooper()); mUserManager = UserManager.get(context); - mService = new BackupManagerService(mContext, this); + mUserServices = userServices; + mService = new BackupManagerService(mContext, this, mUserServices); Set<ComponentName> transportWhitelist = SystemConfig.getInstance().getBackupTransportWhitelist(); mTransportWhitelist = (transportWhitelist == null) ? emptySet() : transportWhitelist; @@ -297,7 +308,12 @@ public class Trampoline extends IBackupManager.Stub { postToHandler(() -> startServiceForUser(userId)); } - private void startServiceForUser(int userId) { + /** + * Starts the backup service for user {@code userId} by creating a new instance of {@link + * UserBackupManagerService} and registering it with this service. + */ + @VisibleForTesting + void startServiceForUser(int userId) { // We know that the user is unlocked here because it is called from setBackupServiceActive // and unlockUser which have these guarantees. So we can check if the file exists. if (mGlobalDisable) { @@ -308,8 +324,53 @@ public class Trampoline extends IBackupManager.Stub { Slog.i(TAG, "Backup not activated for user " + userId); return; } + if (mUserServices.get(userId) != null) { + Slog.i(TAG, "userId " + userId + " already started, so not starting again"); + return; + } Slog.i(TAG, "Starting service for user: " + userId); - mService.startServiceForUser(userId, mTransportWhitelist); + UserBackupManagerService userBackupManagerService = + UserBackupManagerService.createAndInitializeService( + userId, mContext, this, mTransportWhitelist); + startServiceForUser(userId, userBackupManagerService); + } + + /** + * Starts the backup service for user {@code userId} by registering its instance of {@link + * UserBackupManagerService} with this service and setting enabled state. + */ + void startServiceForUser(int userId, UserBackupManagerService userBackupManagerService) { + mUserServices.put(userId, userBackupManagerService); + + Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup enable"); + userBackupManagerService.initializeBackupEnableState(); + Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); + } + + /** Stops the backup service for user {@code userId} when the user is stopped. */ + @VisibleForTesting + protected void stopServiceForUser(int userId) { + UserBackupManagerService userBackupManagerService = mUserServices.removeReturnOld(userId); + + if (userBackupManagerService != null) { + userBackupManagerService.tearDownService(); + + KeyValueBackupJob.cancel(userId, mContext); + FullBackupJob.cancel(userId, mContext); + } + } + + /** + * Returns a list of users currently unlocked that have a {@link UserBackupManagerService} + * registered. + * + * Warning: Do NOT modify returned object as it's used inside. + * + * TODO: Return a copy or only expose read-only information through other means. + */ + @VisibleForTesting + SparseArray<UserBackupManagerService> getUserServices() { + return mUserServices; } /** @@ -321,7 +382,7 @@ public class Trampoline extends IBackupManager.Stub { () -> { if (!mGlobalDisable) { Slog.i(TAG, "Stopping service for user: " + userId); - mService.stopServiceForUser(userId); + stopServiceForUser(userId); } }); } @@ -329,7 +390,7 @@ public class Trampoline extends IBackupManager.Stub { /** Returns {@link UserBackupManagerService} for user {@code userId}. */ @Nullable public UserBackupManagerService getUserService(int userId) { - return mService.getUserServices().get(userId); + return mUserServices.get(userId); } /** diff --git a/services/backup/java/com/android/server/backup/UserBackupManagerService.java b/services/backup/java/com/android/server/backup/UserBackupManagerService.java index d599aabbaa28..0e81e077652e 100644 --- a/services/backup/java/com/android/server/backup/UserBackupManagerService.java +++ b/services/backup/java/com/android/server/backup/UserBackupManagerService.java @@ -649,7 +649,8 @@ public class UserBackupManagerService { } /** Cleans up state when the user of this service is stopped. */ - void tearDownService() { + @VisibleForTesting + protected void tearDownService() { mAgentTimeoutParameters.stop(); mConstants.stop(); mContext.getContentResolver().unregisterContentObserver(mSetupObserver); diff --git a/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java b/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java index 0d99561af889..c92737b96897 100644 --- a/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java +++ b/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java @@ -36,8 +36,6 @@ import static org.mockito.Mockito.verify; import static org.robolectric.Shadows.shadowOf; import static org.testng.Assert.expectThrows; -import static java.util.Collections.emptySet; - import android.annotation.UserIdInt; import android.app.Application; import android.app.backup.IBackupManagerMonitor; @@ -48,14 +46,18 @@ import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.os.ParcelFileDescriptor; +import android.os.Process; import android.os.UserHandle; +import android.os.UserManager; import android.platform.test.annotations.Presubmit; import android.util.SparseArray; import com.android.server.backup.testing.TransportData; import com.android.server.testing.shadows.ShadowApplicationPackageManager; import com.android.server.testing.shadows.ShadowBinder; +import com.android.server.testing.shadows.ShadowEnvironment; import com.android.server.testing.shadows.ShadowSystemServiceRegistry; +import com.android.server.testing.shadows.ShadowUserManager; import org.junit.After; import org.junit.Before; @@ -66,6 +68,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import org.robolectric.shadow.api.Shadow; import org.robolectric.shadows.ShadowContextWrapper; import java.io.File; @@ -80,7 +83,9 @@ import java.io.StringWriter; shadows = { ShadowApplicationPackageManager.class, ShadowBinder.class, - ShadowSystemServiceRegistry.class + ShadowEnvironment.class, + ShadowSystemServiceRegistry.class, + ShadowUserManager.class, }) @Presubmit public class BackupManagerServiceTest { @@ -89,7 +94,9 @@ public class BackupManagerServiceTest { private static final String[] ADB_TEST_PACKAGES = {TEST_PACKAGE}; private ShadowContextWrapper mShadowContext; + private ShadowUserManager mShadowUserManager; private Context mContext; + private Trampoline mTrampoline; @UserIdInt private int mUserOneId; @UserIdInt private int mUserTwoId; @Mock private UserBackupManagerService mUserOneService; @@ -103,9 +110,18 @@ public class BackupManagerServiceTest { Application application = RuntimeEnvironment.application; mContext = application; mShadowContext = shadowOf(application); + mShadowUserManager = Shadow.extract(UserManager.get(application)); mUserOneId = UserHandle.USER_SYSTEM + 1; mUserTwoId = mUserOneId + 1; + mShadowUserManager.addUser(mUserOneId, "mUserOneId", 0); + mShadowUserManager.addUser(mUserTwoId, "mUserTwoId", 0); + + mShadowContext.grantPermissions(BACKUP); + mShadowContext.grantPermissions(INTERACT_ACROSS_USERS_FULL); + + mTrampoline = new Trampoline(mContext); + ShadowBinder.setCallingUid(Process.SYSTEM_UID); } /** @@ -133,7 +149,7 @@ public class BackupManagerServiceTest { public void testConstructor_doesNotRegisterUsers() throws Exception { BackupManagerService backupManagerService = createService(); - assertThat(backupManagerService.getUserServices().size()).isEqualTo(0); + assertThat(mTrampoline.getUserServices().size()).isEqualTo(0); } /** Test that the constructor handles {@code null} parameters. */ @@ -144,7 +160,8 @@ public class BackupManagerServiceTest { () -> new BackupManagerService( /* context */ null, - new Trampoline(mContext))); + new Trampoline(mContext), + new SparseArray<>())); } /** Test that the constructor handles {@code null} parameters. */ @@ -154,70 +171,7 @@ public class BackupManagerServiceTest { NullPointerException.class, () -> new BackupManagerService( - mContext, /* trampoline */ null)); - } - - /** Test that the service registers users. */ - @Test - public void testStartServiceForUser_registersUser() throws Exception { - BackupManagerService backupManagerService = createService(); - - backupManagerService.startServiceForUser(mUserOneId, emptySet()); - - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); - 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.getUserServices(); - assertThat(serviceUsers.size()).isEqualTo(1); - assertThat(serviceUsers.get(mUserOneId)).isEqualTo(mUserOneService); - } - - /** Test that the service unregisters users when stopped. */ - @Test - public void testStopServiceForUser_forRegisteredUser_unregistersCorrectUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); - - backupManagerService.stopServiceForUser(mUserOneId); - - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); - assertThat(serviceUsers.size()).isEqualTo(1); - assertThat(serviceUsers.get(mUserOneId)).isNull(); - assertThat(serviceUsers.get(mUserTwoId)).isEqualTo(mUserTwoService); - } - - /** Test that the service unregisters users when stopped. */ - @Test - public void testStopServiceForUser_forRegisteredUser_tearsDownCorrectUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); - - backupManagerService.stopServiceForUser(mUserOneId); - - verify(mUserOneService).tearDownService(); - verify(mUserTwoService, never()).tearDownService(); - } - - /** Test that the service unregisters users when stopped. */ - @Test - public void testStopServiceForUser_forUnknownUser_doesNothing() throws Exception { - BackupManagerService backupManagerService = createService(); - - backupManagerService.stopServiceForUser(mUserOneId); - - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); - assertThat(serviceUsers.size()).isEqualTo(0); + mContext, /* trampoline */ null, new SparseArray<>())); } /** @@ -226,8 +180,8 @@ public class BackupManagerServiceTest { */ @Test public void testGetServiceForUser_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); expectThrows( @@ -243,8 +197,8 @@ public class BackupManagerServiceTest { */ @Test public void testGetServiceForUserIfCallerHasPermission_withPermission_worksForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ true); assertEquals( @@ -258,8 +212,8 @@ public class BackupManagerServiceTest { */ @Test public void testGetServiceForUserIfCallerHasPermission_withoutPermission_worksForCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); assertEquals( @@ -274,8 +228,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testDataChanged_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.dataChanged(mUserOneId, TEST_PACKAGE); @@ -286,8 +240,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testDataChanged_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.dataChanged(mUserTwoId, TEST_PACKAGE); @@ -298,8 +252,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testAgentConnected_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); IBinder agentBinder = mock(IBinder.class); @@ -311,8 +265,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testAgentConnected_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); IBinder agentBinder = mock(IBinder.class); @@ -324,8 +278,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testAgentDisconnected_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.agentDisconnected(mUserOneId, TEST_PACKAGE); @@ -336,8 +290,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testAgentDisconnected_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.agentDisconnected(mUserTwoId, TEST_PACKAGE); @@ -348,8 +302,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testOpComplete_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.opComplete(mUserOneId, /* token */ 0, /* result */ 0L); @@ -360,8 +314,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testOpComplete_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.opComplete(mUserTwoId, /* token */ 0, /* result */ 0L); @@ -376,8 +330,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testInitializeTransports_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); String[] transports = {TEST_TRANSPORT}; @@ -389,8 +343,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testInitializeTransports_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); String[] transports = {TEST_TRANSPORT}; @@ -402,8 +356,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testClearBackupData_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.clearBackupData(mUserOneId, TEST_TRANSPORT, TEST_PACKAGE); @@ -414,8 +368,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testClearBackupData_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.clearBackupData(mUserTwoId, TEST_TRANSPORT, TEST_PACKAGE); @@ -426,8 +380,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testGetCurrentTransport_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getCurrentTransport(mUserOneId); @@ -438,8 +392,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testGetCurrentTransport_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getCurrentTransport(mUserTwoId); @@ -451,8 +405,8 @@ public class BackupManagerServiceTest { @Test public void testGetCurrentTransportComponent_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getCurrentTransportComponent(mUserOneId); @@ -464,8 +418,8 @@ public class BackupManagerServiceTest { @Test public void testGetCurrentTransportComponent_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getCurrentTransportComponent(mUserTwoId); @@ -476,8 +430,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testListAllTransports_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.listAllTransports(mUserOneId); @@ -488,8 +442,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testListAllTransports_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.listAllTransports(mUserTwoId); @@ -501,8 +455,8 @@ public class BackupManagerServiceTest { @Test public void testListAllTransportComponents_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.listAllTransportComponents(mUserOneId); @@ -514,8 +468,8 @@ public class BackupManagerServiceTest { @Test public void testListAllTransportComponents_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.listAllTransportComponents(mUserTwoId); @@ -527,8 +481,8 @@ public class BackupManagerServiceTest { @Test public void testUpdateTransportAttributes_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); TransportData transport = backupTransport(); Intent configurationIntent = new Intent(); @@ -557,8 +511,8 @@ public class BackupManagerServiceTest { @Test public void testUpdateTransportAttributes_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); TransportData transport = backupTransport(); Intent configurationIntent = new Intent(); @@ -586,8 +540,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testSelectBackupTransport_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.selectBackupTransport(mUserOneId, TEST_TRANSPORT); @@ -598,8 +552,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testSelectBackupTransport_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.selectBackupTransport(mUserTwoId, TEST_TRANSPORT); @@ -610,8 +564,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testSelectTransportAsync_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); TransportData transport = backupTransport(); ISelectBackupTransportCallback callback = mock(ISelectBackupTransportCallback.class); @@ -627,8 +581,8 @@ public class BackupManagerServiceTest { @Test public void testSelectBackupTransportAsync_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); TransportData transport = backupTransport(); ISelectBackupTransportCallback callback = mock(ISelectBackupTransportCallback.class); @@ -643,8 +597,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testGetConfigurationIntent_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getConfigurationIntent(mUserOneId, TEST_TRANSPORT); @@ -655,8 +609,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testGetConfigurationIntent_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getConfigurationIntent(mUserTwoId, TEST_TRANSPORT); @@ -667,8 +621,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testGetDestinationString_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getDestinationString(mUserOneId, TEST_TRANSPORT); @@ -679,8 +633,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testGetDestinationString_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getDestinationString(mUserTwoId, TEST_TRANSPORT); @@ -691,8 +645,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testGetDataManagementIntent_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getDataManagementIntent(mUserOneId, TEST_TRANSPORT); @@ -703,8 +657,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testGetDataManagementIntent_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getDataManagementIntent(mUserTwoId, TEST_TRANSPORT); @@ -715,8 +669,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testGetDataManagementLabel_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getDataManagementLabel(mUserOneId, TEST_TRANSPORT); @@ -727,8 +681,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testGetDataManagementLabel_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getDataManagementLabel(mUserTwoId, TEST_TRANSPORT); @@ -745,8 +699,8 @@ public class BackupManagerServiceTest { */ @Test public void testSetBackupEnabled_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows( @@ -760,9 +714,10 @@ public class BackupManagerServiceTest { */ @Test public void testSetBackupEnabled_withPermission_propagatesForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + registerUser(mUserOneId, mUserOneService); + registerUser(mUserTwoId, mUserTwoService); + BackupManagerService backupManagerService = createService(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); backupManagerService.setBackupEnabled(mUserTwoId, true); @@ -773,8 +728,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testSetBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.setBackupEnabled(mUserOneId, true); @@ -785,8 +740,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.setBackupEnabled(mUserTwoId, true); @@ -797,8 +752,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testSetAutoRestore_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.setAutoRestore(mUserOneId, true); @@ -809,8 +764,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testSetAutoRestore_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.setAutoRestore(mUserTwoId, true); @@ -821,8 +776,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testIsBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.isBackupEnabled(mUserOneId); @@ -833,8 +788,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.isBackupEnabled(mUserTwoId); @@ -849,8 +804,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testIsAppEligibleForBackup_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.isAppEligibleForBackup(mUserOneId, TEST_PACKAGE); @@ -861,8 +816,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testIsAppEligibleForBackup_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.isAppEligibleForBackup(mUserTwoId, TEST_PACKAGE); @@ -874,8 +829,8 @@ public class BackupManagerServiceTest { @Test public void testFilterAppsEligibleForBackup_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); String[] packages = {TEST_PACKAGE}; @@ -888,8 +843,8 @@ public class BackupManagerServiceTest { @Test public void testFilterAppsEligibleForBackup_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); String[] packages = {TEST_PACKAGE}; @@ -904,8 +859,8 @@ public class BackupManagerServiceTest { */ @Test public void testBackupNow_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows(SecurityException.class, () -> backupManagerService.backupNow(mUserTwoId)); @@ -917,9 +872,10 @@ public class BackupManagerServiceTest { */ @Test public void testBackupNow_withPermission_propagatesForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + registerUser(mUserOneId, mUserOneService); + registerUser(mUserTwoId, mUserTwoService); + BackupManagerService backupManagerService = createService(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); backupManagerService.backupNow(mUserTwoId); @@ -930,8 +886,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testBackupNow_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.backupNow(mUserOneId); @@ -942,8 +898,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.backupNow(mUserTwoId); @@ -957,8 +913,8 @@ public class BackupManagerServiceTest { */ @Test public void testRequestBackup_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); String[] packages = {TEST_PACKAGE}; IBackupObserver observer = mock(IBackupObserver.class); @@ -977,9 +933,10 @@ public class BackupManagerServiceTest { */ @Test public void testRequestBackup_withPermission_propagatesForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + registerUser(mUserOneId, mUserOneService); + registerUser(mUserTwoId, mUserTwoService); + BackupManagerService backupManagerService = createService(); + String[] packages = {TEST_PACKAGE}; IBackupObserver observer = mock(IBackupObserver.class); IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); @@ -993,8 +950,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); String[] packages = {TEST_PACKAGE}; IBackupObserver observer = mock(IBackupObserver.class); IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); @@ -1008,8 +965,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testRequestBackup_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); String[] packages = {TEST_PACKAGE}; IBackupObserver observer = mock(IBackupObserver.class); IBackupManagerMonitor monitor = mock(IBackupManagerMonitor.class); @@ -1026,8 +983,8 @@ public class BackupManagerServiceTest { */ @Test public void testCancelBackups_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows(SecurityException.class, () -> backupManagerService.cancelBackups(mUserTwoId)); @@ -1039,9 +996,10 @@ public class BackupManagerServiceTest { */ @Test public void testCancelBackups_withPermission_propagatesForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + registerUser(mUserOneId, mUserOneService); + registerUser(mUserTwoId, mUserTwoService); + BackupManagerService backupManagerService = createService(); + setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); backupManagerService.cancelBackups(mUserTwoId); @@ -1052,8 +1010,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testCancelBackups_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.cancelBackups(mUserOneId); @@ -1064,8 +1022,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.cancelBackups(mUserTwoId); @@ -1076,8 +1034,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testBeginFullBackup_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(UserHandle.USER_SYSTEM, mUserOneService); + registerUser(UserHandle.USER_SYSTEM, mUserOneService); + BackupManagerService backupManagerService = createService(); FullBackupJob job = new FullBackupJob(); backupManagerService.beginFullBackup(UserHandle.USER_SYSTEM, job); @@ -1099,8 +1057,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testEndFullBackup_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(UserHandle.USER_SYSTEM, mUserOneService); + registerUser(UserHandle.USER_SYSTEM, mUserOneService); + BackupManagerService backupManagerService = createService(); backupManagerService.endFullBackup(UserHandle.USER_SYSTEM); @@ -1120,8 +1078,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testFullTransportBackup_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); String[] packages = {TEST_PACKAGE}; @@ -1133,8 +1091,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testFullTransportBackup_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); String[] packages = {TEST_PACKAGE}; @@ -1150,8 +1108,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testRestoreAtInstall_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.restoreAtInstall(mUserOneId, TEST_PACKAGE, /* token */ 0); @@ -1162,8 +1120,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testRestoreAtInstall_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.restoreAtInstall(mUserTwoId, TEST_PACKAGE, /* token */ 0); @@ -1174,8 +1132,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testBeginRestoreSession_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.beginRestoreSession(mUserOneId, TEST_PACKAGE, TEST_TRANSPORT); @@ -1186,8 +1144,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testBeginRestoreSession_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.beginRestoreSession(mUserTwoId, TEST_PACKAGE, TEST_TRANSPORT); @@ -1199,8 +1157,8 @@ public class BackupManagerServiceTest { @Test public void testGetAvailableRestoreToken_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); backupManagerService.getAvailableRestoreToken(mUserOneId, TEST_PACKAGE); @@ -1211,8 +1169,8 @@ public class BackupManagerServiceTest { /** Test that the backup service does not route methods for non-registered users. */ @Test public void testGetAvailableRestoreToken_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); backupManagerService.getAvailableRestoreToken(mUserTwoId, TEST_PACKAGE); @@ -1227,8 +1185,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testSetBackupPassword_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(UserHandle.USER_SYSTEM, mUserOneService); + registerUser(UserHandle.USER_SYSTEM, mUserOneService); + BackupManagerService backupManagerService = createService(); backupManagerService.setBackupPassword("currentPassword", "newPassword"); @@ -1248,8 +1206,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testHasBackupPassword_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(UserHandle.USER_SYSTEM, mUserOneService); + registerUser(UserHandle.USER_SYSTEM, mUserOneService); + BackupManagerService backupManagerService = createService(); backupManagerService.hasBackupPassword(); @@ -1272,8 +1230,8 @@ public class BackupManagerServiceTest { */ @Test public void testAdbBackup_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows( @@ -1299,9 +1257,10 @@ public class BackupManagerServiceTest { */ @Test public void testAdbBackup_withPermission_propagatesForNonCallingUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + registerUser(mUserOneId, mUserOneService); + registerUser(mUserTwoId, mUserTwoService); + BackupManagerService backupManagerService = createService(); + ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); @@ -1335,8 +1294,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testAdbBackup_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); @@ -1370,8 +1329,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); @@ -1408,8 +1367,8 @@ public class BackupManagerServiceTest { */ @Test public void testAdbRestore_withoutPermission_throwsSecurityExceptionForNonCallingUser() { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); expectThrows( @@ -1422,9 +1381,9 @@ public class BackupManagerServiceTest { */ @Test public void testAdbRestore_withPermission_propagatesForNonCallingUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); - backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + registerUser(mUserOneId, mUserOneService); + registerUser(mUserTwoId, mUserTwoService); + BackupManagerService backupManagerService = createService(); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true); @@ -1436,8 +1395,8 @@ public class BackupManagerServiceTest { /** Test that the backup service routes methods correctly to the user that requests it. */ @Test public void testAdbRestore_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); @@ -1449,8 +1408,8 @@ public class BackupManagerServiceTest { /** 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); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); ParcelFileDescriptor parcelFileDescriptor = getFileDescriptorForAdbTest(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); @@ -1463,8 +1422,8 @@ public class BackupManagerServiceTest { @Test public void testAcknowledgeAdbBackupOrRestore_onRegisteredUser_callsMethodForUser() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false); IFullBackupRestoreObserver observer = mock(IFullBackupRestoreObserver.class); @@ -1489,8 +1448,8 @@ public class BackupManagerServiceTest { @Test public void testAcknowledgeAdbBackupOrRestore_onUnknownUser_doesNotPropagateCall() throws Exception { - BackupManagerService backupManagerService = - createServiceAndRegisterUser(mUserOneId, mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false); IFullBackupRestoreObserver observer = mock(IFullBackupRestoreObserver.class); @@ -1515,7 +1474,6 @@ public class BackupManagerServiceTest { // Lifecycle tests // --------------------------------------------- - /** testOnStart_publishesService */ @Test public void testOnStart_publishesService() { @@ -1562,8 +1520,8 @@ public class BackupManagerServiceTest { public void testDump_onRegisteredUser_callsMethodForUser() throws Exception { grantDumpPermissions(); - BackupManagerService backupManagerService = - createServiceAndRegisterUser(UserHandle.USER_SYSTEM, mUserOneService); + registerUser(UserHandle.USER_SYSTEM, mUserOneService); + BackupManagerService backupManagerService = createService(); File testFile = createTestFile(); FileDescriptor fileDescriptor = new FileDescriptor(); PrintWriter printWriter = new PrintWriter(testFile); @@ -1595,8 +1553,8 @@ public class BackupManagerServiceTest { public void testDump_users_dumpsListOfRegisteredUsers() { grantDumpPermissions(); - BackupManagerService backupManagerService = createServiceAndRegisterUser(mUserOneId, - mUserOneService); + registerUser(mUserOneId, mUserOneService); + BackupManagerService backupManagerService = createService(); StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out); String[] args = {"users"}; @@ -1623,14 +1581,12 @@ public class BackupManagerServiceTest { private BackupManagerService createService() { mShadowContext.grantPermissions(BACKUP); - return new BackupManagerService(mContext, new Trampoline(mContext)); + return new BackupManagerService(mContext, mTrampoline, mTrampoline.getUserServices()); } - private BackupManagerService createServiceAndRegisterUser( - int userId, UserBackupManagerService userBackupManagerService) { - BackupManagerService backupManagerService = createService(); - backupManagerService.startServiceForUser(userId, userBackupManagerService); - return backupManagerService; + private void registerUser(int userId, UserBackupManagerService userBackupManagerService) { + mTrampoline.setBackupServiceActive(userId, true); + mTrampoline.startServiceForUser(userId, userBackupManagerService); } /** diff --git a/services/robotests/backup/src/com/android/server/backup/TrampolineRoboTest.java b/services/robotests/backup/src/com/android/server/backup/TrampolineRoboTest.java new file mode 100644 index 000000000000..dfad1a9120d7 --- /dev/null +++ b/services/robotests/backup/src/com/android/server/backup/TrampolineRoboTest.java @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2019 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.server.backup; + +import static android.Manifest.permission.BACKUP; +import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.robolectric.Shadows.shadowOf; + +import android.annotation.UserIdInt; +import android.app.Application; +import android.content.Context; +import android.os.Process; +import android.os.UserHandle; +import android.os.UserManager; +import android.platform.test.annotations.Presubmit; +import android.util.SparseArray; + +import com.android.server.testing.shadows.ShadowApplicationPackageManager; +import com.android.server.testing.shadows.ShadowBinder; +import com.android.server.testing.shadows.ShadowEnvironment; +import com.android.server.testing.shadows.ShadowSystemServiceRegistry; +import com.android.server.testing.shadows.ShadowUserManager; + +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.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadow.api.Shadow; +import org.robolectric.shadows.ShadowContextWrapper; + +/** Tests for {@link com.android.server.backup.Trampoline}. */ +@RunWith(RobolectricTestRunner.class) +@Config( + shadows = { + ShadowApplicationPackageManager.class, + ShadowBinder.class, + ShadowUserManager.class, + ShadowEnvironment.class, + ShadowSystemServiceRegistry.class + }) +@Presubmit +public class TrampolineRoboTest { + private Context mContext; + private ShadowContextWrapper mShadowContext; + private ShadowUserManager mShadowUserManager; + @UserIdInt private int mUserOneId; + @UserIdInt private int mUserTwoId; + @Mock private UserBackupManagerService mUserOneService; + @Mock private UserBackupManagerService mUserTwoService; + + /** Setup */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + Application application = RuntimeEnvironment.application; + mContext = application; + mShadowContext = shadowOf(application); + mShadowUserManager = Shadow.extract(UserManager.get(application)); + + mUserOneId = UserHandle.USER_SYSTEM + 1; + mUserTwoId = mUserOneId + 1; + mShadowUserManager.addUser(mUserOneId, "mUserOneId", 0); + mShadowUserManager.addUser(mUserTwoId, "mUserTwoId", 0); + + mShadowContext.grantPermissions(BACKUP); + mShadowContext.grantPermissions(INTERACT_ACROSS_USERS_FULL); + + ShadowBinder.setCallingUid(Process.SYSTEM_UID); + } + + /** Test that the service registers users. */ + @Test + public void testStartServiceForUser_registersUser() throws Exception { + Trampoline backupManagerService = createService(); + backupManagerService.setBackupServiceActive(mUserOneId, true); + + backupManagerService.startServiceForUser(mUserOneId); + + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); + assertThat(serviceUsers.size()).isEqualTo(1); + assertThat(serviceUsers.get(mUserOneId)).isNotNull(); + } + + /** Test that the service registers users. */ + @Test + public void testStartServiceForUser_withServiceInstance_registersUser() throws Exception { + Trampoline backupManagerService = createService(); + backupManagerService.setBackupServiceActive(mUserOneId, true); + + backupManagerService.startServiceForUser(mUserOneId, mUserOneService); + + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); + assertThat(serviceUsers.size()).isEqualTo(1); + assertThat(serviceUsers.get(mUserOneId)).isEqualTo(mUserOneService); + } + + /** Test that the service unregisters users when stopped. */ + @Test + public void testStopServiceForUser_forRegisteredUser_unregistersCorrectUser() throws Exception { + Trampoline backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + ShadowBinder.setCallingUid(Process.SYSTEM_UID); + + backupManagerService.stopServiceForUser(mUserOneId); + + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); + assertThat(serviceUsers.size()).isEqualTo(1); + assertThat(serviceUsers.get(mUserOneId)).isNull(); + assertThat(serviceUsers.get(mUserTwoId)).isEqualTo(mUserTwoService); + } + + /** Test that the service unregisters users when stopped. */ + @Test + public void testStopServiceForUser_forRegisteredUser_tearsDownCorrectUser() throws Exception { + Trampoline backupManagerService = + createServiceAndRegisterUser(mUserOneId, mUserOneService); + backupManagerService.setBackupServiceActive(mUserTwoId, true); + backupManagerService.startServiceForUser(mUserTwoId, mUserTwoService); + + backupManagerService.stopServiceForUser(mUserOneId); + + verify(mUserOneService).tearDownService(); + verify(mUserTwoService, never()).tearDownService(); + } + + /** Test that the service unregisters users when stopped. */ + @Test + public void testStopServiceForUser_forUnknownUser_doesNothing() throws Exception { + Trampoline backupManagerService = createService(); + backupManagerService.setBackupServiceActive(mUserOneId, true); + ShadowBinder.setCallingUid(Process.SYSTEM_UID); + + backupManagerService.stopServiceForUser(mUserOneId); + + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); + assertThat(serviceUsers.size()).isEqualTo(0); + } + + private Trampoline createService() { + return new Trampoline(mContext); + } + + private Trampoline createServiceAndRegisterUser( + int userId, UserBackupManagerService userBackupManagerService) { + Trampoline backupManagerService = createService(); + backupManagerService.setBackupServiceActive(userBackupManagerService.getUserId(), true); + backupManagerService.startServiceForUser(userId, userBackupManagerService); + return backupManagerService; + } +} diff --git a/services/robotests/src/com/android/server/testing/shadows/ShadowEnvironment.java b/services/robotests/src/com/android/server/testing/shadows/ShadowEnvironment.java new file mode 100644 index 000000000000..577b0824e775 --- /dev/null +++ b/services/robotests/src/com/android/server/testing/shadows/ShadowEnvironment.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 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.server.testing.shadows; + +import android.annotation.Nullable; +import android.os.Environment; + +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.Resetter; + +import java.io.File; +import java.nio.file.Path; + +/** Implementation mimics {@link org.robolectric.shadows.ShadowEnvironment}. */ +@Implements(Environment.class) +public class ShadowEnvironment extends org.robolectric.shadows.ShadowEnvironment { + @Nullable private static Path sDataDirectory; + + /** @see Environment#getDataDirectory() */ + @Implementation + public static File getDataDirectory() { + if (sDataDirectory == null) { + sDataDirectory = RuntimeEnvironment.getTempDirectory().create("data"); + } + return sDataDirectory.toFile(); + } + + /** Resets static state. */ + @Resetter + public static void reset() { + org.robolectric.shadows.ShadowEnvironment.reset(); + sDataDirectory = null; + } +} diff --git a/services/robotests/src/com/android/server/testing/shadows/ShadowUserManager.java b/services/robotests/src/com/android/server/testing/shadows/ShadowUserManager.java new file mode 100644 index 000000000000..c6ae1a1b6863 --- /dev/null +++ b/services/robotests/src/com/android/server/testing/shadows/ShadowUserManager.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2019 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.server.testing.shadows; + +import android.annotation.UserIdInt; +import android.os.UserManager; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +/** Shadow for {@link UserManager}. */ +@Implements(UserManager.class) +public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager { + /** @see UserManager#isUserUnlocked() */ + @Implementation + public boolean isUserUnlocked(@UserIdInt int userId) { + return false; + } +} diff --git a/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java b/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java index 8668a3cfa26c..1f9351352e2b 100644 --- a/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java +++ b/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java @@ -22,11 +22,11 @@ import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -39,6 +39,7 @@ import android.app.backup.IBackupManagerMonitor; import android.app.backup.IBackupObserver; import android.app.backup.IFullBackupRestoreObserver; import android.app.backup.ISelectBackupTransportCallback; +import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -71,7 +72,6 @@ import java.io.File; import java.io.FileDescriptor; import java.io.IOException; import java.io.PrintWriter; -import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -129,16 +129,17 @@ public class TrampolineTest { private TrampolineTestable mTrampoline; private File mTestDir; private File mSuppressFile; + private SparseArray<UserBackupManagerService> mUserServices; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mUserId = UserHandle.USER_SYSTEM; - SparseArray<UserBackupManagerService> serviceUsers = new SparseArray<>(); - serviceUsers.append(UserHandle.USER_SYSTEM, mUserBackupManagerService); - serviceUsers.append(NON_USER_SYSTEM, mUserBackupManagerService); - when(mBackupManagerServiceMock.getUserServices()).thenReturn(serviceUsers); + mUserServices = new SparseArray<>(); + mUserServices.append(UserHandle.USER_SYSTEM, mUserBackupManagerService); + mUserServices.append(NON_USER_SYSTEM, mUserBackupManagerService); + when(mBackupManagerServiceMock.getUserServices()).thenReturn(mUserServices); when(mUserManagerMock.getUserInfo(UserHandle.USER_SYSTEM)).thenReturn(mUserInfoMock); when(mUserManagerMock.getUserInfo(NON_USER_SYSTEM)).thenReturn(mUserInfoMock); @@ -159,7 +160,9 @@ public class TrampolineTest { setUpStateFilesForNonSystemUser(NON_USER_SYSTEM); setUpStateFilesForNonSystemUser(UNSTARTED_NON_USER_SYSTEM); - mTrampoline = new TrampolineTestable(mContextMock); + when(mContextMock.getSystemService(Context.JOB_SCHEDULER_SERVICE)) + .thenReturn(mock(JobScheduler.class)); + mTrampoline = new TrampolineTestable(mContextMock, mUserServices); } private void setUpStateFilesForNonSystemUser(int userId) { @@ -190,9 +193,8 @@ public class TrampolineTest { @Test public void testOnUnlockUser_forNonSystemUserWhenBackupsDisabled_doesNotStartUser() { - when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); TrampolineTestable.sBackupDisabled = true; - TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + TrampolineTestable trampoline = new TrampolineTestable(mContextMock, new SparseArray<>()); ConditionVariable unlocked = new ConditionVariable(false); trampoline.onUnlockUser(NON_USER_SYSTEM); @@ -204,9 +206,8 @@ public class TrampolineTest { @Test public void testOnUnlockUser_forSystemUserWhenBackupsDisabled_doesNotStartUser() { - when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); TrampolineTestable.sBackupDisabled = true; - TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + TrampolineTestable trampoline = new TrampolineTestable(mContextMock, new SparseArray<>()); ConditionVariable unlocked = new ConditionVariable(false); trampoline.onUnlockUser(UserHandle.USER_SYSTEM); @@ -218,9 +219,8 @@ public class TrampolineTest { @Test public void testOnUnlockUser_whenBackupNotActivated_doesNotStartUser() { - when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); TrampolineTestable.sBackupDisabled = false; - TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + TrampolineTestable trampoline = new TrampolineTestable(mContextMock, new SparseArray<>()); trampoline.setBackupServiceActive(NON_USER_SYSTEM, false); ConditionVariable unlocked = new ConditionVariable(false); @@ -229,16 +229,13 @@ public class TrampolineTest { trampoline.getBackupHandler().post(unlocked::open); unlocked.block(); assertNull(trampoline.getUserService(NON_USER_SYSTEM)); - //noinspection unchecked - verify(mBackupManagerServiceMock, never()).startServiceForUser( - eq(NON_USER_SYSTEM), any(Set.class)); } @Test public void testIsBackupServiceActive_forSystemUserWhenBackupDisabled_returnsTrue() throws Exception { TrampolineTestable.sBackupDisabled = true; - Trampoline trampoline = new TrampolineTestable(mContextMock); + Trampoline trampoline = new TrampolineTestable(mContextMock, mUserServices); trampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); @@ -248,7 +245,7 @@ public class TrampolineTest { public void testIsBackupServiceActive_forNonSystemUserWhenBackupDisabled_returnsTrue() throws Exception { TrampolineTestable.sBackupDisabled = true; - Trampoline trampoline = new TrampolineTestable(mContextMock); + Trampoline trampoline = new TrampolineTestable(mContextMock, mUserServices); trampoline.setBackupServiceActive(NON_USER_SYSTEM, true); assertFalse(trampoline.isBackupServiceActive(NON_USER_SYSTEM)); @@ -396,7 +393,7 @@ public class TrampolineTest { @Test public void setBackupServiceActive_backupDisabled_ignored() { TrampolineTestable.sBackupDisabled = true; - TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + TrampolineTestable trampoline = new TrampolineTestable(mContextMock, mUserServices); trampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); @@ -1045,7 +1042,7 @@ public class TrampolineTest { public void testGetUserForAncestralSerialNumber() { TrampolineTestable.sBackupDisabled = false; - Trampoline trampoline = new TrampolineTestable(mContextMock); + Trampoline trampoline = new TrampolineTestable(mContextMock, mUserServices); trampoline.getUserForAncestralSerialNumber(0L); verify(mBackupManagerServiceMock).getUserForAncestralSerialNumber(anyInt()); @@ -1053,7 +1050,7 @@ public class TrampolineTest { public void testGetUserForAncestralSerialNumber_whenDisabled() { TrampolineTestable.sBackupDisabled = true; - Trampoline trampoline = new TrampolineTestable(mContextMock); + Trampoline trampoline = new TrampolineTestable(mContextMock, mUserServices); trampoline.getUserForAncestralSerialNumber(0L); verify(mBackupManagerServiceMock, never()).getUserForAncestralSerialNumber(anyInt()); @@ -1069,8 +1066,8 @@ public class TrampolineTest { static SparseArray<File> sRememberActivatedFiles = new SparseArray<>(); static UserManager sUserManagerMock = null; - TrampolineTestable(Context context) { - super(context); + TrampolineTestable(Context context, SparseArray<UserBackupManagerService> userServices) { + super(context, userServices); mService = sBackupManagerServiceMock; } |