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