-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JarResourceTest.java
286 lines (230 loc) · 10.2 KB
/
JarResourceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.resource;
import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipFile;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(WorkDirExtension.class)
public class JarResourceTest
{
public WorkDir workDir;
@Test
public void testJarFile()
throws Exception
{
Path testZip = MavenTestingUtils.getTestResourcePathFile("TestData/test.zip");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/subdir/";
Resource r = Resource.newResource(s);
Set<String> entries = new HashSet<>(Arrays.asList(r.list()));
assertThat(entries, containsInAnyOrder("alphabet", "numbers", "subsubdir/"));
Path extract = workDir.getPathFile("extract");
FS.ensureEmpty(extract);
r.copyTo(extract.toFile());
Resource e = Resource.newResource(extract.toString());
entries = new HashSet<>(Arrays.asList(e.list()));
assertThat(entries, containsInAnyOrder("alphabet", "numbers", "subsubdir/"));
s = "jar:" + testZip.toUri().toASCIIString() + "!/subdir/subsubdir/";
r = Resource.newResource(s);
entries = new HashSet<>(Arrays.asList(r.list()));
assertThat(entries, containsInAnyOrder("alphabet", "numbers"));
Path extract2 = workDir.getPathFile("extract2");
FS.ensureEmpty(extract2);
r.copyTo(extract2.toFile());
e = Resource.newResource(extract2.toString());
entries = new HashSet<>(Arrays.asList(e.list()));
assertThat(entries, containsInAnyOrder("alphabet", "numbers"));
}
@Test
public void testJarFileGetAllResoures()
throws Exception
{
Path testZip = MavenTestingUtils.getTestResourcePathFile("TestData/test.zip");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/subdir/";
Resource r = Resource.newResource(s);
Collection<Resource> deep = r.getAllResources();
assertEquals(4, deep.size());
}
@Test
public void testJarFileIsContainedIn()
throws Exception
{
Path testZip = MavenTestingUtils.getTestResourcePathFile("TestData/test.zip");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/subdir/";
Resource r = Resource.newResource(s);
Resource container = Resource.newResource(testZip);
assertThat(r, instanceOf(JarFileResource.class));
JarFileResource jarFileResource = (JarFileResource)r;
assertTrue(jarFileResource.isContainedIn(container));
container = Resource.newResource(testZip.getParent());
assertFalse(jarFileResource.isContainedIn(container));
}
@Test
public void testJarFileLastModified()
throws Exception
{
Path testZip = MavenTestingUtils.getTestResourcePathFile("TestData/test.zip");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/subdir/numbers";
try (ZipFile zf = new ZipFile(testZip.toFile()))
{
long last = zf.getEntry("subdir/numbers").getTime();
Resource r = Resource.newResource(s);
assertEquals(last, r.lastModified());
}
}
@Test
public void testJarFileCopyToDirectoryTraversal() throws Exception
{
Path extractZip = MavenTestingUtils.getTestResourcePathFile("TestData/extract.zip");
String s = "jar:" + extractZip.toUri().toASCIIString() + "!/";
Resource r = Resource.newResource(s);
assertThat(r, instanceOf(JarResource.class));
JarResource jarResource = (JarResource)r;
Path destParent = workDir.getPathFile("copyjar");
FS.ensureEmpty(destParent);
Path dest = destParent.toRealPath().resolve("extract");
FS.ensureEmpty(dest);
jarResource.copyTo(dest.toFile());
// dest contains only the valid entry; dest.getParent() contains only the dest directory
assertEquals(1, listFiles(dest).size());
assertEquals(1, listFiles(dest.getParent()).size());
DirectoryStream.Filter<? super Path> dotdotFilenameFilter = (path) ->
path.getFileName().toString().equalsIgnoreCase("dotdot.dot");
assertEquals(0, listFiles(dest, dotdotFilenameFilter).size());
assertEquals(0, listFiles(dest.getParent(), dotdotFilenameFilter).size());
DirectoryStream.Filter<? super Path> extractfileFilenameFilter = (path) ->
path.getFileName().toString().equalsIgnoreCase("extract-filenotdir");
assertEquals(0, listFiles(dest, extractfileFilenameFilter).size());
assertEquals(0, listFiles(dest.getParent(), extractfileFilenameFilter).size());
DirectoryStream.Filter<? super Path> currentDirectoryFilenameFilter = (path) ->
path.getFileName().toString().equalsIgnoreCase("current.txt");
assertEquals(1, listFiles(dest, currentDirectoryFilenameFilter).size());
assertEquals(0, listFiles(dest.getParent(), currentDirectoryFilenameFilter).size());
}
@Test
public void testEncodedFileName()
throws Exception
{
Path testZip = MavenTestingUtils.getTestResourcePathFile("TestData/test.zip");
String s = "jar:" + testZip.toUri().toASCIIString() + "!/file%20name.txt";
Resource r = Resource.newResource(s);
assertTrue(r.exists());
}
@Test
public void testJarFileResourceList() throws Exception
{
Path testJar = MavenTestingUtils.getTestResourcePathFile("jar-file-resource.jar");
String uri = "jar:" + testJar.toUri().toASCIIString() + "!/";
Resource resource = new JarFileResource(URI.create(uri).toURL());
Resource rez = resource.addPath("rez/");
assertThat("path /rez/ is a dir", rez.isDirectory(), is(true));
List<String> actual = Arrays.asList(rez.list());
String[] expected = new String[]{
"one",
"aaa",
"bbb",
"oddities/",
"another dir/",
"ccc",
"deep/",
};
assertThat("Dir contents", actual, containsInAnyOrder(expected));
}
/**
* Test getting a file listing of a Directory in a JAR
* Where the JAR entries contain names that are URI encoded / escaped
*/
@Test
public void testJarFileResourceListPreEncodedEntries() throws Exception
{
Path testJar = MavenTestingUtils.getTestResourcePathFile("jar-file-resource.jar");
String uri = "jar:" + testJar.toUri().toASCIIString() + "!/";
Resource resource = new JarFileResource(URI.create(uri).toURL());
Resource rez = resource.addPath("rez/oddities/");
assertThat("path /rez/oddities/ is a dir", rez.isDirectory(), is(true));
List<String> actual = Arrays.asList(rez.list());
String[] expected = new String[]{
";",
"#hashcode",
"index.html#fragment",
"other%2fkind%2Fof%2fslash", // pre-encoded / escaped
"a file with a space",
";\" onmousedown=\"alert(document.location)\"",
"some\\slash\\you\\got\\there" // not encoded, stored as backslash native
};
assertThat("Dir contents", actual, containsInAnyOrder(expected));
}
@Test
public void testJarFileResourceListDirWithSpace() throws Exception
{
Path testJar = MavenTestingUtils.getTestResourcePathFile("jar-file-resource.jar");
String uri = "jar:" + testJar.toUri().toASCIIString() + "!/";
Resource resource = new JarFileResource(URI.create(uri).toURL());
Resource anotherDir = resource.addPath("rez/another dir/");
assertThat("path /rez/another dir/ is a dir", anotherDir.isDirectory(), is(true));
List<String> actual = Arrays.asList(anotherDir.list());
String[] expected = new String[]{
"a file.txt",
"another file.txt",
"..\\a different file.txt",
};
assertThat("Dir contents", actual, containsInAnyOrder(expected));
}
private List<Path> listFiles(Path dir) throws IOException
{
try (Stream<Path> s = Files.list(dir))
{
return s.collect(Collectors.toList());
}
}
private List<Path> listFiles(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException
{
List<Path> results = new ArrayList<>();
try (DirectoryStream<Path> filteredDirStream = Files.newDirectoryStream(dir, filter))
{
for (Path path : filteredDirStream)
{
results.add(path);
}
return results;
}
}
}