diff options
author | Izabela Orlowska <imorlowska@google.com> | 2017-05-23 12:57:44 +0100 |
---|---|---|
committer | Izabela Orlowska <imorlowska@google.com> | 2017-05-23 14:48:16 +0100 |
commit | f751e5860c73de6fae6f0f9688c6e3bc4481163b (patch) | |
tree | 8673f57ee19ad7282ad8baf75917a88dee7ba944 /src/test | |
parent | 761b093e9607d94e9405e1976d772a810abd0b94 (diff) | |
download | apkzlib-f751e5860c73de6fae6f0f9688c6e3bc4481163b.tar.gz |
Fix JarSigningTest
The missing AndroidManifest.xml in the tested zip files was causing
setting the "dirty" field to be set to true and the zip file being
needlessly overwritten.
Test: JarSigningTest
Change-Id: I494fbcc257332d0eb8eceadf2938c2d74dd5bce5
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/android/apkzlib/sign/JarSigningTest.java | 21 | ||||
-rw-r--r-- | src/test/java/com/android/apkzlib/utils/ApkZFileTestUtils.java | 18 | ||||
-rw-r--r-- | src/test/resources/testData/packaging/AndroidManifest.xml | bin | 0 -> 1436 bytes |
3 files changed, 31 insertions, 8 deletions
diff --git a/src/test/java/com/android/apkzlib/sign/JarSigningTest.java b/src/test/java/com/android/apkzlib/sign/JarSigningTest.java index 8e330fa..35aeeaf 100644 --- a/src/test/java/com/android/apkzlib/sign/JarSigningTest.java +++ b/src/test/java/com/android/apkzlib/sign/JarSigningTest.java @@ -20,10 +20,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; -import com.android.apkzlib.zip.StoredEntry; -import com.android.apkzlib.zip.ZFile; import com.android.apkzlib.utils.ApkZFileTestUtils; import com.android.apkzlib.utils.ApkZLibPair; +import com.android.apkzlib.zip.StoredEntry; +import com.android.apkzlib.zip.ZFile; import com.google.common.base.Charsets; import com.google.common.hash.Hashing; import java.io.ByteArrayInputStream; @@ -34,7 +34,6 @@ import java.security.cert.X509Certificate; import java.util.Base64; import java.util.jar.Attributes; import java.util.jar.Manifest; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -49,6 +48,7 @@ public class JarSigningTest { File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip"); try (ZFile zf = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf); ManifestGenerationExtension manifestExtension = new ManifestGenerationExtension("Me", "Me"); manifestExtension.register(zf); @@ -77,6 +77,7 @@ public class JarSigningTest { ApkZLibPair<PrivateKey, X509Certificate> p = SignatureTestUtils.generateSignaturePre18(); try (ZFile zf1 = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf1); zf1.add("directory/file", new ByteArrayInputStream("useless text".getBytes(Charsets.US_ASCII))); } @@ -131,6 +132,7 @@ public class JarSigningTest { public void signJarWithPrexistingSimpleTextFilePos18() throws Exception { File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip"); try (ZFile zf1 = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf1); zf1.add("directory/file", new ByteArrayInputStream("useless text".getBytes( Charsets.US_ASCII))); } @@ -189,6 +191,7 @@ public class JarSigningTest { public void v2SignAddsApkSigningBlock() throws Exception { File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip"); try (ZFile zf = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf); ManifestGenerationExtension manifestExtension = new ManifestGenerationExtension("Me", "Me"); manifestExtension.register(zf); @@ -221,6 +224,7 @@ public class JarSigningTest { String createdBy = "Uses Android"; try (ZFile zf1 = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf1); zf1.add(file1Name, new ByteArrayInputStream(file1Contents)); ManifestGenerationExtension me = new ManifestGenerationExtension(builtBy, createdBy); me.register(zf1); @@ -234,7 +238,7 @@ public class JarSigningTest { try (InputStream manifestIs = manifestEntry.open()) { Manifest manifest = new Manifest(manifestIs); - assertEquals(1, manifest.getEntries().size()); + assertEquals(2, manifest.getEntries().size()); Attributes file1Attrs = manifest.getEntries().get(file1Name); assertNotNull(file1Attrs); @@ -258,7 +262,7 @@ public class JarSigningTest { try (InputStream manifestIs = manifestEntry.open()) { Manifest manifest = new Manifest(manifestIs); - assertEquals(1, manifest.getEntries().size()); + assertEquals(2, manifest.getEntries().size()); Attributes file1Attrs = manifest.getEntries().get(file1Name); assertNotNull(file1Attrs); @@ -274,6 +278,7 @@ public class JarSigningTest { file1ShaTxt = Base64.getEncoder().encodeToString(file1Sha); try (ZFile zf2 = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf2); ManifestGenerationExtension me = new ManifestGenerationExtension(builtBy, createdBy); me.register(zf2); new SigningExtension(21, p.v2, p.v1, true, false).register(zf2); @@ -288,7 +293,7 @@ public class JarSigningTest { try (InputStream manifestIs = manifestEntry.open()) { Manifest manifest = new Manifest(manifestIs); - assertEquals(1, manifest.getEntries().size()); + assertEquals(2, manifest.getEntries().size()); Attributes file1Attrs = manifest.getEntries().get(file1Name); assertNotNull(file1Attrs); @@ -297,9 +302,8 @@ public class JarSigningTest { } } - @Ignore @Test - public void openSignedJarDoesNotForcesWriteifSignatureIsNotCorrect() throws Exception { + public void openSignedJarDoesNotForcesWriteIfSignatureIsNotCorrect() throws Exception { File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip"); ApkZLibPair<PrivateKey, X509Certificate> p = SignatureTestUtils.generateSignaturePos18(); @@ -308,6 +312,7 @@ public class JarSigningTest { byte[] fileContents = "Very interesting contents".getBytes(Charsets.US_ASCII); try (ZFile zf = new ZFile(zipFile)) { + ApkZFileTestUtils.addAndroidManifest(zf); ManifestGenerationExtension me = new ManifestGenerationExtension("I", "Android"); me.register(zf); new SigningExtension(21, p.v2, p.v1, true, false).register(zf); diff --git a/src/test/java/com/android/apkzlib/utils/ApkZFileTestUtils.java b/src/test/java/com/android/apkzlib/utils/ApkZFileTestUtils.java index 916ef46..1ef087f 100644 --- a/src/test/java/com/android/apkzlib/utils/ApkZFileTestUtils.java +++ b/src/test/java/com/android/apkzlib/utils/ApkZFileTestUtils.java @@ -18,10 +18,12 @@ package com.android.apkzlib.utils; import static org.junit.Assert.assertTrue; +import com.android.apkzlib.zip.ZFile; import com.android.testutils.TestResources; import com.google.common.base.Preconditions; import com.google.common.io.ByteSource; import com.google.common.io.Resources; +import java.io.ByteArrayInputStream; import java.io.EOFException; import java.io.File; import java.io.IOException; @@ -106,6 +108,22 @@ public final class ApkZFileTestUtils { } } + /* + * Adds a basic compiled AndroidManifest to the given ZFile containing minSdkVersion equal 15 + * and targetSdkVersion equal 25. + */ + public static void addAndroidManifest(ZFile zf) throws IOException { + zf.add("AndroidManifest.xml", new ByteArrayInputStream(getAndroidManifest())); + } + + /* + * Provides a basic compiled AndroidManifest containing minSdkVersion equal 15 and + * targetSdkVersion equal 25. + */ + public static byte[] getAndroidManifest() throws IOException { + return ApkZFileTestUtils.getResourceBytes("/testData/packaging/AndroidManifest.xml").read(); + } + /** * Obtains the timestamp of a newly-created file. * diff --git a/src/test/resources/testData/packaging/AndroidManifest.xml b/src/test/resources/testData/packaging/AndroidManifest.xml Binary files differnew file mode 100644 index 0000000..060ec31 --- /dev/null +++ b/src/test/resources/testData/packaging/AndroidManifest.xml |