8223573: Replace wildcard address with loopback or local host in tests - part 4
authordfuchs
Thu, 09 May 2019 14:23:52 +0100
changeset 54787 7748aa47b4e2
parent 54786 ebf733a324d4
child 54788 7f782ee493f7
8223573: Replace wildcard address with loopback or local host in tests - part 4 Summary: Makes a few intermittently failing tests more resilient to port reuse issues by ensuring they bind to the loopback address instead of the wildcard. Reviewed-by: alanb, chegar
test/jdk/java/net/Socket/Streams.java
test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
test/jdk/sun/net/www/AuthHeaderTest.java
test/jdk/sun/net/www/ftptest/FtpServer.java
test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java
test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java
test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java
test/jdk/sun/net/www/http/HttpURLConnection/DigestAuth.java
test/jdk/sun/net/www/protocol/http/6550798/test.java
test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java
--- a/test/jdk/java/net/Socket/Streams.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/java/net/Socket/Streams.java	Thu May 09 14:23:52 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,6 +29,8 @@
 
 import java.io.IOException;
 import java.lang.reflect.Constructor;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.concurrent.Phaser;
@@ -42,7 +44,9 @@
 
     public static void main(String[] args) throws Exception {
 
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = new ServerSocket()) {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            ss.bind(new InetSocketAddress(loopback, 0));
             runTest(OutputStreamGetter.class, ss);
             runTest(InputStreamGetter.class, ss);
         }
