diff --git a/jdk/test/com/sun/net/httpserver/bugs/B6361557.java b/jdk/test/com/sun/net/httpserver/bugs/B6361557.java index d854c772503..70ed5392ccd 100644 --- a/jdk/test/com/sun/net/httpserver/bugs/B6361557.java +++ b/jdk/test/com/sun/net/httpserver/bugs/B6361557.java @@ -69,7 +69,8 @@ public void handle (HttpExchange t) public static void main (String[] args) throws Exception { Handler handler = new Handler(); - InetSocketAddress addr = new InetSocketAddress (0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + InetSocketAddress addr = new InetSocketAddress (loopback, 0); HttpServer server = HttpServer.create (addr, 0); HttpContext ctx = server.createContext ("/test", handler); @@ -78,7 +79,7 @@ public static void main (String[] args) throws Exception { server.start (); InetSocketAddress destaddr = new InetSocketAddress ( - "127.0.0.1", server.getAddress().getPort() + loopback, server.getAddress().getPort() ); System.out.println ("destaddr " + destaddr); @@ -86,7 +87,10 @@ public static void main (String[] args) throws Exception { int requests = 0; int responses = 0; while (true) { - int selres = selector.select (1); + // we need to read responses from time to time: slightly + // increase the timeout with the amount of pending responses + // to give a chance to the server to reply. + int selres = selector.select (requests - responses + 1); Set selkeys = selector.selectedKeys(); for (SelectionKey key : selkeys) { if (key.isReadable()) { @@ -95,14 +99,18 @@ public static void main (String[] args) throws Exception { try { int x = chan.read(buf); if (x == -1 || responseComplete(buf)) { + System.out.print("_"); key.attach(null); chan.close(); responses++; } - } catch (IOException e) {} + } catch (IOException e) { + System.out.println(e); + } } } if (requests < NUM) { + System.out.print("."); SocketChannel schan = SocketChannel.open(destaddr); requestBuf.rewind(); int c = 0; diff --git a/jdk/test/java/net/Authenticator/B4722333.java b/jdk/test/java/net/Authenticator/B4722333.java index 441e1107887..4ed2466d8de 100644 --- a/jdk/test/java/net/Authenticator/B4722333.java +++ b/jdk/test/java/net/Authenticator/B4722333.java @@ -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. * * This code is free software; you can redistribute it and/or modify it @@ -120,13 +120,14 @@ public static void main (String[] args) throws Exception { MyAuthenticator auth = new MyAuthenticator (); Authenticator.setDefault (auth); try { - server = new TestHttpServer (new B4722333(), 1, 10, 0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + server = new TestHttpServer (new B4722333(), 1, 10, loopback, 0); System.out.println ("Server started: listening on port: " + server.getLocalPort()); - client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html"); - client ("http://localhost:"+server.getLocalPort()+"/ASD/d3/x.html"); - client ("http://localhost:"+server.getLocalPort()+"/biz/d3/x.html"); - client ("http://localhost:"+server.getLocalPort()+"/bar/d3/x.html"); - client ("http://localhost:"+server.getLocalPort()+"/fuzz/d3/x.html"); + client ("http://" + server.getAuthority() + "/d1/d2/d3/foo.html"); + client ("http://" + server.getAuthority() + "/ASD/d3/x.html"); + client ("http://" + server.getAuthority() + "/biz/d3/x.html"); + client ("http://" + server.getAuthority() + "/bar/d3/x.html"); + client ("http://" + server.getAuthority() + "/fuzz/d3/x.html"); } catch (Exception e) { if (server != null) { server.terminate(); diff --git a/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java b/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java index c8c6e1b54f6..64c63729e10 100644 --- a/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java +++ b/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2019, 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 @@ -24,11 +24,13 @@ /** * @test * @bug 7128648 + * @library /lib/testlibrary * @modules jdk.httpserver * @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map */ import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; import java.net.HttpURLConnection; @@ -41,6 +43,7 @@ import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.Headers; import static java.net.Proxy.NO_PROXY; +import jdk.testlibrary.net.URIBuilder; public class UnmodifiableMaps { @@ -48,7 +51,12 @@ void test(String[] args) throws Exception { HttpServer server = startHttpServer(); try { InetSocketAddress address = server.getAddress(); - URI uri = new URI("http://localhost:" + address.getPort() + "/foo"); + URI uri = URIBuilder.newBuilder() + .scheme("http") + .host(address.getAddress()) + .port(address.getPort()) + .path("/foo") + .build(); doClient(uri); } finally { server.stop(0); @@ -78,7 +86,8 @@ void doClient(URI uri) throws Exception { // HTTP Server HttpServer startHttpServer() throws IOException { - HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0); httpServer.createContext("/foo", new SimpleHandler()); httpServer.start(); return httpServer; @@ -146,4 +155,3 @@ public void instanceMain(String[] args) throws Throwable { System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); if (failed > 0) throw new AssertionError("Some tests failed");} } - diff --git a/jdk/test/java/net/ResponseCache/ResponseCacheTest.java b/jdk/test/java/net/ResponseCache/ResponseCacheTest.java index 57d87c04499..cf432740418 100644 --- a/jdk/test/java/net/ResponseCache/ResponseCacheTest.java +++ b/jdk/test/java/net/ResponseCache/ResponseCacheTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2019, 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 @@ -24,6 +24,7 @@ /* @test * @summary Unit test for java.net.ResponseCache * @bug 4837267 + * @library /lib/testlibrary * @author Yingxian Wang */ @@ -32,6 +33,7 @@ import java.io.*; import sun.net.www.ParseUtil; import javax.net.ssl.*; +import jdk.testlibrary.net.URIBuilder; /** * Request should get serviced by the cache handler. Response get @@ -91,14 +93,17 @@ public void run() { try { fis.close(); } catch (IOException unused) {} } } -static class NameVerifier implements HostnameVerifier { + static class NameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } } ResponseCacheTest() throws Exception { /* start the server */ - ss = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ss = new ServerSocket(); + ss.bind(new InetSocketAddress(loopback, 0)); + (new Thread(this)).start(); /* establish http connection to server */ url1 = new URL("http://localhost/file1.cache"); @@ -127,8 +132,12 @@ public boolean verify(String hostname, SSLSession session) { http.disconnect(); // testing ResponseCacheHandler.put() - url2 = new URL("http://localhost:" + - Integer.toString(ss.getLocalPort())+"/file2.1"); + url2 = URIBuilder.newBuilder() + .scheme("http") + .host(ss.getInetAddress()) + .port(ss.getLocalPort()) + .path("/file2.1") + .toURL(); http = (HttpURLConnection)url2.openConnection(); System.out.println("responsecode2 is :"+http.getResponseCode()); Map> headers2 = http.getHeaderFields(); diff --git a/jdk/test/java/net/Socket/GetLocalAddress.java b/jdk/test/java/net/Socket/GetLocalAddress.java index 3133f92ca54..aade594df72 100644 --- a/jdk/test/java/net/Socket/GetLocalAddress.java +++ b/jdk/test/java/net/Socket/GetLocalAddress.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2013, 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. * * This code is free software; you can redistribute it and/or modify it @@ -45,7 +45,8 @@ public static void main(String args[]) throws Exception { int linger = 65546; int value = 0; addr = InetAddress.getLocalHost(); - ss = new ServerSocket(0); + ss = new ServerSocket(); + ss.bind(new InetSocketAddress(addr, 0)); port = ss.getLocalPort(); Thread t = new Thread(new GetLocalAddress()); diff --git a/jdk/test/java/net/Socket/SetReceiveBufferSize.java b/jdk/test/java/net/Socket/SetReceiveBufferSize.java index 3f848f1fee5..00a5d396bd6 100644 --- a/jdk/test/java/net/Socket/SetReceiveBufferSize.java +++ b/jdk/test/java/net/Socket/SetReceiveBufferSize.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2019, 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 @@ -28,6 +28,8 @@ * */ +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.Socket; import java.net.ServerSocket; @@ -37,8 +39,10 @@ public static void main(String[] args) throws Exception { } public SetReceiveBufferSize() throws Exception { - ServerSocket ss = new ServerSocket(0); - Socket s = new Socket("localhost", ss.getLocalPort()); + ServerSocket ss = new ServerSocket(); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ss.bind(new InetSocketAddress(loopback, 0)); + Socket s = new Socket(loopback, ss.getLocalPort()); Socket accepted = ss.accept(); try { s.setReceiveBufferSize(0); diff --git a/jdk/test/java/net/Socket/SoTimeout.java b/jdk/test/java/net/Socket/SoTimeout.java index 9c2204746dc..5432c392bfc 100644 --- a/jdk/test/java/net/Socket/SoTimeout.java +++ b/jdk/test/java/net/Socket/SoTimeout.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2010, 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. * * This code is free software; you can redistribute it and/or modify it @@ -44,7 +44,8 @@ public class SoTimeout implements Runnable { public static void main(String[] args) throws Exception { addr = InetAddress.getLocalHost(); - serverSocket = new ServerSocket(0); + serverSocket = new ServerSocket(); + serverSocket.bind(new InetSocketAddress(addr, 0)); port = serverSocket.getLocalPort(); byte[] b = new byte[12]; diff --git a/jdk/test/java/net/Socket/TestAfterClose.java b/jdk/test/java/net/Socket/TestAfterClose.java index a3f819c0df1..9da1364e2f9 100644 --- a/jdk/test/java/net/Socket/TestAfterClose.java +++ b/jdk/test/java/net/Socket/TestAfterClose.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2019, 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 @@ -36,8 +36,9 @@ public class TestAfterClose public static void main(String[] args) { try { - ServerSocket ss = new ServerSocket(0, 0, null); - Socket socket = new Socket("localhost", ss.getLocalPort()); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ServerSocket ss = new ServerSocket(0, 0, loopback); + Socket socket = new Socket(loopback, ss.getLocalPort()); ss.accept(); ss.close(); test(socket); diff --git a/jdk/test/java/net/Socket/UrgentDataTest.java b/jdk/test/java/net/Socket/UrgentDataTest.java index 198712ebbe5..46644150271 100644 --- a/jdk/test/java/net/Socket/UrgentDataTest.java +++ b/jdk/test/java/net/Socket/UrgentDataTest.java @@ -53,10 +53,12 @@ public static void main (String args[]) { try { UrgentDataTest test = new UrgentDataTest (); if (args.length == 0) { - test.listener = new ServerSocket (0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + test.listener = new ServerSocket (); + test.listener.bind(new InetSocketAddress(loopback, 0)); test.isClient = true; test.isServer = true; - test.clHost = "127.0.0.1"; + test.clHost = loopback.getHostAddress(); test.clPort = test.listener.getLocalPort(); test.run(); } else if (args[0].equals ("-server")) { diff --git a/jdk/test/java/net/URL/GetContent.java b/jdk/test/java/net/URL/GetContent.java index 70dbf219bd5..5efe6cfdc82 100644 --- a/jdk/test/java/net/URL/GetContent.java +++ b/jdk/test/java/net/URL/GetContent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2010, 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. * * This code is free software; you can redistribute it and/or modify it @@ -24,11 +24,13 @@ /** * @test * @bug 4145315 + * @library /lib/testlibrary * @summary Test a read from nonexistant URL */ import java.net.*; import java.io.*; +import jdk.testlibrary.net.URIBuilder; public class GetContent implements Runnable { @@ -71,10 +73,12 @@ public void run() { boolean error = true; try { - String name = "http://localhost:" + ss.getLocalPort() + - "/no-such-name"; - java.net.URL url = null; - url = new java.net.URL(name); + java.net.URL url = URIBuilder.newBuilder() + .scheme("http") + .host(ss.getInetAddress()) + .port(ss.getLocalPort()) + .path("/no-such-name") + .toURL(); Object obj = url.getContent(); InputStream in = (InputStream) obj; byte buff[] = new byte[200]; diff --git a/jdk/test/java/net/URLConnection/B5052093.java b/jdk/test/java/net/URLConnection/B5052093.java index be1f23ff312..3af5cf1e11c 100644 --- a/jdk/test/java/net/URLConnection/B5052093.java +++ b/jdk/test/java/net/URLConnection/B5052093.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2019, 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 @@ -63,9 +63,10 @@ public void request(HttpTransaction req) { } public static void main(String[] args) throws Exception { - server = new TestHttpServer(new B5052093(), 1, 10, 0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + server = new TestHttpServer(new B5052093(), 1, 10, loopback, 0); try { - URL url = new URL("http://localhost:"+server.getLocalPort()+"/foo"); + URL url = new URL("http://" + server.getAuthority() + "/foo"); URLConnection conn = url.openConnection(); int i = conn.getContentLength(); long l = conn.getContentLengthLong(); diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/net/URIBuilder.java b/jdk/test/lib/testlibrary/jdk/testlibrary/net/URIBuilder.java index 78844678e57..0f4a0378888 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/net/URIBuilder.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/net/URIBuilder.java @@ -61,6 +61,12 @@ public URIBuilder host(String host) { return this; } + public URIBuilder host(InetAddress address) { + String hostaddr = address.isAnyLocalAddress() + ? "localhost" : address.getHostAddress(); + return host(hostaddr); + } + public URIBuilder loopback() { return host(InetAddress.getLoopbackAddress().getHostAddress()); } diff --git a/jdk/test/sun/net/ftp/TestFtpClientNameListWithNull.java b/jdk/test/sun/net/ftp/TestFtpClientNameListWithNull.java index 7a142f628cb..e89439cd578 100644 --- a/jdk/test/sun/net/ftp/TestFtpClientNameListWithNull.java +++ b/jdk/test/sun/net/ftp/TestFtpClientNameListWithNull.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; @@ -52,7 +53,8 @@ public static void main(String[] args) throws Exception { FtpClient client = FtpClient.create()) { (new Thread(server)).start(); int port = server.getPort(); - client.connect(new InetSocketAddress("localhost", port)); + InetAddress loopback = InetAddress.getLoopbackAddress(); + client.connect(new InetSocketAddress(loopback, port)); client.nameList(null); } finally { if (commandHasArgs) { @@ -66,7 +68,9 @@ private static class FtpServer implements AutoCloseable, Runnable { private final ServerSocket serverSocket; FtpServer() throws IOException { - serverSocket = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + serverSocket = new ServerSocket(); + serverSocket.bind(new InetSocketAddress(loopback, 0)); } public void handleClient(Socket client) throws IOException { diff --git a/jdk/test/sun/net/www/http/HttpClient/ProxyTest.java b/jdk/test/sun/net/www/http/HttpClient/ProxyTest.java index 5bbd5781467..71b8660e4ec 100644 --- a/jdk/test/sun/net/www/http/HttpClient/ProxyTest.java +++ b/jdk/test/sun/net/www/http/HttpClient/ProxyTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2013, 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. * * This code is free software; you can redistribute it and/or modify it @@ -126,7 +126,9 @@ public void run() { } public HttpProxyServer() throws IOException { - server = new ServerSocket(0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + server = new ServerSocket(); + server.bind(new InetSocketAddress(loopback, 0)); } public int getPort() { @@ -183,7 +185,8 @@ public ProxyTest() throws Exception { server.start(); int port = server.getPort(); - Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port)); + InetAddress loopback = InetAddress.getLoopbackAddress(); + Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(loopback, port)); URL url = new URL(testURL); InputStream ins = (url.openConnection(ftpProxy)).getInputStream(); in = new BufferedReader(new InputStreamReader(ins)); diff --git a/jdk/test/sun/net/www/http/HttpURLConnection/PostOnDelete.java b/jdk/test/sun/net/www/http/HttpURLConnection/PostOnDelete.java index 07d353cba85..5228c905a34 100644 --- a/jdk/test/sun/net/www/http/HttpURLConnection/PostOnDelete.java +++ b/jdk/test/sun/net/www/http/HttpURLConnection/PostOnDelete.java @@ -80,6 +80,13 @@ public void startServer() { server.start(); } + public String getAuthority() { + InetAddress address = server.getAddress().getAddress(); + String hostaddr = address.isAnyLocalAddress() ? "localhost" : address.getHostAddress(); + hostaddr = (hostaddr.indexOf(':') >= 0) ? ("[" + hostaddr + "]") : hostaddr; + return hostaddr + ":" + getPort(); + } + public int getPort() { return server.getAddress().getPort(); } diff --git a/jdk/test/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java b/jdk/test/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java index d2f5490e9e8..7b972c3415e 100644 --- a/jdk/test/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java +++ b/jdk/test/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2019, 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 @@ -25,11 +25,13 @@ * @test * @bug 4392195 * @summary Infinite loop in sun.net.www.http.KeepAliveStream [due to skip()] + * @library /lib/testlibrary * @run main/othervm/timeout=30 KeepAliveStreamClose */ import java.net.*; import java.io.*; +import jdk.testlibrary.net.URIBuilder; public class KeepAliveStreamClose { static class XServer extends Thread { @@ -78,11 +80,16 @@ public void run() { public static void main (String[] args) { try { - ServerSocket serversocket = new ServerSocket (0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + ServerSocket serversocket = new ServerSocket (0, 50, loopback); int port = serversocket.getLocalPort (); XServer server = new XServer (serversocket); server.start (); - URL url = new URL ("http://localhost:"+port); + URL url = URIBuilder.newBuilder() + .scheme("http") + .loopback() + .port(port) + .toURL(); URLConnection urlc = url.openConnection (); InputStream is = urlc.getInputStream (); int i=0, c; diff --git a/jdk/test/sun/net/www/httptest/TestHttpServer.java b/jdk/test/sun/net/www/httptest/TestHttpServer.java index 13f2098915e..0802b71f777 100644 --- a/jdk/test/sun/net/www/httptest/TestHttpServer.java +++ b/jdk/test/sun/net/www/httptest/TestHttpServer.java @@ -68,6 +68,22 @@ public TestHttpServer (HttpCallback cb) throws IOException { this (cb, 1, 10, 0); } + /** + * Create a TestHttpServer instance with the specified callback object + * for handling requests. One thread is created to handle requests, + * and up to ten TCP connections will be handled simultaneously. + * @param cb the callback object which is invoked to handle each + * incoming request + * @param address the address to bind the server to. Null + * means bind to the wildcard address. + * @param port the port number to bind the server to. Zero + * means choose any free port. + */ + + public TestHttpServer (HttpCallback cb, InetAddress address, int port) throws IOException { + this (cb, 1, 10, address, 0); + } + /** * Create a TestHttpServer instance with the specified number of * threads and maximum number of connections per thread. This functions @@ -102,9 +118,33 @@ public TestHttpServer (HttpCallback cb, int threads, int cperthread) */ public TestHttpServer (HttpCallback cb, int threads, int cperthread, int port) + throws IOException { + this(cb, threads, cperthread, null, port); + } + + /** + * Create a TestHttpServer 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 address to bind the server to. Null + * means bind to the wildcard address. + * @param port the port number to bind the server to. Zero + * means choose any free port. + */ + + public TestHttpServer (HttpCallback cb, int threads, int cperthread, + InetAddress address, int port) throws IOException { schan = ServerSocketChannel.open (); - InetSocketAddress addr = new InetSocketAddress (port); + InetSocketAddress addr = new InetSocketAddress (address, port); schan.socket().bind (addr); this.threads = threads; this.cb = cb; @@ -136,6 +176,14 @@ public int 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 { ServerSocketChannel schan; diff --git a/jdk/test/sun/net/www/protocol/http/B8012625.java b/jdk/test/sun/net/www/protocol/http/B8012625.java index c8c8ef0c033..7283009f84d 100644 --- a/jdk/test/sun/net/www/protocol/http/B8012625.java +++ b/jdk/test/sun/net/www/protocol/http/B8012625.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 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. * * This code is free software; you can redistribute it and/or modify it @@ -72,7 +72,8 @@ public void run() throws Exception { ExecutorService ex; public B8012625 () throws Exception { - server = HttpServer.create(new InetSocketAddress(0), 10); + InetAddress loopback = InetAddress.getLoopbackAddress(); + server = HttpServer.create(new InetSocketAddress(loopback, 0), 10); HttpContext ctx = server.createContext("/", this); ex = Executors.newFixedThreadPool(5); server.setExecutor(ex); diff --git a/jdk/test/sun/net/www/protocol/http/Finalizer.java b/jdk/test/sun/net/www/protocol/http/Finalizer.java index 6d6b33bc95b..2808bab9395 100644 --- a/jdk/test/sun/net/www/protocol/http/Finalizer.java +++ b/jdk/test/sun/net/www/protocol/http/Finalizer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2019, 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 @@ -80,8 +80,11 @@ public void run() { public class Finalizer { public static void main (String args[]) { + ServerSocket serversocket = null; try { - ServerSocket serversocket = new ServerSocket (0); + InetAddress loopback = InetAddress.getLoopbackAddress(); + serversocket = new ServerSocket(); + serversocket.bind(new InetSocketAddress(loopback, 0)); int port = serversocket.getLocalPort (); XServer server = new XServer (serversocket); server.start (); @@ -107,6 +110,10 @@ public static void main (String args[]) { } catch (IOException e) { throw new RuntimeException("finalize method failure."+e); } catch (InterruptedException ie) { + } finally { + if (serversocket != null) { + try {serversocket.close();} catch (IOException io) {} + } } } diff --git a/jdk/test/sun/net/www/protocol/http/ResponseCacheStream.java b/jdk/test/sun/net/www/protocol/http/ResponseCacheStream.java index bff2a765d6c..b6a5a1f3e0f 100644 --- a/jdk/test/sun/net/www/protocol/http/ResponseCacheStream.java +++ b/jdk/test/sun/net/www/protocol/http/ResponseCacheStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2019, 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 @@ -96,8 +96,9 @@ public byte[] getBuffer() { public static void main(String[] args) throws Exception { MyResponseCache cache = new MyResponseCache(); try { + InetAddress loopback = InetAddress.getLoopbackAddress(); ResponseCache.setDefault(cache); - server = new TestHttpServer (new ResponseCacheStream()); + server = new TestHttpServer (new ResponseCacheStream(), loopback, 0); System.out.println ("Server: listening on port: " + server.getLocalPort()); URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/"); System.out.println ("Client: connecting to " + url); diff --git a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java b/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java index 565138c143d..110179da3c5 100644 --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java +++ b/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2019, 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 @@ -24,6 +24,7 @@ /* @test * @bug 4696506 4942650 * @summary Unit test for java.net.CookieHandler + * @library /lib/testlibrary * @run main/othervm CookieHandlerTest * * SunJSSE does not support dynamic system properties, no way to re-use @@ -35,6 +36,7 @@ import java.util.*; import java.io.*; import javax.net.ssl.*; +import jdk.testlibrary.net.URIBuilder; public class CookieHandlerTest { static Map cookies; @@ -78,10 +80,12 @@ public class CookieHandlerTest { * to avoid infinite hangs. */ void doServerSide() throws Exception { + InetAddress loopback = InetAddress.getLoopbackAddress(); SSLServerSocketFactory sslssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket sslServerSocket = - (SSLServerSocket) sslssf.createServerSocket(serverPort); + (SSLServerSocket) sslssf.createServerSocket(); + sslServerSocket.bind(new InetSocketAddress(loopback, serverPort)); serverPort = sslServerSocket.getLocalPort(); /* @@ -151,8 +155,11 @@ void doClientSide() throws Exception { } HttpsURLConnection http = null; /* establish http connection to server */ - String uri = "https://localhost:" + +serverPort ; - URL url = new URL(uri); + URL url = URIBuilder.newBuilder() + .scheme("https") + .loopback() + .port(serverPort) + .toURL(); HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); http = (HttpsURLConnection)url.openConnection();