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