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