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 android.platform.test.annotations.Presubmit;
21 
22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
23 
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 /**
33  * Tests the visibility of installed applications.
34  */
35 @Presubmit
36 @RunWith(DeviceJUnit4ClassRunner.class)
37 public class ApplicationVisibilityTest extends BaseAppSecurityTest {
38 
39     private static final String TINY_APK = "CtsPkgInstallTinyApp.apk";
40     private static final String TINY_PKG = "android.appsecurity.cts.tinyapp";
41 
42     private static final String TEST_WITH_PERMISSION_APK =
43             "CtsApplicationVisibilityCrossUserApp.apk";
44     private static final String TEST_WITH_PERMISSION_PKG =
45             "com.android.cts.applicationvisibility";
46 
47     private int[] mUsers;
48     private String mOldVerifierValue;
49 
50     @Before
setUpPackage()51     public void setUpPackage() throws Exception {
52         mUsers = Utils.prepareMultipleUsers(getDevice(), 2);
53         mOldVerifierValue =
54                 getDevice().executeShellCommand("settings get global package_verifier_enable");
55         getDevice().executeShellCommand("settings put global package_verifier_enable 0");
56     }
57 
58     @After
tearDown()59     public void tearDown() throws Exception {
60         getDevice().uninstallPackage(TEST_WITH_PERMISSION_PKG);
61         getDevice().uninstallPackage(TINY_PKG);
62         getDevice().executeShellCommand("settings put global package_verifier_enable "
63                 + mOldVerifierValue);
64     }
65 
66     @Test
67     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testPackageListCrossUserGrant()68     public void testPackageListCrossUserGrant() throws Exception {
69         if (!mSupportsMultiUser) {
70             return;
71         }
72 
73         final int installUserId = getInstallUserId();
74         final int testUserId = getTestUserId();
75 
76         installTestAppForUser(TINY_APK, installUserId);
77         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
78 
79         Utils.runDeviceTests(
80                 getDevice(),
81                 TEST_WITH_PERMISSION_PKG,
82                 ".ApplicationVisibilityCrossUserTest",
83                 "testPackageVisibility_currentUser",
84                 testUserId);
85         Utils.runDeviceTests(
86                 getDevice(),
87                 TEST_WITH_PERMISSION_PKG,
88                 ".ApplicationVisibilityCrossUserTest",
89                 "testPackageVisibility_anyUserCrossUserGrant",
90                 testUserId);
91     }
92 
93     @Test
94     @AppModeFull(reason = "instant applications cannot see any other application")
testPackageListCrossUserNoGrant()95     public void testPackageListCrossUserNoGrant() throws Exception {
96         if (!mSupportsMultiUser) {
97             return;
98         }
99 
100         final int installUserId = getInstallUserId();
101         final int testUserId = getTestUserId();
102 
103         installTestAppForUser(TINY_APK, installUserId);
104         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
105 
106         Utils.runDeviceTests(
107                 getDevice(),
108                 TEST_WITH_PERMISSION_PKG,
109                 ".ApplicationVisibilityCrossUserTest",
110                 "testPackageVisibility_currentUser",
111                 testUserId);
112         Utils.runDeviceTests(
113                 getDevice(),
114                 TEST_WITH_PERMISSION_PKG,
115                 ".ApplicationVisibilityCrossUserTest",
116                 "testPackageVisibility_anyUserCrossUserNoGrant",
117                 testUserId);
118     }
119 
120     @Test
121     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testPackageListOtherUserCrossUserGrant()122     public void testPackageListOtherUserCrossUserGrant() throws Exception {
123         if (!mSupportsMultiUser) {
124             return;
125         }
126 
127         final int installUserId = getInstallUserId();
128         final int testUserId = getTestUserId();
129         final Map<String, String> testArgs = new HashMap<>();
130         testArgs.put("installUser", Integer.toString(installUserId));
131 
132         installTestAppForUser(TINY_APK, installUserId);
133         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
134 
135         Utils.runDeviceTests(
136                 getDevice(),
137                 TEST_WITH_PERMISSION_PKG,
138                 ".ApplicationVisibilityCrossUserTest",
139                 "testPackageVisibility_otherUserGrant",
140                 testUserId,
141                 testArgs);
142     }
143 
144     @Test
145     @AppModeFull(reason = "instant applications cannot see any other application")
testPackageListOtherUserCrossUserNoGrant()146     public void testPackageListOtherUserCrossUserNoGrant() throws Exception {
147         if (!mSupportsMultiUser) {
148             return;
149         }
150 
151         final int installUserId = getInstallUserId();
152         final int testUserId = getTestUserId();
153         final Map<String, String> testArgs = new HashMap<>();
154         testArgs.put("installUser", Integer.toString(installUserId));
155 
156         installTestAppForUser(TINY_APK, installUserId);
157         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
158 
159         Utils.runDeviceTests(
160                 getDevice(),
161                 TEST_WITH_PERMISSION_PKG,
162                 ".ApplicationVisibilityCrossUserTest",
163                 "testPackageVisibility_otherUserNoGrant",
164                 testUserId,
165                 testArgs);
166     }
167 
168     @Test
169     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testApplicationListCrossUserGrant()170     public void testApplicationListCrossUserGrant() throws Exception {
171         if (!mSupportsMultiUser) {
172             return;
173         }
174 
175         final int installUserId = getInstallUserId();
176         final int testUserId = getTestUserId();
177 
178         installTestAppForUser(TINY_APK, installUserId);
179         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
180 
181         Utils.runDeviceTests(
182                 getDevice(),
183                 TEST_WITH_PERMISSION_PKG,
184                 ".ApplicationVisibilityCrossUserTest",
185                 "testApplicationVisibility_currentUser",
186                 testUserId);
187         Utils.runDeviceTests(
188                 getDevice(),
189                 TEST_WITH_PERMISSION_PKG,
190                 ".ApplicationVisibilityCrossUserTest",
191                 "testApplicationVisibility_anyUserCrossUserGrant",
192                 testUserId);
193     }
194 
195     @Test
196     @AppModeFull(reason = "instant applications cannot see any other application")
testApplicationListCrossUserNoGrant()197     public void testApplicationListCrossUserNoGrant() throws Exception {
198         if (!mSupportsMultiUser) {
199             return;
200         }
201 
202         final int installUserId = getInstallUserId();
203         final int testUserId = getTestUserId();
204 
205         installTestAppForUser(TINY_APK, installUserId);
206         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
207 
208         Utils.runDeviceTests(
209                 getDevice(),
210                 TEST_WITH_PERMISSION_PKG,
211                 ".ApplicationVisibilityCrossUserTest",
212                 "testApplicationVisibility_currentUser",
213                 testUserId);
214         Utils.runDeviceTests(
215                 getDevice(),
216                 TEST_WITH_PERMISSION_PKG,
217                 ".ApplicationVisibilityCrossUserTest",
218                 "testApplicationVisibility_anyUserCrossUserNoGrant",
219                 testUserId);
220     }
221 
222     @Test
223     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testApplicationListOtherUserCrossUserGrant()224     public void testApplicationListOtherUserCrossUserGrant() throws Exception {
225         if (!mSupportsMultiUser) {
226             return;
227         }
228 
229         final int installUserId = getInstallUserId();
230         final int testUserId = getTestUserId();
231         final Map<String, String> testArgs = new HashMap<>();
232         testArgs.put("installUser", Integer.toString(installUserId));
233 
234         installTestAppForUser(TINY_APK, installUserId);
235         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
236 
237         Utils.runDeviceTests(
238                 getDevice(),
239                 TEST_WITH_PERMISSION_PKG,
240                 ".ApplicationVisibilityCrossUserTest",
241                 "testApplicationVisibility_otherUserGrant",
242                 testUserId,
243                 testArgs);
244     }
245 
246     @Test
247     @AppModeFull(reason = "instant applications cannot see any other application")
testApplicationListOtherUserCrossUserNoGrant()248     public void testApplicationListOtherUserCrossUserNoGrant() throws Exception {
249         if (!mSupportsMultiUser) {
250             return;
251         }
252 
253         final int installUserId = getInstallUserId();
254         final int testUserId = getTestUserId();
255         final Map<String, String> testArgs = new HashMap<>();
256         testArgs.put("installUser", Integer.toString(installUserId));
257 
258         installTestAppForUser(TINY_APK, installUserId);
259         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
260 
261         Utils.runDeviceTests(
262                 getDevice(),
263                 TEST_WITH_PERMISSION_PKG,
264                 ".ApplicationVisibilityCrossUserTest",
265                 "testApplicationVisibility_otherUserNoGrant",
266                 testUserId,
267                 testArgs);
268     }
269 
270     @Test
271     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testGetPackagesForUidCrossUserGrant()272     public void testGetPackagesForUidCrossUserGrant() throws Exception {
273         if (!mSupportsMultiUser) {
274             return;
275         }
276 
277         final int installUserId = getInstallUserId();
278         final int testUserId = getTestUserId();
279         final Map<String, String> testArgs = new HashMap<>();
280         testArgs.put("installUser", Integer.toString(installUserId));
281 
282         installTestAppForUser(TINY_APK, installUserId);
283         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
284 
285         Utils.runDeviceTests(
286                 getDevice(),
287                 TEST_WITH_PERMISSION_PKG,
288                 ".ApplicationVisibilityCrossUserTest",
289                 "testGetPackagesForUidVisibility_currentUser",
290                 testUserId);
291         Utils.runDeviceTests(
292                 getDevice(),
293                 TEST_WITH_PERMISSION_PKG,
294                 ".ApplicationVisibilityCrossUserTest",
295                 "testGetPackagesForUidVisibility_anotherUserCrossUserGrant",
296                 testUserId,
297                 testArgs);
298     }
299 
300     @Test
301     @AppModeFull(reason = "instant applications cannot see any other application")
testGetPackagesForUidCrossUserNoGrant()302     public void testGetPackagesForUidCrossUserNoGrant() throws Exception {
303         if (!mSupportsMultiUser) {
304             return;
305         }
306 
307         final int installUserId = getInstallUserId();
308         final int testUserId = getTestUserId();
309         final Map<String, String> testArgs = new HashMap<>();
310         testArgs.put("installUser", Integer.toString(installUserId));
311 
312         installTestAppForUser(TINY_APK, installUserId);
313         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
314 
315         Utils.runDeviceTests(
316                 getDevice(),
317                 TEST_WITH_PERMISSION_PKG,
318                 ".ApplicationVisibilityCrossUserTest",
319                 "testGetPackagesForUidVisibility_currentUser",
320                 testUserId);
321         Utils.runDeviceTests(
322                 getDevice(),
323                 TEST_WITH_PERMISSION_PKG,
324                 ".ApplicationVisibilityCrossUserTest",
325                 "testGetPackagesForUidVisibility_anotherUserCrossUserNoGrant",
326                 testUserId,
327                 testArgs);
328     }
329 
330     @Test
331     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testGetPackageUidCrossUserGrant()332     public void testGetPackageUidCrossUserGrant() throws Exception {
333         if (!mSupportsMultiUser) {
334             return;
335         }
336 
337         final int installUserId = getInstallUserId();
338         final int testUserId = getTestUserId();
339         final Map<String, String> testArgs = new HashMap<>();
340         testArgs.put("installUser", Integer.toString(installUserId));
341 
342         installTestAppForUser(TINY_APK, installUserId);
343         // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context
344         installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId);
345         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
346 
347         Utils.runDeviceTests(
348                 getDevice(),
349                 TEST_WITH_PERMISSION_PKG,
350                 ".ApplicationVisibilityCrossUserTest",
351                 "testGetPackageUidVisibility_currentUser",
352                 testUserId);
353         Utils.runDeviceTests(
354                 getDevice(),
355                 TEST_WITH_PERMISSION_PKG,
356                 ".ApplicationVisibilityCrossUserTest",
357                 "testGetPackageUidVisibility_anotherUserCrossUserGrant",
358                 testUserId,
359                 testArgs);
360     }
361 
362     @Test
363     @AppModeFull(reason = "instant applications cannot see any other application")
testGetPackageUidCrossUserNoGrant()364     public void testGetPackageUidCrossUserNoGrant() throws Exception {
365         if (!mSupportsMultiUser) {
366             return;
367         }
368 
369         final int installUserId = getInstallUserId();
370         final int testUserId = getTestUserId();
371         final Map<String, String> testArgs = new HashMap<>();
372         testArgs.put("installUser", Integer.toString(installUserId));
373 
374         installTestAppForUser(TINY_APK, installUserId);
375         // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context
376         installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId);
377         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
378 
379         Utils.runDeviceTests(
380                 getDevice(),
381                 TEST_WITH_PERMISSION_PKG,
382                 ".ApplicationVisibilityCrossUserTest",
383                 "testGetPackageUidVisibility_currentUser",
384                 testUserId);
385         Utils.runDeviceTests(
386                 getDevice(),
387                 TEST_WITH_PERMISSION_PKG,
388                 ".ApplicationVisibilityCrossUserTest",
389                 "testGetPackageUidVisibility_anotherUserCrossUserNoGrant",
390                 testUserId,
391                 testArgs);
392     }
393 
394     @Test
395     @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS")
testGetPackageGidsCrossUserGrant()396     public void testGetPackageGidsCrossUserGrant() throws Exception {
397         if (!mSupportsMultiUser) {
398             return;
399         }
400 
401         final int installUserId = getInstallUserId();
402         final int testUserId = getTestUserId();
403         final Map<String, String> testArgs = new HashMap<>();
404         testArgs.put("installUser", Integer.toString(installUserId));
405 
406         installTestAppForUser(TINY_APK, installUserId);
407         // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context
408         installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId);
409         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
410 
411         Utils.runDeviceTests(
412                 getDevice(),
413                 TEST_WITH_PERMISSION_PKG,
414                 ".ApplicationVisibilityCrossUserTest",
415                 "testGetPackageGidsVisibility_currentUser",
416                 testUserId);
417         Utils.runDeviceTests(
418                 getDevice(),
419                 TEST_WITH_PERMISSION_PKG,
420                 ".ApplicationVisibilityCrossUserTest",
421                 "testGetPackageGidsVisibility_anotherUserCrossUserGrant",
422                 testUserId,
423                 testArgs);
424     }
425 
426     @Test
427     @AppModeFull(reason = "instant applications cannot see any other application")
testGetPackageGidsCrossUserNoGrant()428     public void testGetPackageGidsCrossUserNoGrant() throws Exception {
429         if (!mSupportsMultiUser) {
430             return;
431         }
432 
433         final int installUserId = getInstallUserId();
434         final int testUserId = getTestUserId();
435         final Map<String, String> testArgs = new HashMap<>();
436         testArgs.put("installUser", Integer.toString(installUserId));
437 
438         installTestAppForUser(TINY_APK, installUserId);
439         // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context
440         installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId);
441         installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId);
442 
443         Utils.runDeviceTests(
444                 getDevice(),
445                 TEST_WITH_PERMISSION_PKG,
446                 ".ApplicationVisibilityCrossUserTest",
447                 "testGetPackageGidsVisibility_currentUser",
448                 testUserId);
449         Utils.runDeviceTests(
450                 getDevice(),
451                 TEST_WITH_PERMISSION_PKG,
452                 ".ApplicationVisibilityCrossUserTest",
453                 "testGetPackageGidsVisibility_anotherUserCrossUserNoGrant",
454                 testUserId,
455                 testArgs);
456     }
457 
getInstallUserId()458     private int getInstallUserId() {
459         return mUsers[0];
460     }
461 
getTestUserId()462     private int getTestUserId() {
463         return mUsers[1];
464     }
465 }
466