Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 431faf4e authored by Piyush Mehrotra's avatar Piyush Mehrotra Committed by Android (Google) Code Review
Browse files

Merge "Enfore caller permission check before reading whether a user is ready for backup."

parents 0e2eb0e9 57a56f91
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.app.backup.BackupManager;
import android.app.backup.BackupManager.OperationType;
import android.app.backup.IBackupManager;
import android.app.backup.IBackupManagerMonitor;
import android.app.backup.IBackupObserver;
@@ -284,6 +283,7 @@ public class BackupManagerService extends IBackupManager.Stub {
     */
    @Override
    public boolean isUserReadyForBackup(int userId) {
        enforceCallingPermissionOnUserId(userId, "isUserReadyForBackup()");
        return mUserServices.get(UserHandle.USER_SYSTEM) != null
                && mUserServices.get(userId) != null;
    }
+29 −0
Original line number Diff line number Diff line
@@ -301,6 +301,35 @@ public class BackupManagerServiceRoboTest {
        verify(mUserOneService, never()).initializeTransports(transports, /* observer */ null);
    }

    /**
     * Test that the backup services throws a {@link SecurityException} if the caller does not have
     * INTERACT_ACROSS_USERS_FULL permission and passes a different user id.
     */
    @Test
    public void testIsUserReadyForBackup_withoutPermission_throwsSecurityException() {
        BackupManagerService backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        expectThrows(
                SecurityException.class,
                () -> backupManagerService.isUserReadyForBackup(mUserOneId));
    }

    /**
     * Test that the backup service does not throw a {@link SecurityException} if the caller has
     * INTERACT_ACROSS_USERS_FULL permission and passes a different user id.
     */
    @Test
    public void testIsUserReadyForBackup_withPermission_callsMethodForUser() {
        BackupManagerService backupManagerService = createService();
        registerUser(backupManagerService, UserHandle.USER_SYSTEM, mUserSystemService);
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ true);

        assertThat(backupManagerService.isUserReadyForBackup(mUserOneId)).isTrue();
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testClearBackupData_onRegisteredUser_callsMethodForUser() throws Exception {