8225578: Replace wildcard address with loopback or local host in tests - part 16
authordfuchs
Mon, 17 Jun 2019 20:03:34 +0100
changeset 55399 46049b8a5658
parent 55398 e53ec3b362f4
child 55400 da554fdb51d0
8225578: Replace wildcard address with loopback or local host in tests - part 16 Summary: Fixes java/net/Authenticator and java/net/CookieHandler to stop depending on the wildcard address, wherever possible. Reviewed-by: chegar
test/jdk/java/net/Authenticator/AuthNPETest.java
test/jdk/java/net/Authenticator/B4678055.java
test/jdk/java/net/Authenticator/B4759514.java
test/jdk/java/net/Authenticator/B4769350.java
test/jdk/java/net/Authenticator/B4921848.java
test/jdk/java/net/Authenticator/B4933582.java
test/jdk/java/net/Authenticator/B4962064.java
test/jdk/java/net/Authenticator/B6870935.java
test/jdk/java/net/Authenticator/B8034170.java
test/jdk/java/net/Authenticator/BasicTest.java
test/jdk/java/net/Authenticator/BasicTest3.java
test/jdk/java/net/Authenticator/BasicTest4.java
test/jdk/java/net/Authenticator/Deadlock.java
test/jdk/java/net/CookieHandler/CookieHandlerTest.java
test/jdk/java/net/CookieHandler/CookieManagerTest.java
test/jdk/java/net/CookieHandler/EmptyCookieHeader.java
test/jdk/java/net/CookieHandler/LocalHostCookie.java
--- a/test/jdk/java/net/Authenticator/AuthNPETest.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/AuthNPETest.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 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
@@ -24,11 +24,15 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * @test
  * @bug 4662246
  * @summary  REGRESSION: plugin 14x client authentication dialog returns NullPointerException
