1 /*
2  * Copyright (C) 2020 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 android.scopedstorage.cts.host;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import android.platform.test.annotations.AppModeFull;
22 
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 /**
31  * Runs the core ScopedStorageTest tests.
32  */
33 @RunWith(DeviceJUnit4ClassRunner.class)
34 @AppModeFull
35 public class ScopedStorageCoreHostTest extends BaseHostTestCase {
36     private boolean mIsExternalStorageSetup = false;
37 
38     /**
39      * Runs the given phase of ScopedStorageTest by calling into the device.
40      * Throws an exception if the test phase fails.
41      */
runDeviceTest(String phase)42     void runDeviceTest(String phase) throws Exception {
43         assertTrue(runDeviceTests("android.scopedstorage.cts",
44                 "android.scopedstorage.cts.ScopedStorageTest", phase));
45 
46     }
47 
setupExternalStorage()48     private void setupExternalStorage() throws Exception {
49         if (!mIsExternalStorageSetup) {
50             runDeviceTest("setupExternalStorage");
51             mIsExternalStorageSetup = true;
52         }
53     }
54 
55     @Before
setup()56     public void setup() throws Exception {
57         setupExternalStorage();
58         executeShellCommand("mkdir /sdcard/Android/data/com.android.shell -m 2770");
59         executeShellCommand("mkdir /sdcard/Android/data/com.android.shell/files -m 2770");
60     }
61 
62     @Before
revokeStoragePermissions()63     public void revokeStoragePermissions() throws Exception {
64         revokePermissions("android.permission.WRITE_EXTERNAL_STORAGE",
65                 "android.permission.READ_EXTERNAL_STORAGE");
66     }
67 
68     @After
tearDown()69     public void tearDown() throws Exception {
70         executeShellCommand("rm -r /sdcard/Android/data/com.android.shell");
71     }
72 
73     @Test
testManageExternalStorageCanCreateFilesAnywhere()74     public void testManageExternalStorageCanCreateFilesAnywhere() throws Exception {
75         allowAppOps("android:manage_external_storage");
76         try {
77             runDeviceTest("testManageExternalStorageCanCreateFilesAnywhere");
78         } finally {
79             denyAppOps("android:manage_external_storage");
80         }
81     }
82 
83     @Test
testManageExternalStorageReaddir()84     public void testManageExternalStorageReaddir() throws Exception {
85         allowAppOps("android:manage_external_storage");
86         try {
87             runDeviceTest("testManageExternalStorageReaddir");
88         } finally {
89             denyAppOps("android:manage_external_storage");
90         }
91     }
92 
93     @Test
testAccess_file()94     public void testAccess_file() throws Exception {
95         grantPermissions("android.permission.READ_EXTERNAL_STORAGE");
96         try {
97             runDeviceTest("testAccess_file");
98         } finally {
99             revokePermissions("android.permission.READ_EXTERNAL_STORAGE");
100         }
101     }
102 
103     @Test
testAccess_directory()104     public void testAccess_directory() throws Exception {
105         grantPermissions("android.permission.READ_EXTERNAL_STORAGE",
106                 "android.permission.WRITE_EXTERNAL_STORAGE");
107         try {
108             runDeviceTest("testAccess_directory");
109         } finally {
110             revokePermissions("android.permission.READ_EXTERNAL_STORAGE",
111                     "android.permission.WRITE_EXTERNAL_STORAGE");
112         }
113     }
114 
grantPermissions(String... perms)115     private void grantPermissions(String... perms) throws Exception {
116         int currentUserId = getCurrentUserId();
117         for (String perm : perms) {
118             executeShellCommand("pm grant --user %d android.scopedstorage.cts %s",
119                     currentUserId, perm);
120         }
121     }
122 
revokePermissions(String... perms)123     private void revokePermissions(String... perms) throws Exception {
124         int currentUserId = getCurrentUserId();
125         for (String perm : perms) {
126             executeShellCommand("pm revoke --user %d android.scopedstorage.cts %s",
127                     currentUserId, perm);
128         }
129     }
130 
allowAppOps(String... ops)131     private void allowAppOps(String... ops) throws Exception {
132         for (String op : ops) {
133             executeShellCommand("cmd appops set --uid android.scopedstorage.cts %s allow", op);
134         }
135     }
136 
denyAppOps(String... ops)137     private void denyAppOps(String... ops) throws Exception {
138         for (String op : ops) {
139             executeShellCommand("cmd appops set --uid android.scopedstorage.cts %s deny", op);
140         }
141     }
142 }
143