• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.app.backup;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.backup.FullBackup.BackupScheme.PathWithRequiredFlags;
22 import android.content.Context;
23 import android.platform.test.annotations.Presubmit;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.robolectric.RobolectricTestRunner;
29 import org.robolectric.RuntimeEnvironment;
30 import org.robolectric.annotation.internal.DoNotInstrument;
31 
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.Collection;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37 
38 @RunWith(RobolectricTestRunner.class)
39 @Presubmit
40 @DoNotInstrument
41 public class BackupUtilsTest {
42     private Context mContext;
43 
44     @Before
setUp()45     public void setUp() throws Exception {
46         mContext = RuntimeEnvironment.application;
47     }
48 
49     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListHasIt()50     public void testIsFileSpecifiedInPathList_whenFileAndPathListHasIt() throws Exception {
51         boolean isSpecified =
52                 BackupUtils.isFileSpecifiedInPathList(file("a/b.txt"), paths(file("a/b.txt")));
53 
54         assertThat(isSpecified).isTrue();
55     }
56 
57     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListHasItsDirectory()58     public void testIsFileSpecifiedInPathList_whenFileAndPathListHasItsDirectory()
59             throws Exception {
60         boolean isSpecified =
61                 BackupUtils.isFileSpecifiedInPathList(file("a/b.txt"), paths(directory("a")));
62 
63         assertThat(isSpecified).isTrue();
64     }
65 
66     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListHasOtherFile()67     public void testIsFileSpecifiedInPathList_whenFileAndPathListHasOtherFile() throws Exception {
68         boolean isSpecified =
69                 BackupUtils.isFileSpecifiedInPathList(file("a/b.txt"), paths(file("a/c.txt")));
70 
71         assertThat(isSpecified).isFalse();
72     }
73 
74     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListEmpty()75     public void testIsFileSpecifiedInPathList_whenFileAndPathListEmpty() throws Exception {
76         boolean isSpecified = BackupUtils.isFileSpecifiedInPathList(file("a/b.txt"), paths());
77 
78         assertThat(isSpecified).isFalse();
79     }
80 
81     @Test
testIsFileSpecifiedInPathList_whenDirectoryAndPathListHasIt()82     public void testIsFileSpecifiedInPathList_whenDirectoryAndPathListHasIt() throws Exception {
83         boolean isSpecified =
84                 BackupUtils.isFileSpecifiedInPathList(directory("a"), paths(directory("a")));
85 
86         assertThat(isSpecified).isTrue();
87     }
88 
89     @Test
testIsFileSpecifiedInPathList_whenDirectoryAndPathListEmpty()90     public void testIsFileSpecifiedInPathList_whenDirectoryAndPathListEmpty() throws Exception {
91         boolean isSpecified = BackupUtils.isFileSpecifiedInPathList(directory("a"), paths());
92 
93         assertThat(isSpecified).isFalse();
94     }
95 
96     @Test
testIsFileSpecifiedInPathList_whenDirectoryAndPathListHasParent()97     public void testIsFileSpecifiedInPathList_whenDirectoryAndPathListHasParent() throws Exception {
98         boolean isSpecified =
99                 BackupUtils.isFileSpecifiedInPathList(directory("a/b"), paths(directory("a")));
100 
101         assertThat(isSpecified).isFalse();
102     }
103 
104     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListDoesntContainDirectory()105     public void testIsFileSpecifiedInPathList_whenFileAndPathListDoesntContainDirectory()
106             throws Exception {
107         boolean isSpecified =
108                 BackupUtils.isFileSpecifiedInPathList(file("a/b.txt"), paths(directory("c")));
109 
110         assertThat(isSpecified).isFalse();
111     }
112 
113     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListHasDirectoryWhoseNameIsPrefix()114     public void testIsFileSpecifiedInPathList_whenFileAndPathListHasDirectoryWhoseNameIsPrefix()
115             throws Exception {
116         boolean isSpecified =
117                 BackupUtils.isFileSpecifiedInPathList(file("a/b.txt"), paths(directory("a/b")));
118 
119         assertThat(isSpecified).isFalse();
120     }
121 
122     @Test
testIsFileSpecifiedInPathList_whenFileAndPathListHasDirectoryWhoseNameIsPrefix2()123     public void testIsFileSpecifiedInPathList_whenFileAndPathListHasDirectoryWhoseNameIsPrefix2()
124             throws Exception {
125         boolean isSpecified =
126                 BackupUtils.isFileSpecifiedInPathList(
127                         file("name/subname.txt"), paths(directory("nam")));
128 
129         assertThat(isSpecified).isFalse();
130     }
131 
132     @Test
133     public void
testIsFileSpecifiedInPathList_whenFileAndPathListContainsFirstNotRelatedAndSecondContainingDirectory()134             testIsFileSpecifiedInPathList_whenFileAndPathListContainsFirstNotRelatedAndSecondContainingDirectory()
135                     throws Exception {
136         boolean isSpecified =
137                 BackupUtils.isFileSpecifiedInPathList(
138                         file("a/b.txt"), paths(directory("b"), directory("a")));
139 
140         assertThat(isSpecified).isTrue();
141     }
142 
143     @Test
144     public void
testIsFileSpecifiedInPathList_whenDirectoryAndPathListContainsFirstNotRelatedAndSecondSameDirectory()145             testIsFileSpecifiedInPathList_whenDirectoryAndPathListContainsFirstNotRelatedAndSecondSameDirectory()
146                     throws Exception {
147         boolean isSpecified =
148                 BackupUtils.isFileSpecifiedInPathList(
149                         directory("a/b"), paths(directory("b"), directory("a/b")));
150 
151         assertThat(isSpecified).isTrue();
152     }
153 
154     @Test
155     public void
testIsFileSpecifiedInPathList_whenFileAndPathListContainsFirstNotRelatedFileAndSecondSameFile()156             testIsFileSpecifiedInPathList_whenFileAndPathListContainsFirstNotRelatedFileAndSecondSameFile()
157                     throws Exception {
158         boolean isSpecified =
159                 BackupUtils.isFileSpecifiedInPathList(
160                         file("a/b.txt"), paths(directory("b"), file("a/b.txt")));
161 
162         assertThat(isSpecified).isTrue();
163     }
164 
file(String path)165     private File file(String path) throws IOException {
166         File file = new File(mContext.getDataDir(), path);
167         File parent = file.getParentFile();
168         parent.mkdirs();
169         file.createNewFile();
170         if (!file.isFile()) {
171             throw new IOException("Couldn't create file");
172         }
173         return file;
174     }
175 
directory(String path)176     private File directory(String path) throws IOException {
177         File directory = new File(mContext.getDataDir(), path);
178         directory.mkdirs();
179         if (!directory.isDirectory()) {
180             throw new IOException("Couldn't create directory");
181         }
182         return directory;
183     }
184 
paths(File... files)185     private Collection<PathWithRequiredFlags> paths(File... files) {
186         return Stream.of(files)
187                 .map(file -> new PathWithRequiredFlags(file.getPath(), 0))
188                 .collect(Collectors.toList());
189     }
190 }
191