Skip to content

Commit

Permalink
Backport 646b666a265c4de961b8ba3f9e4e8c9231be8a6f
Browse files Browse the repository at this point in the history
  • Loading branch information
GoeLin committed Mar 4, 2024
1 parent 39c9e9d commit 8acb04e
Show file tree
Hide file tree
Showing 32 changed files with 391 additions and 236 deletions.
44 changes: 23 additions & 21 deletions test/jdk/java/net/httpclient/AsFileDownloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,6 @@
* questions.
*/

/*
* @test
* @summary Basic test for ofFileDownload
* @bug 8196965 8302475
* @modules java.base/sun.net.www.http
* java.net.http/jdk.internal.net.http.common
* java.net.http/jdk.internal.net.http.frame
* java.net.http/jdk.internal.net.http.hpack
* java.logging
* jdk.httpserver
* @library /test/lib http2/server
* @build Http2TestServer
* @build jdk.test.lib.net.SimpleSSLContext
* @build jdk.test.lib.Platform
* @build jdk.test.lib.util.FileUtils
* @run testng/othervm AsFileDownloadTest
* @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
*/

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
Expand Down Expand Up @@ -81,6 +62,25 @@
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

/*
* @test
* @summary Basic test for ofFileDownload
* @bug 8196965 8302475
* @modules java.base/sun.net.www.http
* java.net.http/jdk.internal.net.http.common
* java.net.http/jdk.internal.net.http.frame
* java.net.http/jdk.internal.net.http.hpack
* java.logging
* jdk.httpserver
* @library /test/lib http2/server
* @build Http2TestServer
* @build jdk.test.lib.net.SimpleSSLContext
* @build jdk.test.lib.Platform
* @build jdk.test.lib.util.FileUtils
* @run testng/othervm AsFileDownloadTest
* @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
*/

public class AsFileDownloadTest {

SSLContext sslContext;
Expand Down Expand Up @@ -267,8 +267,10 @@ void negativeTest(String uriString, String contentDispositionValue)
// -- Infrastructure

static String serverAuthority(HttpServer server) {
return InetAddress.getLoopbackAddress().getHostName() + ":"
+ server.getAddress().getPort();
final String hostIP = InetAddress.getLoopbackAddress().getHostAddress();
// escape for ipv6
final String h = hostIP.contains(":") ? "[" + hostIP + "]" : hostIP;
return h + ":" + server.getAddress().getPort();
}

@BeforeTest
Expand Down
19 changes: 13 additions & 6 deletions test/jdk/java/net/httpclient/AsFileDownloadTest.policy
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ grant codeBase "file:${test.classes}/../../../../java/net/httpclient/http2/serve
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.net.http.hpack";
permission java.lang.RuntimePermission "accessClassInPackage.sun.net.www.http";

permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";
permission java.lang.RuntimePermission "modifyThread";
};

