From 10b27eb2ccbb8992895827c0fb21e2d3f9c727db Mon Sep 17 00:00:00 2001 From: Calin Juravle Date: Mon, 22 Jan 2018 12:50:01 -0800 Subject: [PATCH] Perform a non strict matching of .dm files when computing the size 'adb shell pm install' creates the PackageLite structure without validating or renaming the input files to '.apk'. Be more permissive in DexMetadataHeler when computing the size of the package to allow for this scenario. Test: atest core/tests/coretests/src/android/content/pm/dex/DexMetadataHelperTest.java Bug: 72267410 Change-Id: Ica446b0822be71826d02d01ada015a43d8133c68 --- .../content/pm/dex/DexMetadataHelper.java | 6 ++-- .../content/pm/dex/DexMetadataHelperTest.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/core/java/android/content/pm/dex/DexMetadataHelper.java b/core/java/android/content/pm/dex/DexMetadataHelper.java index c5f1c852c7b22..5d10b8826b007 100644 --- a/core/java/android/content/pm/dex/DexMetadataHelper.java +++ b/core/java/android/content/pm/dex/DexMetadataHelper.java @@ -107,8 +107,8 @@ public class DexMetadataHelper { * For each code path (.apk) the method checks if a matching dex metadata file (.dm) exists. * If it does it adds the pair to the returned map. * - * Note that this method will do a strict - * matching based on the extension ('foo.dm' will only match 'foo.apk'). + * Note that this method will do a loose + * matching based on the extension ('foo.dm' will match 'foo.apk' or 'foo'). * * This should only be used for code paths extracted from a package structure after the naming * was enforced in the installer. @@ -118,7 +118,7 @@ public class DexMetadataHelper { ArrayMap result = new ArrayMap<>(); for (int i = codePaths.size() - 1; i >= 0; i--) { String codePath = codePaths.get(i); - String dexMetadataPath = buildDexMetadataPathForApk(codePath); + String dexMetadataPath = buildDexMetadataPathForFile(new File(codePath)); if (Files.exists(Paths.get(dexMetadataPath))) { result.put(codePath, dexMetadataPath); diff --git a/core/tests/coretests/src/android/content/pm/dex/DexMetadataHelperTest.java b/core/tests/coretests/src/android/content/pm/dex/DexMetadataHelperTest.java index 4b84429589001..584257b1f6a98 100644 --- a/core/tests/coretests/src/android/content/pm/dex/DexMetadataHelperTest.java +++ b/core/tests/coretests/src/android/content/pm/dex/DexMetadataHelperTest.java @@ -24,7 +24,9 @@ import static org.junit.Assert.fail; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageParser; +import android.content.pm.PackageParser.ApkLite; import android.content.pm.PackageParser.Package; +import android.content.pm.PackageParser.PackageLite; import android.content.pm.PackageParser.PackageParserException; import android.os.FileUtils; import android.support.test.InstrumentationRegistry; @@ -34,6 +36,7 @@ import android.test.suitebuilder.annotation.SmallTest; import com.android.frameworks.coretests.R; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -45,6 +48,7 @@ import java.util.zip.ZipOutputStream; import libcore.io.IoUtils; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -201,6 +205,31 @@ public class DexMetadataHelperTest { } } + @Test + public void testPackageSizeWithDmFile() + throws IOException, PackageParserException { + copyApkToToTmpDir("install_split_base.apk", R.raw.install_split_base); + File dm = createDexMetadataFile("install_split_base.apk"); + PackageParser.PackageLite pkg = new PackageParser().parsePackageLite(mTmpDir, + 0 /* flags */); + + Assert.assertEquals(dm.length(), DexMetadataHelper.getPackageDexMetadataSize(pkg)); + } + + // This simulates the 'adb shell pm install' flow. + @Test + public void testPackageSizeWithPartialPackageLite() throws IOException, PackageParserException { + File base = copyApkToToTmpDir("install_split_base", R.raw.install_split_base); + File dm = createDexMetadataFile("install_split_base.apk"); + try (FileInputStream is = new FileInputStream(base)) { + ApkLite baseApk = PackageParser.parseApkLite(is.getFD(), base.getAbsolutePath(), 0); + PackageLite pkgLite = new PackageLite(null, baseApk, null, null, null, null, + null, null); + Assert.assertEquals(dm.length(), DexMetadataHelper.getPackageDexMetadataSize(pkgLite)); + } + + } + private static boolean isDexMetadataForApk(String dmaPath, String apkPath) { return apkPath.substring(0, apkPath.length() - APK_FILE_EXTENSION.length()).equals( dmaPath.substring(0, dmaPath.length() - DEX_METADATA_FILE_EXTENSION.length()));