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.backupeligibilityapp;
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 AllowBackupHostSideTest. These are not
42  * designed to be called in any other way, as they rely on state set up by the host side test.
43  *
44  */
45 @RunWith(AndroidJUnit4.class)
46 @AppModeFull
47 public class BackupEligibilityTest {
48     public static final String TAG = "AllowBackupCTSApp";
49     private static final int FILE_SIZE_BYTES = 1024 * 1024;
50 
51     private Context mContext;
52 
53     private File mDoBackupFile;
54     private File mDoBackupFile2;
55 
56     @Before
setUp()57     public void setUp() {
58         mContext = getTargetContext();
59         setupFiles();
60     }
61 
setupFiles()62     private void setupFiles() {
63         File filesDir = mContext.getFilesDir();
64         File normalFolder = new File(filesDir, "normal_folder");
65 
66         mDoBackupFile = new File(filesDir, "file_to_backup");
67         mDoBackupFile2 = new File(normalFolder, "file_to_backup2");
68     }
69 
70     @Test
createFiles()71     public void createFiles() throws Exception {
72         // Make sure the data does not exist from before
73         deleteFilesAndAssertNoneExist();
74 
75         // Create test data
76         generateFiles();
77         assertAllFilesExist();
78 
79         Log.d(TAG, "Test files created: \n"
80                 + mDoBackupFile.getAbsolutePath() + "\n"
81                 + mDoBackupFile2.getAbsolutePath());
82     }
83 
84     @Test
deleteFilesAndAssertNoneExist()85     public void deleteFilesAndAssertNoneExist() {
86         deleteAllFiles();
87         assertNoFilesExist();
88     }
89 
90     @Test
checkNoFilesExist()91     public void checkNoFilesExist() throws Exception {
92         assertNoFilesExist();
93     }
94 
95     @Test
checkAllFilesExist()96     public void checkAllFilesExist() throws Exception {
97         assertAllFilesExist();
98     }
99 
generateFiles()100     private void generateFiles() {
101         try {
102             // Add data to all the files we created
103             addData(mDoBackupFile);
104             addData(mDoBackupFile2);
105             Log.d(TAG, "Files generated!");
106         } catch (IOException e) {
107             Log.e(TAG, "Unable to generate files", e);
108         }
109     }
110 
deleteAllFiles()111     private void deleteAllFiles() {
112         mDoBackupFile.delete();
113         mDoBackupFile2.delete();
114         Log.d(TAG, "Files deleted!");
115     }
116 
addData(File file)117     private void addData(File file) throws IOException {
118         file.getParentFile().mkdirs();
119         byte[] bytes = new byte[FILE_SIZE_BYTES];
120         new Random().nextBytes(bytes);
121 
122         try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
123             bos.write(bytes, 0, bytes.length);
124         }
125     }
126 
assertAllFilesExist()127     private void assertAllFilesExist() {
128         assertTrue("File in 'files' did not exist!", mDoBackupFile.exists());
129         assertTrue("File in folder inside 'files' did not exist!", mDoBackupFile2.exists());
130     }
131 
assertNoFilesExist()132     private void assertNoFilesExist() {
133         assertFalse("File in 'files' did exist!", mDoBackupFile.exists());
134         assertFalse("File in folder inside 'files' did exist!", mDoBackupFile2.exists());
135     }
136 }
137