8224035: Replace wildcard address with loopback or local host in tests - part 9
authoraefimov
Mon, 27 May 2019 13:29:11 +0100
changeset 55045 3a8433d967ea
parent 55044 d3afe760b392
child 55046 7f03c3e42628
8224035: Replace wildcard address with loopback or local host in tests - part 9 Reviewed-by: dfuchs
test/jdk/java/net/ServerSocket/ThreadStop.java
test/jdk/java/net/Socket/asyncClose/Race.java
test/jdk/java/net/URLClassLoader/HttpTest.java
test/jdk/java/net/URLConnection/TimeoutTest.java
test/jdk/sun/net/www/http/HttpClient/CookieHttpClientTest.java
test/jdk/sun/net/www/protocol/http/HttpInputStream.java
test/jdk/sun/net/www/protocol/http/HttpStreams.java
test/jdk/sun/net/www/protocol/http/RedirectOnPost.java
test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java
test/jdk/sun/net/www/protocol/http/SetIfModifiedSince.java
--- a/test/jdk/java/net/ServerSocket/ThreadStop.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/java/net/ServerSocket/ThreadStop.java	Mon May 27 13:29:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2016, 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
@@ -39,7 +39,8 @@
         ServerSocket ss;
 
         Server() throws IOException {
-            ss = new ServerSocket(0);
+            ss = new ServerSocket();
+            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         }
 
         public int localPort() {
@@ -81,7 +82,7 @@
         // still in accept() so we connect to server which causes
         // it to unblock and do JNI-stuff with a pending exception
 
-        try (Socket s = new Socket("localhost", svr.localPort())) {
+        try (Socket s = new Socket(svr.ss.getInetAddress(), svr.localPort())) {
         } catch (IOException ioe) {
         }
         thr.join();
--- a/test/jdk/java/net/Socket/asyncClose/Race.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/java/net/Socket/asyncClose/Race.java	Mon May 27 13:29:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 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
@@ -28,6 +28,8 @@
  */
 
 import java.io.InputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.ConnectException;
@@ -40,12 +42,14 @@
     final static int THREADS = 100;
 
     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));
             final int port = ss.getLocalPort();
             final Phaser phaser = new Phaser(THREADS + 1);
             for (int i=0; i<100; i++) {
                 try {
-                    final Socket s = new Socket("localhost", port);
+                    final Socket s = new Socket(loopback, port);
                     s.setSoLinger(false, 0);
                     try (Socket sa = ss.accept()) {
                         sa.setSoLinger(false, 0);
--- a/test/jdk/java/net/URLClassLoader/HttpTest.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/java/net/URLClassLoader/HttpTest.java	Mon May 27 13:29:11 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
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 4636331
+ * @library /test/lib
  * @summary Check that URLClassLoader doesn't create excessive http
  *          connections
  */
@@ -31,6 +32,8 @@
 import java.io.*;
 import java.util.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class HttpTest {
 
     /*
@@ -119,7 +122,8 @@
         }
 
         HttpServer() throws Exception {
-            ss = new ServerSocket(0);
+            ss = new ServerSocket();
+            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         }
 
         public void run() {
@@ -200,9 +204,12 @@
         HttpServer svr = HttpServer.create();
 
         // create class loader
-        URL urls[] =
-            { new URL("http://localhost:" + svr.port() + "/dir1/"),
-              new URL("http://localhost:" + svr.port() + "/dir2/") };
+        URL urls[] = {
+                URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
+                        .path("/dir1/").toURL(),
+                URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())
+                        .path("/dir2/").toURL(),
+        };
         URLClassLoader cl = new URLClassLoader(urls);
 
         // Test 1 - check that getResource does single HEAD request
--- a/test/jdk/java/net/URLConnection/TimeoutTest.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/java/net/URLConnection/TimeoutTest.java	Mon May 27 13:29:11 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
@@ -67,7 +67,8 @@
     }
 
     public void test() throws Exception {
-        ServerSocket ss = new ServerSocket(0);
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         Server s = new Server (ss);
         try{
             URL url = URIBuilder.newBuilder()
--- a/test/jdk/sun/net/www/http/HttpClient/CookieHttpClientTest.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpClient/CookieHttpClientTest.java	Mon May 27 13:29:11 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
@@ -24,6 +24,7 @@
 /*
  * @test
  * @bug 7129083
+ * @library /test/lib
  * @summary Cookiemanager does not store cookies if url is read
  *          before setting cookiemanager
  */
@@ -31,12 +32,16 @@
 import java.net.CookieHandler;
 import java.net.CookieManager;
 import java.net.CookiePolicy;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
 import java.io.InputStream;
 import java.io.IOException;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class CookieHttpClientTest implements Runnable {
     final ServerSocket ss;
     static final int TIMEOUT = 10 * 1000;
@@ -85,10 +90,15 @@
 
     CookieHttpClientTest() throws Exception {
         /* start the server */
-        ss = new ServerSocket(0);
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         (new Thread(this)).start();
 
-        URL url = new URL("http://localhost:" + ss.getLocalPort() +"/");
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .path("/").toURL();
 
         // Run without a CookieHandler first
         InputStream in = url.openConnection().getInputStream();
--- a/test/jdk/sun/net/www/protocol/http/HttpInputStream.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/HttpInputStream.java	Mon May 27 13:29:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2016, 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
@@ -23,6 +23,7 @@
 
 /* @test
  * @bug 4937598
+ * @library /test/lib
  * @summary http://www.clipstream.com video does not play; read() problem
  */
 
@@ -30,10 +31,14 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class HttpInputStream {
 
     private static final int CONTENT_LENGTH = 20;
@@ -45,7 +50,9 @@
         static final int TIMEOUT = 10 * 1000;
 
         Server() throws IOException {
-            serverSocket = new ServerSocket(0);
+            serverSocket = new ServerSocket();
+            serverSocket.bind(new InetSocketAddress(
+                    InetAddress.getLoopbackAddress(), 0));
         }
 
         void readOneRequest(InputStream is) throws IOException {
@@ -106,7 +113,12 @@
     public static void main(String args[]) throws IOException {
         try (Server server = new Server()) {
             (new Thread(server)).start();
-            URL url = new URL("http://localhost:" + server.getPort() + "/anything");
+            URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(server.getPort())
+                    .path("/anything")
+                    .toURLUnchecked();
             try (InputStream is = url.openConnection().getInputStream()) {
                 if (read(is) != CONTENT_LENGTH) {
                     throw new RuntimeException("HttpInputStream.read() failed with 0xff");
--- a/test/jdk/sun/net/www/protocol/http/HttpStreams.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/HttpStreams.java	Mon May 27 13:29:11 2019 +0100
@@ -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
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 8011719
+ * @library /test/lib
  * @modules jdk.httpserver
  * @summary Basic checks to verify behavior of returned input streams
  */
@@ -36,6 +37,8 @@
 import java.nio.charset.StandardCharsets;
 import java.util.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class HttpStreams {
 
     void client(String u) throws Exception {
@@ -56,24 +59,33 @@
         expectThrow(() -> { is.read(ba, 0, 2); }, "read on closed stream should throw: " + u);
     }
 
+    String constructUrlString(int port, String path) throws Exception {
+        return URIBuilder.newBuilder()
+                .scheme("http")
+                .port(port)
+                .loopback()
+                .path(path)
+                .toURL().toString();
+    }
+
     void test() throws Exception {
         HttpServer server = null;
         try {
             server = startHttpServer();
-            String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/";
-            client(baseUrl +  "chunked/");
-            client(baseUrl +  "fixed/");
-            client(baseUrl +  "error/");
-            client(baseUrl +  "chunkedError/");
+            int serverPort = server.getAddress().getPort();
+            client(constructUrlString(serverPort, "/chunked/"));
+            client(constructUrlString(serverPort, "/fixed/"));
+            client(constructUrlString(serverPort, "/error/"));
+            client(constructUrlString(serverPort, "/chunkedError/"));
 
             // Test with a response cache
             ResponseCache ch = ResponseCache.getDefault();
             ResponseCache.setDefault(new TrivialCacheHandler());
             try {
-                client(baseUrl +  "chunked/");
-                client(baseUrl +  "fixed/");
-                client(baseUrl +  "error/");
-                client(baseUrl +  "chunkedError/");
+                client(constructUrlString(serverPort, "/chunked/"));
+                client(constructUrlString(serverPort, "/fixed/"));
+                client(constructUrlString(serverPort, "/error/"));
+                client(constructUrlString(serverPort, "/chunkedError/"));
             } finally {
                 ResponseCache.setDefault(ch);
             }
@@ -93,7 +105,8 @@
 
     // HTTP Server
     HttpServer startHttpServer() throws IOException {
-        HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
+        HttpServer httpServer = HttpServer.create();
+        httpServer.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
         httpServer.createContext("/chunked/", new ChunkedHandler());
         httpServer.createContext("/fixed/", new FixedHandler());
         httpServer.createContext("/error/", new ErrorHandler());
--- a/test/jdk/sun/net/www/protocol/http/RedirectOnPost.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/RedirectOnPost.java	Mon May 27 13:29:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, 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
@@ -165,7 +165,8 @@
     private static HttpServer getHttpServer(ExecutorService execs)
         throws Exception
     {
-        InetSocketAddress inetAddress = new InetSocketAddress(0);
+        InetSocketAddress inetAddress = new InetSocketAddress(
+                InetAddress.getLoopbackAddress(), 0);
         HttpServer testServer = HttpServer.create(inetAddress, 15);
         int port = testServer.getAddress().getPort();
         testServer.setExecutor(execs);
@@ -180,7 +181,8 @@
     )
         throws Exception
     {
-        InetSocketAddress inetAddress = new InetSocketAddress(0);
+        InetSocketAddress inetAddress = new InetSocketAddress(
+                InetAddress.getLoopbackAddress(), 0);
         HttpsServer testServer = HttpsServer.create(inetAddress, 15);
         int port = testServer.getAddress().getPort();
         testServer.setExecutor(execs);
--- a/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java	Mon May 27 13:29:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2012, 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
@@ -67,7 +67,8 @@
 
     public static void main (String[] args) throws Exception {
         try {
-            server = new TestHttpServer (new SetChunkedStreamingMode(), 1, 10, 0);
+            server = new TestHttpServer(new SetChunkedStreamingMode(), 1, 10,
+                    InetAddress.getLoopbackAddress(), 0);
             System.out.println ("Server: listening on port: " + server.getLocalPort());
             URL url = URIBuilder.newBuilder()
                 .scheme("http")
--- a/test/jdk/sun/net/www/protocol/http/SetIfModifiedSince.java	Thu May 23 15:15:01 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/http/SetIfModifiedSince.java	Mon May 27 13:29:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2000, 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
@@ -22,13 +22,15 @@
  */
 
 /* @test
-   @bug 4213164 8172253
-   @summary setIfModifiedSince mehtod in HttpURLConnection sometimes fails
-   */
+ * @bug 4213164 8172253
+ * @library /test/lib
+ * @summary setIfModifiedSince method in HttpURLConnection sometimes fails
+ */
 import java.util.*;
 import java.io.*;
 import java.net.*;
-import java.text.*;
+
+import jdk.test.lib.net.URIBuilder;
 
 public class SetIfModifiedSince implements Runnable {
 
@@ -75,7 +77,8 @@
 
   public SetIfModifiedSince() throws Exception {
 
-     serverSock = new ServerSocket(0);
+      serverSock = new ServerSocket();
+      serverSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
      int port = serverSock.getLocalPort();
 
      Thread thr = new Thread(this);
@@ -86,8 +89,12 @@
      HttpURLConnection con;
 
      //url = new URL(args[0]);
-     url = new URL("http://localhost:" + String.valueOf(port) +
-                   "/anything");
+      url = URIBuilder.newBuilder()
+              .scheme("http")
+              .loopback()
+              .port(port)
+              .path("/anything")
+              .toURL();
      con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
 
      con.setIfModifiedSince(date.getTime());