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

Commit 470f8be8 authored by Bernardo Rufino's avatar Bernardo Rufino
Browse files

Move updateTransportAttributes() to Trampoline

From BMS.

Test: atest BackupManagerServiceTest TrampolineRoboTest TrampolineTest
Bug: 135661048
Change-Id: I4527e24a35f8a65f87cb3d9d383485546b4b64e8
parent b188863f
Loading
Loading
Loading
Loading
+0 −51
Original line number Original line Diff line number Diff line
@@ -28,9 +28,7 @@ import android.app.backup.IRestoreSession;
import android.app.job.JobParameters;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Binder;
import android.os.IBinder;
import android.os.IBinder;
@@ -102,55 +100,6 @@ public class BackupManagerService {
     */
     */
    // TODO (b/118520567): Stop hardcoding system user when we pass in user id as a parameter
    // TODO (b/118520567): Stop hardcoding system user when we pass in user id as a parameter


    // ---------------------------------------------
    // TRANSPORT OPERATIONS
    // ---------------------------------------------

    /**
     * Update the attributes of the transport identified by {@code transportComponent}. If the
     * specified transport has not been bound at least once (for registration), this call will be
     * ignored. Only the host process of the transport can change its description, otherwise a
     * {@link SecurityException} will be thrown.
     *
     * @param transportComponent The identity of the transport being described.
     * @param name A {@link String} with the new name for the transport. This is NOT for
     *     identification. MUST NOT be {@code null}.
     * @param configurationIntent An {@link Intent} that can be passed to {@link
     *     Context#startActivity} in order to launch the transport's configuration UI. It may be
     *     {@code null} if the transport does not offer any user-facing configuration UI.
     * @param currentDestinationString A {@link String} describing the destination to which the
     *     transport is currently sending data. MUST NOT be {@code null}.
     * @param dataManagementIntent An {@link Intent} that can be passed to {@link
     *     Context#startActivity} in order to launch the transport's data-management UI. It may be
     *     {@code null} if the transport does not offer any user-facing data management UI.
     * @param dataManagementLabel A {@link CharSequence} to be used as the label for the transport's
     *     data management affordance. This MUST be {@code null} when dataManagementIntent is {@code
     *     null} and MUST NOT be {@code null} when dataManagementIntent is not {@code null}.
     * @throws SecurityException If the UID of the calling process differs from the package UID of
     *     {@code transportComponent} or if the caller does NOT have BACKUP permission.
     */
    public void updateTransportAttributes(
            @UserIdInt int userId,
            ComponentName transportComponent,
            String name,
            @Nullable Intent configurationIntent,
            String currentDestinationString,
            @Nullable Intent dataManagementIntent,
            CharSequence dataManagementLabel) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "updateTransportAttributes()");

        if (userBackupManagerService != null) {
            userBackupManagerService.updateTransportAttributes(
                    transportComponent,
                    name,
                    configurationIntent,
                    currentDestinationString,
                    dataManagementIntent,
                    dataManagementLabel);
        }
    }

    /**
    /**
     * Sets the ancestral work profile for the calling user.
     * Sets the ancestral work profile for the calling user.
     *
     *
+46 −1
Original line number Original line Diff line number Diff line
@@ -857,7 +857,7 @@ public class Trampoline extends IBackupManager.Stub {
            @Nullable Intent dataManagementIntent,
            @Nullable Intent dataManagementIntent,
            CharSequence dataManagementLabel) {
            CharSequence dataManagementLabel) {
        if (isUserReadyForBackup(userId)) {
        if (isUserReadyForBackup(userId)) {
            mService.updateTransportAttributes(
            updateTransportAttributes(
                    userId,
                    userId,
                    transportComponent,
                    transportComponent,
                    name,
                    name,
@@ -868,6 +868,51 @@ public class Trampoline extends IBackupManager.Stub {
        }
        }
    }
    }


    /**
     * Update the attributes of the transport identified by {@code transportComponent}. If the
     * specified transport has not been bound at least once (for registration), this call will be
     * ignored. Only the host process of the transport can change its description, otherwise a
     * {@link SecurityException} will be thrown.
     *
     * @param transportComponent The identity of the transport being described.
     * @param name A {@link String} with the new name for the transport. This is NOT for
     *     identification. MUST NOT be {@code null}.
     * @param configurationIntent An {@link Intent} that can be passed to {@link
     *     Context#startActivity} in order to launch the transport's configuration UI. It may be
     *     {@code null} if the transport does not offer any user-facing configuration UI.
     * @param currentDestinationString A {@link String} describing the destination to which the
     *     transport is currently sending data. MUST NOT be {@code null}.
     * @param dataManagementIntent An {@link Intent} that can be passed to {@link
     *     Context#startActivity} in order to launch the transport's data-management UI. It may be
     *     {@code null} if the transport does not offer any user-facing data management UI.
     * @param dataManagementLabel A {@link CharSequence} to be used as the label for the transport's
     *     data management affordance. This MUST be {@code null} when dataManagementIntent is {@code
     *     null} and MUST NOT be {@code null} when dataManagementIntent is not {@code null}.
     * @throws SecurityException If the UID of the calling process differs from the package UID of
     *     {@code transportComponent} or if the caller does NOT have BACKUP permission.
     */
    public void updateTransportAttributes(
            @UserIdInt int userId,
            ComponentName transportComponent,
            String name,
            @Nullable Intent configurationIntent,
            String currentDestinationString,
            @Nullable Intent dataManagementIntent,
            CharSequence dataManagementLabel) {
        UserBackupManagerService userBackupManagerService =
                getServiceForUserIfCallerHasPermission(userId, "updateTransportAttributes()");

        if (userBackupManagerService != null) {
            userBackupManagerService.updateTransportAttributes(
                    transportComponent,
                    name,
                    configurationIntent,
                    currentDestinationString,
                    dataManagementIntent,
                    dataManagementLabel);
        }
    }

    @Override
    @Override
    public String selectBackupTransportForUser(int userId, String transport)
    public String selectBackupTransportForUser(int userId, String transport)
            throws RemoteException {
            throws RemoteException {
+0 −68
Original line number Original line Diff line number Diff line
@@ -21,8 +21,6 @@ import static android.Manifest.permission.DUMP;
import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
import static android.Manifest.permission.PACKAGE_USAGE_STATS;
import static android.Manifest.permission.PACKAGE_USAGE_STATS;


import static com.android.server.backup.testing.TransportData.backupTransport;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
@@ -42,7 +40,6 @@ import android.app.backup.IBackupManagerMonitor;
import android.app.backup.IBackupObserver;
import android.app.backup.IBackupObserver;
import android.app.backup.IFullBackupRestoreObserver;
import android.app.backup.IFullBackupRestoreObserver;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserHandle;
@@ -50,7 +47,6 @@ import android.os.UserManager;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.Presubmit;
import android.util.SparseArray;
import android.util.SparseArray;


import com.android.server.backup.testing.TransportData;
import com.android.server.testing.shadows.ShadowApplicationPackageManager;
import com.android.server.testing.shadows.ShadowApplicationPackageManager;
import com.android.server.testing.shadows.ShadowBinder;
import com.android.server.testing.shadows.ShadowBinder;
import com.android.server.testing.shadows.ShadowEnvironment;
import com.android.server.testing.shadows.ShadowEnvironment;
@@ -219,70 +215,6 @@ public class BackupManagerServiceTest {
                backupManagerService.getServiceForUserIfCallerHasPermission(mUserOneId, "test"));
                backupManagerService.getServiceForUserIfCallerHasPermission(mUserOneId, "test"));
    }
    }


    // ---------------------------------------------
    // Transport tests
    // ---------------------------------------------

    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testUpdateTransportAttributes_onRegisteredUser_callsMethodForUser()
            throws Exception {
        registerUser(mUserOneId, mUserOneService);
        BackupManagerService backupManagerService = createService();
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);
        TransportData transport = backupTransport();
        Intent configurationIntent = new Intent();
        Intent dataManagementIntent = new Intent();

        backupManagerService.updateTransportAttributes(
                mUserOneId,
                transport.getTransportComponent(),
                transport.transportName,
                configurationIntent,
                "currentDestinationString",
                dataManagementIntent,
                "dataManagementLabel");

        verify(mUserOneService)
                .updateTransportAttributes(
                        transport.getTransportComponent(),
                        transport.transportName,
                        configurationIntent,
                        "currentDestinationString",
                        dataManagementIntent,
                        "dataManagementLabel");
    }

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

        backupManagerService.updateTransportAttributes(
                mUserTwoId,
                transport.getTransportComponent(),
                transport.transportName,
                configurationIntent,
                "currentDestinationString",
                dataManagementIntent,
                "dataManagementLabel");

        verify(mUserOneService, never())
                .updateTransportAttributes(
                        transport.getTransportComponent(),
                        transport.transportName,
                        configurationIntent,
                        "currentDestinationString",
                        dataManagementIntent,
                        "dataManagementLabel");
    }

    // ---------------------------------------------
    // ---------------------------------------------
    // Settings tests
    // Settings tests
    // ---------------------------------------------
    // ---------------------------------------------
