forked from McModLauncher/securejarhandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUnionFS.java
More file actions
307 lines (284 loc) · 14.7 KB
/
TestUnionFS.java
File metadata and controls
307 lines (284 loc) · 14.7 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package cpw.mods.niofs.union;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestUnionFS {
private static final UnionFileSystemProvider UFSP = (UnionFileSystemProvider) FileSystemProvider.installedProviders().stream().filter(fsp->fsp.getScheme().equals("union")).findFirst().orElseThrow(()->new IllegalStateException("Couldn't find UnionFileSystemProvider"));
@Test
void testUnionFileSystem() throws IOException {
final var dir1 = Paths.get("src", "test", "resources", "dir1").toAbsolutePath().normalize();
final var dir2 = Paths.get("src", "test", "resources", "dir2").toAbsolutePath().normalize();
final var fileSystem = FileSystems.newFileSystem(dir1, Map.of("additional", List.of(dir2)));
assertAll(
()->assertTrue(fileSystem instanceof UnionFileSystem),
()->assertIterableEquals(fileSystem instanceof UnionFileSystem ufs ? ufs.getBasePaths(): List.of(), List.of(dir2, dir1))
);
UnionFileSystem ufs = (UnionFileSystem) fileSystem;
final var masktest = ufs.getPath("masktest.txt");
assertAll(
()->assertTrue(Files.exists(masktest)),
()->assertEquals(Files.readString(masktest), "dir2")
);
assertAll(
Files.walk(masktest.toAbsolutePath().getRoot())
.map(Files::exists)
.map(f->()->assertTrue(f))
);
assertFalse(Files.exists(ufs.getPath("fishyfishhead.txt")));
var p = ufs.getRoot().resolve("subdir1/masktestd1.txt");
p.subpath(1, 2);
}
@Test
void testUnionFileSystemJar() throws Throwable {
final var jar1 = Paths.get("sjh-jmh","src", "testjars", "testjar1.jar").toAbsolutePath().normalize();
final var jar2 = Paths.get("sjh-jmh","src", "testjars", "testjar2.jar").toAbsolutePath().normalize();
final var jar3 = Paths.get("sjh-jmh","src", "testjars", "testjar3.jar").toAbsolutePath().normalize();
final var fileSystem = UFSP.newFileSystem(jar1, Map.of("additional", List.of(jar2, jar3)));
assertAll(
()->assertTrue(fileSystem instanceof UnionFileSystem),
()->assertIterableEquals(fileSystem instanceof UnionFileSystem ufs ? ufs.getBasePaths(): List.of(), List.of(jar3, jar2, jar1))
);
UnionFileSystem ufs = (UnionFileSystem) fileSystem;
var doexist = List.of("cpw/mods/niofs/union/UnionPath.class", "net/minecraftforge/client/event/GuiOpenEvent.class", "cpw/mods/modlauncher/Launcher.class"); //jar 3
var dontexist = List.of("cpw/mods/modlauncher/api/NoIDontExist.class", "net/minecraftforge/client/nonexistent/Nope.class", "Missing.class");
assertAll(
doexist.stream().map(ufs::getPath).map(p->()->assertTrue(Files.exists(p)))
);
assertAll(
dontexist.stream().map(ufs::getPath).map(p->()->assertTrue(Files.notExists(p)))
);
}
@Test
void testRelativize() {
final var dir1 = Paths.get("src", "test", "resources", "dir1").toAbsolutePath().normalize();
final var dir2 = Paths.get("src", "test", "resources", "dir2").toAbsolutePath().normalize();
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
var ufs = fsp.newFileSystem((path, base) -> true, dir1, dir2);
var p1 = ufs.getPath("path1");
var p123 = ufs.getPath("path1/path2/path3");
var p11 = ufs.getPath("path1/path1");
var p12 = ufs.getPath("path1/path2");
var p13 = ufs.getPath("path1/path3");
var p23 = ufs.getPath("path2/path3");
var p13plus = ufs.getPath("path1/path3");
assertAll(
()->assertEquals("path2/path3", p1.relativize(p123).toString()),
()->assertEquals("../..", p123.relativize(p1).toString()),
()->assertEquals("path1", p1.relativize(p11).toString()),
()->assertEquals("path2", p1.relativize(p12).toString()),
()->assertEquals("path3", p1.relativize(p13).toString()),
()->assertEquals("../../path1/path1", p23.relativize(p11).toString()),
()->assertEquals("../../path1", p123.relativize(p11).toString()),
()->assertEquals(0, p13.relativize(p13plus).getNameCount())
);
}
@Test
void testRelativizeAbsolute() {
final var dir1 = Paths.get("src", "test", "resources", "dir1").toAbsolutePath().normalize();
final var dir2 = Paths.get("src", "test", "resources", "dir2").toAbsolutePath().normalize();
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
var ufs = fsp.newFileSystem((path, base) -> true, dir1, dir2);
var p1 = ufs.getPath("/path1");
var p123 = ufs.getPath("/path1/path2/path3");
var p11 = ufs.getPath("/path1/path1");
var p12 = ufs.getPath("/path1/path2");
var p13 = ufs.getPath("/path1/path3");
var p23 = ufs.getPath("/path2/path3");
var p13plus = ufs.getPath("/path1/path3");
assertAll(
()->assertEquals("path2/path3", p1.relativize(p123).toString()),
()->assertEquals("../..", p123.relativize(p1).toString()),
()->assertEquals("path1", p1.relativize(p11).toString()),
()->assertEquals("path2", p1.relativize(p12).toString()),
()->assertEquals("path3", p1.relativize(p13).toString()),
()->assertEquals("../../path1/path1", p23.relativize(p11).toString()),
()->assertEquals("../../path1", p123.relativize(p11).toString()),
()->assertEquals(0, p13.relativize(p13plus).getNameCount())
);
}
@Test
void testPathFiltering() {
final var dir1 = Paths.get("src", "test", "resources", "dir1").toAbsolutePath().normalize();
final var dir2 = Paths.get("src", "test", "resources", "dir2").toAbsolutePath().normalize();
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
var ufs = fsp.newFileSystem((path, base)->!path.startsWith("masktest2.txt"), dir1, dir2);
var t1 = ufs.getPath("/masktest.txt");
var t3 = ufs.getPath("/masktest3.txt");
var t2 = ufs.getPath("/masktest2.txt");
assertTrue(Files.exists(t1));
assertTrue(Files.exists(t3));
assertTrue(Files.notExists(t2));
var sd1 = ufs.getPath("/subdir1");
var sdt1 = sd1.resolve("masktestsd1.txt");
var walk = Set.of(ufs.getRoot(), t1, t3, sd1, sdt1);
assertDoesNotThrow(()-> {
try (var set = Files.walk(ufs.getRoot())) {
var paths = set.collect(Collectors.toSet());
assertEquals(walk, paths);
}
});
}
@Test
void testFilteredDuplicate() {
final var dir1 = Paths.get("src", "test", "resources", "dir1.zip").toAbsolutePath().normalize();
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
var all = fsp.newFileSystem((a,b) -> true, dir1);
var all_expected = Set.of(
all.getPath("/masktest.txt"),
all.getPath("/masktest2.txt"),
all.getPath("/subdir1/masktestsd1.txt")
);
assertDoesNotThrow(() -> {
try (var walk = Files.walk(all.getRoot())) {
var paths = walk.filter(Files::isRegularFile).collect(Collectors.toSet());
assertEquals(all_expected, paths);
}
});
var some = assertDoesNotThrow(() -> fsp.newFileSystem((a,b) -> a.endsWith("/") || a.equals("masktest.txt"), dir1));
var some_expected = Set.of(
some.getPath("/masktest.txt")
);
assertDoesNotThrow(() -> {
try (var walk = Files.walk(some.getRoot())) {
var paths = walk.filter(Files::isRegularFile).collect(Collectors.toSet());
assertEquals(some_expected, paths);
}
});
}
@Test
void testNested() {
final var dir1 = Paths.get("src", "test", "resources", "dir1.zip").toAbsolutePath().normalize();
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
var inner = fsp.newFileSystem((a,b) -> a.endsWith("/") || a.equals("masktest.txt"), dir1);
var outer = fsp.newFileSystem((a, b) -> true, inner.getRoot());
var path = outer.getPath("/masktest.txt");
var expected = Set.of(path);
assertDoesNotThrow(() -> {
try (var walk = Files.walk(outer.getRoot())) {
var paths = walk.filter(Files::isRegularFile).collect(Collectors.toSet());
assertEquals(expected, paths);
}
});
var uri = path.toUri();
var npath = Paths.get(uri);
var input = assertDoesNotThrow(() -> Files.newInputStream(npath));
var data = assertDoesNotThrow(() -> input.readAllBytes());
assertFalse(Files.exists(outer.getPath("definitely", "does", "not", "exist")));
}
@Test
void testFileAttributes() {
final var dir1 = Paths.get("src", "test", "resources", "dir1.zip").toAbsolutePath().normalize();
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
var ufs = fsp.newFileSystem((a,b) -> true, dir1);
var path = (UnionPath) ufs.getPath("subdir1");
var nonExistentPath = (UnionPath) ufs.getPath("non-existent-path");
// Non-union path
assertDoesNotThrow(() -> {
assertNull(fsp.getFileAttributeView(Paths.get("subdir1"), BasicFileAttributeView.class));
});
// Unsupported attribute view
assertDoesNotThrow(() -> {
assertNull(fsp.getFileAttributeView(path, FileAttributeView.class));
});
// Non-existent path w/ supported attribute view
assertThrows(NoSuchFileException.class, () -> {
var nonExistentView = assertDoesNotThrow(() -> {
var view = fsp.getFileAttributeView(nonExistentPath, BasicFileAttributeView.class);
assertNotNull(view);
return view;
});
nonExistentView.readAttributes();
});
// Non-existent path
assertThrows(NoSuchFileException.class, () -> {
ufs.readAttributes(nonExistentPath, BasicFileAttributes.class);
});
// Union path w/ supported attribute view
var validViewAttributes = assertDoesNotThrow(() -> {
var view = fsp.getFileAttributeView(path, BasicFileAttributeView.class);
assertNotNull(view);
return view.readAttributes();
});
// Known existing path
var validAttributes = assertDoesNotThrow(() -> {
var attributes = ufs.readAttributes(path, BasicFileAttributes.class);
assertNotNull(attributes);
return attributes;
});
// Ensure the attributes are the same through both methods
assertEquals(validAttributes, validViewAttributes);
}
@Test
public void testDirectoryVisitorJar() throws Exception {
final var jar1 = Paths.get("sjh-jmh","src", "testjars", "testjar1.jar").toAbsolutePath().normalize();
final var jar2 = Paths.get("sjh-jmh","src", "testjars", "testjar2.jar").toAbsolutePath().normalize();
final var jar3 = Paths.get("sjh-jmh","src", "testjars", "testjar3.jar").toAbsolutePath().normalize();
final var fileSystem = UFSP.newFileSystem(jar1, Map.of("additional", List.of(jar2, jar3)));
var root = fileSystem.getPath("/");
try (var dirStream = Files.newDirectoryStream(root)) {
final List<String> foundFiles = new ArrayList<>();
assertAll(
StreamSupport.stream(dirStream.spliterator(), false)
.peek(path -> foundFiles.add(root.relativize(path).toString()))
.map(p-> ()-> {
if (!Files.exists(p)) {
throw new NoSuchFileException(p.toString());
}
})
);
assertEquals(foundFiles, List.of(
"log4j2.xml",
"module-info.class",
"cpw",
"META-INF",
"LICENSE.txt",
"CREDITS.txt",
"url.png",
"pack.mcmeta",
"mcplogo.png",
"forge_logo.png",
"forge.srg",
"forge.sas",
"forge.exc",
"data",
"coremods",
"assets",
"net"
));
}
}
@Test
public void testDirectoryVisitorDirs() throws Exception {
final var dir1 = Paths.get("src", "test", "resources", "dir1").toAbsolutePath().normalize();
final var dir2 = Paths.get("src", "test", "resources", "dir2").toAbsolutePath().normalize();
final var fileSystem = UFSP.newFileSystem(dir1, Map.of("additional", List.of(dir2)));
var root = fileSystem.getPath("/");
try (var dirStream = Files.newDirectoryStream(root)) {
assertAll(
StreamSupport.stream(dirStream.spliterator(), false).map(p->()->Files.exists(p))
);
}
}
}