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