summaryrefslogtreecommitdiff
path: root/luni/src/test/java/libcore/dalvik/system/BlockGuardTest.java
blob: 440fd40457c131f7746aa7ae42f0d112f0a7e460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package libcore.dalvik.system;

import android.system.Os;
import android.system.OsConstants;
import junit.framework.TestCase;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import dalvik.system.BlockGuard;

public class BlockGuardTest extends TestCase {

    private BlockGuard.Policy oldPolicy;
    private RecordingPolicy recorder = new RecordingPolicy();

    @Override
    public void setUp() {
        recorder.setChecks(EnumSet.allOf(RecordingPolicy.Check.class));
        oldPolicy = BlockGuard.getThreadPolicy();
        BlockGuard.setThreadPolicy(recorder);
    }

    @Override
    public void tearDown() {
        BlockGuard.setThreadPolicy(oldPolicy);
        recorder.clear();
    }

    public void testFile() throws Exception {
        File f = File.createTempFile("foo", "bar");
        recorder.expectAndClear("onReadFromDisk", "onWriteToDisk");

        f.getAbsolutePath();
        f.getParentFile();
        f.getName();
        f.getParent();
        f.getPath();
        f.isAbsolute();
        recorder.expectNoViolations();

        f.mkdir();
        recorder.expectAndClear("onWriteToDisk");

        f.listFiles();
        recorder.expectAndClear("onReadFromDisk");

        f.list();
        recorder.expectAndClear("onReadFromDisk");

        f.length();
        recorder.expectAndClear("onReadFromDisk");

        f.lastModified();
        recorder.expectAndClear("onReadFromDisk");

        f.canExecute();
        recorder.expectAndClear("onReadFromDisk");

        f.canRead();
        recorder.expectAndClear("onReadFromDisk");

        f.canWrite();
        recorder.expectAndClear("onReadFromDisk");

        f.isFile();
        recorder.expectAndClear("onReadFromDisk");

        f.isDirectory();
        recorder.expectAndClear("onReadFromDisk");

        f.setExecutable(true, false);
        recorder.expectAndClear("onWriteToDisk");

        f.setReadable(true, false);
        recorder.expectAndClear("onWriteToDisk");

        f.setWritable(true, false);
        recorder.expectAndClear("onWriteToDisk");

        f.delete();
        recorder.expectAndClear("onWriteToDisk");
    }

    public void testFileInputStream() throws Exception {
        // The file itself doesn't matter: it just has to exist and allow the creation of the
        // FileInputStream. The BlockGuard should have the same behavior towards a normal file and
        // system file.
        File tmpFile = File.createTempFile("inputFile", ".txt");
        try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
            fos.write("01234567890".getBytes());
        }

