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

Commit 7dedc861 authored by Bernardo Rufino's avatar Bernardo Rufino
Browse files

Move settings operations to Trampoline

From BMS.

Test: atest BackupManagerServiceTest TrampolineRoboTest TrampolineTest
Bug: 135661048
Change-Id: I47e8072898555fd881ffa7b3d546e858fad01c6d
parent 0c8b84e4
Loading
Loading
Loading
Loading
+0 −34
Original line number Diff line number Diff line
@@ -98,40 +98,6 @@ public class BackupManagerService {
     */
    // TODO (b/118520567): Stop hardcoding system user when we pass in user id as a parameter

    // ---------------------------------------------
    // SETTINGS OPERATIONS
    // ---------------------------------------------

    /** Enable/disable the backup service. This is user-configurable via backup settings. */
    public void setBackupEnabled(@UserIdInt int userId, boolean enable) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "setBackupEnabled()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setBackupEnabled(enable);
        }
    }

    /** Enable/disable automatic restore of app data at install time. */
    public void setAutoRestore(@UserIdInt int userId, boolean autoRestore) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "setAutoRestore()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setAutoRestore(autoRestore);
        }
    }

    /**
     * Return {@code true} if the backup mechanism is currently enabled, else returns {@code false}.
     */
    public boolean isBackupEnabled(@UserIdInt int userId) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "isBackupEnabled()");

        return userBackupManagerService != null && userBackupManagerService.isBackupEnabled();
    }

    // ---------------------------------------------
    // BACKUP OPERATIONS
    // ---------------------------------------------
+33 −3
Original line number Diff line number Diff line
@@ -644,7 +644,7 @@ public class Trampoline extends IBackupManager.Stub {
    public void setBackupEnabledForUser(@UserIdInt int userId, boolean isEnabled)
            throws RemoteException {
        if (isUserReadyForBackup(userId)) {
            mService.setBackupEnabled(userId, isEnabled);
            setBackupEnabled(userId, isEnabled);
        }
    }

@@ -653,10 +653,20 @@ public class Trampoline extends IBackupManager.Stub {
        setBackupEnabledForUser(binderGetCallingUserId(), isEnabled);
    }

    /** Enable/disable the backup service. This is user-configurable via backup settings. */
    public void setBackupEnabled(@UserIdInt int userId, boolean enable) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "setBackupEnabled()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setBackupEnabled(enable);
        }
    }

    @Override
    public void setAutoRestoreForUser(int userId, boolean doAutoRestore) throws RemoteException {
        if (isUserReadyForBackup(userId)) {
            mService.setAutoRestore(userId, doAutoRestore);
            setAutoRestore(userId, doAutoRestore);
        }
    }

@@ -665,9 +675,19 @@ public class Trampoline extends IBackupManager.Stub {
        setAutoRestoreForUser(binderGetCallingUserId(), doAutoRestore);
    }

    /** Enable/disable automatic restore of app data at install time. */
    public void setAutoRestore(@UserIdInt int userId, boolean autoRestore) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "setAutoRestore()");

        if (userBackupManagerService != null) {
            userBackupManagerService.setAutoRestore(autoRestore);
        }
    }

    @Override
    public boolean isBackupEnabledForUser(@UserIdInt int userId) throws RemoteException {
        return isUserReadyForBackup(userId) && mService.isBackupEnabled(userId);
        return isUserReadyForBackup(userId) && isBackupEnabled(userId);
    }

    @Override