grant codeBase "file:${test.classes}/*" {
permission java.io.FilePermission "${user.dir}${/}asFileDownloadTest.tmp.dir", "read,write";
permission java.io.FilePermission "${user.dir}${/}asFileDownloadTest.tmp.dir/-", "read,write";

permission java.net.URLPermission "http://localhost:*/http1/afdt", "POST";
permission java.net.URLPermission "https://localhost:*/https1/afdt", "POST";
permission java.net.URLPermission "http://localhost:*/http2/afdt", "POST";
permission java.net.URLPermission "https://localhost:*/https2/afdt", "POST";
permission java.net.URLPermission "http://127.0.0.1:*/http1/afdt", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https1/afdt", "POST";
permission java.net.URLPermission "http://127.0.0.1:*/http2/afdt", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https2/afdt", "POST";
// ipv6
permission java.net.URLPermission "http://[::1]:*/http1/afdt", "POST";
permission java.net.URLPermission "https://[::1]:*/https1/afdt", "POST";
permission java.net.URLPermission "http://[::1]:*/http2/afdt", "POST";
permission java.net.URLPermission "https://[::1]:*/https2/afdt", "POST";


// needed to grant permission to the HTTP/2 server
Expand All @@ -58,7 +64,8 @@ grant codeBase "file:${test.classes}/*" {
permission java.util.logging.LoggingPermission "control";

// needed to grant the HTTP servers
permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";

permission java.util.PropertyPermission "*", "read";
permission java.lang.RuntimePermission "modifyThread";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,29 @@ grant codeBase "file:${test.classes}/../../../../../java/net/httpclient/http2/se
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.net.http.hpack";
permission java.lang.RuntimePermission "accessClassInPackage.sun.net.www.http";

permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";
permission java.lang.RuntimePermission "modifyThread";
};

grant codeBase "file:${test.classes}/*" {
permission java.net.URLPermission "http://localhost:*/http1/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https1/echo", "POST";
permission java.net.URLPermission "http://localhost:*/http2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/http1/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https1/echo", "GET";
permission java.net.URLPermission "http://localhost:*/http2/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https2/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http1/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "POST";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/http1/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "GET";
// ipv6
permission java.net.URLPermission "http://[::1]:*/http1/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "POST";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/http1/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "GET";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "GET";

// file permissions
permission java.io.FilePermission "${user.dir}${/}defaultFile.txt", "read,write,delete";
Expand All @@ -69,7 +79,8 @@ grant codeBase "file:${test.classes}/*" {
permission java.util.logging.LoggingPermission "control";

// needed to grant the HTTP servers
permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";

permission java.util.PropertyPermission "*", "read";
permission java.lang.RuntimePermission "modifyThread";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,29 @@ grant codeBase "file:${test.classes}/../../../../../java/net/httpclient/http2/se
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.net.http.hpack";
permission java.lang.RuntimePermission "accessClassInPackage.sun.net.www.http";

permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";
permission java.lang.RuntimePermission "modifyThread";
};

grant codeBase "file:${test.classes}/*" {
permission java.net.URLPermission "http://localhost:*/http1/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https1/echo", "POST";
permission java.net.URLPermission "http://localhost:*/http2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/http1/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https1/echo", "GET";
permission java.net.URLPermission "http://localhost:*/http2/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https2/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http1/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "POST";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/http1/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "GET";
// ipv6
permission java.net.URLPermission "http://[::1]:*/http1/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "POST";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/http1/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "GET";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "GET";

// file permissions
permission java.io.FilePermission "${user.dir}${/}defaultFile.txt", "read,write,delete";
Expand All @@ -74,7 +84,8 @@ grant codeBase "file:${test.classes}/*" {
permission java.util.logging.LoggingPermission "control";

// needed to grant the HTTP servers
permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";

permission java.util.PropertyPermission "*", "read";
permission java.lang.RuntimePermission "modifyThread";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,29 @@ grant codeBase "file:${test.classes}/../../../../../java/net/httpclient/http2/se
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.net.http.hpack";
permission java.lang.RuntimePermission "accessClassInPackage.sun.net.www.http";

permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";
permission java.lang.RuntimePermission "modifyThread";
};

grant codeBase "file:${test.classes}/*" {
permission java.net.URLPermission "http://localhost:*/http1/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https1/echo", "POST";
permission java.net.URLPermission "http://localhost:*/http2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/http1/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https1/echo", "GET";
permission java.net.URLPermission "http://localhost:*/http2/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https2/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http1/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "POST";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/http1/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "GET";
// ipv6
permission java.net.URLPermission "http://[::1]:*/http1/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "POST";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/http1/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "GET";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "GET";

// file permissions
permission java.io.FilePermission "${user.dir}${/}defaultFile.txt", "read,write,delete";
Expand All @@ -77,7 +87,8 @@ grant codeBase "file:${test.classes}/*" {
permission java.util.logging.LoggingPermission "control";

// needed to grant the HTTP servers
permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";

permission java.util.PropertyPermission "*", "read";
permission java.lang.RuntimePermission "modifyThread";
Expand Down
31 changes: 21 additions & 10 deletions test/jdk/java/net/httpclient/FilePublisher/FilePublisherTest.policy
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,29 @@ grant codeBase "file:${test.classes}/../../../../../java/net/httpclient/http2/se
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.net.http.hpack";
permission java.lang.RuntimePermission "accessClassInPackage.sun.net.www.http";

permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";
permission java.lang.RuntimePermission "modifyThread";
};

grant codeBase "file:${test.classes}/*" {
permission java.net.URLPermission "http://localhost:*/http1/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https1/echo", "POST";
permission java.net.URLPermission "http://localhost:*/http2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/https2/echo", "POST";
permission java.net.URLPermission "https://localhost:*/http1/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https1/echo", "GET";
permission java.net.URLPermission "http://localhost:*/http2/echo", "GET";
permission java.net.URLPermission "https://localhost:*/https2/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http1/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "POST";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "POST";
permission java.net.URLPermission "https://127.0.0.1:*/http1/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https1/echo", "GET";
permission java.net.URLPermission "http://127.0.0.1:*/http2/echo", "GET";
permission java.net.URLPermission "https://127.0.0.1:*/https2/echo", "GET";
// ipv6
permission java.net.URLPermission "http://[::1]:*/http1/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "POST";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "POST";
permission java.net.URLPermission "https://[::1]:*/http1/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https1/echo", "GET";
permission java.net.URLPermission "http://[::1]:*/http2/echo", "GET";
permission java.net.URLPermission "https://[::1]:*/https2/echo", "GET";

// file permissions
permission java.io.FilePermission "${user.dir}${/}defaultFile.txt", "read,write,delete";
Expand All @@ -62,7 +72,8 @@ grant codeBase "file:${test.classes}/*" {
permission java.util.logging.LoggingPermission "control";

// needed to grant the HTTP servers
permission java.net.SocketPermission "localhost:*", "accept,resolve";
permission java.net.SocketPermission "127.0.0.1:*", "accept,resolve";
permission java.net.SocketPermission "[::1]:*", "accept,resolve";

permission java.util.PropertyPermission "*", "read";
permission java.lang.RuntimePermission "modifyThread";
Expand Down
11 changes: 9 additions & 2 deletions test/jdk/java/net/httpclient/HttpServerAdapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,15 @@ static void enableLogging() {
public abstract Version getVersion();

public String serverAuthority() {
return InetAddress.getLoopbackAddress().getHostName() + ":"
+ getAddress().getPort();
InetSocketAddress address = getAddress();
String hostString = address.getHostString();
hostString = address.getAddress().isLoopbackAddress() || hostString.equals("localhost")
? address.getAddress().getHostAddress() // use the raw IP address, if loopback
: hostString; // use whatever host string was used to construct the address
hostString = hostString.contains(":")
? "[" + hostString + "]"
: hostString;
return hostString + ":" + address.getPort();
}

public static HttpTestServer of(HttpServer server) {
Expand Down
13 changes: 10 additions & 3 deletions test/jdk/java/net/httpclient/LightWeightHttpServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -119,14 +119,21 @@ public static void initServer() throws IOException {
System.out.println("HTTP server port = " + port);
httpsport = httpsServer.getAddress().getPort();
System.out.println("HTTPS server port = " + httpsport);
httproot = "http://localhost:" + port + "/";
httpsroot = "https://localhost:" + httpsport + "/";
httproot = "http://" + makeServerAuthority(httpServer.getAddress()) + "/";
httpsroot = "https://" + makeServerAuthority(httpsServer.getAddress()) + "/";

proxy = new ProxyServer(0, false);
proxyPort = proxy.getPort();
System.out.println("Proxy port = " + proxyPort);
}

private static String makeServerAuthority(final InetSocketAddress addr) {
final String hostIP = addr.getAddress().getHostAddress();
// escape for ipv6
final String h = hostIP.contains(":") ? "[" + hostIP + "]" : hostIP;
return h + ":" + addr.getPort();
}

public static void stop() throws IOException {
if (httpServer != null) {
httpServer.stop(0);
Expand Down
Loading

0 comments on commit 8acb04e

Please sign in to comment.