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.fullbackupapp; 18 19 import static android.support.test.InstrumentationRegistry.getTargetContext; 20 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.assertFalse; 23 24 import android.content.Context; 25 import android.support.test.runner.AndroidJUnit4; 26 import android.util.Log; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.io.BufferedOutputStream; 33 import java.io.File; 34 import java.io.FileOutputStream; 35 import java.io.IOException; 36 import java.util.Random; 37 38 /** 39 * Device side routines to be invoked by the host side FullbackupRulesHostSideTest. These are not 40 * designed to be called in any other way, as they rely on state set up by the host side test. 41 */ 42 @RunWith(AndroidJUnit4.class) 43 public class FullbackupTest { 44 public static final String TAG = "FullbackupCTSApp"; 45 private static final int FILE_SIZE_BYTES = 1024 * 1024; 46 47 private Context mContext; 48 49 private File mNoBackupFile; 50 private File mNoBackupFile2; 51 private File mDoBackupFile; 52 private File mDoBackupFile2; 53 54 @Before 55 public void setUp() { 56 mContext = getTargetContext(); 57 setupFiles(); 58 } 59 60 private void setupFiles() { 61 // Files in the 'no_backup' directory. We expect these to not be backed up. 62 File noBackupDir = mContext.getNoBackupFilesDir(); 63 File folderInNoBackup = new File(noBackupDir, "folder_not_to_backup"); 64 65 mNoBackupFile = new File(noBackupDir, "file_not_backed_up"); 66 mNoBackupFile2 = new File(folderInNoBackup, "file_not_backed_up2"); 67 68 // Files in the normal files directory. These should be backed up and restored. 69 File filesDir = mContext.getFilesDir(); 70 File normalFolder = new File(filesDir, "normal_folder"); 71 72 mDoBackupFile = new File(filesDir, "file_to_backup"); 73 mDoBackupFile2 = new File(normalFolder, "file_to_backup2"); 74 } 75 76 @Test 77 public void createFiles() throws Exception { 78 // Make sure the data does not exist from before 79 deleteAllFiles(); 80 checkNoFilesExist(); 81 82 // Create test data 83 generateFiles(); 84 checkAllFilesExist(); 85 86 Log.d(TAG, "Test files created:"); 87 Log.d(TAG, mNoBackupFile.getAbsolutePath()); 88 Log.d(TAG, mNoBackupFile2.getAbsolutePath()); 89 Log.d(TAG, mDoBackupFile.getAbsolutePath()); 90 Log.d(TAG, mDoBackupFile2.getAbsolutePath()); 91 } 92 93 @Test 94 public void deleteFilesAfterBackup() throws Exception { 95 // Make sure the test data exists first 96 checkAllFilesExist(); 97 98 // Delete test data 99 deleteAllFiles(); 100 101 // Now there should be no files left 102 checkNoFilesExist(); 103 } 104 105 @Test 106 public void checkRestoredFiles() throws Exception { 107 // After a restore, only files outside the 'no_backup' folder should exist 108 checkNoBackupFilesDoNotExist(); 109 checkDoBackupFilesDoExist(); 110 } 111 112 private void generateFiles() { 113 try { 114 // Add data to all the files we created 115 addData(mNoBackupFile); 116 addData(mNoBackupFile2); 117 addData(mDoBackupFile); 118 addData(mDoBackupFile2); 119 Log.d(TAG, "Files generated!"); 120 } catch (IOException e) { 121 Log.e(TAG, "Unable to generate files", e); 122 } 123 } 124 125 private void deleteAllFiles() { 126 mNoBackupFile.delete(); 127 mNoBackupFile2.delete(); 128 mDoBackupFile.delete(); 129 mDoBackupFile2.delete(); 130 Log.d(TAG, "Files deleted!"); 131 } 132 133 private void addData(File file) throws IOException { 134 file.getParentFile().mkdirs(); 135 byte[] bytes = new byte[FILE_SIZE_BYTES]; 136 new Random().nextBytes(bytes); 137 138 try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { 139 bos.write(bytes, 0, bytes.length); 140 } 141 } 142 143 private void checkAllFilesExist() { 144 assertTrue("File in 'no_backup' did not exist!", mNoBackupFile.exists()); 145 assertTrue("File in folder inside 'no_backup' did not exist!", mNoBackupFile2.exists()); 146 assertTrue("File in 'files' did not exist!", mDoBackupFile.exists()); 147 assertTrue("File in folder inside 'files' did not exist!", mDoBackupFile2.exists()); 148 } 149 150 private void checkNoFilesExist() { 151 assertFalse("File in 'no_backup' did exist!", mNoBackupFile.exists()); 152 assertFalse("File in folder inside 'no_backup' did exist!", mNoBackupFile2.exists()); 153 assertFalse("File in 'files' did exist!", mDoBackupFile.exists()); 154 assertFalse("File in folder inside 'files' did exist!", mDoBackupFile2.exists()); 155 } 156 157 private void checkNoBackupFilesDoNotExist() { 158 assertFalse("File in 'no_backup' did exist!", mNoBackupFile.exists()); 159 assertFalse("File in folder inside 'no_backup' did exist!", mNoBackupFile2.exists()); 160 } 161 162 private void checkDoBackupFilesDoExist() { 163 assertTrue("File in 'files' did not exist!", mDoBackupFile.exists()); 164 assertTrue("File in folder inside 'files' did not exist!", mDoBackupFile2.exists()); 165 } 166 } 167