8223145: Replace wildcard address with loopback or local host in tests - part 1
authordfuchs
Thu, 02 May 2019 11:55:16 +0100
changeset 54681 edd709e64ea1
parent 54680 5b168a63cec5
child 54682 79d13fd7db9c
8223145: Replace wildcard address with loopback or local host in tests - part 1 Summary: Replaces binding to wildacard with alternative less susceptible to intermittent failure in some intermittently failing tests. Reviewed-by: chegar, msheppar
test/jdk/com/sun/net/httpserver/bugs/B6361557.java
test/jdk/java/net/Authenticator/B4722333.java
test/jdk/java/net/HttpURLConnection/UnmodifiableMaps.java
test/jdk/java/net/ResponseCache/ResponseCacheTest.java
test/jdk/java/net/Socket/GetLocalAddress.java
test/jdk/java/net/Socket/SetReceiveBufferSize.java
test/jdk/java/net/Socket/SoTimeout.java
test/jdk/java/net/Socket/TestAfterClose.java
test/jdk/java/net/Socket/UrgentDataTest.java
test/jdk/java/net/SocketOption/OptionsTest.java
test/jdk/java/net/URL/GetContent.java
test/jdk/java/net/URLConnection/B5052093.java
test/jdk/java/net/URLPermission/nstest/LookupTest.java
test/jdk/sun/net/ftp/TestFtpClientNameListWithNull.java
test/jdk/sun/net/www/http/HttpClient/ProxyTest.java
test/jdk/sun/net/www/http/HttpURLConnection/NTLMAuthWithSM.java
test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java
test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java
test/jdk/sun/net/www/httptest/TestHttpServer.java
test/jdk/sun/net/www/protocol/http/B8012625.java
test/jdk/sun/net/www/protocol/http/Finalizer.java
test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java
test/lib/jdk/test/lib/net/URIBuilder.java
--- a/test/jdk/com/sun/net/httpserver/bugs/B6361557.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/com/sun/net/httpserver/bugs/B6361557.java	Thu May 02 11:55:16 2019 +0100
@@ -69,7 +69,8 @@
 
     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 @@
         server.start ();
 
         InetSocketAddress destaddr = new InetSocketAddress (
-                InetAddress.getLoopbackAddress(), server.getAddress().getPort()
+                loopback, server.getAddress().getPort()
         );
         System.out.println ("destaddr " + destaddr);
 
@@ -86,7 +87,10 @@
         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<SelectionKey> selkeys = selector.selectedKeys();
             for (SelectionKey key : selkeys) {
                 if (key.isReadable()) {
@@ -95,14 +99,18 @@
                     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;
--- a/test/jdk/java/net/Authenticator/B4722333.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/Authenticator/B4722333.java	Thu May 02 11:55:16 2019 +0100
@@ -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
@@ -121,13 +121,14 @@
         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();
--- a/test/jdk/java/net/HttpURLConnection/UnmodifiableMaps.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/HttpURLConnection/UnmodifiableMaps.java	Thu May 02 11:55:16 2019 +0100
@@ -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 /test/lib
  * @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.test.lib.net.URIBuilder;
 
 public class UnmodifiableMaps {
 
@@ -48,7 +51,12 @@
         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 @@
 
     // 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 @@
         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
         if (failed > 0) throw new AssertionError("Some tests failed");}
 }
-
--- a/test/jdk/java/net/ResponseCache/ResponseCacheTest.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/ResponseCache/ResponseCacheTest.java	Thu May 02 11:55:16 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2014, 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 /test/lib
  * @author Yingxian Wang
  */
 
@@ -31,6 +32,7 @@
 import java.util.*;
 import java.io.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * Request should get serviced by the cache handler. Response get
@@ -90,14 +92,17 @@
             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");
@@ -126,8 +131,12 @@
         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<String,List<String>> headers2 = http.getHeaderFields();
--- a/test/jdk/java/net/Socket/GetLocalAddress.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/Socket/GetLocalAddress.java	Thu May 02 11:55:16 2019 +0100
@@ -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 @@
         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());
