8223856: Replace wildcard address with loopback or local host in tests - part 8
authordfuchs
Mon, 20 May 2019 12:37:40 +0100
changeset 54938 8c977741c3c8
parent 54937 7e5e0b326ed7
child 54939 bafd6c944db4
8223856: Replace wildcard address with loopback or local host in tests - part 8 Summary: Fixes some intermittent test failures by replacing wildcard with loopback - or retrying once. Reviewed-by: aefimov, chegar
test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java
test/jdk/java/net/BindException/Test.java
test/jdk/java/net/PlainSocketImpl/SetOption.java
test/jdk/java/net/Socket/RST.java
test/jdk/java/net/URLConnection/URLConnectionHeaders.java
test/jdk/java/net/ipv6tests/UdpTest.java
test/jdk/sun/net/ftp/B6427768.java
test/jdk/sun/net/www/ftptest/FtpCommandHandler.java
test/jdk/sun/net/www/ftptest/FtpServer.java
test/jdk/sun/net/www/http/HttpClient/RetryPost.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHttpsClientTest.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Redirect.java
--- a/test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/com/sun/net/httpserver/SimpleHttpServerTest.java	Mon May 20 12:37:40 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,8 +24,12 @@
 /**
  * @test
  * @bug 8015692
+ * @key intermittent
  * @summary  Test HttpServer instantiation, start, and stop repeated in a loop
- *           Testing for Bind exception on Windows
+ *           Testing for Bind exception on Windows. This test may fail
+ *           intermittently if other tests / process manage to bind to
+ *           the same port that the test is using in the short window
+ *           time where the port might appear available again.
  */
 
 import java.net.InetSocketAddress;
@@ -41,24 +45,40 @@
         System.out.println(System.getProperty("java.version"));
         InetSocketAddress serverAddr = new InetSocketAddress(0);
         HttpServer server = HttpServer.create(serverAddr, 0);
-        final int serverPort = server.getAddress().getPort();
+        int serverPort = server.getAddress().getPort();
         server.start();
         server.stop(0);
         serverAddr = new InetSocketAddress(serverPort);
         int exceptionCount = 0;
+        boolean failedOnce = false;
         System.out.println("Using serverPort == " + serverPort);
-        for (int i = 0; i < 100; i++) {
-            try {
-                server = HttpServer.create(serverAddr, 0);
-                server.start();
-                server.stop(0);
-            } catch (Exception ex) {
-                ex.printStackTrace();
-                exceptionCount++;
+        RETRY: while (exceptionCount == 0) {
+            for (int i = 0; i < 100; i++) {
+                try {
+                    server = HttpServer.create(serverAddr, 0);
+                    server.start();
+                    server.stop(0);
+                } catch (Exception ex) {
+                    if (!failedOnce) {
+                        failedOnce = true;
+                        server = HttpServer.create(new InetSocketAddress(0), 0);
+                        serverPort = server.getAddress().getPort();
+                        server.start();
+                        server.stop(0);
+                        serverAddr = new InetSocketAddress(serverPort);
+                        System.out.println("Retrying with serverPort == " + serverPort);
+                        continue RETRY;
+                    }
+                    System.err.println("Got exception at iteration: " + i );
+                    ex.printStackTrace();
+                    exceptionCount++;
+                }
             }
+            break;
         }
         if (exceptionCount > 0) {
-           throw new RuntimeException("Test Failed");
+           throw new RuntimeException("Test Failed: got "
+                 + exceptionCount + " exceptions.");
         }
     }
 }
--- a/test/jdk/java/net/BindException/Test.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/java/net/BindException/Test.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2016, 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
@@ -51,18 +51,24 @@
 
     static int count;
     static int failures;
