1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package android.cts.backup; 18 19 import static org.junit.Assert.assertNull; 20 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 24 25 import org.junit.After; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 /** 30 * Test checking that allowBackup manifest attribute is respected by backup manager. 31 * 32 * Uses 2 apps that differ only by 'allowBackup' manifest attribute value. 33 * 34 * Tests 2 scenarios: 35 * 36 * 1. App that has 'allowBackup=false' in the manifest shouldn't be backed up. 37 * 2. App that doesn't have 'allowBackup' in the manifest (default is true) should be backed up. 38 * 39 * The flow of the tests is the following: 40 * 1. Install the app 41 * 2. Generate files in the app's data folder. 42 * 3. Run 'bmgr backupnow'. Depending on the manifest we expect either 'Success' or 43 * 'Backup is not allowed' in the output. 44 * 4. Uninstall/reinstall the app 45 * 5. Check whether the files were restored or not depending on the manifest. 46 * 47 * Invokes device side tests provided by 48 * android.cts.backup.backupnotallowedapp.AllowBackupTest. 49 */ 50 @RunWith(DeviceJUnit4ClassRunner.class) 51 public class AllowBackupHostSideTest extends BaseBackupHostSideTest { 52 53 private static final String ALLOWBACKUP_APP_NAME = "android.cts.backup.backupnotallowedapp"; 54 private static final String ALLOWBACKUP_DEVICE_TEST_CLASS_NAME = 55 ALLOWBACKUP_APP_NAME + ".AllowBackupTest"; 56 57 /** The name of the APK of the app that has allowBackup=false in the manifest */ 58 private static final String ALLOWBACKUP_FALSE_APP_APK = "BackupNotAllowedApp.apk"; 59 60 /** The name of the APK of the app that doesn't have allowBackup in the manifest 61 * (same as allowBackup=true by default) */ 62 private static final String ALLOWBACKUP_APP_APK = "BackupAllowedApp.apk"; 63 64 @After 65 @Override 66 public void tearDown() throws Exception { 67 super.tearDown(); 68 69 if (!mIsBackupSupported) { 70 return; 71 } 72 73 // Clear backup data and uninstall the package (in that order!) 74 clearBackupDataInLocalTransport(ALLOWBACKUP_APP_NAME); 75 assertNull(uninstallPackage(ALLOWBACKUP_APP_NAME)); 76 } 77 78 @Test 79 public void testAllowBackup_False() throws Exception { 80 if (!mIsBackupSupported) { 81 CLog.i("android.software.backup feature is not supported on this device"); 82 return; 83 } 84 85 installPackage(ALLOWBACKUP_FALSE_APP_APK, "-d", "-r"); 86 87 // Generate the files that are going to be backed up. 88 checkAllowBackupDeviceTest("createFiles"); 89 90 // Do a backup 91 String backupnowOutput = backupNow(ALLOWBACKUP_APP_NAME); 92 93 assertBackupIsNotAllowed(ALLOWBACKUP_APP_NAME, backupnowOutput); 94 95 assertNull(uninstallPackage(ALLOWBACKUP_APP_NAME)); 96 97 installPackage(ALLOWBACKUP_FALSE_APP_APK, "-d", "-r"); 98 99 checkAllowBackupDeviceTest("checkNoFilesExist"); 100 } 101 102 @Test 103 public void testAllowBackup_True() throws Exception { 104 if (!mIsBackupSupported) { 105 CLog.i("android.software.backup feature is not supported on this device"); 106 return; 107 } 108 109 installPackage(ALLOWBACKUP_APP_APK, "-d", "-r"); 110 111 // Generate the files that are going to be backed up. 112 checkAllowBackupDeviceTest("createFiles"); 113 114 // Do a backup 115 String backupnowOutput = backupNow(ALLOWBACKUP_APP_NAME); 116 117 assertBackupIsSuccessful(ALLOWBACKUP_APP_NAME, backupnowOutput); 118 119 assertNull(uninstallPackage(ALLOWBACKUP_APP_NAME)); 120 121 installPackage(ALLOWBACKUP_APP_APK, "-d", "-r"); 122 123 checkAllowBackupDeviceTest("checkAllFilesExist"); 124 } 125 126 private void checkAllowBackupDeviceTest(String methodName) 127 throws DeviceNotAvailableException { 128 checkDeviceTest(ALLOWBACKUP_APP_NAME, ALLOWBACKUP_DEVICE_TEST_CLASS_NAME, 129 methodName); 130 } 131 132 } 133