49765
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @summary Basic test for ofFileDownload
|
|
27 |
* @bug 8196965
|
|
28 |
* @modules java.base/sun.net.www.http
|
|
29 |
* java.net.http/jdk.internal.net.http.common
|
|
30 |
* java.net.http/jdk.internal.net.http.frame
|
|
31 |
* java.net.http/jdk.internal.net.http.hpack
|
|
32 |
* java.logging
|
|
33 |
* jdk.httpserver
|
|
34 |
* @library /lib/testlibrary /test/lib http2/server
|
|
35 |
* @build Http2TestServer
|
|
36 |
* @build jdk.testlibrary.SimpleSSLContext
|
|
37 |
* @build jdk.test.lib.Platform
|
|
38 |
* @build jdk.test.lib.util.FileUtils
|
|
39 |
* @run testng/othervm AsFileDownloadTest
|
|
40 |
* @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
|
|
41 |
*/
|
|
42 |
|
|
43 |
import com.sun.net.httpserver.HttpExchange;
|
|
44 |
import com.sun.net.httpserver.HttpHandler;
|
|
45 |
import com.sun.net.httpserver.HttpServer;
|
|
46 |
import com.sun.net.httpserver.HttpsConfigurator;
|
|
47 |
import com.sun.net.httpserver.HttpsServer;
|
|
48 |
import java.io.IOException;
|
|
49 |
import java.io.InputStream;
|
|
50 |
import java.io.OutputStream;
|
|
51 |
import java.io.UncheckedIOException;
|
|
52 |
import java.net.InetAddress;
|
|
53 |
import java.net.InetSocketAddress;
|
|
54 |
import java.net.URI;
|
|
55 |
import java.net.http.HttpClient;
|
|
56 |
import java.net.http.HttpRequest;
|
|
57 |
import java.net.http.HttpRequest.BodyPublishers;
|
|
58 |
import java.net.http.HttpResponse;
|
|
59 |
import java.net.http.HttpResponse.BodyHandler;
|
|
60 |
import java.nio.file.Files;
|
|
61 |
import java.nio.file.Path;
|
|
62 |
import java.nio.file.Paths;
|
|
63 |
import java.util.ArrayList;
|
|
64 |
import java.util.Arrays;
|
|
65 |
import java.util.List;
|
|
66 |
import java.util.logging.ConsoleHandler;
|
|
67 |
import java.util.logging.Level;
|
|
68 |
import java.util.logging.Logger;
|
|
69 |
import javax.net.ssl.SSLContext;
|
|
70 |
import jdk.testlibrary.SimpleSSLContext;
|
|
71 |
import jdk.test.lib.util.FileUtils;
|
|
72 |
import org.testng.annotations.AfterTest;
|
|
73 |
import org.testng.annotations.BeforeTest;
|
|
74 |
import org.testng.annotations.DataProvider;
|
|
75 |
import org.testng.annotations.Test;
|
|
76 |
import static java.lang.System.out;
|
|
77 |
import static java.net.http.HttpResponse.BodyHandlers.ofFileDownload;
|
|
78 |
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
79 |
import static java.nio.file.StandardOpenOption.*;
|
|
80 |
import static org.testng.Assert.assertEquals;
|
|
81 |
import static org.testng.Assert.assertTrue;
|
|
82 |
import static org.testng.Assert.fail;
|
|
83 |
|
|
84 |
public class AsFileDownloadTest {
|
|
85 |
|
|
86 |
SSLContext sslContext;
|
|
87 |
HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ]
|
|
88 |
HttpsServer httpsTestServer; // HTTPS/1.1
|
|
89 |
Http2TestServer http2TestServer; // HTTP/2 ( h2c )
|
|
90 |
Http2TestServer https2TestServer; // HTTP/2 ( h2 )
|
|
91 |
String httpURI;
|
|
92 |
String httpsURI;
|
|
93 |
String http2URI;
|
|
94 |
String https2URI;
|
|
95 |
|
|
96 |
Path tempDir;
|
|
97 |
|
|
98 |
static final String[][] contentDispositionValues = new String[][] {
|
|
99 |
// URI query Content-Type header value Expected filename
|
|
100 |
{ "001", "Attachment; filename=example001.html", "example001.html" },
|
|
101 |
{ "002", "attachment; filename=example002.html", "example002.html" },
|
|
102 |
{ "003", "ATTACHMENT; filename=example003.html", "example003.html" },
|
|
103 |
{ "004", "attAChment; filename=example004.html", "example004.html" },
|
|
104 |
{ "005", "attachmeNt; filename=example005.html", "example005.html" },
|
|
105 |
|
|
106 |
{ "006", "attachment; Filename=example006.html", "example006.html" },
|
|
107 |
{ "007", "attachment; FILENAME=example007.html", "example007.html" },
|
|
108 |
{ "008", "attachment; fileName=example008.html", "example008.html" },
|
|
109 |
{ "009", "attachment; fIlEnAMe=example009.html", "example009.html" },
|
|
110 |
|
|
111 |
{ "010", "attachment; filename=Example010.html", "Example010.html" },
|
|
112 |
{ "011", "attachment; filename=EXAMPLE011.html", "EXAMPLE011.html" },
|
|
113 |
{ "012", "attachment; filename=eXample012.html", "eXample012.html" },
|
|
114 |
{ "013", "attachment; filename=example013.HTML", "example013.HTML" },
|
|
115 |
{ "014", "attachment; filename =eXaMpLe014.HtMl", "eXaMpLe014.HtMl"},
|
|
116 |
|
|
117 |
{ "015", "attachment; filename=a", "a" },
|
|
118 |
{ "016", "attachment; filename= b", "b" },
|
|
119 |
{ "017", "attachment; filename= c", "c" },
|
|
120 |
{ "018", "attachment; filename= d", "d" },
|
|
121 |
{ "019", "attachment; filename=e ; filename*=utf-8''eee.txt", "e"},
|
|
122 |
{ "020", "attachment; filename*=utf-8''fff.txt; filename=f", "f"},
|
|
123 |
{ "021", "attachment; filename=g", "g" },
|
|
124 |
{ "022", "attachment; filename= h", "h" },
|
|
125 |
|
|
126 |
{ "023", "attachment; filename=\"space name\"", "space name" },
|
|
127 |
{ "024", "attachment; filename=me.txt; filename*=utf-8''you.txt", "me.txt" },
|
|
128 |
{ "025", "attachment; filename=\"m y.txt\"; filename*=utf-8''you.txt", "m y.txt" },
|
|
129 |
|
|
130 |
{ "030", "attachment; filename=foo/file1.txt", "file1.txt" },
|
|
131 |
{ "031", "attachment; filename=foo/bar/file2.txt", "file2.txt" },
|
|
132 |
{ "032", "attachment; filename=baz\\file3.txt", "file3.txt" },
|
|
133 |
{ "033", "attachment; filename=baz\\bar\\file4.txt", "file4.txt" },
|
|
134 |
{ "034", "attachment; filename=x/y\\file5.txt", "file5.txt" },
|
|
135 |
{ "035", "attachment; filename=x/y\\file6.txt", "file6.txt" },
|
|
136 |
{ "036", "attachment; filename=x/y\\z/file7.txt", "file7.txt" },
|
|
137 |
{ "037", "attachment; filename=x/y\\z/\\x/file8.txt", "file8.txt" },
|
|
138 |
{ "038", "attachment; filename=/root/file9.txt", "file9.txt" },
|
|
139 |
{ "039", "attachment; filename=../file10.txt", "file10.txt" },
|
|
140 |
{ "040", "attachment; filename=..\\file11.txt", "file11.txt" },
|
|
141 |
{ "041", "attachment; filename=foo/../../file12.txt", "file12.txt" },
|
|
142 |
};
|
|
143 |
|
|
144 |
@DataProvider(name = "positive")
|
|
145 |
public Object[][] positive() {
|
|
146 |
List<Object[]> list = new ArrayList<>();
|
|
147 |
|
|
148 |
Arrays.asList(contentDispositionValues).stream()
|
|
149 |
.map(e -> new Object[] {httpURI + "?" + e[0], e[1], e[2]})
|
|
150 |
.forEach(list::add);
|
|
151 |
Arrays.asList(contentDispositionValues).stream()
|
|
152 |
.map(e -> new Object[] {httpsURI + "?" + e[0], e[1], e[2]})
|
|
153 |
.forEach(list::add);
|
|
154 |
Arrays.asList(contentDispositionValues).stream()
|
|
155 |
.map(e -> new Object[] {http2URI + "?" + e[0], e[1], e[2]})
|
|
156 |
.forEach(list::add);
|
|
157 |
Arrays.asList(contentDispositionValues).stream()
|
|
158 |
.map(e -> new Object[] {https2URI + "?" + e[0], e[1], e[2]})
|
|
159 |
.forEach(list::add);
|
|
160 |
|
|
161 |
return list.stream().toArray(Object[][]::new);
|
|
162 |
}
|
|
163 |
|
|
164 |
@Test(dataProvider = "positive")
|
|
165 |
void test(String uriString, String contentDispositionValue, String expectedFilename)
|
|
166 |
throws Exception
|
|
167 |
{
|
|
168 |
out.printf("test(%s, %s, %s): starting", uriString, contentDispositionValue, expectedFilename);
|
|
169 |
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
|
|
170 |
|
|
171 |
URI uri = URI.create(uriString);
|
|
172 |
HttpRequest request = HttpRequest.newBuilder(uri)
|
|
173 |
.POST(BodyPublishers.ofString("May the luck of the Irish be with you!"))
|
|
174 |
.build();
|
|
175 |
|
|
176 |
BodyHandler bh = ofFileDownload(tempDir.resolve(uri.getPath().substring(1)),
|
|
177 |
CREATE, TRUNCATE_EXISTING, WRITE);
|
|
178 |
HttpResponse<Path> response = client.send(request, bh);
|
|
179 |
|
|
180 |
out.println("Got response: " + response);
|
|
181 |
out.println("Got body Path: " + response.body());
|
|
182 |
String fileContents = new String(Files.readAllBytes(response.body()), UTF_8);
|
|
183 |
out.println("Got body: " + fileContents);
|
|
184 |
|
|
185 |
assertEquals(response.statusCode(),200);
|
|
186 |
assertEquals(response.body().getFileName().toString(), expectedFilename);
|
|
187 |
assertTrue(response.headers().firstValue("Content-Disposition").isPresent());
|
|
188 |
assertEquals(response.headers().firstValue("Content-Disposition").get(),
|
|
189 |
contentDispositionValue);
|
|
190 |
assertEquals(fileContents, "May the luck of the Irish be with you!");
|
|
191 |
}
|
|
192 |
|
|
193 |
// --- Negative
|
|
194 |
|
|
195 |
static final String[][] contentDispositionBADValues = new String[][] {
|
|
196 |
// URI query Content-Type header value
|
|
197 |
{ "100", "" }, // empty
|
|
198 |
{ "101", "filename=example.html" }, // no attachment
|
|
199 |
{ "102", "attachment; filename=space name" }, // unquoted with space
|
|
200 |
{ "103", "attachment; filename=" }, // empty filename param
|
|
201 |
{ "104", "attachment; filename=\"" }, // single quote
|
|
202 |
{ "105", "attachment; filename=\"\"" }, // empty quoted
|
|
203 |
{ "106", "attachment; filename=." }, // dot
|
|
204 |
{ "107", "attachment; filename=.." }, // dot dot
|
|
205 |
{ "108", "attachment; filename=\".." }, // badly quoted dot dot
|
|
206 |
{ "109", "attachment; filename=\"..\"" }, // quoted dot dot
|
|
207 |
{ "110", "attachment; filename=\"bad" }, // badly quoted
|
|
208 |
{ "111", "attachment; filename=\"bad;" }, // badly quoted with ';'
|
|
209 |
{ "112", "attachment; filename=\"bad ;" }, // badly quoted with ' ;'
|
|
210 |
{ "113", "attachment; filename*=utf-8''xx.txt "}, // no "filename" param
|
|
211 |
|
|
212 |
{ "120", "<<NOT_PRESENT>>" }, // header not present
|
|
213 |
|
|
214 |
};
|
|
215 |
|
|
216 |
@DataProvider(name = "negative")
|
|
217 |
public Object[][] negative() {
|
|
218 |
List<Object[]> list = new ArrayList<>();
|
|
219 |
|
|
220 |
Arrays.asList(contentDispositionBADValues).stream()
|
|
221 |
.map(e -> new Object[] {httpURI + "?" + e[0], e[1]})
|
|
222 |
.forEach(list::add);
|
|
223 |
Arrays.asList(contentDispositionBADValues).stream()
|
|
224 |
.map(e -> new Object[] {httpsURI + "?" + e[0], e[1]})
|
|
225 |
.forEach(list::add);
|
|
226 |
Arrays.asList(contentDispositionBADValues).stream()
|
|
227 |
.map(e -> new Object[] {http2URI + "?" + e[0], e[1]})
|
|
228 |
.forEach(list::add);
|
|
229 |
Arrays.asList(contentDispositionBADValues).stream()
|
|
230 |
.map(e -> new Object[] {https2URI + "?" + e[0], e[1]})
|
|
231 |
.forEach(list::add);
|
|
232 |
|
|
233 |
return list.stream().toArray(Object[][]::new);
|
|
234 |
}
|
|
235 |
|
|
236 |
@Test(dataProvider = "negative")
|
|
237 |
void negativeTest(String uriString, String contentDispositionValue)
|
|
238 |
throws Exception
|
|
239 |
{
|
|
240 |
out.printf("negativeTest(%s, %s): starting", uriString, contentDispositionValue);
|
|
241 |
HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
|
|
242 |
|
|
243 |
URI uri = URI.create(uriString);
|
|
244 |
HttpRequest request = HttpRequest.newBuilder(uri)
|
|
245 |
.POST(BodyPublishers.ofString("Does not matter"))
|
|
246 |
.build();
|
|
247 |
|
|
248 |
BodyHandler bh = ofFileDownload(tempDir, CREATE, TRUNCATE_EXISTING, WRITE);
|
|
249 |
try {
|
|
250 |
HttpResponse<Path> response = client.send(request, bh);
|
|
251 |
fail("UNEXPECTED response: " + response + ", path:" + response.body());
|
|
252 |
} catch (UncheckedIOException | IOException ioe) {
|
|
253 |
System.out.println("Caught expected: " + ioe);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
// -- Infrastructure
|
|
258 |
|
|
259 |
static String serverAuthority(HttpServer server) {
|
|
260 |
return InetAddress.getLoopbackAddress().getHostName() + ":"
|
|
261 |
+ server.getAddress().getPort();
|
|
262 |
}
|
|
263 |
|
|
264 |
@BeforeTest
|
|
265 |
public void setup() throws Exception {
|
|
266 |
tempDir = Paths.get("asFileDownloadTest.tmp.dir");
|
|
267 |
if (Files.exists(tempDir))
|
|
268 |
throw new AssertionError("Unexpected test work dir existence: " + tempDir.toString());
|
|
269 |
|
|
270 |
Files.createDirectory(tempDir);
|
|
271 |
// Unique dirs per test run, based on the URI path
|
|
272 |
Files.createDirectories(tempDir.resolve("http1/afdt/"));
|
|
273 |
Files.createDirectories(tempDir.resolve("https1/afdt/"));
|
|
274 |
Files.createDirectories(tempDir.resolve("http2/afdt/"));
|
|
275 |
Files.createDirectories(tempDir.resolve("https2/afdt/"));
|
|
276 |
|
|
277 |
// HTTP/1.1 server logging in case of security exceptions ( uncomment if needed )
|
|
278 |
//Logger logger = Logger.getLogger("com.sun.net.httpserver");
|
|
279 |
//ConsoleHandler ch = new ConsoleHandler();
|
|
280 |
//logger.setLevel(Level.ALL);
|
|
281 |
//ch.setLevel(Level.ALL);
|
|
282 |
//logger.addHandler(ch);
|
|
283 |
|
|
284 |
sslContext = new SimpleSSLContext().get();
|
|
285 |
if (sslContext == null)
|
|
286 |
throw new AssertionError("Unexpected null sslContext");
|
|
287 |
|
|
288 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
|
|
289 |
httpTestServer = HttpServer.create(sa, 0);
|
|
290 |
httpTestServer.createContext("/http1/afdt", new Http1FileDispoHandler());
|
|
291 |
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1/afdt";
|
|
292 |
|
|
293 |
httpsTestServer = HttpsServer.create(sa, 0);
|
|
294 |
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
|
295 |
httpsTestServer.createContext("/https1/afdt", new Http1FileDispoHandler());
|
|
296 |
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1/afdt";
|
|
297 |
|
|
298 |
http2TestServer = new Http2TestServer("localhost", false, 0);
|
|
299 |
http2TestServer.addHandler(new Http2FileDispoHandler(), "/http2/afdt");
|
|
300 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/afdt";
|
|
301 |
|
|
302 |
https2TestServer = new Http2TestServer("localhost", true, 0);
|
|
303 |
https2TestServer.addHandler(new Http2FileDispoHandler(), "/https2/afdt");
|
|
304 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/afdt";
|
|
305 |
|
|
306 |
httpTestServer.start();
|
|
307 |
httpsTestServer.start();
|
|
308 |
http2TestServer.start();
|
|
309 |
https2TestServer.start();
|
|
310 |
}
|
|
311 |
|
|
312 |
@AfterTest
|
|
313 |
public void teardown() throws Exception {
|
|
314 |
httpTestServer.stop(0);
|
|
315 |
httpsTestServer.stop(0);
|
|
316 |
http2TestServer.stop();
|
|
317 |
https2TestServer.stop();
|
|
318 |
|
|
319 |
if (System.getSecurityManager() == null && Files.exists(tempDir)) {
|
|
320 |
// clean up before next run with security manager
|
|
321 |
FileUtils.deleteFileTreeWithRetry(tempDir);
|
|
322 |
}
|
|
323 |
}
|
|
324 |
|
|
325 |
static String contentDispositionValueFromURI(URI uri) {
|
|
326 |
String queryIndex = uri.getQuery();
|
|
327 |
String[][] values;
|
|
328 |
if (queryIndex.startsWith("0")) // positive tests start with '0'
|
|
329 |
values = contentDispositionValues;
|
|
330 |
else if (queryIndex.startsWith("1")) // negative tests start with '1'
|
|
331 |
values = contentDispositionBADValues;
|
|
332 |
else
|
|
333 |
throw new AssertionError("SERVER: UNEXPECTED query:" + queryIndex);
|
|
334 |
|
|
335 |
return Arrays.asList(values).stream()
|
|
336 |
.filter(e -> e[0].equals(queryIndex))
|
|
337 |
.map(e -> e[1])
|
|
338 |
.findFirst()
|
|
339 |
.orElseThrow();
|
|
340 |
}
|
|
341 |
|
|
342 |
static class Http1FileDispoHandler implements HttpHandler {
|
|
343 |
@Override
|
|
344 |
public void handle(HttpExchange t) throws IOException {
|
|
345 |
try (InputStream is = t.getRequestBody();
|
|
346 |
OutputStream os = t.getResponseBody()) {
|
|
347 |
byte[] bytes = is.readAllBytes();
|
|
348 |
|
|
349 |
String value = contentDispositionValueFromURI(t.getRequestURI());
|
|
350 |
if (!value.equals("<<NOT_PRESENT>>"))
|
|
351 |
t.getResponseHeaders().set("Content-Disposition", value);
|
|
352 |
|
|
353 |
t.sendResponseHeaders(200, bytes.length);
|
|
354 |
os.write(bytes);
|
|
355 |
}
|
|
356 |
}
|
|
357 |
}
|
|
358 |
|
|
359 |
static class Http2FileDispoHandler implements Http2Handler {
|
|
360 |
@Override
|
|
361 |
public void handle(Http2TestExchange t) throws IOException {
|
|
362 |
try (InputStream is = t.getRequestBody();
|
|
363 |
OutputStream os = t.getResponseBody()) {
|
|
364 |
byte[] bytes = is.readAllBytes();
|
|
365 |
|
|
366 |
String value = contentDispositionValueFromURI(t.getRequestURI());
|
|
367 |
if (!value.equals("<<NOT_PRESENT>>"))
|
|
368 |
t.getResponseHeaders().addHeader("Content-Disposition", value);
|
|
369 |
|
|
370 |
t.sendResponseHeaders(200, bytes.length);
|
|
371 |
os.write(bytes);
|
|
372 |
}
|
|
373 |
}
|
|
374 |
}
|
|
375 |
}
|