1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.compressors.z;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.SequenceInputStream;
29 import java.nio.file.Files;
30 import java.util.Collections;
31 import java.util.stream.IntStream;
32 import java.util.stream.Stream;
33
34 import org.apache.commons.compress.AbstractTest;
35 import org.apache.commons.io.IOUtils;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41
42
43 public class ZCompressorInputStreamTest {
44
45 @Test
46 public void testFailsToCreateZCompressorInputStreamAndThrowsIOException() {
47 final SequenceInputStream sequenceInputStream = new SequenceInputStream(Collections.emptyEnumeration());
48 assertThrows(IOException.class, () -> new ZCompressorInputStream(sequenceInputStream));
49 }
50
51 @Test
52 public void testInvalidMaxCodeSize() throws IOException {
53 final byte[] bytes = AbstractTest.readAllBytes("bla.tar.Z");
54
55
56 final IntStream[] invalid = {
57 IntStream.range(Byte.MIN_VALUE, -120),
58 IntStream.range(-97, -88),
59 IntStream.range(-65, -56),
60 IntStream.range(-33, -24),
61 IntStream.range(-1, 8),
62 IntStream.range(31, 40),
63 IntStream.range(63, 72),
64 IntStream.range(95, 104),
65 IntStream.range(127, 127)
66 };
67
68
69 Stream.of(invalid).forEach(ints -> ints.forEach(i -> {
70 bytes[2] = (byte) i;
71 assertThrows(IllegalArgumentException.class, () -> new ZCompressorInputStream(new ByteArrayInputStream(bytes), 1024 * 1024), () -> "value=" + i);
72 }));
73 }
74
75 @Test
76 public void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
77 final File input = AbstractTest.getFile("bla.tar.Z");
78 final byte[] buf = new byte[2];
79 try (InputStream is = Files.newInputStream(input.toPath());
80 ZCompressorInputStream in = new ZCompressorInputStream(is)) {
81 IOUtils.toByteArray(in);
82 assertEquals(-1, in.read(buf));
83 assertEquals(-1, in.read(buf));
84 }
85 }
86
87 @Test
88 public void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
89 final File input = AbstractTest.getFile("bla.tar.Z");
90 try (InputStream is = Files.newInputStream(input.toPath());
91 ZCompressorInputStream in = new ZCompressorInputStream(is)) {
92 IOUtils.toByteArray(in);
93 assertEquals(-1, in.read());
94 assertEquals(-1, in.read());
95 }
96 }
97
98 }