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.appsecurity.cts;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import android.platform.test.annotations.AppModeFull;
22 import android.platform.test.annotations.AppModeInstant;
23 
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 /**
32  * Test that install of apps using major version codes is being handled properly.
33  */
34 @RunWith(DeviceJUnit4ClassRunner.class)
35 public class UseProcessTest extends BaseAppSecurityTest {
36     private static final String PKG = "com.android.cts.useprocess";
37     private static final String APK_SUCCESS = "CtsUseProcessSuccess.apk";
38     private static final String APK_FAIL_APPLICATION = "CtsUseProcessFailApplication.apk";
39     private static final String APK_FAIL_ACTIVITY = "CtsUseProcessFailActivity.apk";
40     private static final String APK_FAIL_SERVICE = "CtsUseProcessFailService.apk";
41     private static final String APK_FAIL_RECEIVER = "CtsUseProcessFailReceiver.apk";
42     private static final String APK_FAIL_PROVIDER = "CtsUseProcessFailProvider.apk";
43 
44     private static final String SUCCESS_UNIT_TEST_CLASS
45             = "com.android.cts.useprocess.AccessNetworkTest";
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         Utils.prepareSingleUser(getDevice());
50         getDevice().uninstallPackage(PKG);
51     }
52 
53     @After
tearDown()54     public void tearDown() throws Exception {
55         getDevice().uninstallPackage(PKG);
56     }
57 
58     @Test
59     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUsePackageSuccess_full()60     public void testInstallUsePackageSuccess_full() throws Exception {
61         testInstallUsePackageSuccess(false);
62     }
63     @Test
64     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUsePackageSuccess_instant()65     public void testInstallUsePackageSuccess_instant() throws Exception {
66         testInstallUsePackageSuccess(true);
67     }
testInstallUsePackageSuccess(boolean instant)68     private void testInstallUsePackageSuccess(boolean instant) throws Exception {
69         new InstallMultiple(instant).addFile(APK_SUCCESS).run();
70         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
71 
72         Utils.runDeviceTestsAsCurrentUser(getDevice(), PKG, SUCCESS_UNIT_TEST_CLASS, null);
73     }
74 
75     @Test
76     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUsePackageFailApplication_full()77     public void testInstallUsePackageFailApplication_full() throws Exception {
78         testInstallUsePackageFailApplication(false);
79     }
80     @Test
81     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUsePackageFailApplication_instant()82     public void testInstallUsePackageFailApplication_instant() throws Exception {
83         testInstallUsePackageFailApplication(true);
84     }
testInstallUsePackageFailApplication(boolean instant)85     private void testInstallUsePackageFailApplication(boolean instant) throws Exception {
86         new InstallMultiple(instant).addFile(APK_FAIL_APPLICATION).runExpectingFailure(
87                 "Failure [INSTALL_FAILED_PROCESS_NOT_DEFINED: Scanning Failed.: " +
88                         "Can't install because application");
89         assertTrue(!getDevice().getInstalledPackageNames().contains(PKG));
90     }
91 
92     @Test
93     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUsePackageFailActivity_full()94     public void testInstallUsePackageFailActivity_full() throws Exception {
95         testInstallUsePackageFailActivity(false);
96     }
97     @Test
98     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUsePackageFailActivity_instant()99     public void testInstallUsePackageFailActivity_instant() throws Exception {
100         testInstallUsePackageFailActivity(true);
101     }
testInstallUsePackageFailActivity(boolean instant)102     private void testInstallUsePackageFailActivity(boolean instant) throws Exception {
103         new InstallMultiple(instant).addFile(APK_FAIL_ACTIVITY).runExpectingFailure(
104                 "Failure [INSTALL_FAILED_PROCESS_NOT_DEFINED: Scanning Failed.: " +
105                         "Can't install because activity com.android.cts.useprocess.DummyActivity");
106         assertTrue(!getDevice().getInstalledPackageNames().contains(PKG));
107     }
108 
109     @Test
110     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUsePackageFailService_full()111     public void testInstallUsePackageFailService_full() throws Exception {
112         testInstallUsePackageFailService(false);
113     }
114     @Test
115     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUsePackageFailService_instant()116     public void testInstallUsePackageFailService_instant() throws Exception {
117         testInstallUsePackageFailService(true);
118     }
testInstallUsePackageFailService(boolean instant)119     private void testInstallUsePackageFailService(boolean instant) throws Exception {
120         new InstallMultiple(instant).addFile(APK_FAIL_SERVICE).runExpectingFailure(
121                 "Failure [INSTALL_FAILED_PROCESS_NOT_DEFINED: Scanning Failed.: " +
122                         "Can't install because service com.android.cts.useprocess.DummyService");
123         assertTrue(!getDevice().getInstalledPackageNames().contains(PKG));
124     }
125 
126     @Test
127     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUsePackageFailReceiver_full()128     public void testInstallUsePackageFailReceiver_full() throws Exception {
129         testInstallUsePackageFailReceiver(false);
130     }
131     @Test
132     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUsePackageFailReceiver_instant()133     public void testInstallUsePackageFailReceiver_instant() throws Exception {
134         testInstallUsePackageFailReceiver(true);
135     }
testInstallUsePackageFailReceiver(boolean instant)136     private void testInstallUsePackageFailReceiver(boolean instant) throws Exception {
137         new InstallMultiple(instant).addFile(APK_FAIL_RECEIVER).runExpectingFailure(
138                 "Failure [INSTALL_FAILED_PROCESS_NOT_DEFINED: Scanning Failed.: " +
139                         "Can't install because receiver com.android.cts.useprocess.DummyReceiver");
140         assertTrue(!getDevice().getInstalledPackageNames().contains(PKG));
141     }
142 
143     @Test
144     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUsePackageFailProvider_full()145     public void testInstallUsePackageFailProvider_full() throws Exception {
146         testInstallUsePackageFailProvider(false);
147     }
148     @Test
149     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUsePackageFailProvider_instant()150     public void testInstallUsePackageFailProvider_instant() throws Exception {
151         testInstallUsePackageFailProvider(true);
152     }
testInstallUsePackageFailProvider(boolean instant)153     private void testInstallUsePackageFailProvider(boolean instant) throws Exception {
154         new InstallMultiple(instant).addFile(APK_FAIL_PROVIDER).runExpectingFailure(
155                 "Failure [INSTALL_FAILED_PROCESS_NOT_DEFINED: Scanning Failed.: " +
156                         "Can't install because provider com.android.cts.useprocess.DummyProvider");
157         assertTrue(!getDevice().getInstalledPackageNames().contains(PKG));
158     }
159 }
160