• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 dalvik.system;
18 
19 import junit.framework.TestCase;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 /**
28  * Created by narayan on 1/7/16.
29  */
30 public class BlockGuardTest extends TestCase {
31 
32     private BlockGuard.Policy oldPolicy;
33     private RecordingPolicy recorder = new RecordingPolicy();
34 
35     @Override
setUp()36     public void setUp() {
37         oldPolicy = BlockGuard.getThreadPolicy();
38         BlockGuard.setThreadPolicy(recorder);
39     }
40 
41     @Override
tearDown()42     public void tearDown() {
43         BlockGuard.setThreadPolicy(oldPolicy);
44         recorder.clear();
45     }
46 
testFile()47     public void testFile() throws Exception {
48         File f = File.createTempFile("foo", "bar");
49         recorder.expectAndClear("onReadFromDisk", "onWriteToDisk");
50 
51         f.getAbsolutePath();
52         f.getParentFile();
53         f.getName();
54         f.getParent();
55         f.getPath();
56         f.isAbsolute();
57         recorder.expectNoViolations();
58 
59         f.mkdir();
60         recorder.expectAndClear("onWriteToDisk");
61 
62         f.listFiles();
63         recorder.expectAndClear("onReadFromDisk");
64 
65         f.list();
66         recorder.expectAndClear("onReadFromDisk");
67 
68         f.length();
69         recorder.expectAndClear("onReadFromDisk");
70 
71         f.lastModified();
72         recorder.expectAndClear("onReadFromDisk");
73 
74         f.canExecute();
75         recorder.expectAndClear("onReadFromDisk");
76 
77         f.canRead();
78         recorder.expectAndClear("onReadFromDisk");
79 
80         f.canWrite();
81         recorder.expectAndClear("onReadFromDisk");
82 
83         f.isFile();
84         recorder.expectAndClear("onReadFromDisk");
85 
86         f.isDirectory();
87         recorder.expectAndClear("onReadFromDisk");
88 
89         f.setExecutable(true, false);
90         recorder.expectAndClear("onWriteToDisk");
91 
92         f.setReadable(true, false);
93         recorder.expectAndClear("onWriteToDisk");
94 
95         f.setWritable(true, false);
96         recorder.expectAndClear("onWriteToDisk");
97 
98         f.delete();
99         recorder.expectAndClear("onWriteToDisk");
100     }
101 
testFileInputStream()102     public void testFileInputStream() throws Exception {
103         File f = new File("/proc/version");
104         recorder.clear();
105 
106         FileInputStream fis = new FileInputStream(f);
107         recorder.expectAndClear("onReadFromDisk");
108 
109         fis.read(new byte[4],0, 4);
110         recorder.expectAndClear("onReadFromDisk");
111 
112         fis.read();
113         recorder.expectAndClear("onReadFromDisk");
114 
115         fis.skip(1);
116         recorder.expectAndClear("onReadFromDisk");
117 
118         fis.close();
119     }
120 
testFileOutputStream()121     public void testFileOutputStream() throws Exception {
122         File f = File.createTempFile("foo", "bar");
123         recorder.clear();
124 
125         FileOutputStream fos = new FileOutputStream(f);
126         recorder.expectAndClear("onWriteToDisk");
127 
128         fos.write(new byte[3]);
129         recorder.expectAndClear("onWriteToDisk");
130 
131         fos.write(4);
132         recorder.expectAndClear("onWriteToDisk");
133 
134         fos.flush();
135         recorder.expectNoViolations();
136 
137         fos.close();
138         recorder.expectNoViolations();
139     }
140 
141 
142     public static class RecordingPolicy implements BlockGuard.Policy {
143         private final List<String> violations = new ArrayList<>();
144 
145         @Override
onWriteToDisk()146         public void onWriteToDisk() {
147             addViolation("onWriteToDisk");
148         }
149 
150         @Override
onReadFromDisk()151         public void onReadFromDisk() {
152             addViolation("onReadFromDisk");
153         }
154 
155         @Override
onNetwork()156         public void onNetwork() {
157             addViolation("onNetwork");
158         }
159 
addViolation(String type)160         private void addViolation(String type) {
161             StackTraceElement[] threadTrace = Thread.currentThread().getStackTrace();
162 
163             final StackTraceElement violator = threadTrace[4];
164             violations.add(type + " [caller= " + violator.getMethodName() + "]");
165         }
166 
clear()167         public void clear() {
168             violations.clear();
169         }
170 
expectNoViolations()171         public void expectNoViolations() {
172             if (violations.size() != 0) {
173                 throw new AssertionError("Expected 0 violations but found " + violations.size());
174             }
175         }
176 
expectAndClear(String... expected)177         public void expectAndClear(String... expected) {
178             if (expected.length != violations.size()) {
179                 throw new AssertionError("Expected " + expected.length + " violations but found "
180                         + violations.size());
181             }
182 
183             for (int i = 0; i < expected.length; ++i) {
184                 if (!violations.get(i).startsWith(expected[i])) {
185                     throw new AssertionError("Expected: " + expected[i] + " but was "
186                             + violations.get(i));
187                 }
188             }
189 
190             clear();
191         }
192 
193         @Override
getPolicyMask()194         public int getPolicyMask() {
195             return 0;
196         }
197     }
198 }
199