summaryrefslogtreecommitdiff
path: root/android/os/StrictModeTest.java
blob: d973c204f89e3bff85aa7f76082d46c8bcca80aa (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
/*
 * Copyright (C) 2017 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 android.os;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;
import com.google.common.util.concurrent.SettableFuture;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class StrictModeTest {
    @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    @Test
    public void timeVmViolation() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
        causeVmViolations(state);
    }

    @Test
    public void timeVmViolationNoStrictMode() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        causeVmViolations(state);
    }

    private static void causeVmViolations(BenchmarkState state) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.parse("content://com.example/foobar"), "image/jpeg");
        final Context context = InstrumentationRegistry.getTargetContext();
        while (state.keepRunning()) {
            context.startActivity(intent);
        }
    }

    @Test
    public void timeThreadViolation() throws IOException {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        StrictMode.setThreadPolicy(
                new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
        causeThreadViolations(state);
    }

    @Test
    public void timeThreadViolationNoStrictMode() throws IOException {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        causeThreadViolations(state);
    }

    private static void causeThreadViolations(BenchmarkState state) throws IOException {
        final File test = File.createTempFile("foo", "bar");
        while (state.keepRunning()) {
            test.exists();
        }
        test.delete();
    }

    @Test
    public void timeCrossBinderThreadViolation() throws Exception {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        StrictMode.setThreadPolicy(
                new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
        causeCrossProcessThreadViolations(state);
    }

    @Test
    public void timeCrossBinderThreadViolationNoStrictMode() throws Exception {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        causeCrossProcessThreadViolations(state);
    }

    private static void causeCrossProcessThreadViolations(BenchmarkState state)
            throws ExecutionException, InterruptedException, RemoteException {
        final Context context = InstrumentationRegistry.getTargetContext();

        SettableFuture<IBinder> binder = SettableFuture.create();
        ServiceConnection connection =
                new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName className, IBinder service) {
                        binder.set(service);
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName arg0) {
                        binder.set(null);
                    }
                };
        context.bindService(
                new Intent(context, SomeService.class), connection, Context.BIND_AUTO_CREATE);
        ISomeService someService = ISomeService.Stub.asInterface(binder.get());
        while (state.keepRunning()) {
            // Violate strictmode heavily.
            someService.readDisk(10);
        }
        context.unbindService(connection);
    }
}