From 4949c14402562af123745d59020cd4fc5eb565cb Mon Sep 17 00:00:00 2001 From: Urs Grob Date: Mon, 7 Sep 2009 16:34:47 +0200 Subject: [PATCH] JarFile was not able to verify signed files with size 0. This regressen was introduced by harmony optimizations done in HARMONY-4569. This fix allows for jar entries of size 0 to also get successfully validated. A regression test has been added to JarFileTest. --- .../src/main/java/java/util/jar/JarFile.java | 25 ++++++++-- .../archive/tests/java/util/jar/JarFileTest.java | 52 ++++++++++++++++++--- .../java/tests/resources/EmptyEntries_signed.jar | Bin 0 -> 1927 bytes 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 libcore/support/src/test/java/tests/resources/EmptyEntries_signed.jar diff --git a/libcore/archive/src/main/java/java/util/jar/JarFile.java b/libcore/archive/src/main/java/java/util/jar/JarFile.java index d6e8339ae..6f4eb836e 100644 --- a/libcore/archive/src/main/java/java/util/jar/JarFile.java +++ b/libcore/archive/src/main/java/java/util/jar/JarFile.java @@ -68,6 +68,10 @@ public class JarFile extends ZipFile { private JarVerifier.VerifierEntry entry; + // BEGIN android-added + private boolean done = false; + // END android-added + JarFileInputStream(InputStream is, ZipEntry ze, JarVerifier.VerifierEntry e) { super(is); @@ -78,6 +82,10 @@ public class JarFile extends ZipFile { @Override public int read() throws IOException { + // BEGIN android-changed + if (done) { + return -1; + } if (count > 0) { int r = super.read(); if (r != -1) { @@ -87,16 +95,24 @@ public class JarFile extends ZipFile { count = 0; } if (count == 0) { + done = true; entry.verify(); } return r; } else { + done = true; + entry.verify(); return -1; } + // END android-changed } @Override public int read(byte[] buf, int off, int nbytes) throws IOException { + // BEGIN android-changed + if (done) { + return -1; + } if (count > 0) { int r = super.read(buf, off, nbytes); if (r != -1) { @@ -110,22 +126,25 @@ public class JarFile extends ZipFile { count = 0; } if (count == 0) { + done = true; entry.verify(); } return r; } else { + done = true; + entry.verify(); return -1; } + // END android-changed } // BEGIN android-added @Override public int available() throws IOException { - if (count > 0) { - return super.available(); - } else { + if (done) { return 0; } + return super.available(); } // END android-added diff --git a/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java b/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java index 96321a42a..d2a51109f 100644 --- a/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java +++ b/libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java @@ -18,10 +18,15 @@ package org.apache.harmony.archive.tests.java.util.jar; import dalvik.annotation.AndroidOnly; -import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; +import junit.framework.TestCase; + +import tests.support.Support_PlatformFile; +import tests.support.resource.Support_Resources; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; @@ -41,10 +46,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; -import junit.framework.TestCase; -import tests.support.Support_PlatformFile; -import tests.support.resource.Support_Resources; - @TestTargetClass(JarFile.class) public class JarFileTest extends TestCase { @@ -74,14 +75,22 @@ public class JarFileTest extends TestCase { private final String jarName5 = "hyts_signed_inc.jar"; - private final String integrateJar = "Integrate.jar"; - private final String entryName = "foo/bar/A.class"; private final String entryName3 = "coucou/FileAccess.class"; + private final String integrateJar = "Integrate.jar"; + private final String integrateJarEntry = "Test.class"; + private final String emptyEntryJar = "EmptyEntries_signed.jar"; + + private final String emptyEntry1 = "subfolder/internalSubset01.js"; + + private final String emptyEntry2 = "svgtest.js"; + + private final String emptyEntry3 = "svgunit.js"; + private File resources; // custom security manager @@ -1061,4 +1070,33 @@ public class JarFileTest extends TestCase { // expected } } + + /** + * The jar is intact, but the entry object is modified. + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "Regression test for issue introduced by HAROMNY-4569. " + + "signed archives containing files with size 0 could not get verified", + method = "getInputStream", + args = {ZipEntry.class} + ) + public void testJarVerificationEmptyEntry() throws IOException { + Support_Resources.copyFile(resources, null, emptyEntryJar); + File f = new File(resources, emptyEntryJar); + + JarFile jarFile = new JarFile(f); + + ZipEntry zipEntry = jarFile.getJarEntry(emptyEntry1); + int res = jarFile.getInputStream(zipEntry).read(new byte[100], 0, 100); + assertEquals("Wrong length of empty jar entry", -1, res); + + zipEntry = jarFile.getJarEntry(emptyEntry2); + res = jarFile.getInputStream(zipEntry).read(new byte[100], 0, 100); + assertEquals("Wrong length of empty jar entry", -1, res); + + zipEntry = jarFile.getJarEntry(emptyEntry3); + res = jarFile.getInputStream(zipEntry).read(); + assertEquals("Wrong length of empty jar entry", -1, res); + } } diff --git a/libcore/support/src/test/java/tests/resources/EmptyEntries_signed.jar b/libcore/support/src/test/java/tests/resources/EmptyEntries_signed.jar new file mode 100644 index 0000000000000000000000000000000000000000..237d244d6987d00ece0ac757b8d569c727d7a07e GIT binary patch literal 1927 zcmWIWW@Zs#-~dAJPIYSrBp?E0`?`iW>U#RQ>H9kRdAhjw*&z3K5*DhY%S6(pPYpvD>y%Pr_OkZ>qoHDSI zoWOTVVWxeO|IP0FlU^0RL7_7ap1bCle==0Oq*u_b{HC+Zn~2>{U8TDx?l@v&vAXJE zN6xH$mL=B8YG&z`7G?cEZr(eTZnX9I`a9WQ>)(COdf{cT@Dg9ow7DO4vL65O{*1>mJ5=K-Y^iGB5-X=f4nF-+*{;*GRo!x7@z7c})%yuHV)69oD%m@nV6JuBUj% zMy`cN9oCjy<~hgich&K^`;q1AbInal%gvw9nZ03>@KRQZVu4AacU-4+-d>yXDDqgx zRE4R6g6F#KAKdX!Q#E3psE7F4O07q+->&)A^6#-+wopQ}XdB<@f62R_aw~AHFwEJv zlv%hw!nR~`MxW9)k*PULn%24m?=#6?FCM$4Yu=?E{s*$=p$uQ22<*9*+J0`<-%l|a z+vlAWi&5c!`m^82{%PxR*Y;mGwkyT3s>H~|%XIaht&j+iJM~8B`kdH06^>^i;?$>)(n28*beayw8KHKHFkjS=w(&3)xXFiFxU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