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 
17 package android.appsecurity.cts;
18 
19 import android.platform.test.annotations.AppModeFull;
20 import android.platform.test.annotations.LargeTest;
21 import android.platform.test.annotations.Presubmit;
22 
23 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
24 import com.android.ddmlib.Log;
25 import com.android.tradefed.build.IBuildInfo;
26 import com.android.tradefed.device.DeviceNotAvailableException;
27 import com.android.tradefed.testtype.DeviceTestCase;
28 import com.android.tradefed.testtype.IAbi;
29 import com.android.tradefed.testtype.IAbiReceiver;
30 import com.android.tradefed.testtype.IBuildReceiver;
31 import com.android.tradefed.util.AbiFormatter;
32 
33 /**
34  * Tests that verify intent filters.
35  */
36 @Presubmit
37 @LargeTest
38 @AppModeFull(reason="Instant applications can never be system or privileged")
39 public class PrivilegedUpdateTests extends DeviceTestCase implements IAbiReceiver, IBuildReceiver {
40     private static final String TAG = "PrivilegedUpdateTests";
41     private static final String SHIM_PKG = "com.android.cts.priv.ctsshim";
42     /** Package name of the tests to be run */
43     private static final String TEST_PKG = "com.android.cts.privilegedupdate";
44 
45     /** APK that contains the shim update; to test upgrading */
46     private static final String SHIM_UPDATE_APK = "CtsShimPrivUpgradePrebuilt.apk";
47     /** APK that contains the shim update w/ incorrect SHA; to test upgrade fails */
48     private static final String SHIM_UPDATE_FAIL_APK = "CtsShimPrivUpgradeWrongSHAPrebuilt.apk";
49     /** APK that contains individual shim test cases */
50     private static final String TEST_APK = "CtsPrivilegedUpdateTests.apk";
51 
52     private static final String RESTRICTED_UPGRADE_FAILURE =
53             "INSTALL_FAILED_INVALID_APK:"
54             + " New package fails restrict-update check:"
55             + " com.android.cts.priv.ctsshim";
56 
57     private IAbi mAbi;
58     private CompatibilityBuildHelper mBuildHelper;
59 
isDefaultAbi()60     private boolean isDefaultAbi() throws Exception {
61         String defaultAbi = AbiFormatter.getDefaultAbi(getDevice(), mAbi.getBitness());
62         return mAbi.getName().equals(defaultAbi);
63     }
64 
65     @Override
setAbi(IAbi abi)66     public void setAbi(IAbi abi) {
67         mAbi = abi;
68     }
69 
70     @Override
setBuild(IBuildInfo buildInfo)71     public void setBuild(IBuildInfo buildInfo) {
72         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
73     }
74 
75     @Override
setUp()76     protected void setUp() throws Exception {
77         super.setUp();
78 
79         Utils.prepareSingleUser(getDevice());
80         assertNotNull(mAbi);
81         assertNotNull(mBuildHelper);
82 
83         getDevice().uninstallPackage(SHIM_PKG);
84         getDevice().uninstallPackage(TEST_PKG);
85 
86         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(TEST_APK), false));
87         getDevice().executeShellCommand("pm enable " + SHIM_PKG);
88     }
89 
90     @Override
tearDown()91     protected void tearDown() throws Exception {
92         super.tearDown();
93 
94         getDevice().uninstallPackage(SHIM_PKG);
95         getDevice().uninstallPackage(TEST_PKG);
96         getDevice().executeShellCommand("pm enable " + SHIM_PKG);
97     }
98 
testPrivilegedAppUpgradeRestricted()99     public void testPrivilegedAppUpgradeRestricted() throws Exception {
100         getDevice().uninstallPackage(SHIM_PKG);
101         assertEquals(RESTRICTED_UPGRADE_FAILURE, getDevice().installPackage(
102                 mBuildHelper.getTestFile(SHIM_UPDATE_FAIL_APK), true));
103     }
104 
testSystemAppPriorities()105     public void testSystemAppPriorities() throws Exception {
106         runDeviceTests(TEST_PKG, ".PrivilegedUpdateTest", "testSystemAppPriorities");
107     }
108 
testPrivilegedAppPriorities()109     public void testPrivilegedAppPriorities() throws Exception {
110         runDeviceTests(TEST_PKG, ".PrivilegedUpdateTest", "testPrivilegedAppPriorities");
111     }
112 
testPrivilegedAppUpgradePrioritiesPreservedOnReboot()113     public void testPrivilegedAppUpgradePrioritiesPreservedOnReboot() throws Exception {
114         if (!isDefaultAbi()) {
115             Log.w(TAG, "Skipping test for non-default abi.");
116             return;
117         }
118 
119         getDevice().uninstallPackage(SHIM_PKG);
120 
121         try {
122             assertNull(getDevice().installPackage(
123                     mBuildHelper.getTestFile(SHIM_UPDATE_APK), true));
124             runDeviceTests(TEST_PKG, ".PrivilegedUpdateTest", "testPrivilegedAppUpgradePriorities");
125 
126             getDevice().reboot();
127 
128             runDeviceTests(TEST_PKG, ".PrivilegedUpdateTest", "testPrivilegedAppUpgradePriorities");
129         } finally {
130             getDevice().uninstallPackage(SHIM_PKG);
131         }
132     }
133 
testDisableSystemApp()134     public void testDisableSystemApp() throws Exception {
135         getDevice().executeShellCommand("pm enable " + SHIM_PKG);
136         runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndEnabled");
137         getDevice().executeShellCommand("pm disable-user " + SHIM_PKG);
138         runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndDisabled");
139     }
140 
testDisableUpdatedSystemApp()141     public void testDisableUpdatedSystemApp() throws Exception {
142         if (!isDefaultAbi()) {
143             Log.w(TAG, "Skipping test for non-default abi.");
144             return;
145         }
146 
147         getDevice().executeShellCommand("pm enable " + SHIM_PKG);
148         runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndEnabled");
149         try {
150             assertNull(getDevice().installPackage(
151                     mBuildHelper.getTestFile(SHIM_UPDATE_APK), true));
152             getDevice().executeShellCommand("pm disable-user " + SHIM_PKG);
153             runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndDisabled");
154             getDevice().executeShellCommand("pm enable " + SHIM_PKG);
155             runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndEnabled");
156         } finally {
157             getDevice().uninstallPackage(SHIM_PKG);
158         }
159     }
160 
testUpdatedSystemAppPreservedOnReboot()161     public void testUpdatedSystemAppPreservedOnReboot() throws Exception {
162         if (!isDefaultAbi()) {
163             Log.w(TAG, "Skipping test for non-default abi.");
164             return;
165         }
166 
167         getDevice().executeShellCommand("pm enable " + SHIM_PKG);
168         runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndEnabled");
169         try {
170             assertNull(getDevice().installPackage(
171                     mBuildHelper.getTestFile(SHIM_UPDATE_APK), true));
172             getDevice().executeShellCommand("pm enable " + SHIM_PKG);
173             runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndEnabled");
174 
175             getDevice().reboot();
176 
177             runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndEnabled");
178         } finally {
179             getDevice().uninstallPackage(SHIM_PKG);
180         }
181     }
182 
testUninstallDisabledUpdatedSystemApp_remainingDisabled()183     public void testUninstallDisabledUpdatedSystemApp_remainingDisabled() throws Exception {
184         if (!isDefaultAbi()) {
185             Log.w(TAG, "Skipping test for non-default abi.");
186             return;
187         }
188 
189         getDevice().executeShellCommand("pm enable " + SHIM_PKG);
190         runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndEnabled");
191         try {
192             assertNull(getDevice().installPackage(
193                     mBuildHelper.getTestFile(SHIM_UPDATE_APK), true));
194             getDevice().executeShellCommand("pm disable-user " + SHIM_PKG);
195             runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndDisabled");
196         } finally {
197             getDevice().uninstallPackage(SHIM_PKG);
198         }
199         runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndDisabled");
200     }
201 
runDeviceTests(String packageName, String testClassName, String testMethodName)202     private void runDeviceTests(String packageName, String testClassName, String testMethodName)
203             throws DeviceNotAvailableException {
204         Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName);
205     }
206 }
207