aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Bodewig <bodewig@apache.org>2018-08-09 20:29:39 +0200
committerStefan Bodewig <bodewig@apache.org>2018-08-09 20:29:39 +0200
commit64ed6dde03afbef6715fdfdeab5fc04be6192899 (patch)
tree3328e26b9cc91e93644fa0c74819a708d9b88850
parent0fe6ae319d50edd1389b6ecb5a6fd12f8091aea0 (diff)
downloadapache-commons-compress-64ed6dde03afbef6715fdfdeab5fc04be6192899.tar.gz
ensure archive streams really mean EOF if they say so
-rw-r--r--src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java4
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java143
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java42
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java26
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java26
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java25
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java24
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java81
-rw-r--r--src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java83
9 files changed, 442 insertions, 12 deletions
diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
index bdfc80287..4cedc272d 100644
--- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
@@ -332,7 +332,9 @@ public class CpioArchiveInputStream extends ArchiveInputStream implements
this.crc &= 0xFFFFFFFFL;
}
}
- this.entryBytesRead += tmpread;
+ if (tmpread > 0) {
+ this.entryBytesRead += tmpread;
+ }
return tmpread;
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java b/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
index c5499c95e..0b5bd95f9 100644
--- a/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
@@ -22,16 +22,34 @@ import static org.junit.Assert.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
+import javax.crypto.Cipher;
import org.apache.commons.compress.AbstractTestCase;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZMethod;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
+import org.junit.Assume;
+import org.junit.Before;
import org.junit.Test;
public class SevenZTestCase extends AbstractTestCase {
+ private File output;
+ private final File file1, file2;
+
+ public SevenZTestCase() throws IOException {
+ file1 = getFile("test1.xml");
+ file2 = getFile("test2.xml");
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ output = new File(dir, "bla.7z");
+ }
+
@Test
public void testSevenZArchiveCreationUsingCopy() throws Exception {
testSevenZArchiveCreation(SevenZMethod.COPY);
@@ -58,10 +76,23 @@ public class SevenZTestCase extends AbstractTestCase {
}
private void testSevenZArchiveCreation(final SevenZMethod method) throws Exception {
- final File output = new File(dir, "bla.7z");
- final File file1 = getFile("test1.xml");
- final File file2 = getFile("test2.xml");
+ createArchive(method);
+ try (SevenZFile archive = new SevenZFile(output)) {
+ SevenZArchiveEntry entry;
+
+ entry = archive.getNextEntry();
+ assert (entry != null);
+ assertEquals(entry.getName(), file1.getName());
+
+ entry = archive.getNextEntry();
+ assert (entry != null);
+ assertEquals(entry.getName(), file2.getName());
+
+ assert (archive.getNextEntry() == null);
+ }
+ }
+ private void createArchive(final SevenZMethod method) throws Exception {
final SevenZOutputFile outArchive = new SevenZOutputFile(output);
outArchive.setContentCompression(method);
try {
@@ -79,22 +110,100 @@ public class SevenZTestCase extends AbstractTestCase {
} finally {
outArchive.close();
}
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingCopy() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.COPY);
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingLZMA() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.LZMA);
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingLZMA2() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.LZMA2);
+ }
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingBZIP2() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.BZIP2);
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.DEFLATE);
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingAES() throws Exception {
+ assumeStrongCryptoIsAvailable();
+ try (SevenZFile archive = new SevenZFile(getFile("bla.encrypted.7z"), "foo".toCharArray())) {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(archive);
+ }
+ }
+
+ private void singleByteReadConsistentlyReturnsMinusOneAtEof(final SevenZMethod method) throws Exception {
+ createArchive(method);
try (SevenZFile archive = new SevenZFile(output)) {
- SevenZArchiveEntry entry;
+ singleByteReadConsistentlyReturnsMinusOneAtEof(archive);
+ }
+ }
- entry = archive.getNextEntry();
- assert (entry != null);
- assertEquals(entry.getName(), file1.getName());
+ private void singleByteReadConsistentlyReturnsMinusOneAtEof(SevenZFile archive) throws Exception {
+ SevenZArchiveEntry entry = archive.getNextEntry();
+ entry = archive.getNextEntry();
+ readFully(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
- entry = archive.getNextEntry();
- assert (entry != null);
- assertEquals(entry.getName(), file2.getName());
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingLZMA() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.LZMA);
+ }
- assert (archive.getNextEntry() == null);
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingLZMA2() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.LZMA2);
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingBZIP2() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.BZIP2);
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(SevenZMethod.DEFLATE);
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingAES() throws Exception {
+ assumeStrongCryptoIsAvailable();
+ try (SevenZFile archive = new SevenZFile(getFile("bla.encrypted.7z"), "foo".toCharArray())) {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(archive);
+ }
+ }
+
+ private void multiByteReadConsistentlyReturnsMinusOneAtEof(final SevenZMethod method) throws Exception {
+ createArchive(method);
+ try (SevenZFile archive = new SevenZFile(output)) {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(archive);
}
}
+ private void multiByteReadConsistentlyReturnsMinusOneAtEof(SevenZFile archive) throws Exception {
+ final byte[] buf = new byte[2];
+ SevenZArchiveEntry entry = archive.getNextEntry();
+ entry = archive.getNextEntry();
+ readFully(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+
private void copy(final File src, final SevenZOutputFile dst) throws IOException {
FileInputStream fis = null;
try {
@@ -110,4 +219,16 @@ public class SevenZTestCase extends AbstractTestCase {
}
}
}
+
+ private void readFully(final SevenZFile archive) throws IOException {
+ final byte[] buf = new byte[1024];
+ int x = 0;
+ while (0 <= (x = archive.read(buf))) {
+ ;
+ }
+ }
+
+ private static void assumeStrongCryptoIsAvailable() throws NoSuchAlgorithmException {
+ Assume.assumeTrue("test requires strong crypto", Cipher.getMaxAllowedKeyLength("AES/ECB/PKCS5Padding") >= 256);
+ }
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java
index f93679fa8..6f2d0d5e6 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java
@@ -26,6 +26,7 @@ import java.io.FileInputStream;
import org.apache.commons.compress.AbstractTestCase;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.utils.ArchiveUtils;
+import org.apache.commons.compress.utils.IOUtils;
import org.junit.Test;
public class ArArchiveInputStreamTest extends AbstractTestCase {
@@ -58,4 +59,45 @@ public class ArArchiveInputStreamTest extends AbstractTestCase {
assertNull(s.getNextEntry());
}
}
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.ar"));
+ ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ byte[] buf = new byte[2];
+ try (FileInputStream in = new FileInputStream(getFile("bla.ar"));
+ ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+ }
+
+ @org.junit.Ignore("COMPRESS-462")
+ @Test(expected=IllegalStateException.class)
+ public void cantReadWithoutOpeningAnEntry() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.ar"));
+ ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ archive.read();
+ }
+ }
+
+ @Test(expected=java.io.IOException.class)
+ public void cantReadAfterClose() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.ar"));
+ ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ archive.close();
+ archive.read();
+ }
+ }
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java
index 1983f1bde..70a8b1f5b 100644
--- a/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java
@@ -26,6 +26,8 @@ import java.util.Calendar;
import java.util.TimeZone;
import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.utils.IOUtils;
import org.junit.Test;
public class ArjArchiveInputStreamTest extends AbstractTestCase {
@@ -81,4 +83,28 @@ public class ArjArchiveInputStreamTest extends AbstractTestCase {
assertEquals(cal.getTime(), entry.getLastModifiedDate());
in.close();
}
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.arj"));
+ ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ byte[] buf = new byte[2];
+ try (FileInputStream in = new FileInputStream(getFile("bla.arj"));
+ ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+ }
+
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
index 762d4648e..55a9f753f 100644
--- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
@@ -23,6 +23,8 @@ import static org.junit.Assert.*;
import java.io.FileInputStream;
import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.utils.IOUtils;
import org.junit.Test;
public class CpioArchiveInputStreamTest extends AbstractTestCase {
@@ -81,4 +83,28 @@ public class CpioArchiveInputStreamTest extends AbstractTestCase {
assertEquals(2, count);
}
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.cpio"));
+ CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ byte[] buf = new byte[2];
+ try (FileInputStream in = new FileInputStream(getFile("bla.cpio"));
+ CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+ }
+
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java
index a3b67642d..89f6f15df 100644
--- a/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java
@@ -24,7 +24,9 @@ import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.utils.IOUtils;
import org.junit.Test;
public class DumpArchiveInputStreamTest extends AbstractTestCase {
@@ -68,4 +70,27 @@ public class DumpArchiveInputStreamTest extends AbstractTestCase {
dump.close();
}
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.dump"));
+ DumpArchiveInputStream archive = new DumpArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ byte[] buf = new byte[2];
+ try (FileInputStream in = new FileInputStream(getFile("bla.dump"));
+ DumpArchiveInputStream archive = new DumpArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+ }
+
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index f16d58401..b7dcb06c4 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -41,6 +41,7 @@ import java.util.Map;
import java.util.TimeZone;
import java.util.zip.GZIPInputStream;
+import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.utils.CharsetNames;
import org.apache.commons.compress.utils.IOUtils;
import org.junit.Test;
@@ -349,6 +350,29 @@ public class TarArchiveInputStreamTest {
}
}
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ try (FileInputStream in = new FileInputStream(getFile("bla.tar"));
+ TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
+ byte[] buf = new byte[2];
+ try (FileInputStream in = new FileInputStream(getFile("bla.tar"));
+ TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+ }
+
private TarArchiveInputStream getTestStream(final String name) {
return new TarArchiveInputStream(
TarArchiveInputStreamTest.class.getResourceAsStream(name));
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
index 5d1cdb190..5b6595682 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
@@ -340,6 +340,87 @@ public class ZipArchiveInputStreamTest {
}
}
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("bla.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingStore() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-264.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingUnshrink() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("SHRUNK.ZIP"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingExplode() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("imploding-8Kdict-3trees.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate64() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-380/COMPRESS-380.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingBzip2() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("bzip2-zip.zip"));
+ }
+
+ private void singleByteReadConsistentlyReturnsMinusOneAtEof(File file) throws Exception {
+ try (FileInputStream in = new FileInputStream(file);
+ ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read());
+ assertEquals(-1, archive.read());
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("bla.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingStore() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-264.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingUnshrink() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("SHRUNK.ZIP"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingExplode() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("imploding-8Kdict-3trees.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate64() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-380/COMPRESS-380.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingBzip2() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("bzip2-zip.zip"));
+ }
+
+ private void multiByteReadConsistentlyReturnsMinusOneAtEof(File file) throws Exception {
+ byte[] buf = new byte[2];
+ try (FileInputStream in = new FileInputStream(getFile("bla.zip"));
+ ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) {
+ ArchiveEntry e = archive.getNextEntry();
+ IOUtils.toByteArray(archive);
+ assertEquals(-1, archive.read(buf));
+ assertEquals(-1, archive.read(buf));
+ }
+ }
+
private static byte[] readEntry(ZipArchiveInputStream zip, ZipArchiveEntry zae) throws IOException {
final int len = (int)zae.getSize();
final byte[] buff = new byte[len];
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
index 8e07d7e4b..cb6d1975e 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
@@ -616,6 +616,89 @@ public class ZipFileTest {
}
}
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("bla.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingStore() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-264.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingUnshrink() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("SHRUNK.ZIP"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingExplode() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("imploding-8Kdict-3trees.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate64() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-380/COMPRESS-380.zip"));
+ }
+
+ @Test
+ public void singleByteReadConsistentlyReturnsMinusOneAtEofUsingBzip2() throws Exception {
+ singleByteReadConsistentlyReturnsMinusOneAtEof(getFile("bzip2-zip.zip"));
+ }
+
+ private void singleByteReadConsistentlyReturnsMinusOneAtEof(File file) throws Exception {
+ try (ZipFile archive = new ZipFile(file)) {
+ ZipArchiveEntry e = archive.getEntries().nextElement();
+ try (InputStream is = archive.getInputStream(e)) {
+ IOUtils.toByteArray(is);
+ assertEquals(-1, is.read());
+ assertEquals(-1, is.read());
+ }
+ }
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("bla.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingStore() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-264.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingUnshrink() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("SHRUNK.ZIP"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingExplode() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("imploding-8Kdict-3trees.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingDeflate64() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("COMPRESS-380/COMPRESS-380.zip"));
+ }
+
+ @Test
+ public void multiByteReadConsistentlyReturnsMinusOneAtEofUsingBzip2() throws Exception {
+ multiByteReadConsistentlyReturnsMinusOneAtEof(getFile("bzip2-zip.zip"));
+ }
+
+ private void multiByteReadConsistentlyReturnsMinusOneAtEof(File file) throws Exception {
+ byte[] buf = new byte[2];
+ try (ZipFile archive = new ZipFile(file)) {
+ ZipArchiveEntry e = archive.getEntries().nextElement();
+ try (InputStream is = archive.getInputStream(e)) {
+ IOUtils.toByteArray(is);
+ assertEquals(-1, is.read(buf));
+ assertEquals(-1, is.read(buf));
+ }
+ }
+ }
+
private void assertAllReadMethods(byte[] expected, ZipFile zipFile, ZipArchiveEntry entry) {
// simple IOUtil read
try (InputStream stream = zf.getInputStream(entry)) {