Home | History | Annotate | Download | only in backup
      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.assertEquals;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import com.android.tradefed.device.DeviceNotAvailableException;
     23 import com.android.tradefed.log.LogUtil.CLog;
     24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
     25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
     26 
     27 import org.junit.After;
     28 import org.junit.Before;
     29 import org.junit.runner.RunWith;
     30 
     31 import java.util.Scanner;
     32 import java.util.regex.Matcher;
     33 import java.util.regex.Pattern;
     34 
     35 /**
     36  * Base class for CTS backup/restore hostside tests
     37  */
     38 @RunWith(DeviceJUnit4ClassRunner.class)
     39 public abstract class BaseBackupHostSideTest extends BaseHostJUnit4Test {
     40     protected boolean mIsBackupSupported;
     41 
     42     /** Value of PackageManager.FEATURE_BACKUP */
     43     private static final String FEATURE_BACKUP = "android.software.backup";
     44 
     45     protected static final String LOCAL_TRANSPORT =
     46             "android/com.android.internal.backup.LocalTransport";
     47 
     48     @Before
     49     public void setUp() throws DeviceNotAvailableException, Exception {
     50         mIsBackupSupported = getDevice().hasFeature("feature:" + FEATURE_BACKUP);
     51         if (!mIsBackupSupported) {
     52             CLog.i("android.software.backup feature is not supported on this device");
     53             return;
     54         }
     55 
     56         // Check that the backup wasn't disabled and the transport wasn't switched unexpectedly.
     57         assertTrue("Backup was unexpectedly disabled during the module test run",
     58                 isBackupEnabled());
     59         assertEquals("LocalTransport should be selected at this point", LOCAL_TRANSPORT,
     60                 getCurrentTransport());
     61     }
     62 
     63     @After
     64     public void tearDown() throws Exception {
     65         // Not deleting to avoid breaking the tests calling super.tearDown()
     66     }
     67 
     68     /**
     69      * Execute shell command "bmgr backupnow <packageName>" and return output from this command.
     70      */
     71     protected String backupNow(String packageName) throws DeviceNotAvailableException {
     72         return getDevice().executeShellCommand("bmgr backupnow " + packageName);
     73     }
     74 
     75     /**
     76      * Execute shell command "bmgr backupnow <packageName>" and assert success.
     77      */
     78     protected void backupNowAndAssertSuccess(String packageName)
     79             throws DeviceNotAvailableException {
     80         String backupnowOutput = backupNow(packageName);
     81 
     82         assertBackupIsSuccessful(packageName, backupnowOutput);
     83     }
     84 
     85     /**
     86      * Execute shell command "bmgr restore <packageName>" and assert success.
     87      */
     88     protected void restoreAndAssertSuccess(String packageName) throws DeviceNotAvailableException {
     89         String restoreOutput = restore(packageName);
     90         assertRestoreIsSuccessful(restoreOutput);
     91     }
     92 
     93     /**
     94      * Execute shell command "bmgr restore <packageName>" and return output from this command.
     95      */
     96     protected String restore(String packageName) throws DeviceNotAvailableException {
     97         return getDevice().executeShellCommand("bmgr restore " + packageName);
     98     }
     99 
    100     /**
    101      * Attempts to clear the device log.
    102      */
    103     protected void clearLogcat() throws DeviceNotAvailableException {
    104         getDevice().executeAdbCommand("logcat", "-c");
    105     }
    106 
    107     /**
    108      * Run test <testName> in test <className> found in package <packageName> on the device, and
    109      * assert it is successful.
    110      */
    111     protected void checkDeviceTest(String packageName, String className, String testName)
    112             throws DeviceNotAvailableException {
    113         boolean result = runDeviceTests(packageName, className, testName);
    114         assertTrue("Device test failed: " + testName, result);
    115     }
    116 
    117     /**
    118      * Parsing the output of "bmgr backupnow" command and checking that the package under test
    119      * was backed up successfully.
    120      *
    121      * Expected format: "Package <packageName> with result: Success"
    122      */
    123     protected void assertBackupIsSuccessful(String packageName, String backupnowOutput) {
    124         Scanner in = new Scanner(backupnowOutput);
    125         boolean success = false;
    126         while (in.hasNextLine()) {
    127             String line = in.nextLine();
    128 
    129             if (line.contains(packageName)) {
    130                 String result = line.split(":")[1].trim();
    131                 if ("Success".equals(result)) {
    132                     success = true;
    133                 }
    134             }
    135         }
    136         in.close();
    137         assertTrue(success);
    138     }
    139 
    140     /**
    141      * Parsing the output of "bmgr backupnow" command and checking that the package under test
    142      * wasn't backed up because backup is not allowed
    143      *
    144      * Expected format: "Package <packageName> with result:  Backup is not allowed"
    145      */
    146     protected void assertBackupIsNotAllowed(String packageName, String backupnowOutput) {
    147         Scanner in = new Scanner(backupnowOutput);
    148         boolean found = false;
    149         while (in.hasNextLine()) {
    150             String line = in.nextLine();
    151 
    152             if (line.contains(packageName)) {
    153                 String result = line.split(":")[1].trim();
    154                 if ("Backup is not allowed".equals(result)) {
    155                     found = true;
    156                 }
    157             }
    158         }
    159         in.close();
    160         assertTrue("Didn't find \'Backup not allowed\' in the output", found);
    161     }
    162 
    163     /**
    164      * Parsing the output of "bmgr restore" command and checking that the package under test
    165      * was restored successfully.
    166      *
    167      * Expected format: "restoreFinished: 0"
    168      */
    169     protected void assertRestoreIsSuccessful(String restoreOutput) {
    170         assertTrue("Restore not successful", restoreOutput.contains("restoreFinished: 0"));
    171     }
    172 
    173     protected void startActivityInPackageAndWait(String packageName, String className)
    174             throws DeviceNotAvailableException {
    175         getDevice().executeShellCommand(String.format(
    176                 "am start -W -a android.intent.action.MAIN -n %s/%s.%s", packageName,
    177                 packageName,
    178                 className));
    179     }
    180 
    181     /**
    182      * Clears backup data stored in Local Transport for a package.
    183      * NB: 'bmgr wipe' does not produce any useful output if the package or transport not found,
    184      * so we cannot really check the success of the operation
    185       */
    186     protected void clearBackupDataInLocalTransport(String packageName)
    187             throws DeviceNotAvailableException {
    188         getDevice().executeShellCommand(String.format("bmgr wipe %s %s", LOCAL_TRANSPORT, packageName));
    189     }
    190 
    191     /**
    192      * Clears package data
    193      */
    194     protected void clearPackageData(String packageName) throws DeviceNotAvailableException {
    195         getDevice().executeShellCommand(String.format("pm clear %s", packageName));
    196     }
    197 
    198     protected boolean isBackupEnabled() throws DeviceNotAvailableException {
    199         boolean isEnabled;
    200         String output = getDevice().executeShellCommand("bmgr enabled");
    201         Pattern pattern = Pattern.compile("^Backup Manager currently (enabled|disabled)$");
    202         Matcher matcher = pattern.matcher(output.trim());
    203         if (matcher.find()) {
    204             isEnabled = "enabled".equals(matcher.group(1));
    205         } else {
    206             throw new RuntimeException("non-parsable output setting bmgr enabled: " + output);
    207         }
    208         return isEnabled;
    209     }
    210 
    211     protected String getCurrentTransport() throws DeviceNotAvailableException {
    212         String output = getDevice().executeShellCommand("bmgr list transports");
    213         Pattern pattern = Pattern.compile("\\* (.*)");
    214         Matcher matcher = pattern.matcher(output);
    215         if (matcher.find()) {
    216             return matcher.group(1);
    217         } else {
    218             throw new RuntimeException("non-parsable output setting bmgr transport: " + output);
    219         }
    220     }
    221 
    222     private void setLocalTransportParameters(String parameters) throws Exception {
    223         getDevice().executeShellCommand("settings put secure backup_local_transport_parameters "
    224                 + parameters);
    225     }
    226 
    227     protected void enableFakeEncryptionOnTransport() throws Exception {
    228         setLocalTransportParameters("fake_encryption_flag=true");
    229     }
    230 
    231     protected void disableFakeEncryptionOnTransport() throws Exception {
    232         setLocalTransportParameters("fake_encryption_flag=false");
    233     }
    234 }
    235