Skip to content

Commit

Permalink
verify the compressor streams really mean EOF when they say so
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed Aug 9, 2018
1 parent 792da67 commit 0fe6ae3
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 3 deletions.
Expand Up @@ -279,4 +279,31 @@ public void testMetadataRoundTrip() throws Exception {
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();
}
}
}
Expand Up @@ -77,6 +77,33 @@ public void testLZMAUnarchiveWithAutodetection() throws Exception {
}
}

@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 {
Expand Down
Expand Up @@ -198,4 +198,46 @@ public void testOutputStreamMethods() throws Exception {
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();
}
}

}
Expand Up @@ -123,13 +123,28 @@ public void singleByteReadWorksAsExpected() throws IOException {
}

@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();
}
}
Expand Down
Expand Up @@ -22,8 +22,11 @@

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;

Expand Down Expand Up @@ -67,4 +70,31 @@ public void readOfLength0ShouldReturn0() throws Exception {
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();
}
}

}
Expand Up @@ -65,13 +65,28 @@ public void singleByteReadWorksAsExpected() throws IOException {
}

@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();
}
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -152,4 +153,27 @@ public void throwsEOFExceptionOnTruncatedStreams() throws Exception
}
}

@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();
}
}

}
Expand Up @@ -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;
Expand All @@ -38,4 +39,30 @@ public void readBlaLz4() throws IOException {
}
}

@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();
}
}
}
Expand Up @@ -592,6 +592,33 @@ public void rejectsTrailingBytesAfterValidFrame() throws IOException {
}
}

@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;
}
Expand Down
Expand Up @@ -189,6 +189,33 @@ public void readIWAFileWithBiggerOffset() throws Exception {
}
}

@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
Expand Down

0 comments on commit 0fe6ae3

Please sign in to comment.