        try {
            recorder.clear();

            // Opening a file for read triggers:
            // 1. Read violation from open()
            // 2. Read violation from EISDIR check
            FileInputStream fis = new FileInputStream(tmpFile);
            recorder.expectAndClear("onReadFromDisk", "onReadFromDisk");

            fis.read(new byte[4], 0, 4);
            recorder.expectAndClear("onReadFromDisk");

            fis.read();
            recorder.expectAndClear("onReadFromDisk");

            fis.skip(1);
            recorder.expectAndClear("onReadFromDisk");

            fis.close();
        } finally {
            tmpFile.delete();
        }
    }

    public void testFileOutputStream() throws Exception {
        File f = File.createTempFile("foo", "bar");
        recorder.clear();

        // Opening a file for write triggers:
        // 1. Read violation from open()
        // 2. Write violation from open()
        // 3. Read violation from EISDIR check
        FileOutputStream fos = new FileOutputStream(f);
        recorder.expectAndClear("onReadFromDisk", "onWriteToDisk", "onReadFromDisk");

        fos.write(new byte[3]);
        recorder.expectAndClear("onWriteToDisk");

        fos.write(4);
        recorder.expectAndClear("onWriteToDisk");

        fos.flush();
        recorder.expectNoViolations();

        fos.close();
        recorder.expectNoViolations();
    }

    public void testRandomAccessFile() throws Exception {
        File f = File.createTempFile("foo", "bar");
        recorder.clear();

        // Opening a file for write triggers:
        // 1. Read violation from open()
        // 2. Write violation from open()
        // 3. Read violation from EISDIR check
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        recorder.expectAndClear("onReadFromDisk", "onWriteToDisk", "onReadFromDisk");

        raf.seek(0);
        recorder.expectAndClear("onReadFromDisk");

        raf.length();
        recorder.expectAndClear("onReadFromDisk");

        raf.read();
        recorder.expectAndClear("onReadFromDisk");

        raf.write(42);
        recorder.expectAndClear("onWriteToDisk");

        raf.close();
    }

    public void testUnbufferedIO() throws Exception {
        File f = File.createTempFile("foo", "bar");
        recorder.setChecks(EnumSet.of(RecordingPolicy.Check.UNBUFFERED_IO));
        recorder.clear();

        try (FileOutputStream fos = new FileOutputStream(f)) {
            recorder.expectNoViolations();
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                fos.write("a".getBytes());
            }
            recorder.expectAndClear("onUnbufferedIO");
        }

        try (FileInputStream fis = new FileInputStream(new File("/dev/null"))) {
            recorder.expectNoViolations();
            byte[] b = new byte[1];
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                fis.read(b);
            }
            recorder.expectAndClear("onUnbufferedIO");
        }

        try (RandomAccessFile ras = new RandomAccessFile(f, "rw")) {
            // seek should reset the IoTracker.
            ras.seek(0);
            recorder.expectNoViolations();
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                ras.read("a".getBytes());
            }
            recorder.expectAndClear("onUnbufferedIO");
        }

        try (RandomAccessFile ras = new RandomAccessFile(f, "rw")) {
            // No violation is expected as a write is called while reading which should reset the
            // IoTracker counter.
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                if (i == 5) {
                    ras.write("a".getBytes());
                }
                ras.read("a".getBytes());
            }
            recorder.expectNoViolations();
        }

        try (RandomAccessFile ras = new RandomAccessFile(f, "rw")) {
            // No violation is expected as a seek is called while reading which should reset the
            // IoTracker counter.
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                if (i == 5) {
                    ras.seek(0);
                }
                ras.read("a".getBytes());
            }
            recorder.expectNoViolations();
        }

        try (RandomAccessFile ras = new RandomAccessFile(f, "rw")) {
            // seek should reset the IoTracker.
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                ras.write("a".getBytes());
            }
            recorder.expectAndClear("onUnbufferedIO");
        }

        try (RandomAccessFile ras = new RandomAccessFile(f, "rw")) {
            // No violation is expected as a read is called while writing which should reset the
            // IoTracker counter.
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                if (i == 5) {
                    ras.read("a".getBytes());
                }
                ras.write("a".getBytes());
            }
            recorder.expectNoViolations();
        }

        try (RandomAccessFile ras = new RandomAccessFile(f, "rw")) {
            for (int i = 0; i < 11; i++) {
                recorder.expectNoViolations();
                if (i == 5) {
                    ras.seek(0);
                }
                ras.write("a".getBytes());
            }
            recorder.expectNoViolations();
        }
    }

    public void testOpen() throws Exception {
        File temp = File.createTempFile("foo", "bar");
        recorder.clear();

        // Open in read/write mode : should be recorded as a read and a write to disk.
        FileDescriptor fd = Os.open(temp.getPath(), OsConstants.O_RDWR, 0);
        recorder.expectAndClear("onReadFromDisk", "onWriteToDisk");
        Os.close(fd);

        // Open in read only mode : should be recorded as a read from disk.
        recorder.clear();
        fd = Os.open(temp.getPath(), OsConstants.O_RDONLY, 0);
        recorder.expectAndClear("onReadFromDisk");
        Os.close(fd);
    }

    public void testSystemGc() throws Exception {
        recorder.clear();
        Runtime.getRuntime().gc();
        recorder.expectAndClear("onExplicitGc");
    }

    private static class RecordingPolicy implements BlockGuard.Policy {
        private final List<String> violations = new ArrayList<>();
        private Set<Check> checksList;

        public enum Check {
            WRITE_TO_DISK,
            READ_FROM_DISK,
            NETWORK,
            UNBUFFERED_IO,
            EXPLICIT_GC,
        }

        public void setChecks(EnumSet<Check> checksList) {
            this.checksList = checksList;
        }

        @Override
        public void onWriteToDisk() {
            if (checksList != null && checksList.contains(Check.WRITE_TO_DISK)) {
                addViolation("onWriteToDisk");
            }
        }

        @Override
        public void onReadFromDisk() {
            if (checksList != null && checksList.contains(Check.READ_FROM_DISK)) {
                addViolation("onReadFromDisk");
            }
        }

        @Override
        public void onNetwork() {
            if (checksList != null && checksList.contains(Check.NETWORK)) {
                addViolation("onNetwork");
            }
        }

        @Override
        public void onUnbufferedIO() {
            if (checksList != null && checksList.contains(Check.UNBUFFERED_IO)) {
                addViolation("onUnbufferedIO");
            }
        }

        @Override
        public void onExplicitGc() {
            if (checksList != null && checksList.contains(Check.EXPLICIT_GC)) {
                addViolation("onExplicitGc");
            }
        }

        private void addViolation(String type) {
            StackTraceElement[] threadTrace = Thread.currentThread().getStackTrace();

            final StackTraceElement violator = threadTrace[4];
            violations.add(type + " [caller= " + violator.getMethodName() + "]");
        }

        public void clear() {
            violations.clear();
        }

        public void expectNoViolations() {
            if (violations.size() != 0) {
                throw new AssertionError("Expected 0 violations but found " + violations.size());
            }
        }

        public void expectAndClear(String... expected) {
            if (expected.length != violations.size()) {
                throw new AssertionError("Expected " + expected.length + " violations but found "
                        + violations.size());
            }

            for (int i = 0; i < expected.length; ++i) {
                if (!violations.get(i).startsWith(expected[i])) {
                    throw new AssertionError("Expected: " + expected[i] + " but was "
                            + violations.get(i));
                }
            }

            clear();
        }

        @Override
        public int getPolicyMask() {
            return 0;
        }
    }
}