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.archivers.dump;
20  
21  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
24  import static org.junit.jupiter.api.Assertions.assertNotNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
27  import static org.junit.jupiter.api.Assertions.assertTrue;
28  
29  import java.io.InputStream;
30  import java.time.Duration;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.apache.commons.compress.AbstractTest;
34  import org.apache.commons.compress.archivers.ArchiveException;
35  import org.apache.commons.io.IOUtils;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.Timeout;
38  import org.junit.jupiter.api.Timeout.ThreadMode;
39  
40  public class DumpArchiveInputStreamTest extends AbstractTest {
41  
42      @SuppressWarnings("deprecation")
43      @Test
44      public void testConsumesArchiveCompletely() throws Exception {
45          try (InputStream is = DumpArchiveInputStreamTest.class.getResourceAsStream("/archive_with_trailer.dump");
46                  DumpArchiveInputStream dump = new DumpArchiveInputStream(is)) {
47              while (dump.getNextDumpEntry() != null) {
48                  // just consume the archive
49              }
50              final byte[] expected = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' };
51              final byte[] actual = new byte[expected.length];
52              is.read(actual);
53              assertArrayEquals(expected, actual);
54          }
55      }
56  
57      @Test
58      public void testDirectoryNullBytes() throws Exception {
59          try (InputStream is = newInputStream("org/apache/commons/compress/dump/directory_null_bytes-fail.dump");
60                  DumpArchiveInputStream archive = new DumpArchiveInputStream(is)) {
61              assertThrows(InvalidFormatException.class, archive::getNextEntry);
62          }
63      }
64  
65      @Test
66      public void testInvalidCompressType() throws Exception {
67          try (InputStream is = newInputStream("org/apache/commons/compress/dump/invalid_compression_type-fail.dump")) {
68              final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is).close());
69              assertInstanceOf(UnsupportedCompressionAlgorithmException.class, ex.getCause());
70          }
71      }
72  
73      @Test
74      @Timeout(value = 15,unit = TimeUnit.SECONDS, threadMode = ThreadMode.SEPARATE_THREAD)
75      public void testLoopingInodes() throws Exception {
76          try (InputStream is = newInputStream("org/apache/commons/compress/dump/looping_inodes-fail.dump");
77                  DumpArchiveInputStream archive = new DumpArchiveInputStream(is)) {
78              archive.getNextEntry();
79              assertThrows(DumpArchiveException.class, archive::getNextEntry);
80          }
81      }
82  
83      @Test
84      public void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
85          final byte[] buf = new byte[2];
86          try (InputStream in = newInputStream("bla.dump");
87                  DumpArchiveInputStream archive = new DumpArchiveInputStream(in)) {
88              assertNotNull(archive.getNextEntry());
89              IOUtils.toByteArray(archive);
90              assertEquals(-1, archive.read(buf));
91              assertEquals(-1, archive.read(buf));
92          }
93      }
94  
95      @Test
96      public void testNotADumpArchive() throws Exception {
97          try (InputStream is = newInputStream("bla.zip")) {
98              final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is).close(), "expected an exception");
99              assertTrue(ex.getCause() instanceof ShortFileException);
100         }
101     }
102 
103     @Test
104     public void testNotADumpArchiveButBigEnough() throws Exception {
105         try (InputStream is = newInputStream("zip64support.tar.bz2")) {
106             final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is).close(), "expected an exception");
107             assertInstanceOf(UnrecognizedFormatException.class, ex.getCause());
108         }
109     }
110 
111     @Test
112     public void testRecLenZeroLongExecution() throws Exception {
113         try (InputStream is = newInputStream("org/apache/commons/compress/dump/reclen_zero-fail.dump");
114                 DumpArchiveInputStream archive = new DumpArchiveInputStream(is)) {
115             assertTimeoutPreemptively(Duration.ofSeconds(20), () -> {
116                 assertThrows(DumpArchiveException.class, archive::getNextEntry);
117             });
118         }
119     }
120 
121     @Test
122     public void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
123         try (InputStream in = newInputStream("bla.dump");
124                 DumpArchiveInputStream archive = new DumpArchiveInputStream(in)) {
125             assertNotNull(archive.getNextEntry());
126             IOUtils.toByteArray(archive);
127             assertEquals(-1, archive.read());
128             assertEquals(-1, archive.read());
129         }
130     }
131 
132 }