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  
20  package org.apache.commons.compress.archivers;
21  
22  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.io.BufferedInputStream;
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.net.URISyntaxException;
30  import java.nio.file.Files;
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.stream.Stream;
34  
35  import org.apache.commons.compress.AbstractTest;
36  import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
37  import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
38  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
39  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
40  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
41  import org.junit.jupiter.api.BeforeAll;
42  import org.junit.jupiter.params.ParameterizedTest;
43  import org.junit.jupiter.params.provider.Arguments;
44  import org.junit.jupiter.params.provider.MethodSource;
45  
46  /**
47   * Test that can read various tar file examples.
48   *
49   * Files must be in resources/longsymlink, and there must be a file.txt containing the list of files in the archives.
50   */
51  class LongSymLinkTest extends AbstractTest {
52  
53      private static final ClassLoader CLASS_LOADER = LongSymLinkTest.class.getClassLoader();
54      private static final File ARC_DIR;
55      private static final ArrayList<String> FILE_LIST = new ArrayList<>();
56  
57      static {
58          try {
59              ARC_DIR = new File(CLASS_LOADER.getResource("longsymlink").toURI());
60          } catch (final URISyntaxException e) {
61              throw new AssertionError(e);
62          }
63      }
64  
65      public static Stream<Arguments> data() {
66          final Collection<Arguments> params = new ArrayList<>();
67          for (final String fileName : ARC_DIR.list((dir, name) -> !name.endsWith(".txt"))) {
68              params.add(Arguments.of(new File(ARC_DIR, fileName)));
69          }
70          return params.stream();
71      }
72  
73      @BeforeAll
74      public static void setUpFileList() throws Exception {
75          assertTrue(ARC_DIR.exists());
76          final File listing = new File(ARC_DIR, "files.txt");
77          assertTrue(listing.canRead(), "files.txt is readable");
78          try (BufferedReader br = new BufferedReader(Files.newBufferedReader(listing.toPath()))) {
79              String line;
80              while ((line = br.readLine()) != null) {
81                  if (!line.startsWith("#")) {
82                      FILE_LIST.add(line);
83                  }
84              }
85          }
86      }
87  
88      @Override
89      protected String getExpectedString(final ArchiveEntry entry) {
90          if (entry instanceof TarArchiveEntry) {
91              final TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
92              if (tarEntry.isSymbolicLink()) {
93                  return tarEntry.getName() + " -> " + tarEntry.getLinkName();
94              }
95          }
96          return entry.getName();
97      }
98  
99      @ParameterizedTest
100     @MethodSource("data")
101     void testArchive(final File file) throws Exception {
102         @SuppressWarnings("unchecked") // fileList is of correct type
103         final ArrayList<String> expected = (ArrayList<String>) FILE_LIST.clone();
104         final String name = file.getName();
105         if ("minotaur.jar".equals(name) || "minotaur-0.jar".equals(name)) {
106             expected.add("META-INF/");
107             expected.add("META-INF/MANIFEST.MF");
108         }
109         try (ArchiveInputStream<?> ais = factory.createArchiveInputStream(new BufferedInputStream(Files.newInputStream(file.toPath())))) {
110             // check if expected type recognized
111             if (name.endsWith(".tar")) {
112                 assertInstanceOf(TarArchiveInputStream.class, ais);
113             } else if (name.endsWith(".jar") || name.endsWith(".zip")) {
114                 assertInstanceOf(ZipArchiveInputStream.class, ais);
115             } else if (name.endsWith(".cpio")) {
116                 assertInstanceOf(CpioArchiveInputStream.class, ais);
117                 // Hack: cpio does not add trailing "/" to directory names
118                 for (int i = 0; i < expected.size(); i++) {
119                     final String ent = expected.get(i);
120                     if (ent.endsWith("/")) {
121                         expected.set(i, ent.substring(0, ent.length() - 1));
122                     }
123                 }
124             } else if (name.endsWith(".ar")) {
125                 assertInstanceOf(ArArchiveInputStream.class, ais);
126                 // CPIO does not store directories or directory names
127                 expected.clear();
128                 for (final String ent : FILE_LIST) {
129                     if (!ent.endsWith("/")) {
130                         // not a directory
131                         final int lastSlash = ent.lastIndexOf('/');
132                         if (lastSlash >= 0) {
133                             // extract path name
134                             expected.add(ent.substring(lastSlash + 1));
135                         } else {
136                             expected.add(ent);
137                         }
138                     }
139                 }
140             } else {
141                 fail("Unexpected file type: " + name);
142             }
143         }
144     }
145 }