+61 −0
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@ import android.annotation.UserIdInt;
import android.app.Application;
import android.app.Application;
import android.app.backup.ISelectBackupTransportCallback;
import android.app.backup.ISelectBackupTransportCallback;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.IBinder;
import android.os.Process;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserHandle;
@@ -555,6 +556,66 @@ public class TrampolineRoboTest {
        verify(mUserOneService, never()).getDataManagementLabel(TEST_TRANSPORT);
        verify(mUserOneService, never()).getDataManagementLabel(TEST_TRANSPORT);
    }
    }


    /** Test that the backup service routes methods correctly to the user that requests it. */
    @Test
    public void testUpdateTransportAttributes_onRegisteredUser_callsMethodForUser()
            throws Exception {
        Trampoline backupManagerService = createService();
        registerUser(backupManagerService, mUserOneId, mUserOneService);
        setCallerAndGrantInteractUserPermission(mUserOneId, /* shouldGrantPermission */ false);
        TransportData transport = backupTransport();
        Intent configurationIntent = new Intent();
        Intent dataManagementIntent = new Intent();

        backupManagerService.updateTransportAttributes(
                mUserOneId,
                transport.getTransportComponent(),
                transport.transportName,
                configurationIntent,
                "currentDestinationString",
                dataManagementIntent,
                "dataManagementLabel");

        verify(mUserOneService)
                .updateTransportAttributes(
                        transport.getTransportComponent(),
                        transport.transportName,
                        configurationIntent,
                        "currentDestinationString",
                        dataManagementIntent,
                        "dataManagementLabel");
    }

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

        backupManagerService.updateTransportAttributes(
                mUserTwoId,
                transport.getTransportComponent(),
                transport.transportName,
                configurationIntent,
                "currentDestinationString",
                dataManagementIntent,
                "dataManagementLabel");

        verify(mUserOneService, never())
                .updateTransportAttributes(
                        transport.getTransportComponent(),
                        transport.transportName,
                        configurationIntent,
                        "currentDestinationString",
                        dataManagementIntent,
                        "dataManagementLabel");
    }

    private Trampoline createService() {
    private Trampoline createService() {
        return new Trampoline(mContext);
        return new Trampoline(mContext);
    }
    }
+0 −22
Original line number Original line Diff line number Diff line
@@ -639,28 +639,6 @@ public class TrampolineTest {
                        mFullBackupRestoreObserverMock);
                        mFullBackupRestoreObserverMock);
    }
    }


    @Test
    public void updateTransportAttributesForUser_forwarded() {
        mTrampoline.updateTransportAttributesForUser(
                mUserId,
                TRANSPORT_COMPONENT_NAME,
                TRANSPORT_NAME,
                null,
                "Transport Destination",
                null,
                "Data Management");

        verify(mBackupManagerServiceMock)
                .updateTransportAttributes(
                        mUserId,
                        TRANSPORT_COMPONENT_NAME,
                        TRANSPORT_NAME,
                        null,
                        "Transport Destination",
                        null,
                        "Data Management");
    }

    @Test
    @Test
    public void selectBackupTransportAsyncForUser_beforeUserUnlocked_notifiesBackupNotAllowed()
    public void selectBackupTransportAsyncForUser_beforeUserUnlocked_notifiesBackupNotAllowed()
            throws Exception {
            throws Exception {