1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
48
49
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")
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
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
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
127 expected.clear();
128 for (final String ent : FILE_LIST) {
129 if (!ent.endsWith("/")) {
130
131 final int lastSlash = ent.lastIndexOf('/');
132 if (lastSlash >= 0) {
133
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 }