1 /* 2 * Copyright (C) 2018 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.Presubmit; 21 22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 23 24 import org.junit.After; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** 33 * Tests the visibility of installed applications. 34 */ 35 @Presubmit 36 @RunWith(DeviceJUnit4ClassRunner.class) 37 public class ApplicationVisibilityTest extends BaseAppSecurityTest { 38 39 private static final String TINY_APK = "CtsPkgInstallTinyApp.apk"; 40 private static final String TINY_PKG = "android.appsecurity.cts.tinyapp"; 41 42 private static final String TEST_WITH_PERMISSION_APK = 43 "CtsApplicationVisibilityCrossUserApp.apk"; 44 private static final String TEST_WITH_PERMISSION_PKG = 45 "com.android.cts.applicationvisibility"; 46 47 private int[] mUsers; 48 private String mOldVerifierValue; 49 50 @Before setUpPackage()51 public void setUpPackage() throws Exception { 52 mUsers = Utils.prepareMultipleUsers(getDevice(), 2); 53 mOldVerifierValue = 54 getDevice().executeShellCommand("settings get global package_verifier_enable"); 55 getDevice().executeShellCommand("settings put global package_verifier_enable 0"); 56 } 57 58 @After tearDown()59 public void tearDown() throws Exception { 60 getDevice().uninstallPackage(TEST_WITH_PERMISSION_PKG); 61 getDevice().uninstallPackage(TINY_PKG); 62 getDevice().executeShellCommand("settings put global package_verifier_enable " 63 + mOldVerifierValue); 64 } 65 66 @Test 67 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testPackageListCrossUserGrant()68 public void testPackageListCrossUserGrant() throws Exception { 69 if (!mSupportsMultiUser) { 70 return; 71 } 72 73 final int installUserId = getInstallUserId(); 74 final int testUserId = getTestUserId(); 75 76 installTestAppForUser(TINY_APK, installUserId); 77 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 78 79 Utils.runDeviceTests( 80 getDevice(), 81 TEST_WITH_PERMISSION_PKG, 82 ".ApplicationVisibilityCrossUserTest", 83 "testPackageVisibility_currentUser", 84 testUserId); 85 Utils.runDeviceTests( 86 getDevice(), 87 TEST_WITH_PERMISSION_PKG, 88 ".ApplicationVisibilityCrossUserTest", 89 "testPackageVisibility_anyUserCrossUserGrant", 90 testUserId); 91 } 92 93 @Test 94 @AppModeFull(reason = "instant applications cannot see any other application") testPackageListCrossUserNoGrant()95 public void testPackageListCrossUserNoGrant() throws Exception { 96 if (!mSupportsMultiUser) { 97 return; 98 } 99 100 final int installUserId = getInstallUserId(); 101 final int testUserId = getTestUserId(); 102 103 installTestAppForUser(TINY_APK, installUserId); 104 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 105 106 Utils.runDeviceTests( 107 getDevice(), 108 TEST_WITH_PERMISSION_PKG, 109 ".ApplicationVisibilityCrossUserTest", 110 "testPackageVisibility_currentUser", 111 testUserId); 112 Utils.runDeviceTests( 113 getDevice(), 114 TEST_WITH_PERMISSION_PKG, 115 ".ApplicationVisibilityCrossUserTest", 116 "testPackageVisibility_anyUserCrossUserNoGrant", 117 testUserId); 118 } 119 120 @Test 121 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testPackageListOtherUserCrossUserGrant()122 public void testPackageListOtherUserCrossUserGrant() throws Exception { 123 if (!mSupportsMultiUser) { 124 return; 125 } 126 127 final int installUserId = getInstallUserId(); 128 final int testUserId = getTestUserId(); 129 final Map<String, String> testArgs = new HashMap<>(); 130 testArgs.put("testUser", Integer.toString(installUserId)); 131 132 installTestAppForUser(TINY_APK, installUserId); 133 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 134 135 Utils.runDeviceTests( 136 getDevice(), 137 TEST_WITH_PERMISSION_PKG, 138 ".ApplicationVisibilityCrossUserTest", 139 "testPackageVisibility_otherUserGrant", 140 testUserId, 141 testArgs); 142 } 143 144 @Test 145 @AppModeFull(reason = "instant applications cannot see any other application") testPackageListOtherUserCrossUserNoGrant()146 public void testPackageListOtherUserCrossUserNoGrant() throws Exception { 147 if (!mSupportsMultiUser) { 148 return; 149 } 150 151 final int installUserId = getInstallUserId(); 152 final int testUserId = getTestUserId(); 153 final Map<String, String> testArgs = new HashMap<>(); 154 testArgs.put("testUser", Integer.toString(installUserId)); 155 156 installTestAppForUser(TINY_APK, installUserId); 157 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 158 159 Utils.runDeviceTests( 160 getDevice(), 161 TEST_WITH_PERMISSION_PKG, 162 ".ApplicationVisibilityCrossUserTest", 163 "testPackageVisibility_otherUserNoGrant", 164 testUserId, 165 testArgs); 166 } 167 168 @Test 169 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testApplicationListCrossUserGrant()170 public void testApplicationListCrossUserGrant() throws Exception { 171 if (!mSupportsMultiUser) { 172 return; 173 } 174 175 final int installUserId = getInstallUserId(); 176 final int testUserId = getTestUserId(); 177 178 installTestAppForUser(TINY_APK, installUserId); 179 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 180 181 Utils.runDeviceTests( 182 getDevice(), 183 TEST_WITH_PERMISSION_PKG, 184 ".ApplicationVisibilityCrossUserTest", 185 "testApplicationVisibility_currentUser", 186 testUserId); 187 Utils.runDeviceTests( 188 getDevice(), 189 TEST_WITH_PERMISSION_PKG, 190 ".ApplicationVisibilityCrossUserTest", 191 "testApplicationVisibility_anyUserCrossUserGrant", 192 testUserId); 193 } 194 195 @Test 196 @AppModeFull(reason = "instant applications cannot see any other application") testApplicationListCrossUserNoGrant()197 public void testApplicationListCrossUserNoGrant() throws Exception { 198 if (!mSupportsMultiUser) { 199 return; 200 } 201 202 final int installUserId = getInstallUserId(); 203 final int testUserId = getTestUserId(); 204 205 installTestAppForUser(TINY_APK, installUserId); 206 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 207 208 Utils.runDeviceTests( 209 getDevice(), 210 TEST_WITH_PERMISSION_PKG, 211 ".ApplicationVisibilityCrossUserTest", 212 "testApplicationVisibility_currentUser", 213 testUserId); 214 Utils.runDeviceTests( 215 getDevice(), 216 TEST_WITH_PERMISSION_PKG, 217 ".ApplicationVisibilityCrossUserTest", 218 "testApplicationVisibility_anyUserCrossUserNoGrant", 219 testUserId); 220 } 221 222 @Test 223 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testApplicationListOtherUserCrossUserGrant()224 public void testApplicationListOtherUserCrossUserGrant() throws Exception { 225 if (!mSupportsMultiUser) { 226 return; 227 } 228 229 final int installUserId = getInstallUserId(); 230 final int testUserId = getTestUserId(); 231 final Map<String, String> testArgs = new HashMap<>(); 232 testArgs.put("testUser", Integer.toString(installUserId)); 233 234 installTestAppForUser(TINY_APK, installUserId); 235 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 236 237 Utils.runDeviceTests( 238 getDevice(), 239 TEST_WITH_PERMISSION_PKG, 240 ".ApplicationVisibilityCrossUserTest", 241 "testApplicationVisibility_otherUserGrant", 242 testUserId, 243 testArgs); 244 } 245 246 @Test 247 @AppModeFull(reason = "instant applications cannot see any other application") testApplicationListOtherUserCrossUserNoGrant()248 public void testApplicationListOtherUserCrossUserNoGrant() throws Exception { 249 if (!mSupportsMultiUser) { 250 return; 251 } 252 253 final int installUserId = getInstallUserId(); 254 final int testUserId = getTestUserId(); 255 final Map<String, String> testArgs = new HashMap<>(); 256 testArgs.put("testUser", Integer.toString(installUserId)); 257 258 installTestAppForUser(TINY_APK, installUserId); 259 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 260 261 Utils.runDeviceTests( 262 getDevice(), 263 TEST_WITH_PERMISSION_PKG, 264 ".ApplicationVisibilityCrossUserTest", 265 "testApplicationVisibility_otherUserNoGrant", 266 testUserId, 267 testArgs); 268 } 269 270 @Test 271 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testGetPackagesForUidCrossUserGrant()272 public void testGetPackagesForUidCrossUserGrant() throws Exception { 273 if (!mSupportsMultiUser) { 274 return; 275 } 276 277 final int installUserId = getInstallUserId(); 278 final int testUserId = getTestUserId(); 279 280 installTestAppForUser(TINY_APK, installUserId); 281 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 282 283 Utils.runDeviceTests( 284 getDevice(), 285 TEST_WITH_PERMISSION_PKG, 286 ".ApplicationVisibilityCrossUserTest", 287 "testGetPackagesForUidVisibility_currentUser", 288 testUserId); 289 Utils.runDeviceTests( 290 getDevice(), 291 TEST_WITH_PERMISSION_PKG, 292 ".ApplicationVisibilityCrossUserTest", 293 "testGetPackagesForUidVisibility_anotherUserCrossUserGrant", 294 testUserId); 295 } 296 297 @Test 298 @AppModeFull(reason = "instant applications cannot see any other application") testGetPackagesForUidCrossUserNoGrant()299 public void testGetPackagesForUidCrossUserNoGrant() throws Exception { 300 if (!mSupportsMultiUser) { 301 return; 302 } 303 304 final int installUserId = getInstallUserId(); 305 final int testUserId = getTestUserId(); 306 307 installTestAppForUser(TINY_APK, installUserId); 308 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 309 310 Utils.runDeviceTests( 311 getDevice(), 312 TEST_WITH_PERMISSION_PKG, 313 ".ApplicationVisibilityCrossUserTest", 314 "testGetPackagesForUidVisibility_currentUser", 315 testUserId); 316 Utils.runDeviceTests( 317 getDevice(), 318 TEST_WITH_PERMISSION_PKG, 319 ".ApplicationVisibilityCrossUserTest", 320 "testGetPackagesForUidVisibility_anotherUserCrossUserNoGrant", 321 testUserId); 322 } 323 324 @Test 325 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testGetPackageUidCrossUserGrant()326 public void testGetPackageUidCrossUserGrant() throws Exception { 327 if (!mSupportsMultiUser) { 328 return; 329 } 330 331 final int installUserId = getInstallUserId(); 332 final int testUserId = getTestUserId(); 333 final Map<String, String> testArgs = new HashMap<>(); 334 testArgs.put("testUser", Integer.toString(installUserId)); 335 336 installTestAppForUser(TINY_APK, installUserId); 337 // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context 338 installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId); 339 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 340 341 Utils.runDeviceTests( 342 getDevice(), 343 TEST_WITH_PERMISSION_PKG, 344 ".ApplicationVisibilityCrossUserTest", 345 "testGetPackageUidVisibility_currentUser", 346 testUserId); 347 Utils.runDeviceTests( 348 getDevice(), 349 TEST_WITH_PERMISSION_PKG, 350 ".ApplicationVisibilityCrossUserTest", 351 "testGetPackageUidVisibility_anotherUserCrossUserGrant", 352 testUserId, 353 testArgs); 354 } 355 356 @Test 357 @AppModeFull(reason = "instant applications cannot see any other application") testGetPackageUidCrossUserNoGrant()358 public void testGetPackageUidCrossUserNoGrant() throws Exception { 359 if (!mSupportsMultiUser) { 360 return; 361 } 362 363 final int installUserId = getInstallUserId(); 364 final int testUserId = getTestUserId(); 365 final Map<String, String> testArgs = new HashMap<>(); 366 testArgs.put("testUser", Integer.toString(installUserId)); 367 368 installTestAppForUser(TINY_APK, installUserId); 369 // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context 370 installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId); 371 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 372 373 Utils.runDeviceTests( 374 getDevice(), 375 TEST_WITH_PERMISSION_PKG, 376 ".ApplicationVisibilityCrossUserTest", 377 "testGetPackageUidVisibility_currentUser", 378 testUserId); 379 Utils.runDeviceTests( 380 getDevice(), 381 TEST_WITH_PERMISSION_PKG, 382 ".ApplicationVisibilityCrossUserTest", 383 "testGetPackageUidVisibility_anotherUserCrossUserNoGrant", 384 testUserId, 385 testArgs); 386 } 387 388 @Test 389 @AppModeFull(reason = "instant applications cannot be granted INTERACT_ACROSS_USERS") testGetPackageGidsCrossUserGrant()390 public void testGetPackageGidsCrossUserGrant() throws Exception { 391 if (!mSupportsMultiUser) { 392 return; 393 } 394 395 final int installUserId = getInstallUserId(); 396 final int testUserId = getTestUserId(); 397 final Map<String, String> testArgs = new HashMap<>(); 398 testArgs.put("testUser", Integer.toString(installUserId)); 399 400 installTestAppForUser(TINY_APK, installUserId); 401 // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context 402 installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId); 403 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 404 405 Utils.runDeviceTests( 406 getDevice(), 407 TEST_WITH_PERMISSION_PKG, 408 ".ApplicationVisibilityCrossUserTest", 409 "testGetPackageGidsVisibility_currentUser", 410 testUserId); 411 Utils.runDeviceTests( 412 getDevice(), 413 TEST_WITH_PERMISSION_PKG, 414 ".ApplicationVisibilityCrossUserTest", 415 "testGetPackageGidsVisibility_anotherUserCrossUserGrant", 416 testUserId, 417 testArgs); 418 } 419 420 @Test 421 @AppModeFull(reason = "instant applications cannot see any other application") testGetPackageGidsCrossUserNoGrant()422 public void testGetPackageGidsCrossUserNoGrant() throws Exception { 423 if (!mSupportsMultiUser) { 424 return; 425 } 426 427 final int installUserId = getInstallUserId(); 428 final int testUserId = getTestUserId(); 429 final Map<String, String> testArgs = new HashMap<>(); 430 testArgs.put("testUser", Integer.toString(installUserId)); 431 432 installTestAppForUser(TINY_APK, installUserId); 433 // Also install TEST_WITH_PERMISSION_APK in installUSerId for creating across user context 434 installTestAppForUser(TEST_WITH_PERMISSION_APK, installUserId); 435 installTestAppForUser(TEST_WITH_PERMISSION_APK, testUserId); 436 437 Utils.runDeviceTests( 438 getDevice(), 439 TEST_WITH_PERMISSION_PKG, 440 ".ApplicationVisibilityCrossUserTest", 441 "testGetPackageGidsVisibility_currentUser", 442 testUserId); 443 Utils.runDeviceTests( 444 getDevice(), 445 TEST_WITH_PERMISSION_PKG, 446 ".ApplicationVisibilityCrossUserTest", 447 "testGetPackageGidsVisibility_anotherUserCrossUserNoGrant", 448 testUserId, 449 testArgs); 450 } 451 getInstallUserId()452 private int getInstallUserId() { 453 return mUsers[0]; 454 } 455 getTestUserId()456 private int getTestUserId() { 457 return mUsers[1]; 458 } 459 } 460