Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package android.os;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.ServiceConnection;
     23 import android.net.Uri;
     24 import android.perftests.utils.BenchmarkState;
     25 import android.perftests.utils.PerfStatusReporter;
     26 import android.support.test.InstrumentationRegistry;
     27 import android.support.test.filters.LargeTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import com.google.common.util.concurrent.SettableFuture;
     30 import java.io.File;
     31 import java.io.IOException;
     32 import java.util.concurrent.ExecutionException;
     33 import org.junit.Rule;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 
     37 @RunWith(AndroidJUnit4.class)
     38 @LargeTest
     39 public class StrictModeTest {
     40     @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     41 
     42     @Test
     43     public void timeVmViolation() {
     44         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     45         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
     46         causeVmViolations(state);
     47     }
     48 
     49     @Test
     50     public void timeVmViolationNoStrictMode() {
     51         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     52         causeVmViolations(state);
     53     }
     54 
     55     private static void causeVmViolations(BenchmarkState state) {
     56         Intent intent = new Intent(Intent.ACTION_VIEW);
     57         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     58         intent.setDataAndType(Uri.parse("content://com.example/foobar"), "image/jpeg");
     59         final Context context = InstrumentationRegistry.getTargetContext();
     60         while (state.keepRunning()) {
     61             context.startActivity(intent);
     62         }
     63     }
     64 
     65     @Test
     66     public void timeThreadViolation() throws IOException {
     67         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     68         StrictMode.setThreadPolicy(
     69                 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
     70         causeThreadViolations(state);
     71     }
     72 
     73     @Test
     74     public void timeThreadViolationNoStrictMode() throws IOException {
     75         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     76         causeThreadViolations(state);
     77     }
     78 
     79     private static void causeThreadViolations(BenchmarkState state) throws IOException {
     80         final File test = File.createTempFile("foo", "bar");
     81         while (state.keepRunning()) {
     82             test.exists();
     83         }
     84         test.delete();
     85     }
     86 
     87     @Test
     88     public void timeCrossBinderThreadViolation() throws Exception {
     89         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     90         StrictMode.setThreadPolicy(
     91                 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
     92         causeCrossProcessThreadViolations(state);
     93     }
     94 
     95     @Test
     96     public void timeCrossBinderThreadViolationNoStrictMode() throws Exception {
     97         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     98         causeCrossProcessThreadViolations(state);
     99     }
    100 
    101     private static void causeCrossProcessThreadViolations(BenchmarkState state)
    102             throws ExecutionException, InterruptedException, RemoteException {
    103         final Context context = InstrumentationRegistry.getTargetContext();
    104 
    105         SettableFuture<IBinder> binder = SettableFuture.create();
    106         ServiceConnection connection =
    107                 new ServiceConnection() {
    108                     @Override
    109                     public void onServiceConnected(ComponentName className, IBinder service) {
    110                         binder.set(service);
    111                     }
    112 
    113                     @Override
    114                     public void onServiceDisconnected(ComponentName arg0) {
    115                         binder.set(null);
    116                     }
    117                 };
    118         context.bindService(
    119                 new Intent(context, SomeService.class), connection, Context.BIND_AUTO_CREATE);
    120         ISomeService someService = ISomeService.Stub.asInterface(binder.get());
    121         while (state.keepRunning()) {
    122             // Violate strictmode heavily.
    123             someService.readDisk(10);
    124         }
    125         context.unbindService(connection);
    126     }
    127 }
    128