View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests for class {@link ZCompressorInputStream}.
40   *
41   * @see ZCompressorInputStream
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          // @formatter:off
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          // @formatter:on
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  }