1 /*
2  * Copyright (C) 2018 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.appsecurity.cts;
18 
19 import android.platform.test.annotations.AppModeFull;
20 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
21 
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 /**
32  * Tests the visibility of installed applications.
33  */
34 @RunWith(DeviceJUnit4ClassRunner.class)
35 public class ApplicationVisibilityTest extends BaseAppSecurityTest {
36 
37     private static final String TINY_APK = "CtsPkgInstallTinyApp.apk";
38     private static final String TINY_PKG = "android.appsecurity.cts.tinyapp";
39 
40     private static final String TEST_WITH_PERMISSION_APK =
41             "CtsApplicationVisibilityCrossUserApp.apk";
42     private static final String TEST_WITH_PERMISSION_PKG =
43             "com.android.cts.applicationvisibility";
44 
45     private int[] mUsers;
46     private String mOldVerifierValue;
47 
48     @Before
setUpPackage()49     public void setUpPackage() throws Exception {
50         mUsers = Utils.prepareMultipleUsers(getDevice(), 2);
51         mOldVerifierValue =
52                 getDevice().executeShellCommand("settings get global package_verifier_enable");
53         getDevice().executeShellCommand("settings put global package_verifier_enable 0");
54     }
55 
56     @After
tearDown()57     public void tearDown() throws Exception {
58         getDevice().uninstallPackage(TEST_WITH_PERMISSION_PKG);
59         getDevice().uninstallPackage(TINY_PKG);
60         getDevice().executeShellCommand("settings put global package_verifier_enable "
61                 + mOldVerifierValue);
62     }
63 
64     @Test
65     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testPackageListCrossUserGrant()66     public void testPackageListCrossUserGrant() throws Exception {
67         if (!mSupportsMultiUser) {
68             return;
69         }
70 
71         final int installUserId = getInstallUserId();
72         final int testUserId = getTestUserId();
73 
74         installTestAppForUser(TINY_APK, installUserId);
75         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
76 
77         Utils.runDeviceTests(
78                 getDevice(),
79                 TEST_WITH_PERMISSION_PKG,
80                 ".ApplicationVisibilityCrossUserTest",
81                 "testPackageVisibility_currentUser",
82                 testUserId);
83         Utils.runDeviceTests(
84                 getDevice(),
85                 TEST_WITH_PERMISSION_PKG,
86                 ".ApplicationVisibilityCrossUserTest",
87                 "testPackageVisibility_anyUserCrossUserGrant",
88                 testUserId);
89     }
90 
91     @Test
92     @AppModeFull(reason = "instant applications cannot see any other application")
testPackageListCrossUserNoGrant()93     public void testPackageListCrossUserNoGrant() throws Exception {
94         if (!mSupportsMultiUser) {
95             return;
96         }
97 
98         final int installUserId = getInstallUserId();
99         final int testUserId = getTestUserId();
100 
101         installTestAppForUser(TINY_APK, installUserId);
102         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
103 
104         Utils.runDeviceTests(
105                 getDevice(),
106                 TEST_WITH_PERMISSION_PKG,
107                 ".ApplicationVisibilityCrossUserTest",
108                 "testPackageVisibility_currentUser",
109                 testUserId);
110         Utils.runDeviceTests(
111                 getDevice(),
112                 TEST_WITH_PERMISSION_PKG,
113                 ".ApplicationVisibilityCrossUserTest",
114                 "testPackageVisibility_anyUserCrossUserNoGrant",
115                 testUserId);
116     }
117 
118     @Test
119     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testPackageListOtherUserCrossUserGrant()120     public void testPackageListOtherUserCrossUserGrant() throws Exception {
121         if (!mSupportsMultiUser) {
122             return;
123         }
124 
125         final int installUserId = getInstallUserId();
126         final int testUserId = getTestUserId();
127         final Map<String, String> testArgs = new HashMap<>();
128         testArgs.put("testUser", Integer.toString(installUserId));
129 
130         installTestAppForUser(TINY_APK, installUserId);
131         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
132 
133         Utils.runDeviceTests(
134                 getDevice(),
135                 TEST_WITH_PERMISSION_PKG,
136                 ".ApplicationVisibilityCrossUserTest",
137                 "testPackageVisibility_otherUserGrant",
138                 testUserId,
139                 testArgs);
140     }
141 
142     @Test
143     @AppModeFull(reason = "instant applications cannot see any other application")
testPackageListOtherUserCrossUserNoGrant()144     public void testPackageListOtherUserCrossUserNoGrant() throws Exception {
145         if (!mSupportsMultiUser) {
146             return;
147         }
148 
149         final int installUserId = getInstallUserId();
150         final int testUserId = getTestUserId();
151         final Map<String, String> testArgs = new HashMap<>();
152         testArgs.put("testUser", Integer.toString(installUserId));
153 
154         installTestAppForUser(TINY_APK, installUserId);
155         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
156 
157         Utils.runDeviceTests(
158                 getDevice(),
159                 TEST_WITH_PERMISSION_PKG,
160                 ".ApplicationVisibilityCrossUserTest",
161                 "testPackageVisibility_otherUserNoGrant",
162                 testUserId,
163                 testArgs);
164     }
165 
166     @Test
167     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testApplicationListCrossUserGrant()168     public void testApplicationListCrossUserGrant() throws Exception {
169         if (!mSupportsMultiUser) {
170             return;
171         }
172 
173         final int installUserId = getInstallUserId();
174         final int testUserId = getTestUserId();
175 
176         installTestAppForUser(TINY_APK, installUserId);
177         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
178 
179         Utils.runDeviceTests(
180                 getDevice(),
181                 TEST_WITH_PERMISSION_PKG,
182                 ".ApplicationVisibilityCrossUserTest",
183                 "testApplicationVisibility_currentUser",
184                 testUserId);
185         Utils.runDeviceTests(
186                 getDevice(),
187                 TEST_WITH_PERMISSION_PKG,
188                 ".ApplicationVisibilityCrossUserTest",
189                 "testApplicationVisibility_anyUserCrossUserGrant",
190                 testUserId);
191     }
192 
193     @Test
194     @AppModeFull(reason = "instant applications cannot see any other application")
testApplicationListCrossUserNoGrant()195     public void testApplicationListCrossUserNoGrant() throws Exception {
196         if (!mSupportsMultiUser) {
197             return;
198         }
199 
200         final int installUserId = getInstallUserId();
201         final int testUserId = getTestUserId();
202 
203         installTestAppForUser(TINY_APK, installUserId);
204         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
205 
206         Utils.runDeviceTests(
207                 getDevice(),
208                 TEST_WITH_PERMISSION_PKG,
209                 ".ApplicationVisibilityCrossUserTest",
210                 "testApplicationVisibility_currentUser",
211                 testUserId);
212         Utils.runDeviceTests(
213                 getDevice(),
214                 TEST_WITH_PERMISSION_PKG,
215                 ".ApplicationVisibilityCrossUserTest",
216                 "testApplicationVisibility_anyUserCrossUserNoGrant",
217                 testUserId);
218     }
219 
220     @Test
221     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testApplicationListOtherUserCrossUserGrant()222     public void testApplicationListOtherUserCrossUserGrant() throws Exception {
223         if (!mSupportsMultiUser) {
224             return;
225         }
226 
227         final int installUserId = getInstallUserId();
228         final int testUserId = getTestUserId();
229         final Map<String, String> testArgs = new HashMap<>();
230         testArgs.put("testUser", Integer.toString(installUserId));
231 
232         installTestAppForUser(TINY_APK, installUserId);
233         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
234 
235         Utils.runDeviceTests(
236                 getDevice(),
237                 TEST_WITH_PERMISSION_PKG,
238                 ".ApplicationVisibilityCrossUserTest",
239                 "testApplicationVisibility_otherUserGrant",
240                 testUserId,
241                 testArgs);
242     }
243 
244     @Test
245     @AppModeFull(reason = "instant applications cannot see any other application")
testApplicationListOtherUserCrossUserNoGrant()246     public void testApplicationListOtherUserCrossUserNoGrant() throws Exception {
247         if (!mSupportsMultiUser) {
248             return;
249         }
250 
251         final int installUserId = getInstallUserId();
252         final int testUserId = getTestUserId();
253         final Map<String, String> testArgs = new HashMap<>();
254         testArgs.put("testUser", Integer.toString(installUserId));
255 
256         installTestAppForUser(TINY_APK, installUserId);
257         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
258 
259         Utils.runDeviceTests(
260                 getDevice(),
261                 TEST_WITH_PERMISSION_PKG,
262                 ".ApplicationVisibilityCrossUserTest",
263                 "testApplicationVisibility_otherUserNoGrant",
264                 testUserId,
265                 testArgs);
266     }
267 
268     @Test
269     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testGetPackagesForUidCrossUserGrant()270     public void testGetPackagesForUidCrossUserGrant() throws Exception {
271         if (!mSupportsMultiUser) {
272             return;
273         }
274 
275         final int installUserId = getInstallUserId();
276         final int testUserId = getTestUserId();
277 
278         installTestAppForUser(TINY_APK, installUserId);
279         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
280 
281         Utils.runDeviceTests(
282                 getDevice(),
283                 TEST_WITH_PERMISSION_PKG,
284                 ".ApplicationVisibilityCrossUserTest",
285                 "testGetPackagesForUidVisibility_currentUser",
286                 testUserId);
287         Utils.runDeviceTests(
288                 getDevice(),
289                 TEST_WITH_PERMISSION_PKG,
290                 ".ApplicationVisibilityCrossUserTest",
291                 "testGetPackagesForUidVisibility_anotherUserCrossUserGrant",
292                 testUserId);
293     }
294 
295     @Test
296     @AppModeFull(reason = "instant applications cannot see any other application")
testGetPackagesForUidCrossUserNoGrant()297     public void testGetPackagesForUidCrossUserNoGrant() throws Exception {
298         if (!mSupportsMultiUser) {
299             return;
300         }
301 
302         final int installUserId = getInstallUserId();
303         final int testUserId = getTestUserId();
304 
305         installTestAppForUser(TINY_APK, installUserId);
306         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
307 
308         Utils.runDeviceTests(
309                 getDevice(),
310                 TEST_WITH_PERMISSION_PKG,
311                 ".ApplicationVisibilityCrossUserTest",
312                 "testGetPackagesForUidVisibility_currentUser",
313                 testUserId);
314         Utils.runDeviceTests(
315                 getDevice(),
316                 TEST_WITH_PERMISSION_PKG,
317                 ".ApplicationVisibilityCrossUserTest",
318                 "testGetPackagesForUidVisibility_anotherUserCrossUserNoGrant",
319                 testUserId);
320     }
321 
getInstallUserId()322     private int getInstallUserId() {
323         return mUsers[0];
324     }
325 
getTestUserId()326     private int getTestUserId() {
327         return mUsers[1];
328     }
329 }
330