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.tradefed.build.IBuildInfo; 22 import com.android.tradefed.device.DeviceNotAvailableException; 23 import com.android.tradefed.testtype.DeviceTestCase; 24 import com.android.tradefed.testtype.IAbi; 25 import com.android.tradefed.testtype.IAbiReceiver; 26 import com.android.tradefed.testtype.IBuildReceiver; 27 28 import java.util.ArrayList; 29 30 /** 31 * Tests for ephemeral packages. 32 */ 33 @AppModeFull // Already handles instant installs when needed. 34 public class InstantAppUserTest extends DeviceTestCase 35 implements IAbiReceiver, IBuildReceiver { 36 37 // an application to verify instant/full app per user 38 private static final String USER_APK = "CtsEphemeralTestsUserApp.apk"; 39 private static final String USER_PKG = "com.android.cts.userapp"; 40 41 private static final String USER_TEST_APK = "CtsEphemeralTestsUserAppTest.apk"; 42 private static final String USER_TEST_PKG = "com.android.cts.userapptest"; 43 44 private static final String TEST_CLASS = ".ClientTest"; 45 46 private static final boolean MATCH_UNINSTALLED = true; 47 private static final boolean MATCH_NORMAL = false; 48 49 private static final int USER_SYSTEM = 0; // From the UserHandle class. 50 51 private String mOldVerifierValue; 52 private IAbi mAbi; 53 private IBuildInfo mBuildInfo; 54 private boolean mSupportsMultiUser; 55 private int mPrimaryUserId; 56 /** Users we shouldn't delete in the tests */ 57 private ArrayList<Integer> mFixedUsers; 58 private int[] mTestUser = new int[2]; 59 60 @Override 61 public void setAbi(IAbi abi) { 62 mAbi = abi; 63 } 64 65 @Override 66 public void setBuild(IBuildInfo buildInfo) { 67 mBuildInfo = buildInfo; 68 } 69 70 public void setUp() throws Exception { 71 super.setUp(); 72 73 assertNotNull(mAbi); 74 assertNotNull(mBuildInfo); 75 76 // This test only runs when we have at least 3 users to work with 77 final int[] users = Utils.prepareMultipleUsers(getDevice(), 3); 78 mSupportsMultiUser = (users.length == 3); 79 if (mSupportsMultiUser) { 80 mPrimaryUserId = getDevice().getPrimaryUserId(); 81 mFixedUsers = new ArrayList<>(); 82 mFixedUsers.add(mPrimaryUserId); 83 if (mPrimaryUserId != USER_SYSTEM) { 84 mFixedUsers.add(USER_SYSTEM); 85 } 86 getDevice().switchUser(mPrimaryUserId); 87 88 mTestUser[0] = users[1]; 89 mTestUser[1] = users[2]; 90 91 uninstallTestPackages(); 92 installTestPackages(); 93 } 94 } 95 96 public void tearDown() throws Exception { 97 if (mSupportsMultiUser) { 98 uninstallTestPackages(); 99 } 100 super.tearDown(); 101 } 102 103 public void testInstallInstant() throws Exception { 104 if (!mSupportsMultiUser) { 105 return; 106 } 107 installInstantApp(USER_APK); 108 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 109 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[0]); 110 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[1]); 111 } 112 113 public void testInstallFull() throws Exception { 114 if (!mSupportsMultiUser) { 115 return; 116 } 117 installApp(USER_APK); 118 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mPrimaryUserId); 119 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[0]); 120 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[1]); 121 } 122 123 public void testInstallMultiple() throws Exception { 124 if (!mSupportsMultiUser) { 125 return; 126 } 127 installAppAsUser(USER_APK, mPrimaryUserId); 128 installExistingInstantAppAsUser(USER_PKG, mTestUser[0]); 129 installExistingFullAppAsUser(USER_PKG, mTestUser[1]); 130 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mPrimaryUserId); 131 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[0]); 132 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[1]); 133 } 134 135 public void testUpgradeExisting() throws Exception { 136 if (!mSupportsMultiUser) { 137 return; 138 } 139 installInstantApp(USER_APK); 140 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 141 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[0]); 142 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[1]); 143 144 installExistingFullAppAsUser(USER_PKG, mTestUser[0]); 145 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 146 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[0]); 147 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[1]); 148 149 installExistingFullAppAsUser(USER_PKG, mTestUser[1]); 150 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 151 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[0]); 152 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[1]); 153 } 154 155 public void testReplaceExisting() throws Exception { 156 if (!mSupportsMultiUser) { 157 return; 158 } 159 installInstantApp(USER_APK); 160 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 161 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[0]); 162 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[1]); 163 164 replaceFullAppAsUser(USER_APK, mTestUser[0]); 165 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 166 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[0]); 167 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mTestUser[1]); 168 169 replaceFullAppAsUser(USER_APK, mTestUser[1]); 170 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryInstant", mPrimaryUserId); 171 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[0]); 172 runDeviceTestsAsUser(USER_TEST_PKG, TEST_CLASS, "testQueryFull", mTestUser[1]); 173 } 174 175 private void installTestPackages() throws Exception { 176 installApp(USER_TEST_APK); 177 } 178 179 private void uninstallTestPackages() throws Exception { 180 getDevice().uninstallPackage(USER_TEST_PKG); 181 getDevice().uninstallPackage(USER_PKG); 182 } 183 184 private void runDeviceTests(String packageName, String testClassName, String testMethodName) 185 throws DeviceNotAvailableException { 186 Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName); 187 } 188 189 private void runDeviceTestsAsUser(String packageName, String testClassName, 190 String testMethodName, int userId) 191 throws DeviceNotAvailableException { 192 Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName, userId); 193 } 194 195 private void installApp(String apk) throws Exception { 196 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuildInfo); 197 assertNull(getDevice().installPackage(buildHelper.getTestFile(apk), false)); 198 } 199 200 private void installInstantApp(String apk) throws Exception { 201 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuildInfo); 202 assertNull(getDevice().installPackage(buildHelper.getTestFile(apk), false, "--instant")); 203 } 204 205 private void installAppAsUser(String apk, int userId) throws Exception { 206 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuildInfo); 207 assertNull(getDevice().installPackage(buildHelper.getTestFile(apk), false)); 208 } 209 210 private void replaceFullAppAsUser(String apk, int userId) throws Exception { 211 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuildInfo); 212 assertNull(getDevice().installPackageForUser( 213 buildHelper.getTestFile(apk), true, userId, "--full")); 214 } 215 216 private void installExistingInstantAppAsUser(String packageName, int userId) throws Exception { 217 final String installString = 218 "Package " + packageName + " installed for user: " + userId + "\n"; 219 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuildInfo); 220 assertEquals(installString, getDevice().executeShellCommand( 221 "cmd package install-existing --instant" 222 + " --user " + Integer.toString(userId) 223 + " " + packageName)); 224 } 225 226 private void installExistingFullAppAsUser(String packageName, int userId) throws Exception { 227 final String installString = 228 "Package " + packageName + " installed for user: " + userId + "\n"; 229 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuildInfo); 230 assertEquals(installString, getDevice().executeShellCommand( 231 "cmd package install-existing --full" 232 + " --user " + Integer.toString(userId) 233 + " " + packageName)); 234 } 235 } 236