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 class ZCompressorInputStreamTest {
44
45 @Test
46 void testFailsToCreateZCompressorInputStreamAndThrowsIOException() throws IOException {
47 try (SequenceInputStream sequenceInputStream = new SequenceInputStream(Collections.emptyEnumeration())) {
48 assertThrows(IOException.class, () -> new ZCompressorInputStream(sequenceInputStream));
49 }
50 }
51
52 @Test
53 void testInvalidMaxCodeSize() throws IOException {
54 final byte[] bytes = AbstractTest.readAllBytes("bla.tar.Z");
55
56
57 final IntStream[] invalid = {
58 IntStream.range(Byte.MIN_VALUE, -120),
59 IntStream.range(-97, -88),
60 IntStream.range(-65, -56),
61 IntStream.range(-33, -24),
62 IntStream.range(-1, 8),
63 IntStream.range(31, 40),
64 IntStream.range(63, 72),
65 IntStream.range(95, 104),
66 IntStream.range(127, 127)
67 };
68
69
70 Stream.of(invalid).forEach(ints -> ints.forEach(i -> {
71 bytes[2] = (byte) i;
72 assertThrows(IllegalArgumentException.class, () -> new ZCompressorInputStream(new ByteArrayInputStream(bytes), 1024 * 1024), () -> "value=" + i);
73 }));
74 }
75
76 @Test
77 void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
78 final File input = AbstractTest.getFile("bla.tar.Z");
79 final byte[] buf = new byte[2];
80 try (InputStream is = Files.newInputStream(input.toPath());
81 ZCompressorInputStream in = new ZCompressorInputStream(is)) {
82 IOUtils.toByteArray(in);
83 assertEquals(-1, in.read(buf));
84 assertEquals(-1, in.read(buf));
85 }
86 }
87
88 @Test
89 void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
90 final File input = AbstractTest.getFile("bla.tar.Z");
91 try (InputStream is = Files.newInputStream(input.toPath());
92 ZCompressorInputStream in = new ZCompressorInputStream(is)) {
93 IOUtils.toByteArray(in);
94 assertEquals(-1, in.read());
95 assertEquals(-1, in.read());
96 }
97 }
98
99 }