8223798: Replace wildcard address with loopback or local host in tests - part 7
authoraefimov
Wed, 15 May 2019 19:47:28 +0100
changeset 54887 442e22c051f0
parent 54886 4dd7ea5f28cf
child 54888 2c19c55a289a
8223798: Replace wildcard address with loopback or local host in tests - part 7 Reviewed-by: dfuchs, vtewari
test/jdk/java/net/Socket/DeadlockTest.java
test/jdk/java/net/Socket/SocketGrowth.java
test/jdk/java/net/Socket/asyncClose/DatagramSocket_receive.java
test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java
test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java
test/jdk/java/net/URLConnection/HttpContinueStackOverflow.java
test/jdk/java/net/URLConnection/ResendPostBody.java
test/jdk/sun/net/ftp/MarkResetTest.java
test/jdk/sun/net/www/http/ChunkedOutputStream/Test.java
test/jdk/sun/net/www/protocol/http/B6518816.java
test/jdk/sun/net/www/protocol/http/ProxyTunnelServer.java
test/jdk/sun/net/www/protocol/http/TunnelThroughProxy.java
--- a/test/jdk/java/net/Socket/DeadlockTest.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/Socket/DeadlockTest.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -39,7 +39,7 @@
     public static void main(String [] argv) throws Exception {
         IPSupport.throwSkippedExceptionIfNonOperational();
 
-        ServerSocket ss = new ServerSocket(0);
+        ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
         Socket clientSocket = new Socket();
 
         try {
@@ -154,7 +154,7 @@
         try {
             System.out.println("About to connect the client socket");
             this.sock = sock;
-            this.sock.connect(new InetSocketAddress("localhost", serverPort));
+            this.sock.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), serverPort));
             System.out.println("connected");
 
             out = new ObjectOutputStream(sock.getOutputStream());
