From 9ec040771540a731da7f6260097253c4dd463137 Mon Sep 17 00:00:00 2001 From: Annie Meng Date: Tue, 16 Jan 2018 14:30:59 +0000 Subject: [PATCH] Move BackupManagerServiceTest to Robolectric framework Bug: 71988336 Test: m -j ROBOTEST_FILTER=BackupManagerServiceRoboTest RunFrameworksServicesRoboTests Change-Id: I81c45267ef98b66c05383bfdca9a1b5ab4ccbfbd --- .../backup/BackupManagerServiceRoboTest.java | 229 ++++++++++++++++++++- 1 file changed, 220 insertions(+), 9 deletions(-) diff --git a/services/robotests/src/com/android/server/backup/BackupManagerServiceRoboTest.java b/services/robotests/src/com/android/server/backup/BackupManagerServiceRoboTest.java index 4ff24e9a5e38..03d28b79b5dd 100644 --- a/services/robotests/src/com/android/server/backup/BackupManagerServiceRoboTest.java +++ b/services/robotests/src/com/android/server/backup/BackupManagerServiceRoboTest.java @@ -21,9 +21,7 @@ import static com.android.server.backup.testing.TransportData.d2dTransport; import static com.android.server.backup.testing.TransportData.localTransport; import static com.android.server.backup.testing.TransportTestUtils.setUpCurrentTransport; import static com.android.server.backup.testing.TransportTestUtils.setUpTransports; - import static com.google.common.truth.Truth.assertThat; - import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; @@ -39,10 +37,10 @@ import android.app.backup.ISelectBackupTransportCallback; 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.provider.Settings; - import com.android.server.backup.testing.ShadowAppBackupUtils; import com.android.server.backup.testing.ShadowBackupPolicyEnforcer; import com.android.server.backup.testing.TransportData; @@ -50,7 +48,10 @@ import com.android.server.backup.testing.TransportTestUtils.TransportMock; import com.android.server.backup.transport.TransportNotRegisteredException; import com.android.server.testing.FrameworkRobolectricTestRunner; import com.android.server.testing.SystemLoaderClasses; - +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -62,14 +63,10 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowContextWrapper; import org.robolectric.shadows.ShadowLog; import org.robolectric.shadows.ShadowLooper; +import org.robolectric.shadows.ShadowPackageManager; import org.robolectric.shadows.ShadowSettings; import org.robolectric.shadows.ShadowSystemClock; -import java.io.File; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - @RunWith(FrameworkRobolectricTestRunner.class) @Config( manifest = Config.NONE, @@ -415,6 +412,220 @@ public class BackupManagerServiceRoboTest { mContext.getContentResolver(), Settings.Secure.BACKUP_TRANSPORT); } + /* Tests for updating transport attributes */ + + private static final int PACKAGE_UID = 10; + private ComponentName mTransportComponent; + private int mTransportUid; + + private void setUpForUpdateTransportAttributes() throws Exception { + mTransportComponent = mTransport.getTransportComponent(); + String transportPackage = mTransportComponent.getPackageName(); + + ShadowPackageManager shadowPackageManager = shadowOf(mContext.getPackageManager()); + shadowPackageManager.addPackage(transportPackage); + shadowPackageManager.setPackagesForUid(PACKAGE_UID, transportPackage); + + mTransportUid = mContext.getPackageManager().getPackageUid(transportPackage, 0); + } + + @Test + public void + testUpdateTransportAttributes_whenTransportUidEqualsToCallingUid_callsThroughToTransportManager() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + Intent configurationIntent = new Intent(); + Intent dataManagementIntent = new Intent(); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + mTransportName, + configurationIntent, + "currentDestinationString", + dataManagementIntent, + "dataManagementLabel"); + + verify(mTransportManager) + .updateTransportAttributes( + eq(mTransportComponent), + eq(mTransportName), + eq(configurationIntent), + eq("currentDestinationString"), + eq(dataManagementIntent), + eq("dataManagementLabel")); + } + + @Test + public void testUpdateTransportAttributes_whenTransportUidNotEqualToCallingUid_throwsException() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + expectThrows( + SecurityException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid + 1, + mTransportComponent, + mTransportName, + new Intent(), + "currentDestinationString", + new Intent(), + "dataManagementLabel")); + } + + @Test + public void testUpdateTransportAttributes_whenTransportComponentNull_throwsException() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + expectThrows( + RuntimeException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid, + null, + mTransportName, + new Intent(), + "currentDestinationString", + new Intent(), + "dataManagementLabel")); + } + + @Test + public void testUpdateTransportAttributes_whenNameNull_throwsException() throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + expectThrows( + RuntimeException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + null, + new Intent(), + "currentDestinationString", + new Intent(), + "dataManagementLabel")); + } + + @Test + public void testUpdateTransportAttributes_whenCurrentDestinationStringNull_throwsException() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + expectThrows( + RuntimeException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + mTransportName, + new Intent(), + null, + new Intent(), + "dataManagementLabel")); + } + + @Test + public void + testUpdateTransportAttributes_whenDataManagementArgumentsNullityDontMatch_throwsException() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + expectThrows( + RuntimeException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + mTransportName, + new Intent(), + "currentDestinationString", + null, + "dataManagementLabel")); + + expectThrows( + RuntimeException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + mTransportName, + new Intent(), + "currentDestinationString", + new Intent(), + null)); + } + + @Test + public void testUpdateTransportAttributes_whenPermissionGranted_callsThroughToTransportManager() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.grantPermissions(android.Manifest.permission.BACKUP); + Intent configurationIntent = new Intent(); + Intent dataManagementIntent = new Intent(); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + mTransportName, + configurationIntent, + "currentDestinationString", + dataManagementIntent, + "dataManagementLabel"); + + verify(mTransportManager) + .updateTransportAttributes( + eq(mTransportComponent), + eq(mTransportName), + eq(configurationIntent), + eq("currentDestinationString"), + eq(dataManagementIntent), + eq("dataManagementLabel")); + } + + @Test + public void testUpdateTransportAttributes_whenPermissionDenied_throwsSecurityException() + throws Exception { + setUpForUpdateTransportAttributes(); + mShadowContext.denyPermissions(android.Manifest.permission.BACKUP); + RefactoredBackupManagerService backupManagerService = + createInitializedBackupManagerService(); + + expectThrows( + SecurityException.class, + () -> + backupManagerService.updateTransportAttributes( + mTransportUid, + mTransportComponent, + mTransportName, + new Intent(), + "currentDestinationString", + new Intent(), + "dataManagementLabel")); + } + /* Miscellaneous tests */ @Test -- 2.11.0