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 'fullBackupOnly' manifest attribute is respected by backup manager. 31 * 32 * Uses 3 different versions of the same app that differ only by 'fullBackupOnly' value and the 33 * presence/absence of the backup agent.* 34 * 35 * Invokes device side tests provided by 36 * {@link android.cts.backup.fullbackupapp.FullBackupOnlyTest}. 37 * 38 * The flow of the tests is the following: 39 * 1. Install the app 40 * 2. Generate files in data folder of the app, including a file in no_backup folder. The file in 41 * no_backup folder is used for key/value backup by the backup agent of the app. 42 * 3. Run 'bmgr backupnow'. Depending on the manifest of the app we expect either Dolly or Key/value 43 * backup to be performed. 44 * 4. Uninstall and reinstall the app. 45 * 5. Check that the correct files were restored depending on the manifest: 46 * - Files in the app's data folder for Dolly backup 47 * - Only file in no_backup folder for key/value. 48 */ 49 @RunWith(DeviceJUnit4ClassRunner.class) 50 public class FullBackupOnlyHostSideTest extends BaseBackupHostSideTest { 51 52 private static final String FULLBACKUPONLY_APP_PACKAGE = "android.cts.backup.fullbackuponlyapp"; 53 private static final String FULLBACKUPONLY_DEVICE_TEST_CLASS_NAME = 54 FULLBACKUPONLY_APP_PACKAGE + ".FullBackupOnlyTest"; 55 56 /** 57 * The name of the APK of the app that has a backup agent and fullBackupOnly=false in the 58 * manifest 59 */ 60 private static final String FULLBACKUPONLY_FALSE_WITH_AGENT_APP_APK = 61 "FullBackupOnlyFalseWithAgentApp.apk"; 62 63 /** 64 * The name of the APK of the app that has no backup agent and fullBackupOnly=false in the 65 * manifest 66 */ 67 private static final String FULLBACKUPONLY_FALSE_NO_AGENT_APP_APK = 68 "FullBackupOnlyFalseNoAgentApp.apk"; 69 70 /** 71 * The name of the APK of the app that has a backup agent and fullBackupOnly=true in the 72 * manifest 73 */ 74 private static final String FULLBACKUPONLY_TRUE_WITH_AGENT_APP_APK = 75 "FullBackupOnlyTrueWithAgentApp.apk"; 76 77 78 @After 79 @Override 80 public void tearDown() throws Exception { 81 super.tearDown(); 82 83 if (!mIsBackupSupported) { 84 return; 85 } 86 87 // Clear backup data and uninstall the package (in that order!) 88 clearBackupDataInLocalTransport(FULLBACKUPONLY_APP_PACKAGE); 89 assertNull(uninstallPackage(FULLBACKUPONLY_APP_PACKAGE)); 90 } 91 92 /** 93 * Tests that the app that doesn't have fullBackupOnly (same as fullBackupOnly=false by default) 94 * and has a backup agent will get key/value backup. 95 * We check that key/value data was restored after reinstall and dolly data was not. 96 */ 97 @Test 98 public void testFullBackupOnlyFalse_WithAgent() throws Exception { 99 if (!mIsBackupSupported) { 100 CLog.i("android.software.backup feature is not supported on this device"); 101 return; 102 } 103 104 installPackage(FULLBACKUPONLY_FALSE_WITH_AGENT_APP_APK, "-d", "-r"); 105 106 checkFullBackupOnlyDeviceTest("createFiles"); 107 108 backupNowAndAssertSuccess(FULLBACKUPONLY_APP_PACKAGE); 109 110 assertNull(uninstallPackage(FULLBACKUPONLY_APP_PACKAGE)); 111 112 installPackage(FULLBACKUPONLY_FALSE_WITH_AGENT_APP_APK, "-d", "-r"); 113 114 checkFullBackupOnlyDeviceTest("checkKeyValueFileExists"); 115 checkFullBackupOnlyDeviceTest("checkDollyFilesDontExist"); 116 } 117 118 /** 119 * Tests that the app that doesn't have fullBackupOnly (same as fullBackupOnly=false by default) 120 * and has no backup agent will get Dolly backup. 121 * We check that key/value data was not restored after reinstall and dolly data was. 122 */ 123 @Test 124 public void testFullBackupOnlyFalse_NoAgent() throws Exception { 125 if (!mIsBackupSupported) { 126 CLog.i("android.software.backup feature is not supported on this device"); 127 return; 128 } 129 130 installPackage(FULLBACKUPONLY_FALSE_NO_AGENT_APP_APK, "-d", "-r"); 131 132 checkFullBackupOnlyDeviceTest("createFiles"); 133 134 backupNowAndAssertSuccess(FULLBACKUPONLY_APP_PACKAGE); 135 136 assertNull(uninstallPackage(FULLBACKUPONLY_APP_PACKAGE)); 137 138 installPackage(FULLBACKUPONLY_FALSE_NO_AGENT_APP_APK, "-d", "-r"); 139 140 checkFullBackupOnlyDeviceTest("checkKeyValueFileDoesntExist"); 141 checkFullBackupOnlyDeviceTest("checkDollyFilesExist"); 142 } 143 144 /** 145 * Tests that the app that has fullBackupOnly=true and has a backup agent will only get 146 * Dolly backup. 147 * We check that key/value data was not restored after reinstall and dolly data was. 148 */ 149 @Test 150 public void testFullBackupOnlyTrue_WithAgent() throws Exception { 151 if (!mIsBackupSupported) { 152 CLog.i("android.software.backup feature is not supported on this device"); 153 return; 154 } 155 156 installPackage(FULLBACKUPONLY_TRUE_WITH_AGENT_APP_APK, "-d", "-r"); 157 158 checkFullBackupOnlyDeviceTest("createFiles"); 159 160 backupNowAndAssertSuccess(FULLBACKUPONLY_APP_PACKAGE); 161 162 assertNull(uninstallPackage(FULLBACKUPONLY_APP_PACKAGE)); 163 164 installPackage(FULLBACKUPONLY_TRUE_WITH_AGENT_APP_APK, "-d", "-r"); 165 166 checkFullBackupOnlyDeviceTest("checkKeyValueFileDoesntExist"); 167 checkFullBackupOnlyDeviceTest("checkDollyFilesExist"); 168 } 169 170 171 private void checkFullBackupOnlyDeviceTest(String methodName) 172 throws DeviceNotAvailableException { 173 checkDeviceTest(FULLBACKUPONLY_APP_PACKAGE, FULLBACKUPONLY_DEVICE_TEST_CLASS_NAME, 174 methodName); 175 } 176 } 177