From 2ad9f11d7763051fc8c2ed9175ddc9f4b329c636 Mon Sep 17 00:00:00 2001
From: Daniel Fuchs <dfuchs@openjdk.org>
Date: Fri, 28 Jun 2019 15:58:10 +0100
Subject: [PATCH] 8226514: Replace wildcard address with loopback or local host
 in tests - part 14

Improve test stabilty by getting rid of the wildcard address whenever possible.

Reviewed-by: chegar, vtewari
---
 .../net/HttpCookie/IllegalCookieNameTest.java | 20 ++++++--
 .../SetAuthenticator/HTTPTest.java            | 23 +++++++--
 .../SetAuthenticator/HTTPTestServer.java      |  8 ++-
 .../jdk/sun/net/ftp/FtpURLConnectionLeak.java |  2 +-
 .../net/www/http/HttpClient/RetryPost.java    | 20 +++-----
 .../sun/net/www/protocol/http/B5017051.java   | 50 ++++++++++---------
 .../sun/net/www/protocol/http/B6296310.java   | 25 ++++------
 .../sun/net/www/protocol/http/B6299712.java   |  7 ++-
 .../jdk/sun/net/www/protocol/http/NoNTLM.java | 34 +++++++++----
 .../sun/net/www/protocol/http/UserAgent.java  |  9 ++--
 .../jdk/sun/net/www/protocol/http/ZoneId.java |  4 +-
 .../https/NewImpl/JavaxHTTPSConnection.java   | 21 ++++++--
 12 files changed, 137 insertions(+), 86 deletions(-)

