blob: 0c071d1c03efe77e6547678a4e93577ef2063f9c [file] [log] [blame]
Anton Philippovf4057e82017-05-26 15:51:33 +01001/*
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
Ruslan Tkhakokhov9e9e2aa2021-01-11 16:36:38 +000017package android.cts.backup.backupeligibilityapp;
Anton Philippovf4057e82017-05-26 15:51:33 +010018
Brett Chabot244f0382019-02-15 13:02:34 -080019import static androidx.test.InstrumentationRegistry.getTargetContext;
Anton Philippovf4057e82017-05-26 15:51:33 +010020
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
23
24import android.content.Context;
Brett Chabot244f0382019-02-15 13:02:34 -080025import android.platform.test.annotations.AppModeFull;
Anton Philippovf4057e82017-05-26 15:51:33 +010026import android.util.Log;
27
Brett Chabot244f0382019-02-15 13:02:34 -080028import androidx.test.runner.AndroidJUnit4;
29
Anton Philippovf4057e82017-05-26 15:51:33 +010030import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.io.BufferedOutputStream;
35import java.io.File;
36import java.io.FileOutputStream;
37import java.io.IOException;
38import 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)
Bernardo Rufino9754e752018-07-26 14:20:36 +010046@AppModeFull
Ruslan Tkhakokhov9e9e2aa2021-01-11 16:36:38 +000047public class BackupEligibilityTest {
Anton Philippovf4057e82017-05-26 15:51:33 +010048 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
57 public void setUp() {
58 mContext = getTargetContext();
59 setupFiles();
60 }
61
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
71 public void createFiles() throws Exception {
72 // Make sure the data does not exist from before
Ruslan Tkhakokhov9e9e2aa2021-01-11 16:36:38 +000073 deleteFilesAndAssertNoneExist();
Anton Philippovf4057e82017-05-26 15:51:33 +010074
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
Ruslan Tkhakokhov9e9e2aa2021-01-11 16:36:38 +000085 public void deleteFilesAndAssertNoneExist() {
86 deleteAllFiles();
87 assertNoFilesExist();
88 }
89
90 @Test
Anton Philippovf4057e82017-05-26 15:51:33 +010091 public void checkNoFilesExist() throws Exception {
92 assertNoFilesExist();
93 }
94
95 @Test
96 public void checkAllFilesExist() throws Exception {
97 assertAllFilesExist();
98 }
99
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
111 private void deleteAllFiles() {
112 mDoBackupFile.delete();
113 mDoBackupFile2.delete();
114 Log.d(TAG, "Files deleted!");
115 }
116
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
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
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}