--- a/test/jdk/java/net/Socket/SetReceiveBufferSize.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/Socket/SetReceiveBufferSize.java	Thu May 02 11:55:16 2019 +0100
@@ -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 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);
--- a/test/jdk/java/net/Socket/SoTimeout.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/Socket/SoTimeout.java	Thu May 02 11:55:16 2019 +0100
@@ -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 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];
--- a/test/jdk/java/net/Socket/TestAfterClose.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/Socket/TestAfterClose.java	Thu May 02 11:55:16 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2018, 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
@@ -39,8 +39,9 @@
 
     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);
--- a/test/jdk/java/net/Socket/UrgentDataTest.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/Socket/UrgentDataTest.java	Thu May 02 11:55:16 2019 +0100
@@ -54,10 +54,12 @@
         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 = InetAddress.getLoopbackAddress().getHostAddress();
+                test.clHost = loopback.getHostAddress();
                 test.clPort = test.listener.getLocalPort();
                 test.run();
             } else if (args[0].equals ("-server")) {
--- a/test/jdk/java/net/SocketOption/OptionsTest.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/SocketOption/OptionsTest.java	Thu May 02 11:55:16 2019 +0100
@@ -99,7 +99,7 @@
 
     static void doSocketTests() throws Exception {
         try (
-            ServerSocket srv = new ServerSocket(0);
+            ServerSocket srv = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
             Socket c = new Socket(InetAddress.getLoopbackAddress(), srv.getLocalPort());
             Socket s = srv.accept();
         ) {
--- a/test/jdk/java/net/URL/GetContent.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/URL/GetContent.java	Thu May 02 11:55:16 2019 +0100
@@ -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 /test/lib
  * @summary Test a read from nonexistant URL
  */
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class GetContent implements Runnable {
 
@@ -71,10 +73,12 @@
 
          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];
--- a/test/jdk/java/net/URLConnection/B5052093.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/URLConnection/B5052093.java	Thu May 02 11:55:16 2019 +0100
@@ -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
@@ -64,9 +64,10 @@
     }
 
     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();
--- a/test/jdk/java/net/URLPermission/nstest/LookupTest.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/java/net/URLPermission/nstest/LookupTest.java	Thu May 02 11:55:16 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2017, 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
@@ -36,6 +36,8 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.NetPermission;
 import java.net.ProxySelector;
 import java.net.ServerSocket;
@@ -104,7 +106,7 @@
         String hostsFileName = CWD + "/LookupTestHosts";
         System.setProperty("jdk.net.hosts.file", hostsFileName);
         addMappingToHostsFile("allowedAndFound.com",
-                              "127.0.0.1",
+                              InetAddress.getLoopbackAddress().getHostAddress(),
                               hostsFileName,
                               false);
         addMappingToHostsFile("notAllowedButFound.com",
@@ -131,7 +133,9 @@
         private volatile boolean done;
 
         public Server() throws IOException {
-            serverSocket = new ServerSocket(0);
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            serverSocket = new ServerSocket();
+            serverSocket.bind(new InetSocketAddress(loopback, 0));
             port = serverSocket.getLocalPort();
         }
 
--- a/test/jdk/sun/net/ftp/TestFtpClientNameListWithNull.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/ftp/TestFtpClientNameListWithNull.java	Thu May 02 11:55:16 2019 +0100
@@ -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 @@
              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 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 {
--- a/test/jdk/sun/net/www/http/HttpClient/ProxyTest.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpClient/ProxyTest.java	Thu May 02 11:55:16 2019 +0100
@@ -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 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 @@
             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));
--- a/test/jdk/sun/net/www/http/HttpURLConnection/NTLMAuthWithSM.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpURLConnection/NTLMAuthWithSM.java	Thu May 02 11:55:16 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.Authenticator;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.PasswordAuthentication;
 import java.net.URL;
@@ -62,8 +63,8 @@
             // set authenticator
             Authenticator.setDefault(new AuthenticatorImpl());
 
-            String url = String.format("http://localhost:%d/test/",
-                    server.getPort());
+            String url = String.format("http://%s/test/",
+                    server.getAuthority());
 
             // load a document which is protected with NTML authentication
             System.out.println("load() called: " + url);
@@ -107,8 +108,9 @@
         }
 
         static LocalHttpServer startServer() throws IOException {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
             HttpServer httpServer = HttpServer.create(
-                    new InetSocketAddress(0), 0);
+                    new InetSocketAddress(loopback, 0), 0);
             LocalHttpServer localHttpServer = new LocalHttpServer(httpServer);
             localHttpServer.start();
 
@@ -126,6 +128,14 @@
             System.out.println("HttpServer: stopped");
         }
 
