8224603: Replace wildcard address with loopback or local host in tests - part 11

Fixes a batch of tests that were observed failing intermittently.

Reviewed-by: chegar, vtewari
This commit is contained in:
Daniel Fuchs 2019-05-24 15:34:14 +01:00
parent 241c32ca51
commit ee040e4be2
13 changed files with 524 additions and 433 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,11 +27,14 @@
* @library /test/lib * @library /test/lib
* @build jdk.test.lib.net.SimpleSSLContext * @build jdk.test.lib.net.SimpleSSLContext
* @run main/othervm -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest * @run main/othervm -Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest
* @run main/othervm -Djava.net.preferIPv6Addresses=true
-Dsun.net.httpserver.selCacheTimeout=2 SelCacheTest
* @summary Light weight HTTP server * @summary Light weight HTTP server
*/ */
import com.sun.net.httpserver.*; import com.sun.net.httpserver.*;
import jdk.test.lib.net.SimpleSSLContext; import jdk.test.lib.net.SimpleSSLContext;
import jdk.test.lib.net.URIBuilder;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -53,10 +56,11 @@ public class SelCacheTest extends Test {
HttpServer s1 = null; HttpServer s1 = null;
HttpsServer s2 = null; HttpsServer s2 = null;
ExecutorService executor=null; ExecutorService executor=null;
InetAddress loopback = InetAddress.getLoopbackAddress();
try { try {
String root = System.getProperty("test.src")+ "/docs"; String root = System.getProperty("test.src")+ "/docs";
System.out.print("Test1: "); System.out.print("Test1: ");
InetSocketAddress addr = new InetSocketAddress (0); InetSocketAddress addr = new InetSocketAddress(loopback, 0);
s1 = HttpServer.create(addr, 0); s1 = HttpServer.create(addr, 0);
if (s1 instanceof HttpsServer) { if (s1 instanceof HttpsServer) {
throw new RuntimeException("should not be httpsserver"); throw new RuntimeException("should not be httpsserver");
@ -75,14 +79,14 @@ public class SelCacheTest extends Test {
int port = s1.getAddress().getPort(); int port = s1.getAddress().getPort();
int httpsport = s2.getAddress().getPort(); int httpsport = s2.getAddress().getPort();
test (true, "http", root+"/test1", port, "smallfile.txt", 23); test(true, "http", root+"/test1", loopback, port, "smallfile.txt", 23);
test (true, "http", root+"/test1", port, "largefile.txt", 2730088); test(true, "http", root+"/test1", loopback, port, "largefile.txt", 2730088);
test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23); test(true, "https", root+"/test1", loopback, httpsport, "smallfile.txt", 23);
test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088); test(true, "https", root+"/test1", loopback, httpsport, "largefile.txt", 2730088);
test (false, "http", root+"/test1", port, "smallfile.txt", 23); test(false, "http", root+"/test1", loopback, port, "smallfile.txt", 23);
test (false, "http", root+"/test1", port, "largefile.txt", 2730088); test(false, "http", root+"/test1", loopback, port, "largefile.txt", 2730088);
test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23); test(false, "https", root+"/test1", loopback, httpsport, "smallfile.txt", 23);
test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088); test(false, "https", root+"/test1", loopback, httpsport, "largefile.txt", 2730088);
System.out.println("OK"); System.out.println("OK");
} finally { } finally {
delay(); delay();
@ -92,10 +96,16 @@ public class SelCacheTest extends Test {
} }
} }
static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception { static void test(boolean fixedLen, String protocol, String root,
InetAddress address, int port, String f, int size) throws Exception {
Thread.sleep(2000); Thread.sleep(2000);
URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f); URL url = URIBuilder.newBuilder()
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); .scheme(protocol)
.host(address)
.port(port)
.path("/test1/"+f)
.toURL();
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
if (urlc instanceof HttpsURLConnection) { if (urlc instanceof HttpsURLConnection) {
HttpsURLConnection urlcs = (HttpsURLConnection) urlc; HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
urlcs.setHostnameVerifier(new HostnameVerifier() { urlcs.setHostnameVerifier(new HostnameVerifier() {

View File

@ -26,6 +26,7 @@
* @bug 6725892 * @bug 6725892
* @library /test/lib * @library /test/lib
* @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test * @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test
* @run main/othervm -Djava.net.preferIPv6Addresses=true -Dsun.net.httpserver.maxReqTime=2 Test
* @summary * @summary
*/ */
@ -69,8 +70,9 @@ public class Test {
ExecutorService exec = Executors.newCachedThreadPool(); ExecutorService exec = Executors.newCachedThreadPool();
InetAddress loopback = InetAddress.getLoopbackAddress();
try { try {
InetSocketAddress addr = new InetSocketAddress (0); InetSocketAddress addr = new InetSocketAddress(loopback, 0);
s1 = HttpServer.create(addr, 100); s1 = HttpServer.create(addr, 100);
HttpHandler h = new Handler(); HttpHandler h = new Handler();
HttpContext c1 = s1.createContext("/", h); HttpContext c1 = s1.createContext("/", h);
@ -126,7 +128,7 @@ public class Test {
// send request and don't read response. Check server closes connection // send request and don't read response. Check server closes connection
static void test2() throws IOException { static void test2() throws IOException {
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
urlc.setReadTimeout(20 * 1000); urlc.setReadTimeout(20 * 1000);
InputStream is = urlc.getInputStream(); InputStream is = urlc.getInputStream();
// we won't read response and check if it times out // we won't read response and check if it times out
@ -183,7 +185,7 @@ public class Test {
InputStream is = null; InputStream is = null;
try { try {
urlc = (HttpURLConnection) url.openConnection(); urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
urlc.setReadTimeout(20 * 1000); urlc.setReadTimeout(20 * 1000);
urlc.setDoOutput(true); urlc.setDoOutput(true);
} catch (IOException e) { } catch (IOException e) {

View File

@ -27,7 +27,7 @@
* @modules java.base/sun.net.www * @modules java.base/sun.net.www
* @library ../../../sun/net/www/httptest/ * @library ../../../sun/net/www/httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main B4722333 * @run main/othervm B4722333
* @summary JRE Proxy Authentication Not Working with ISA2000 * @summary JRE Proxy Authentication Not Working with ISA2000
*/ */
@ -154,8 +154,7 @@ public class B4722333 implements HttpCallback {
int count = 0; int count = 0;
public PasswordAuthentication getPasswordAuthentication () public PasswordAuthentication getPasswordAuthentication() {
{
System.out.println("Auth called"); System.out.println("Auth called");
String scheme = getRequestingScheme(); String scheme = getRequestingScheme();
System.out.println("getRequestingScheme() returns " + scheme); System.out.println("getRequestingScheme() returns " + scheme);
@ -173,7 +172,7 @@ public class B4722333 implements HttpCallback {
} }
public int getCount () { public int getCount () {
return (count); return count;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,6 +26,8 @@
* @bug 4094894 * @bug 4094894
* @summary On W95/W98 it's not possible to send a datagram >12k * @summary On W95/W98 it's not possible to send a datagram >12k
* via the loopback address. * via the loopback address.
* @run main Send12k
* @run main/othervm -Djava.net.preferIPv6Addresses=true Send12k
*/ */
import java.net.*; import java.net.*;
@ -43,21 +45,24 @@ public class Send12k {
} else { } else {
SEND_SIZE = 16 * 1024; SEND_SIZE = 16 * 1024;
} }
InetAddress localHost = InetAddress.getLocalHost();
DatagramSocket s1 = new DatagramSocket(); DatagramSocket s1 = new DatagramSocket();
DatagramSocket s2 = new DatagramSocket(); DatagramSocket s2 = new DatagramSocket(0, localHost);
byte b1[] = new byte[ SEND_SIZE ]; byte b1[] = new byte[ SEND_SIZE ];
DatagramPacket p1 = new DatagramPacket(b1, 0, b1.length, DatagramPacket p1 = new DatagramPacket(b1, 0, b1.length,
InetAddress.getLocalHost(), localHost,
s2.getLocalPort()); s2.getLocalPort());
boolean sendOkay = true; boolean sendOkay = true;
try { try {
System.out.println("Sending to: [" + localHost + "]:" + s2.getLocalPort());
s1.send(p1); s1.send(p1);
} catch (IOException e) { } catch (IOException e) {
/* /*
* Prior to merlin a send of > 12k to loopback address * Prior to merlin a send of > 12k to loopback address
* would fail silently. * would fail silently.
*/ */
System.out.println("Sending failed: " + e);
sendOkay = false; sendOkay = false;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -51,7 +51,7 @@ public class CloseAvailable {
static void testClose() throws IOException { static void testClose() throws IOException {
boolean error = true; boolean error = true;
InetAddress addr = InetAddress.getLocalHost(); InetAddress addr = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket(0); ServerSocket ss = new ServerSocket(0, 0, addr);
int port = ss.getLocalPort(); int port = ss.getLocalPort();
Thread t = new Thread(new Thread("Close-Available-1") { Thread t = new Thread(new Thread("Close-Available-1") {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -51,12 +51,13 @@ public class Restart {
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
IPSupport.throwSkippedExceptionIfNonOperational(); IPSupport.throwSkippedExceptionIfNonOperational();
ServerSocket ss = new ServerSocket(0); InetAddress localHost = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket(0, 0, localHost);
Socket s1 = null, s2 = null; Socket s1 = null, s2 = null;
try { try {
int port = ss.getLocalPort(); int port = ss.getLocalPort();
s1 = new Socket(InetAddress.getLocalHost(), port); s1 = new Socket(localHost, port);
s2 = ss.accept(); s2 = ss.accept();
// close server socket and the accepted connection // close server socket and the accepted connection
@ -64,7 +65,7 @@ public class Restart {
s2.close(); s2.close();
ss = new ServerSocket(); ss = new ServerSocket();
ss.bind( new InetSocketAddress(port) ); ss.bind( new InetSocketAddress(localHost, port) );
ss.close(); ss.close();
// close the client socket // close the client socket

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,6 +27,7 @@
* @run main/othervm BadProxySelector * @run main/othervm BadProxySelector
*/ */
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.ProxySelector; import java.net.ProxySelector;
@ -41,13 +42,13 @@ import java.io.*;
public class BadProxySelector { public class BadProxySelector {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ProxySelector.setDefault(new HTTPProxySelector()); ProxySelector.setDefault(new HTTPProxySelector());
try (ServerSocket ss = new ServerSocket(0); try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort()); Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
Socket s2 = ss.accept()) { Socket s2 = ss.accept()) {
} }
ProxySelector.setDefault(new NullHTTPProxySelector()); ProxySelector.setDefault(new NullHTTPProxySelector());
try (ServerSocket ss = new ServerSocket(0); try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort()); Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
Socket s2 = ss.accept()) { Socket s2 = ss.accept()) {
} }
@ -60,6 +61,7 @@ public class BadProxySelector {
@Override @Override
public List<Proxy> select(URI uri) { public List<Proxy> select(URI uri) {
System.out.println(this.getClass().getSimpleName() + " called for " + uri);
List<Proxy> proxies = new ArrayList<>(); List<Proxy> proxies = new ArrayList<>();
proxies.add(new Proxy(Proxy.Type.HTTP, proxies.add(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("localhost", 0))); new InetSocketAddress("localhost", 0)));
@ -75,6 +77,7 @@ public class BadProxySelector {
@Override @Override
public List<Proxy> select(URI uri) { public List<Proxy> select(URI uri) {
System.out.println(this.getClass().getSimpleName() + " called for " + uri);
List<Proxy> proxies = new ArrayList<>(); List<Proxy> proxies = new ArrayList<>();
proxies.add(null); proxies.add(null);
proxies.add(new Proxy(Proxy.Type.HTTP, proxies.add(new Proxy(Proxy.Type.HTTP,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -51,7 +51,7 @@ public class SocksProxyVersion implements Runnable {
} }
public SocksProxyVersion() throws Exception { public SocksProxyVersion() throws Exception {
ss = new ServerSocket(0); ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
int port = ss.getLocalPort(); int port = ss.getLocalPort();
Thread serverThread = new Thread(this); Thread serverThread = new Thread(this);
serverThread.start(); serverThread.start();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,6 +29,7 @@ import java.net.URLPermission;
* @library /test/lib * @library /test/lib
* @build jdk.test.lib.net.SimpleSSLContext * @build jdk.test.lib.net.SimpleSSLContext
* @run main/othervm URLTest * @run main/othervm URLTest
* @run main/othervm -Djava.net.preferIPv6Addresses=true URLTest
* @summary check URLPermission with Http(s)URLConnection * @summary check URLPermission with Http(s)URLConnection
*/ */
@ -77,14 +78,14 @@ public class URLTest {
if (sm != null) { if (sm != null) {
expectException = true; expectException = true;
Policy.setPolicy(new CustomPolicy( Policy.setPolicy(new CustomPolicy(
new URLPermission("http://127.0.0.1:"+httpPort+"/foo.html", "GET:X-Foo,Z-Bar"), new URLPermission("http://" + httpAuth + "/foo.html", "GET:X-Foo,Z-Bar"),
new URLPermission("https://127.0.0.1:"+httpsPort+"/foo.html", "POST:X-Fob,T-Bar"))); new URLPermission("https://" + httpsAuth + "/foo.html", "POST:X-Fob,T-Bar")));
} }
String url1 = "http://127.0.0.1:"+httpPort+"/foo.html"; String url1 = "http://" + httpAuth + "/foo.html";
String url2 = "https://127.0.0.1:"+httpsPort+"/foo.html"; String url2 = "https://" + httpsAuth + "/foo.html";
String url3 = "http://127.0.0.1:"+httpPort+"/bar.html"; String url3 = "http://" + httpAuth + "/bar.html";
String url4 = "https://127.0.0.1:"+httpsPort+"/bar.html"; String url4 = "https://" + httpsAuth + "/bar.html";
// simple positive test. Should succeed // simple positive test. Should succeed
test(url1, "GET", "X-Foo"); test(url1, "GET", "X-Foo");
@ -108,14 +109,14 @@ public class URLTest {
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();
if (sm != null) { if (sm != null) {
Policy.setPolicy(new CustomPolicy( Policy.setPolicy(new CustomPolicy(
new URLPermission("http://127.0.0.1:"+httpPort+"/*", "GET:X-Foo"), new URLPermission("http://" + httpAuth + "/*", "GET:X-Foo"),
new URLPermission("https://127.0.0.1:"+httpsPort+"/*", "POST:X-Fob"))); new URLPermission("https://" + httpsAuth + "/*", "POST:X-Fob")));
} }
String url1 = "http://127.0.0.1:"+httpPort+"/foo.html"; String url1 = "http://" + httpAuth + "/foo.html";
String url2 = "https://127.0.0.1:"+httpsPort+"/foo.html"; String url2 = "https://" + httpsAuth + "/foo.html";
String url3 = "http://127.0.0.1:"+httpPort+"/bar.html"; String url3 = "http://" + httpAuth + "/bar.html";
String url4 = "https://127.0.0.1:"+httpsPort+"/bar.html"; String url4 = "https://" + httpsAuth + "/bar.html";
// simple positive test. Should succeed // simple positive test. Should succeed
test(url1, "GET", "X-Foo"); test(url1, "GET", "X-Foo");
@ -132,14 +133,14 @@ public class URLTest {
if (sm != null) { if (sm != null) {
expectException = true; expectException = true;
Policy.setPolicy(new CustomPolicy( Policy.setPolicy(new CustomPolicy(
new URLPermission("http://127.0.0.1:"+httpPort+"/a/b/-", "DELETE,GET:X-Foo,Y-Foo"), new URLPermission("http://" + httpAuth + "/a/b/-", "DELETE,GET:X-Foo,Y-Foo"),
new URLPermission("https://127.0.0.1:"+httpsPort+"/a/c/-", "POST:*"))); new URLPermission("https://" + httpsAuth + "/a/c/-", "POST:*")));
} }
String url1 = "http://127.0.0.1:"+httpPort+"/foo.html"; String url1 = "http://" + httpAuth + "/foo.html";
String url2 = "https://127.0.0.1:"+httpsPort+"/a/c/d/e/foo.html"; String url2 = "https://" + httpsAuth + "/a/c/d/e/foo.html";
String url3 = "http://127.0.0.1:"+httpPort+"/a/b/c"; String url3 = "http://" + httpAuth + "/a/b/c";
String url4 = "https://127.0.0.1:"+httpsPort+"/a/b/c"; String url4 = "https://" + httpsAuth + "/a/b/c";
test(url1, "GET", "X-Foo", expectException); test(url1, "GET", "X-Foo", expectException);
test(url2, "POST", "X-Zxc"); test(url2, "POST", "X-Zxc");
@ -147,6 +148,16 @@ public class URLTest {
test(url4, "POST", "Y-Foo", expectException); test(url4, "POST", "Y-Foo", expectException);
} }
static String authority(InetSocketAddress address) {
String hostaddr = address.getAddress().getHostAddress();
int port = address.getPort();
if (hostaddr.indexOf(':') > -1) {
return "[" + hostaddr + "]:" + port;
} else {
return hostaddr + ":" + port;
}
}
// Convenience methods to simplify previous explicit test scenarios. // Convenience methods to simplify previous explicit test scenarios.
static void test(String u, String method, String header) throws IOException { static void test(String u, String method, String header) throws IOException {
test(u, method, header, null, false); test(u, method, header, null, false);
@ -175,7 +186,7 @@ public class URLTest {
System.out.println("url=" + u + " method=" + method + System.out.println("url=" + u + " method=" + method +
" header1=" + header1 + " header2=" + header2 + " header1=" + header1 + " header2=" + header2 +
" expectException=" + expectException); " expectException=" + expectException);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
if (urlc instanceof HttpsURLConnection) { if (urlc instanceof HttpsURLConnection) {
HttpsURLConnection ssl = (HttpsURLConnection)urlc; HttpsURLConnection ssl = (HttpsURLConnection)urlc;
ssl.setHostnameVerifier((host, sess) -> true); ssl.setHostnameVerifier((host, sess) -> true);
@ -220,11 +231,14 @@ public class URLTest {
static SSLContext ctx; static SSLContext ctx;
static int httpPort; static int httpPort;
static int httpsPort; static int httpsPort;
static String httpAuth;
static String httpsAuth;
static void createServers() throws Exception { static void createServers() throws Exception {
InetSocketAddress any = new InetSocketAddress(0); InetAddress loopback = InetAddress.getLoopbackAddress();
httpServer = HttpServer.create(any, 0); InetSocketAddress address = new InetSocketAddress(loopback, 0);
httpsServer = HttpsServer.create(any, 0); httpServer = HttpServer.create(address, 0);
httpsServer = HttpsServer.create(address, 0);
OkHandler h = new OkHandler(); OkHandler h = new OkHandler();
@ -243,6 +257,8 @@ public class URLTest {
httpPort = httpServer.getAddress().getPort(); httpPort = httpServer.getAddress().getPort();
httpsPort = httpsServer.getAddress().getPort(); httpsPort = httpsServer.getAddress().getPort();
httpAuth = authority(httpServer.getAddress());
httpsAuth = authority(httpsServer.getAddress());
} }
static void shutdown() { static void shutdown() {
@ -265,7 +281,9 @@ public class URLTest {
java.util.Arrays.stream(permissions).forEach(perms::add); java.util.Arrays.stream(permissions).forEach(perms::add);
// needed for the HTTP(S) server // needed for the HTTP(S) server
perms.add(new SocketPermission("localhost:1024-", "listen,resolve,accept")); InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress serverBound = new InetSocketAddress(loopback,1024);
perms.add(new SocketPermission(authority(serverBound) + "-", "listen,resolve,accept"));
// needed by the test to reset the policy, per testX method // needed by the test to reset the policy, per testX method
perms.add(new SecurityPermission("setPolicy")); perms.add(new SecurityPermission("setPolicy"));
// needed to shutdown the ThreadPoolExecutor ( used by the servers ) // needed to shutdown the ThreadPoolExecutor ( used by the servers )

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,7 +24,10 @@
/** /**
* @test * @test
* @bug 5054016 * @bug 5054016
* @library /test/lib
* @summary get the failure immediately when writing individual chunks over socket fail * @summary get the failure immediately when writing individual chunks over socket fail
* @run main CheckError
* @run main/othervm -Djava.net.preferIPv6Addresses=true CheckError
*/ */
import java.io.BufferedReader; import java.io.BufferedReader;
@ -33,11 +36,16 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.URL; import java.net.URL;
import static java.lang.System.out; import static java.lang.System.out;
import jdk.test.lib.net.URIBuilder;
public class CheckError { public class CheckError {
static int BUFFER_SIZE = 8192; // 8k static int BUFFER_SIZE = 8192; // 8k
@ -51,8 +59,13 @@ public class CheckError {
out.println("Server listening on " + port); out.println("Server listening on " + port);
URL url = new URL("http://localhost:" + port); URL url = URIBuilder.newBuilder()
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); .scheme("http")
.host(server.getAddress())
.port(port)
.toURL();
HttpURLConnection conn = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setChunkedStreamingMode(1024); conn.setChunkedStreamingMode(1024);
@ -98,13 +111,19 @@ public class CheckError {
final ServerSocket serverSocket; final ServerSocket serverSocket;
HTTPServer() throws IOException { HTTPServer() throws IOException {
serverSocket = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(loopback, 0));
} }
int getPort() { int getPort() {
return serverSocket.getLocalPort(); return serverSocket.getLocalPort();
} }
InetAddress getAddress() {
return serverSocket.getInetAddress();
}
public void run() { public void run() {
try (Socket client = serverSocket.accept()) { try (Socket client = serverSocket.accept()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -221,6 +221,7 @@ public class TestHttpServer {
listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT); listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) { } catch (IOException e) {
System.err.println ("Server could not start: " + e); System.err.println ("Server could not start: " + e);
throw new RuntimeException("Server could not start: " + e, e);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,6 +27,7 @@
* @modules java.base/sun.net.www * @modules java.base/sun.net.www
* @build TestHttpsServer HttpCallback * @build TestHttpsServer HttpCallback
* @run main/othervm ChunkedOutputStream * @run main/othervm ChunkedOutputStream
* @run main/othervm -Djava.net.preferIPv6Addresses=true ChunkedOutputStream
* *
* SunJSSE does not support dynamic system properties, no way to re-use * SunJSSE does not support dynamic system properties, no way to re-use
* system properties in samevm/agentvm mode. * system properties in samevm/agentvm mode.
@ -157,7 +158,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test1(String u) throws Exception { static void test1(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setChunkedStreamingMode(20); urlc.setChunkedStreamingMode(20);
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -174,7 +175,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test3(String u) throws Exception { static void test3(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setFixedLengthStreamingMode(str2.length()); urlc.setFixedLengthStreamingMode(str2.length());
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -191,7 +192,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test4(String u) throws Exception { static void test4(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setFixedLengthStreamingMode(str2.length()+1); urlc.setFixedLengthStreamingMode(str2.length()+1);
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -208,7 +209,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test5(String u) throws Exception { static void test5(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setFixedLengthStreamingMode(str2.length()-1); urlc.setFixedLengthStreamingMode(str2.length()-1);
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -224,7 +225,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test6(String u) throws Exception { static void test6(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setChunkedStreamingMode(20); urlc.setChunkedStreamingMode(20);
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -249,7 +250,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test7(String u) throws Exception { static void test7(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setChunkedStreamingMode(20); urlc.setChunkedStreamingMode(20);
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -264,7 +265,7 @@ public class ChunkedOutputStream implements HttpCallback {
static void test8(String u) throws Exception { static void test8(String u) throws Exception {
URL url = new URL(u); URL url = new URL(u);
System.out.println("client opening connection to: " + u); System.out.println("client opening connection to: " + u);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.setFixedLengthStreamingMode(0); urlc.setFixedLengthStreamingMode(0);
urlc.setDoOutput(true); urlc.setDoOutput(true);
urlc.setRequestMethod("POST"); urlc.setRequestMethod("POST");
@ -287,6 +288,8 @@ public class ChunkedOutputStream implements HttpCallback {
System.getProperty("test.src", "./") + "/" + pathToStores + System.getProperty("test.src", "./") + "/" + pathToStores +
"/" + trustStoreFile; "/" + trustStoreFile;
InetAddress loopback = InetAddress.getLoopbackAddress();
HostnameVerifier reservedHV = HostnameVerifier reservedHV =
HttpsURLConnection.getDefaultHostnameVerifier(); HttpsURLConnection.getDefaultHostnameVerifier();
try { try {
@ -298,17 +301,17 @@ public class ChunkedOutputStream implements HttpCallback {
try { try {
server = new TestHttpsServer( server = new TestHttpsServer(
new ChunkedOutputStream(), 1, 10, 0); new ChunkedOutputStream(), 1, 10, loopback, 0);
System.out.println ("Server started: listening on port: " + server.getLocalPort()); System.out.println("Server started: listening on: " + server.getAuthority());
// the test server doesn't support keep-alive yet // the test server doesn't support keep-alive yet
// test1("http://localhost:"+server.getLocalPort()+"/d0"); // test1("http://" + server.getAuthority() + "/d0");
test1("https://localhost:"+server.getLocalPort()+"/d01"); test1("https://" + server.getAuthority() + "/d01");
test3("https://localhost:"+server.getLocalPort()+"/d3"); test3("https://" + server.getAuthority() + "/d3");
test4("https://localhost:"+server.getLocalPort()+"/d4"); test4("https://" + server.getAuthority() + "/d4");
test5("https://localhost:"+server.getLocalPort()+"/d5"); test5("https://" + server.getAuthority() + "/d5");
test6("https://localhost:"+server.getLocalPort()+"/d6"); test6("https://" + server.getAuthority() + "/d6");
test7("https://localhost:"+server.getLocalPort()+"/d7"); test7("https://" + server.getAuthority() + "/d7");
test8("https://localhost:"+server.getLocalPort()+"/d8"); test8("https://" + server.getAuthority() + "/d8");
} catch (Exception e) { } catch (Exception e) {
if (server != null) { if (server != null) {
server.terminate(); server.terminate();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -106,11 +106,33 @@ public class TestHttpsServer {
* @param port the port number to bind the server to. <code>Zero</code> * @param port the port number to bind the server to. <code>Zero</code>
* means choose any free port. * means choose any free port.
*/ */
public TestHttpsServer(HttpCallback cb, int threads, int cperthread, int port) public TestHttpsServer(HttpCallback cb, int threads, int cperthread, int port)
throws IOException {
this(cb, threads, cperthread, null, port);
}
/**
* Create a <code>TestHttpsServer<code> instance with the specified number
* of threads and maximum number of connections per thread and running on
* the specified port. The specified number of threads are created to
* handle incoming requests, and each thread is allowed
* to handle a number of simultaneous TCP connections.
* @param cb the callback object which is invoked to handle
* each incoming request
* @param threads the number of threads to create to handle
* requests in parallel
* @param cperthread the number of simultaneous TCP connections
* to handle per thread
* @param address the InetAddress to bind to. {@code Null} means the
* wildcard address.
* @param port the port number to bind the server to. {@code Zero}
* means choose any free port.
*/
public TestHttpsServer(HttpCallback cb, int threads, int cperthread, InetAddress address, int port)
throws IOException { throws IOException {
schan = ServerSocketChannel.open(); schan = ServerSocketChannel.open();
InetSocketAddress addr = new InetSocketAddress (port); InetSocketAddress addr = new InetSocketAddress(address, port);
schan.socket().bind(addr); schan.socket().bind(addr);
this.threads = threads; this.threads = threads;
this.cb = cb; this.cb = cb;
@ -165,6 +187,14 @@ public class TestHttpsServer {
return schan.socket().getLocalPort (); return schan.socket().getLocalPort ();
} }
public String getAuthority() {
InetAddress address = schan.socket().getInetAddress();
String hostaddr = address.getHostAddress();
if (address.isAnyLocalAddress()) hostaddr = "localhost";
if (hostaddr.indexOf(':') > -1) hostaddr = "[" + hostaddr + "]";
return hostaddr + ":" + getLocalPort();
}
static class Server extends Thread { static class Server extends Thread {
ServerSocketChannel schan; ServerSocketChannel schan;