1 /*
2  * Copyright (C) 2016 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 package com.android.cts.packageaccessapp;
17 
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.fail;
21 
22 import android.content.pm.PackageInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.support.test.uiautomator.UiDevice;
26 
27 import androidx.test.InstrumentationRegistry;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 import java.util.List;
35 
36 @RunWith(JUnit4.class)
37 public class PackageAccessTest {
38 
39     private String TINY_PKG = "android.appsecurity.cts.tinyapp";
40 
41     private UiDevice mUiDevice;
42 
43     @Before
setUp()44     public void setUp() throws Exception {
45         mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
46     }
47 
48     @Test
testPackageAccess_inUser()49     public void testPackageAccess_inUser() throws Exception {
50         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
51         try {
52             PackageInfo pi = pm.getPackageInfo(TINY_PKG, 0);
53             assertNotNull(pi);
54         } catch (NameNotFoundException e) {
55             fail("Package must be found");
56         }
57     }
58 
59     @Test
testPackageAccess_inUserUninstalled()60     public void testPackageAccess_inUserUninstalled() throws Exception {
61         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
62         try {
63             PackageInfo pi = pm.getPackageInfo(TINY_PKG, PackageManager.MATCH_UNINSTALLED_PACKAGES);
64             assertNotNull(pi);
65         } catch (NameNotFoundException e) {
66             fail("Package must be found");
67         }
68     }
69 
70     @Test
testPackageAccess_notInOtherUser()71     public void testPackageAccess_notInOtherUser() throws Exception {
72         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
73         try {
74             PackageInfo pi = pm.getPackageInfo(TINY_PKG, 0);
75             // If it doesn't throw an exception, then at least it should be null
76             assertNull(pi);
77         } catch (NameNotFoundException e) {
78         }
79     }
80 
81     @Test
testPackageAccess_notInOtherUserUninstalled()82     public void testPackageAccess_notInOtherUserUninstalled() throws Exception {
83         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
84         try {
85             PackageInfo pi = pm.getPackageInfo(TINY_PKG, PackageManager.MATCH_UNINSTALLED_PACKAGES);
86             // If it doesn't throw an exception, then at least it should be null
87             assertNull(pi);
88         } catch (NameNotFoundException e) {
89         }
90     }
91 
92     @Test
testPackageAccess_getPackagesCantSeeTiny()93     public void testPackageAccess_getPackagesCantSeeTiny() throws Exception {
94         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
95         List<PackageInfo> packages = pm.getInstalledPackages(
96                 PackageManager.MATCH_UNINSTALLED_PACKAGES);
97         for (PackageInfo pi : packages) {
98             if (TINY_PKG.equals(pi.packageName)) {
99                 fail(TINY_PKG + " visible in user");
100             }
101         }
102     }
103 
104     @Test
testPackageAccess_getPackagesCanSeeTiny()105     public void testPackageAccess_getPackagesCanSeeTiny() throws Exception {
106         PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
107         List<PackageInfo> packages = pm.getInstalledPackages(
108                 PackageManager.MATCH_UNINSTALLED_PACKAGES);
109         for (PackageInfo pi : packages) {
110             if (TINY_PKG.equals(pi.packageName)) {
111                 return;
112             }
113         }
114         fail(TINY_PKG + " not found in getInstalledPackages()");
115     }
116 }