@@ -55,9 +59,12 @@
         throws Exception
     {
         final int port = ss.getLocalPort();
+        final InetAddress address = ss.getInetAddress();
         Socket[] sockets = new Socket[NUM_THREADS];
         for (int i=0; i<NUM_THREADS; i++) {
-            sockets[i] = new Socket("localhost", port);
+            sockets[i] = address.isAnyLocalAddress()
+                         ? new Socket("localhost", port)
+                         : new Socket(address, port);
             try (Socket socket = ss.accept()) {}
         }
 
--- a/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java	Thu May 09 14:23:52 2019 +0100
@@ -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
@@ -32,17 +32,19 @@
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.InetAddress;
 import java.net.URL;
 
 public class FtpURLConnectionLeak {
 
     public static void main(String[] args) throws Exception {
-        FtpServer server = new FtpServer(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        FtpServer server = new FtpServer(loopback, 0);
         server.setFileSystemHandler(new CustomFileSystemHandler("/"));
         server.setAuthHandler(new MyAuthHandler());
-        int port = server.getLocalPort();
+        String authority = server.getAuthority();
         server.start();
-        URL url = new URL("ftp://localhost:" + port + "/filedoesNotExist.txt");
+        URL url = new URL("ftp://" + authority + "/filedoesNotExist.txt");
         try (server) {
             for (int i = 0; i < 3; i++) {
                 try {
--- a/test/jdk/sun/net/www/AuthHeaderTest.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/AuthHeaderTest.java	Thu May 09 14:23:52 2019 +0100
@@ -96,10 +96,11 @@
     public static void main (String[] args) throws Exception {
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
         try {
-            server = new TestHttpServer (new AuthHeaderTest(), 1, 10, 0);
-            System.out.println ("Server: listening on port: " + server.getLocalPort());
-            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
+            server = new TestHttpServer (new AuthHeaderTest(), 1, 10, loopback, 0);
+            System.out.println ("Server: listening on port: " + server.getAuthority());
+            client ("http://" + server.getAuthority() + "/d1/foo.html");
         } catch (Exception e) {
             if (server != null) {
                 server.terminate();
--- a/test/jdk/sun/net/www/ftptest/FtpServer.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/ftptest/FtpServer.java	Thu May 09 14:23:52 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -59,6 +59,16 @@
      * connections on the specified port. If the port is set to 0, it will
      * automatically select an available ephemeral port.
      */
+    public FtpServer(InetAddress addr, int port) throws IOException {
+        listener = new ServerSocket();
+        listener.bind(new InetSocketAddress(addr, port));
+    }
+
+    /**
+     * Creates an instance of an FTP server which will listen for incoming
+     * connections on the specified port. If the port is set to 0, it will
+     * automatically select an available ephemeral port.
+     */
     public FtpServer(int port) throws IOException {
         listener = new ServerSocket(port);
     }
@@ -100,6 +110,17 @@
         return listener.getLocalPort();
     }
 
+    public String getAuthority() {
+        InetAddress address = listener.getInetAddress();
+        String hostaddr = address.isAnyLocalAddress()
+            ? "localhost" : address.getHostAddress();
+        if (hostaddr.indexOf(':') > -1) {
+            hostaddr = "[" + hostaddr + "]";
+        }
+        return hostaddr + ":" + getLocalPort();
+    }
+
+
     void addClient(Socket client) {
         FtpCommandHandler h = new FtpCommandHandler(client, this);
         h.setHandlers(fsh, auth);
--- a/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java	Thu May 09 14:23:52 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 4333920
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main ChunkedEncodingTest
  * @summary ChunkedEncodingTest unit test
  */
@@ -36,6 +37,7 @@
 import com.sun.net.httpserver.HttpHandler;
 import com.sun.net.httpserver.HttpExchange;
 import static java.lang.System.out;
+import jdk.test.lib.net.URIBuilder;
 
 public class ChunkedEncodingTest{
     private static MessageDigest serverDigest, clientDigest;
@@ -61,7 +63,13 @@
 
             int port = server.getAddress().getPort();
             out.println ("Server listening on port: " + port);
-            client("http://localhost:" + port + "/chunked/");
+            String url = URIBuilder.newBuilder()
+                .scheme("http")
+                .host(server.getAddress().getAddress())
+                .port(port)
+                .path("/chunked/")
+                .build().toString();
+            client(url);
 
             if (!MessageDigest.isEqual(clientMac, serverMac)) {
                 throw new RuntimeException(
@@ -83,7 +91,8 @@
      * Http Server
      */
     static HttpServer startHttpServer() throws IOException {
-        HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
         HttpHandler httpHandler = new SimpleHandler();
         httpServer.createContext("/chunked/", httpHandler);
         httpServer.start();
--- a/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingWithProgressMonitorTest.java	Thu May 09 14:23:52 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
@@ -27,6 +27,7 @@
  * @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem
  * @modules java.base/sun.net
  *          jdk.httpserver
+ * @library /test/lib
  * @run main ChunkedEncodingWithProgressMonitorTest
  */
 
--- a/test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/http/ChunkedInputStream/TestAvailable.java	Thu May 09 14:23:52 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
  * @test
  * @bug 6446990
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main/othervm TestAvailable
  * @summary HttpURLConnection#available() reads more and more data into memory
  */
@@ -35,6 +36,7 @@
 import com.sun.net.httpserver.*;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
+import jdk.test.lib.net.URIBuilder;
 
 public class TestAvailable
 {
@@ -60,7 +62,13 @@
         try {
             InetSocketAddress address = httpServer.getAddress();
 
-            URL url = new URL("http://localhost:" + address.getPort() + "/testAvailable/");
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .host(address.getAddress())
+                      .port(address.getPort())
+                      .path("/testAvailable/")
+                      .toURLUnchecked();
+
             HttpURLConnection uc = (HttpURLConnection)url.openConnection();
 
             uc.setDoOutput(true);
@@ -102,7 +110,9 @@
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress sockaddr = new InetSocketAddress(loopback, 0);
+        httpServer = com.sun.net.httpserver.HttpServer.create(sockaddr, 0);
 
         // create HttpServer context
         HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler());
--- a/test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java	Thu May 09 14:23:52 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2018, 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
@@ -84,8 +84,8 @@
     byte[] b;
     int requests;
 
-    MultiThreadTest(int port, int requests) throws Exception {
-        uri = "http://localhost:" + port + "/foo.html";
+    MultiThreadTest(String authority, int requests) throws Exception {
+        uri = "http://" + authority + "/foo.html";
 
         b = new byte [256];
         this.requests = requests;
@@ -134,14 +134,16 @@
         }
 
         /* start the server */
-        ServerSocket ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         Server svr = new Server(ss);
         svr.start();
 
         Object lock = MultiThreadTest.getLock();
         synchronized (lock) {
             for (int i=0; i<threads; i++) {
-                MultiThreadTest t = new MultiThreadTest(ss.getLocalPort(), requests);
+                MultiThreadTest t = new MultiThreadTest(svr.getAuthority(), requests);
                 t.start ();
             }
             try {
@@ -185,6 +187,16 @@
             this.ss = ss;
         }
 
+        public String getAuthority() {
+            InetAddress address = ss.getInetAddress();
+            String hostaddr = address.isAnyLocalAddress()
+                ? "localhost" : address.getHostAddress();
+            if (hostaddr.indexOf(':') > -1) {
+                hostaddr = "[" + hostaddr + "]";
+            }
+            return hostaddr + ":" + ss.getLocalPort();
+        }
+
         public Queue<Worker> workers() {
             return workers;
         }
--- a/test/jdk/sun/net/www/http/HttpURLConnection/DigestAuth.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpURLConnection/DigestAuth.java	Thu May 09 14:23:52 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 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
@@ -29,6 +29,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.Authenticator;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.PasswordAuthentication;
 import java.net.URL;
@@ -54,7 +55,6 @@
  */
 public class DigestAuth {
 
-    static final String LOCALHOST = "localhost";
     static final String EXPECT_FAILURE = null;
     static final String EXPECT_DIGEST = "Digest";
     static final String REALM = "testrealm@host.com";
@@ -119,8 +119,7 @@
             AuthenticatorImpl auth = new AuthenticatorImpl();
             Authenticator.setDefault(auth);
 
-            String url = String.format("http://%s:%d/test/",
-                    LOCALHOST, server.getPort());
+            String url = String.format("http://%s/test/", server.getAuthority());
 
             boolean success = true;
             switch (testcase) {
@@ -322,6 +321,16 @@
             this.server = server;
         }
 
+        public String getAuthority() {
+            InetAddress address = server.getAddress().getAddress();
+            String hostaddr = address.isAnyLocalAddress()
+                ? "localhost" : address.getHostAddress();
+            if (hostaddr.indexOf(':') > -1) {
+                hostaddr = "[" + hostaddr + "]";
+            }
+            return hostaddr + ":" + getPort();
+        }
+
         void setWWWAuthHeader(String wwwAuthHeader) {
             this.wwwAuthHeader = wwwAuthHeader;
         }
@@ -331,8 +340,9 @@
         }
 
         static LocalHttpServer startServer() throws IOException {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
             HttpServer httpServer = HttpServer.create(
-                    new InetSocketAddress(0), 0);
+                    new InetSocketAddress(loopback, 0), 0);
             LocalHttpServer localHttpServer = new LocalHttpServer(httpServer);
             localHttpServer.start();
 
@@ -342,7 +352,7 @@
         void start() {
             server.createContext("/test", this);
             server.start();
-            System.out.println("HttpServer: started on port " + getPort());
+            System.out.println("HttpServer: started on port " + getAuthority());
         }
 
         void stop() {
--- a/test/jdk/sun/net/www/protocol/http/6550798/test.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/6550798/test.java	Thu May 09 14:23:52 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
@@ -43,14 +43,15 @@
     public static void main(String[] args)  throws Exception {
 
         TestCache.reset();
-        HttpServer s = HttpServer.create (new InetSocketAddress(0), 10);
-        s.createContext ("/", new HttpHandler () {
-            public void handle (HttpExchange e) {
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
+        s.createContext("/", new HttpHandler() {
+            public void handle(HttpExchange e) {
                 try {
                     byte[] buf = new byte [LEN];
                     OutputStream o = e.getResponseBody();
                     e.sendResponseHeaders(200, LEN);
-                    o.write (buf);
+                    o.write(buf);
                     e.close();
                 } catch (IOException ex) {
                     ex.printStackTrace();
@@ -91,10 +92,10 @@
         }
 
         if (TestCache.fail) {
-            System.out.println ("TEST FAILED");
-            throw new RuntimeException ();
+            System.out.println("TEST FAILED");
+            throw new RuntimeException();
         } else {
-            System.out.println ("TEST OK");
+            System.out.println("TEST OK");
         }
     }
 }
--- a/test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java	Thu May 09 14:28:30 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java	Thu May 09 14:23:52 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 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 6189206
  * @modules java.base/sun.net.www
+ * @library /test/lib
  * @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader
  * @summary  HTTP client should set "Connection: close" header in request when keepalive is disabled
  */
@@ -33,7 +34,7 @@
 import java.util.*;
 import java.io.*;
 import sun.net.www.MessageHeader;
-
+import jdk.test.lib.net.URIBuilder;
 
 public class CloseOptionHeader implements Runnable {
     static ServerSocket ss;
@@ -79,14 +80,20 @@
         Thread tester = new Thread(new CloseOptionHeader());
 
         /* start the server */
-        ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         tester.start();
 
         /* connect to the server just started
          * server then check the request to see whether
          * there is a close connection option header in it
          */
-        URL url = new URL("http://localhost:" + ss.getLocalPort());
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .host(ss.getInetAddress())
+            .port(ss.getLocalPort())
+            .toURL();
         HttpURLConnection huc = (HttpURLConnection)url.openConnection();
         huc.connect();
         huc.getResponseCode();