8224204: Replace wildcard address with loopback or local host in tests - part 10
authordfuchs
Wed, 22 May 2019 13:58:19 +0100
changeset 54984 8f7d232b4225
parent 54983 81becad91321
child 54985 37d8790efed7
8224204: Replace wildcard address with loopback or local host in tests - part 10 Summary: Fixed intermittently failing tests Reviewed-by: chegar
test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java
test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java
test/jdk/sun/net/www/http/KeepAliveStream/InfiniteLoop.java
test/jdk/sun/net/www/protocol/http/B6369510.java
test/jdk/sun/net/www/protocol/http/BasicLongCredentials.java
test/jdk/sun/net/www/protocol/http/NTLMTest.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsPost.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/RetryHttps.java
--- a/test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java	Wed May 22 13:58:19 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
@@ -33,98 +33,112 @@
 import java.io.FileWriter;
 import java.io.PrintWriter;
 import java.io.BufferedWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
 
 public class DefaultCaching {
 
     public static void main(String args[]) throws Exception {
 
-        String hostsFileName = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
+        String hostsFileNameSrc = System.getProperty("test.src", ".") + "/DefaultCachingHosts";
+        String hostsFileName = System.getProperty("user.dir", ".") + "/DefaultCachingHosts";
+        if (!hostsFileNameSrc.equals(hostsFileName)) {
+            Files.copy(Path.of(hostsFileNameSrc), Path.of(hostsFileName), REPLACE_EXISTING);
+            System.out.println("Host file created: " + hostsFileName);
+        }
         System.setProperty("jdk.net.hosts.file", hostsFileName);
         // initial mapping
         // name service needs to resolve this.
         addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
 
-        test ("theclub", "129.156.220.219", true);      // lk: 1
-        test ("luster", "1.16.20.2", false);            // lk: 2
+        test("theclub", "129.156.220.219", true);      // lk: 1
+        test("luster", "1.16.20.2", false);            // lk: 2
 
         // name service now needs to know about luster
         addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
 
-        test ("luster", "1.16.20.2", false);            // lk: 2
-        sleep (10+1);
+        test("luster", "1.16.20.2", false);            // lk: 2
+        sleep(10+1);
         test("luster", "10.5.18.21", true, 3);          // lk: 3
-        sleep (5);
+        sleep(5);
 
         // new mapping for theclub and rewrite existing foo and luster mappings
         addMappingToHostsFile("theclub", "129.156.220.1", hostsFileName, false);
         addMappingToHostsFile("foo", "10.5.18.22", hostsFileName, true);
         addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
 
-        test ("theclub", "129.156.220.219", true, 3);
-        test ("luster", "10.5.18.21", true, 3);
-        test ("bar", "10.5.18.22", false, 4);
-        test ("foo", "10.5.18.22", true, 5);
+        test("theclub", "129.156.220.219", true, 3);
+        test("luster", "10.5.18.21", true, 3);
+        test("bar", "10.5.18.22", false, 4);
+        test("foo", "10.5.18.22", true, 5);
 
         // now delay to see if theclub has expired
-        sleep (5);
+        sleep(5);
 
-        test ("foo", "10.5.18.22", true, 5);
-        test ("theclub", "129.156.220.1", true, 6);
+        test("foo", "10.5.18.22", true, 5);
+        test("theclub", "129.156.220.1", true, 6);
 
-        sleep (11);
+        sleep(11);
         // now see if luster has expired
-        test ("luster", "10.5.18.21", true, 7);
-        test ("theclub", "129.156.220.1", true, 7);
+        test("luster", "10.5.18.21", true, 7);
+        test("theclub", "129.156.220.1", true, 7);
 
         // now delay to see if 3rd has expired
-        sleep (10+6);
+        sleep(10+6);
 
-        test ("theclub", "129.156.220.1", true, 8);
-        test ("luster", "10.5.18.21", true, 8);
-        test ("foo", "10.5.18.22", true, 9);
+        test("theclub", "129.156.220.1", true, 8);
+        test("luster", "10.5.18.21", true, 8);
+        test("foo", "10.5.18.22", true, 9);
     }
 
     /* throws RuntimeException if it fails */
 
-    static void test (String host, String address,
-                        boolean shouldSucceed, int count) {
-        test (host, address, shouldSucceed);
+    static void test(String host, String address,
+                     boolean shouldSucceed, int count) {
+        test(host, address, shouldSucceed);
     }
 
-    static void sleep (int seconds) {
+    static void sleep(int seconds) {
         try {
-            Thread.sleep (seconds * 1000);
+            Thread.sleep(seconds * 1000);
         } catch (InterruptedException e) {}
     }
 
-    static void test (String host, String address, boolean shouldSucceed) {
+    static void test(String host, String address, boolean shouldSucceed) {
         InetAddress addr = null;
         try {
-            addr = InetAddress.getByName (host);
+            addr = InetAddress.getByName(host);
             if (!shouldSucceed) {
-                throw new RuntimeException (host+":"+address+": should fail");
-
+                throw new RuntimeException(host+":"+address+": should fail (got "
+                                           + addr + ")");
             }
             if (!address.equals(addr.getHostAddress())) {
-                throw new RuntimeException(host+":"+address+": compare failed");
+                throw new RuntimeException(host+":"+address+": compare failed (found "
+                                           + addr + ")");
             }
+            System.out.println("test: " + host + "/" + address
+                               + " succeeded - got " + addr);
         } catch (UnknownHostException e) {
             if (shouldSucceed) {
                 throw new RuntimeException(host+":"+address+": should succeed");
+            } else {
+                System.out.println("test: " + host + "/" + address
+                                   + " succeeded - got expected " + e);
             }
         }
     }
 
 
-    private static void addMappingToHostsFile (String host,
-                                               String addr,
-                                               String hostsFileName,
-                                               boolean append)
+    private static void addMappingToHostsFile(String host,
+                                              String addr,
+                                              String hostsFileName,
+                                              boolean append)
                                              throws Exception {
         String mapping = addr + " " + host;
         try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
                 new FileWriter(hostsFileName, append)))) {
             hfPWriter.println(mapping);
-}
+        }
     }
 }
