View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.commons.compress.archivers.zip;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import org.apache.commons.compress.AbstractTest;
27  import org.apache.commons.compress.archivers.ArchiveEntry;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * JUnit test for a multi-volume ZIP file.
32   *
33   * Some tools (like 7-zip) allow users to split a large archives into 'volumes' with a given size to fit them into multiple cds, usb drives, or emails with an
34   * attachment size limit. It's basically the same file split into chunks of exactly 65536 bytes length. Concatenating volumes yields exactly the original file.
35   * There is no mechanism in the ZIP algorithm to accommodate for this. Before commons-compress used to enter an infinite loop on the last entry for such a file.
36   * This test is intended to prove that this error doesn't occur anymore. All entries but the last one are returned correctly, the last entry yields an
37   * exception.
38   */
39  public class Maven221MultiVolumeTest extends AbstractTest {
40  
41      private static final String[] ENTRIES = {
42          // @formatter:off
43          "apache-maven-2.2.1/",
44          "apache-maven-2.2.1/LICENSE.txt",
45          "apache-maven-2.2.1/NOTICE.txt",
46          "apache-maven-2.2.1/README.txt",
47          "apache-maven-2.2.1/bin/",
48          "apache-maven-2.2.1/bin/m2.conf",
49          "apache-maven-2.2.1/bin/mvn",
50          "apache-maven-2.2.1/bin/mvn.bat",
51          "apache-maven-2.2.1/bin/mvnDebug",
52          "apache-maven-2.2.1/bin/mvnDebug.bat",
53          "apache-maven-2.2.1/boot/",
54          "apache-maven-2.2.1/boot/classworlds-1.1.jar",
55          "apache-maven-2.2.1/conf/",
56          "apache-maven-2.2.1/conf/settings.xml",
57          "apache-maven-2.2.1/lib/"
58          // @formatter:on
59      };
60  
61      private static final String LAST_ENTRY_NAME = "apache-maven-2.2.1/lib/maven-2.2.1-uber.jar";
62  
63      @Test
64      public void testRead7ZipMultiVolumeArchiveForFile() {
65          assertThrows(IOException.class, () -> ZipFile.builder().setFile(getFile("apache-maven-2.2.1.zip.001")).get());
66      }
67  
68      @Test
69      public void testRead7ZipMultiVolumeArchiveForStream() throws IOException {
70  
71          try (InputStream archive = newInputStream("apache-maven-2.2.1.zip.001");
72                  ZipArchiveInputStream zi = new ZipArchiveInputStream(archive, null, false)) {
73  
74              // these are the entries that are supposed to be processed
75              // correctly without any problems
76              for (final String element : ENTRIES) {
77                  assertEquals(element, zi.getNextEntry().getName());
78              }
79  
80              // this is the last entry that is truncated
81              final ArchiveEntry lastEntry = zi.getNextEntry();
82              assertEquals(LAST_ENTRY_NAME, lastEntry.getName());
83              final byte[] buffer = new byte[4096];
84  
85              // before the fix, we'd get 0 bytes on this read and all
86              // subsequent reads thus a client application might enter
87              // an infinite loop after the fix, we should get an
88              // exception
89              final IOException e1 = assertThrows(IOException.class, () -> {
90                  while (zi.read(buffer) > 0) {
91                      // empty
92                  }
93              }, "shouldn't be able to read from truncated entry");
94              assertEquals("Truncated ZIP file", e1.getMessage());
95  
96              final IOException e2 = assertThrows(IOException.class, () -> zi.read(buffer), "shouldn't be able to read from truncated entry after exception");
97              assertEquals("Truncated ZIP file", e2.getMessage());
98  
99              // and now we get another entry, which should also yield
100             // an exception
101             assertThrows(IOException.class, zi::getNextEntry, "shouldn't be able to read another entry from truncated file");
102         }
103     }
104 }