+ * @library /test/lib
+ * @run main/othervm AuthNPETest
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true AuthNPETest
  */
 
 public class AuthNPETest {
@@ -53,45 +57,45 @@
             "Content-Type: text/html; charset=iso-8859-1\r\n" +
             "Content-Length: 10\r\n\r\n";
 
-        BasicServer (ServerSocket s) {
+        BasicServer(ServerSocket s) {
             server = s;
         }
 
-        void readAll (Socket s) throws IOException {
+        void readAll(Socket s) throws IOException {
             byte[] buf = new byte [128];
-            InputStream is = s.getInputStream ();
+            InputStream is = s.getInputStream();
             s.setSoTimeout(1000);
             try {
                 while (is.read(buf) > 0) ;
             } catch (SocketTimeoutException x) { }
         }
 
-        public void run () {
+        public void run() {
             try {
-                System.out.println ("Server 1: accept");
-                s = server.accept ();
-                System.out.println ("accepted");
+                System.out.println("Server 1: accept");
+                s = server.accept();
+                System.out.println("accepted");
                 os = s.getOutputStream();
-                os.write (reply1.getBytes());
-                readAll (s);
-                s.close ();
+                os.write(reply1.getBytes());
+                readAll(s);
+                s.close();
 
-                System.out.println ("Server 2: accept");
-                s = server.accept ();
-                System.out.println ("accepted");
+                System.out.println("Server 2: accept");
+                s = server.accept();
+                System.out.println("accepted");
                 os = s.getOutputStream();
-                os.write ((reply2+"HelloWorld").getBytes());
-                readAll (s);
-                s.close ();
+                os.write((reply2+"HelloWorld").getBytes());
+                readAll(s);
+                s.close();
 
             }
             catch (Exception e) {
                 System.out.println (e);
             }
-            finished ();
+            finished();
         }
 
-        public synchronized void finished () {
+        public synchronized void finished() {
             notifyAll();
         }
 
@@ -99,48 +103,54 @@
 
     static class MyAuthenticator extends Authenticator {
 
-        MyAuthenticator () {
-            super ();
+        MyAuthenticator() {
+            super();
         }
 
         int count = 0;
 
-        public PasswordAuthentication getPasswordAuthentication ()
+        public PasswordAuthentication getPasswordAuthentication()
             {
-            count ++;
-            System.out.println ("Auth called");
-            return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
+            count++;
+            System.out.println("Auth called");
+            return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));
         }
 
-        public int getCount () {
-            return (count);
+        public int getCount() {
+            return count;
         }
     }
 
 
-    static void read (InputStream is) throws IOException {
+    static void read(InputStream is) throws IOException {
         int c;
-        System.out.println ("reading");
+        System.out.println("reading");
         while ((c=is.read()) != -1) {
-            System.out.write (c);
+            System.out.write(c);
         }
-        System.out.println ("");
-        System.out.println ("finished reading");
+        System.out.println("");
+        System.out.println("finished reading");
     }
 
-    public static void main (String args[]) throws Exception {
-        MyAuthenticator auth = new MyAuthenticator ();
-        Authenticator.setDefault (auth);
-        ServerSocket ss = new ServerSocket (0);
-        int port = ss.getLocalPort ();
-        BasicServer server = new BasicServer (ss);
+    public static void main(String args[]) throws Exception {
+        MyAuthenticator auth = new MyAuthenticator();
+        Authenticator.setDefault(auth);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
+        int port = ss.getLocalPort();
+        BasicServer server = new BasicServer(ss);
         synchronized (server) {
             server.start();
             System.out.println ("client 1");
-            URL url = new URL ("http://localhost:"+port);
-            URLConnection urlc = url.openConnection ();
-            InputStream is = urlc.getInputStream ();
-            read (is);
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .toURL();
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
+            InputStream is = urlc.getInputStream();
+            read(is);
             is.close();
         }
     }
--- a/test/jdk/java/net/Authenticator/B4678055.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B4678055.java	Mon Jun 17 20:03:34 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
@@ -25,14 +25,16 @@
  * @test
  * @bug 4678055
  * @modules java.base/sun.net.www
- * @library ../../../sun/net/www/httptest/
+ * @library ../../../sun/net/www/httptest/ /test/lib
  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
- * @run main B4678055
+ * @run main/othervm B4678055
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B4678055
  * @summary Basic Authentication fails with multiple realms
  */
 
 import java.io.*;
 import java.net.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class B4678055 implements HttpCallback {
 
@@ -125,12 +127,21 @@
     public static void main (String[] args) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
+        ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
         try {
-            server = new TestHttpServer (new B4678055(), 1, 10, 0);
-            System.out.println ("Server: listening on port: " + server.getLocalPort());
-            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
-            client ("http://localhost:"+server.getLocalPort()+"/d2/foo.html");
-            client ("http://localhost:"+server.getLocalPort()+"/d2/foo.html");
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            server = new TestHttpServer(new B4678055(), 1, 10, loopback, 0);
+            String serverURL = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .build()
+                .toString();
+            System.out.println("Server: listening at: " + serverURL);
+            client(serverURL + "d1/foo.html");
+            client(serverURL + "d2/foo.html");
+            client(serverURL + "d2/foo.html");
         } catch (Exception e) {
             if (server != null) {
                 server.terminate();
--- a/test/jdk/java/net/Authenticator/B4759514.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B4759514.java	Mon Jun 17 20:03:34 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
@@ -25,14 +25,16 @@
  * @test
  * @bug 4759514
  * @modules java.base/sun.net.www
- * @library ../../../sun/net/www/httptest/
+ * @library ../../../sun/net/www/httptest/ /test/lib
  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
- * @run main B4759514
+ * @run main/othervm B4759514
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B4759514
  * @summary Digest Authentication is erroniously quoting the nc value, contrary to RFC 2617
  */
 
 import java.io.*;
 import java.net.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class B4759514 implements HttpCallback {
 
@@ -97,10 +99,19 @@
     public static void main (String[] args) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
+        ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
         try {
-            server = new TestHttpServer (new B4759514(), 1, 10, 0);
-            System.out.println ("Server: listening on port: " + server.getLocalPort());
-            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            server = new TestHttpServer (new B4759514(), 1, 10, loopback, 0);
+            String serverURL = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .build()
+                .toString();
+            System.out.println("Server: listening at: " + serverURL);
+            client(serverURL + "d1/foo.html");
         } catch (Exception e) {
             if (server != null) {
                 server.terminate();
--- a/test/jdk/java/net/Authenticator/B4769350.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B4769350.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2016, 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
@@ -27,6 +27,8 @@
  * @modules jdk.httpserver
  * @run main/othervm B4769350 server
  * @run main/othervm B4769350 proxy
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B4769350 server
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B4769350 proxy
  * @summary proxy authentication username and password caching only works in serial case
  * Run in othervm since the test sets system properties that are read by the
  * networking stack and cached when the HTTP handler is invoked, and previous
@@ -99,7 +101,8 @@
         }
 
         public void startServer() {
-            InetSocketAddress addr = new InetSocketAddress(0);
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            InetSocketAddress addr = new InetSocketAddress(loopback, 0);
 
             try {
                 server = HttpServer.create(addr, 0);
@@ -456,17 +459,28 @@
             System.out.println ("Server: listening on port: "
                     + server.getPort());
             if (proxy) {
-                System.setProperty ("http.proxyHost", "localhost");
+                System.setProperty ("http.proxyHost",
+                        InetAddress.getLoopbackAddress().getHostAddress());
                 System.setProperty ("http.proxyPort",
                         Integer.toString(server.getPort()));
                 doProxyTests ("www.foo.com", server);
             } else {
-                doServerTests ("localhost:"+server.getPort(), server);
+                ProxySelector.setDefault(ProxySelector.of(null));
+                doServerTests (authority(server.getPort()), server);
             }
         }
 
     }
 
+    static String authority(int port) {
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        String hoststr = loopback.getHostAddress();
+        if (hoststr.indexOf(':') > -1) {
+            hoststr = "[" + hoststr + "]";
+        }
+        return hoststr + ":" + port;
+    }
+
     public static void except (String s, Server server) {
         server.close();
         throw new RuntimeException (s);
@@ -496,4 +510,3 @@
         }
     }
 }
-
--- a/test/jdk/java/net/Authenticator/B4921848.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B4921848.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, 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
@@ -25,14 +25,17 @@
  * @test
  * @bug 4921848
  * @modules java.base/sun.net.www
- * @library ../../../sun/net/www/httptest/
+ * @library ../../../sun/net/www/httptest/ /test/lib
  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
  * @run main/othervm -Dhttp.auth.preference=basic B4921848
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true
+ *                   -Dhttp.auth.preference=basic B4921848
  * @summary Allow user control over authentication schemes
  */
 
 import java.io.*;
 import java.net.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class B4921848 implements HttpCallback {
 
@@ -88,10 +91,19 @@
     public static void main (String[] args) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
+        ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
         try {
-            server = new TestHttpServer (new B4921848(), 1, 10, 0);
-            System.out.println ("Server started: listening on port: " + server.getLocalPort());
-            client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            server = new TestHttpServer (new B4921848(), 1, 10, loopback, 0);
+            String serverURL = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .build()
+                .toString();
+            System.out.println("Server: listening at: " + serverURL);
+            client(serverURL + "d1/d2/d3/foo.html");
         } catch (Exception e) {
             if (server != null) {
                 server.terminate();
--- a/test/jdk/java/net/Authenticator/B4933582.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B4933582.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2016, 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
@@ -21,10 +21,15 @@
  * questions.
  */
 
+// Note: this test saves a cache.ser file in the scratch directory,
+//       which the cache implementation will load its configuration
+//       from. Therefore adding several @run lines does not work.
+
 /*
  * @test
  * @bug 4933582
- * @library ../../../sun/net/www/httptest
+ * @key intermittent
+ * @library ../../../sun/net/www/httptest /test/lib
  * @modules java.base/sun.net.www
  *          java.base/sun.net.www.protocol.http
  * @build HttpCallback HttpTransaction TestHttpServer B4933582
@@ -34,6 +39,7 @@
 import java.net.*;
 import java.util.*;
 import sun.net.www.protocol.http.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class B4933582 implements HttpCallback {
 
@@ -133,12 +139,21 @@
     public static void main (String[] args) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
+        ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
+        InetAddress loopback = InetAddress.getLoopbackAddress();
         CacheImpl cache;
         try {
-            server = new TestHttpServer (new B4933582(), 1, 10, 0);
+            server = new TestHttpServer(new B4933582(), 1, 10, loopback, 0);
             cache = new CacheImpl (server.getLocalPort());
             AuthCacheValue.setAuthCache (cache);
-            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
+            String serverURL = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .build()
+                .toString();
+            client(serverURL + "d1/foo.html");
         } finally {
             if (server != null) {
                 server.terminate();
@@ -157,7 +172,7 @@
         while (true) {
             try {
                 server = new TestHttpServer(new B4933582(), 1, 10,
-                        cache.getPort());
+                                            loopback, cache.getPort());
                 break;
             } catch (BindException e) {
                 if (retries++ < 5) {
@@ -173,7 +188,14 @@
 
         try {
             AuthCacheValue.setAuthCache(cache);
-            client("http://localhost:" + server.getLocalPort() + "/d1/foo.html");
+            String serverURL = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(server.getLocalPort())
+                .path("/")
+                .build()
+                .toString();
+            client(serverURL + "d1/foo.html");
         } finally {
             if (server != null) {
                 server.terminate();
--- a/test/jdk/java/net/Authenticator/B4962064.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B4962064.java	Mon Jun 17 20:03:34 2019 +0100
@@ -28,6 +28,7 @@
  * @library ../../../sun/net/www/httptest/
  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
  * @run main/othervm B4962064
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B4962064
  * @summary Extend Authenticator to provide access to request URI and server/proxy
  */
 
@@ -91,18 +92,24 @@
 
     public static void main (String[] args) throws Exception {
         try {
-            server = new TestHttpServer (new B4962064(), 1, 10, 0);
+            InetAddress address = InetAddress.getLoopbackAddress();
+            InetAddress resolved = InetAddress.getByName(address.getHostName());
+            System.out.println("Lookup: " + address + " -> \""
+                               + address.getHostName() + "\" -> "
+                               + resolved);
+            server = new TestHttpServer (new B4962064(), 1, 10, address, 0);
             int port = server.getLocalPort();
-            System.setProperty ("http.proxyHost", "localhost");
+            String proxyHost = address.equals(resolved)
+                ? address.getHostName()
+                : address.getHostAddress();
+            System.setProperty ("http.proxyHost", proxyHost);
             System.setProperty ("http.proxyPort", Integer.toString (port));
             MyAuthenticator auth = new MyAuthenticator ();
             Authenticator.setDefault (auth);
             System.out.println ("Server started: listening on port: " + port);
-            //String s = new String ("http://localhost:"+port+"/d1/d2/d3/foo.html");
             String s = new String ("http://foo.com/d1/d2/d3/foo.html");
             urlsave = new URL (s);
             client (s);
-            //s = new String ("http://localhost:"+port+"/dr/d3/foo.html");
             s = new String ("http://bar.com/dr/d3/foo.html");
             urlsave = new URL (s);
             client (s);
--- a/test/jdk/java/net/Authenticator/B6870935.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B6870935.java	Mon Jun 17 20:03:34 2019 +0100
@@ -26,6 +26,8 @@
  * @bug 6870935
  * @modules java.base/sun.net.www
  * @run main/othervm -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true
+ *                   -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
  */
 
 import java.io.*;
@@ -80,18 +82,21 @@
 
         public void run () {
             try {
+                System.out.println("Server started");
                 Socket s1 = s.accept ();
                 is = s1.getInputStream ();
                 os = s1.getOutputStream ();
                 is.read ();
                 os.write (reply1.getBytes());
+                System.out.println("First response sent");
                 Thread.sleep (2000);
                 s1.close ();
+                System.out.println("First connection closed");
 
                 s1 = s.accept ();
                 is = s1.getInputStream ();
                 os = s1.getOutputStream ();
-                is.read ();
+                // is.read ();
                 // need to get the cnonce out of the response
                 MessageHeader header = new MessageHeader (is);
                 String raw = header.findValue ("Proxy-Authorization");
@@ -115,12 +120,16 @@
                         cnstring, passwd, username
                 ) +"\r\n";
                 os.write (reply.getBytes());
+                System.out.println("Second response sent");
                 Thread.sleep (2000);
                 s1.close ();
+                System.out.println("Second connection closed");
             }
             catch (Exception e) {
                 System.out.println (e);
                 e.printStackTrace();
+            } finally {
+                System.out.println("Server finished");
             }
         }
 
@@ -225,8 +234,17 @@
         DigestServer server;
         ServerSocket sock;
 
+        InetAddress address = InetAddress.getLoopbackAddress();
+        InetAddress resolved = InetAddress.getByName(address.getHostName());
+        System.out.println("Lookup: "
+                            + address + " -> \"" + address.getHostName() + "\" -> "
+                            + resolved);
+        String proxyHost = address.equals(resolved)
+            ? address.getHostName()
+            : address.getHostAddress();
         try {
-            sock = new ServerSocket (0);
+            sock = new ServerSocket();
+            sock.bind(new InetSocketAddress(address, 0));
             port = sock.getLocalPort ();
         }
         catch (Exception e) {
@@ -238,12 +256,12 @@
         server.start ();
 
         try  {
-
             Authenticator.setDefault (new MyAuthenticator ());
-            SocketAddress addr = new InetSocketAddress (InetAddress.getLoopbackAddress(), port);
+            SocketAddress addr = InetSocketAddress.createUnresolved(proxyHost, port);
             Proxy proxy = new Proxy (Proxy.Type.HTTP, addr);
             String s = "http://www.ibm.com";
             URL url = new URL(s);
+            System.out.println("opening connection through proxy: " + addr);
             java.net.URLConnection conURL =  url.openConnection(proxy);
 
             InputStream in = conURL.getInputStream();
@@ -255,6 +273,9 @@
         catch(IOException e) {
             e.printStackTrace();
             error = true;
+            sock.close();
+        } finally {
+            server.join();
         }
         if (error) {
             throw new RuntimeException ("Error in test");
--- a/test/jdk/java/net/Authenticator/B8034170.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/B8034170.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 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,13 +24,16 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * @test
  * @bug 8034170
  * @summary Digest authentication interop issue
+ * @library /test/lib
  * @run main/othervm B8034170 unquoted
  * @run main/othervm -Dhttp.auth.digest.quoteParameters=true B8034170 quoted
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B8034170 unquoted
  */
 
 public class B8034170 {
@@ -176,14 +179,21 @@
 
         MyAuthenticator3 auth = new MyAuthenticator3 ();
         Authenticator.setDefault (auth);
-        ServerSocket ss = new ServerSocket (0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         int port = ss.getLocalPort ();
         BasicServer server = new BasicServer (ss);
         synchronized (server) {
             server.start();
             System.out.println ("client 1");
-            URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
-            URLConnection urlc = url.openConnection ();
+            URL url =  URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path("/d1/d2/d3/foo.html")
+                .toURL();
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
             InputStream is = urlc.getInputStream ();
             read (is);
             is.close ();
--- a/test/jdk/java/net/Authenticator/BasicTest.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/BasicTest.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2002, 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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,11 +24,15 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * @test
  * @bug 4474947
  * @summary  fix for bug #4244472 is incomplete - HTTP authorization still needs work
+ * @library /test/lib
+ * @run main/othervm BasicTest
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest
  */
 
 /*
@@ -151,19 +155,28 @@
     public static void main (String args[]) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
-        ServerSocket ss = new ServerSocket (0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         int port = ss.getLocalPort ();
         BasicServer server = new BasicServer (ss);
         synchronized (server) {
             server.start();
             System.out.println ("client 1");
-            URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
-            URLConnection urlc = url.openConnection ();
+            String base = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path("/")
+                .build()
+                .toString();
+            URL url = new URL(base + "d1/d2/d3/foo.html");
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
             InputStream is = urlc.getInputStream ();
             read (is);
             System.out.println ("client 2");
-            url = new URL ("http://localhost:"+port+"/d1/foo.html");
-            urlc = url.openConnection ();
+            url = new URL(base + "d1/foo.html");
+            urlc = url.openConnection(Proxy.NO_PROXY);
             is = urlc.getInputStream ();
             read (is);
             server.wait ();
--- a/test/jdk/java/net/Authenticator/BasicTest3.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/BasicTest3.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 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
@@ -24,11 +24,15 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * @test
  * @bug 4513440
  * @summary BasicAuthentication is zeroing out the given password
+ * @library /test/lib
+ * @run main/othervm BasicTest3
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest3
  */
 
 public class BasicTest3 {
@@ -130,14 +134,21 @@
     public static void main (String args[]) throws Exception {
         MyAuthenticator3 auth = new MyAuthenticator3 ();
         Authenticator.setDefault (auth);
-        ServerSocket ss = new ServerSocket (0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         int port = ss.getLocalPort ();
         BasicServer3 server = new BasicServer3 (ss);
         synchronized (server) {
             server.start();
             System.out.println ("client 1");
-            URL url = new URL ("http://localhost:"+port+"/d1/d2/d3/foo.html");
-            URLConnection urlc = url.openConnection ();
+            URL url =  URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path("/d1/d2/d3/foo.html")
+                .toURL();
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
             InputStream is = urlc.getInputStream ();
             read (is);
             is.close ();
--- a/test/jdk/java/net/Authenticator/BasicTest4.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/BasicTest4.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 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
@@ -24,11 +24,15 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * @test
  * @bug 4623722
  * @summary  performance hit for Basic Authentication
+ * @library /test/lib
+ * @run main/othervm BasicTest4
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest4
  */
 
 public class BasicTest4 {
@@ -59,12 +63,17 @@
 
         static boolean checkFor (InputStream in, char[] seq) throws IOException {
             System.out.println ("checkfor");
+            StringBuilder message = new StringBuilder();
             try {
                 int i=0, count=0;
                 while (true) {
                     int c = in.read();
-                    if (c == -1)
+                    if (c == -1) {
+                        System.out.println(new String(seq) + " not found in \n<<"
+                                           + message + ">>");
                         return false;
+                    }
+                    message.append((char)c);
                     count++;
                     if (c == seq[i]) {
                         i++;
@@ -77,6 +86,7 @@
                 }
             }
             catch (SocketTimeoutException e) {
+                System.out.println("checkFor: " + e);
                 return false;
             }
         }
@@ -194,23 +204,33 @@
     public static void main (String args[]) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
-        ServerSocket ss = new ServerSocket (0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         int port = ss.getLocalPort ();
         BasicServer server = new BasicServer (ss);
         synchronized (server) {
             server.start();
             System.out.println ("client 1");
-            URL url = new URL ("http://localhost:"+port+"/d1/d3/foo.html");
-            URLConnection urlc = url.openConnection ();
+            String base = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path("/d1/")
+                .build()
+                .toString();
+            System.out.println("Base URL: " + base);
+            URL url = new URL (base + "d3/foo.html");
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
             InputStream is = urlc.getInputStream ();
             read (is);
             System.out.println ("client 2");
-            url = new URL ("http://localhost:"+port+"/d1/d2/bar.html");
-            urlc = url.openConnection ();
+            url = new URL (base + "d2/bar.html");
+            urlc = url.openConnection(Proxy.NO_PROXY);
             is = urlc.getInputStream ();
             System.out.println ("client 3");
-            url = new URL ("http://localhost:"+port+"/d1/d4/foobar.html");
-            urlc = url.openConnection ();
+            url = new URL (base + "d4/foobar.html");
+            urlc = url.openConnection(Proxy.NO_PROXY);
             is = urlc.getInputStream ();
             read (is);
             server.wait ();
--- a/test/jdk/java/net/Authenticator/Deadlock.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/Authenticator/Deadlock.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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,7 +25,10 @@
  * @test
  * @bug 6648001
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main/othervm/timeout=20 -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock
+ * @run main/othervm/timeout=20 -Djava.net.preferIPv6Addresses=true
+ *                              -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock
  * @summary  cancelling HTTP authentication causes deadlock
  */
 
@@ -34,8 +37,10 @@
 import java.io.InputStream;
 import java.io.IOException;
 import java.net.HttpURLConnection;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.PasswordAuthentication;
+import java.net.Proxy;
 import java.net.URL;
 import com.sun.net.httpserver.BasicAuthenticator;
 import com.sun.net.httpserver.Headers;
@@ -44,12 +49,14 @@
 import com.sun.net.httpserver.HttpHandler;
 import com.sun.net.httpserver.HttpPrincipal;
 import com.sun.net.httpserver.HttpServer;
+import jdk.test.lib.net.URIBuilder;
 
 public class Deadlock {
 
     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);
         BasicAuthenticator a = new BasicAuthenticator("foobar@test.realm") {
@@ -97,8 +104,13 @@
             URL url;
             HttpURLConnection urlc;
             try {
-                url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
-                urlc = (HttpURLConnection)url.openConnection ();
+                url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(server.getAddress().getPort())
+                    .path("/test/foo.html")
+                    .toURLUnchecked();
+                urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
             } catch (IOException e) {
                 error = true;
                 return;
--- a/test/jdk/java/net/CookieHandler/CookieHandlerTest.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/CookieHandler/CookieHandlerTest.java	Mon Jun 17 20:03:34 2019 +0100
@@ -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,7 +24,9 @@
 /* @test
  * @summary Unit test for java.net.CookieHandler
  * @bug 4696506
+ * @library /test/lib
  * @run main/othervm CookieHandlerTest
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true CookieHandlerTest
  * @author Yingxian Wang
  */
 
@@ -34,6 +36,7 @@
 import java.net.*;
 import java.util.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class CookieHandlerTest implements Runnable {
     static Map<String,String> cookies;
@@ -92,15 +95,19 @@
     CookieHandlerTest() 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 */
-        String uri = "http://localhost:" +
-                     Integer.toString(ss.getLocalPort());
-        URL url = new URL(uri);
+        URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(ss.getLocalPort())
+                    .toURL();
 
-        HttpURLConnection http = (HttpURLConnection)url.openConnection();
+        HttpURLConnection http = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
 
         int respCode = http.getResponseCode();
         http.disconnect();
--- a/test/jdk/java/net/CookieHandler/CookieManagerTest.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/CookieHandler/CookieManagerTest.java	Mon Jun 17 20:03:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2013, 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
@@ -57,8 +57,9 @@
         } catch (IOException x) {
             System.out.println("Debug: caught:" + x);
         }
-        System.out.println("Using: \"127.0.0.1\"");
-        return "127.0.0.1";
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        System.out.println("Using: \"" + loopback.getHostAddress() + "\"");
+        return loopback.getHostAddress();
     }
 
     public static void main(String[] args) throws Exception {
@@ -73,7 +74,7 @@
 
    public static void startHttpServer() throws IOException {
         httpTrans = new CookieTransactionHandler();
-        server = HttpServer.create(new InetSocketAddress(0), 0);
+        server = HttpServer.create(new InetSocketAddress(hostAddress, 0), 0);
         server.createContext("/", httpTrans);
         server.start();
     }
--- a/test/jdk/java/net/CookieHandler/EmptyCookieHeader.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/CookieHandler/EmptyCookieHeader.java	Mon Jun 17 20:03:34 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
@@ -26,6 +26,9 @@
  * @bug 8015799
  * @modules jdk.httpserver
  * @summary HttpURLConnection.getHeaderFields() throws IllegalArgumentException
+ * @library /test/lib
+ * @run main EmptyCookieHeader
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true EmptyCookieHeader
  */
 
 import com.sun.net.httpserver.*;
@@ -33,6 +36,7 @@
 import java.io.OutputStream;
 import java.net.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class EmptyCookieHeader {
 
@@ -43,11 +47,17 @@
     public void runTest() throws Exception {
         final CookieHandler oldHandler = CookieHandler.getDefault();
         CookieHandler.setDefault(new TestCookieHandler());
-        HttpServer s = HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
         try {
             startServer(s);
-            URL url = new URL("http://localhost:" + s.getAddress().getPort() + "/");
-            HttpURLConnection c = (HttpURLConnection)url.openConnection();
+            URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(s.getAddress().getPort())
+                    .path("/")
+                    .toURL();
+            HttpURLConnection c = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
             c.getHeaderFields();
         } finally {
             CookieHandler.setDefault(oldHandler);
--- a/test/jdk/java/net/CookieHandler/LocalHostCookie.java	Mon Jun 17 15:31:49 2019 +0800
+++ b/test/jdk/java/net/CookieHandler/LocalHostCookie.java	Mon Jun 17 20:03:34 2019 +0100
@@ -33,8 +33,11 @@
 /*
  * @test
  * @bug 7169142
+ * @key intermittent
  * @modules jdk.httpserver
- * @summary CookieHandler does not work with localhost
+ * @summary CookieHandler does not work with localhost. This requires
+ *    binding to the wildcard address and might fail intermittently
+ *    due to port reuse issues.
  * @run main/othervm LocalHostCookie
  */
 public class LocalHostCookie {
@@ -126,4 +129,3 @@
         }
     }
 }
-