diff options
author | Paul Duffin <paulduffin@google.com> | 2014-02-26 14:31:44 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-02-26 14:31:44 +0000 |
commit | f5fe8b735bc185d1221c9e0ec07de87aea2723ff (patch) | |
tree | 5a5967fe23611bd294addfc7cbfddbe9ff90be9e /luni | |
parent | 5357ce35bfb271d557324f26b0ccbff59c053616 (diff) | |
parent | ef164bf196538c04f499dcbb49a389c70ff5601a (diff) | |
download | libcore-f5fe8b735bc185d1221c9e0ec07de87aea2723ff.zip libcore-f5fe8b735bc185d1221c9e0ec07de87aea2723ff.tar.gz libcore-f5fe8b735bc185d1221c9e0ec07de87aea2723ff.tar.bz2 |
Merge "Improve error message when attempting to open an empty zip"
Diffstat (limited to 'luni')
-rw-r--r-- | luni/src/main/java/java/util/zip/ZipFile.java | 3 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/util/zip/ZipFileTest.java | 29 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java | 19 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java | 24 | ||||
-rw-r--r-- | luni/src/test/resources/tests/resources/java/util/zip/EmptyArchive.zip | bin | 0 -> 22 bytes |
5 files changed, 68 insertions, 7 deletions
diff --git a/luni/src/main/java/java/util/zip/ZipFile.java b/luni/src/main/java/java/util/zip/ZipFile.java index 40036cf..4380281 100644 --- a/luni/src/main/java/java/util/zip/ZipFile.java +++ b/luni/src/main/java/java/util/zip/ZipFile.java @@ -357,6 +357,9 @@ public class ZipFile implements Closeable, ZipConstants { raf.seek(0); final int headerMagic = Integer.reverseBytes(raf.readInt()); + if (headerMagic == ENDSIG) { + throw new ZipException("Empty zip archive not supported"); + } if (headerMagic != LOCSIG) { throw new ZipException("Not a zip archive"); } diff --git a/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java b/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java index 60af4d0..8afc223 100644 --- a/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java +++ b/luni/src/test/java/libcore/java/util/zip/ZipFileTest.java @@ -21,7 +21,6 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; @@ -34,6 +33,8 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import junit.framework.TestCase; +import tests.support.resource.Support_Resources; + public final class ZipFileTest extends TestCase { /** * Exercise Inflater's ability to refill the zlib's input buffer. As of this @@ -468,4 +469,30 @@ public final class ZipFileTest extends TestCase { out.closeEntry(); out.close(); } + + /** + * RI does not allow reading of an empty zip using a {@link ZipFile}. + */ + public void testConstructorFailsWhenReadingEmptyZipArchive() throws IOException { + + File resources = Support_Resources.createTempFolder(); + File emptyZip = Support_Resources.copyFile( + resources, "java/util/zip", "EmptyArchive.zip"); + + try { + // The following should fail with an exception but if it doesn't then we need to clean + // up the resource so we need a reference to it. + ZipFile zipFile = new ZipFile(emptyZip); + + // Clean up the resource. + try { + zipFile.close(); + } catch (Exception e) { + // Ignore + } + fail(); + } catch (ZipException expected) { + // expected + } + } } diff --git a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java index cb98322..3d6e600 100644 --- a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java +++ b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java @@ -20,15 +20,16 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Arrays; import java.util.Random; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; import junit.framework.TestCase; +import tests.support.resource.Support_Resources; + public final class ZipInputStreamTest extends TestCase { + public void testShortMessage() throws IOException { byte[] data = "Hello World".getBytes("UTF-8"); byte[] zipped = ZipOutputStreamTest.zip("short", data); @@ -59,4 +60,18 @@ public final class ZipInputStreamTest extends TestCase { in.close(); return out.toByteArray(); } + + /** + * Reference implementation allows reading of empty zip using a {@link ZipInputStream}. + */ + public void testReadEmpty() throws IOException { + InputStream emptyZipIn = Support_Resources.getStream("java/util/zip/EmptyArchive.zip"); + ZipInputStream in = new ZipInputStream(emptyZipIn); + try { + ZipEntry entry = in.getNextEntry(); + assertNull("An empty zip has no entries", entry); + } finally { + in.close(); + } + } } diff --git a/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java index e7c518f..dc80512 100644 --- a/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java +++ b/luni/src/test/java/libcore/java/util/zip/ZipOutputStreamTest.java @@ -16,15 +16,15 @@ package libcore.java.util.zip; -import java.io.ByteArrayInputStream; +import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.Arrays; import java.util.Random; import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; +import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; import junit.framework.TestCase; @@ -60,4 +60,20 @@ public final class ZipOutputStreamTest extends TestCase { zippedOut.close(); return bytesOut.toByteArray(); } + + /** + * Reference implementation does NOT allow writing of an empty zip using a + * {@link ZipOutputStream}. + */ + public void testCreateEmpty() throws IOException { + File result = File.createTempFile("ZipFileTest", "zip"); + ZipOutputStream out = + new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result))); + try { + out.close(); + fail("Close on empty stream failed to throw exception"); + } catch (ZipException e) { + // expected + } + } } diff --git a/luni/src/test/resources/tests/resources/java/util/zip/EmptyArchive.zip b/luni/src/test/resources/tests/resources/java/util/zip/EmptyArchive.zip Binary files differnew file mode 100644 index 0000000..15cb0ec --- /dev/null +++ b/luni/src/test/resources/tests/resources/java/util/zip/EmptyArchive.zip |