1 /*
2  * Copyright (C) 2015 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 com.android.cts.usepermission;
18 
19 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertDirNoAccess;
20 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertDirReadWriteAccess;
21 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertMediaNoAccess;
22 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertMediaReadWriteAccess;
23 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.getAllPackageSpecificPaths;
24 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.logCommand;
25 
26 import android.content.Context;
27 import android.content.pm.PackageManager;
28 import android.os.Environment;
29 import android.os.Process;
30 import android.test.InstrumentationTestCase;
31 
32 import java.io.File;
33 
34 public class UsePermissionCompatTest extends InstrumentationTestCase {
35     private static final String TAG = "UsePermissionTest";
36 
testCompatDefault()37     public void testCompatDefault() throws Exception {
38         final Context context = getInstrumentation().getContext();
39         logCommand("/system/bin/cat", "/proc/self/mountinfo");
40 
41         // Legacy permission model is granted by default
42         assertEquals(PackageManager.PERMISSION_GRANTED,
43                 context.checkPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE,
44                         Process.myPid(), Process.myUid()));
45         assertEquals(PackageManager.PERMISSION_GRANTED,
46                 context.checkPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
47                         Process.myPid(), Process.myUid()));
48         assertEquals(Environment.MEDIA_MOUNTED, Environment.getExternalStorageState());
49         assertDirReadWriteAccess(Environment.getExternalStorageDirectory());
50         for (File path : getAllPackageSpecificPaths(context)) {
51             if (path != null) {
52                 assertDirReadWriteAccess(path);
53             }
54         }
55         assertMediaReadWriteAccess(getInstrumentation().getContext().getContentResolver());
56     }
57 
testCompatRevoked()58     public void testCompatRevoked() throws Exception {
59         final Context context = getInstrumentation().getContext();
60         logCommand("/system/bin/cat", "/proc/self/mountinfo");
61 
62         // Legacy permission model appears granted, but storage looks and
63         // behaves like it's ejected
64         assertEquals(PackageManager.PERMISSION_GRANTED,
65                 context.checkPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE,
66                         Process.myPid(), Process.myUid()));
67         assertEquals(PackageManager.PERMISSION_GRANTED,
68                 context.checkPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
69                         Process.myPid(), Process.myUid()));
70         assertEquals(Environment.MEDIA_UNMOUNTED, Environment.getExternalStorageState());
71         assertDirNoAccess(Environment.getExternalStorageDirectory());
72         for (File dir : getAllPackageSpecificPaths(context)) {
73             if (dir != null) {
74                 assertDirNoAccess(dir);
75             }
76         }
77         assertMediaNoAccess(getInstrumentation().getContext().getContentResolver());
78 
79         // Just to be sure, poke explicit path
80         assertDirNoAccess(new File(Environment.getExternalStorageDirectory(),
81                 "/Android/data/" + getInstrumentation().getContext().getPackageName()));
82     }
83 }
84