diff --git a/test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java b/test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java
index 2badd761488..dcdbe7f18f2 100644
--- a/test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java
+++ b/test/jdk/java/net/HttpCookie/IllegalCookieNameTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2016, 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
@@ -23,25 +23,35 @@
 
 /* @test
  * @bug 7183292
+ * @library /test/lib
  * @modules jdk.httpserver
+ * @run main IllegalCookieNameTest
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true IllegalCookieNameTest
  */
 import java.net.*;
 import java.util.*;
 import java.io.*;
 import com.sun.net.httpserver.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class IllegalCookieNameTest {
-    public static void main(String[] args) throws IOException {
+    public static void main(String[] args) throws Exception {
         HttpServer s = null;
         try {
-            InetSocketAddress addr = new InetSocketAddress(0);
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            InetSocketAddress addr = new InetSocketAddress(loopback, 0);
             s = HttpServer.create(addr, 10);
             s.createContext("/", new HHandler());
             s.start();
-            String u = "http://127.0.0.1:" + s.getAddress().getPort() + "/";
+            String u = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(s.getAddress().getPort())
+                .path("/")
+                .build().toString();
             CookieHandler.setDefault(new TestCookieHandler());
             URL url = new URL(u);
-            HttpURLConnection c = (HttpURLConnection) url.openConnection();
+            HttpURLConnection c = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
             c.getHeaderFields();
             System.out.println ("OK");
         } finally {
diff --git a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTest.java b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTest.java
index b6d4f46b589..5f4eec0af21 100644
--- a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTest.java
+++ b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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
@@ -258,14 +258,27 @@ public class HTTPTest {
     public static URL url(HttpProtocolType protocol, InetSocketAddress address,
                           String path) throws MalformedURLException {
         return new URL(protocol(protocol),
-                       address.getHostString(),
+                       address.getAddress().getHostAddress(),
                        address.getPort(), path);
     }
 
     public static Proxy proxy(HTTPTestServer server, HttpAuthType authType) {
-        return (authType == HttpAuthType.PROXY)
-               ? new Proxy(Proxy.Type.HTTP, server.getAddress())
-               : null;
+        if (authType != HttpAuthType.PROXY) return null;
+
+        InetSocketAddress proxyAddress = server.getProxyAddress();
+        if (!proxyAddress.isUnresolved()) {
+            // Forces the proxy to use an unresolved address created
+            // from the actual IP address to avoid using the proxy
+            // address hostname which would result in resolving to
+            // a posibly different address. For instance we want to
+            // avoid cases such as:
+            //    ::1 => "localhost" => 127.0.0.1
+            proxyAddress = InetSocketAddress.
+                createUnresolved(proxyAddress.getAddress().getHostAddress(),
+                                 proxyAddress.getPort());
+        }
+
+        return new Proxy(Proxy.Type.HTTP, proxyAddress);
     }
 
     public static HttpURLConnection openConnection(URL url,
diff --git a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java
index 1c48b502003..7daf8b16673 100644
--- a/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java
+++ b/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java
@@ -391,6 +391,10 @@ public class HTTPTestServer extends HTTPTest {
         return serverImpl.getAddress();
     }
 
+    public InetSocketAddress getProxyAddress() {
+        return serverImpl.getAddress();
+    }
+
     public void stop() {
         serverImpl.stop(0);
         if (redirect != null) {
@@ -1019,7 +1023,7 @@ public class HTTPTestServer extends HTTPTest {
         }
 
         @Override
-        public InetSocketAddress getAddress() {
+        public InetSocketAddress getProxyAddress() {
             return new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort());
         }
 
@@ -1047,7 +1051,7 @@ public class HTTPTestServer extends HTTPTest {
             Socket clientConnection = null;
             try {
                 while (true) {
-                    System.out.println("Tunnel: Waiting for client");
+                    System.out.println("Tunnel: Waiting for client at: " + ss);
                     Socket previous = clientConnection;
                     try {
                         clientConnection = ss.accept();
diff --git a/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java b/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
index 91e258c86cc..ab3578ec465 100644
--- a/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
+++ b/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
@@ -27,7 +27,7 @@
  * @summary FtpURLConnection doesn't close FTP connection when FileNotFoundException is thrown
  * @library ../www/ftptest/
  * @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler
- * @run main FtpURLConnectionLeak
+ * @run main/othervm FtpURLConnectionLeak
  */
 import java.io.FileNotFoundException;
 import java.io.InputStream;
diff --git a/test/jdk/sun/net/www/http/HttpClient/RetryPost.java b/test/jdk/sun/net/www/http/HttpClient/RetryPost.java
index 26290860ff6..6b80175135c 100644
--- a/test/jdk/sun/net/www/http/HttpClient/RetryPost.java
+++ b/test/jdk/sun/net/www/http/HttpClient/RetryPost.java
@@ -54,23 +54,19 @@ public class RetryPost
     MyHandler httpHandler;
     ExecutorService executorService;
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         if (args.length == 1 && args[0].equals("noRetry"))
             shouldRetry = false;
 
         new RetryPost();
     }
 
-    public RetryPost() {
-        try {
-            startHttpServer(shouldRetry);
-            doClient();
-        } catch (IOException ioe) {
-            System.err.println(ioe);
-        }
+    public RetryPost() throws Exception {
+        startHttpServer(shouldRetry);
+        doClient();
     }
 
-    void doClient() {
+    void doClient() throws Exception {
         try {
             InetSocketAddress address = httpServer.getAddress();
             URL url = URIBuilder.newBuilder()
@@ -95,8 +91,6 @@ public class RetryPost
             else if (!shouldRetry && httpHandler.getCallCount() != 1)
                 throw new RuntimeException("Failed: Handler should have only been called once" +
                                            "It was called "+ httpHandler.getCallCount() + " times");
-        } catch (IOException e) {
-            e.printStackTrace();
         } finally {
             httpServer.stop(1);
             executorService.shutdown();
@@ -119,8 +113,8 @@ public class RetryPost
     }
 
     class MyHandler implements HttpHandler {
-        int callCount = 0;
-        boolean shouldRetry;
+        volatile int callCount = 0;
+        final boolean shouldRetry;
 
         public MyHandler(boolean shouldRetry) {
             this.shouldRetry = shouldRetry;
diff --git a/test/jdk/sun/net/www/protocol/http/B5017051.java b/test/jdk/sun/net/www/protocol/http/B5017051.java
index 1859be41a61..26ac9321beb 100644
--- a/test/jdk/sun/net/www/protocol/http/B5017051.java
+++ b/test/jdk/sun/net/www/protocol/http/B5017051.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2016, 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
@@ -25,7 +25,9 @@
  * @test
  * @bug 5017051 6360774
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main/othervm B5017051
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B5017051
  * @summary Tests CR 5017051 & 6360774
  */
 
@@ -35,6 +37,7 @@ import java.io.*;
 import com.sun.net.httpserver.*;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * Part 1:
@@ -55,42 +58,47 @@ import java.util.concurrent.ExecutorService;
 
 public class B5017051
 {
-    com.sun.net.httpserver.HttpServer httpServer;
+    HttpServer httpServer;
     ExecutorService executorService;
 
-    public static void main(String[] args)
-    {
+    public static void main(String[] args) throws Exception {
         new B5017051();
     }
 
-    public B5017051()
-    {
-        try {
-            startHttpServer();
-            doClient();
-        } catch (IOException ioe) {
-            System.err.println(ioe);
-        }
+    public B5017051() throws Exception {
+        startHttpServer();
+        doClient();
     }
 
-    void doClient() {
+    void doClient() throws Exception {
         java.net.Authenticator.setDefault(new MyAuthenticator());
         CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
+        ProxySelector.setDefault(ProxySelector.of(null));
 
         try {
             InetSocketAddress address = httpServer.getAddress();
 
             // Part 1
-            URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
+            URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .host(address.getAddress())
+                .port(address.getPort())
+                .path("/test/")
+                .toURL();
             HttpURLConnection uc = (HttpURLConnection)url.openConnection();
             int resp = uc.getResponseCode();
             if (resp != 200)
-                throw new RuntimeException("Failed: Part 1, Response code is not 200");
+                throw new RuntimeException("Failed: Part 1, Response code is not 200: " + resp);
 
             System.out.println("Response code from Part 1 = 200 OK");
 
             // Part 2
-            URL url2 = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test2/");
+            URL url2 = URIBuilder.newBuilder()
+                .scheme("http")
+                .host(address.getAddress())
+                .port(address.getPort())
+                .path("/test2/")
+                .toURL();
 
             // can use the global CookieHandler used for the first test as the URL's are different
             CookieHandler ch = CookieHandler.getDefault();
@@ -106,15 +114,10 @@ public class B5017051
             uc = (HttpURLConnection)url2.openConnection();
             resp = uc.getResponseCode();
             if (resp != 200)
-                throw new RuntimeException("Failed: Part 2, Response code is not 200");
+                throw new RuntimeException("Failed: Part 2, Response code is not 200: " + resp);
 
             System.out.println("Response code from Part 2 = 200 OK");
 
-
-        } catch (IOException e) {
-            e.printStackTrace();
-        } catch (URISyntaxException ue) {
-            ue.printStackTrace();
         } finally {
             httpServer.stop(1);
             executorService.shutdown();
@@ -125,7 +128,8 @@ public class B5017051
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
 
         // create HttpServer context for Part 1.
         HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
diff --git a/test/jdk/sun/net/www/protocol/http/B6296310.java b/test/jdk/sun/net/www/protocol/http/B6296310.java
index 1b276c25647..26d0c70ad24 100644
--- a/test/jdk/sun/net/www/protocol/http/B6296310.java
+++ b/test/jdk/sun/net/www/protocol/http/B6296310.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
  * @library ../../httptest/
  * @build HttpCallback TestHttpServer HttpTransaction
  * @run main/othervm B6296310
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B6296310
  * @summary  REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases
  */
 
@@ -45,32 +46,26 @@ public class B6296310
    static SimpleHttpTransaction httpTrans;
    static TestHttpServer server;
 
-   public static void main(String[] args)
+   public static void main(String[] args) throws Exception
    {
       ResponseCache.setDefault(new MyCacheHandler());
       startHttpServer();
-
       makeHttpCall();
    }
 
-   public static void startHttpServer() {
-      try {
-         httpTrans = new SimpleHttpTransaction();
-         server = new TestHttpServer(httpTrans, 1, 10, 0);
-      } catch (IOException e) {
-         e.printStackTrace();
-      }
+   public static void startHttpServer() throws IOException {
+     httpTrans = new SimpleHttpTransaction();
+     InetAddress loopback = InetAddress.getLoopbackAddress();
+     server = new TestHttpServer(httpTrans, 1, 10, loopback, 0);
    }
 
-   public static void makeHttpCall() {
+   public static void makeHttpCall() throws IOException {
       try {
          System.out.println("http server listen on: " + server.getLocalPort());
-         URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
+         URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),
                             server.getLocalPort(), "/");
-         HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+         HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
          System.out.println(uc.getResponseCode());
-      } catch (IOException e) {
-         e.printStackTrace();
       } finally {
          server.terminate();
       }
diff --git a/test/jdk/sun/net/www/protocol/http/B6299712.java b/test/jdk/sun/net/www/protocol/http/B6299712.java
index 24c78d64bfa..d35d6453c46 100644
--- a/test/jdk/sun/net/www/protocol/http/B6299712.java
+++ b/test/jdk/sun/net/www/protocol/http/B6299712.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2016, 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
@@ -26,6 +26,7 @@
  * @bug 6299712 7150552
  * @modules jdk.httpserver
  * @run main/othervm B6299712
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B6299712
  * @summary  NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect
  */
 
@@ -54,13 +55,15 @@ public class B6299712 {
 
     public static void main(String[] args) throws Exception {
         ResponseCache.setDefault(new DeployCacheHandler());
+        ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
         startHttpServer();
 
         makeHttpCall();
     }
 
     public static void startHttpServer() throws IOException {
-        server = HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress address = InetAddress.getLocalHost();
+        server = HttpServer.create(new InetSocketAddress(address, 0), 0);
         server.createContext("/", new DefaultHandler());
         server.createContext("/redirect", new RedirectHandler());
         server.start();
diff --git a/test/jdk/sun/net/www/protocol/http/NoNTLM.java b/test/jdk/sun/net/www/protocol/http/NoNTLM.java
index 9458b26a48f..2eaa212bf29 100644
--- a/test/jdk/sun/net/www/protocol/http/NoNTLM.java
+++ b/test/jdk/sun/net/www/protocol/http/NoNTLM.java
@@ -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
@@ -29,6 +29,7 @@
  * @modules java.base/sun.net.www
  *          java.base/sun.net.www.protocol.http:open
  * @run main/othervm NoNTLM
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true NoNTLM
  */
 
 import java.io.IOException;
@@ -155,8 +156,8 @@ public class NoNTLM {
         System.out.println("====================================");
         System.out.println("Expect client to choose: " + expected);
         System.out.println(reply);
-
-        try (ServerSocket ss = new ServerSocket(0)) {
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        try (ServerSocket ss = new ServerSocket(0, 0, loopback)) {
             Client.start(ss.getLocalPort());
 
             // client ---- GET ---> server
@@ -198,7 +199,8 @@ public class NoNTLM {
         System.out.println("Expect client to fail with 401 Unauthorized");
         System.out.println(reply);
 
-        try (ServerSocket ss = new ServerSocket(0)) {
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        try (ServerSocket ss = new ServerSocket(0, 0, loopback)) {
             Client client = new Client(ss.getLocalPort());
             Thread thr = new Thread(client);
             thr.start();
@@ -225,13 +227,14 @@ public class NoNTLM {
     }
 
     public static void main(String[] args) throws Exception {
+        boolean ntlmSupported = false;
         try {
             Class<?> ntlmProxyClass = Class.forName("sun.net.www.protocol.http.NTLMAuthenticationProxy", true, NoNTLM.class.getClassLoader());
             Field ntlmSupportedField = ntlmProxyClass.getDeclaredField("supported");
             ntlmSupportedField.setAccessible(true);
             if (ntlmSupportedField.getBoolean(null)) {
-                System.out.println("NTLM is supported. Nothing to do. Exiting.");
-                return;
+                System.out.println("NTLM is supported.");
+                ntlmSupported = true;
             }
         } catch (ClassNotFoundException okay) { }
 
@@ -247,15 +250,26 @@ public class NoNTLM {
         test("Basic");
         test("Digest");
         test("Basic", "Digest");
-        test("Basic", "NTLM");
+
+        if (ntlmSupported) {
+            System.out.println("====================================");
+            System.out.println("NTLM is supported: client would select NTLM: skipping `test(\"Basic\", \"NTLM\")`..");
+        } else {
+            test("Basic", "NTLM");
+        }
+
         test("Digest", "NTLM");
         test("Basic", "Digest", "NTLM");
 
-        // test NTLM only, this should fail with "401 Unauthorized"
-        testNTLM();
+        if (ntlmSupported) {
+            System.out.println("====================================");
+            System.out.println("NTLM is supported: client would select NTLM: skipping `testNTLM()`..");
+        } else {
+            // test NTLM only, this should fail with "401 Unauthorized"
+            testNTLM();
+        }
 
         System.out.println();
         System.out.println("TEST PASSED");
     }
 }
-
diff --git a/test/jdk/sun/net/www/protocol/http/UserAgent.java b/test/jdk/sun/net/www/protocol/http/UserAgent.java
index 0a87bac5562..7db84882488 100644
--- a/test/jdk/sun/net/www/protocol/http/UserAgent.java
+++ b/test/jdk/sun/net/www/protocol/http/UserAgent.java
@@ -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
@@ -27,6 +27,7 @@
  * @library /test/lib
  * @modules java.base/sun.net.www
  * @run main/othervm -Dhttp.agent=foo UserAgent
+ * @run main/othervm -Dhttp.agent=foo -Djava.net.preferIPv6Addresses=true UserAgent
  * @summary  HTTP header "User-Agent" format incorrect
  */
 
@@ -87,7 +88,9 @@ class Server extends Thread {
 public class UserAgent {
 
     public static void main(String[] args) throws Exception {
-        ServerSocket server = new ServerSocket (0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket server = new ServerSocket ();
+        server.bind(new InetSocketAddress(loopback, 0));
         Server s = new Server (server);
         s.start ();
         int port = server.getLocalPort ();
@@ -97,7 +100,7 @@ public class UserAgent {
             .port(port)
             .toURL();
         System.out.println("URL: " + url);
-        URLConnection urlc = url.openConnection ();
+        URLConnection urlc = url.openConnection (Proxy.NO_PROXY);
         urlc.getInputStream ();
         s.join ();
         if (!s.succeeded()) {
diff --git a/test/jdk/sun/net/www/protocol/http/ZoneId.java b/test/jdk/sun/net/www/protocol/http/ZoneId.java
index 017984770a4..374ee8be733 100644
--- a/test/jdk/sun/net/www/protocol/http/ZoneId.java
+++ b/test/jdk/sun/net/www/protocol/http/ZoneId.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, 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
@@ -60,7 +60,7 @@ public class ZoneId {
         out.println("Found an appropriate IPv6 address: " + address);
 
         out.println("Starting http server...");
-        HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
+        HttpServer server = HttpServer.create(new InetSocketAddress(address, 0), 0);
         CompletableFuture<Headers> headers = new CompletableFuture<>();
         server.createContext("/", createCapturingHandler(headers));
         server.start();
diff --git a/test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHTTPSConnection.java b/test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHTTPSConnection.java
index 196f1d4bf0f..f6d8df6bdd4 100644
--- a/test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHTTPSConnection.java
+++ b/test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHTTPSConnection.java
@@ -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
@@ -25,7 +25,9 @@
  * @test
  * @bug 4474255
  * @summary Can no longer obtain a com.sun.net.ssl.HttpsURLConnection
+ * @library /test/lib
  * @run main/othervm JavaxHTTPSConnection
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true JavaxHTTPSConnection
  *
  *     SunJSSE does not support dynamic system properties, no way to re-use
  *     system properties in samevm/agentvm mode.
@@ -36,6 +38,7 @@ import java.io.*;
 import java.net.*;
 import java.security.cert.*;
 import javax.net.ssl.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * See if we can obtain a javax.net.ssl.HttpsURLConnection,
@@ -138,10 +141,13 @@ public class JavaxHTTPSConnection {
      */
     void doServerSide() throws Exception {
 
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress serverAddress = new InetSocketAddress(loopback, serverPort);
         SSLServerSocketFactory sslssf =
           (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
         SSLServerSocket sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(serverAddress);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -204,9 +210,14 @@ public class JavaxHTTPSConnection {
             }
 
             HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
-            URL url = new URL("https://" + "localhost:" + serverPort +
-                                    "/etc/hosts");
-            URLConnection urlc = url.openConnection();
+            URL url = URIBuilder.newBuilder()
+                .scheme("https")
+                .loopback()
+                .port(serverPort)
+                .path("/etc/hosts")
+                .toURL();
+            System.out.println("Client opening: " + url);
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
 
             if (!(urlc instanceof javax.net.ssl.HttpsURLConnection)) {
                 throw new Exception("URLConnection ! instanceof " +
-- 
GitLab