+        String getAuthority() {
+            InetAddress address = server.getAddress().getAddress();
+            String hostaddr = address.isAnyLocalAddress()
+                   ? "localhost" : address.getHostAddress();
+            if (hostaddr.indexOf(':') > -1) hostaddr = "[" + hostaddr + "]";
+            return hostaddr + ":" + getPort();
+        }
+
         int getPort() {
             return server.getAddress().getPort();
         }
--- a/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java	Thu May 02 11:55:16 2019 +0100
@@ -83,9 +83,10 @@
         }
 
         public String getAuthority() {
-            String address = server.getAddress().getHostString();
-            address =  (address.indexOf(':') >= 0) ? ("[" + address + "]") : address;
-            return address + ":" + getPort();
+            InetAddress address = server.getAddress().getAddress();
+            String hostaddr = address.isAnyLocalAddress() ? "localhost" : address.getHostAddress();
+            hostaddr =  (hostaddr.indexOf(':') >= 0) ? ("[" + hostaddr + "]") : hostaddr;
+            return hostaddr + ":" + getPort();
         }
 
         public int getPort() {
--- a/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java	Thu May 02 11:55:16 2019 +0100
@@ -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 /test/lib
  * @run main/othervm/timeout=30 KeepAliveStreamClose
  */
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class KeepAliveStreamClose {
     static class XServer extends Thread {
@@ -78,11 +80,16 @@
 
     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;
--- a/test/jdk/sun/net/www/httptest/TestHttpServer.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/httptest/TestHttpServer.java	Thu May 02 11:55:16 2019 +0100
@@ -69,6 +69,22 @@
     }
 
     /**
+     * Create a <code>TestHttpServer<code> 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. <code>Null</code>
+     *  means bind to the wildcard address.
+     * @param port the port number to bind the server to. <code>Zero</code>
+     *  means choose any free port.
+     */
+
+    public TestHttpServer (HttpCallback cb, InetAddress address, int port) throws IOException {
+        this (cb, 1, 10, address, 0);
+    }
+
+    /**
      * Create a <code>TestHttpServer<code> instance with the specified number of
      * threads and maximum number of connections per thread. This functions
      * the same as the 4 arg constructor, where the port argument is set to zero.
@@ -102,9 +118,33 @@
      */
 
     public TestHttpServer (HttpCallback cb, int threads, int cperthread, int port)
+            throws IOException {
+        this(cb, threads, cperthread, null, port);
+    }
+
+    /**
+     * Create a <code>TestHttpServer<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 address to bind the server to. <code>Null</code>
+     *  means bind to the wildcard address.
+     * @param port the port number to bind the server to. <code>Zero</code>
+     *  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;
@@ -147,6 +187,14 @@
         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;
@@ -746,4 +794,3 @@
         }
     }
 }
-
--- a/test/jdk/sun/net/www/protocol/http/B8012625.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/B8012625.java	Thu May 02 11:55:16 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2016, 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
@@ -82,7 +82,8 @@
     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);
--- a/test/jdk/sun/net/www/protocol/http/Finalizer.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/Finalizer.java	Thu May 02 11:55:16 2019 +0100
@@ -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 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 @@
         } catch (IOException e) {
             throw new RuntimeException("finalize method failure."+e);
         } catch (InterruptedException ie) {
+        } finally {
+            if (serversocket != null) {
+                try {serversocket.close();} catch (IOException io) {}
+            }
         }
     }
 
--- a/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java	Thu May 02 11:55:16 2019 +0100
@@ -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
@@ -99,8 +99,9 @@
     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 = URIBuilder.newBuilder()
                 .scheme("http")
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHandlerTest.java	Thu May 02 11:55:16 2019 +0100
@@ -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 /test/lib
  * @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.test.lib.net.URIBuilder;
 
 public class CookieHandlerTest {
     static Map<String,String> cookies;
@@ -78,10 +80,12 @@
      * 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 @@
         }
         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();
 
--- a/test/lib/jdk/test/lib/net/URIBuilder.java	Thu May 02 10:32:28 2019 +0200
+++ b/test/lib/jdk/test/lib/net/URIBuilder.java	Thu May 02 11:55:16 2019 +0100
@@ -61,6 +61,12 @@
         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());
     }