@@ -675,6 +695,16 @@ public class Trampoline extends IBackupManager.Stub {
        return isBackupEnabledForUser(binderGetCallingUserId());
    }

    /**
     * Return {@code true} if the backup mechanism is currently enabled, else returns {@code false}.
     */
    public boolean isBackupEnabled(@UserIdInt int userId) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "isBackupEnabled()");

        return userBackupManagerService != null && userBackupManagerService.isBackupEnabled();
    }

    @Override
    public boolean setBackupPassword(String currentPw, String newPw) throws RemoteException {
        int userId = binderGetCallingUserId();
+0 −107
Original line number Diff line number Diff line
@@ -215,113 +215,6 @@ public class BackupManagerServiceTest {
                backupManagerService.getServiceForUserIfCallerHasPermission(mUserOneId, "test"));
    }

    // ---------------------------------------------
    // Settings tests
    // ---------------------------------------------
    /**
     * 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 testSetBackupEnabled_withoutPermission_throwsSecurityExceptionForNonCallingUser() {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        expectThrows(
                SecurityException.class,
                () -> backupManagerService.setBackupEnabled(mUserTwoId, true));
    }

    /**
     * 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 testSetBackupEnabled_withPermission_propagatesForNonCallingUser() {
        registerUser(mUserOneId, mUserOneService);
        registerUser(mUserTwoId, mUserTwoService);
        BackupManagerService backupManagerService = createService();

        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true);

        backupManagerService.setBackupEnabled(mUserTwoId, true);

        verify(mUserTwoService).setBackupEnabled(true);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testSetBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.setBackupEnabled(mUserOneId, true);

        verify(mUserOneService).setBackupEnabled(true);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testSetBackupEnabled_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.setBackupEnabled(mUserTwoId, true);

        verify(mUserOneService, never()).setBackupEnabled(true);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testSetAutoRestore_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.setAutoRestore(mUserOneId, true);

        verify(mUserOneService).setAutoRestore(true);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testSetAutoRestore_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.setAutoRestore(mUserTwoId, true);

        verify(mUserOneService, never()).setAutoRestore(true);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testIsBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.isBackupEnabled(mUserOneId);

        verify(mUserOneService).isBackupEnabled();
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testIsBackupEnabled_onUnknownUser_doesNotPropagateCall() throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.isBackupEnabled(mUserTwoId);

        verify(mUserOneService, never()).isBackupEnabled();
    }

    // ---------------------------------------------
    // Backup tests
    // ---------------------------------------------
+109 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;
import static org.testng.Assert.expectThrows;

import android.annotation.UserIdInt;
import android.app.Application;
@@ -616,6 +617,114 @@ public class TrampolineRoboTest {
                        "dataManagementLabel");
    }

    // ---------------------------------------------
    // Settings tests
    // ---------------------------------------------

    /**
     * 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 testSetBackupEnabled_withoutPermission_throwsSecurityExceptionForNonCallingUser() {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        expectThrows(
                SecurityException.class,
                () -> backupManagerService.setBackupEnabled(mUserTwoId, true));
    }

    /**
     * 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 testSetBackupEnabled_withPermission_propagatesForNonCallingUser() {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        registerUser(backupManagerService, mUserTwoId, mUserTwoService);

        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ true);

        backupManagerService.setBackupEnabled(mUserTwoId, true);

        verify(mUserTwoService).setBackupEnabled(true);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testSetBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.setBackupEnabled(mUserOneId, true);

        verify(mUserOneService).setBackupEnabled(true);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testSetBackupEnabled_onUnknownUser_doesNotPropagateCall() throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.setBackupEnabled(mUserTwoId, true);

        verify(mUserOneService, never()).setBackupEnabled(true);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testSetAutoRestore_onRegisteredUser_callsMethodForUser() throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.setAutoRestore(mUserOneId, true);

        verify(mUserOneService).setAutoRestore(true);
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testSetAutoRestore_onUnknownUser_doesNotPropagateCall() throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.setAutoRestore(mUserTwoId, true);

        verify(mUserOneService, never()).setAutoRestore(true);
    }

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testIsBackupEnabled_onRegisteredUser_callsMethodForUser() throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);

        backupManagerService.isBackupEnabled(mUserOneId);

        verify(mUserOneService).isBackupEnabled();
    }

    /** Test that the backup service does not route methods for non-registered users. */
    @Test
    public void testIsBackupEnabled_onUnknownUser_doesNotPropagateCall() throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserTwoId, /* shouldGrantPermission */ false);

        backupManagerService.isBackupEnabled(mUserTwoId);

        verify(mUserOneService, never()).isBackupEnabled();
    }

    private Trampoline createService() {
        return new Trampoline(mContext);
    }
+0 −51
Original line number Diff line number Diff line
@@ -498,57 +498,6 @@ public class TrampolineTest {
        verify(mBackupManagerServiceMock).restoreAtInstall(mUserId, PACKAGE_NAME, 123);
    }

    @Test
    public void setBackupEnabledForUser_forwarded() throws Exception {

        mTrampoline.setBackupEnabledForUser(mUserId, true);

        verify(mBackupManagerServiceMock).setBackupEnabled(mUserId, true);
    }

    @Test
    public void setBackupEnabled_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.setBackupEnabled(true);

        verify(mBackupManagerServiceMock).setBackupEnabled(mUserId, true);
    }

    @Test
    public void setAutoRestoreForUser_forwarded() throws Exception {

        mTrampoline.setAutoRestoreForUser(mUserId, true);

        verify(mBackupManagerServiceMock).setAutoRestore(mUserId, true);
    }

    @Test
    public void setAutoRestore_forwarded() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.setAutoRestore(true);

        verify(mBackupManagerServiceMock).setAutoRestore(mUserId, true);
    }

    @Test
    public void isBackupEnabledForUser_forwarded() throws Exception {

        mTrampoline.isBackupEnabledForUser(mUserId);

        verify(mBackupManagerServiceMock).isBackupEnabled(mUserId);
    }

    @Test
    public void isBackupEnabled_forwardedToCallingUserId() throws Exception {
        TrampolineTestable.sCallingUserId = mUserId;

        mTrampoline.isBackupEnabled();

        verify(mBackupManagerServiceMock).isBackupEnabled(mUserId);
    }

    @Test
    public void setBackupPassword_forwarded() throws Exception {
        mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);