--- a/test/jdk/java/net/Socket/SocketGrowth.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/Socket/SocketGrowth.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,15 +29,16 @@
  */
 
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 
 public class SocketGrowth {
 
     public static void main(String[] args) throws IOException {
-
-        try (ServerSocket ss = new ServerSocket(0)) {
-            try (Socket s = new Socket("localhost", ss.getLocalPort());
+        InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
+        try (ServerSocket ss = new ServerSocket(0, 0, loopbackAddress)) {
+            try (Socket s = new Socket(loopbackAddress, ss.getLocalPort());
                     Socket peer = ss.accept()) {
                 for (int i=0; i<1000000; i++) {
                     // buggy JDK will run out of memory in this loop
--- a/test/jdk/java/net/Socket/asyncClose/DatagramSocket_receive.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/Socket/asyncClose/DatagramSocket_receive.java	Wed May 15 19:47:28 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
@@ -27,11 +27,13 @@
  */
 import java.net.*;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 public class DatagramSocket_receive extends AsyncCloseTest implements Runnable {
     private final DatagramSocket s;
     private final int timeout;
     private final CountDownLatch latch;
+    private final AtomicBoolean readyToClose = new AtomicBoolean(false);
 
     public DatagramSocket_receive() throws SocketException {
         this(0);
@@ -59,8 +61,13 @@
                 s.setSoTimeout(timeout);
             }
             latch.countDown();
-            s.receive(p);
-            failed("DatagramSocket.receive(DatagramPacket) returned unexpectly!!" + " - " + p.getAddress());
+            do {
+                // if readyToClose is still false it means some other
+                // process on the system attempted to send datagram packet:
+                // just ignore it, and go back to accept again.
+                s.receive(p);
+            } while (!readyToClose.get());
+            failed("DatagramSocket.receive(DatagramPacket) returned unexpectedly!!" + " - " + p.getAddress());
         } catch (SocketException se) {
             if (latch.getCount() != 1) {
                 closed();
@@ -80,6 +87,7 @@
             thr.start();
             latch.await();
             Thread.sleep(5000); //sleep, so receive(DatagramPacket) can block
+            readyToClose.set(true);
             s.close();
             thr.join();
 
--- a/test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/Socket/asyncClose/Socket_getInputStream_read.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 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
@@ -60,7 +60,7 @@
             }
             latch.countDown();
             int n = in.read();
-            failed("Socket.getInputStream().read() returned unexpectly!!");
+            failed("Socket.getInputStream().read() returned unexpectedly!!");
         } catch (SocketException se) {
             if (latch.getCount() != 1) {
                 closed();
@@ -76,8 +76,8 @@
 
     public AsyncCloseTest go() {
         try {
-            ServerSocket ss = new ServerSocket(0);
             InetAddress lh = InetAddress.getLocalHost();
+            ServerSocket ss = new ServerSocket(0, 0, lh);
             s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );
             Socket s2 = ss.accept();
             Thread thr = new Thread(this);
--- a/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 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
@@ -65,8 +65,8 @@
 
     public AsyncCloseTest go() {
         try {
-            ServerSocket ss = new ServerSocket(0);
             InetAddress lh = InetAddress.getLocalHost();
+            ServerSocket ss = new ServerSocket(0, 0, lh);
             s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );
             Socket s2 = ss.accept();
             Thread thr = new Thread(this);
--- a/test/jdk/java/net/URLConnection/HttpContinueStackOverflow.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/URLConnection/HttpContinueStackOverflow.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
 
 /* @test
  * @bug 4258697
+ * @library /test/lib
  * @summary Make sure that http CONTINUE status followed by invalid
  * response doesn't cause HttpClient to recursively loop and
  * eventually StackOverflow.
@@ -32,19 +33,21 @@
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
 import java.net.HttpURLConnection;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class HttpContinueStackOverflow {
 
     static class Server implements Runnable {
-        int port;
         ServerSocket serverSock ;
 
         Server() throws IOException {
-            serverSock = new ServerSocket(0);
+            serverSock = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
         }
 
         int getLocalPort() {
@@ -82,8 +85,14 @@
         Server s = new Server();
         (new Thread(s)).start();
 
-        /* connect to server, connect to server and get response code */
-        URL url = new URL("http", "localhost", s.getLocalPort(), "anything.html");
+        /* connect to server and get response code */
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(s.getLocalPort())
+                .path("/anything.html")
+                .toURL();
+
         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
         conn.getResponseCode();
         System.out.println("TEST PASSED");
--- a/test/jdk/java/net/URLConnection/ResendPostBody.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/java/net/URLConnection/ResendPostBody.java	Wed May 15 19:47:28 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
@@ -25,12 +25,15 @@
  * @test
  * @bug 4361492
  * @summary HTTPUrlConnection does not receive binary data correctly
+ * @library /test/lib
  * @run main/timeout=20 ResendPostBody
  */
 
 import java.io.*;
 import java.net.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 /*
  * This test does the following:
  * 1. client opens HTTP connection to server
@@ -139,13 +142,16 @@
 
         byte b[] = "X=ABCDEFGHZZZ".getBytes();
 
-        ss = new ServerSocket (0);
+        ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
         server = new Server (ss);
         server.start ();
         /* Get the URL */
-
-        String s = "http://localhost:"+ss.getLocalPort()+"/test";
-        URL url = new URL(s);
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .path("/test")
+                .toURL();
         HttpURLConnection conURL =  (HttpURLConnection)url.openConnection();
 
         conURL.setDoOutput(true);
--- a/test/jdk/sun/net/ftp/MarkResetTest.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/sun/net/ftp/MarkResetTest.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 4673103
+ * @library /test/lib
  * @run main/othervm/timeout=140 MarkResetTest
  * @summary URLConnection.getContent() hangs over FTP for DOC, PPT, XLS files
  */
@@ -36,7 +37,10 @@
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintWriter;
+import java.io.UncheckedIOException;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
@@ -44,6 +48,8 @@
 import java.nio.file.Files;
 import java.nio.file.Paths;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class MarkResetTest {
     private static final String FILE_NAME = "EncDec.doc";
 
@@ -51,9 +57,8 @@
      * A class that simulates, on a separate, an FTP server.
      */
     private class FtpServer extends Thread {
-        private ServerSocket    server;
-        private int port;
-        private boolean done = false;
+        private final ServerSocket server;
+        private volatile boolean done = false;
         private boolean pasvEnabled = true;
         private boolean portEnabled = true;
         private boolean extendedEnabled = true;
@@ -173,7 +178,7 @@
              */
 
             public void run() {
-                boolean done = false;
+                done = false;
                 String str;
                 int res;
                 boolean logged = false;
@@ -244,8 +249,10 @@
                                 continue;
                             }
                             try {
-                                if (pasv == null)
-                                    pasv = new ServerSocket(0);
+                                if (pasv == null) {
+                                    pasv = new ServerSocket();
+                                    pasv.bind(new InetSocketAddress(server.getInetAddress(), 0));
+                                }
                                 int port = pasv.getLocalPort();
                                 out.println("229 Entering Extended" +
                                         " Passive Mode (|||" + port + "|)");
@@ -262,8 +269,10 @@
                                 continue;
                             }
                             try {
-                                if (pasv == null)
-                                    pasv = new ServerSocket(0);
+                                if (pasv == null) {
+                                    pasv = new ServerSocket();
+                                    pasv.bind(new InetSocketAddress("127.0.0.1", 0));
+                                }
                                 int port = pasv.getLocalPort();
 
                                 // Parenthesis are optional, so let's be
@@ -364,17 +373,28 @@
         }
 
         public FtpServer(int port) {
-            this.port = port;
+            this(InetAddress.getLoopbackAddress(), port);
+        }
+
+        public FtpServer(InetAddress address, int port) {
+            try {
+                if (address == null) {
+                    server = new ServerSocket(port);
+                } else {
+                    server = new ServerSocket();
+                    server.bind(new InetSocketAddress(address, port));
+                }
+            } catch (IOException e) {
+                throw new UncheckedIOException(e);
+            }
         }
 
         public FtpServer() {
-            this(21);
+            this(null, 21);
         }
 
         public int getPort() {
-            if (server != null)
-                return server.getLocalPort();
-            return 0;
+            return server.getLocalPort();
         }
 
         /**
@@ -392,7 +412,8 @@
          */
         public void run() {
             try {
-                server = new ServerSocket(port);
+                System.out.println("FTP server waiting for connections at: "
+                        + server.getLocalSocketAddress());
                 Socket client;
                 client = server.accept();
                 (new FtpServerHandler(client)).start();
@@ -419,10 +440,14 @@
                 port = server.getPort();
             }
 
+            URL url = URIBuilder.newBuilder()
+                    .scheme("ftp")
+                    .loopback()
+                    .port(port)
+                    .path("/" + FILE_NAME)
+                    .toURL();
 
-            URL url = new URL("ftp://localhost:" + port + "/" + FILE_NAME);
-
-            URLConnection con = url.openConnection();
+            URLConnection con = url.openConnection(Proxy.NO_PROXY);
             System.out.println("getContent: " + con.getContent());
             System.out.println("getContent-length: " + con.getContentLength());
 
--- a/test/jdk/sun/net/www/http/ChunkedOutputStream/Test.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/sun/net/www/http/ChunkedOutputStream/Test.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 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 5026745 6631048
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main/othervm/timeout=500 Test
  * @summary Cannot flush output stream when writing to an HttpUrlConnection
  */
@@ -33,6 +34,8 @@
 import java.net.*;
 import com.sun.net.httpserver.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class Test implements HttpHandler {
 
     static volatile int count = 0;
@@ -272,9 +275,8 @@
 
     /* basic chunked test (runs twice) */
 
-    static void test1 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test1(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (20);
         urlc.setDoOutput(true);
@@ -289,9 +291,8 @@
 
     /* basic fixed length test */
 
-    static void test3 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test3(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setFixedLengthStreamingMode (str2.length());
         urlc.setDoOutput(true);
@@ -306,9 +307,8 @@
 
     /* write too few bytes */
 
-    static void test4 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test4(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setFixedLengthStreamingMode (str2.length()+1);
         urlc.setDoOutput(true);
@@ -323,9 +323,8 @@
 
     /* write too many bytes */
 
-    static void test5 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test5(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setFixedLengthStreamingMode (str2.length()-1);
         urlc.setDoOutput(true);
@@ -339,9 +338,8 @@
 
     /* check for HttpRetryException on redirection */
 
-    static void test6 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test6(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (20);
         urlc.setDoOutput(true);
@@ -364,9 +362,8 @@
 
     /* next two tests send zero length posts */
 
-    static void test7 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test7(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (20);
         urlc.setDoOutput(true);
@@ -379,9 +376,8 @@
         }
     }
 
-    static void test8 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test8(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setFixedLengthStreamingMode (0);
         urlc.setDoOutput(true);
@@ -396,9 +392,8 @@
 
     /* calling setChunkedStreamingMode with -1 should entail using
        the default chunk size */
-   static void test9 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test9(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (-1);
         urlc.setDoOutput(true);
@@ -411,9 +406,8 @@
         is.close();
     }
 
-   static void test10 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test10(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (4 * 1024);
         urlc.setDoOutput(true);
@@ -435,9 +429,8 @@
         }
     }
 
-    static void test11 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test11(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (36 * 1024);
         urlc.setDoOutput(true);
@@ -462,9 +455,8 @@
         }
     }
 
-    static void test12 (String u) throws Exception {
-        URL url = new URL (u);
-        System.out.println ("client opening connection to: " + u);
+    static void test12(URL url) throws Exception {
+        System.out.println("client opening connection to: " + url);
         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
         urlc.setChunkedStreamingMode (36 * 1024);
         urlc.setDoOutput(true);
@@ -485,29 +477,39 @@
     }
 
 
-    static com.sun.net.httpserver.HttpServer httpserver;
+    static HttpServer httpserver;
+
+    private static URL buildTestURL(int port, String path)
+            throws MalformedURLException, URISyntaxException {
+        return URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(port)
+                .path(path)
+                .toURL();
+    }
 
     public static void main (String[] args) throws Exception {
         try {
-            httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
-            HttpContext ctx = httpserver.createContext("/test/", new Test() );
+            httpserver = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
+            httpserver.createContext("/test/", new Test());
             httpserver.start();
 
             int port = httpserver.getAddress().getPort();
 
             System.out.println ("Server started: listening on port: " + port);
-            test1("http://localhost:"+ port + "/test/test1");
-            test1("http://localhost:"+ port + "/test/test2");
-            test3("http://localhost:"+ port + "/test/test3");
-            test4("http://localhost:"+ port + "/test/test4");
-            test5("http://localhost:"+ port + "/test/test5");
-            test6("http://localhost:"+ port + "/test/test6");
-            test7("http://localhost:"+ port + "/test/test7");
-            test8("http://localhost:"+ port + "/test/test8");
-            test9("http://localhost:"+ port + "/test/test9");
-            test10("http://localhost:"+ port + "/test/test10");
-            test11("http://localhost:"+ port + "/test/test11");
-            test12("http://localhost:"+ port + "/test/test12");
+            test1(buildTestURL(port, "/test/test1"));
+            test1(buildTestURL(port, "/test/test2"));
+            test3(buildTestURL(port, "/test/test3"));
+            test4(buildTestURL(port, "/test/test4"));
+            test5(buildTestURL(port, "/test/test5"));
+            test6(buildTestURL(port, "/test/test6"));
+            test7(buildTestURL(port, "/test/test7"));
+            test8(buildTestURL(port, "/test/test8"));
+            test9(buildTestURL(port, "/test/test9"));
+            test10(buildTestURL(port, "/test/test10"));
+            test11(buildTestURL(port, "/test/test11"));
+            test12(buildTestURL(port, "/test/test12"));
         } finally {
             if (httpserver != null)
                 httpserver.stop(0);
--- a/test/jdk/sun/net/www/protocol/http/B6518816.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/sun/net/www/protocol/http/B6518816.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2016, 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
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 6518816
+ * @library /test/lib
  * @modules jdk.httpserver
  * @run main/othervm B6518816
  */
@@ -35,6 +36,8 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class B6518816
 {
     com.sun.net.httpserver.HttpServer httpServer;
@@ -79,8 +82,12 @@
             byte [] buf = new byte [TEN_MB];
 
             for (int j=0; j<MAX_CONNS; j++) {
-
-                URL url = new URL("http://localhost:"+address.getPort()+"/test/");
+                URL url = URIBuilder.newBuilder()
+                        .scheme("http")
+                        .loopback()
+                        .port(address.getPort())
+                        .path("/test/")
+                        .toURLUnchecked();
                 HttpURLConnection uc = (HttpURLConnection)url.openConnection();
                 uc.setDoOutput (true);
                 OutputStream os = uc.getOutputStream ();
@@ -130,7 +137,9 @@
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        httpServer = com.sun.net.httpserver.HttpServer.create(
+                new InetSocketAddress(InetAddress.getLoopbackAddress(), 0),
+                0);
 
         // create HttpServer context for Part 1.
         HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
--- a/test/jdk/sun/net/www/protocol/http/ProxyTunnelServer.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/sun/net/www/protocol/http/ProxyTunnelServer.java	Wed May 15 19:47:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -66,6 +66,10 @@
         ss = new ServerSocket(0);
     }
 
+    public ProxyTunnelServer(InetAddress address) throws IOException {
+        ss = new ServerSocket(0, 0, address);
+    }
+
     static private void close(Closeable c) {
         try {
             if (c != null)
@@ -124,11 +128,12 @@
      * needAuth is set to false, Proxy-Authorization checks are not made
      */
     private void processRequests(boolean makeTunnel) throws Exception {
-
         InputStream in = clientSocket.getInputStream();
         MessageHeader mheader = new MessageHeader(in);
         String statusLine = mheader.getValue(0);
 
+        System.out.printf("Proxy: Processing request from '%s'%n", clientSocket);
+
         if (statusLine.startsWith("CONNECT")) {
             // retrieve the host and port info from the status-line
             // retrieveConnectInfo(statusLine);
@@ -311,6 +316,10 @@
         return ss.getLocalPort();
     }
 
+    public InetAddress getInetAddress() {
+        return ss.getInetAddress();
+    }
+
     /*
      * do "basic" authentication, authInfo is of the form:
      *                                  Basic <encoded username":"password>
--- a/test/jdk/sun/net/www/protocol/http/TunnelThroughProxy.java	Wed May 15 19:34:34 2019 +0100
+++ b/test/jdk/sun/net/www/protocol/http/TunnelThroughProxy.java	Wed May 15 19:47:28 2019 +0100
@@ -60,13 +60,14 @@
         ProxyTunnelServer proxy = setupProxy(true);
         try {
             int proxyPort = proxy.getPort();
-            ServerSocket server = new ServerSocket(0);
+            InetAddress proxyAddress = proxy.getInetAddress();
+            ServerSocket server = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
             int serverPort = server.getLocalPort();
 
             Socket sock;
             sock = new Socket(new Proxy(Proxy.Type.HTTP,
-                    new InetSocketAddress(InetAddress.getLoopbackAddress(), proxyPort)));
-            InetSocketAddress dest = new InetSocketAddress(InetAddress.getLoopbackAddress(), serverPort);
+                    new InetSocketAddress(proxyAddress, proxyPort)));
+            InetSocketAddress dest = new InetSocketAddress(server.getInetAddress(), serverPort);
             sock.connect(dest);
             int localPort = sock.getLocalPort();
             if (localPort == proxyPort)
@@ -84,14 +85,17 @@
     }
 
     static ProxyTunnelServer setupProxy(boolean makeTunnel) throws IOException {
-        ProxyTunnelServer pserver = new ProxyTunnelServer();
+        InetAddress proxyAddress = InetAddress.getLoopbackAddress();
+        ProxyTunnelServer pserver = new ProxyTunnelServer(proxyAddress);
         pserver.doTunnel(makeTunnel);
         int proxyPort = pserver.getPort();
 
         // disable proxy authentication
         pserver.needUserAuth(false);
         pserver.start();
-        System.setProperty("https.proxyHost", "localhost");
+        System.out.printf("Setting https.proxyHost='%s'%n", proxyAddress.getHostAddress());
+        System.setProperty("https.proxyHost", proxyAddress.getHostAddress());
+        System.out.printf("Setting https.proxyPort='%s'%n", String.valueOf(proxyPort));
         System.setProperty("https.proxyPort", String.valueOf(proxyPort));
         return pserver;
     }