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 com.android.tests.atomicinstall;
18 
19 import static org.junit.Assert.fail;
20 
21 import android.Manifest;
22 import android.content.pm.PackageManager;
23 
24 import androidx.test.InstrumentationRegistry;
25 
26 import com.android.compatibility.common.util.SystemUtil;
27 import com.android.cts.install.lib.Install;
28 import com.android.cts.install.lib.TestApp;
29 import com.android.cts.install.lib.Uninstall;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 
38 /** Tests for package installation while Secure FRP mode is enabled. */
39 @RunWith(JUnit4.class)
40 public class SecureFrpInstallTest {
41 
42     private static PackageManager sPackageManager;
43 
adoptShellPermissions()44     private static void adoptShellPermissions() {
45         InstrumentationRegistry
46                 .getInstrumentation()
47                 .getUiAutomation()
48                 .adoptShellPermissionIdentity(
49                         Manifest.permission.INSTALL_PACKAGES, Manifest.permission.DELETE_PACKAGES);
50     }
51 
dropShellPermissions()52     private static void dropShellPermissions() {
53         InstrumentationRegistry
54                 .getInstrumentation()
55                 .getUiAutomation()
56                 .dropShellPermissionIdentity();
57     }
58 
setSecureFrp(boolean secureFrp)59     private static void setSecureFrp(boolean secureFrp) throws Exception {
60         adoptShellPermissions();
61         SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(),
62                 "settings put --user 0 secure secure_frp_mode " + (secureFrp ? "1" : "0"));
63         dropShellPermissions();
64     }
65 
assertInstalled()66     private static void assertInstalled() throws Exception {
67         sPackageManager.getPackageInfo(TestApp.A, 0);
68     }
69 
assertNotInstalled()70     private static void assertNotInstalled() {
71         try {
72             sPackageManager.getPackageInfo(TestApp.A, 0);
73             fail("Package should not be installed");
74         } catch (PackageManager.NameNotFoundException expected) {
75         }
76     }
77 
78     @BeforeClass
initialize()79     public static void initialize() {
80         sPackageManager = InstrumentationRegistry
81                 .getInstrumentation()
82                 .getContext()
83                 .getPackageManager();
84     }
85 
86     @Before
setup()87     public void setup() throws Exception {
88         adoptShellPermissions();
89         Uninstall.packages(TestApp.A);
90         dropShellPermissions();
91     }
92 
93     @After
teardown()94     public void teardown() throws Exception {
95         dropShellPermissions();
96         setSecureFrp(false);
97     }
98 
99     /** Tests a SecurityException is thrown while in secure FRP mode. */
100     @Test
testPackageInstallApi()101     public void testPackageInstallApi() throws Exception {
102         setSecureFrp(true);
103         try {
104             Install.single(TestApp.A1).commit();
105             fail("Expected a SecurityException");
106         } catch (SecurityException expected) {
107         }
108         assertNotInstalled();
109     }
110 
111     /** Tests can install when granted INSTALL_PACKAGES permission; even in secure FRP mode. */
112     @Test
testPackageInstallApiAsShell()113     public void testPackageInstallApiAsShell() throws Exception {
114         setSecureFrp(true);
115         adoptShellPermissions();
116         Install.single(TestApp.A1).commit();
117         dropShellPermissions();
118         assertInstalled();
119     }
120 }
121