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