CTS test for 'allowBackup' manifest attribute
Uses 2 apps that differ only by 'allowBackup' manifest attribute value.
Tests 2 scenarios:
1. App that has 'allowBackup=false' in the manifest shouldn't be backed
up.
2. App that doesn't have 'allowBackup' in the manifest (default is true)
should be backed up.
The flow of the tests is the following:
1. Install the app
2. Generate files in the app's data folder.
3. Run 'bmgr backupnow'. Depending on the manifest we expect either
'Success' or
'Backup is not allowed' in the output.
4. Uninstall/reinstall the app
5. Check whether the files were restored or not depending on the
manifest.
Test: cts-tradefed run cts-dev -m CtsBackupHostTestCases -t
android.cts.backup.AllowBackupHostSideTest
Bug: 38108587
Change-Id: I09d14e387193f3a0e5e3eb4561b1fc18c70b299a
diff --git a/hostsidetests/backup/AllowBackup/Android.mk b/hostsidetests/backup/AllowBackup/Android.mk
new file mode 100644
index 0000000..b798d87
--- /dev/null
+++ b/hostsidetests/backup/AllowBackup/Android.mk
@@ -0,0 +1,15 @@
+# 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.
+
+include $(call all-subdir-makefiles)
diff --git a/hostsidetests/backup/AllowBackup/BackupAllowedApp/Android.mk b/hostsidetests/backup/AllowBackup/BackupAllowedApp/Android.mk
new file mode 100644
index 0000000..6eaf7f0
--- /dev/null
+++ b/hostsidetests/backup/AllowBackup/BackupAllowedApp/Android.mk
@@ -0,0 +1,39 @@
+# 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
+
+LOCAL_SRC_FILES := $(call all-java-files-under, ../src)
+
+# tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+LOCAL_PACKAGE_NAME := BackupAllowedApp
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/hostsidetests/backup/AllowBackup/BackupAllowedApp/AndroidManifest.xml b/hostsidetests/backup/AllowBackup/BackupAllowedApp/AndroidManifest.xml
new file mode 100644
index 0000000..d4de01d
--- /dev/null
+++ b/hostsidetests/backup/AllowBackup/BackupAllowedApp/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ 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
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.cts.backup.backupnotallowedapp">
+
+ <application android:label="BackupAllowedApp">
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation
+ android:name="android.support.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.cts.backup.backupnotallowedapp" />
+
+</manifest>
diff --git a/hostsidetests/backup/AllowBackup/BackupNotAllowedApp/Android.mk b/hostsidetests/backup/AllowBackup/BackupNotAllowedApp/Android.mk
new file mode 100644
index 0000000..e8c70b9
--- /dev/null
+++ b/hostsidetests/backup/AllowBackup/BackupNotAllowedApp/Android.mk
@@ -0,0 +1,39 @@
+# 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Don't include this package in any target
+LOCAL_MODULE_TAGS := tests
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_DEX_PREOPT := false
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
+
+LOCAL_SRC_FILES := $(call all-java-files-under, ../src)
+
+# tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+LOCAL_PACKAGE_NAME := BackupNotAllowedApp
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/hostsidetests/backup/AllowBackup/BackupNotAllowedApp/AndroidManifest.xml b/hostsidetests/backup/AllowBackup/BackupNotAllowedApp/AndroidManifest.xml
new file mode 100644
index 0000000..5fc71a1
--- /dev/null
+++ b/hostsidetests/backup/AllowBackup/BackupNotAllowedApp/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ 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
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.cts.backup.backupnotallowedapp">
+
+ <application android:label="BackupNotAllowedApp"
+ android:allowBackup="false">
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation
+ android:name="android.support.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.cts.backup.backupnotallowedapp" />
+
+</manifest>
diff --git a/hostsidetests/backup/AllowBackup/src/AllowBackupTest.java b/hostsidetests/backup/AllowBackup/src/AllowBackupTest.java
new file mode 100644
index 0000000..643b594
--- /dev/null
+++ b/hostsidetests/backup/AllowBackup/src/AllowBackupTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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 android.cts.backup.backupnotallowedapp;
+
+import static android.support.test.InstrumentationRegistry.getTargetContext;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.content.Context;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.Log;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Random;
+
+/**
+ * Device side routines to be invoked by the host side AllowBackupHostSideTest. These are not
+ * designed to be called in any other way, as they rely on state set up by the host side test.
+ *
+ */
+@RunWith(AndroidJUnit4.class)
+public class AllowBackupTest {
+ public static final String TAG = "AllowBackupCTSApp";
+ private static final int FILE_SIZE_BYTES = 1024 * 1024;
+
+ private Context mContext;
+
+ private File mDoBackupFile;
+ private File mDoBackupFile2;
+
+ @Before
+ public void setUp() {
+ mContext = getTargetContext();
+ setupFiles();
+ }
+
+ private void setupFiles() {
+ File filesDir = mContext.getFilesDir();
+ File normalFolder = new File(filesDir, "normal_folder");
+
+ mDoBackupFile = new File(filesDir, "file_to_backup");
+ mDoBackupFile2 = new File(normalFolder, "file_to_backup2");
+ }
+
+ @Test
+ public void createFiles() throws Exception {
+ // Make sure the data does not exist from before
+ deleteAllFiles();
+ assertNoFilesExist();
+
+ // Create test data
+ generateFiles();
+ assertAllFilesExist();
+
+ Log.d(TAG, "Test files created: \n"
+ + mDoBackupFile.getAbsolutePath() + "\n"
+ + mDoBackupFile2.getAbsolutePath());
+ }
+
+ @Test
+ public void checkNoFilesExist() throws Exception {
+ assertNoFilesExist();
+ }
+
+ @Test
+ public void checkAllFilesExist() throws Exception {
+ assertAllFilesExist();
+ }
+
+ private void generateFiles() {
+ try {
+ // Add data to all the files we created
+ addData(mDoBackupFile);
+ addData(mDoBackupFile2);
+ Log.d(TAG, "Files generated!");
+ } catch (IOException e) {
+ Log.e(TAG, "Unable to generate files", e);
+ }
+ }
+
+ private void deleteAllFiles() {
+ mDoBackupFile.delete();
+ mDoBackupFile2.delete();
+ Log.d(TAG, "Files deleted!");
+ }
+
+ private void addData(File file) throws IOException {
+ file.getParentFile().mkdirs();
+ byte[] bytes = new byte[FILE_SIZE_BYTES];
+ new Random().nextBytes(bytes);
+
+ try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
+ bos.write(bytes, 0, bytes.length);
+ }
+ }
+
+ private void assertAllFilesExist() {
+ assertTrue("File in 'files' did not exist!", mDoBackupFile.exists());
+ assertTrue("File in folder inside 'files' did not exist!", mDoBackupFile2.exists());
+ }
+
+ private void assertNoFilesExist() {
+ assertFalse("File in 'files' did exist!", mDoBackupFile.exists());
+ assertFalse("File in folder inside 'files' did exist!", mDoBackupFile2.exists());
+ }
+}
diff --git a/hostsidetests/backup/src/android/cts/backup/AllowBackupHostSideTest.java b/hostsidetests/backup/src/android/cts/backup/AllowBackupHostSideTest.java
new file mode 100644
index 0000000..043c982
--- /dev/null
+++ b/hostsidetests/backup/src/android/cts/backup/AllowBackupHostSideTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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 android.cts.backup;
+
+import static junit.framework.Assert.assertNull;
+
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.device.DeviceNotAvailableException;
+
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Test checking that allowBackup manifest attribute is respected by backup manager.
+ *
+ * Uses 2 apps that differ only by 'allowBackup' manifest attribute value.
+ *
+ * Tests 2 scenarios:
+ *
+ * 1. App that has 'allowBackup=false' in the manifest shouldn't be backed up.
+ * 2. App that doesn't have 'allowBackup' in the manifest (default is true) should be backed up.
+ *
+ * The flow of the tests is the following:
+ * 1. Install the app
+ * 2. Generate files in the app's data folder.
+ * 3. Run 'bmgr backupnow'. Depending on the manifest we expect either 'Success' or
+ * 'Backup is not allowed' in the output.
+ * 4. Uninstall/reinstall the app
+ * 5. Check whether the files were restored or not depending on the manifest.
+ *
+ * Invokes device side tests provided by
+ * android.cts.backup.backupnotallowedapp.AllowBackupTest.
+ */
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class AllowBackupHostSideTest extends BaseBackupHostSideTest {
+
+ private static final String ALLOWBACKUP_APP_NAME = "android.cts.backup.backupnotallowedapp";
+ private static final String ALLOWBACKUP_DEVICE_TEST_CLASS_NAME =
+ ALLOWBACKUP_APP_NAME + ".AllowBackupTest";
+
+ /** The name of the APK of the app that has allowBackup=false in the manifest */
+ private static final String ALLOWBACKUP_FALSE_APP_APK = "BackupNotAllowedApp.apk";
+
+ /** The name of the APK of the app that doesn't have allowBackup in the manifest
+ * (same as allowBackup=true by default) */
+ private static final String ALLOWBACKUP_APP_APK = "BackupAllowedApp.apk";
+
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+
+ // Clear backup data and uninstall the package (in that order!)
+ clearBackupDataInLocalTransport(ALLOWBACKUP_APP_NAME);
+ assertNull(uninstallPackage(ALLOWBACKUP_APP_NAME));
+ }
+
+ @Test
+ public void testAllowBackup_False() throws Exception {
+ installPackage(ALLOWBACKUP_FALSE_APP_APK, "-d", "-r");
+
+ // Generate the files that are going to be backed up.
+ checkAllowBackupDeviceTest("createFiles");
+
+ // Do a backup
+ String backupnowOutput = backupNow(ALLOWBACKUP_APP_NAME);
+
+ assertBackupIsNotAllowed(ALLOWBACKUP_APP_NAME, backupnowOutput);
+
+ assertNull(uninstallPackage(ALLOWBACKUP_APP_NAME));
+
+ installPackage(ALLOWBACKUP_FALSE_APP_APK, "-d", "-r");
+
+ checkAllowBackupDeviceTest("checkNoFilesExist");
+ }
+
+ @Test
+ public void testAllowBackup_True() throws Exception {
+ installPackage(ALLOWBACKUP_APP_APK, "-d", "-r");
+
+ // Generate the files that are going to be backed up.
+ checkAllowBackupDeviceTest("createFiles");
+
+ // Do a backup
+ String backupnowOutput = backupNow(ALLOWBACKUP_APP_NAME);
+
+ assertBackupIsSuccessful(ALLOWBACKUP_APP_NAME, backupnowOutput);
+
+ assertNull(uninstallPackage(ALLOWBACKUP_APP_NAME));
+
+ installPackage(ALLOWBACKUP_APP_APK, "-d", "-r");
+
+ checkAllowBackupDeviceTest("checkAllFilesExist");
+ }
+
+ private void checkAllowBackupDeviceTest(String methodName)
+ throws DeviceNotAvailableException {
+ checkDeviceTest(ALLOWBACKUP_APP_NAME, ALLOWBACKUP_DEVICE_TEST_CLASS_NAME,
+ methodName);
+ }
+
+}
diff --git a/hostsidetests/backup/src/android/cts/backup/BaseBackupHostSideTest.java b/hostsidetests/backup/src/android/cts/backup/BaseBackupHostSideTest.java
index a51f074..d440ff4 100644
--- a/hostsidetests/backup/src/android/cts/backup/BaseBackupHostSideTest.java
+++ b/hostsidetests/backup/src/android/cts/backup/BaseBackupHostSideTest.java
@@ -115,7 +115,6 @@
* Expected format: "Package <packageName> with result: Success"
*/
protected void assertBackupIsSuccessful(String packageName, String backupnowOutput) {
- // Assert backup was successful.
Scanner in = new Scanner(backupnowOutput);
boolean success = false;
while (in.hasNextLine()) {
@@ -133,6 +132,29 @@
}
/**
+ * Parsing the output of "bmgr backupnow" command and checking that the package under test
+ * wasn't backed up because backup is not allowed
+ *
+ * Expected format: "Package <packageName> with result: Backup is not allowed"
+ */
+ protected void assertBackupIsNotAllowed(String packageName, String backupnowOutput) {
+ Scanner in = new Scanner(backupnowOutput);
+ boolean found = false;
+ while (in.hasNextLine()) {
+ String line = in.nextLine();
+
+ if (line.contains(packageName)) {
+ String result = line.split(":")[1].trim();
+ if ("Backup is not allowed".equals(result)) {
+ found = true;
+ }
+ }
+ }
+ in.close();
+ assertTrue("Didn't find \'Backup not allowed\' in the output", found);
+ }
+
+ /**
* Parsing the output of "bmgr restore" command and checking that the package under test
* was restored successfully.
*