commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [2/3] commons-compress git commit: COMPRESS-271 more tests
Date Mon, 23 Jan 2017 07:28:20 GMT
COMPRESS-271 more tests


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/45668bfc
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/45668bfc
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/45668bfc

Branch: refs/heads/master
Commit: 45668bfc168f43bca1be6813580411a01454a807
Parents: 3b81f3a
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Mon Jan 23 08:24:20 2017 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Mon Jan 23 08:24:20 2017 +0100

----------------------------------------------------------------------
 .../lz4/FramedLZ4CompressorInputStreamTest.java | 116 +++++++++++++++++++
 1 file changed, 116 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/45668bfc/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java
----------------------------------------------------------------------
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 d013dff..6cacd2e 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
@@ -18,9 +18,11 @@
  */
 package org.apache.commons.compress.compressors.lz4;
 
+import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 
 import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
@@ -87,4 +89,118 @@ public final class FramedLZ4CompressorInputStreamTest
         }
     }
 
+    @Test
+    public void rejectsFileWithoutFrameDescriptor() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18 // signature
+        };
+        try {
+            try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+                fail("expected exception");
+            }
+        } catch (IOException ex) {
+            assertThat(ex.getMessage(), containsString("frame flags"));
+        }
+    }
+
+    @Test
+    public void rejectsFileWithoutBlockSizeByte() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18, // signature
+            0x64, // flag - Version 01, block independent, no block checksum, no content
size, with content checksum
+        };
+        try {
+            try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+                fail("expected exception");
+            }
+        } catch (IOException ex) {
+            assertThat(ex.getMessage(), containsString("BD byte"));
+        }
+    }
+
+    @Test
+    public void rejectsFileWithWrongVersion() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18, // signature
+            0x24, // flag - Version 00, block independent, no block checksum, no content
size, with content checksum
+        };
+        try {
+            try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+                fail("expected exception");
+            }
+        } catch (IOException ex) {
+            assertThat(ex.getMessage(), containsString("version"));
+        }
+    }
+
+    @Test
+    public void rejectsFileWithInsufficientContentSize() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18, // signature
+            0x6C, // flag - Version 01, block independent, no block checksum, with content
size, with content checksum
+            0x70, // block size 4MB
+        };
+        try {
+            try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+                fail("expected exception");
+            }
+        } catch (IOException ex) {
+            assertThat(ex.getMessage(), containsString("content size"));
+        }
+    }
+
+    @Test
+    public void rejectsFileWithoutHeaderChecksum() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18, // signature
+            0x64, // flag - Version 01, block independent, no block checksum, no content
size, with content checksum
+            0x70, // block size 4MB
+        };
+        try {
+            try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+                fail("expected exception");
+            }
+        } catch (IOException ex) {
+            assertThat(ex.getMessage(), containsString("header checksum"));
+        }
+    }
+
+    @Test
+    public void readsUncompressedBlocks() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18, // signature
+            0x60, // flag - Version 01, block independent, no block checksum, no content
size, no content checksum
+            0x70, // block size 4MB
+            0x00, // checksum, revisit once it gets validated
+            13, 0, 0, (byte) 0x80, // 13 bytes length and uncompressed bit set
+            'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // content
+            0, 0, 0, 0, // empty block marker
+        };
+        try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+            byte[] actual = IOUtils.toByteArray(a);
+            assertArrayEquals(new byte[] {
+                    'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'
+                }, actual);
+        }
+    }
+
+    @Test
+    public void rejectsBlocksWithoutChecksum() throws IOException {
+        byte[] input = new byte[] {
+            4, 0x22, 0x4d, 0x18, // signature
+            0x70, // flag - Version 01, block independent, with block checksum, no content
size, no content checksum
+            0x70, // block size 4MB
+            0x00, // checksum, revisit once it gets validated
+            13, 0, 0, (byte) 0x80, // 13 bytes length and uncompressed bit set
+            'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // content
+        };
+        try {
+            try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input)))
{
+                IOUtils.toByteArray(a);
+                fail("expected exception");
+            }
+        } catch (IOException ex) {
+            assertThat(ex.getMessage(), containsString("block checksum"));
+        }
+    }
 }


Mime
View raw message