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   * http://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   * Unit tests for class {@link ZCompressorInputStream}.
40   *
41   * @see ZCompressorInputStream
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          // @formatter:off
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          // @formatter:on
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  }