1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.server.pm; 17 18 import static android.content.res.Resources.ID_NULL; 19 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertSame; 23 import static org.junit.Assert.assertTrue; 24 25 import android.content.pm.ActivityInfo; 26 import android.content.pm.ApplicationInfo; 27 import android.content.pm.ConfigurationInfo; 28 import android.content.pm.FeatureGroupInfo; 29 import android.content.pm.FeatureInfo; 30 import android.content.pm.InstrumentationInfo; 31 import android.content.pm.PackageParser; 32 import android.content.pm.ProviderInfo; 33 import android.content.pm.ServiceInfo; 34 import android.content.pm.SharedLibraryInfo; 35 import android.content.pm.Signature; 36 import android.os.Bundle; 37 import android.os.Parcel; 38 import android.platform.test.annotations.Presubmit; 39 import android.util.ArrayMap; 40 import android.util.ArraySet; 41 42 import androidx.test.filters.MediumTest; 43 import androidx.test.filters.SmallTest; 44 import androidx.test.runner.AndroidJUnit4; 45 46 import libcore.io.IoUtils; 47 48 import org.junit.Before; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 52 import java.io.File; 53 import java.lang.reflect.Array; 54 import java.lang.reflect.Field; 55 import java.nio.charset.StandardCharsets; 56 import java.util.ArrayList; 57 import java.util.Arrays; 58 import java.util.HashSet; 59 import java.util.List; 60 import java.util.Set; 61 62 @RunWith(AndroidJUnit4.class) 63 @MediumTest 64 public class PackageParserTest { 65 private File mTmpDir; 66 private static final File FRAMEWORK = new File("/system/framework/framework-res.apk"); 67 68 @Before setUp()69 public void setUp() { 70 // Create a new temporary directory for each of our tests. 71 mTmpDir = IoUtils.createTemporaryDirectory("PackageParserTest"); 72 } 73 74 @Test testParse_noCache()75 public void testParse_noCache() throws Exception { 76 PackageParser pp = new CachePackageNameParser(); 77 PackageParser.Package pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 78 false /* useCaches */); 79 assertNotNull(pkg); 80 81 pp.setCacheDir(mTmpDir); 82 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 83 false /* useCaches */); 84 assertNotNull(pkg); 85 86 // Make sure that we always write out a cache entry for future reference, 87 // whether or not we're asked to use caches. 88 assertEquals(1, mTmpDir.list().length); 89 } 90 91 @Test testParse_withCache()92 public void testParse_withCache() throws Exception { 93 PackageParser pp = new CachePackageNameParser(); 94 95 pp.setCacheDir(mTmpDir); 96 // The first parse will write this package to the cache. 97 pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, true /* useCaches */); 98 99 // Now attempt to parse the package again, should return the 100 // cached result. 101 PackageParser.Package pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 102 true /* useCaches */); 103 assertEquals("cache_android", pkg.packageName); 104 105 // Try again, with useCaches == false, shouldn't return the parsed 106 // result. 107 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, false /* useCaches */); 108 assertEquals("android", pkg.packageName); 109 110 // We haven't set a cache directory here : the parse should still succeed, 111 // just not using the cached results. 112 pp = new CachePackageNameParser(); 113 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, true /* useCaches */); 114 assertEquals("android", pkg.packageName); 115 116 pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, false /* useCaches */); 117 assertEquals("android", pkg.packageName); 118 } 119 120 @Test test_serializePackage()121 public void test_serializePackage() throws Exception { 122 PackageParser pp = new PackageParser(); 123 pp.setCacheDir(mTmpDir); 124 125 PackageParser.Package pkg = pp.parsePackage(FRAMEWORK, 0 /* parseFlags */, 126 true /* useCaches */); 127 128 Parcel p = Parcel.obtain(); 129 pkg.writeToParcel(p, 0 /* flags */); 130 131 p.setDataPosition(0); 132 PackageParser.Package deserialized = new PackageParser.Package(p); 133 134 assertPackagesEqual(pkg, deserialized); 135 } 136 137 @Test 138 @SmallTest 139 @Presubmit test_roundTripKnownFields()140 public void test_roundTripKnownFields() throws Exception { 141 PackageParser.Package pkg = new PackageParser.Package("foo"); 142 setKnownFields(pkg); 143 144 Parcel p = Parcel.obtain(); 145 pkg.writeToParcel(p, 0 /* flags */); 146 147 p.setDataPosition(0); 148 PackageParser.Package deserialized = new PackageParser.Package(p); 149 assertAllFieldsExist(deserialized); 150 } 151 152 @Test test_stringInterning()153 public void test_stringInterning() throws Exception { 154 PackageParser.Package pkg = new PackageParser.Package("foo"); 155 setKnownFields(pkg); 156 157 Parcel p = Parcel.obtain(); 158 pkg.writeToParcel(p, 0 /* flags */); 159 160 p.setDataPosition(0); 161 PackageParser.Package deserialized = new PackageParser.Package(p); 162 163 p.setDataPosition(0); 164 PackageParser.Package deserialized2 = new PackageParser.Package(p); 165 166 assertSame(deserialized.packageName, deserialized2.packageName); 167 assertSame(deserialized.applicationInfo.permission, 168 deserialized2.applicationInfo.permission); 169 assertSame(deserialized.requestedPermissions.get(0), 170 deserialized2.requestedPermissions.get(0)); 171 assertSame(deserialized.protectedBroadcasts.get(0), 172 deserialized2.protectedBroadcasts.get(0)); 173 assertSame(deserialized.usesLibraries.get(0), 174 deserialized2.usesLibraries.get(0)); 175 assertSame(deserialized.usesOptionalLibraries.get(0), 176 deserialized2.usesOptionalLibraries.get(0)); 177 assertSame(deserialized.mVersionName, deserialized2.mVersionName); 178 assertSame(deserialized.mSharedUserId, deserialized2.mSharedUserId); 179 } 180 181 182 /** 183 * A trivial subclass of package parser that only caches the package name, and throws away 184 * all other information. 185 */ 186 public static class CachePackageNameParser extends PackageParser { 187 @Override toCacheEntry(Package pkg)188 public byte[] toCacheEntry(Package pkg) { 189 return ("cache_" + pkg.packageName).getBytes(StandardCharsets.UTF_8); 190 } 191 192 @Override fromCacheEntry(byte[] cacheEntry)193 public Package fromCacheEntry(byte[] cacheEntry) { 194 return new Package(new String(cacheEntry, StandardCharsets.UTF_8)); 195 } 196 } 197 198 // NOTE: The equality assertions below are based on code autogenerated by IntelliJ. 199 assertPackagesEqual(PackageParser.Package a, PackageParser.Package b)200 public static void assertPackagesEqual(PackageParser.Package a, PackageParser.Package b) { 201 assertEquals(a.baseRevisionCode, b.baseRevisionCode); 202 assertEquals(a.baseHardwareAccelerated, b.baseHardwareAccelerated); 203 assertEquals(a.mVersionCode, b.mVersionCode); 204 assertEquals(a.mSharedUserLabel, b.mSharedUserLabel); 205 assertEquals(a.mPreferredOrder, b.mPreferredOrder); 206 assertEquals(a.installLocation, b.installLocation); 207 assertEquals(a.coreApp, b.coreApp); 208 assertEquals(a.mRequiredForAllUsers, b.mRequiredForAllUsers); 209 assertEquals(a.mCompileSdkVersion, b.mCompileSdkVersion); 210 assertEquals(a.mCompileSdkVersionCodename, b.mCompileSdkVersionCodename); 211 assertEquals(a.use32bitAbi, b.use32bitAbi); 212 assertEquals(a.packageName, b.packageName); 213 assertTrue(Arrays.equals(a.splitNames, b.splitNames)); 214 assertEquals(a.volumeUuid, b.volumeUuid); 215 assertEquals(a.codePath, b.codePath); 216 assertEquals(a.baseCodePath, b.baseCodePath); 217 assertTrue(Arrays.equals(a.splitCodePaths, b.splitCodePaths)); 218 assertTrue(Arrays.equals(a.splitRevisionCodes, b.splitRevisionCodes)); 219 assertTrue(Arrays.equals(a.splitFlags, b.splitFlags)); 220 assertTrue(Arrays.equals(a.splitPrivateFlags, b.splitPrivateFlags)); 221 assertApplicationInfoEqual(a.applicationInfo, b.applicationInfo); 222 223 assertEquals(a.permissions.size(), b.permissions.size()); 224 for (int i = 0; i < a.permissions.size(); ++i) { 225 assertPermissionsEqual(a.permissions.get(i), b.permissions.get(i)); 226 assertSame(a.permissions.get(i).owner, a); 227 assertSame(b.permissions.get(i).owner, b); 228 } 229 230 assertEquals(a.permissionGroups.size(), b.permissionGroups.size()); 231 for (int i = 0; i < a.permissionGroups.size(); ++i) { 232 assertPermissionGroupsEqual(a.permissionGroups.get(i), b.permissionGroups.get(i)); 233 } 234 235 assertEquals(a.activities.size(), b.activities.size()); 236 for (int i = 0; i < a.activities.size(); ++i) { 237 assertActivitiesEqual(a.activities.get(i), b.activities.get(i)); 238 } 239 240 assertEquals(a.receivers.size(), b.receivers.size()); 241 for (int i = 0; i < a.receivers.size(); ++i) { 242 assertActivitiesEqual(a.receivers.get(i), b.receivers.get(i)); 243 } 244 245 assertEquals(a.providers.size(), b.providers.size()); 246 for (int i = 0; i < a.providers.size(); ++i) { 247 assertProvidersEqual(a.providers.get(i), b.providers.get(i)); 248 } 249 250 assertEquals(a.services.size(), b.services.size()); 251 for (int i = 0; i < a.services.size(); ++i) { 252 assertServicesEqual(a.services.get(i), b.services.get(i)); 253 } 254 255 assertEquals(a.instrumentation.size(), b.instrumentation.size()); 256 for (int i = 0; i < a.instrumentation.size(); ++i) { 257 assertInstrumentationEqual(a.instrumentation.get(i), b.instrumentation.get(i)); 258 } 259 260 assertEquals(a.requestedPermissions, b.requestedPermissions); 261 assertEquals(a.protectedBroadcasts, b.protectedBroadcasts); 262 assertEquals(a.parentPackage, b.parentPackage); 263 assertEquals(a.childPackages, b.childPackages); 264 assertEquals(a.libraryNames, b.libraryNames); 265 assertEquals(a.usesLibraries, b.usesLibraries); 266 assertEquals(a.usesOptionalLibraries, b.usesOptionalLibraries); 267 assertTrue(Arrays.equals(a.usesLibraryFiles, b.usesLibraryFiles)); 268 assertEquals(a.mOriginalPackages, b.mOriginalPackages); 269 assertEquals(a.mRealPackage, b.mRealPackage); 270 assertEquals(a.mAdoptPermissions, b.mAdoptPermissions); 271 assertBundleApproximateEquals(a.mAppMetaData, b.mAppMetaData); 272 assertEquals(a.mVersionName, b.mVersionName); 273 assertEquals(a.mSharedUserId, b.mSharedUserId); 274 assertTrue(Arrays.equals(a.mSigningDetails.signatures, b.mSigningDetails.signatures)); 275 assertTrue(Arrays.equals(a.mLastPackageUsageTimeInMills, b.mLastPackageUsageTimeInMills)); 276 assertEquals(a.mExtras, b.mExtras); 277 assertEquals(a.mRestrictedAccountType, b.mRestrictedAccountType); 278 assertEquals(a.mRequiredAccountType, b.mRequiredAccountType); 279 assertEquals(a.mOverlayTarget, b.mOverlayTarget); 280 assertEquals(a.mOverlayTargetName, b.mOverlayTargetName); 281 assertEquals(a.mOverlayCategory, b.mOverlayCategory); 282 assertEquals(a.mOverlayPriority, b.mOverlayPriority); 283 assertEquals(a.mOverlayIsStatic, b.mOverlayIsStatic); 284 assertEquals(a.mSigningDetails.publicKeys, b.mSigningDetails.publicKeys); 285 assertEquals(a.mUpgradeKeySets, b.mUpgradeKeySets); 286 assertEquals(a.mKeySetMapping, b.mKeySetMapping); 287 assertEquals(a.cpuAbiOverride, b.cpuAbiOverride); 288 assertTrue(Arrays.equals(a.restrictUpdateHash, b.restrictUpdateHash)); 289 } 290 assertBundleApproximateEquals(Bundle a, Bundle b)291 private static void assertBundleApproximateEquals(Bundle a, Bundle b) { 292 if (a == b) { 293 return; 294 } 295 296 // Force the bundles to be unparceled. 297 a.getBoolean("foo"); 298 b.getBoolean("foo"); 299 300 assertEquals(a.toString(), b.toString()); 301 } 302 assertComponentsEqual(PackageParser.Component<?> a, PackageParser.Component<?> b)303 private static void assertComponentsEqual(PackageParser.Component<?> a, 304 PackageParser.Component<?> b) { 305 assertEquals(a.className, b.className); 306 assertBundleApproximateEquals(a.metaData, b.metaData); 307 assertEquals(a.getComponentName(), b.getComponentName()); 308 309 if (a.intents != null && b.intents != null) { 310 assertEquals(a.intents.size(), b.intents.size()); 311 } else if (a.intents == null || b.intents == null) { 312 return; 313 } 314 315 for (int i = 0; i < a.intents.size(); ++i) { 316 PackageParser.IntentInfo aIntent = a.intents.get(i); 317 PackageParser.IntentInfo bIntent = b.intents.get(i); 318 319 assertEquals(aIntent.hasDefault, bIntent.hasDefault); 320 assertEquals(aIntent.labelRes, bIntent.labelRes); 321 assertEquals(aIntent.nonLocalizedLabel, bIntent.nonLocalizedLabel); 322 assertEquals(aIntent.icon, bIntent.icon); 323 assertEquals(aIntent.logo, bIntent.logo); 324 assertEquals(aIntent.banner, bIntent.banner); 325 assertEquals(aIntent.preferred, bIntent.preferred); 326 } 327 } 328 assertPermissionsEqual(PackageParser.Permission a, PackageParser.Permission b)329 private static void assertPermissionsEqual(PackageParser.Permission a, 330 PackageParser.Permission b) { 331 assertComponentsEqual(a, b); 332 assertEquals(a.tree, b.tree); 333 334 // Verify basic flags in PermissionInfo to make sure they're consistent. We don't perform 335 // a full structural equality here because the code that serializes them isn't parser 336 // specific and is tested elsewhere. 337 assertEquals(a.info.protectionLevel, b.info.protectionLevel); 338 assertEquals(a.info.group, b.info.group); 339 assertEquals(a.info.flags, b.info.flags); 340 341 if (a.group != null && b.group != null) { 342 assertPermissionGroupsEqual(a.group, b.group); 343 } else if (a.group != null || b.group != null) { 344 throw new AssertionError(); 345 } 346 } 347 assertInstrumentationEqual(PackageParser.Instrumentation a, PackageParser.Instrumentation b)348 private static void assertInstrumentationEqual(PackageParser.Instrumentation a, 349 PackageParser.Instrumentation b) { 350 assertComponentsEqual(a, b); 351 352 // Sanity check for InstrumentationInfo. 353 assertEquals(a.info.targetPackage, b.info.targetPackage); 354 assertEquals(a.info.targetProcesses, b.info.targetProcesses); 355 assertEquals(a.info.sourceDir, b.info.sourceDir); 356 assertEquals(a.info.publicSourceDir, b.info.publicSourceDir); 357 } 358 assertServicesEqual(PackageParser.Service a, PackageParser.Service b)359 private static void assertServicesEqual(PackageParser.Service a, PackageParser.Service b) { 360 assertComponentsEqual(a, b); 361 362 // Sanity check for ServiceInfo. 363 assertApplicationInfoEqual(a.info.applicationInfo, b.info.applicationInfo); 364 assertEquals(a.info.name, b.info.name); 365 } 366 assertProvidersEqual(PackageParser.Provider a, PackageParser.Provider b)367 private static void assertProvidersEqual(PackageParser.Provider a, PackageParser.Provider b) { 368 assertComponentsEqual(a, b); 369 370 // Sanity check for ProviderInfo 371 assertApplicationInfoEqual(a.info.applicationInfo, b.info.applicationInfo); 372 assertEquals(a.info.name, b.info.name); 373 } 374 assertActivitiesEqual(PackageParser.Activity a, PackageParser.Activity b)375 private static void assertActivitiesEqual(PackageParser.Activity a, PackageParser.Activity b) { 376 assertComponentsEqual(a, b); 377 378 // Sanity check for ActivityInfo. 379 assertApplicationInfoEqual(a.info.applicationInfo, b.info.applicationInfo); 380 assertEquals(a.info.name, b.info.name); 381 } 382 assertPermissionGroupsEqual(PackageParser.PermissionGroup a, PackageParser.PermissionGroup b)383 private static void assertPermissionGroupsEqual(PackageParser.PermissionGroup a, 384 PackageParser.PermissionGroup b) { 385 assertComponentsEqual(a, b); 386 387 // Sanity check for PermissionGroupInfo. 388 assertEquals(a.info.name, b.info.name); 389 assertEquals(a.info.descriptionRes, b.info.descriptionRes); 390 } 391 assertApplicationInfoEqual(ApplicationInfo a, ApplicationInfo that)392 private static void assertApplicationInfoEqual(ApplicationInfo a, ApplicationInfo that) { 393 assertEquals(a.descriptionRes, that.descriptionRes); 394 assertEquals(a.theme, that.theme); 395 assertEquals(a.fullBackupContent, that.fullBackupContent); 396 assertEquals(a.uiOptions, that.uiOptions); 397 assertEquals(a.flags, that.flags); 398 assertEquals(a.privateFlags, that.privateFlags); 399 assertEquals(a.requiresSmallestWidthDp, that.requiresSmallestWidthDp); 400 assertEquals(a.compatibleWidthLimitDp, that.compatibleWidthLimitDp); 401 assertEquals(a.largestWidthLimitDp, that.largestWidthLimitDp); 402 assertEquals(a.nativeLibraryRootRequiresIsa, that.nativeLibraryRootRequiresIsa); 403 assertEquals(a.uid, that.uid); 404 assertEquals(a.minSdkVersion, that.minSdkVersion); 405 assertEquals(a.targetSdkVersion, that.targetSdkVersion); 406 assertEquals(a.versionCode, that.versionCode); 407 assertEquals(a.enabled, that.enabled); 408 assertEquals(a.enabledSetting, that.enabledSetting); 409 assertEquals(a.installLocation, that.installLocation); 410 assertEquals(a.networkSecurityConfigRes, that.networkSecurityConfigRes); 411 assertEquals(a.taskAffinity, that.taskAffinity); 412 assertEquals(a.permission, that.permission); 413 assertEquals(a.processName, that.processName); 414 assertEquals(a.className, that.className); 415 assertEquals(a.manageSpaceActivityName, that.manageSpaceActivityName); 416 assertEquals(a.backupAgentName, that.backupAgentName); 417 assertEquals(a.volumeUuid, that.volumeUuid); 418 assertEquals(a.scanSourceDir, that.scanSourceDir); 419 assertEquals(a.scanPublicSourceDir, that.scanPublicSourceDir); 420 assertEquals(a.sourceDir, that.sourceDir); 421 assertEquals(a.publicSourceDir, that.publicSourceDir); 422 assertTrue(Arrays.equals(a.splitSourceDirs, that.splitSourceDirs)); 423 assertTrue(Arrays.equals(a.splitPublicSourceDirs, that.splitPublicSourceDirs)); 424 assertTrue(Arrays.equals(a.resourceDirs, that.resourceDirs)); 425 assertEquals(a.seInfo, that.seInfo); 426 assertTrue(Arrays.equals(a.sharedLibraryFiles, that.sharedLibraryFiles)); 427 assertEquals(a.dataDir, that.dataDir); 428 assertEquals(a.deviceProtectedDataDir, that.deviceProtectedDataDir); 429 assertEquals(a.credentialProtectedDataDir, that.credentialProtectedDataDir); 430 assertEquals(a.nativeLibraryDir, that.nativeLibraryDir); 431 assertEquals(a.secondaryNativeLibraryDir, that.secondaryNativeLibraryDir); 432 assertEquals(a.nativeLibraryRootDir, that.nativeLibraryRootDir); 433 assertEquals(a.primaryCpuAbi, that.primaryCpuAbi); 434 assertEquals(a.secondaryCpuAbi, that.secondaryCpuAbi); 435 } 436 setKnownFields(PackageParser.Package pkg)437 public static void setKnownFields(PackageParser.Package pkg) { 438 pkg.baseRevisionCode = 100; 439 pkg.baseHardwareAccelerated = true; 440 pkg.mVersionCode = 100; 441 pkg.mSharedUserLabel = 100; 442 pkg.mPreferredOrder = 100; 443 pkg.installLocation = 100; 444 pkg.coreApp = true; 445 pkg.mRequiredForAllUsers = true; 446 pkg.use32bitAbi = true; 447 pkg.packageName = "foo"; 448 pkg.splitNames = new String[] { "foo2" }; 449 pkg.volumeUuid = "foo3"; 450 pkg.codePath = "foo4"; 451 pkg.baseCodePath = "foo5"; 452 pkg.splitCodePaths = new String[] { "foo6" }; 453 pkg.splitRevisionCodes = new int[] { 100 }; 454 pkg.splitFlags = new int[] { 100 }; 455 pkg.splitPrivateFlags = new int[] { 100 }; 456 pkg.applicationInfo = new ApplicationInfo(); 457 458 pkg.permissions.add(new PackageParser.Permission(pkg, (String) null)); 459 pkg.permissionGroups.add(new PackageParser.PermissionGroup(pkg, ID_NULL, ID_NULL, ID_NULL)); 460 461 final PackageParser.ParseComponentArgs dummy = new PackageParser.ParseComponentArgs( 462 pkg, new String[1], 0, 0, 0, 0, 0, 0, null, 0, 0, 0); 463 464 pkg.activities.add(new PackageParser.Activity(dummy, new ActivityInfo())); 465 pkg.receivers.add(new PackageParser.Activity(dummy, new ActivityInfo())); 466 pkg.providers.add(new PackageParser.Provider(dummy, new ProviderInfo())); 467 pkg.services.add(new PackageParser.Service(dummy, new ServiceInfo())); 468 pkg.instrumentation.add(new PackageParser.Instrumentation(dummy, new InstrumentationInfo())); 469 pkg.requestedPermissions.add("foo7"); 470 pkg.implicitPermissions.add("foo25"); 471 472 pkg.protectedBroadcasts = new ArrayList<>(); 473 pkg.protectedBroadcasts.add("foo8"); 474 475 pkg.parentPackage = new PackageParser.Package("foo9"); 476 477 pkg.childPackages = new ArrayList<>(); 478 pkg.childPackages.add(new PackageParser.Package("bar")); 479 480 pkg.staticSharedLibName = "foo23"; 481 pkg.staticSharedLibVersion = 100; 482 pkg.usesStaticLibraries = new ArrayList<>(); 483 pkg.usesStaticLibraries.add("foo23"); 484 pkg.usesStaticLibrariesCertDigests = new String[1][]; 485 pkg.usesStaticLibrariesCertDigests[0] = new String[] { "digest" }; 486 pkg.usesStaticLibrariesVersions = new long[] { 100 }; 487 488 pkg.libraryNames = new ArrayList<>(); 489 pkg.libraryNames.add("foo10"); 490 491 pkg.usesLibraries = new ArrayList<>(); 492 pkg.usesLibraries.add("foo11"); 493 494 pkg.usesOptionalLibraries = new ArrayList<>(); 495 pkg.usesOptionalLibraries.add("foo12"); 496 497 pkg.usesLibraryFiles = new String[] { "foo13"}; 498 499 pkg.usesLibraryInfos = new ArrayList<>(); 500 pkg.usesLibraryInfos.add( 501 new SharedLibraryInfo(null, null, null, null, 0L, 0, null, null, null)); 502 503 pkg.mOriginalPackages = new ArrayList<>(); 504 pkg.mOriginalPackages.add("foo14"); 505 506 pkg.mRealPackage = "foo15"; 507 508 pkg.mAdoptPermissions = new ArrayList<>(); 509 pkg.mAdoptPermissions.add("foo16"); 510 511 pkg.mAppMetaData = new Bundle(); 512 pkg.mVersionName = "foo17"; 513 pkg.mSharedUserId = "foo18"; 514 pkg.mSigningDetails = 515 new PackageParser.SigningDetails( 516 new Signature[] { new Signature(new byte[16]) }, 517 2, 518 new ArraySet<>(), 519 null); 520 pkg.mExtras = new Bundle(); 521 pkg.mRestrictedAccountType = "foo19"; 522 pkg.mRequiredAccountType = "foo20"; 523 pkg.mOverlayTarget = "foo21"; 524 pkg.mOverlayPriority = 100; 525 pkg.mUpgradeKeySets = new ArraySet<>(); 526 pkg.mKeySetMapping = new ArrayMap<>(); 527 pkg.cpuAbiOverride = "foo22"; 528 pkg.restrictUpdateHash = new byte[16]; 529 530 pkg.preferredActivityFilters = new ArrayList<>(); 531 pkg.preferredActivityFilters.add(new PackageParser.ActivityIntentInfo( 532 new PackageParser.Activity(dummy, new ActivityInfo()))); 533 534 pkg.configPreferences = new ArrayList<>(); 535 pkg.configPreferences.add(new ConfigurationInfo()); 536 537 pkg.reqFeatures = new ArrayList<>(); 538 pkg.reqFeatures.add(new FeatureInfo()); 539 540 pkg.featureGroups = new ArrayList<>(); 541 pkg.featureGroups.add(new FeatureGroupInfo()); 542 543 pkg.mCompileSdkVersionCodename = "foo23"; 544 pkg.mCompileSdkVersion = 100; 545 pkg.mVersionCodeMajor = 100; 546 547 pkg.mOverlayCategory = "foo24"; 548 pkg.mOverlayIsStatic = true; 549 pkg.mOverlayTargetName = "foo26"; 550 551 pkg.baseHardwareAccelerated = true; 552 pkg.coreApp = true; 553 pkg.mRequiredForAllUsers = true; 554 pkg.visibleToInstantApps = true; 555 pkg.use32bitAbi = true; 556 } 557 assertAllFieldsExist(PackageParser.Package pkg)558 private static void assertAllFieldsExist(PackageParser.Package pkg) throws Exception { 559 Field[] fields = PackageParser.Package.class.getDeclaredFields(); 560 561 Set<String> nonSerializedFields = new HashSet<>(); 562 nonSerializedFields.add("mExtras"); 563 nonSerializedFields.add("packageUsageTimeMillis"); 564 nonSerializedFields.add("isStub"); 565 566 for (Field f : fields) { 567 final Class<?> fieldType = f.getType(); 568 569 if (nonSerializedFields.contains(f.getName())) { 570 continue; 571 } 572 573 if (List.class.isAssignableFrom(fieldType)) { 574 // Sanity check for list fields: Assume they're non-null and contain precisely 575 // one element. 576 List<?> list = (List<?>) f.get(pkg); 577 assertNotNull("List was null: " + f, list); 578 assertEquals(1, list.size()); 579 } else if (fieldType.getComponentType() != null) { 580 // Sanity check for array fields: Assume they're non-null and contain precisely 581 // one element. 582 Object array = f.get(pkg); 583 assertNotNull(Array.get(array, 0)); 584 } else if (fieldType == String.class) { 585 // String fields: Check that they're set to "foo". 586 String value = (String) f.get(pkg); 587 588 assertTrue("Bad value for field: " + f, value != null && value.startsWith("foo")); 589 } else if (fieldType == int.class) { 590 // int fields: Check that they're set to 100. 591 int value = (int) f.get(pkg); 592 assertEquals("Bad value for field: " + f, 100, value); 593 } else if (fieldType == boolean.class) { 594 // boolean fields: Check that they're set to true. 595 boolean value = (boolean) f.get(pkg); 596 assertEquals("Bad value for field: " + f, true, value); 597 } else { 598 // All other fields: Check that they're set. 599 Object o = f.get(pkg); 600 assertNotNull("Field was null: " + f, o); 601 } 602 } 603 } 604 } 605 606