--- a/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveTimerThread.java	Wed May 22 13:58:19 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2010, 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
@@ -26,6 +26,8 @@
  * @library /test/lib
  * @bug 4701299
  * @summary Keep-Alive-Timer thread management in KeepAliveCache causes memory leak
+ * @run main KeepAliveTimerThread
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true KeepAliveTimerThread
  */
 
 import java.net.*;
@@ -103,8 +105,10 @@
 
 
     public static void main(String args[]) throws Exception {
-        ServerSocket ss = new ServerSocket(0);
-        Server s = new Server (ss);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
+        Server s = new Server(ss);
         s.start();
 
         URL url = URIBuilder.newBuilder()
--- a/test/jdk/sun/net/www/http/KeepAliveStream/InfiniteLoop.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/http/KeepAliveStream/InfiniteLoop.java	Wed May 22 13:58:19 2019 +0100
@@ -26,6 +26,9 @@
  * @bug 8004863
  * @modules jdk.httpserver
  * @summary Checks for proper close code in KeepAliveStream
+ * @library /test/lib
+ * @run main InfiniteLoop
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true InfiniteLoop
  */
 
 import com.sun.net.httpserver.HttpExchange;
@@ -35,10 +38,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.URL;
 import java.util.concurrent.Phaser;
 
+import jdk.test.lib.net.URIBuilder;
+
 // Racey test, will not always fail, but if it does then we have a problem.
 
 public class InfiniteLoop {
@@ -49,11 +56,16 @@
         server.start();
         try {
             InetSocketAddress address = server.getAddress();
-            URL url = new URL("http://localhost:" + address.getPort()
-                              + "/test/InfiniteLoop");
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .host(server.getAddress().getAddress())
+                      .port(server.getAddress().getPort())
+                      .path("/test/InfiniteLoop")
+                      .toURL();
             final Phaser phaser = new Phaser(2);
             for (int i=0; i<10; i++) {
-                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+                HttpURLConnection uc = (HttpURLConnection)
+                    url.openConnection(Proxy.NO_PROXY);
                 final InputStream is = uc.getInputStream();
                 final Thread thread = new Thread() {
                     public void run() {
--- a/test/jdk/sun/net/www/protocol/http/B6369510.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/B6369510.java	Wed May 22 13:58:19 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
@@ -59,11 +59,12 @@
     void doClient() {
         try {
             InetSocketAddress address = httpServer.getAddress();
-            String urlString = "http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/";
+            String urlString = "http://" + InetAddress.getLocalHost().getHostName()
+                               + ":" + address.getPort() + "/test/";
             System.out.println("URL == " + urlString);
 
             // GET Request
-            URL url = new URL("http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/");
+            URL url = new URL(urlString);
             HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
             int resp = uc.getResponseCode();
             if (resp != 200)
@@ -95,7 +96,8 @@
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress localhost = InetAddress.getLocalHost();
+        httpServer = HttpServer.create(new InetSocketAddress(localhost, 0), 0);
 
         // create HttpServer context
         HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
--- a/test/jdk/sun/net/www/protocol/http/BasicLongCredentials.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/BasicLongCredentials.java	Wed May 22 13:58:19 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
@@ -26,6 +26,9 @@
  * @bug 6947917
  * @modules jdk.httpserver
  * @summary  Error in basic authentication when user name and password are long
+ * @library /test/lib
+ * @run main BasicLongCredentials
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true BasicLongCredentials
  */
 
 import com.sun.net.httpserver.BasicAuthenticator;
@@ -37,11 +40,15 @@
 import java.io.InputStream;
 import java.io.IOException;
 import java.net.Authenticator;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.PasswordAuthentication;
+import java.net.Proxy;
 import java.net.HttpURLConnection;
 import java.net.URL;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class BasicLongCredentials {
 
     static final String USERNAME = "ThisIsMyReallyReallyReallyReallyReallyReally" +
@@ -51,7 +58,8 @@
     static final String REALM = "foobar@test.realm";
 
     public static void main (String[] args) throws Exception {
-        HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
         try {
             Handler handler = new Handler();
             HttpContext ctx = server.createContext("/test", handler);
@@ -66,8 +74,13 @@
 
             Authenticator.setDefault(new MyAuthenticator());
 
-            URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
-            HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .host(server.getAddress().getAddress())
+                      .port(server.getAddress().getPort())
+                      .path("/test/")
+                      .toURL();
+            HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
             InputStream is = urlc.getInputStream();
             int c = 0;
             while (is.read()!= -1) { c ++; }
--- a/test/jdk/sun/net/www/protocol/http/NTLMTest.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/NTLMTest.java	Wed May 22 13:58:19 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
  * @test
  * @bug 6520665 6357133
  * @modules java.base/sun.net.www
+ * @library /test/lib
  * @run main/othervm NTLMTest
  * @summary 6520665 & 6357133: NTLM authentication issues.
  */
@@ -32,35 +33,41 @@
 import java.net.*;
 import java.io.*;
 import sun.net.www.MessageHeader;
+import jdk.test.lib.net.URIBuilder;
 
 public class NTLMTest
 {
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         Authenticator.setDefault(new NullAuthenticator());
 
         try {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+
             // Test with direct connection.
-            ServerSocket serverSS = new ServerSocket(0);
-            startServer(serverSS, false);
-            runClient(Proxy.NO_PROXY, serverSS.getLocalPort());
-
+            try (NTLMServer server = startServer(new ServerSocket(0, 0, loopback), false)) {
+                runClient(Proxy.NO_PROXY, server.getLocalPort());
+            }
             // Test with proxy.
-            serverSS = new ServerSocket(0);
-            startServer(serverSS, true /*proxy*/);
-            SocketAddress proxyAddr = new InetSocketAddress("localhost", serverSS.getLocalPort());
-            runClient(new Proxy(java.net.Proxy.Type.HTTP, proxyAddr), 8888);
-
+            try (NTLMServer server =
+                    startServer(new ServerSocket(0, 0, loopback), true /*proxy*/)) {
+                SocketAddress proxyAddr = new InetSocketAddress(loopback, server.getLocalPort());
+                runClient(new Proxy(java.net.Proxy.Type.HTTP, proxyAddr), 8888);
+            }
         } catch (IOException e) {
-            e.printStackTrace();
+            throw e;
         }
     }
 
     static void runClient(Proxy proxy, int serverPort) {
         try {
-            String urlStr = "http://localhost:" + serverPort + "/";
-            URL url = new URL(urlStr);
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .loopback()
+                      .port(serverPort)
+                      .path("/")
+                      .toURLUnchecked();
             HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
-            uc.getInputStream();
+            uc.getInputStream().readAllBytes();
 
         } catch (ProtocolException e) {
             /* java.net.ProtocolException: Server redirected too many  times (20) */
@@ -70,6 +77,7 @@
              * returned HTTP response code: 401 for URL: ..."
              */
             //ioe.printStackTrace();
+            System.out.println("Got expected " + ioe);
         } catch (NullPointerException npe) {
             throw new RuntimeException("Failed: NPE thrown ", npe);
         }
@@ -93,34 +101,56 @@
                 "Content-Length: 0\r\n" +
                 "Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};
 
-    static void startServer(ServerSocket serverSS, boolean proxy) {
-        final ServerSocket ss = serverSS;
-        final boolean isProxy = proxy;
+    static class NTLMServer extends Thread implements AutoCloseable {
+        final ServerSocket ss;
+        final boolean isProxy;
+        volatile boolean closed;
+
+        NTLMServer(ServerSocket serverSS, boolean proxy) {
+            super();
+            setDaemon(true);
+            ss = serverSS;
+            isProxy = proxy;
+        }
+
+       public int getLocalPort() { return ss.getLocalPort(); }
 
-        Thread thread = new Thread(new Runnable() {
-            public void run() {
-                boolean doing2ndStageNTLM = false;
-                while (true) {
-                    try {
-                        Socket s = ss.accept();
-                        if (!doing2ndStageNTLM) {
-                            handleConnection(s, isProxy ? proxyResp : serverResp, 0, 1);
-                            doing2ndStageNTLM = true;
-                        } else {
-                            handleConnection(s, isProxy ? proxyResp : serverResp, 1, 2);
-                            doing2ndStageNTLM = false;
-                        }
-                        connectionCount++;
-                        //System.out.println("connectionCount = " + connectionCount);
+        @Override
+        public void run() {
+            boolean doing2ndStageNTLM = false;
+            while (!closed) {
+                try {
+                    Socket s = ss.accept();
+                    if (!doing2ndStageNTLM) {
+                        handleConnection(s, isProxy ? proxyResp : serverResp, 0, 1);
+                        doing2ndStageNTLM = true;
+                    } else {
+                        handleConnection(s, isProxy ? proxyResp : serverResp, 1, 2);
+                        doing2ndStageNTLM = false;
+                    }
+                    connectionCount++;
+                    //System.out.println("connectionCount = " + connectionCount);
+                } catch (IOException ioe) {
+                    if (!closed) ioe.printStackTrace();
+                }
+            }
+        }
 
-                    } catch (IOException ioe) {
-                        ioe.printStackTrace();
-                    }
-                }
-            } });
-            thread.setDaemon(true);
-            thread.start();
+        @Override
+        public void close() {
+           if (closed) return;
+           synchronized(this) {
+               if (closed) return;
+               closed = true;
+           }
+           try { ss.close(); } catch (IOException x) { };
+        }
+    }
 
+    public static NTLMServer startServer(ServerSocket serverSS, boolean proxy) {
+        NTLMServer server = new NTLMServer(serverSS, proxy);
+        server.start();
+        return server;
     }
 
     static int connectionCount = 0;
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsPost.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsPost.java	Wed May 22 13:58:19 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,7 +26,9 @@
  * @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 HttpsPost
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true HttpsPost
  *
  *     SunJSSE does not support dynamic system properties, no way to re-use
  *     system properties in samevm/agentvm mode.
@@ -35,6 +37,7 @@
 import java.io.*;
 import java.net.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class HttpsPost {
 
@@ -95,12 +98,16 @@
      * 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();
 
+        System.out.println("Starting server at: "
+                            +  sslServerSocket.getInetAddress()
+                            + ":" + serverPort);
         /*
          * Signal Client, we're ready for his connect.
          */
@@ -155,10 +162,15 @@
             }
 
             // Send HTTP POST request to server
-            URL url = new URL("https://localhost:"+serverPort);
+            URL url = URIBuilder.newBuilder()
+                      .scheme("https")
+                      .loopback()
+                      .port(serverPort)
+                      .toURL();
 
+            System.out.println("Client connecting to: " + url);
             HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
-            HttpsURLConnection http = (HttpsURLConnection)url.openConnection();
+            HttpsURLConnection http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
             http.setDoOutput(true);
 
             http.setRequestMethod("POST");
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java	Wed May 22 13:58:19 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
@@ -27,8 +27,7 @@
 import javax.net.*;
 import javax.net.ssl.*;
 
-import jdk.test.lib.process.OutputAnalyzer;
-import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * @test
@@ -39,12 +38,6 @@
  *          that serves http POST method requests in secure channel, and a client
  *          that makes https POST request through a proxy.
  * @library /test/lib
- * @build jdk.test.lib.Utils
- *        jdk.test.lib.Asserts
- *        jdk.test.lib.JDKToolFinder
- *        jdk.test.lib.JDKToolLauncher
- *        jdk.test.lib.Platform
- *        jdk.test.lib.process.*
  * @compile OriginServer.java ProxyTunnelServer.java
  * @run main/othervm PostThruProxy
  */
@@ -62,6 +55,9 @@
     static String passwd = "passphrase";
 
     private static int serverPort = 0;
+    private static ProxyTunnelServer pserver;
+    private static TestServer server;
+    static final String RESPONSE_MSG = "Https POST thru proxy is successful";
 
     /*
      * The TestServer implements a OriginServer that
@@ -79,8 +75,7 @@
          * @return bytes for the data in the response
          */
         public byte[] getBytes() {
-            return "Https POST thru proxy is successful".
-                        getBytes();
+            return RESPONSE_MSG.getBytes();
         }
     }
 
@@ -88,6 +83,7 @@
      * Main method to create the server and client
      */
     public static void main(String args[]) throws Exception {
+
         String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;
         String trustFilename = TEST_SRC + "/" + pathToStores + "/"
                 + trustStoreFile;
@@ -97,16 +93,18 @@
         System.setProperty("javax.net.ssl.trustStore", trustFilename);
         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
 
+        InetAddress loopback = InetAddress.getLoopbackAddress();
         boolean useSSL = true;
         /*
          * setup the server
          */
         try {
             ServerSocketFactory ssf = getServerSocketFactory(useSSL);
-            ServerSocket ss = ssf.createServerSocket(serverPort);
+            ServerSocket ss = ssf.createServerSocket(serverPort, 0, loopback);
             ss.setSoTimeout(TIMEOUT);  // 30 seconds
             serverPort = ss.getLocalPort();
-            new TestServer(ss);
+            server = new TestServer(ss);
+            System.out.println("Server started at: " + ss);
         } catch (Exception e) {
             System.out.println("Server side failed:" +
                                 e.getMessage());
@@ -120,6 +118,12 @@
                                 e.getMessage());
             throw e;
         }
+        long connectCount = pserver.getConnectCount();
+        if (connectCount == 0) {
+            throw new AssertionError("Proxy was not used!");
+        } else {
+            System.out.println("Proxy CONNECT count: " + connectCount);
+        }
     }
 
     private static ServerSocketFactory getServerSocketFactory
@@ -162,9 +166,15 @@
              */
             HttpsURLConnection.setDefaultHostnameVerifier(
                                           new NameVerifier());
-            URL url = new URL("https://" + getHostname() +":" + serverPort);
+            URL url = URIBuilder.newBuilder()
+                      .scheme("https")
+                      .loopback()
+                      .port(serverPort)
+                      .toURL();
 
             Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);
+            System.out.println("Client connecting to: " + url);
+            System.out.println("Through proxy: " + pAddr);
             HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);
             https.setConnectTimeout(TIMEOUT);
             https.setReadTimeout(TIMEOUT);
@@ -185,9 +195,15 @@
                                     new InputStreamReader(
                                     https.getInputStream()));
                String inputLine;
-               while ((inputLine = in.readLine()) != null)
+               boolean msgFound = false;
+               while ((inputLine = in.readLine()) != null) {
                     System.out.println("Client received: " + inputLine);
+                    if (inputLine.contains(RESPONSE_MSG)) msgFound = true;
+               }
                in.close();
+               if (!msgFound) {
+                   throw new RuntimeException("POST message not found.");
+               }
             } catch (SSLException e) {
                 if (ps != null)
                     ps.close();
@@ -208,20 +224,13 @@
     }
 
     static SocketAddress setupProxy() throws IOException {
-        ProxyTunnelServer pserver = new ProxyTunnelServer();
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        pserver = new ProxyTunnelServer(loopback);
 
         // disable proxy authentication
         pserver.needUserAuth(false);
         pserver.start();
-        return new InetSocketAddress("localhost", pserver.getPort());
+        return new InetSocketAddress(loopback, pserver.getPort());
     }
 
-    private static String getHostname() {
-        try {
-            OutputAnalyzer oa = ProcessTools.executeCommand("hostname");
-            return oa.getOutput().trim();
-        } catch (Throwable e) {
-            throw new RuntimeException("Get hostname failed.", e);
-        }
-    }
 }
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java	Wed May 22 13:58:19 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
@@ -29,6 +29,7 @@
 
 import jdk.test.lib.process.OutputAnalyzer;
 import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * @test
@@ -47,6 +48,8 @@
  *        jdk.test.lib.process.*
  * @compile OriginServer.java ProxyTunnelServer.java
  * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes= PostThruProxyWithAuth
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true
+                     -Djdk.http.auth.tunneling.disabledSchemes= PostThruProxyWithAuth
  */
 public class PostThruProxyWithAuth {
 
@@ -62,6 +65,11 @@
     static String passwd = "passphrase";
 
     volatile private static int serverPort = 0;
+    private static ProxyTunnelServer pserver;
+    private static TestServer server;
+
+    static final String RESPONSE_MSG =
+        "Https POST thru proxy is successful with proxy authentication";
 
     /*
      * The TestServer implements a OriginServer that
@@ -79,9 +87,7 @@
          * @return bytes for the data in the response
          */
         public byte[] getBytes() {
-            return
-                "Https POST thru proxy is successful with proxy authentication".
-                getBytes();
+            return RESPONSE_MSG.getBytes();
         }
     }
 
@@ -103,11 +109,13 @@
          * setup the server
          */
         try {
+            InetAddress localhost = InetAddress.getLocalHost();
             ServerSocketFactory ssf = getServerSocketFactory(useSSL);
-            ServerSocket ss = ssf.createServerSocket(serverPort);
+            ServerSocket ss = ssf.createServerSocket(serverPort, 0, localhost);
             ss.setSoTimeout(TIMEOUT);  // 30 seconds
             serverPort = ss.getLocalPort();
-            new TestServer(ss);
+            server = new TestServer(ss);
+            System.out.println("Server started at: " + ss);
         } catch (Exception e) {
             System.out.println("Server side failed:" +
                                 e.getMessage());
@@ -120,7 +128,13 @@
             System.out.println("Client side failed: " +
                                 e.getMessage());
             throw e;
-          }
+        }
+        long connectCount = pserver.getConnectCount();
+        if (connectCount == 0) {
+            throw new AssertionError("Proxy was not used!");
+        } else {
+            System.out.println("Proxy CONNECT count: " + connectCount);
+        }
     }
 
     private static ServerSocketFactory getServerSocketFactory
@@ -160,9 +174,16 @@
          */
         HttpsURLConnection.setDefaultHostnameVerifier(
                                       new NameVerifier());
-        URL url = new URL("https://" + getHostname() + ":" + serverPort);
+
+        URL url = URIBuilder.newBuilder()
+                      .scheme("https")
+                      .host(getHostname())
+                      .port(serverPort)
+                      .toURL();
 
         Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);
+        System.out.println("Client connecting to: " + url);
+        System.out.println("Through proxy: " + pAddr);
         HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);
         https.setConnectTimeout(TIMEOUT);
         https.setReadTimeout(TIMEOUT);
@@ -182,9 +203,15 @@
                                 new InputStreamReader(
                                 https.getInputStream()));
            String inputLine;
-           while ((inputLine = in.readLine()) != null)
-                 System.out.println("Client received: " + inputLine);
+           boolean msgFound = false;
+           while ((inputLine = in.readLine()) != null) {
+                System.out.println("Client received: " + inputLine);
+                if (inputLine.contains(RESPONSE_MSG)) msgFound = true;
+           }
            in.close();
+           if (!msgFound) {
+               throw new RuntimeException("POST message not found.");
+           }
         } catch (SSLException e) {
             if (ps != null)
                 ps.close();
@@ -202,7 +229,9 @@
     }
 
     static SocketAddress setupProxy() throws IOException {
-        ProxyTunnelServer pserver = new ProxyTunnelServer();
+
+        InetAddress localhost = InetAddress.getLocalHost();
+        pserver = new ProxyTunnelServer(localhost);
 
         /*
          * register a system wide authenticator and setup the proxy for
@@ -216,7 +245,7 @@
 
         pserver.start();
 
-        return new InetSocketAddress("localhost", pserver.getPort());
+        return new InetSocketAddress(localhost, pserver.getPort());
     }
 
     public static class TestAuthenticator extends Authenticator {
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java	Wed May 22 13:58:19 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
@@ -63,11 +63,23 @@
      */
     static boolean needAuth = false;
 
+    volatile long connectCount;
+
     public ProxyTunnelServer() throws IOException {
         if (ss == null) {
             ss = (ServerSocket) ServerSocketFactory.getDefault()
                     .createServerSocket(0);
             ss.setSoTimeout(TIMEOUT);
+            System.out.println("Proxy server created: " + ss);
+        }
+    }
+
+    public ProxyTunnelServer(InetAddress address) throws IOException {
+        if (ss == null) {
+            ss = (ServerSocket) ServerSocketFactory.getDefault()
+                    .createServerSocket(0, 0, address);
+            ss.setSoTimeout(TIMEOUT);
+            System.out.println("Proxy server created: " + ss);
         }
     }
 
@@ -86,7 +98,9 @@
 
     public void run() {
         try {
+            System.out.println("Proxy server listening at: " + ss);
             clientSocket = ss.accept();
+            System.out.println("Proxy server accepted connection: " + clientSocket);
             processRequests();
         } catch (SocketTimeoutException e) {
             System.out.println(
@@ -101,7 +115,9 @@
                 System.out.println("ProxyServer close error: " + excep);
                 excep.printStackTrace();
             }
-          }
+         } finally {
+            System.out.println("Proxy server: request served");
+         }
     }
 
     /*
@@ -118,6 +134,7 @@
         String statusLine = mheader.getValue(0);
 
         if (statusLine.startsWith("CONNECT")) {
+            synchronized(this) { connectCount++; }
             // retrieve the host and port info from the status-line
             retrieveConnectInfo(statusLine);
             if (needAuth) {
@@ -154,6 +171,10 @@
         }
     }
 
+    public long getConnectCount() {
+        return connectCount;
+    }
+
     private void respondForConnect(boolean needAuth) throws Exception {
 
         OutputStream out = clientSocket.getOutputStream();
@@ -273,13 +294,19 @@
             endi = connectStr.lastIndexOf(' ');
             connectInfo = connectStr.substring(starti+1, endi).trim();
             // retrieve server name and port
-            endi = connectInfo.indexOf(':');
-            serverName = connectInfo.substring(0, endi);
+            if (connectInfo.charAt(0) == '[') {
+                 endi = connectInfo.indexOf(']');
+                 serverName = connectInfo.substring(1, endi++);
+                 assert connectInfo.charAt(endi) == ':' : "Expected [IPv6]:port";
+            } else {
+                 endi = connectInfo.indexOf(':');
+                 serverName = connectInfo.substring(0, endi);
+            }
             serverPort = Integer.parseInt(connectInfo.substring(endi+1));
         } catch (Exception e) {
             throw new IOException("Proxy recieved a request: "
                                         + connectStr, e);
-          }
+        }
         serverInetAddr = InetAddress.getByName(serverName);
     }
 
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/RetryHttps.java	Wed May 22 13:06:31 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/RetryHttps.java	Wed May 22 13:58:19 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, 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
  * @bug 4799427
  * @summary Https can not retry request
+ * @library /test/lib
  * @run main/othervm RetryHttps
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true RetryHttps
  *
  *     SunJSSE does not support dynamic system properties, no way to re-use
  *     system properties in samevm/agentvm mode.
@@ -35,6 +37,7 @@
 import java.util.*;
 import java.io.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class RetryHttps {
     static Map cookies;
@@ -80,12 +83,17 @@
      * to avoid infinite hangs.
      */
     void doServerSide() throws Exception {
+        InetAddress loopback = InetAddress.getLoopbackAddress();
         SSLServerSocketFactory sslssf =
             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
         sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket(serverPort, 0, loopback);
         serverPort = sslServerSocket.getLocalPort();
 
+        System.out.println("Starting server at: "
+                            +  sslServerSocket.getInetAddress()
+                            + ":" + serverPort);
+
         /*
          * Signal Client, we're ready for his connect.
          */
@@ -145,11 +153,16 @@
             try {
                 HttpsURLConnection http = null;
                 /* establish http connection to server */
-                URL url = new URL("https://localhost:" + serverPort+"/file1");
+               URL url = URIBuilder.newBuilder()
+                      .scheme("https")
+                      .loopback()
+                      .port(serverPort)
+                      .path("/file1")
+                      .toURL();
                 System.out.println("url is "+url.toString());
                 HttpsURLConnection.setDefaultHostnameVerifier(
                                                         new NameVerifier());
-                http = (HttpsURLConnection)url.openConnection();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
                 int respCode = http.getResponseCode();
                 int cl = http.getContentLength();
                 InputStream is = http.getInputStream ();
@@ -157,8 +170,13 @@
                 while (is.read() != -1 && count++ < cl);
                 System.out.println("respCode1 = "+respCode);
                 Thread.sleep(2000);
-                url = new URL("https://localhost:" + serverPort+"/file2");
-                http = (HttpsURLConnection)url.openConnection();
+                url = URIBuilder.newBuilder()
+                      .scheme("https")
+                      .loopback()
+                      .port(serverPort)
+                      .path("/file2")
+                      .toURL();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
                 respCode = http.getResponseCode();
                 System.out.println("respCode2 = "+respCode);
             } catch (IOException ioex) {