• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 AllowBackupTest {
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         deleteAllFiles();
74         assertNoFilesExist();
75 
76         // Create test data
77         generateFiles();
78         assertAllFilesExist();
79 
80         Log.d(TAG, "Test files created: \n"
81                 + mDoBackupFile.getAbsolutePath() + "\n"
82                 + mDoBackupFile2.getAbsolutePath());
83     }
84 
85     @Test
checkNoFilesExist()86     public void checkNoFilesExist() throws Exception {
87         assertNoFilesExist();
88     }
89 
90     @Test
checkAllFilesExist()91     public void checkAllFilesExist() throws Exception {
92         assertAllFilesExist();
93     }
94 
generateFiles()95     private void generateFiles() {
96         try {
97             // Add data to all the files we created
98             addData(mDoBackupFile);
99             addData(mDoBackupFile2);
100             Log.d(TAG, "Files generated!");
101         } catch (IOException e) {
102             Log.e(TAG, "Unable to generate files", e);
103         }
104     }
105 
deleteAllFiles()106     private void deleteAllFiles() {
107         mDoBackupFile.delete();
108         mDoBackupFile2.delete();
109         Log.d(TAG, "Files deleted!");
110     }
111 
addData(File file)112     private void addData(File file) throws IOException {
113         file.getParentFile().mkdirs();
114         byte[] bytes = new byte[FILE_SIZE_BYTES];
115         new Random().nextBytes(bytes);
116 
117         try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
118             bos.write(bytes, 0, bytes.length);
119         }
120     }
121 
assertAllFilesExist()122     private void assertAllFilesExist() {
123         assertTrue("File in 'files' did not exist!", mDoBackupFile.exists());
124         assertTrue("File in folder inside 'files' did not exist!", mDoBackupFile2.exists());
125     }
126 
assertNoFilesExist()127     private void assertNoFilesExist() {
128         assertFalse("File in 'files' did exist!", mDoBackupFile.exists());
129         assertFalse("File in folder inside 'files' did exist!", mDoBackupFile2.exists());
130     }
131 }
132