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