+    static boolean retried;
 
     static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
                        boolean silent) throws Exception {
-        String s1_type = (String)test[0];
-        String s2_type = (String)test[1];
-        int port = 0;
-
         /*
          * Increment test count
          */
         count++;
 
+        doTest(test, count, ia1, ia2, silent, !retried);
+    }
+
+    static void doTest(Object test[], int count, InetAddress ia1, InetAddress ia2,
+                       boolean silent, boolean retry) throws Exception {
+        String s1_type = (String)test[0];
+        String s2_type = (String)test[1];
+        int port = 0;
+
         /*
          * Do the test
          */
@@ -74,6 +80,8 @@
         Socket sock1 = null;
         ServerSocket ss = null;
         DatagramSocket dsock1 = null;
+        boolean firstBound = false;
+
         try {
             /* bind the first socket */
 
@@ -95,6 +103,13 @@
 
             /* bind the second socket */
 
+            // The fact that the port was available for ia1 does not
+            // guarantee that it will also be available for ia2 as something
+            // else might already be bound to that port.
+            // For the sake of test stability we will retry once in
+            // case of unexpected bind exception.
+
+            firstBound = true;
             if (s2_type.equals("Socket")) {
                 try (Socket sock2 = new Socket()) {
                     sock2.bind( new InetSocketAddress(ia2, port));
@@ -148,6 +163,18 @@
             return;
         }
 
+        if (failed && retry && firstBound) {
+            // retry once at the first failure only
+            retried = true;
+            if (!silent) {
+                System.out.println("");
+                System.out.println("**************************");
+                System.out.println("Test " + count + ": Retrying...");
+            }
+            doTest(test, count, ia1, ia2, silent, false);
+            return;
+        }
+
         if (failed || !silent) {
             System.out.println("");
             System.out.println("**************************");
--- a/test/jdk/java/net/PlainSocketImpl/SetOption.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/java/net/PlainSocketImpl/SetOption.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 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
@@ -35,8 +35,10 @@
 
     public static void main(String args[]) throws Exception {
 
-        ServerSocket ss = new ServerSocket(0);
-        Socket s1 = new Socket("localhost", ss.getLocalPort());
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket(0, 0, loopback);
+
+        Socket s1 = new Socket(loopback, ss.getLocalPort());
         Socket s2 = ss.accept();
 
         s1.close();
--- a/test/jdk/java/net/Socket/RST.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/java/net/Socket/RST.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,8 +47,10 @@
     }
 
     RST() throws Exception {
-        ServerSocket ss = new ServerSocket(0);
-        client = new Socket("localhost", ss.getLocalPort());
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
+        client = new Socket(loopback, ss.getLocalPort());
         Socket server = ss.accept();
 
         Thread thr = new Thread(this);
--- a/test/jdk/java/net/URLConnection/URLConnectionHeaders.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/java/net/URLConnection/URLConnectionHeaders.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2010, 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
@@ -27,12 +27,14 @@
  * @summary URLConnection cannot enumerate request properties,
  *          and URLConnection can neither get nor set multiple
  *          request properties w/ same key
+ * @library /test/lib
  *
  */
 
 import java.net.*;
 import java.util.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class URLConnectionHeaders {
 
@@ -77,15 +79,22 @@
         }
     }
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         try {
-            ServerSocket serversocket = new ServerSocket (0);
-            int port = serversocket.getLocalPort ();
-            XServer server = new XServer (serversocket);
-            server.start ();
-            Thread.sleep (200);
-            URL url = new URL ("http://localhost:"+port+"/index.html");
-            URLConnection uc = url.openConnection ();
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            ServerSocket serversocket = new ServerSocket();
+            serversocket.bind(new InetSocketAddress(loopback, 0));
+            int port = serversocket.getLocalPort();
+            XServer server = new XServer(serversocket);
+            server.start();
+            Thread.sleep(200);
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .loopback()
+                      .port(port)
+                      .path("/index.html")
+                      .toURL();
+            URLConnection uc = url.openConnection();
 
             // add request properties
             uc.addRequestProperty("Cookie", "cookie1");
@@ -106,6 +115,7 @@
 
         } catch (Exception e) {
             e.printStackTrace();
+            throw e;
         }
     }
 }
--- a/test/jdk/java/net/ipv6tests/UdpTest.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/java/net/ipv6tests/UdpTest.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2018, 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,11 +24,14 @@
 /*
  * @test
  * @bug 4868820
- * @summary IPv6 support for Windows XP and 2003 server
+ * @key intermittent
+ * @summary IPv6 support for Windows XP and 2003 server.
+ *          This test requires binding to the wildcard address and as such
+ *          may fail intermittently on some platforms.
  * @library /test/lib
  * @build jdk.test.lib.NetworkConfiguration
  *        jdk.test.lib.Platform
- * @run main UdpTest
+ * @run main UdpTest -d
  */
 
 import java.net.DatagramPacket;
@@ -92,6 +95,7 @@
     /* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */
 
     static void test1 () throws Exception {
+        System.out.println("Test1 starting");
         s1 = new DatagramSocket ();
         s2 = new DatagramSocket ();
         simpleDataExchange (s1, ia4addr, s2, ia4addr);
@@ -130,6 +134,7 @@
     /* check timeouts on receive */
 
     static void test2 () throws Exception {
+        System.out.println("Test2 starting");
         s1 = new DatagramSocket ();
         s2 = new DatagramSocket ();
         s1.setSoTimeout (4000);
@@ -180,6 +185,7 @@
     /* check connected sockets */
 
     static void test3 () throws Exception {
+        System.out.println("Test3 starting");
         s1 = new DatagramSocket ();
         s2 = new DatagramSocket ();
         s1.connect (ia6addr, s2.getLocalPort());
@@ -191,6 +197,7 @@
     /* check PortUnreachable */
 
     static void test4 () throws Exception {
+        System.out.println("Test4 starting");
         s1 = new DatagramSocket ();
         s1.connect (ia6addr, 5000);
         s1.setSoTimeout (3000);
--- a/test/jdk/sun/net/ftp/B6427768.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/ftp/B6427768.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -105,13 +105,14 @@
     }
 
     public static void main(String[] args) throws IOException {
-        FtpServer server = new FtpServer(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        FtpServer server = new FtpServer(loopback, 0);
         int port = server.getLocalPort();
         server.setFileSystemHandler(new MyFileSystemHandler("/"));
         server.setAuthHandler(new MyAuthHandler());
         server.start();
-        URL url = new URL("ftp://user:passwd@localhost:" + port + "/foo.txt");
-        URLConnection con = url.openConnection();
+        URL url = new URL("ftp://user:passwd@" + server.getAuthority() + "/foo.txt");
+        URLConnection con = url.openConnection(Proxy.NO_PROXY);
         // triggers the connection
         try {
             con.getInputStream();
--- a/test/jdk/sun/net/www/ftptest/FtpCommandHandler.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/www/ftptest/FtpCommandHandler.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -238,14 +238,14 @@
             return;
         }
         try {
-            if (pasv == null)
-                pasv = new ServerSocket(0);
-            int port = pasv.getLocalPort();
             InetAddress rAddress = cmd.getLocalAddress();
             if (rAddress instanceof Inet6Address) {
                 out.println("500 PASV illegal over IPv6 addresses, use EPSV.");
                 return;
             }
+            if (pasv == null)
+                pasv = new ServerSocket(0, 0, rAddress);
+            int port = pasv.getLocalPort();
             byte[] a = rAddress.getAddress();
             out.println("227 Entering Passive Mode " + a[0] + "," + a[1] + "," + a[2] + "," + a[3] + "," +
                         (port >> 8) + "," + (port & 0xff) );
@@ -266,7 +266,7 @@
         }
         try {
             if (pasv == null)
-                pasv = new ServerSocket(0);
+                pasv = new ServerSocket(0, 0, parent.getInetAddress());
             int port = pasv.getLocalPort();
             out.println("229 Entering Extended Passive Mode (|||" + port + "|)");
         } catch (IOException e) {
--- a/test/jdk/sun/net/www/ftptest/FtpServer.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/www/ftptest/FtpServer.java	Mon May 20 12:37:40 2019 +0100
@@ -110,8 +110,12 @@
         return listener.getLocalPort();
     }
 
+    public InetAddress getInetAddress() {
+        return listener.getInetAddress();
+    }
+
     public String getAuthority() {
-        InetAddress address = listener.getInetAddress();
+        InetAddress address = getInetAddress();
         String hostaddr = address.isAnyLocalAddress()
             ? "localhost" : address.getHostAddress();
         if (hostaddr.indexOf(':') > -1) {
--- a/test/jdk/sun/net/www/http/HttpClient/RetryPost.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpClient/RetryPost.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
  * @test
  * @bug 6427251 6382788
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main RetryPost
  * @run main/othervm -Dsun.net.http.retryPost=false RetryPost noRetry
  * @summary HttpURLConnection automatically retries non-idempotent method POST
@@ -36,12 +37,14 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Proxy;
 import java.net.SocketException;
 import java.net.URL;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
+import jdk.test.lib.net.URIBuilder;
 
 public class RetryPost
 {
@@ -70,7 +73,12 @@
     void doClient() {
         try {
             InetSocketAddress address = httpServer.getAddress();
-            URL url = new URL("http://localhost:" + address.getPort() + "/test/");
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .host(address.getAddress())
+                      .port(address.getPort())
+                      .path("/test/")
+                      .toURLUnchecked();
             HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
             uc.setDoOutput(true);
             uc.setRequestMethod("POST");
@@ -99,7 +107,8 @@
      * Http Server
      */
     public void startHttpServer(boolean shouldRetry) throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(loopback, 0), 0);
         httpHandler = new MyHandler(shouldRetry);
 
         HttpContext ctx = httpServer.createContext("/test/", httpHandler);
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHttpsClientTest.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHttpsClientTest.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, 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
@@ -29,21 +29,25 @@
  * @bug 7129083
  * @summary Cookiemanager does not store cookies if url is read
  *          before setting cookiemanager
+ * @library /test/lib
  * @run main/othervm CookieHttpsClientTest
  */
 
 import java.net.CookieHandler;
 import java.net.CookieManager;
 import java.net.CookiePolicy;
+import java.net.InetAddress;
 import java.net.URL;
 import java.io.InputStream;
 import java.io.IOException;
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLHandshakeException;
 import javax.net.ssl.SSLServerSocket;
 import javax.net.ssl.SSLServerSocketFactory;
 import javax.net.ssl.SSLSession;
 import javax.net.ssl.SSLSocket;
+import jdk.test.lib.net.URIBuilder;
 
 public class CookieHttpsClientTest {
     static final int TIMEOUT = 10 * 1000;
@@ -91,10 +95,11 @@
      * 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(serverPort, 0, loopback);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -137,10 +142,17 @@
                 return true;
             }});
 
-        URL url = new URL("https://localhost:" + serverPort +"/");
+        URL url = URIBuilder.newBuilder()
+                  .scheme("https")
+                  .loopback()
+                  .port(serverPort)
+                  .path("/")
+                  .toURL();
+
+        System.out.println("Client ready to connect to: " + url);
 
         // Run without a CookieHandler first
-        InputStream in = url.openConnection().getInputStream();
+        InputStream in = url.openConnection(java.net.Proxy.NO_PROXY).getInputStream();
         while (in.read() != -1);  // read response body so connection can be reused
 
         // Set a CookeHandler and retest using the HttpClient from the KAC
@@ -183,6 +195,10 @@
     volatile Exception serverException = null;
     volatile Exception clientException = null;
 
+    private boolean sslConnectionFailed() {
+        return clientException instanceof SSLHandshakeException;
+    }
+
     public static void main(String args[]) throws Exception {
         String keyFilename =
             System.getProperty("test.src", ".") + "/" + pathToStores +
@@ -229,7 +245,11 @@
          */
         if (separateServerThread) {
             if (serverThread != null) {
-                serverThread.join();
+                // don't join the server thread if the
+                // client failed to connect
+                if (!sslConnectionFailed()) {
+                    serverThread.join();
+                }
             }
         } else {
             if (clientThread != null) {
@@ -259,7 +279,7 @@
          */
         if ((local != null) && (remote != null)) {
             // If both failed, return the curthread's exception.
-            local.initCause(remote);
+            local.addSuppressed(remote);
             exception = local;
         } else if (local != null) {
             exception = local;
@@ -274,7 +294,7 @@
          * output it.
          */
         if (exception != null) {
-            if (exception != startException) {
+            if (exception != startException && startException != null) {
                 exception.addSuppressed(startException);
             }
             throw exception;
@@ -323,7 +343,7 @@
                         /*
                          * Our client thread just died.
                          */
-                        System.err.println("Client died...");
+                        System.err.println("Client died: " + e);
                         clientException = e;
                     }
                 }
@@ -333,6 +353,7 @@
             try {
                 doClientSide();
             } catch (Exception e) {
+                System.err.println("Client died: " + e);
                 clientException = e;
             }
         }
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2018, 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
@@ -32,12 +32,14 @@
  * @summary sun.net.client.defaultConnectTimeout should work with
  *     HttpsURLConnection; HTTP client: Connect and read timeouts;
  *     Https needs to support new tiger features that went into http
+ * @library /test/lib
  * @run main/othervm ReadTimeout
  */
 
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class ReadTimeout {
 
@@ -93,10 +95,11 @@
      * 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(serverPort, 0, loopback);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -163,7 +166,11 @@
             }
             HttpsURLConnection http = null;
             try {
-                URL url = new URL("https://localhost:" + serverPort);
+                URL url = URIBuilder.newBuilder()
+                          .scheme("https")
+                          .loopback()
+                          .port(serverPort)
+                          .toURL();
 
                 // set read timeout through system property
                 System.setProperty("sun.net.client.defaultReadTimeout", "2000");
@@ -184,7 +191,11 @@
             }
 
             try {
-                URL url = new URL("https://localhost:" + serverPort);
+                URL url = URIBuilder.newBuilder()
+                          .scheme("https")
+                          .loopback()
+                          .port(serverPort)
+                          .toURL();
 
                 HttpsURLConnection.setDefaultHostnameVerifier(
                                           new NameVerifier());
@@ -239,6 +250,10 @@
     volatile Exception serverException = null;
     volatile Exception clientException = null;
 
+    private boolean sslConnectionFailed() {
+        return clientException instanceof SSLHandshakeException;
+    }
+
     public static void main(String[] args) throws Exception {
         String keyFilename =
             System.getProperty("test.src", "./") + "/" + pathToStores +
@@ -282,7 +297,9 @@
          * Wait for other side to close down.
          */
         if (separateServerThread) {
-            serverThread.join();
+            if (!sslConnectionFailed()) {
+                serverThread.join();
+            }
         } else {
             clientThread.join();
         }
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Redirect.java	Tue May 14 10:21:55 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Redirect.java	Mon May 20 12:37:40 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2011, 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
@@ -26,6 +26,7 @@
  * @bug 4423074
  * @summary Need to rebase all the duplicated classes from Merlin.
  *          This test will check out http POST
+ * @library /test/lib
  * @run main/othervm Redirect
  *
  *     SunJSSE does not support dynamic system properties, no way to re-use
@@ -35,6 +36,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class Redirect {
 
@@ -95,10 +97,11 @@
      * 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(serverPort, 0, loopback);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -154,7 +157,11 @@
             }
 
             // Send HTTP POST request to server
-            URL url = new URL("https://localhost:"+serverPort);
+            URL url = URIBuilder.newBuilder()
+                      .scheme("https")
+                      .loopback()
+                      .port(serverPort)
+                      .toURL();
 
             HttpsURLConnection.setDefaultHostnameVerifier(
                                           new NameVerifier());
@@ -190,6 +197,10 @@
     volatile Exception serverException = null;
     volatile Exception clientException = null;
 
+    private boolean sslConnectionFailed() {
+        return clientException instanceof SSLHandshakeException;
+    }
+
     public static void main(String[] args) throws Exception {
         String keyFilename =
             System.getProperty("test.src", "./") + "/" + pathToStores +
@@ -233,7 +244,9 @@
          * Wait for other side to close down.
          */
         if (separateServerThread) {
-            serverThread.join();
+            if (!sslConnectionFailed()) {
+                serverThread.join();
+            }
         } else {
             clientThread.join();
         }