diff options
author | Stefano Tommasini <stefanot@google.com> | 2019-01-22 10:44:49 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-01-22 10:44:49 +0000 |
commit | 25b54058c0e3bb7e0630650f750092f7ccd2289f (patch) | |
tree | 3a7ae1bc7ca9d3171c7cea9e0f1acf070c6ccf6f | |
parent | 0fa45f1e50c130b3f9c620ac47f905abf26ad20c (diff) | |
parent | 72d03de8fa0ae7546122509ffefc84a11bee95e0 (diff) |
Merge "Make Trampoline support multi-user backup."
3 files changed, 330 insertions, 273 deletions
diff --git a/services/backup/java/com/android/server/backup/BackupManagerService.java b/services/backup/java/com/android/server/backup/BackupManagerService.java index ff378b373775..14322ecd76b9 100644 --- a/services/backup/java/com/android/server/backup/BackupManagerService.java +++ b/services/backup/java/com/android/server/backup/BackupManagerService.java @@ -124,6 +124,11 @@ public class BackupManagerService { */ @VisibleForTesting protected void startServiceForUser(int userId) { + if (mServiceUsers.get(userId) != null) { + Slog.i(TAG, "userId " + userId + " already started, so not starting again"); + return; + } + UserBackupManagerService userBackupManagerService = UserBackupManagerService.createAndInitializeService( userId, mContext, mTrampoline, mTransportWhitelist); @@ -155,7 +160,12 @@ public class BackupManagerService { } } - SparseArray<UserBackupManagerService> getServiceUsers() { + /** + * Returns a lst of users currently unlocked that have a + * {@link UserBackupManagerService} registered. + */ + @VisibleForTesting + public SparseArray<UserBackupManagerService> getServiceUsers() { return mServiceUsers; } diff --git a/services/backup/java/com/android/server/backup/Trampoline.java b/services/backup/java/com/android/server/backup/Trampoline.java index 4ca2545802c7..b9a6f3c08cc4 100644 --- a/services/backup/java/com/android/server/backup/Trampoline.java +++ b/services/backup/java/com/android/server/backup/Trampoline.java @@ -32,7 +32,6 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Binder; -import android.os.Environment; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; @@ -45,7 +44,6 @@ import android.os.UserHandle; import android.provider.Settings; import android.util.Slog; -import com.android.internal.annotations.GuardedBy; import com.android.internal.util.DumpUtils; import java.io.File; @@ -65,8 +63,8 @@ import java.io.PrintWriter; * following two ways: * * <ul> - * <li>Temporary - create the file {@link #BACKUP_SUPPRESS_FILENAME}, or - * <li>Permanent - set the system property {@link #BACKUP_DISABLE_PROPERTY} to true. + * <li>Temporary - create the file {@link #BACKUP_SUPPRESS_FILENAME}, or + * <li>Permanent - set the system property {@link #BACKUP_DISABLE_PROPERTY} to true. * </ul> * * Temporary disabling is controlled by {@link #setBackupServiceActive(int, boolean)} through @@ -91,10 +89,9 @@ public class Trampoline extends IBackupManager.Stub { private final Context mContext; - @GuardedBy("mStateLock") - private final File mSuppressFile; - private final boolean mGlobalDisable; + // Lock to write backup suppress files. + // TODD(b/121198006): remove this object and synchronized all methods on "this". private final Object mStateLock = new Object(); private volatile BackupManagerService mService; @@ -104,9 +101,6 @@ public class Trampoline extends IBackupManager.Stub { public Trampoline(Context context) { mContext = context; mGlobalDisable = isBackupDisabled(); - mSuppressFile = getSuppressFile(); - mSuppressFile.getParentFile().mkdirs(); - mHandlerThread = new HandlerThread(BACKUP_THREAD, Process.THREAD_PRIORITY_BACKGROUND); mHandlerThread.start(); mHandler = new Handler(mHandlerThread.getLooper()); @@ -132,11 +126,42 @@ public class Trampoline extends IBackupManager.Stub { return Binder.getCallingUid(); } - protected File getSuppressFile() { - return new File(new File(Environment.getDataDirectory(), "backup"), + protected File getSuppressFileForUser(int userId) { + return new File(UserBackupManagerFiles.getBaseStateDir(userId), BACKUP_SUPPRESS_FILENAME); } + protected void createBackupSuppressFileForUser(int userId) throws IOException { + synchronized (mStateLock) { + getSuppressFileForUser(userId).getParentFile().mkdirs(); + getSuppressFileForUser(userId).createNewFile(); + } + } + + private void deleteBackupSuppressFileForUser(int userId) { + if (!getSuppressFileForUser(userId).delete()) { + Slog.w(TAG, "Failed deleting backup suppressed file for user: " + userId); + } + } + + // A user is ready for a backup if it's unlocked and is not suppressed by a device + // admin (device owner or profile owner). + private boolean isUserReadyForBackup(int userId) { + return mService != null && mService.getServiceUsers().get(userId) != null + && !isBackupSuppressedForUser(userId); + } + + private boolean isBackupSuppressedForUser(int userId) { + // If backup is disabled for system user, it's disabled for all other users on device. + if (getSuppressFileForUser(UserHandle.USER_SYSTEM).exists()) { + return true; + } + if (userId != UserHandle.USER_SYSTEM) { + return getSuppressFileForUser(userId).exists(); + } + return false; + } + protected Context getContext() { return mContext; } @@ -162,12 +187,9 @@ public class Trampoline extends IBackupManager.Stub { Slog.i(TAG, "Backup service not supported"); return; } - synchronized (mStateLock) { - if (!mSuppressFile.exists()) { + if (mService == null) { mService = createBackupManagerService(); - } else { - Slog.i(TAG, "Backup service inactive"); } } Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); @@ -190,10 +212,11 @@ public class Trampoline extends IBackupManager.Stub { } private void startServiceForUser(int userId) { - BackupManagerService service = mService; - if (service != null) { + // 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 (mService != null && !isBackupSuppressedForUser(userId)) { Slog.i(TAG, "Starting service for user: " + userId); - service.startServiceForUser(userId); + mService.startServiceForUser(userId); } } @@ -209,10 +232,9 @@ public class Trampoline extends IBackupManager.Stub { postToHandler( () -> { - BackupManagerService service = mService; - if (service != null) { + if (mService != null) { Slog.i(TAG, "Stopping service for user: " + userId); - service.stopServiceForUser(userId); + mService.stopServiceForUser(userId); } }); } @@ -221,6 +243,8 @@ public class Trampoline extends IBackupManager.Stub { * Only privileged callers should be changing the backup state. This method only acts on {@link * UserHandle#USER_SYSTEM} and is a no-op if passed non-system users. Deactivating backup in the * system user also deactivates backup in all users. + * + * This call will only work if the calling {@code userID} is unlocked. */ public void setBackupServiceActive(int userId, boolean makeActive) { int caller = binderGetCallingUid(); @@ -246,16 +270,21 @@ public class Trampoline extends IBackupManager.Stub { synchronized (mStateLock) { Slog.i(TAG, "Making backup " + (makeActive ? "" : "in") + "active"); if (makeActive) { - mService = createBackupManagerService(); - mSuppressFile.delete(); + if (mService == null) { + mService = createBackupManagerService(); + } + deleteBackupSuppressFileForUser(userId); startServiceForUser(userId); } else { - mService = null; try { - mSuppressFile.createNewFile(); + //TODO(b/121198006): what if this throws an exception? + createBackupSuppressFileForUser(userId); } catch (IOException e) { Slog.e(TAG, "Unable to persist backup service inactivity"); } + //TODO(b/121198006): loop through active users that have work profile and + // stop them as well. + stopUser(userId); } } } @@ -271,20 +300,15 @@ public class Trampoline extends IBackupManager.Stub { */ @Override public boolean isBackupServiceActive(int userId) { - // TODO: http://b/22388012 - if (userId == UserHandle.USER_SYSTEM) { - synchronized (mStateLock) { - return mService != null; - } + synchronized (mStateLock) { + return isUserReadyForBackup(userId); } - return false; } @Override public void dataChangedForUser(int userId, String packageName) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.dataChanged(userId, packageName); + if (isUserReadyForBackup(userId)) { + mService.dataChanged(userId, packageName); } } @@ -296,18 +320,16 @@ public class Trampoline extends IBackupManager.Stub { @Override public void initializeTransportsForUser( int userId, String[] transportNames, IBackupObserver observer) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.initializeTransports(userId, transportNames, observer); + if (isUserReadyForBackup(userId)) { + mService.initializeTransports(userId, transportNames, observer); } } @Override public void clearBackupDataForUser(int userId, String transportName, String packageName) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.clearBackupData(userId, transportName, packageName); + if (isUserReadyForBackup(userId)) { + mService.clearBackupData(userId, transportName, packageName); } } @@ -320,9 +342,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void agentConnectedForUser(int userId, String packageName, IBinder agent) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.agentConnected(userId, packageName, agent); + if (isUserReadyForBackup(userId)) { + mService.agentConnected(userId, packageName, agent); } } @@ -333,9 +354,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void agentDisconnectedForUser(int userId, String packageName) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.agentDisconnected(userId, packageName); + if (isUserReadyForBackup(userId)) { + mService.agentDisconnected(userId, packageName); } } @@ -347,9 +367,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void restoreAtInstallForUser(int userId, String packageName, int token) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.restoreAtInstall(userId, packageName, token); + if (isUserReadyForBackup(userId)) { + mService.restoreAtInstall(userId, packageName, token); } } @@ -361,9 +380,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void setBackupEnabledForUser(@UserIdInt int userId, boolean isEnabled) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.setBackupEnabled(userId, isEnabled); + if (isUserReadyForBackup(userId)) { + mService.setBackupEnabled(userId, isEnabled); } } @@ -374,9 +392,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void setAutoRestoreForUser(int userId, boolean doAutoRestore) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.setAutoRestore(userId, doAutoRestore); + if (isUserReadyForBackup(userId)) { + mService.setAutoRestore(userId, doAutoRestore); } } @@ -387,8 +404,7 @@ public class Trampoline extends IBackupManager.Stub { @Override public boolean isBackupEnabledForUser(@UserIdInt int userId) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.isBackupEnabled(userId) : false; + return isUserReadyForBackup(userId) && mService.isBackupEnabled(userId); } @Override @@ -398,21 +414,20 @@ public class Trampoline extends IBackupManager.Stub { @Override public boolean setBackupPassword(String currentPw, String newPw) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.setBackupPassword(currentPw, newPw) : false; + int userId = binderGetCallingUserId(); + return (isUserReadyForBackup(userId)) && mService.setBackupPassword(currentPw, newPw); } @Override public boolean hasBackupPassword() throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.hasBackupPassword() : false; + int userId = binderGetCallingUserId(); + return (isUserReadyForBackup(userId)) && mService.hasBackupPassword(); } @Override public void backupNowForUser(@UserIdInt int userId) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.backupNow(userId); + if (isUserReadyForBackup(userId)) { + mService.backupNow(userId); } } @@ -425,9 +440,8 @@ public class Trampoline extends IBackupManager.Stub { boolean includeApks, boolean includeObbs, boolean includeShared, boolean doWidgets, boolean allApps, boolean allIncludesSystem, boolean doCompress, boolean doKeyValue, String[] packageNames) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.adbBackup(userId, fd, includeApks, includeObbs, includeShared, doWidgets, + if (isUserReadyForBackup(userId)) { + mService.adbBackup(userId, fd, includeApks, includeObbs, includeShared, doWidgets, allApps, allIncludesSystem, doCompress, doKeyValue, packageNames); } } @@ -435,17 +449,15 @@ public class Trampoline extends IBackupManager.Stub { @Override public void fullTransportBackupForUser(int userId, String[] packageNames) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.fullTransportBackup(userId, packageNames); + if (isUserReadyForBackup(userId)) { + mService.fullTransportBackup(userId, packageNames); } } @Override public void adbRestore(@UserIdInt int userId, ParcelFileDescriptor fd) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.adbRestore(userId, fd); + if (isUserReadyForBackup(userId)) { + mService.adbRestore(userId, fd); } } @@ -458,9 +470,8 @@ public class Trampoline extends IBackupManager.Stub { String encryptionPassword, IFullBackupRestoreObserver observer) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.acknowledgeAdbBackupOrRestore(userId, token, allow, + if (isUserReadyForBackup(userId)) { + mService.acknowledgeAdbBackupOrRestore(userId, token, allow, curPassword, encryptionPassword, observer); } } @@ -468,8 +479,7 @@ public class Trampoline extends IBackupManager.Stub { @Override public void acknowledgeFullBackupOrRestore(int token, boolean allow, String curPassword, String encryptionPassword, IFullBackupRestoreObserver observer) - throws RemoteException { - BackupManagerService svc = mService; + throws RemoteException { acknowledgeFullBackupOrRestoreForUser( binderGetCallingUserId(), token, allow, curPassword, encryptionPassword, observer); } @@ -477,8 +487,7 @@ public class Trampoline extends IBackupManager.Stub { @Override public String getCurrentTransportForUser(int userId) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.getCurrentTransport(userId) : null; + return (isUserReadyForBackup(userId)) ? mService.getCurrentTransport(userId) : null; } @Override @@ -493,14 +502,13 @@ public class Trampoline extends IBackupManager.Stub { @Override @Nullable public ComponentName getCurrentTransportComponentForUser(int userId) { - BackupManagerService svc = mService; - return (svc != null) ? svc.getCurrentTransportComponent(userId) : null; + return (isUserReadyForBackup(userId)) ? mService.getCurrentTransportComponent(userId) + : null; } @Override public String[] listAllTransportsForUser(int userId) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.listAllTransports(userId) : null; + return (isUserReadyForBackup(userId)) ? mService.listAllTransports(userId) : null; } @Override @@ -510,14 +518,14 @@ public class Trampoline extends IBackupManager.Stub { @Override public ComponentName[] listAllTransportComponentsForUser(int userId) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.listAllTransportComponents(userId) : null; + return (isUserReadyForBackup(userId)) ? mService.listAllTransportComponents(userId) + : null; } @Override public String[] getTransportWhitelist() { - BackupManagerService svc = mService; - return (svc != null) ? svc.getTransportWhitelist() : null; + int userId = binderGetCallingUserId(); + return (isUserReadyForBackup(userId)) ? mService.getTransportWhitelist() : null; } @Override @@ -529,9 +537,9 @@ public class Trampoline extends IBackupManager.Stub { String currentDestinationString, @Nullable Intent dataManagementIntent, String dataManagementLabel) { - BackupManagerService svc = mService; - if (svc != null) { - svc.updateTransportAttributes( + + if (isUserReadyForBackup(userId)) { + mService.updateTransportAttributes( userId, transportComponent, name, @@ -545,8 +553,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public String selectBackupTransportForUser(int userId, String transport) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.selectBackupTransport(userId, transport) : null; + return (isUserReadyForBackup(userId)) ? mService.selectBackupTransport(userId, transport) + : null; } @Override @@ -557,9 +565,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void selectBackupTransportAsyncForUser(int userId, ComponentName transport, ISelectBackupTransportCallback listener) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.selectBackupTransportAsync(userId, transport, listener); + if (isUserReadyForBackup(userId)) { + mService.selectBackupTransportAsync(userId, transport, listener); } else { if (listener != null) { try { @@ -574,8 +581,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public Intent getConfigurationIntentForUser(int userId, String transport) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.getConfigurationIntent(userId, transport) : null; + return isUserReadyForBackup(userId) ? mService.getConfigurationIntent(userId, transport) + : null; } @Override @@ -586,8 +593,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public String getDestinationStringForUser(int userId, String transport) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.getDestinationString(userId, transport) : null; + return isUserReadyForBackup(userId) ? mService.getDestinationString(userId, transport) + : null; } @Override @@ -598,8 +605,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public Intent getDataManagementIntentForUser(int userId, String transport) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.getDataManagementIntent(userId, transport) : null; + return isUserReadyForBackup(userId) ? mService.getDataManagementIntent(userId, transport) + : null; } @Override @@ -611,8 +618,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public String getDataManagementLabelForUser(int userId, String transport) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.getDataManagementLabel(userId, transport) : null; + return isUserReadyForBackup(userId) ? mService.getDataManagementLabel(userId, transport) + : null; } @Override @@ -624,44 +631,43 @@ public class Trampoline extends IBackupManager.Stub { @Override public IRestoreSession beginRestoreSessionForUser( int userId, String packageName, String transportID) throws RemoteException { - BackupManagerService svc = mService; - return (svc != null) ? svc.beginRestoreSession(userId, packageName, transportID) : null; + return isUserReadyForBackup(userId) ? mService.beginRestoreSession(userId, packageName, + transportID) : null; } @Override public void opComplete(int token, long result) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.opComplete(binderGetCallingUserId(), token, result); + int userId = binderGetCallingUserId(); + if (isUserReadyForBackup(userId)) { + mService.opComplete(binderGetCallingUserId(), token, result); } } @Override public long getAvailableRestoreTokenForUser(int userId, String packageName) { - BackupManagerService svc = mService; - return (svc != null) ? svc.getAvailableRestoreToken(userId, packageName) : 0; + return isUserReadyForBackup(userId) ? mService.getAvailableRestoreToken(userId, + packageName) : 0; } @Override public boolean isAppEligibleForBackupForUser(int userId, String packageName) { - BackupManagerService svc = mService; - return (svc != null) ? svc.isAppEligibleForBackup(userId, packageName) : false; + return isUserReadyForBackup(userId) && mService.isAppEligibleForBackup(userId, + packageName); } @Override public String[] filterAppsEligibleForBackupForUser(int userId, String[] packages) { - BackupManagerService svc = mService; - return (svc != null) ? svc.filterAppsEligibleForBackup(userId, packages) : null; + return isUserReadyForBackup(userId) ? mService.filterAppsEligibleForBackup(userId, + packages) : null; } @Override public int requestBackupForUser(@UserIdInt int userId, String[] packages, IBackupObserver observer, IBackupManagerMonitor monitor, int flags) throws RemoteException { - BackupManagerService svc = mService; - if (svc == null) { + if (!isUserReadyForBackup(userId)) { return BackupManager.ERROR_BACKUP_NOT_ALLOWED; } - return svc.requestBackup(userId, packages, observer, monitor, flags); + return mService.requestBackup(userId, packages, observer, monitor, flags); } @Override @@ -673,9 +679,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public void cancelBackupsForUser(@UserIdInt int userId) throws RemoteException { - BackupManagerService svc = mService; - if (svc != null) { - svc.cancelBackups(userId); + if (isUserReadyForBackup(userId)) { + mService.cancelBackups(userId); } } @@ -687,10 +692,9 @@ public class Trampoline extends IBackupManager.Stub { @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; - - BackupManagerService svc = mService; - if (svc != null) { - svc.dump(fd, pw, args); + int userId = binderGetCallingUserId(); + if (isUserReadyForBackup(userId)) { + mService.dump(fd, pw, args); } else { pw.println("Inactive"); } @@ -699,14 +703,12 @@ public class Trampoline extends IBackupManager.Stub { // Full backup/restore entry points - non-Binder; called directly // by the full-backup scheduled job /* package */ boolean beginFullBackup(@UserIdInt int userId, FullBackupJob scheduledJob) { - BackupManagerService svc = mService; - return (svc != null) ? svc.beginFullBackup(userId, scheduledJob) : false; + return (isUserReadyForBackup(userId)) && mService.beginFullBackup(userId, scheduledJob); } /* package */ void endFullBackup(@UserIdInt int userId) { - BackupManagerService svc = mService; - if (svc != null) { - svc.endFullBackup(userId); + if (isUserReadyForBackup(userId)) { + mService.endFullBackup(userId); } } } 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 6a9a12133e70..ac4a5fe90c06 100644 --- a/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java +++ b/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java @@ -25,7 +25,6 @@ import static junit.framework.Assert.fail; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -48,6 +47,7 @@ import android.os.UserHandle; import android.platform.test.annotations.Presubmit; import android.provider.Settings; import android.test.mock.MockContentResolver; +import android.util.SparseArray; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; @@ -90,17 +90,30 @@ public class TrampolineTest { }; private static final int NON_USER_SYSTEM = UserHandle.USER_SYSTEM + 1; - @UserIdInt private int mUserId; - @Mock private BackupManagerService mBackupManagerServiceMock; - @Mock private Context mContextMock; - @Mock private File mSuppressFileMock; - @Mock private File mSuppressFileParentMock; - @Mock private IBinder mAgentMock; - @Mock private ParcelFileDescriptor mParcelFileDescriptorMock; - @Mock private IFullBackupRestoreObserver mFullBackupRestoreObserverMock; - @Mock private IBackupObserver mBackupObserverMock; - @Mock private IBackupManagerMonitor mBackupManagerMonitorMock; - @Mock private PrintWriter mPrintWriterMock; + @UserIdInt + private int mUserId; + @Mock + private BackupManagerService mBackupManagerServiceMock; + @Mock + private UserBackupManagerService mUserBackupManagerService; + @Mock + private Context mContextMock; + @Mock + private File mSuppressFileMock; + @Mock + private File mSuppressFileParentMock; + @Mock + private IBinder mAgentMock; + @Mock + private ParcelFileDescriptor mParcelFileDescriptorMock; + @Mock + private IFullBackupRestoreObserver mFullBackupRestoreObserverMock; + @Mock + private IBackupObserver mBackupObserverMock; + @Mock + private IBackupManagerMonitor mBackupManagerMonitorMock; + @Mock + private PrintWriter mPrintWriterMock; private FileDescriptor mFileDescriptorStub = new FileDescriptor(); @@ -110,16 +123,20 @@ public class TrampolineTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); + mUserId = NON_USER_SYSTEM; + + SparseArray<UserBackupManagerService> serviceUsers = new SparseArray<>(); + serviceUsers.append(UserHandle.SYSTEM.getIdentifier(), mUserBackupManagerService); + serviceUsers.append(NON_USER_SYSTEM, mUserBackupManagerService); + when(mBackupManagerServiceMock.getServiceUsers()).thenReturn(serviceUsers); TrampolineTestable.sBackupManagerServiceMock = mBackupManagerServiceMock; - TrampolineTestable.sSuppressFile = mSuppressFileMock; TrampolineTestable.sCallingUserId = UserHandle.USER_SYSTEM; TrampolineTestable.sCallingUid = Process.SYSTEM_UID; TrampolineTestable.sBackupDisabled = false; when(mSuppressFileMock.getParentFile()).thenReturn(mSuppressFileParentMock); - mUserId = NON_USER_SYSTEM; mTrampoline = new TrampolineTestable(mContextMock); mContentResolver = new MockContentResolver(); @@ -128,11 +145,6 @@ public class TrampolineTest { } @Test - public void constructor_createsSuppressFileDirectory() { - verify(mSuppressFileParentMock).mkdirs(); - } - - @Test public void unlockUser_whenMultiUserSettingDisabled_callsBackupManagerServiceForSystemUser() { Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 0); mTrampoline.initializeService(); @@ -147,9 +159,9 @@ public class TrampolineTest { Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 0); mTrampoline.initializeService(); - mTrampoline.unlockUser(10); + mTrampoline.unlockUser(NON_USER_SYSTEM); - verify(mBackupManagerServiceMock, never()).startServiceForUser(10); + verify(mBackupManagerServiceMock, never()).startServiceForUser(NON_USER_SYSTEM); } @Test @@ -157,9 +169,9 @@ public class TrampolineTest { Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 1); mTrampoline.initializeService(); - mTrampoline.unlockUser(10); + mTrampoline.unlockUser(NON_USER_SYSTEM); - verify(mBackupManagerServiceMock).startServiceForUser(10); + verify(mBackupManagerServiceMock).startServiceForUser(NON_USER_SYSTEM); } @Test @@ -177,19 +189,20 @@ public class TrampolineTest { Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 0); mTrampoline.initializeService(); - mTrampoline.stopUser(10); + mTrampoline.stopUser(NON_USER_SYSTEM); - verify(mBackupManagerServiceMock, never()).stopServiceForUser(10); + verify(mBackupManagerServiceMock, never()).stopServiceForUser(NON_USER_SYSTEM); } @Test public void stopUser_whenMultiUserSettingEnabled_callsBackupManagerServiceForNonSystemUser() { Settings.Global.putInt(mContentResolver, Settings.Global.BACKUP_MULTI_USER_ENABLED, 1); + mTrampoline.initializeService(); - mTrampoline.stopUser(10); + mTrampoline.stopUser(NON_USER_SYSTEM); - verify(mBackupManagerServiceMock).stopServiceForUser(10); + verify(mBackupManagerServiceMock).stopServiceForUser(NON_USER_SYSTEM); } @Test @@ -211,9 +224,10 @@ public class TrampolineTest { // Verify that BackupManagerService is not initialized if suppress file exists. @Test - public void initializeService_suppressFileExists_nonInitialized() { - when(mSuppressFileMock.exists()).thenReturn(true); + public void initializeService_suppressFileExists_nonInitialized() throws Exception { TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + trampoline.createBackupSuppressFileForUser(UserHandle.USER_SYSTEM); + trampoline.initializeService(); @@ -233,6 +247,14 @@ public class TrampolineTest { } @Test + public void isBackupServiceActive_forNonSysUser_whenSysUserIsDeactivated_returnsFalse() { + mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); + mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); + + assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM)); + } + + @Test public void setBackupServiceActive_callerSystemUid_serviceCreated() { TrampolineTestable.sCallingUid = Process.SYSTEM_UID; @@ -292,11 +314,18 @@ public class TrampolineTest { } @Test + public void setBackupServiceActive_makeNonActive_alreadyNonActive_ignored() { + mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); + mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); + + assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); + } + + @Test public void setBackupServiceActive_makeActive_serviceCreatedAndSuppressFileDeleted() { mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); - verify(mSuppressFileMock).delete(); } @Test @@ -308,40 +337,25 @@ public class TrampolineTest { mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); - verify(mSuppressFileMock).createNewFile(); } @Test - public void - setBackupServiceActive_makeNonActive_serviceDeletedAndSuppressFileCreated_ioExceptionHandled() - throws IOException { - when(mSuppressFileMock.createNewFile()).thenThrow(new IOException()); + public void setBackupActive_nonSystemUser_disabledForSystemUser_ignored() { mTrampoline.initializeService(); - assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); - - mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); - - assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); - verify(mSuppressFileMock).createNewFile(); - } - - @Test - public void setBackupServiceActive_makeNonActive_alreadyNonActive_ignored() throws IOException { - reset(mSuppressFileMock); - mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); + mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); - verifyNoMoreInteractions(mSuppressFileMock); + assertFalse(mTrampoline.isBackupServiceActive(NON_USER_SYSTEM)); } @Test - public void dataChanged_calledBeforeInitialize_ignored() throws RemoteException { + public void dataChanged_calledBeforeInitialize_ignored() throws Exception { mTrampoline.dataChanged(PACKAGE_NAME); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void dataChangedForUser_forwarded() throws RemoteException { + public void dataChangedForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.dataChangedForUser(mUserId, PACKAGE_NAME); @@ -350,7 +364,7 @@ public class TrampolineTest { } @Test - public void dataChanged_forwarded() throws RemoteException { + public void dataChanged_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -360,13 +374,13 @@ public class TrampolineTest { } @Test - public void clearBackupData_calledBeforeInitialize_ignored() throws RemoteException { + public void clearBackupData_calledBeforeInitialize_ignored() throws Exception { mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void clearBackupDataForUser_forwarded() throws RemoteException { + public void clearBackupDataForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.clearBackupDataForUser(mUserId, TRANSPORT_NAME, PACKAGE_NAME); @@ -375,7 +389,7 @@ public class TrampolineTest { } @Test - public void clearBackupData_forwarded() throws RemoteException { + public void clearBackupData_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -385,13 +399,13 @@ public class TrampolineTest { } @Test - public void agentConnected_calledBeforeInitialize_ignored() throws RemoteException { + public void agentConnected_calledBeforeInitialize_ignored() throws Exception { mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void agentConnectedForUser_forwarded() throws RemoteException { + public void agentConnectedForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.agentConnectedForUser(mUserId, PACKAGE_NAME, mAgentMock); @@ -400,7 +414,7 @@ public class TrampolineTest { } @Test - public void agentConnected_forwarded() throws RemoteException { + public void agentConnected_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -410,13 +424,13 @@ public class TrampolineTest { } @Test - public void agentDisconnected_calledBeforeInitialize_ignored() throws RemoteException { + public void agentDisconnected_calledBeforeInitialize_ignored() throws Exception { mTrampoline.agentDisconnected(PACKAGE_NAME); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void agentDisconnectedForUser_forwarded() throws RemoteException { + public void agentDisconnectedForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.agentDisconnectedForUser(mUserId, PACKAGE_NAME); @@ -425,7 +439,7 @@ public class TrampolineTest { } @Test - public void agentDisconnected_forwarded() throws RemoteException { + public void agentDisconnected_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -435,13 +449,13 @@ public class TrampolineTest { } @Test - public void restoreAtInstall_calledBeforeInitialize_ignored() throws RemoteException { + public void restoreAtInstall_calledBeforeInitialize_ignored() throws Exception { mTrampoline.restoreAtInstall(PACKAGE_NAME, 123); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void restoreAtInstallForUser_forwarded() throws RemoteException { + public void restoreAtInstallForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.restoreAtInstallForUser(mUserId, PACKAGE_NAME, 123); @@ -450,7 +464,7 @@ public class TrampolineTest { } @Test - public void restoreAtInstall_forwarded() throws RemoteException { + public void restoreAtInstall_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -460,13 +474,13 @@ public class TrampolineTest { } @Test - public void setBackupEnabled_calledBeforeInitialize_ignored() throws RemoteException { + public void setBackupEnabled_calledBeforeInitialize_ignored() throws Exception { mTrampoline.setBackupEnabled(true); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void setBackupEnabledForUser_forwarded() throws RemoteException { + public void setBackupEnabledForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.setBackupEnabledForUser(mUserId, true); @@ -475,7 +489,7 @@ public class TrampolineTest { } @Test - public void setBackupEnabled_forwardedToCallingUserId() throws RemoteException { + public void setBackupEnabled_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -485,13 +499,13 @@ public class TrampolineTest { } @Test - public void setAutoRestore_calledBeforeInitialize_ignored() throws RemoteException { + public void setAutoRestore_calledBeforeInitialize_ignored() throws Exception { mTrampoline.setAutoRestore(true); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void setAutoRestoreForUser_forwarded() throws RemoteException { + public void setAutoRestoreForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.setAutoRestoreForUser(mUserId, true); @@ -500,7 +514,7 @@ public class TrampolineTest { } @Test - public void setAutoRestore_forwarded() throws RemoteException { + public void setAutoRestore_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -510,13 +524,13 @@ public class TrampolineTest { } @Test - public void isBackupEnabled_calledBeforeInitialize_ignored() throws RemoteException { + public void isBackupEnabled_calledBeforeInitialize_ignored() throws Exception { assertFalse(mTrampoline.isBackupEnabled()); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void isBackupEnabledForUser_forwarded() throws RemoteException { + public void isBackupEnabledForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.isBackupEnabledForUser(mUserId); @@ -525,7 +539,7 @@ public class TrampolineTest { } @Test - public void isBackupEnabled_forwardedToCallingUserId() throws RemoteException { + public void isBackupEnabled_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -535,39 +549,39 @@ public class TrampolineTest { } @Test - public void setBackupPassword_calledBeforeInitialize_ignored() throws RemoteException { + public void setBackupPassword_calledBeforeInitialize_ignored() throws Exception { mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void setBackupPassword_forwarded() throws RemoteException { + public void setBackupPassword_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); verify(mBackupManagerServiceMock).setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); } @Test - public void hasBackupPassword_calledBeforeInitialize_ignored() throws RemoteException { + public void hasBackupPassword_calledBeforeInitialize_ignored() throws Exception { assertFalse(mTrampoline.hasBackupPassword()); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void hasBackupPassword_forwarded() throws RemoteException { + public void hasBackupPassword_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.hasBackupPassword(); verify(mBackupManagerServiceMock).hasBackupPassword(); } @Test - public void backupNow_calledBeforeInitialize_ignored() throws RemoteException { + public void backupNow_calledBeforeInitialize_ignored() throws Exception { mTrampoline.backupNow(); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void backupNowForUser_forwarded() throws RemoteException { + public void backupNowForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.backupNowForUser(mUserId); @@ -576,7 +590,7 @@ public class TrampolineTest { } @Test - public void backupNow_forwardedToCallingUserId() throws RemoteException { + public void backupNow_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -586,7 +600,7 @@ public class TrampolineTest { } @Test - public void adbBackup_calledBeforeInitialize_ignored() throws RemoteException { + public void adbBackup_calledBeforeInitialize_ignored() throws Exception { mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true, true, true, true, true, true, true, PACKAGE_NAMES); @@ -594,7 +608,7 @@ public class TrampolineTest { } @Test - public void adbBackup_forwarded() throws RemoteException { + public void adbBackup_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true, true, true, true, true, true, true, @@ -604,13 +618,13 @@ public class TrampolineTest { } @Test - public void fullTransportBackup_calledBeforeInitialize_ignored() throws RemoteException { + public void fullTransportBackup_calledBeforeInitialize_ignored() throws Exception { mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void fullTransportBackupForUser_forwarded() throws RemoteException { + public void fullTransportBackupForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES); @@ -619,13 +633,13 @@ public class TrampolineTest { } @Test - public void adbRestore_calledBeforeInitialize_ignored() throws RemoteException { + public void adbRestore_calledBeforeInitialize_ignored() throws Exception { mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void adbRestore_forwarded() throws RemoteException { + public void adbRestore_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock); verify(mBackupManagerServiceMock).adbRestore(mUserId, mParcelFileDescriptorMock); @@ -633,14 +647,14 @@ public class TrampolineTest { @Test public void acknowledgeFullBackupOrRestore_calledBeforeInitialize_ignored() - throws RemoteException { + throws Exception { mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD, mFullBackupRestoreObserverMock); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void acknowledgeFullBackupOrRestoreForUser_forwarded() throws RemoteException { + public void acknowledgeFullBackupOrRestoreForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.acknowledgeFullBackupOrRestoreForUser( @@ -662,7 +676,7 @@ public class TrampolineTest { } @Test - public void acknowledgeFullBackupOrRestore_forwarded() throws RemoteException { + public void acknowledgeFullBackupOrRestore_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -680,13 +694,13 @@ public class TrampolineTest { } @Test - public void getCurrentTransport_calledBeforeInitialize_ignored() throws RemoteException { + public void getCurrentTransport_calledBeforeInitialize_ignored() throws Exception { assertNull(mTrampoline.getCurrentTransport()); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getCurrentTransportForUser_forwarded() throws RemoteException { + public void getCurrentTransportForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.getCurrentTransport(mUserId)).thenReturn(TRANSPORT_NAME); mTrampoline.initializeService(); @@ -695,7 +709,7 @@ public class TrampolineTest { } @Test - public void getCurrentTransport_forwarded() throws RemoteException { + public void getCurrentTransport_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.getCurrentTransport(mUserId)).thenReturn(TRANSPORT_NAME); mTrampoline.initializeService(); @@ -705,13 +719,13 @@ public class TrampolineTest { } @Test - public void listAllTransports_calledBeforeInitialize_ignored() throws RemoteException { + public void listAllTransports_calledBeforeInitialize_ignored() throws Exception { assertNull(mTrampoline.listAllTransports()); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void listAllTransportsForUser_forwarded() throws RemoteException { + public void listAllTransportsForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.listAllTransports(mUserId)).thenReturn(TRANSPORTS); mTrampoline.initializeService(); @@ -721,7 +735,7 @@ public class TrampolineTest { @Test - public void listAllTransports_forwarded() throws RemoteException { + public void listAllTransports_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.listAllTransports(mUserId)).thenReturn(TRANSPORTS); mTrampoline.initializeService(); @@ -732,13 +746,13 @@ public class TrampolineTest { @Test public void listAllTransportComponentsForUser_calledBeforeInitialize_ignored() - throws RemoteException { + throws Exception { assertNull(mTrampoline.listAllTransportComponentsForUser(mUserId)); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void listAllTransportComponentsForUser_forwarded() throws RemoteException { + public void listAllTransportComponentsForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.listAllTransportComponents(mUserId)).thenReturn( TRANSPORT_COMPONENTS); mTrampoline.initializeService(); @@ -748,13 +762,13 @@ public class TrampolineTest { } @Test - public void getTransportWhitelist_calledBeforeInitialize_ignored() throws RemoteException { + public void getTransportWhitelist_calledBeforeInitialize_ignored() throws Exception { assertNull(mTrampoline.getTransportWhitelist()); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getTransportWhitelist_forwarded() throws RemoteException { + public void getTransportWhitelist_forwarded() { when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS); mTrampoline.initializeService(); @@ -763,8 +777,7 @@ public class TrampolineTest { } @Test - public void updateTransportAttributesForUser_calledBeforeInitialize_ignored() - throws RemoteException { + public void updateTransportAttributesForUser_calledBeforeInitialize_ignored() { mTrampoline.updateTransportAttributesForUser( mUserId, TRANSPORT_COMPONENT_NAME, @@ -778,7 +791,7 @@ public class TrampolineTest { } @Test - public void updateTransportAttributesForUser_forwarded() throws RemoteException { + public void updateTransportAttributesForUser_forwarded() { when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS); mTrampoline.initializeService(); @@ -809,7 +822,7 @@ public class TrampolineTest { } @Test - public void selectBackupTransportForUser_forwarded() throws RemoteException { + public void selectBackupTransportForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.selectBackupTransportForUser(mUserId, TRANSPORT_NAME); @@ -818,7 +831,7 @@ public class TrampolineTest { } @Test - public void selectBackupTransport_forwarded() throws RemoteException { + public void selectBackupTransport_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -895,7 +908,7 @@ public class TrampolineTest { } @Test - public void selectBackupTransportAsyncForUser_forwarded() throws RemoteException { + public void selectBackupTransportAsyncForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, null); @@ -905,13 +918,13 @@ public class TrampolineTest { } @Test - public void getConfigurationIntent_calledBeforeInitialize_ignored() throws RemoteException { + public void getConfigurationIntent_calledBeforeInitialize_ignored() throws Exception { mTrampoline.getConfigurationIntent(TRANSPORT_NAME); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getConfigurationIntentForUser_forwarded() throws RemoteException { + public void getConfigurationIntentForUser_forwarded() throws Exception { Intent configurationIntentStub = new Intent(); when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn( configurationIntentStub); @@ -924,7 +937,7 @@ public class TrampolineTest { } @Test - public void getConfigurationIntent_forwarded() throws RemoteException { + public void getConfigurationIntent_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; Intent configurationIntentStub = new Intent(); when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn( @@ -936,13 +949,13 @@ public class TrampolineTest { } @Test - public void getDestinationString_calledBeforeInitialize_ignored() throws RemoteException { + public void getDestinationString_calledBeforeInitialize_ignored() throws Exception { assertNull(mTrampoline.getDestinationString(TRANSPORT_NAME)); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getDestinationStringForUser_forwarded() throws RemoteException { + public void getDestinationStringForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn( DESTINATION_STRING); mTrampoline.initializeService(); @@ -954,7 +967,7 @@ public class TrampolineTest { } @Test - public void getDestinationString_forwarded() throws RemoteException { + public void getDestinationString_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn( DESTINATION_STRING); @@ -965,13 +978,13 @@ public class TrampolineTest { } @Test - public void getDataManagementIntent_calledBeforeInitialize_ignored() throws RemoteException { + public void getDataManagementIntent_calledBeforeInitialize_ignored() throws Exception { assertNull(mTrampoline.getDataManagementIntent(TRANSPORT_NAME)); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getDataManagementIntentForUser_forwarded() throws RemoteException { + public void getDataManagementIntentForUser_forwarded() throws Exception { Intent dataManagementIntent = new Intent(); when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn( dataManagementIntent); @@ -984,7 +997,7 @@ public class TrampolineTest { } @Test - public void getDataManagementIntent_forwarded() throws RemoteException { + public void getDataManagementIntent_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; Intent dataManagementIntent = new Intent(); when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn( @@ -996,13 +1009,13 @@ public class TrampolineTest { } @Test - public void getDataManagementLabel_calledBeforeInitialize_ignored() throws RemoteException { + public void getDataManagementLabel_calledBeforeInitialize_ignored() throws Exception { assertNull(mTrampoline.getDataManagementLabel(TRANSPORT_NAME)); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getDataManagementLabelForUser_forwarded() throws RemoteException { + public void getDataManagementLabelForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.getDataManagementLabel(mUserId, TRANSPORT_NAME)).thenReturn( DATA_MANAGEMENT_LABEL); mTrampoline.initializeService(); @@ -1014,7 +1027,7 @@ public class TrampolineTest { } @Test - public void getDataManagementLabel_forwarded() throws RemoteException { + public void getDataManagementLabel_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.getDataManagementLabel(mUserId, TRANSPORT_NAME)).thenReturn( DATA_MANAGEMENT_LABEL); @@ -1025,13 +1038,13 @@ public class TrampolineTest { } @Test - public void beginRestoreSession_calledBeforeInitialize_ignored() throws RemoteException { + public void beginRestoreSession_calledBeforeInitialize_ignored() throws Exception { mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void beginRestoreSessionForUser_forwarded() throws RemoteException { + public void beginRestoreSessionForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME); @@ -1041,13 +1054,13 @@ public class TrampolineTest { } @Test - public void opComplete_calledBeforeInitialize_ignored() throws RemoteException { + public void opComplete_calledBeforeInitialize_ignored() throws Exception { mTrampoline.opComplete(1, 2); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void opComplete_forwarded() throws RemoteException { + public void opComplete_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -1057,14 +1070,13 @@ public class TrampolineTest { } @Test - public void getAvailableRestoreTokenForUser_calledBeforeInitialize_ignored() - throws RemoteException { + public void getAvailableRestoreTokenForUser_calledBeforeInitialize_ignored() { assertEquals(0, mTrampoline.getAvailableRestoreTokenForUser(mUserId, PACKAGE_NAME)); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void getAvailableRestoreTokenForUser_forwarded() throws RemoteException { + public void getAvailableRestoreTokenForUser_forwarded() { when(mBackupManagerServiceMock.getAvailableRestoreToken(mUserId, PACKAGE_NAME)) .thenReturn(123L); mTrampoline.initializeService(); @@ -1074,14 +1086,13 @@ public class TrampolineTest { } @Test - public void isAppEligibleForBackupForUser_calledBeforeInitialize_ignored() - throws RemoteException { + public void isAppEligibleForBackupForUser_calledBeforeInitialize_ignored() { assertFalse(mTrampoline.isAppEligibleForBackupForUser(mUserId, PACKAGE_NAME)); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void isAppEligibleForBackupForUser_forwarded() throws RemoteException { + public void isAppEligibleForBackupForUser_forwarded() { when(mBackupManagerServiceMock.isAppEligibleForBackup(mUserId, PACKAGE_NAME)) .thenReturn(true); mTrampoline.initializeService(); @@ -1098,7 +1109,7 @@ public class TrampolineTest { } @Test - public void requestBackupForUser_forwarded() throws RemoteException { + public void requestBackupForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456); mTrampoline.initializeService(); @@ -1110,7 +1121,7 @@ public class TrampolineTest { } @Test - public void requestBackup_forwardedToCallingUserId() throws RemoteException { + public void requestBackup_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.requestBackup(NON_USER_SYSTEM, PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456); @@ -1123,13 +1134,13 @@ public class TrampolineTest { } @Test - public void cancelBackups_calledBeforeInitialize_ignored() throws RemoteException { + public void cancelBackups_calledBeforeInitialize_ignored() throws Exception { mTrampoline.cancelBackups(); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void cancelBackupsForUser_forwarded() throws RemoteException { + public void cancelBackupsForUser_forwarded() throws Exception { mTrampoline.initializeService(); mTrampoline.cancelBackupsForUser(mUserId); @@ -1138,7 +1149,7 @@ public class TrampolineTest { } @Test - public void cancelBackups_forwardedToCallingUserId() throws RemoteException { + public void cancelBackups_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; mTrampoline.initializeService(); @@ -1148,13 +1159,13 @@ public class TrampolineTest { } @Test - public void beginFullBackup_calledBeforeInitialize_ignored() throws RemoteException { + public void beginFullBackup_calledBeforeInitialize_ignored() throws Exception { mTrampoline.beginFullBackup(mUserId, new FullBackupJob()); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void beginFullBackup_forwarded() throws RemoteException { + public void beginFullBackup_forwarded() throws Exception { FullBackupJob fullBackupJob = new FullBackupJob(); when(mBackupManagerServiceMock.beginFullBackup(mUserId, fullBackupJob)).thenReturn(true); @@ -1164,20 +1175,20 @@ public class TrampolineTest { } @Test - public void endFullBackup_calledBeforeInitialize_ignored() throws RemoteException { + public void endFullBackup_calledBeforeInitialize_ignored() { mTrampoline.endFullBackup(mUserId); verifyNoMoreInteractions(mBackupManagerServiceMock); } @Test - public void endFullBackup_forwarded() throws RemoteException { + public void endFullBackup_forwarded() { mTrampoline.initializeService(); mTrampoline.endFullBackup(mUserId); verify(mBackupManagerServiceMock).endFullBackup(mUserId); } @Test - public void dump_callerDoesNotHavePermission_ignored() throws RemoteException { + public void dump_callerDoesNotHavePermission_ignored() { when(mContextMock.checkCallingOrSelfPermission( android.Manifest.permission.DUMP)).thenReturn( PackageManager.PERMISSION_DENIED); @@ -1189,7 +1200,7 @@ public class TrampolineTest { } @Test - public void dump_calledBeforeInitialize_ignored() throws RemoteException { + public void dump_calledBeforeInitialize_ignored() { when(mContextMock.checkCallingOrSelfPermission( android.Manifest.permission.DUMP)).thenReturn( PackageManager.PERMISSION_GRANTED); @@ -1200,7 +1211,7 @@ public class TrampolineTest { } @Test - public void dump_callerHasPermission_forwarded() throws RemoteException { + public void dump_callerHasPermission_forwarded() { when(mContextMock.checkCallingOrSelfPermission( android.Manifest.permission.DUMP)).thenReturn( PackageManager.PERMISSION_GRANTED); @@ -1213,11 +1224,36 @@ public class TrampolineTest { private static class TrampolineTestable extends Trampoline { static boolean sBackupDisabled = false; - static File sSuppressFile = null; static int sCallingUserId = -1; static int sCallingUid = -1; static BackupManagerService sBackupManagerServiceMock = null; private int mCreateServiceCallsCount = 0; + private SparseArray<FakeFile> mSuppressFiles = new SparseArray<>(); + + private static class FakeFile extends File { + private boolean mExists; + + FakeFile(String pathname) { + super(pathname); + } + + @Override + public boolean exists() { + return mExists; + } + + @Override + public boolean delete() { + mExists = false; + return true; + } + + @Override + public boolean createNewFile() throws IOException { + mExists = true; + return true; + } + } TrampolineTestable(Context context) { super(context); @@ -1229,8 +1265,12 @@ public class TrampolineTest { } @Override - public File getSuppressFile() { - return sSuppressFile; + public File getSuppressFileForUser(int userId) { + if (mSuppressFiles.get(userId) == null) { + FakeFile file = new FakeFile(Integer.toString(userId)); + mSuppressFiles.append(userId, file); + } + return mSuppressFiles.get(userId); } protected int binderGetCallingUserId() { @@ -1249,6 +1289,11 @@ public class TrampolineTest { } @Override + protected void createBackupSuppressFileForUser(int userId) throws IOException { + getSuppressFileForUser(userId).createNewFile(); + } + + @Override protected void postToHandler(Runnable runnable) { runnable.run(); } |