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
|
package com.yahoo.vespa.archive;
import com.yahoo.vespa.archive.ArchiveStreamReader.Options;
import com.yahoo.yolean.Exceptions;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author mpolden
*/
class ArchiveStreamReaderTest {
@Test
void reading() {
Map<String, String> zipFiles = Map.of("foo", "contents of foo",
"bar", "contents of bar",
"baz", "0".repeat(2049));
Map<String, String> zipContents = new HashMap<>(zipFiles);
zipContents.put("dir/", ""); // Directories are always ignored
Map<String, String> extracted = readAll(zip(zipContents), Options.standard());
assertEquals(zipFiles, extracted);
}
@Test
void entry_size_limit() {
Map<String, String> entries = Map.of("foo.xml", "foobar");
Options options = Options.standard().pathPredicate("foo.xml"::equals).maxEntrySize(1);
try {
readAll(zip(entries), options);
fail("Expected exception");
} catch (IllegalArgumentException ignored) {}
entries = Map.of("foo.xml", "foobar",
"foo.jar", "0".repeat(100) // File not extracted and thus not subject to size limit
);
Map<String, String> extracted = readAll(zip(entries), options.maxEntrySize(10));
assertEquals(Map.of("foo.xml", "foobar"), extracted);
}
@Test
void size_limit() {
Map<String, String> entries = Map.of("foo.xml", "foo", "bar.xml", "bar");
try {
readAll(zip(entries), Options.standard().maxSize(4));
fail("Expected exception");
} catch (IllegalArgumentException ignored) {}
}
@Test
void entry_limit() {
Map<String, String> entries = Map.of("foo.xml", "foo", "bar.xml", "bar");
try {
readAll(zip(entries), Options.standard().maxEntries(1));
fail("Expected exception");
} catch (IllegalArgumentException ignored) {}
}
@Test
void paths() {
Map<String, Boolean> tests = Map.of(
"../../services.xml", true,
"/../.././services.xml", true,
"./application/././services.xml", true,
"application//services.xml", true,
"artifacts/", false, // empty dir
"services..xml", false,
"application/services.xml", false,
"components/foo-bar-deploy.jar", false,
"services.xml", false
);
Options options = Options.standard().maxEntrySize(1024);
tests.forEach((name, expectException) -> {
try {
readAll(zip(Map.of(name, "foo")), options.pathPredicate(name::equals));
assertFalse(expectException, "Expected exception for '" + name + "'");
} catch (IllegalArgumentException ignored) {
assertTrue(expectException, "Unexpected exception for '" + name + "'");
}
});
}
private static Map<String, String> readAll(InputStream inputStream, Options options) {
ArchiveStreamReader reader = ArchiveStreamReader.ofZip(inputStream, options);
ArchiveStreamReader.ArchiveFile file;
Map<String, String> entries = new HashMap<>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((file = reader.readNextTo(baos)) != null) {
entries.put(file.path().toString(), baos.toString(StandardCharsets.UTF_8));
baos.reset();
}
return entries;
}
private static InputStream zip(Map<String, String> entries) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipArchiveOutputStream archiveOutputStream = null;
try {
archiveOutputStream = new ZipArchiveOutputStream(baos);
for (var kv : entries.entrySet()) {
String entryName = kv.getKey();
String contents = kv.getValue();
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
archiveOutputStream.putArchiveEntry(entry);
archiveOutputStream.write(contents.getBytes(StandardCharsets.UTF_8));
archiveOutputStream.closeArchiveEntry();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (archiveOutputStream != null) Exceptions.uncheck(archiveOutputStream::close);
}
return new ByteArrayInputStream(baos.toByteArray());
}
}
|