diff options
author | Stefan Bodewig <bodewig@apache.org> | 2018-08-09 18:12:52 +0200 |
---|---|---|
committer | Stefan Bodewig <bodewig@apache.org> | 2018-08-09 18:12:52 +0200 |
commit | 0fe6ae319d50edd1389b6ecb5a6fd12f8091aea0 (patch) | |
tree | 1bf137903535eb5e84ec61dbca9ca59078e80753 | |
parent | 792da6735e532a6e7f2f2a017d999a8f7c30f2fe (diff) | |
download | apache-commons-compress-0fe6ae319d50edd1389b6ecb5a6fd12f8091aea0.tar.gz |
verify the compressor streams really mean EOF when they say so
13 files changed, 364 insertions, 3 deletions
diff --git a/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java b/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java index 02fda3e2d..9faa874af 100644 --- a/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java +++ b/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java @@ -279,4 +279,31 @@ public final class GZipTestCase extends AbstractTestCase { assertEquals("test3.xml", readParams.getFilename()); assertEquals("Umlaute möglich?", readParams.getComment()); } + + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tgz"); + try (InputStream is = new FileInputStream(input)) { + final GzipCompressorInputStream in = + new GzipCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tgz"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final GzipCompressorInputStream in = + new GzipCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); + in.close(); + } + } } diff --git a/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java index 622449dc5..3d2ab027b 100644 --- a/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java +++ b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java @@ -77,6 +77,33 @@ public final class LZMATestCase extends AbstractTestCase { } } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.lzma"); + try (InputStream is = new FileInputStream(input)) { + final LZMACompressorInputStream in = + new LZMACompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.lzma"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final LZMACompressorInputStream in = + new LZMACompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); + in.close(); + } + } + private void copy(final InputStream in, final File output) throws IOException { FileOutputStream out = null; try { diff --git a/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java b/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java index 1896d1064..d346185ae 100644 --- a/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java +++ b/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java @@ -198,4 +198,46 @@ public final class Pack200TestCase extends AbstractTestCase { os.close(); } } + + @Test + public void singleByteReadFromMemoryConsistentlyReturnsMinusOneAtEof() throws Exception { + singleByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.IN_MEMORY); + } + + @Test + public void singleByteReadFromTempFileConsistentlyReturnsMinusOneAtEof() throws Exception { + singleByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.TEMP_FILE); + } + + private void singleByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy s) throws Exception { + final File input = getFile("bla.pack"); + try (final Pack200CompressorInputStream in = new Pack200CompressorInputStream(input, s)) { + IOUtils.toByteArray(in); + assertEquals(-1, in.read()); + assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadFromMemoryConsistentlyReturnsMinusOneAtEof() throws Exception { + multiByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.IN_MEMORY); + } + + @Test + public void multiByteReadFromTempFileConsistentlyReturnsMinusOneAtEof() throws Exception { + multiByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.TEMP_FILE); + } + + private void multiByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy s) throws Exception { + final File input = getFile("bla.pack"); + byte[] buf = new byte[2]; + try (final Pack200CompressorInputStream in = new Pack200CompressorInputStream(input, s)) { + IOUtils.toByteArray(in); + assertEquals(-1, in.read(buf)); + assertEquals(-1, in.read(buf)); + in.close(); + } + } + } diff --git a/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java index e26b63713..721ab33e0 100644 --- a/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java @@ -123,13 +123,28 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase { } @Test - public void singleByteReadReturnsMinusOneAtEof() throws IOException { + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { final File input = getFile("brotli.testdata.compressed"); try (InputStream is = new FileInputStream(input)) { final BrotliCompressorInputStream in = new BrotliCompressorInputStream(is); IOUtils.toByteArray(in); Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("brotli.testdata.compressed"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final BrotliCompressorInputStream in = + new BrotliCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); in.close(); } } diff --git a/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java index 1d4910270..7130d928a 100644 --- a/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java @@ -22,8 +22,11 @@ import static org.apache.commons.compress.AbstractTestCase.getFile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.compress.utils.IOUtils; import org.junit.Assert; import org.junit.Test; @@ -67,4 +70,31 @@ public class BZip2CompressorInputStreamTest { bzipIn.close(); } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.txt.bz2"); + try (InputStream is = new FileInputStream(input)) { + final BZip2CompressorInputStream in = + new BZip2CompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.txt.bz2"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final BZip2CompressorInputStream in = + new BZip2CompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); + in.close(); + } + } + } diff --git a/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java index 1d8261e11..97a010f6c 100644 --- a/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java @@ -65,13 +65,28 @@ public class DeflateCompressorInputStreamTest { } @Test - public void singleByteReadReturnsMinusOneAtEof() throws IOException { + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { final File input = AbstractTestCase.getFile("bla.tar.deflatez"); try (InputStream is = new FileInputStream(input)) { final DeflateCompressorInputStream in = new DeflateCompressorInputStream(is); IOUtils.toByteArray(in); Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = AbstractTestCase.getFile("bla.tar.deflatez"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final DeflateCompressorInputStream in = + new DeflateCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); in.close(); } } diff --git a/src/test/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStreamTest.java index 3526224ad..531a14fea 100644 --- a/src/test/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStreamTest.java @@ -18,6 +18,7 @@ package org.apache.commons.compress.compressors.deflate64; import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.commons.compress.utils.IOUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -152,4 +153,27 @@ public class Deflate64CompressorInputStreamTest { } } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception { + try (final Deflate64CompressorInputStream in = + new Deflate64CompressorInputStream(nullDecoder)) { + IOUtils.toByteArray(in); + assertEquals(-1, in.read()); + assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception { + byte[] buf = new byte[2]; + try (final Deflate64CompressorInputStream in = + new Deflate64CompressorInputStream(nullDecoder)) { + IOUtils.toByteArray(in); + assertEquals(-1, in.read(buf)); + assertEquals(-1, in.read(buf)); + in.close(); + } + } + } diff --git a/src/test/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStreamTest.java index 801630ea6..13331dd7a 100644 --- a/src/test/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStreamTest.java @@ -18,6 +18,7 @@ */ package org.apache.commons.compress.compressors.lz4; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -38,4 +39,30 @@ public class BlockLZ4CompressorInputStreamTest extends AbstractTestCase { } } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.block_lz4"); + try (InputStream is = new FileInputStream(input)) { + final BlockLZ4CompressorInputStream in = + new BlockLZ4CompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.block_lz4"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final BlockLZ4CompressorInputStream in = + new BlockLZ4CompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); + in.close(); + } + } } diff --git a/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java index e3211fa85..2fd564b97 100644 --- a/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java @@ -592,6 +592,33 @@ public final class FramedLZ4CompressorInputStreamTest } } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.lz4"); + try (InputStream is = new FileInputStream(input)) { + final FramedLZ4CompressorInputStream in = + new FramedLZ4CompressorInputStream(is); + IOUtils.toByteArray(in); + assertEquals(-1, in.read()); + assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.lz4"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final FramedLZ4CompressorInputStream in = + new FramedLZ4CompressorInputStream(is); + IOUtils.toByteArray(in); + assertEquals(-1, in.read(buf)); + assertEquals(-1, in.read(buf)); + in.close(); + } + } + interface StreamWrapper { InputStream wrap(InputStream in) throws Exception; } diff --git a/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java index cac4b2cf6..879456e32 100644 --- a/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java @@ -189,6 +189,33 @@ public final class FramedSnappyCompressorInputStreamTest } } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.sz"); + try (InputStream is = new FileInputStream(input)) { + final FramedSnappyCompressorInputStream in = + new FramedSnappyCompressorInputStream(is); + IOUtils.toByteArray(in); + assertEquals(-1, in.read()); + assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.sz"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final FramedSnappyCompressorInputStream in = + new FramedSnappyCompressorInputStream(is); + IOUtils.toByteArray(in); + assertEquals(-1, in.read(buf)); + assertEquals(-1, in.read(buf)); + in.close(); + } + } + private void testChecksumUnmasking(final long x) { assertEquals(Long.toHexString(x), Long.toHexString(FramedSnappyCompressorInputStream diff --git a/src/test/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStreamTest.java index 6b8ab1332..a43dca3de 100644 --- a/src/test/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStreamTest.java @@ -18,6 +18,13 @@ */ package org.apache.commons.compress.compressors.xz; +import static org.apache.commons.compress.AbstractTestCase.getFile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.compress.utils.IOUtils; import org.junit.Assert; import org.junit.Test; @@ -33,4 +40,50 @@ public class XZCompressorInputStreamTest { data[5] = '0'; Assert.assertFalse(XZCompressorInputStream.matches(data, 6)); } + + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEofNoDecompressConcatenated() throws IOException { + singleByteReadConsistentlyReturnsMinusOneAtEof(false); + } + + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEofDecompressConcatenated() throws IOException { + singleByteReadConsistentlyReturnsMinusOneAtEof(true); + } + + private void singleByteReadConsistentlyReturnsMinusOneAtEof(boolean decompressConcatenated) throws IOException { + final File input = getFile("bla.tar.xz"); + try (InputStream is = new FileInputStream(input)) { + final XZCompressorInputStream in = + new XZCompressorInputStream(is, decompressConcatenated); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEofNoDecompressConcatenated() throws IOException { + multiByteReadConsistentlyReturnsMinusOneAtEof(false); + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEofDecompressConcatenated() throws IOException { + multiByteReadConsistentlyReturnsMinusOneAtEof(true); + } + + private void multiByteReadConsistentlyReturnsMinusOneAtEof(boolean decompressConcatenated) throws IOException { + final File input = getFile("bla.tar.xz"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final XZCompressorInputStream in = + new XZCompressorInputStream(is, decompressConcatenated); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); + in.close(); + } + } + } diff --git a/src/test/java/org/apache/commons/compress/compressors/z/ZCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/z/ZCompressorInputStreamTest.java index 8f62c9ba6..9e7786b72 100644 --- a/src/test/java/org/apache/commons/compress/compressors/z/ZCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/z/ZCompressorInputStreamTest.java @@ -18,12 +18,18 @@ */ package org.apache.commons.compress.compressors.z; +import org.junit.Assert; import org.junit.Test; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.SequenceInputStream; import java.util.Enumeration; +import org.apache.commons.compress.utils.IOUtils; +import static org.apache.commons.compress.AbstractTestCase.getFile; import static org.mockito.Mockito.mock; import static org.powermock.api.mockito.PowerMockito.doReturn; @@ -58,5 +64,31 @@ public class ZCompressorInputStreamTest { } + @Test + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.Z"); + try (InputStream is = new FileInputStream(input)) { + final ZCompressorInputStream in = + new ZCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("bla.tar.Z"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final ZCompressorInputStream in = + new ZCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); + in.close(); + } + } } diff --git a/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java index cadf19854..1d5f06659 100644 --- a/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java @@ -124,13 +124,28 @@ public class ZstdCompressorInputStreamTest extends AbstractTestCase { } @Test - public void singleByteReadReturnsMinusOneAtEof() throws IOException { + public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { final File input = getFile("zstandard.testdata.zst"); try (InputStream is = new FileInputStream(input)) { final ZstdCompressorInputStream in = new ZstdCompressorInputStream(is); IOUtils.toByteArray(in); Assert.assertEquals(-1, in.read()); + Assert.assertEquals(-1, in.read()); + in.close(); + } + } + + @Test + public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { + final File input = getFile("zstandard.testdata.zst"); + byte[] buf = new byte[2]; + try (InputStream is = new FileInputStream(input)) { + final ZstdCompressorInputStream in = + new ZstdCompressorInputStream(is); + IOUtils.toByteArray(in); + Assert.assertEquals(-1, in.read(buf)); + Assert.assertEquals(-1, in.read(buf)); in.close(); } } |