1 /* 2 * Copyright (C) 2019 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.appenumeration.cts; 18 19 import static android.Manifest.permission.SET_PREFERRED_APPLICATIONS; 20 import static android.appenumeration.cts.Constants.ACTION_APP_ENUMERATION_PREFERRED_ACTIVITY; 21 import static android.appenumeration.cts.Constants.ACTION_BIND_SERVICE; 22 import static android.appenumeration.cts.Constants.ACTION_CAN_PACKAGE_QUERIES; 23 import static android.appenumeration.cts.Constants.ACTION_CAN_PACKAGE_QUERY; 24 import static android.appenumeration.cts.Constants.ACTION_CHECK_PACKAGE; 25 import static android.appenumeration.cts.Constants.ACTION_CHECK_SIGNATURES; 26 import static android.appenumeration.cts.Constants.ACTION_CHECK_URI_PERMISSION; 27 import static android.appenumeration.cts.Constants.ACTION_GET_CONTENT_PROVIDER_MIME_TYPE; 28 import static android.appenumeration.cts.Constants.ACTION_GET_INSTALLED_ACCESSIBILITYSERVICES_PACKAGES; 29 import static android.appenumeration.cts.Constants.ACTION_GET_INSTALLED_APPWIDGET_PROVIDERS; 30 import static android.appenumeration.cts.Constants.ACTION_GET_INSTALLED_PACKAGES; 31 import static android.appenumeration.cts.Constants.ACTION_GET_NAMES_FOR_UIDS; 32 import static android.appenumeration.cts.Constants.ACTION_GET_NAME_FOR_UID; 33 import static android.appenumeration.cts.Constants.ACTION_GET_PACKAGES_FOR_UID; 34 import static android.appenumeration.cts.Constants.ACTION_GET_PACKAGE_INFO; 35 import static android.appenumeration.cts.Constants.ACTION_GET_PREFERRED_ACTIVITIES; 36 import static android.appenumeration.cts.Constants.ACTION_GET_SHAREDLIBRARY_DEPENDENT_PACKAGES; 37 import static android.appenumeration.cts.Constants.ACTION_GRANT_URI_PERMISSION; 38 import static android.appenumeration.cts.Constants.ACTION_HAS_SIGNING_CERTIFICATE; 39 import static android.appenumeration.cts.Constants.ACTION_JUST_FINISH; 40 import static android.appenumeration.cts.Constants.ACTION_MANIFEST_ACTIVITY; 41 import static android.appenumeration.cts.Constants.ACTION_MANIFEST_PROVIDER; 42 import static android.appenumeration.cts.Constants.ACTION_MANIFEST_SERVICE; 43 import static android.appenumeration.cts.Constants.ACTION_MANIFEST_UNEXPORTED_ACTIVITY; 44 import static android.appenumeration.cts.Constants.ACTION_PENDING_INTENT_GET_ACTIVITY; 45 import static android.appenumeration.cts.Constants.ACTION_PENDING_INTENT_GET_CREATOR_PACKAGE; 46 import static android.appenumeration.cts.Constants.ACTION_QUERY_ACTIVITIES; 47 import static android.appenumeration.cts.Constants.ACTION_QUERY_PROVIDERS; 48 import static android.appenumeration.cts.Constants.ACTION_QUERY_RESOLVER; 49 import static android.appenumeration.cts.Constants.ACTION_QUERY_SERVICES; 50 import static android.appenumeration.cts.Constants.ACTION_REVOKE_URI_PERMISSION; 51 import static android.appenumeration.cts.Constants.ACTION_SEND_RESULT; 52 import static android.appenumeration.cts.Constants.ACTION_SET_INSTALLER_PACKAGE_NAME; 53 import static android.appenumeration.cts.Constants.ACTION_START_DIRECTLY; 54 import static android.appenumeration.cts.Constants.ACTION_START_FOR_RESULT; 55 import static android.appenumeration.cts.Constants.ACTION_TAKE_PERSISTABLE_URI_PERMISSION; 56 import static android.appenumeration.cts.Constants.ACTIVITY_CLASS_DUMMY_ACTIVITY; 57 import static android.appenumeration.cts.Constants.ACTIVITY_CLASS_NOT_EXPORTED; 58 import static android.appenumeration.cts.Constants.ACTIVITY_CLASS_PERMISSION_PROTECTED; 59 import static android.appenumeration.cts.Constants.ACTIVITY_CLASS_TEST; 60 import static android.appenumeration.cts.Constants.AUTHORITY_SUFFIX; 61 import static android.appenumeration.cts.Constants.EXTRA_AUTHORITY; 62 import static android.appenumeration.cts.Constants.EXTRA_CERT; 63 import static android.appenumeration.cts.Constants.EXTRA_DATA; 64 import static android.appenumeration.cts.Constants.EXTRA_FLAGS; 65 import static android.appenumeration.cts.Constants.EXTRA_PENDING_INTENT; 66 import static android.appenumeration.cts.Constants.QUERIES_ACTIVITY_ACTION; 67 import static android.appenumeration.cts.Constants.QUERIES_NOTHING; 68 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_APK; 69 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_PERM; 70 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_PROVIDER; 71 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_PROVIDER_APK; 72 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_Q; 73 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI; 74 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI_APK; 75 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_PERM_URI; 76 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_PERM_URI_APK; 77 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI; 78 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI_APK; 79 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_URI; 80 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_RECEIVES_URI_APK; 81 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_SEES_INSTALLER; 82 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_SEES_INSTALLER_APK; 83 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_SHARED_USER; 84 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_USES_LIBRARY; 85 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_USES_OPTIONAL_LIBRARY; 86 import static android.appenumeration.cts.Constants.QUERIES_PACKAGE; 87 import static android.appenumeration.cts.Constants.QUERIES_PACKAGE_PROVIDER; 88 import static android.appenumeration.cts.Constants.QUERIES_PROVIDER_ACTION; 89 import static android.appenumeration.cts.Constants.QUERIES_PROVIDER_AUTH; 90 import static android.appenumeration.cts.Constants.QUERIES_SERVICE_ACTION; 91 import static android.appenumeration.cts.Constants.QUERIES_UNEXPORTED_ACTIVITY_ACTION; 92 import static android.appenumeration.cts.Constants.QUERIES_UNEXPORTED_PROVIDER_ACTION; 93 import static android.appenumeration.cts.Constants.QUERIES_UNEXPORTED_PROVIDER_AUTH; 94 import static android.appenumeration.cts.Constants.QUERIES_UNEXPORTED_SERVICE_ACTION; 95 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_ACTION; 96 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_BROWSABLE; 97 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_BROWSER; 98 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_CONTACTS; 99 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_EDITOR; 100 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_SHARE; 101 import static android.appenumeration.cts.Constants.QUERIES_WILDCARD_WEB; 102 import static android.appenumeration.cts.Constants.SERVICE_CLASS_SELF_VISIBILITY_SERVICE; 103 import static android.appenumeration.cts.Constants.SPLIT_BASE_APK; 104 import static android.appenumeration.cts.Constants.SPLIT_FEATURE_APK; 105 import static android.appenumeration.cts.Constants.SPLIT_PKG; 106 import static android.appenumeration.cts.Constants.TARGET_APPWIDGETPROVIDER; 107 import static android.appenumeration.cts.Constants.TARGET_APPWIDGETPROVIDER_SHARED_USER; 108 import static android.appenumeration.cts.Constants.TARGET_BROWSER; 109 import static android.appenumeration.cts.Constants.TARGET_BROWSER_WILDCARD; 110 import static android.appenumeration.cts.Constants.TARGET_CONTACTS; 111 import static android.appenumeration.cts.Constants.TARGET_EDITOR; 112 import static android.appenumeration.cts.Constants.TARGET_FILTERS; 113 import static android.appenumeration.cts.Constants.TARGET_FILTERS_APK; 114 import static android.appenumeration.cts.Constants.TARGET_FORCEQUERYABLE; 115 import static android.appenumeration.cts.Constants.TARGET_FORCEQUERYABLE_NORMAL; 116 import static android.appenumeration.cts.Constants.TARGET_NO_API; 117 import static android.appenumeration.cts.Constants.TARGET_PREFERRED_ACTIVITY; 118 import static android.appenumeration.cts.Constants.TARGET_PREFIX_WILDCARD_WEB; 119 import static android.appenumeration.cts.Constants.TARGET_SHARE; 120 import static android.appenumeration.cts.Constants.TARGET_SHARED_LIBRARY_PACKAGE; 121 import static android.appenumeration.cts.Constants.TARGET_SHARED_USER; 122 import static android.appenumeration.cts.Constants.TARGET_STUB; 123 import static android.appenumeration.cts.Constants.TARGET_STUB_APK; 124 import static android.appenumeration.cts.Constants.TARGET_SYNCADAPTER; 125 import static android.appenumeration.cts.Constants.TARGET_WEB; 126 import static android.appenumeration.cts.Constants.TEST_NONEXISTENT_PACKAGE_NAME_1; 127 import static android.appenumeration.cts.Constants.TEST_NONEXISTENT_PACKAGE_NAME_2; 128 import static android.appenumeration.cts.Constants.TEST_SHARED_LIB_NAME; 129 import static android.appenumeration.cts.Utils.Result; 130 import static android.appenumeration.cts.Utils.ThrowingBiFunction; 131 import static android.appenumeration.cts.Utils.allowTestApiAccess; 132 import static android.appenumeration.cts.Utils.clearAppDataForUser; 133 import static android.appenumeration.cts.Utils.ensurePackageIsInstalled; 134 import static android.appenumeration.cts.Utils.ensurePackageIsNotInstalled; 135 import static android.appenumeration.cts.Utils.forceStopPackage; 136 import static android.appenumeration.cts.Utils.installPackage; 137 import static android.appenumeration.cts.Utils.resetTestApiAccess; 138 import static android.appenumeration.cts.Utils.suspendPackages; 139 import static android.appenumeration.cts.Utils.uninstallPackage; 140 import static android.content.Intent.EXTRA_PACKAGES; 141 import static android.content.Intent.EXTRA_UID; 142 import static android.content.pm.PackageManager.GET_SIGNING_CERTIFICATES; 143 import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; 144 import static android.content.pm.PackageManager.SIGNATURE_MATCH; 145 import static android.content.pm.PackageManager.SIGNATURE_UNKNOWN_PACKAGE; 146 import static android.os.Process.INVALID_UID; 147 import static android.os.Process.ROOT_UID; 148 149 import static org.hamcrest.MatcherAssert.assertThat; 150 import static org.hamcrest.Matchers.allOf; 151 import static org.hamcrest.Matchers.arrayContaining; 152 import static org.hamcrest.Matchers.arrayContainingInAnyOrder; 153 import static org.hamcrest.Matchers.arrayWithSize; 154 import static org.hamcrest.Matchers.containsString; 155 import static org.hamcrest.Matchers.emptyOrNullString; 156 import static org.hamcrest.Matchers.equalTo; 157 import static org.hamcrest.Matchers.greaterThan; 158 import static org.hamcrest.Matchers.greaterThanOrEqualTo; 159 import static org.hamcrest.core.Is.is; 160 import static org.junit.Assert.assertEquals; 161 import static org.junit.Assert.assertFalse; 162 import static org.junit.Assert.assertNotNull; 163 import static org.junit.Assert.assertThrows; 164 import static org.junit.Assert.assertTrue; 165 import static org.junit.Assert.fail; 166 import static org.junit.Assume.assumeTrue; 167 168 import android.Manifest; 169 import android.app.PendingIntent; 170 import android.appwidget.AppWidgetProviderInfo; 171 import android.content.ActivityNotFoundException; 172 import android.content.ComponentName; 173 import android.content.Intent; 174 import android.content.IntentFilter; 175 import android.content.pm.PackageInfo; 176 import android.content.pm.PackageManager; 177 import android.content.pm.PackageManager.PackageInfoFlags; 178 import android.content.pm.ServiceInfo; 179 import android.content.pm.Signature; 180 import android.content.res.Resources; 181 import android.net.Uri; 182 import android.os.Bundle; 183 184 import androidx.test.filters.LargeTest; 185 186 import com.android.bedstead.nene.users.UserReference; 187 import com.android.compatibility.common.util.AmUtils; 188 import com.android.compatibility.common.util.SystemUtil; 189 190 import org.hamcrest.core.IsNull; 191 import org.junit.After; 192 import org.junit.Assert; 193 import org.junit.Before; 194 import org.junit.Test; 195 import org.junit.runner.RunWith; 196 import org.junit.runners.Parameterized; 197 198 import java.io.ByteArrayInputStream; 199 import java.io.InputStream; 200 import java.io.PrintWriter; 201 import java.io.StringWriter; 202 import java.security.cert.Certificate; 203 import java.security.cert.CertificateFactory; 204 import java.security.cert.X509Certificate; 205 import java.util.ArrayList; 206 import java.util.Arrays; 207 import java.util.List; 208 209 @RunWith(Parameterized.class) 210 public class AppEnumerationTests extends AppEnumerationTestsBase { 211 212 private static final String SKIP_APP_FILTER_CACHE = "0"; 213 private static final String USE_APP_FILTER_CACHE = "1"; 214 215 @Parameterized.Parameter 216 public String mUseAppFilterCache; 217 218 @Parameterized.Parameters initParameters()219 public static Iterable<Object> initParameters() { 220 return Arrays.asList(SKIP_APP_FILTER_CACHE, USE_APP_FILTER_CACHE); 221 } 222 223 @Before onBefore()224 public void onBefore() throws Exception { 225 setSystemProperty("debug.pm.use_app_filter_cache", mUseAppFilterCache); 226 } 227 228 @After onAfter()229 public void onAfter() throws Exception { 230 setSystemProperty("debug.pm.use_app_filter_cache", "invalid"); 231 } 232 233 @Test systemPackagesQueryable_notEnabled()234 public void systemPackagesQueryable_notEnabled() throws Exception { 235 final Resources resources = Resources.getSystem(); 236 assertFalse( 237 "config_forceSystemPackagesQueryable must not be true.", 238 resources.getBoolean(resources.getIdentifier( 239 "config_forceSystemPackagesQueryable", "bool", "android"))); 240 241 // now let's assert that the actual set of system apps is limited 242 assertThat("Not all system apps should be visible.", 243 getInstalledPackages(QUERIES_NOTHING_PERM, MATCH_SYSTEM_ONLY).length, 244 greaterThan(getInstalledPackages(QUERIES_NOTHING, MATCH_SYSTEM_ONLY).length)); 245 } 246 247 @Test all_canSeeForceQueryable()248 public void all_canSeeForceQueryable() throws Exception { 249 assertVisible(QUERIES_NOTHING, TARGET_FORCEQUERYABLE); 250 assertVisible(QUERIES_ACTIVITY_ACTION, TARGET_FORCEQUERYABLE); 251 assertVisible(QUERIES_SERVICE_ACTION, TARGET_FORCEQUERYABLE); 252 assertVisible(QUERIES_PROVIDER_AUTH, TARGET_FORCEQUERYABLE); 253 assertVisible(QUERIES_PACKAGE, TARGET_FORCEQUERYABLE); 254 } 255 256 @Test all_cannotSeeForceQueryableInstalledNormally()257 public void all_cannotSeeForceQueryableInstalledNormally() throws Exception { 258 assertNotVisible(QUERIES_NOTHING, TARGET_FORCEQUERYABLE_NORMAL); 259 assertNotVisible(QUERIES_ACTIVITY_ACTION, TARGET_FORCEQUERYABLE_NORMAL); 260 assertNotVisible(QUERIES_SERVICE_ACTION, TARGET_FORCEQUERYABLE_NORMAL); 261 assertNotVisible(QUERIES_PROVIDER_AUTH, TARGET_FORCEQUERYABLE_NORMAL); 262 assertNotVisible(QUERIES_PACKAGE, TARGET_FORCEQUERYABLE_NORMAL); 263 } 264 265 @Test startExplicitly_canStartNonVisible()266 public void startExplicitly_canStartNonVisible() throws Exception { 267 assertNotVisible(QUERIES_NOTHING, TARGET_FILTERS); 268 startExplicitIntentViaComponent(QUERIES_NOTHING, TARGET_FILTERS); 269 startExplicitIntentViaPackageName(QUERIES_NOTHING, TARGET_FILTERS); 270 } 271 272 @Test startExplicitly_cannotStartNonVisibleNonExported()273 public void startExplicitly_cannotStartNonVisibleNonExported() throws Exception { 274 assertNotVisible(QUERIES_NOTHING, TARGET_FILTERS); 275 Assert.assertThrows("Start of non-exported activity should act as if app not installed", 276 ActivityNotFoundException.class, 277 () -> startExplicitIntentNotExportedViaComponent( 278 QUERIES_NOTHING, TARGET_FILTERS)); 279 } 280 281 @Test startExplicitly_cannotStartVisibleNonExported()282 public void startExplicitly_cannotStartVisibleNonExported() throws Exception { 283 assertVisible(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS); 284 Assert.assertThrows("Start of non-exported activity should fail with SecurityException", 285 SecurityException.class, 286 () -> startExplicitIntentNotExportedViaComponent( 287 QUERIES_ACTIVITY_ACTION, TARGET_FILTERS)); 288 } 289 290 @Test startExplicitly_canStartVisible()291 public void startExplicitly_canStartVisible() throws Exception { 292 assertVisible(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS); 293 startExplicitIntentViaComponent(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS); 294 startExplicitIntentViaPackageName(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS); 295 } 296 297 @Test startExplicitly_activityPermissionProtected_canSeeTarget()298 public void startExplicitly_activityPermissionProtected_canSeeTarget() { 299 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 300 Assert.assertThrows(SecurityException.class, 301 () -> startExplicitPermissionProtectedIntentViaComponent( 302 QUERIES_ACTIVITY_ACTION, TARGET_STUB)); 303 Assert.assertThrows(SecurityException.class, 304 () -> startExplicitIntentViaPackageName(QUERIES_ACTIVITY_ACTION, TARGET_STUB)); 305 } 306 307 @Test startExplicitly_activityPermissionProtected_cannotSeeTarget()308 public void startExplicitly_activityPermissionProtected_cannotSeeTarget() { 309 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 310 Assert.assertThrows(ActivityNotFoundException.class, 311 () -> startExplicitPermissionProtectedIntentViaComponent( 312 QUERIES_NOTHING, TARGET_STUB)); 313 Assert.assertThrows(ActivityNotFoundException.class, 314 () -> startExplicitIntentViaPackageName(QUERIES_NOTHING, TARGET_STUB)); 315 } 316 317 @Test startImplicitly_canStartNonVisible()318 public void startImplicitly_canStartNonVisible() throws Exception { 319 assertNotVisible(QUERIES_NOTHING, TARGET_FILTERS); 320 startImplicitIntent(QUERIES_NOTHING); 321 } 322 323 @Test startActivityWithNoPermissionUri_canSeeProvider()324 public void startActivityWithNoPermissionUri_canSeeProvider() throws Exception { 325 uninstallPackage(QUERIES_NOTHING_RECEIVES_URI); 326 installPackage(QUERIES_NOTHING_RECEIVES_URI_APK); 327 328 assertNotVisible(QUERIES_NOTHING_RECEIVES_URI, QUERIES_NOTHING_PERM); 329 330 // send with uri but no grant flags; shouldn't be visible 331 startExplicitActivityWithIntent(QUERIES_NOTHING_PERM, QUERIES_NOTHING_RECEIVES_URI, 332 new Intent(ACTION_JUST_FINISH) 333 .setData(Uri.parse("content://" + QUERIES_NOTHING_PERM + "/test"))); 334 assertNotVisible(QUERIES_NOTHING_RECEIVES_URI, QUERIES_NOTHING_PERM); 335 336 // send again with uri bug grant flags now set; should be visible 337 startExplicitActivityWithIntent(QUERIES_NOTHING_PERM, QUERIES_NOTHING_RECEIVES_URI, 338 new Intent(ACTION_JUST_FINISH) 339 .setData(Uri.parse("content://" + QUERIES_NOTHING_PERM + "/test")) 340 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)); 341 assertVisible(QUERIES_NOTHING_RECEIVES_URI, QUERIES_NOTHING_PERM); 342 } 343 344 @Test startActivityWithUri_canSeePermissionProtectedProvider()345 public void startActivityWithUri_canSeePermissionProtectedProvider() throws Exception { 346 uninstallPackage(QUERIES_NOTHING_RECEIVES_PERM_URI); 347 installPackage(QUERIES_NOTHING_RECEIVES_PERM_URI_APK); 348 349 assertNotVisible(QUERIES_NOTHING_RECEIVES_PERM_URI, QUERIES_NOTHING_PERM); 350 351 // send with uri but no grant flags; shouldn't be visible 352 // Setting a dummy type to mitigate test failure. Bug: b/273465805 353 startExplicitActivityWithIntent(QUERIES_NOTHING_PERM, QUERIES_NOTHING_RECEIVES_PERM_URI, 354 new Intent(ACTION_JUST_FINISH) 355 .setDataAndType( 356 Uri.parse("content://" + QUERIES_NOTHING_PERM + "2/test"), 357 "null")); 358 359 assertNotVisible(QUERIES_NOTHING_RECEIVES_PERM_URI, QUERIES_NOTHING_PERM); 360 361 // send again with uri bug grant flags now set; should be visible 362 startExplicitActivityWithIntent(QUERIES_NOTHING_PERM, QUERIES_NOTHING_RECEIVES_PERM_URI, 363 new Intent(ACTION_JUST_FINISH) 364 .setDataAndType( 365 Uri.parse("content://" + QUERIES_NOTHING_PERM + "2/test"), 366 "null") 367 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)); 368 assertVisible(QUERIES_NOTHING_RECEIVES_PERM_URI, QUERIES_NOTHING_PERM); 369 } 370 371 @Test startActivityWithUriGrant_cannotSeeProviderAfterUpdated()372 public void startActivityWithUriGrant_cannotSeeProviderAfterUpdated() throws Exception { 373 assertNotVisible(QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI, QUERIES_NOTHING_PERM); 374 375 // send with uri grant flags; should be visible 376 // Setting a dummy type to mitigate test failure. Bug: b/273465805 377 startExplicitActivityWithIntent(QUERIES_NOTHING_PERM, 378 QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI, 379 new Intent(ACTION_JUST_FINISH) 380 .setDataAndType( 381 Uri.parse("content://" + QUERIES_NOTHING_PERM + "3/test"), 382 "null") 383 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)); 384 assertVisible(QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI, QUERIES_NOTHING_PERM); 385 386 // update the package; shouldn't be visible 387 installPackage(QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI_APK); 388 // Wait until the updating is done 389 AmUtils.waitForBroadcastBarrier(); 390 assertNotVisible(QUERIES_NOTHING_RECEIVES_NON_PERSISTABLE_URI, QUERIES_NOTHING_PERM); 391 } 392 393 @Test startActivityWithPersistableUriGrant_canSeeProviderAfterUpdated()394 public void startActivityWithPersistableUriGrant_canSeeProviderAfterUpdated() throws Exception { 395 uninstallPackage(QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI); 396 installPackage(QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI_APK); 397 398 assertNotVisible(QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI, QUERIES_NOTHING_PERM); 399 400 // send with persistable uri grant flags; should be visible 401 // Setting a dummy type to mitigate test failure. Bug: b/273465805 402 startExplicitActivityWithIntent(QUERIES_NOTHING_PERM, 403 QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI, 404 new Intent(ACTION_TAKE_PERSISTABLE_URI_PERMISSION) 405 .setDataAndType( 406 Uri.parse("content://" + QUERIES_NOTHING_PERM + "3/test"), 407 "null") 408 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION 409 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)); 410 assertVisible(QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI, QUERIES_NOTHING_PERM); 411 412 // update the package; should be still visible 413 installPackage(QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI_APK); 414 // Wait until the updating is done 415 AmUtils.waitForBroadcastBarrier(); 416 assertVisible(QUERIES_NOTHING_RECEIVES_PERSISTABLE_URI, QUERIES_NOTHING_PERM); 417 } 418 startExplicitActivityWithIntent( String sourcePackageName, String targetPackageName, Intent intent)419 private void startExplicitActivityWithIntent( 420 String sourcePackageName, String targetPackageName, Intent intent) throws Exception { 421 sendCommandBlocking(sourcePackageName, targetPackageName, 422 intent.setClassName(targetPackageName, ACTIVITY_CLASS_TEST), 423 ACTION_START_DIRECTLY); 424 } 425 426 @Test queriesNothing_cannotSeeNonForceQueryable()427 public void queriesNothing_cannotSeeNonForceQueryable() throws Exception { 428 assertNotVisible(QUERIES_NOTHING, TARGET_NO_API); 429 assertNotVisible(QUERIES_NOTHING, TARGET_FILTERS); 430 } 431 432 @Test queriesNothingTargetsQ_canSeeAll()433 public void queriesNothingTargetsQ_canSeeAll() throws Exception { 434 assertVisible(QUERIES_NOTHING_Q, TARGET_FORCEQUERYABLE); 435 assertVisible(QUERIES_NOTHING_Q, TARGET_NO_API); 436 assertVisible(QUERIES_NOTHING_Q, TARGET_FILTERS); 437 } 438 439 @Test queriesNothingHasPermission_canSeeAll()440 public void queriesNothingHasPermission_canSeeAll() throws Exception { 441 assertVisible(QUERIES_NOTHING_PERM, TARGET_FORCEQUERYABLE); 442 assertVisible(QUERIES_NOTHING_PERM, TARGET_NO_API); 443 assertVisible(QUERIES_NOTHING_PERM, TARGET_FILTERS); 444 } 445 446 @Test queriesNothing_cannotSeeFilters()447 public void queriesNothing_cannotSeeFilters() throws Exception { 448 assertNotQueryable(QUERIES_NOTHING, TARGET_FILTERS, 449 ACTION_MANIFEST_ACTIVITY, this::queryIntentActivities); 450 assertNotQueryable(QUERIES_NOTHING, TARGET_FILTERS, 451 ACTION_MANIFEST_SERVICE, this::queryIntentServices); 452 assertNotQueryable(QUERIES_NOTHING, TARGET_FILTERS, 453 ACTION_MANIFEST_PROVIDER, this::queryIntentProviders); 454 } 455 456 @Test queriesActivityAction_canSeeFilters()457 public void queriesActivityAction_canSeeFilters() throws Exception { 458 assertQueryable(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS, 459 ACTION_MANIFEST_ACTIVITY, this::queryIntentActivities); 460 assertQueryable(QUERIES_SERVICE_ACTION, TARGET_FILTERS, 461 ACTION_MANIFEST_SERVICE, this::queryIntentServices); 462 assertQueryable(QUERIES_PROVIDER_AUTH, TARGET_FILTERS, 463 ACTION_MANIFEST_PROVIDER, this::queryIntentProviders); 464 assertQueryable(QUERIES_PROVIDER_ACTION, TARGET_FILTERS, 465 ACTION_MANIFEST_PROVIDER, this::queryIntentProviders); 466 } 467 468 @Test queriesNothingHasPermission_canSeeFilters()469 public void queriesNothingHasPermission_canSeeFilters() throws Exception { 470 assertQueryable(QUERIES_NOTHING_PERM, TARGET_FILTERS, 471 ACTION_MANIFEST_ACTIVITY, this::queryIntentActivities); 472 assertQueryable(QUERIES_NOTHING_PERM, TARGET_FILTERS, 473 ACTION_MANIFEST_SERVICE, this::queryIntentServices); 474 assertQueryable(QUERIES_NOTHING_PERM, TARGET_FILTERS, 475 ACTION_MANIFEST_PROVIDER, this::queryIntentProviders); 476 } 477 478 @Test queriesSomething_cannotSeeNoApi()479 public void queriesSomething_cannotSeeNoApi() throws Exception { 480 assertNotVisible(QUERIES_ACTIVITY_ACTION, TARGET_NO_API); 481 assertNotVisible(QUERIES_SERVICE_ACTION, TARGET_NO_API); 482 assertNotVisible(QUERIES_PROVIDER_AUTH, TARGET_NO_API); 483 assertNotVisible(QUERIES_PROVIDER_ACTION, TARGET_NO_API); 484 } 485 486 @Test queriesActivityAction_canSeeTarget()487 public void queriesActivityAction_canSeeTarget() throws Exception { 488 assertVisible(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS); 489 } 490 491 @Test queriesServiceAction_canSeeTarget()492 public void queriesServiceAction_canSeeTarget() throws Exception { 493 assertVisible(QUERIES_SERVICE_ACTION, TARGET_FILTERS); 494 } 495 496 @Test queriesWildcardAction_canSeeTargets()497 public void queriesWildcardAction_canSeeTargets() throws Exception { 498 assertVisible(QUERIES_WILDCARD_ACTION, TARGET_FILTERS); 499 } 500 501 @Test queriesProviderAuthority_canSeeTarget()502 public void queriesProviderAuthority_canSeeTarget() throws Exception { 503 assertVisible(QUERIES_PROVIDER_AUTH, TARGET_FILTERS); 504 } 505 506 @Test queriesProviderAction_canSeeTarget()507 public void queriesProviderAction_canSeeTarget() throws Exception { 508 assertVisible(QUERIES_PROVIDER_ACTION, TARGET_FILTERS); 509 } 510 511 @Test queriesActivityAction_cannotSeeUnexportedTarget()512 public void queriesActivityAction_cannotSeeUnexportedTarget() throws Exception { 513 assertNotVisible(QUERIES_UNEXPORTED_ACTIVITY_ACTION, TARGET_FILTERS); 514 } 515 516 @Test queriesServiceAction_cannotSeeUnexportedTarget()517 public void queriesServiceAction_cannotSeeUnexportedTarget() throws Exception { 518 assertNotVisible(QUERIES_UNEXPORTED_SERVICE_ACTION, TARGET_FILTERS); 519 } 520 521 @Test queriesProviderAuthority_cannotSeeUnexportedTarget()522 public void queriesProviderAuthority_cannotSeeUnexportedTarget() throws Exception { 523 assertNotVisible(QUERIES_UNEXPORTED_PROVIDER_AUTH, TARGET_FILTERS); 524 } 525 526 @Test queriesProviderAction_cannotSeeUnexportedTarget()527 public void queriesProviderAction_cannotSeeUnexportedTarget() throws Exception { 528 assertNotVisible(QUERIES_UNEXPORTED_PROVIDER_ACTION, TARGET_FILTERS); 529 } 530 531 @Test queriesPackage_canSeeTarget()532 public void queriesPackage_canSeeTarget() throws Exception { 533 assertVisible(QUERIES_PACKAGE, TARGET_NO_API); 534 } 535 536 @Test queriesNothing_canSeeInstaller()537 public void queriesNothing_canSeeInstaller() throws Exception { 538 uninstallPackage(QUERIES_NOTHING_SEES_INSTALLER); 539 installPackage(QUERIES_NOTHING_SEES_INSTALLER_APK, TARGET_NO_API); 540 try { 541 assertVisible(QUERIES_NOTHING_SEES_INSTALLER, TARGET_NO_API); 542 } finally { 543 uninstallPackage(QUERIES_NOTHING_SEES_INSTALLER); 544 } 545 } 546 547 @Test whenStarted_canSeeCaller()548 public void whenStarted_canSeeCaller() throws Exception { 549 uninstallPackage(QUERIES_NOTHING); 550 installPackage(QUERIES_NOTHING_APK); 551 552 // let's first make sure that the target cannot see the caller. 553 assertNotVisible(QUERIES_NOTHING, QUERIES_NOTHING_PERM); 554 // now let's start the target and make sure that it can see the caller as part of that call 555 PackageInfo packageInfo = startForResult(QUERIES_NOTHING_PERM, QUERIES_NOTHING); 556 assertThat(packageInfo, IsNull.notNullValue()); 557 assertThat(packageInfo.packageName, is(QUERIES_NOTHING_PERM)); 558 // and finally let's re-run the last check to make sure that the target can still see the 559 // caller 560 assertVisible(QUERIES_NOTHING, QUERIES_NOTHING_PERM); 561 } 562 563 @Test whenStartedViaIntentSender_canSeeCaller()564 public void whenStartedViaIntentSender_canSeeCaller() throws Exception { 565 uninstallPackage(QUERIES_NOTHING); 566 installPackage(QUERIES_NOTHING_APK); 567 568 // let's first make sure that the target cannot see the caller. 569 assertNotVisible(QUERIES_NOTHING, QUERIES_NOTHING_Q); 570 // now let's start the target via pending intent and make sure that it can see the caller 571 // as part of that call 572 PackageInfo packageInfo = startSenderForResult(QUERIES_NOTHING_Q, QUERIES_NOTHING); 573 assertThat(packageInfo, IsNull.notNullValue()); 574 assertThat(packageInfo.packageName, is(QUERIES_NOTHING_Q)); 575 // and finally let's re-run the last check to make sure that the target can still see the 576 // caller 577 assertVisible(QUERIES_NOTHING, QUERIES_NOTHING_Q); 578 } 579 580 @Test queriesNothing_cannotSeeLibraryPackage()581 public void queriesNothing_cannotSeeLibraryPackage() throws Exception { 582 assertNotVisible(QUERIES_NOTHING, TARGET_SHARED_LIBRARY_PACKAGE); 583 } 584 585 @Test queriesNothingUsesLibrary_canSeeLibraryPackage()586 public void queriesNothingUsesLibrary_canSeeLibraryPackage() throws Exception { 587 assertVisible(QUERIES_NOTHING_USES_LIBRARY, TARGET_SHARED_LIBRARY_PACKAGE); 588 } 589 590 @Test queriesNothing_cannotSeeOptionalLibraryPackage()591 public void queriesNothing_cannotSeeOptionalLibraryPackage() throws Exception { 592 assertNotVisible(QUERIES_NOTHING, TARGET_SHARED_LIBRARY_PACKAGE); 593 } 594 595 @Test queriesNothingUsesOptionalLibrary_canSeeLibraryPackage()596 public void queriesNothingUsesOptionalLibrary_canSeeLibraryPackage() throws Exception { 597 assertVisible(QUERIES_NOTHING_USES_OPTIONAL_LIBRARY, TARGET_SHARED_LIBRARY_PACKAGE); 598 } 599 600 @Test queriesNothing_getPackagesForUid_consistentVisibility()601 public void queriesNothing_getPackagesForUid_consistentVisibility() 602 throws Exception { 603 final int targetSharedUid = sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 604 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 605 Assert.assertNull(getPackagesForUid(QUERIES_NOTHING, targetSharedUid)); 606 Assert.assertNull(getPackagesForUid(QUERIES_NOTHING, targetUid)); 607 } 608 609 @Test queriesNothingHasPermission_getPackagesForUid_consistentVisibility()610 public void queriesNothingHasPermission_getPackagesForUid_consistentVisibility() 611 throws Exception { 612 final int targetSharedUid = sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 613 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 614 Assert.assertNotNull(getPackagesForUid(QUERIES_NOTHING_PERM, targetSharedUid)); 615 Assert.assertNotNull(getPackagesForUid(QUERIES_NOTHING_PERM, targetUid)); 616 } 617 618 @Test queriesNothing_getNameForUid_consistentVisibility()619 public void queriesNothing_getNameForUid_consistentVisibility() 620 throws Exception { 621 final int targetSharedUid = sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 622 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 623 Assert.assertNull(getNameForUid(QUERIES_NOTHING, targetSharedUid)); 624 Assert.assertNull(getNameForUid(QUERIES_NOTHING, targetUid)); 625 } 626 627 @Test queriesNothingHasPermission_getNameForUid_consistentVisibility()628 public void queriesNothingHasPermission_getNameForUid_consistentVisibility() 629 throws Exception { 630 final int targetSharedUid = sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 631 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 632 Assert.assertNotNull(getNameForUid(QUERIES_NOTHING_PERM, targetSharedUid)); 633 Assert.assertNotNull(getNameForUid(QUERIES_NOTHING_PERM, targetUid)); 634 } 635 636 @Test queriesNothing_getNamesForUids_consistentVisibility()637 public void queriesNothing_getNamesForUids_consistentVisibility() 638 throws Exception { 639 try { 640 allowTestApiAccess(QUERIES_NOTHING); 641 642 final int targetSharedUid = 643 sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 644 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 645 Assert.assertNull(getNamesForUids(QUERIES_NOTHING, targetSharedUid)[0]); 646 Assert.assertNull(getNamesForUids(QUERIES_NOTHING, targetUid)[0]); 647 } finally { 648 resetTestApiAccess(QUERIES_NOTHING); 649 } 650 } 651 652 @Test queriesNothingHasPermission_getNamesForUids_consistentVisibility()653 public void queriesNothingHasPermission_getNamesForUids_consistentVisibility() 654 throws Exception { 655 try { 656 allowTestApiAccess(QUERIES_NOTHING_PERM); 657 658 final int targetSharedUid = 659 sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 660 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 661 Assert.assertNotNull(getNamesForUids(QUERIES_NOTHING_PERM, targetSharedUid)[0]); 662 Assert.assertNotNull(getNamesForUids(QUERIES_NOTHING_PERM, targetUid)[0]); 663 } finally { 664 resetTestApiAccess(QUERIES_NOTHING_PERM); 665 } 666 } 667 668 @Test queriesNothing_checkSignatures_consistentVisibility()669 public void queriesNothing_checkSignatures_consistentVisibility() 670 throws Exception { 671 final int targetSharedUid = sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 672 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 673 Assert.assertEquals(SIGNATURE_UNKNOWN_PACKAGE, 674 checkSignatures(QUERIES_NOTHING, targetSharedUid)); 675 Assert.assertEquals(SIGNATURE_UNKNOWN_PACKAGE, 676 checkSignatures(QUERIES_NOTHING, targetUid)); 677 } 678 679 @Test queriesNothingHasPermission_checkSignatures_consistentVisibility()680 public void queriesNothingHasPermission_checkSignatures_consistentVisibility() 681 throws Exception { 682 final int targetSharedUid = sPm.getPackageUid(TARGET_SHARED_USER, PackageInfoFlags.of(0)); 683 final int targetUid = sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)); 684 Assert.assertEquals(SIGNATURE_MATCH, 685 checkSignatures(QUERIES_NOTHING_PERM, targetSharedUid)); 686 Assert.assertEquals(SIGNATURE_MATCH, checkSignatures(QUERIES_NOTHING_PERM, targetUid)); 687 } 688 689 @Test queriesNothing_hasSigningCertificate_consistentVisibility()690 public void queriesNothing_hasSigningCertificate_consistentVisibility() throws Exception { 691 final PackageInfo targetSharedUidInfo = sPm.getPackageInfo(TARGET_SHARED_USER, 692 PackageInfoFlags.of(GET_SIGNING_CERTIFICATES)); 693 final PackageInfo targetUidInfo = sPm.getPackageInfo(TARGET_FILTERS, 694 PackageInfoFlags.of(GET_SIGNING_CERTIFICATES)); 695 final byte[] targetSharedCert = convertSignaturesToCertificates( 696 targetSharedUidInfo.signingInfo.getApkContentsSigners()).get(0).getEncoded(); 697 final byte[] targetCert = convertSignaturesToCertificates( 698 targetUidInfo.signingInfo.getApkContentsSigners()).get(0).getEncoded(); 699 700 Assert.assertFalse( 701 hasSigningCertificate(QUERIES_NOTHING, targetSharedUidInfo.applicationInfo.uid, 702 targetSharedCert)); 703 Assert.assertFalse( 704 hasSigningCertificate(QUERIES_NOTHING, targetUidInfo.applicationInfo.uid, 705 targetCert)); 706 } 707 708 @Test queriesNothingHasPermission_hasSigningCertificate_consistentVisibility()709 public void queriesNothingHasPermission_hasSigningCertificate_consistentVisibility() 710 throws Exception { 711 final PackageInfo targetSharedUidInfo = sPm.getPackageInfo(TARGET_SHARED_USER, 712 PackageInfoFlags.of(GET_SIGNING_CERTIFICATES)); 713 final PackageInfo targetUidInfo = sPm.getPackageInfo(TARGET_FILTERS, 714 PackageInfoFlags.of(GET_SIGNING_CERTIFICATES)); 715 final byte[] targetSharedCert = convertSignaturesToCertificates( 716 targetSharedUidInfo.signingInfo.getApkContentsSigners()).get(0).getEncoded(); 717 final byte[] targetCert = convertSignaturesToCertificates( 718 targetUidInfo.signingInfo.getApkContentsSigners()).get(0).getEncoded(); 719 720 Assert.assertTrue( 721 hasSigningCertificate(QUERIES_NOTHING_PERM, targetSharedUidInfo.applicationInfo.uid, 722 targetSharedCert)); 723 Assert.assertTrue( 724 hasSigningCertificate(QUERIES_NOTHING_PERM, targetUidInfo.applicationInfo.uid, 725 targetCert)); 726 } 727 728 @Test sharedUserMember_canSeeOtherMember()729 public void sharedUserMember_canSeeOtherMember() throws Exception { 730 assertVisible(QUERIES_NOTHING_SHARED_USER, TARGET_SHARED_USER); 731 } 732 733 @Test queriesPackage_canSeeAllSharedUserMembers()734 public void queriesPackage_canSeeAllSharedUserMembers() throws Exception { 735 // explicitly queries target via manifest 736 assertVisible(QUERIES_PACKAGE, TARGET_SHARED_USER); 737 // implicitly granted visibility to other member of shared user 738 assertVisible(QUERIES_PACKAGE, QUERIES_NOTHING_SHARED_USER); 739 } 740 741 @Test queriesWildcardContacts()742 public void queriesWildcardContacts() throws Exception { 743 assertNotVisible(QUERIES_NOTHING, TARGET_CONTACTS); 744 assertVisible(QUERIES_WILDCARD_CONTACTS, TARGET_CONTACTS); 745 } 746 747 @Test queriesWildcardWeb()748 public void queriesWildcardWeb() throws Exception { 749 assertNotVisible(QUERIES_NOTHING, TARGET_WEB); 750 assertVisible(QUERIES_WILDCARD_BROWSABLE, TARGET_WEB); 751 assertVisible(QUERIES_WILDCARD_WEB, TARGET_WEB); 752 } 753 754 @Test queriesWildcardBrowser()755 public void queriesWildcardBrowser() throws Exception { 756 assertNotVisible(QUERIES_NOTHING, TARGET_BROWSER); 757 assertNotVisible(QUERIES_WILDCARD_BROWSER, TARGET_WEB); 758 assertVisible(QUERIES_WILDCARD_BROWSER, TARGET_BROWSER); 759 assertVisible(QUERIES_WILDCARD_BROWSER, TARGET_BROWSER_WILDCARD); 760 } 761 762 @Test queriesWildcardWeb_canSeePrefixWildcardWeb()763 public void queriesWildcardWeb_canSeePrefixWildcardWeb() throws Exception { 764 assertNotVisible(QUERIES_NOTHING, TARGET_PREFIX_WILDCARD_WEB); 765 assertVisible(QUERIES_WILDCARD_BROWSABLE, TARGET_PREFIX_WILDCARD_WEB); 766 assertVisible(QUERIES_WILDCARD_WEB, TARGET_PREFIX_WILDCARD_WEB); 767 } 768 769 @Test queriesWildcardBrowser_cannotSeePrefixWildcardWeb()770 public void queriesWildcardBrowser_cannotSeePrefixWildcardWeb() throws Exception { 771 assertNotVisible(QUERIES_NOTHING, TARGET_PREFIX_WILDCARD_WEB); 772 assertNotVisible(QUERIES_WILDCARD_BROWSER, TARGET_PREFIX_WILDCARD_WEB); 773 } 774 775 @Test queriesWildcardEditor()776 public void queriesWildcardEditor() throws Exception { 777 assertNotVisible(QUERIES_NOTHING, TARGET_EDITOR); 778 assertVisible(QUERIES_WILDCARD_EDITOR, TARGET_EDITOR); 779 } 780 781 @Test queriesWildcardShareSheet()782 public void queriesWildcardShareSheet() throws Exception { 783 assertNotVisible(QUERIES_NOTHING, TARGET_SHARE); 784 assertVisible(QUERIES_WILDCARD_SHARE, TARGET_SHARE); 785 } 786 787 @Test queriesNothing_cannotSeeA11yService()788 public void queriesNothing_cannotSeeA11yService() throws Exception { 789 try { 790 allowTestApiAccess(QUERIES_NOTHING); 791 792 assertNotVisible(QUERIES_NOTHING, TARGET_FILTERS, 793 this::getInstalledAccessibilityServices); 794 } finally { 795 resetTestApiAccess(QUERIES_NOTHING); 796 } 797 } 798 799 @Test queriesNothingHasPermission_canSeeA11yService()800 public void queriesNothingHasPermission_canSeeA11yService() throws Exception { 801 try { 802 allowTestApiAccess(QUERIES_NOTHING_PERM); 803 804 assertVisible(QUERIES_NOTHING_PERM, TARGET_FILTERS, 805 this::getInstalledAccessibilityServices); 806 } finally { 807 resetTestApiAccess(QUERIES_NOTHING_PERM); 808 } 809 } 810 811 @LargeTest 812 @Test broadcastAdded_notVisibleDoesNotReceive()813 public void broadcastAdded_notVisibleDoesNotReceive() throws Exception { 814 final Result result = sendCommand(QUERIES_NOTHING, TARGET_FILTERS, 815 /* targetUid */ INVALID_UID, /* intentExtra */ null, 816 Constants.ACTION_AWAIT_PACKAGE_ADDED, /* waitForReady */ true); 817 installPackage(TARGET_FILTERS_APK); 818 try { 819 result.await(); 820 fail(); 821 } catch (MissingBroadcastException e) { 822 // hooray 823 } 824 } 825 826 @Test broadcastAdded_visibleReceives()827 public void broadcastAdded_visibleReceives() throws Exception { 828 final Result result = sendCommand(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS, 829 /* targetUid */ INVALID_UID, /* intentExtra */ null, 830 Constants.ACTION_AWAIT_PACKAGE_ADDED, /* waitForReady */ true); 831 installPackage(TARGET_FILTERS_APK); 832 try { 833 Assert.assertEquals(TARGET_FILTERS, 834 Uri.parse(result.await().getString(EXTRA_DATA)).getSchemeSpecificPart()); 835 } catch (MissingBroadcastException e) { 836 fail(e.getMessage()); 837 } 838 } 839 840 @LargeTest 841 @Test reinstallTarget_broadcastRemoved_notVisibleDoesNotReceive()842 public void reinstallTarget_broadcastRemoved_notVisibleDoesNotReceive() throws Exception { 843 final Result result = sendCommand(QUERIES_NOTHING, TARGET_FILTERS, 844 /* targetUid */ INVALID_UID, /* intentExtra */ null, 845 Constants.ACTION_AWAIT_PACKAGE_REMOVED, /* waitForReady */ true); 846 installPackage(TARGET_FILTERS_APK); 847 try { 848 result.await(); 849 fail(); 850 } catch (MissingBroadcastException e) { 851 // hooray 852 } 853 } 854 855 @Test reinstallTarget_broadcastRemoved_visibleReceives()856 public void reinstallTarget_broadcastRemoved_visibleReceives() throws Exception { 857 final Result result = sendCommand(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS, 858 /* targetUid */ INVALID_UID, /* intentExtra */ null, 859 Constants.ACTION_AWAIT_PACKAGE_REMOVED, /* waitForReady */ true); 860 installPackage(TARGET_FILTERS_APK); 861 try { 862 Assert.assertEquals(TARGET_FILTERS, 863 Uri.parse(result.await().getString(EXTRA_DATA)).getSchemeSpecificPart()); 864 } catch (MissingBroadcastException e) { 865 fail(e.getMessage()); 866 } 867 } 868 869 @LargeTest 870 @Test uninstallTarget_broadcastRemoved_notVisibleDoesNotReceive()871 public void uninstallTarget_broadcastRemoved_notVisibleDoesNotReceive() throws Exception { 872 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 873 final Result result = sendCommand(QUERIES_NOTHING, TARGET_STUB, 874 /* targetUid */ INVALID_UID, /* intentExtra */ null, 875 Constants.ACTION_AWAIT_PACKAGE_REMOVED, /* waitForReady */ true); 876 uninstallPackage(TARGET_STUB); 877 try { 878 result.await(); 879 fail(); 880 } catch (MissingBroadcastException e) { 881 // hooray 882 } 883 } 884 885 @Test uninstallTarget_broadcastRemoved_visibleReceives()886 public void uninstallTarget_broadcastRemoved_visibleReceives() throws Exception { 887 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 888 final Result result = sendCommand(QUERIES_NOTHING_PERM, TARGET_STUB, 889 /* targetUid */ INVALID_UID, /* intentExtra */ null, 890 Constants.ACTION_AWAIT_PACKAGE_REMOVED, /* waitForReady */ true); 891 uninstallPackage(TARGET_STUB); 892 try { 893 Assert.assertEquals(TARGET_STUB, 894 Uri.parse(result.await().getString(EXTRA_DATA)).getSchemeSpecificPart()); 895 } catch (MissingBroadcastException e) { 896 fail(); 897 } 898 } 899 900 @LargeTest 901 @Test uninstallTarget_broadcastFullyRemoved_notVisibleDoesNotReceive()902 public void uninstallTarget_broadcastFullyRemoved_notVisibleDoesNotReceive() throws Exception { 903 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 904 final Result result = sendCommand(QUERIES_NOTHING, TARGET_STUB, 905 /* targetUid */ INVALID_UID, /* intentExtra */ null, 906 Constants.ACTION_AWAIT_PACKAGE_FULLY_REMOVED, /* waitForReady */ true); 907 uninstallPackage(TARGET_STUB); 908 try { 909 result.await(); 910 fail(); 911 } catch (MissingBroadcastException e) { 912 // hooray 913 } 914 } 915 916 @Test uninstallTarget_broadcastFullyRemoved_visibleReceives()917 public void uninstallTarget_broadcastFullyRemoved_visibleReceives() throws Exception { 918 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 919 final Result result = sendCommand(QUERIES_NOTHING_PERM, TARGET_STUB, 920 /* targetUid */ INVALID_UID, /* intentExtra */ null, 921 Constants.ACTION_AWAIT_PACKAGE_FULLY_REMOVED, /* waitForReady */ true); 922 uninstallPackage(TARGET_STUB); 923 try { 924 Assert.assertEquals(TARGET_STUB, 925 Uri.parse(result.await().getString(EXTRA_DATA)).getSchemeSpecificPart()); 926 } catch (MissingBroadcastException e) { 927 fail(); 928 } 929 } 930 931 @LargeTest 932 @Test clearTargetData_broadcastDataCleared_notVisibleDoesNotReceive()933 public void clearTargetData_broadcastDataCleared_notVisibleDoesNotReceive() throws Exception { 934 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 935 final Result result = sendCommand(QUERIES_NOTHING, TARGET_STUB, 936 /* targetUid */ INVALID_UID, /* intentExtra */ null, 937 Constants.ACTION_AWAIT_PACKAGE_DATA_CLEARED, /* waitForReady */ true); 938 clearAppDataForUser(TARGET_STUB, UserReference.of(sContext.getUser())); 939 try { 940 result.await(); 941 fail(); 942 } catch (MissingBroadcastException e) { 943 // hooray 944 } 945 } 946 947 @Test clearTargetData_broadcastDataCleared_visibleReceives()948 public void clearTargetData_broadcastDataCleared_visibleReceives() throws Exception { 949 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 950 final Result result = sendCommand(QUERIES_NOTHING_PERM, TARGET_STUB, 951 /* targetUid */ INVALID_UID, /* intentExtra */ null, 952 Constants.ACTION_AWAIT_PACKAGE_DATA_CLEARED, /* waitForReady */ true); 953 clearAppDataForUser(TARGET_STUB, UserReference.of(sContext.getUser())); 954 try { 955 Assert.assertEquals(TARGET_STUB, 956 Uri.parse(result.await().getString(EXTRA_DATA)).getSchemeSpecificPart()); 957 } catch (MissingBroadcastException e) { 958 fail(); 959 } 960 } 961 962 @Test broadcastRestarted_visibleReceives()963 public void broadcastRestarted_visibleReceives() throws Exception { 964 assertBroadcastRestartedVisible(QUERIES_PACKAGE, TARGET_NO_API, TARGET_NO_API); 965 } 966 967 @LargeTest 968 @Test broadcastRestarted_notVisibleDoesNotReceive()969 public void broadcastRestarted_notVisibleDoesNotReceive() throws Exception { 970 assertBroadcastRestartedVisible(QUERIES_NOTHING, /* expectedPackage */ null, TARGET_NO_API); 971 } 972 973 @Test broadcastSuspended_visibleReceives()974 public void broadcastSuspended_visibleReceives() throws Exception { 975 assertBroadcastSuspendedVisible(QUERIES_PACKAGE, 976 Arrays.asList(TARGET_NO_API, TARGET_SYNCADAPTER), 977 Arrays.asList(TARGET_NO_API, TARGET_SYNCADAPTER)); 978 } 979 980 @LargeTest 981 @Test broadcastSuspended_notVisibleDoesNotReceive()982 public void broadcastSuspended_notVisibleDoesNotReceive() throws Exception { 983 assertBroadcastSuspendedVisible(QUERIES_NOTHING, 984 Arrays.asList(), 985 Arrays.asList(TARGET_NO_API, TARGET_SYNCADAPTER)); 986 } 987 988 @LargeTest 989 @Test broadcastSuspended_visibleReceivesAndNotVisibleDoesNotReceive()990 public void broadcastSuspended_visibleReceivesAndNotVisibleDoesNotReceive() throws Exception { 991 assertBroadcastSuspendedVisible(QUERIES_ACTIVITY_ACTION, 992 Arrays.asList(TARGET_FILTERS), 993 Arrays.asList(TARGET_NO_API, TARGET_FILTERS)); 994 } 995 996 @Test queriesResolver_grantsVisibilityToProvider()997 public void queriesResolver_grantsVisibilityToProvider() throws Exception { 998 uninstallPackage(QUERIES_NOTHING_PROVIDER); 999 installPackage(QUERIES_NOTHING_PROVIDER_APK); 1000 1001 assertNotVisible(QUERIES_NOTHING_PROVIDER, QUERIES_NOTHING_PERM); 1002 1003 String[] result = sendCommandBlocking( 1004 QUERIES_NOTHING_PERM, QUERIES_NOTHING_PROVIDER, null, ACTION_QUERY_RESOLVER) 1005 .getStringArray(Intent.EXTRA_RETURN_RESULT); 1006 Arrays.sort(result); 1007 assertThat(QUERIES_NOTHING_PERM + " not visible to " + QUERIES_NOTHING_PROVIDER 1008 + " during resolver interaction", 1009 Arrays.binarySearch(result, QUERIES_NOTHING_PERM), 1010 greaterThanOrEqualTo(0)); 1011 1012 assertVisible(QUERIES_NOTHING_PROVIDER, QUERIES_NOTHING_PERM); 1013 } 1014 1015 @Test bindService_consistentVisibility()1016 public void bindService_consistentVisibility() throws Exception { 1017 // Ensure package visibility isn't impacted by optimization or cached result. 1018 // Target service shouldn't be visible to app without query permission even if 1019 // another app with query permission is binding it. 1020 assertServiceVisible(QUERIES_NOTHING_PERM, TARGET_FILTERS); 1021 assertServiceNotVisible(QUERIES_NOTHING, TARGET_FILTERS); 1022 } 1023 1024 @Test queriesPackage_canSeeAppWidgetProviderTarget()1025 public void queriesPackage_canSeeAppWidgetProviderTarget() throws Exception { 1026 assumeTrue(sPm.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)); 1027 1028 assertVisible(QUERIES_PACKAGE, TARGET_APPWIDGETPROVIDER, 1029 this::getInstalledAppWidgetProviders); 1030 } 1031 1032 @Test queriesNothing_cannotSeeAppWidgetProviderTarget()1033 public void queriesNothing_cannotSeeAppWidgetProviderTarget() throws Exception { 1034 assumeTrue(sPm.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)); 1035 1036 assertNotVisible(QUERIES_NOTHING, TARGET_APPWIDGETPROVIDER, 1037 this::getInstalledAppWidgetProviders); 1038 assertNotVisible(QUERIES_NOTHING, TARGET_APPWIDGETPROVIDER_SHARED_USER, 1039 this::getInstalledAppWidgetProviders); 1040 } 1041 1042 @Test queriesNothingSharedUser_canSeeAppWidgetProviderSharedUserTarget()1043 public void queriesNothingSharedUser_canSeeAppWidgetProviderSharedUserTarget() 1044 throws Exception { 1045 assumeTrue(sPm.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)); 1046 1047 assertVisible(QUERIES_NOTHING_SHARED_USER, TARGET_APPWIDGETPROVIDER_SHARED_USER, 1048 this::getInstalledAppWidgetProviders); 1049 } 1050 1051 @Test queriesNothing_cannotSeeSharedLibraryDependentPackages()1052 public void queriesNothing_cannotSeeSharedLibraryDependentPackages() throws Exception { 1053 assertNotVisible(QUERIES_NOTHING, TARGET_NO_API, this::getSharedLibraryDependentPackages); 1054 assertNotVisible(QUERIES_NOTHING, TARGET_FILTERS, this::getSharedLibraryDependentPackages); 1055 assertNotVisible(QUERIES_NOTHING, TARGET_SHARED_USER, 1056 this::getSharedLibraryDependentPackages); 1057 1058 } 1059 1060 @Test queriesPackage_canSeeSharedLibraryDependentPackages()1061 public void queriesPackage_canSeeSharedLibraryDependentPackages() throws Exception { 1062 assertVisible(QUERIES_PACKAGE, TARGET_NO_API, this::getSharedLibraryDependentPackages); 1063 assertVisible(QUERIES_PACKAGE, TARGET_SHARED_USER, this::getSharedLibraryDependentPackages); 1064 } 1065 1066 @Test queriesNothingSharedUser_canSeeSharedUserInSharedLibraryDependentPackages()1067 public void queriesNothingSharedUser_canSeeSharedUserInSharedLibraryDependentPackages() 1068 throws Exception { 1069 assertVisible(QUERIES_NOTHING_SHARED_USER, TARGET_SHARED_USER, 1070 this::getSharedLibraryDependentPackages); 1071 } 1072 1073 @Test queriesNothing_cannotSeePreferredActivityTarget()1074 public void queriesNothing_cannotSeePreferredActivityTarget() throws Exception { 1075 addPreferredActivity(); 1076 try { 1077 assertNotVisible(QUERIES_NOTHING, TARGET_PREFERRED_ACTIVITY, 1078 this::getPreferredActivities); 1079 } finally { 1080 clearPreferredActivity(); 1081 } 1082 } 1083 1084 @Test queriesPackage_canSeePreferredActivityTarget()1085 public void queriesPackage_canSeePreferredActivityTarget() throws Exception { 1086 addPreferredActivity(); 1087 try { 1088 assertVisible(QUERIES_PACKAGE, TARGET_PREFERRED_ACTIVITY, 1089 this::getPreferredActivities); 1090 } finally { 1091 clearPreferredActivity(); 1092 } 1093 } 1094 1095 @Test queriesNothing_setInstallerPackageName_targetIsNoApi_throwsException()1096 public void queriesNothing_setInstallerPackageName_targetIsNoApi_throwsException() { 1097 final Exception ex = assertThrows(IllegalArgumentException.class, 1098 () -> setInstallerPackageName(QUERIES_NOTHING, TARGET_NO_API, QUERIES_NOTHING)); 1099 assertThat(ex.getMessage(), containsString(TARGET_NO_API)); 1100 } 1101 1102 @Test queriesNothing_setInstallerPackageName_installerIsNoApi_throwsException()1103 public void queriesNothing_setInstallerPackageName_installerIsNoApi_throwsException() { 1104 final Exception ex = assertThrows(IllegalArgumentException.class, 1105 () -> setInstallerPackageName(QUERIES_NOTHING, QUERIES_NOTHING, TARGET_NO_API)); 1106 assertThat(ex.getMessage(), containsString(TARGET_NO_API)); 1107 } 1108 1109 @Test queriesPackageHasProvider_checkUriPermission_canSeeNoApi()1110 public void queriesPackageHasProvider_checkUriPermission_canSeeNoApi() throws Exception { 1111 final int permissionResult = checkUriPermission(QUERIES_PACKAGE_PROVIDER, TARGET_NO_API); 1112 assertThat(permissionResult, is(PackageManager.PERMISSION_GRANTED)); 1113 } 1114 1115 @Test queriesPackageHasProvider_checkUriPermission_cannotSeeFilters()1116 public void queriesPackageHasProvider_checkUriPermission_cannotSeeFilters() throws Exception { 1117 final int permissionResult = checkUriPermission(QUERIES_PACKAGE_PROVIDER, TARGET_FILTERS); 1118 assertThat(permissionResult, is(PackageManager.PERMISSION_DENIED)); 1119 } 1120 1121 @Test queriesPackageHasProvider_grantUriPermission_canSeeNoApi()1122 public void queriesPackageHasProvider_grantUriPermission_canSeeNoApi() throws Exception { 1123 try { 1124 grantUriPermission(QUERIES_PACKAGE_PROVIDER, TARGET_NO_API); 1125 assertThat(sContext.checkUriPermission( 1126 Uri.parse("content://" + QUERIES_PACKAGE_PROVIDER), 1127 0 /* pid */, 1128 sPm.getPackageUid(TARGET_NO_API, PackageInfoFlags.of(0)), 1129 Intent.FLAG_GRANT_READ_URI_PERMISSION), 1130 is(PackageManager.PERMISSION_GRANTED)); 1131 } finally { 1132 revokeUriPermission(QUERIES_PACKAGE_PROVIDER); 1133 } 1134 } 1135 1136 @Test queriesPackageHasProvider_grantUriPermission_cannotSeeFilters()1137 public void queriesPackageHasProvider_grantUriPermission_cannotSeeFilters() throws Exception { 1138 try { 1139 grantUriPermission(QUERIES_PACKAGE_PROVIDER, TARGET_FILTERS); 1140 assertThat(sContext.checkUriPermission( 1141 Uri.parse("content://" + QUERIES_PACKAGE_PROVIDER), 1142 0 /* pid */, 1143 sPm.getPackageUid(TARGET_FILTERS, PackageInfoFlags.of(0)), 1144 Intent.FLAG_GRANT_READ_URI_PERMISSION), 1145 is(PackageManager.PERMISSION_DENIED)); 1146 } finally { 1147 revokeUriPermission(QUERIES_PACKAGE_PROVIDER); 1148 } 1149 } 1150 1151 @Test queriesPackage_getType_canGetMimeType()1152 public void queriesPackage_getType_canGetMimeType() throws Exception { 1153 assertThat(getContentProviderMimeType(QUERIES_PACKAGE, TARGET_SYNCADAPTER), 1154 is("got/theMIME")); 1155 } 1156 1157 @Test queriesNothing_getType_cannotGetMimeType()1158 public void queriesNothing_getType_cannotGetMimeType() throws Exception { 1159 assertThat(getContentProviderMimeType(QUERIES_NOTHING, TARGET_SYNCADAPTER), 1160 IsNull.nullValue()); 1161 } 1162 1163 @Test setAutoRevokeWhitelisted_targetIsNotExisting_setFailed()1164 public void setAutoRevokeWhitelisted_targetIsNotExisting_setFailed() throws Exception { 1165 final boolean result = SystemUtil.callWithShellPermissionIdentity( 1166 () -> sPm.setAutoRevokeWhitelisted(TEST_NONEXISTENT_PACKAGE_NAME_1, 1167 false /* whitelisted */), 1168 Manifest.permission.WHITELIST_AUTO_REVOKE_PERMISSIONS); 1169 assertThat(result, is(false)); 1170 } 1171 1172 @Test setAutoRevokeWhitelisted_cannotSeeTarget_setFailed()1173 public void setAutoRevokeWhitelisted_cannotSeeTarget_setFailed() throws Exception { 1174 final boolean result = SystemUtil.callWithShellPermissionIdentity( 1175 () -> sPm.setAutoRevokeWhitelisted(QUERIES_PACKAGE, false /* whitelisted */), 1176 Manifest.permission.WHITELIST_AUTO_REVOKE_PERMISSIONS); 1177 assertThat(result, is(false)); 1178 } 1179 1180 @Test setAutoRevokeWhitelisted_canSeeTarget_setSuccessful()1181 public void setAutoRevokeWhitelisted_canSeeTarget_setSuccessful() throws Exception { 1182 final boolean result = SystemUtil.callWithShellPermissionIdentity( 1183 () -> sPm.setAutoRevokeWhitelisted(QUERIES_NOTHING, false /* whitelisted */), 1184 Manifest.permission.WHITELIST_AUTO_REVOKE_PERMISSIONS); 1185 assertThat(result, is(true)); 1186 } 1187 1188 @Test setAutoRevokeWhitelisted_withoutPermission_throwsException()1189 public void setAutoRevokeWhitelisted_withoutPermission_throwsException() throws Exception { 1190 assertThrows(SecurityException.class, 1191 () -> sPm.setAutoRevokeWhitelisted(QUERIES_NOTHING, false /* whitelisted */)); 1192 } 1193 1194 @Test canPackageQuery_queriesActivityAction_canSeeFilters()1195 public void canPackageQuery_queriesActivityAction_canSeeFilters() throws Exception { 1196 assertThat(sPm.canPackageQuery(QUERIES_ACTIVITY_ACTION, TARGET_FILTERS), 1197 is(true)); 1198 } 1199 1200 @Test canPackageQuery_queriesNothing_cannotSeeFilters()1201 public void canPackageQuery_queriesNothing_cannotSeeFilters() throws Exception { 1202 assertThat(sPm.canPackageQuery(QUERIES_NOTHING, TARGET_FILTERS), 1203 is(false)); 1204 } 1205 1206 @Test canPackageQuery_withNonexistentPackages()1207 public void canPackageQuery_withNonexistentPackages() { 1208 assertThrows(PackageManager.NameNotFoundException.class, 1209 () -> sPm.canPackageQuery( 1210 TEST_NONEXISTENT_PACKAGE_NAME_1, TEST_NONEXISTENT_PACKAGE_NAME_2)); 1211 assertThrows(PackageManager.NameNotFoundException.class, 1212 () -> sPm.canPackageQuery( 1213 QUERIES_NOTHING_PERM, TEST_NONEXISTENT_PACKAGE_NAME_2)); 1214 assertThrows(PackageManager.NameNotFoundException.class, 1215 () -> sPm.canPackageQuery( 1216 TEST_NONEXISTENT_PACKAGE_NAME_1, TARGET_FILTERS)); 1217 } 1218 1219 @Test canPackageQuery_callerHasNoPackageVisibility()1220 public void canPackageQuery_callerHasNoPackageVisibility() { 1221 assertThrows(PackageManager.NameNotFoundException.class, 1222 () -> canPackageQuery( 1223 QUERIES_NOTHING, QUERIES_ACTIVITY_ACTION, TARGET_FILTERS)); 1224 assertThrows(PackageManager.NameNotFoundException.class, 1225 () -> canPackageQuery( 1226 QUERIES_NOTHING_SHARED_USER, QUERIES_PACKAGE, TARGET_SHARED_USER)); 1227 } 1228 1229 @Test canPackageQuery_cannotDetectPackageExistence()1230 public void canPackageQuery_cannotDetectPackageExistence() { 1231 ensurePackageIsNotInstalled(TARGET_STUB); 1232 final Exception ex1 = assertThrows(PackageManager.NameNotFoundException.class, 1233 () -> canPackageQuery(QUERIES_NOTHING, TARGET_STUB, "")); 1234 final StringWriter stackTrace1 = new StringWriter(); 1235 ex1.printStackTrace(new PrintWriter(stackTrace1)); 1236 1237 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 1238 1239 final Exception ex2 = assertThrows(PackageManager.NameNotFoundException.class, 1240 () -> canPackageQuery(QUERIES_NOTHING, TARGET_STUB, "")); 1241 final StringWriter stackTrace2 = new StringWriter(); 1242 ex1.printStackTrace(new PrintWriter(stackTrace2)); 1243 1244 assertThat(ex1.getMessage(), is(ex2.getMessage())); 1245 assertThat(stackTrace1.toString(), is(stackTrace2.toString())); 1246 } 1247 1248 @Test canPackageQueries_queriesPackage_canSeeDeclaredPackages_cannotSeeOthers()1249 public void canPackageQueries_queriesPackage_canSeeDeclaredPackages_cannotSeeOthers() 1250 throws Exception { 1251 assertThat(canPackageQueries(QUERIES_NOTHING_PERM, QUERIES_PACKAGE, 1252 new String[]{ 1253 TARGET_NO_API, 1254 TARGET_BROWSER, 1255 TARGET_SHARED_USER, 1256 TARGET_WEB, 1257 TARGET_SYNCADAPTER}), 1258 allOf(arrayWithSize(5), arrayContaining(true, false, true, false, true))); 1259 } 1260 1261 @Test canPackageQueries_callerHasNoPackageVisibility()1262 public void canPackageQueries_callerHasNoPackageVisibility() { 1263 assertThrows(PackageManager.NameNotFoundException.class, 1264 () -> canPackageQueries(QUERIES_PACKAGE, TARGET_NO_API, 1265 new String[]{TARGET_SHARED_USER, TARGET_WEB})); 1266 } 1267 1268 @Test checkPackage_queriesNothing_validateFailed()1269 public void checkPackage_queriesNothing_validateFailed() { 1270 // Using ROOT_UID here to pass the check in #verifyAndGetBypass, this is intended by design. 1271 assertThrows(SecurityException.class, 1272 () -> checkPackage(QUERIES_NOTHING, TARGET_FILTERS, ROOT_UID)); 1273 } 1274 1275 @Test checkPackage_queriesNothing_targetIsNotExisting_validateFailed()1276 public void checkPackage_queriesNothing_targetIsNotExisting_validateFailed() { 1277 // Using ROOT_UID here to pass the check in #verifyAndGetBypass, this is intended by design. 1278 assertThrows(SecurityException.class, 1279 () -> checkPackage(QUERIES_NOTHING, TEST_NONEXISTENT_PACKAGE_NAME_1, ROOT_UID)); 1280 } 1281 1282 @Test checkPackage_queriesNothingHasPerm_validateSuccessful()1283 public void checkPackage_queriesNothingHasPerm_validateSuccessful() throws Exception { 1284 // Using ROOT_UID here to pass the check in #verifyAndGetBypass, this is intended by design. 1285 assertThat(checkPackage(QUERIES_NOTHING_PERM, TARGET_FILTERS, ROOT_UID), is(true)); 1286 } 1287 1288 @Test checkPackage_queriesNothingHasPerm_targetIsNotExisting_validateFailed()1289 public void checkPackage_queriesNothingHasPerm_targetIsNotExisting_validateFailed() 1290 throws Exception { 1291 // Using ROOT_UID here to pass the check in #verifyAndGetBypass, this is intended by design. 1292 assertThrows(SecurityException.class, 1293 () -> checkPackage(QUERIES_NOTHING_PERM, TEST_NONEXISTENT_PACKAGE_NAME_1, 1294 ROOT_UID)); 1295 } 1296 1297 @Test pendingIntent_getCreatorPackage_queriesPackage_canSeeNoApi()1298 public void pendingIntent_getCreatorPackage_queriesPackage_canSeeNoApi() 1299 throws Exception { 1300 final PendingIntent pendingIntent = getPendingIntentActivity(TARGET_NO_API); 1301 assertThat(getPendingIntentCreatorPackage(QUERIES_PACKAGE, pendingIntent), 1302 is(TARGET_NO_API)); 1303 } 1304 1305 @Test pendingIntent_getCreatorPackage_queriesNothing_cannotSeeNoApi()1306 public void pendingIntent_getCreatorPackage_queriesNothing_cannotSeeNoApi() 1307 throws Exception { 1308 final PendingIntent pendingIntent = getPendingIntentActivity(TARGET_NO_API); 1309 assertThat(getPendingIntentCreatorPackage(QUERIES_NOTHING, pendingIntent), 1310 is(emptyOrNullString())); 1311 } 1312 1313 @Test makeUidVisible_throwsException()1314 public void makeUidVisible_throwsException() throws Exception { 1315 final int recipientUid = sPm.getPackageUid( 1316 QUERIES_NOTHING, PackageManager.PackageInfoFlags.of(0)); 1317 final int visibleUid = sPm.getPackageUid( 1318 TARGET_NO_API, PackageManager.PackageInfoFlags.of(0)); 1319 assertThrows(SecurityException.class, 1320 () -> sPm.makeUidVisible(recipientUid, visibleUid)); 1321 } 1322 1323 @Test makeUidVisible_queriesNothing_canSeeStub()1324 public void makeUidVisible_queriesNothing_canSeeStub() throws Exception { 1325 ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK); 1326 try { 1327 assertNotVisible(QUERIES_NOTHING, TARGET_STUB); 1328 1329 final int recipientUid = sPm.getPackageUid( 1330 QUERIES_NOTHING, PackageManager.PackageInfoFlags.of(0)); 1331 final int visibleUid = sPm.getPackageUid( 1332 TARGET_STUB, PackageManager.PackageInfoFlags.of(0)); 1333 SystemUtil.runWithShellPermissionIdentity( 1334 () -> sPm.makeUidVisible(recipientUid, visibleUid), 1335 Manifest.permission.MAKE_UID_VISIBLE); 1336 1337 assertVisible(QUERIES_NOTHING, TARGET_STUB); 1338 } finally { 1339 ensurePackageIsNotInstalled(TARGET_STUB); 1340 } 1341 } 1342 1343 @Test testSelfVisibility()1344 public void testSelfVisibility() throws Exception { 1345 final ServiceInfo serviceInfo = sPm.getServiceInfo( 1346 new ComponentName(sContext, SERVICE_CLASS_SELF_VISIBILITY_SERVICE), 1347 PackageManager.GET_META_DATA); 1348 assertNotNull(serviceInfo); 1349 } 1350 1351 @Test grantImplicitAccessByUriGrant_canSeeProviderAfterUpdatedWithDontKill()1352 public void grantImplicitAccessByUriGrant_canSeeProviderAfterUpdatedWithDontKill() 1353 throws Exception { 1354 try { 1355 new InstallMultiple().addApk(SPLIT_BASE_APK).run(); 1356 assertThat(sPm.canPackageQuery(SPLIT_PKG, QUERIES_PACKAGE_PROVIDER), 1357 is(false)); 1358 grantUriPermission(QUERIES_PACKAGE_PROVIDER, SPLIT_PKG); 1359 assertThat(sPm.canPackageQuery(SPLIT_PKG, QUERIES_PACKAGE_PROVIDER), 1360 is(true)); 1361 1362 new InstallMultiple().addApk(SPLIT_FEATURE_APK) 1363 .inheritFrom(SPLIT_PKG).dontKill().run(); 1364 1365 assertThat(sPm.canPackageQuery(SPLIT_PKG, QUERIES_PACKAGE_PROVIDER), 1366 is(true)); 1367 } finally { 1368 revokeUriPermission(QUERIES_PACKAGE_PROVIDER); 1369 uninstallPackage(SPLIT_PKG); 1370 } 1371 } 1372 assertVisible(String sourcePackageName, String targetPackageName)1373 private void assertVisible(String sourcePackageName, String targetPackageName) 1374 throws Exception { 1375 Assert.assertNotNull(sourcePackageName + " should be able to see " + targetPackageName, 1376 getPackageInfo(sourcePackageName, targetPackageName)); 1377 } 1378 assertNotVisible(String sourcePackageName, String targetPackageName)1379 private void assertNotVisible(String sourcePackageName, String targetPackageName) 1380 throws Exception { 1381 try { 1382 getPackageInfo(sourcePackageName, targetPackageName); 1383 fail(sourcePackageName + " should not be able to see " + targetPackageName); 1384 } catch (PackageManager.NameNotFoundException ignored) { 1385 } 1386 } 1387 assertServiceVisible(String sourcePackageName, String targetPackageName)1388 private void assertServiceVisible(String sourcePackageName, String targetPackageName) 1389 throws Exception { 1390 assertTrue(bindService(sourcePackageName, targetPackageName)); 1391 } 1392 assertServiceNotVisible(String sourcePackageName, String targetPackageName)1393 private void assertServiceNotVisible(String sourcePackageName, String targetPackageName) 1394 throws Exception { 1395 assertFalse(bindService(sourcePackageName, targetPackageName)); 1396 } 1397 assertNotQueryable(String sourcePackageName, String targetPackageName, String intentAction, ThrowingBiFunction<String, Intent, String[]> commandMethod)1398 private void assertNotQueryable(String sourcePackageName, String targetPackageName, 1399 String intentAction, ThrowingBiFunction<String, Intent, String[]> commandMethod) 1400 throws Exception { 1401 Intent intent = new Intent(intentAction); 1402 String[] queryablePackageNames = commandMethod.apply(sourcePackageName, intent); 1403 for (String packageName : queryablePackageNames) { 1404 if (packageName.contentEquals(targetPackageName)) { 1405 fail(sourcePackageName + " should not be able to query " + targetPackageName + 1406 " via " + intentAction); 1407 } 1408 } 1409 } 1410 assertQueryable(String sourcePackageName, String targetPackageName, String intentAction, ThrowingBiFunction<String, Intent, String[]> commandMethod)1411 private void assertQueryable(String sourcePackageName, String targetPackageName, 1412 String intentAction, ThrowingBiFunction<String, Intent, String[]> commandMethod) 1413 throws Exception { 1414 Intent intent = new Intent(intentAction); 1415 String[] queryablePackageNames = commandMethod.apply(sourcePackageName, intent); 1416 for (String packageName : queryablePackageNames) { 1417 if (packageName.contentEquals(targetPackageName)) { 1418 return; 1419 } 1420 } 1421 fail(sourcePackageName + " should be able to query " + targetPackageName + " via " 1422 + intentAction); 1423 } 1424 assertBroadcastRestartedVisible(String sourcePackageName, String expectedPackage, String packageToRestart)1425 private void assertBroadcastRestartedVisible(String sourcePackageName, 1426 String expectedPackage, String packageToRestart) throws Exception { 1427 final Bundle extras = new Bundle(); 1428 extras.putString(Intent.EXTRA_PACKAGE_NAME, packageToRestart); 1429 final Result result = sendCommand(sourcePackageName, /* targetPackageName */ null, 1430 /* targetUid */ INVALID_UID, extras, Constants.ACTION_AWAIT_PACKAGE_RESTARTED, 1431 /* waitForReady */ true); 1432 forceStopPackage(packageToRestart); 1433 final String restartedPackages = result.await().getString(Intent.EXTRA_PACKAGE_NAME, 1434 null /* defaultValue */); 1435 assertThat(restartedPackages, expectedPackage == null ? IsNull.nullValue() 1436 : equalTo(expectedPackage)); 1437 } 1438 assertBroadcastSuspendedVisible(String sourcePackageName, List<String> expectedVisiblePackages, List<String> packagesToSuspend)1439 private void assertBroadcastSuspendedVisible(String sourcePackageName, 1440 List<String> expectedVisiblePackages, List<String> packagesToSuspend) 1441 throws Exception { 1442 final Bundle extras = new Bundle(); 1443 extras.putStringArray(EXTRA_PACKAGES, packagesToSuspend.toArray(new String[] {})); 1444 final Result result = sendCommand(sourcePackageName, /* targetPackageName */ null, 1445 /* targetUid */ INVALID_UID, extras, Constants.ACTION_AWAIT_PACKAGES_SUSPENDED, 1446 /* waitForReady */ true); 1447 try { 1448 suspendPackages(true /* suspend */, packagesToSuspend); 1449 final String[] suspendedPackages = result.await().getStringArray(EXTRA_PACKAGES); 1450 assertThat(suspendedPackages, arrayContainingInAnyOrder( 1451 expectedVisiblePackages.toArray())); 1452 } finally { 1453 suspendPackages(false /* suspend */, packagesToSuspend); 1454 } 1455 } 1456 getInstalledAccessibilityServices(String sourcePackageName)1457 private String[] getInstalledAccessibilityServices (String sourcePackageName) 1458 throws Exception { 1459 final Bundle response = sendCommandBlocking(sourcePackageName, null /*targetPackageName*/, 1460 null /*queryIntent*/, ACTION_GET_INSTALLED_ACCESSIBILITYSERVICES_PACKAGES); 1461 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1462 } 1463 getPackageInfo(String sourcePackageName, String targetPackageName)1464 private PackageInfo getPackageInfo(String sourcePackageName, String targetPackageName) 1465 throws Exception { 1466 Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName, 1467 null /*queryIntent*/, ACTION_GET_PACKAGE_INFO); 1468 return response.getParcelable(Intent.EXTRA_RETURN_RESULT, PackageInfo.class); 1469 } 1470 getPackagesForUid(String sourcePackageName, int targetUid)1471 private String[] getPackagesForUid(String sourcePackageName, int targetUid) 1472 throws Exception { 1473 final Bundle response = sendCommandBlocking(sourcePackageName, targetUid, 1474 /* intentExtra */ null, ACTION_GET_PACKAGES_FOR_UID); 1475 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1476 } 1477 getNameForUid(String sourcePackageName, int targetUid)1478 private String getNameForUid(String sourcePackageName, int targetUid) throws Exception { 1479 final Bundle response = sendCommandBlocking(sourcePackageName, targetUid, 1480 /* intentExtra */ null, ACTION_GET_NAME_FOR_UID); 1481 return response.getString(Intent.EXTRA_RETURN_RESULT); 1482 } 1483 getNamesForUids(String sourcePackageName, int targetUid)1484 private String[] getNamesForUids(String sourcePackageName, int targetUid) throws Exception { 1485 final Bundle response = sendCommandBlocking(sourcePackageName, targetUid, 1486 /* intentExtra */ null, ACTION_GET_NAMES_FOR_UIDS); 1487 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1488 } 1489 checkSignatures(String sourcePackageName, int targetUid)1490 private int checkSignatures(String sourcePackageName, int targetUid) throws Exception { 1491 final Bundle response = sendCommandBlocking(sourcePackageName, targetUid, 1492 /* intentExtra */ null, ACTION_CHECK_SIGNATURES); 1493 return response.getInt(Intent.EXTRA_RETURN_RESULT); 1494 } 1495 hasSigningCertificate(String sourcePackageName, int targetUid, byte[] cert)1496 private boolean hasSigningCertificate(String sourcePackageName, int targetUid, byte[] cert) 1497 throws Exception { 1498 final Bundle extra = new Bundle(); 1499 extra.putByteArray(EXTRA_CERT, cert); 1500 final Bundle response = sendCommandBlocking(sourcePackageName, targetUid, extra, 1501 ACTION_HAS_SIGNING_CERTIFICATE); 1502 return response.getBoolean(Intent.EXTRA_RETURN_RESULT); 1503 } 1504 convertSignaturesToCertificates(Signature[] signatures)1505 private List<Certificate> convertSignaturesToCertificates(Signature[] signatures) 1506 throws Exception { 1507 final CertificateFactory cf = CertificateFactory.getInstance("X.509"); 1508 ArrayList<Certificate> certs = new ArrayList<>(signatures.length); 1509 for (Signature signature : signatures) { 1510 final InputStream is = new ByteArrayInputStream(signature.toByteArray()); 1511 final X509Certificate cert = (X509Certificate) cf.generateCertificate(is); 1512 certs.add(cert); 1513 } 1514 return certs; 1515 } 1516 checkPackage(String sourcePackageName, String targetPackageName, int targetUid)1517 private boolean checkPackage(String sourcePackageName, String targetPackageName, int targetUid) 1518 throws Exception { 1519 final Bundle extra = new Bundle(); 1520 extra.putInt(EXTRA_UID, targetUid); 1521 final Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName, extra, 1522 ACTION_CHECK_PACKAGE); 1523 return response.getBoolean(Intent.EXTRA_RETURN_RESULT); 1524 } 1525 startForResult(String sourcePackageName, String targetPackageName)1526 private PackageInfo startForResult(String sourcePackageName, String targetPackageName) 1527 throws Exception { 1528 Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName, 1529 null /*queryIntent*/, ACTION_START_FOR_RESULT); 1530 return response.getParcelable(Intent.EXTRA_RETURN_RESULT, PackageInfo.class); 1531 } 1532 startSenderForResult(String sourcePackageName, String targetPackageName)1533 private PackageInfo startSenderForResult(String sourcePackageName, String targetPackageName) 1534 throws Exception { 1535 PendingIntent pendingIntent = PendingIntent.getActivity(sContext, 100 /* requestCode */, 1536 new Intent(ACTION_SEND_RESULT).setComponent( 1537 new ComponentName(targetPackageName, ACTIVITY_CLASS_TEST)), 1538 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); 1539 1540 Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName, 1541 pendingIntent /*queryIntent*/, Constants.ACTION_START_SENDER_FOR_RESULT); 1542 return response.getParcelable(Intent.EXTRA_RETURN_RESULT, PackageInfo.class); 1543 } 1544 queryIntentActivities(String sourcePackageName, Intent queryIntent)1545 private String[] queryIntentActivities(String sourcePackageName, Intent queryIntent) 1546 throws Exception { 1547 Bundle response = 1548 sendCommandBlocking(sourcePackageName, null, queryIntent, ACTION_QUERY_ACTIVITIES); 1549 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1550 } 1551 queryIntentServices(String sourcePackageName, Intent queryIntent)1552 private String[] queryIntentServices(String sourcePackageName, Intent queryIntent) 1553 throws Exception { 1554 Bundle response = sendCommandBlocking(sourcePackageName, null, queryIntent, 1555 ACTION_QUERY_SERVICES); 1556 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1557 } 1558 queryIntentProviders(String sourcePackageName, Intent queryIntent)1559 private String[] queryIntentProviders(String sourcePackageName, Intent queryIntent) 1560 throws Exception { 1561 Bundle response = sendCommandBlocking(sourcePackageName, null, queryIntent, 1562 ACTION_QUERY_PROVIDERS); 1563 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1564 } 1565 getInstalledPackages(String sourcePackageNames, int flags)1566 private String[] getInstalledPackages(String sourcePackageNames, int flags) throws Exception { 1567 Bundle response = sendCommandBlocking(sourcePackageNames, null, 1568 new Intent().putExtra(EXTRA_FLAGS, flags), ACTION_GET_INSTALLED_PACKAGES); 1569 return response.getStringArray(Intent.EXTRA_RETURN_RESULT); 1570 } 1571 startExplicitIntentViaComponent(String sourcePackage, String targetPackage)1572 private void startExplicitIntentViaComponent(String sourcePackage, String targetPackage) 1573 throws Exception { 1574 sendCommandBlocking(sourcePackage, targetPackage, 1575 new Intent(ACTION_MANIFEST_ACTIVITY) 1576 .setClassName(targetPackage, ACTIVITY_CLASS_DUMMY_ACTIVITY), 1577 ACTION_START_DIRECTLY); 1578 } startExplicitIntentNotExportedViaComponent( String sourcePackage, String targetPackage)1579 private void startExplicitIntentNotExportedViaComponent( 1580 String sourcePackage, String targetPackage) throws Exception { 1581 sendCommandBlocking(sourcePackage, targetPackage, 1582 new Intent(ACTION_MANIFEST_UNEXPORTED_ACTIVITY) 1583 .setClassName(targetPackage, ACTIVITY_CLASS_NOT_EXPORTED), 1584 ACTION_START_DIRECTLY); 1585 } startExplicitIntentViaPackageName(String sourcePackage, String targetPackage)1586 private void startExplicitIntentViaPackageName(String sourcePackage, String targetPackage) 1587 throws Exception { 1588 sendCommandBlocking(sourcePackage, targetPackage, 1589 new Intent(ACTION_MANIFEST_ACTIVITY).setPackage(targetPackage), 1590 ACTION_START_DIRECTLY); 1591 } 1592 startExplicitPermissionProtectedIntentViaComponent( String sourcePackage, String targetPackage)1593 private void startExplicitPermissionProtectedIntentViaComponent( 1594 String sourcePackage, String targetPackage) throws Exception { 1595 sendCommandBlocking(sourcePackage, targetPackage, 1596 new Intent(ACTION_MANIFEST_ACTIVITY) 1597 .setClassName(targetPackage, ACTIVITY_CLASS_PERMISSION_PROTECTED), 1598 ACTION_START_DIRECTLY); 1599 } 1600 startImplicitIntent(String sourcePackage)1601 private void startImplicitIntent(String sourcePackage) throws Exception { 1602 sendCommandBlocking(sourcePackage, TARGET_FILTERS, new Intent(ACTION_MANIFEST_ACTIVITY), 1603 ACTION_START_DIRECTLY); 1604 } 1605 bindService(String sourcePackageName, String targetPackageName)1606 private boolean bindService(String sourcePackageName, String targetPackageName) 1607 throws Exception { 1608 final Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName, 1609 /* intentExtra */ null, ACTION_BIND_SERVICE); 1610 return response.getBoolean(Intent.EXTRA_RETURN_RESULT); 1611 } 1612 getInstalledAppWidgetProviders(String sourcePackageName)1613 private String[] getInstalledAppWidgetProviders(String sourcePackageName) throws Exception { 1614 final Bundle response = sendCommandBlocking(sourcePackageName, /* targetPackageName */ null, 1615 /* intentExtra */ null, ACTION_GET_INSTALLED_APPWIDGET_PROVIDERS); 1616 final List<AppWidgetProviderInfo> infos = response.getParcelableArrayList( 1617 Intent.EXTRA_RETURN_RESULT, AppWidgetProviderInfo.class); 1618 return infos.stream() 1619 .map(info -> info.provider.getPackageName()) 1620 .distinct() 1621 .toArray(String[]::new); 1622 } 1623 getSharedLibraryDependentPackages(String sourcePackageName)1624 private String[] getSharedLibraryDependentPackages(String sourcePackageName) throws Exception { 1625 final Bundle extraData = new Bundle(); 1626 final Bundle response = sendCommandBlocking(sourcePackageName, TEST_SHARED_LIB_NAME, 1627 extraData, ACTION_GET_SHAREDLIBRARY_DEPENDENT_PACKAGES); 1628 return response.getStringArray(Intent.EXTRA_PACKAGES); 1629 } 1630 getPreferredActivities(String sourcePackageName)1631 private String[] getPreferredActivities(String sourcePackageName) throws Exception { 1632 final Bundle extraData = new Bundle(); 1633 final Bundle response = sendCommandBlocking(sourcePackageName, null /* targetPackageName */, 1634 extraData, ACTION_GET_PREFERRED_ACTIVITIES); 1635 return response.getStringArray(Intent.EXTRA_PACKAGES); 1636 } 1637 setInstallerPackageName(String sourcePackageName, String targetPackageName, String installerPackageName)1638 private void setInstallerPackageName(String sourcePackageName, String targetPackageName, 1639 String installerPackageName) throws Exception { 1640 final Bundle extraData = new Bundle(); 1641 extraData.putString(Intent.EXTRA_INSTALLER_PACKAGE_NAME, installerPackageName); 1642 sendCommandBlocking(sourcePackageName, targetPackageName, 1643 extraData, ACTION_SET_INSTALLER_PACKAGE_NAME); 1644 } 1645 checkUriPermission(String sourcePackageName, String targetPackageName)1646 private int checkUriPermission(String sourcePackageName, String targetPackageName) 1647 throws Exception { 1648 final int targetUid = sPm.getPackageUid(targetPackageName, PackageInfoFlags.of(0)); 1649 final Bundle extraData = new Bundle(); 1650 extraData.putString(EXTRA_AUTHORITY, sourcePackageName); 1651 final Result result = sendCommand(sourcePackageName, targetPackageName, targetUid, 1652 extraData, ACTION_CHECK_URI_PERMISSION, /* waitForReady */ false); 1653 final Bundle response = result.await(); 1654 return response.getInt(Intent.EXTRA_RETURN_RESULT); 1655 } 1656 grantUriPermission(String providerPackageName, String targetPackageName)1657 private void grantUriPermission(String providerPackageName, String targetPackageName) 1658 throws Exception { 1659 final Bundle extraData = new Bundle(); 1660 extraData.putString(EXTRA_AUTHORITY, providerPackageName); 1661 sendCommandBlocking(providerPackageName, targetPackageName, extraData, 1662 ACTION_GRANT_URI_PERMISSION); 1663 } 1664 revokeUriPermission(String providerPackageName)1665 private void revokeUriPermission(String providerPackageName) throws Exception { 1666 final Bundle extraData = new Bundle(); 1667 extraData.putString(EXTRA_AUTHORITY, providerPackageName); 1668 sendCommandBlocking(providerPackageName, null /* targetPackageName */, extraData, 1669 ACTION_REVOKE_URI_PERMISSION); 1670 } 1671 getContentProviderMimeType(String sourcePackageName, String targetPackageName)1672 private String getContentProviderMimeType(String sourcePackageName, String targetPackageName) 1673 throws Exception { 1674 final Bundle extraData = new Bundle(); 1675 extraData.putString(EXTRA_AUTHORITY, targetPackageName + AUTHORITY_SUFFIX); 1676 final Bundle response = sendCommandBlocking(sourcePackageName, /* targetPackageName */ null, 1677 extraData, ACTION_GET_CONTENT_PROVIDER_MIME_TYPE); 1678 return response.getString(Intent.EXTRA_RETURN_RESULT); 1679 } 1680 canPackageQuery(String callerPackageName, String sourcePackageName, String targetPackageName)1681 private boolean canPackageQuery(String callerPackageName, String sourcePackageName, 1682 String targetPackageName) throws Exception { 1683 final Bundle extraData = new Bundle(); 1684 extraData.putString(Intent.EXTRA_PACKAGE_NAME, targetPackageName); 1685 final Bundle response = sendCommandBlocking(callerPackageName, sourcePackageName, 1686 extraData, ACTION_CAN_PACKAGE_QUERY); 1687 return response.getBoolean(Intent.EXTRA_RETURN_RESULT); 1688 } 1689 canPackageQueries(String callerPackageName, String sourcePackageName, String[] targetPackageNames)1690 private Boolean[] canPackageQueries(String callerPackageName, String sourcePackageName, 1691 String[] targetPackageNames) throws Exception { 1692 final Bundle extraData = new Bundle(); 1693 extraData.putStringArray(EXTRA_PACKAGES, targetPackageNames); 1694 final Bundle response = sendCommandBlocking(callerPackageName, sourcePackageName, 1695 extraData, ACTION_CAN_PACKAGE_QUERIES); 1696 return toBooleanArray(response.getBooleanArray(Intent.EXTRA_RETURN_RESULT)); 1697 } 1698 getPendingIntentActivity(String sourcePackageName)1699 private PendingIntent getPendingIntentActivity(String sourcePackageName) throws Exception { 1700 final Bundle bundle = sendCommandBlocking(sourcePackageName, null /* targetPackageName */, 1701 null /* intentExtra */, ACTION_PENDING_INTENT_GET_ACTIVITY); 1702 return bundle.getParcelable(EXTRA_PENDING_INTENT, PendingIntent.class); 1703 } 1704 getPendingIntentCreatorPackage(String sourcePackageName, PendingIntent pendingIntent)1705 private String getPendingIntentCreatorPackage(String sourcePackageName, 1706 PendingIntent pendingIntent) throws Exception { 1707 final Bundle bundle = sendCommandBlocking(sourcePackageName, null /* targetPackageName */, 1708 pendingIntent, ACTION_PENDING_INTENT_GET_CREATOR_PACKAGE); 1709 return bundle.getString(Intent.EXTRA_PACKAGE_NAME); 1710 } 1711 addPreferredActivity()1712 private void addPreferredActivity() { 1713 final IntentFilter filter = new IntentFilter( 1714 ACTION_APP_ENUMERATION_PREFERRED_ACTIVITY); 1715 final ComponentName[] candidates = {new ComponentName(TARGET_PREFERRED_ACTIVITY, 1716 ACTIVITY_CLASS_DUMMY_ACTIVITY)}; 1717 SystemUtil.runWithShellPermissionIdentity(() -> { 1718 sPm.addPreferredActivity(filter, IntentFilter.MATCH_ADJUSTMENT_NORMAL, 1719 candidates, candidates[0]); 1720 }, SET_PREFERRED_APPLICATIONS); 1721 } 1722 clearPreferredActivity()1723 private void clearPreferredActivity() { 1724 SystemUtil.runWithShellPermissionIdentity(() -> { 1725 sPm.clearPackagePreferredActivities(TARGET_PREFERRED_ACTIVITY); 1726 }, SET_PREFERRED_APPLICATIONS); 1727 } 1728 setSystemProperty(String name, String value)1729 private void setSystemProperty(String name, String value) throws Exception { 1730 assertEquals("", SystemUtil.runShellCommand("setprop " + name + " " + value)); 1731 } 1732 toBooleanArray(boolean[] from)1733 private static Boolean[] toBooleanArray(boolean[] from) { 1734 final Boolean[] copies = new Boolean[from.length]; 1735 for (int i = 0; i < from.length; i++) { 1736 copies[i] = from[i]; 1737 } 1738 return copies; 1739 } 1740 } 1741