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.fullbackuponlyapp; 18 19 import static androidx.test.InstrumentationRegistry.getTargetContext; 20 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 24 import android.content.Context; 25 import android.platform.test.annotations.AppModeFull; 26 import android.util.Log; 27 28 import androidx.test.runner.AndroidJUnit4; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.io.BufferedOutputStream; 35 import java.io.File; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.util.Random; 39 40 /** 41 * Device side routines to be invoked by the host side FullBackupOnlyHostSideTest. These 42 * are not designed to be called in any other way, as they rely on state set up by the host side 43 * test. 44 */ 45 @RunWith(AndroidJUnit4.class) 46 @AppModeFull 47 public class FullBackupOnlyTest { 48 private static final String TAG = "FullBackupOnlyTest"; 49 50 private static final int FILE_SIZE_BYTES = 1024 * 1024; 51 52 private File mKeyValueBackupFile; 53 private File mDoBackupFile; 54 private File mDoBackupFile2; 55 56 private Context mContext; 57 58 @Before setUp()59 public void setUp() { 60 Log.i(TAG, "set up"); 61 mContext = getTargetContext(); 62 setupFiles(); 63 } 64 setupFiles()65 private void setupFiles() { 66 mKeyValueBackupFile = getKeyValueBackupFile(mContext); 67 68 // Files to be backed up with Dolly 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 createFiles()77 public void createFiles() throws Exception { 78 // Make sure the data does not exist from before 79 deleteAllFiles(); 80 assertNoFilesExist(); 81 82 // Create test data 83 generateFiles(); 84 assertAllFilesExist(); 85 86 Log.d(TAG, "Test files created: \n" 87 + mKeyValueBackupFile.getAbsolutePath() + "\n" 88 + mDoBackupFile.getAbsolutePath() + "\n" 89 + mDoBackupFile2.getAbsolutePath()); 90 } 91 92 @Test checkKeyValueFileDoesntExist()93 public void checkKeyValueFileDoesntExist() throws Exception { 94 assertKeyValueFileDoesntExist(); 95 } 96 97 @Test checkKeyValueFileExists()98 public void checkKeyValueFileExists() throws Exception { 99 assertKeyValueFileExists(); 100 } 101 102 @Test checkDollyFilesExist()103 public void checkDollyFilesExist() throws Exception { 104 assertDollyFilesExist(); 105 } 106 107 @Test checkDollyFilesDontExist()108 public void checkDollyFilesDontExist() throws Exception { 109 assertDollyFilesDontExist(); 110 } 111 getKeyValueBackupFile(Context context)112 protected static File getKeyValueBackupFile(Context context) { 113 // Files in the 'no_backup' directory are not backed up with Dolly. 114 // We're going to use it to store a file the contents of which are backed up via key/value. 115 File noBackupDir = context.getNoBackupFilesDir(); 116 return new File(noBackupDir, "key_value_backup_file"); 117 } 118 generateFiles()119 private void generateFiles() { 120 try { 121 // Add data to all the files we created 122 addData(mKeyValueBackupFile); 123 addData(mDoBackupFile); 124 addData(mDoBackupFile2); 125 Log.d(TAG, "Files generated!"); 126 } catch (IOException e) { 127 Log.e(TAG, "Unable to generate files", e); 128 } 129 } 130 deleteAllFiles()131 private void deleteAllFiles() { 132 mKeyValueBackupFile.delete(); 133 mDoBackupFile.delete(); 134 mDoBackupFile2.delete(); 135 Log.d(TAG, "Files deleted!"); 136 } 137 addData(File file)138 private void addData(File file) throws IOException { 139 file.getParentFile().mkdirs(); 140 byte[] bytes = new byte[FILE_SIZE_BYTES]; 141 new Random().nextBytes(bytes); 142 143 try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { 144 bos.write(bytes, 0, bytes.length); 145 } 146 } 147 assertAllFilesExist()148 private void assertAllFilesExist() { 149 assertKeyValueFileExists(); 150 assertDollyFilesExist(); 151 } 152 assertNoFilesExist()153 private void assertNoFilesExist() { 154 assertKeyValueFileDoesntExist(); 155 assertDollyFilesDontExist(); 156 } 157 assertKeyValueFileExists()158 private void assertKeyValueFileExists() { 159 assertTrue("Key/value file did not exist!", mKeyValueBackupFile.exists()); 160 } 161 assertKeyValueFileDoesntExist()162 private void assertKeyValueFileDoesntExist() { 163 assertFalse("Key/value file did exist!", mKeyValueBackupFile.exists()); 164 } 165 assertDollyFilesExist()166 private void assertDollyFilesExist() { 167 assertTrue("File in 'files' did not exist!", mDoBackupFile.exists()); 168 assertTrue("File in folder inside 'files' did not exist!", mDoBackupFile2.exists()); 169 } 170 assertDollyFilesDontExist()171 private void assertDollyFilesDontExist() { 172 assertFalse("File in 'files' did exist!", mDoBackupFile.exists()); 173 assertFalse("File in folder inside 'files' did exist!", mDoBackupFile2.exists()); 174 } 175 } 176