Binding on-demand #3: updateTransportAttributes() API

This CL introduces the updateTransportAttributes() API to be used by the
transport hosts. It doesn't actually use the description attributes yet,
this will go in another CL. This is because I want to test that CL
together with transport usage. Tests are lacking for TransportManager
and BMS, I'll still add them, but I'm trying to migrate Robolectric
first.

Ref: http://go/br-binding-on-demand
Bug: 17140907
Test: make RunFrameworksServicesRoboTests
Test: runtest -p com.android.server.backup frameworks-services
Test: gts-tradefed run commandAndExit gts-dev -m GtsBackupTestCases
Test: gts-tradefed run commandAndExit gts-dev -m GtsBackupHostTestCases
Test: cts-tradefed run commandAndExit cts-dev -m CtsBackupTestCases
Change-Id: I56f7b5a5026d21d8f11afb371d5560d4913c5f2a
diff --git a/services/tests/servicestests/src/com/android/server/backup/BackupManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/backup/BackupManagerServiceTest.java
new file mode 100644
index 0000000..362856c
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/backup/BackupManagerServiceTest.java
@@ -0,0 +1,324 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.server.backup;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.testng.Assert.expectThrows;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.ContextWrapper;
+import android.content.Intent;
+import android.os.HandlerThread;
+import android.platform.test.annotations.Presubmit;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.Log;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.io.File;
+
+@SmallTest
+@Presubmit
+@RunWith(AndroidJUnit4.class)
+public class BackupManagerServiceTest {
+    private static final String TAG = "BMSTest";
+    private static final ComponentName TRANSPORT_COMPONENT =
+            new ComponentName(
+                    "com.google.android.gms",
+                    "com.google.android.gms.backup.BackupTransportService");
+    private static final String TRANSPORT_NAME = TRANSPORT_COMPONENT.flattenToShortString();
+
+    @Mock private TransportManager mTransportManager;
+    private Context mContext;
+    private HandlerThread mBackupThread;
+    private int mPackageUid;
+    private File mBaseStateDir;
+    private File mDataDir;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        Context baseContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+        mContext = spy(new ContextWrapper(baseContext));
+
+        mBackupThread = new HandlerThread("backup-test");
+        mBackupThread.setUncaughtExceptionHandler(
+                (t, e) -> Log.e(TAG, "Uncaught exception in test thread " + t.getName(), e));
+        mBackupThread.start();
+
+        File cacheDir = mContext.getCacheDir();
+        mBaseStateDir = new File(cacheDir, "base_state_dir");
+        mDataDir = new File(cacheDir, "data_dir");
+
+        mPackageUid =
+                mContext.getPackageManager().getPackageUid(TRANSPORT_COMPONENT.getPackageName(), 0);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        mBackupThread.quit();
+        mBaseStateDir.delete();
+        mDataDir.delete();
+    }
+
+    @Test
+    public void testConstructor_callsTransportManagerSetTransportBoundListener() throws Exception {
+        createBackupManagerService();
+
+        verify(mTransportManager)
+                .setTransportBoundListener(any(TransportManager.TransportBoundListener.class));
+    }
+
+    @Test
+    public void
+            testUpdateTransportAttributes_whenTransportUidEqualsToCallingUid_callsThroughToTransportManager()
+                    throws Exception {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+        Intent configurationIntent = new Intent();
+        Intent dataManagementIntent = new Intent();
+
+        backupManagerService.updateTransportAttributes(
+                mPackageUid,
+                TRANSPORT_COMPONENT,
+                TRANSPORT_NAME,
+                configurationIntent,
+                "currentDestinationString",
+                dataManagementIntent,
+                "dataManagementLabel");
+
+        verify(mTransportManager)
+                .describeTransport(
+                        eq(TRANSPORT_COMPONENT),
+                        eq(TRANSPORT_NAME),
+                        eq(configurationIntent),
+                        eq("currentDestinationString"),
+                        eq(dataManagementIntent),
+                        eq("dataManagementLabel"));
+    }
+
+    @Test
+    public void testUpdateTransportAttributes_whenTransportUidNotEqualToCallingUid_throwsException()
+            throws Exception {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+
+        expectThrows(
+                SecurityException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid + 1,
+                                TRANSPORT_COMPONENT,
+                                TRANSPORT_NAME,
+                                new Intent(),
+                                "currentDestinationString",
+                                new Intent(),
+                                "dataManagementLabel"));
+    }
+
+    @Test
+    public void testUpdateTransportAttributes_whenTransportComponentNull_throwsException() {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+
+        expectThrows(
+                RuntimeException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid,
+                                null,
+                                TRANSPORT_NAME,
+                                new Intent(),
+                                "currentDestinationString",
+                                new Intent(),
+                                "dataManagementLabel"));
+    }
+
+    @Test
+    public void testUpdateTransportAttributes_whenNameNull_throwsException() {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+
+        expectThrows(
+                RuntimeException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid,
+                                TRANSPORT_COMPONENT,
+                                null,
+                                new Intent(),
+                                "currentDestinationString",
+                                new Intent(),
+                                "dataManagementLabel"));
+    }
+
+    @Test
+    public void testUpdateTransportAttributes_whenCurrentDestinationStringNull_throwsException() {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+
+        expectThrows(
+                RuntimeException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid,
+                                TRANSPORT_COMPONENT,
+                                TRANSPORT_NAME,
+                                new Intent(),
+                                null,
+                                new Intent(),
+                                "dataManagementLabel"));
+    }
+
+    @Test
+    public void
+            testUpdateTransportAttributes_whenDataManagementArgumentsNullityDontMatch_throwsException() {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+
+        expectThrows(
+                RuntimeException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid,
+                                TRANSPORT_COMPONENT,
+                                TRANSPORT_NAME,
+                                new Intent(),
+                                "currentDestinationString",
+                                null,
+                                "dataManagementLabel"));
+
+        expectThrows(
+                RuntimeException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid,
+                                TRANSPORT_COMPONENT,
+                                TRANSPORT_NAME,
+                                new Intent(),
+                                "currentDestinationString",
+                                new Intent(),
+                                null));
+    }
+
+    @Test
+    public void
+            testUpdateTransportAttributes_whenDataManagementArgumentsNull_callsThroughToTransportManager() {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+        Intent configurationIntent = new Intent();
+
+        backupManagerService.updateTransportAttributes(
+                mPackageUid,
+                TRANSPORT_COMPONENT,
+                TRANSPORT_NAME,
+                configurationIntent,
+                "currentDestinationString",
+                null,
+                null);
+
+        verify(mTransportManager)
+                .describeTransport(
+                        eq(TRANSPORT_COMPONENT),
+                        eq(TRANSPORT_NAME),
+                        eq(configurationIntent),
+                        eq("currentDestinationString"),
+                        eq(null),
+                        eq(null));
+    }
+
+    @Test
+    public void
+            testUpdateTransportAttributes_whenPermissionGranted_callsThroughToTransportManager() {
+        grantBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+        Intent configurationIntent = new Intent();
+        Intent dataManagementIntent = new Intent();
+
+        backupManagerService.updateTransportAttributes(
+                mPackageUid,
+                TRANSPORT_COMPONENT,
+                TRANSPORT_NAME,
+                configurationIntent,
+                "currentDestinationString",
+                dataManagementIntent,
+                "dataManagementLabel");
+
+        verify(mTransportManager)
+                .describeTransport(
+                        eq(TRANSPORT_COMPONENT),
+                        eq(TRANSPORT_NAME),
+                        eq(configurationIntent),
+                        eq("currentDestinationString"),
+                        eq(dataManagementIntent),
+                        eq("dataManagementLabel"));
+    }
+
+    @Test
+    public void testUpdateTransportAttributes_whenPermissionDenied_throwsSecurityException() {
+        denyBackupPermission();
+        RefactoredBackupManagerService backupManagerService = createBackupManagerService();
+
+        expectThrows(
+                SecurityException.class,
+                () ->
+                        backupManagerService.updateTransportAttributes(
+                                mPackageUid,
+                                TRANSPORT_COMPONENT,
+                                TRANSPORT_NAME,
+                                new Intent(),
+                                "currentDestinationString",
+                                new Intent(),
+                                "dataManagementLabel"));
+    }
+
+    private RefactoredBackupManagerService createBackupManagerService() {
+        return new RefactoredBackupManagerService(
+                mContext,
+                new Trampoline(mContext),
+                mBackupThread,
+                mBaseStateDir,
+                mDataDir,
+                mTransportManager);
+    }
+
+    private void grantBackupPermission() {
+        doNothing().when(mContext).enforceCallingOrSelfPermission(any(), anyString());
+    }
+
+    private void denyBackupPermission() {
+        doThrow(new SecurityException())
+                .when(mContext)
+                .enforceCallingOrSelfPermission(
+                        eq(android.Manifest.permission.BACKUP), anyString());
+    }
+}
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 27ef9d7..ca93663 100644
--- a/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java
+++ b/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java
@@ -559,6 +559,24 @@
     }
 
     @Test
+    public void describeTransport_calledBeforeInitialize_ignored() throws RemoteException {
+        mTrampoline.updateTransportAttributes(TRANSPORT_COMPONENT_NAME, TRANSPORT_NAME, null,
+                "Transport Destination", null, "Data Management");
+        verifyNoMoreInteractions(mBackupManagerServiceMock);
+    }
+
+    @Test
+    public void describeTransport_forwarded() throws RemoteException {
+        when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS);
+
+        mTrampoline.initialize(UserHandle.USER_SYSTEM);
+        mTrampoline.updateTransportAttributes(TRANSPORT_COMPONENT_NAME, TRANSPORT_NAME, null,
+                "Transport Destination", null, "Data Management");
+        verify(mBackupManagerServiceMock).updateTransportAttributes(TRANSPORT_COMPONENT_NAME,
+                TRANSPORT_NAME, null, "Transport Destination", null, "Data Management");
+    }
+
+    @Test
     public void selectBackupTransport_calledBeforeInitialize_ignored() throws RemoteException {
         mTrampoline.selectBackupTransport(TRANSPORT_NAME);
         verifyNoMoreInteractions(mBackupManagerServiceMock);