8226756: Replace wildcard address with loopback or local host in tests - part 18
authoraefimov
Wed, 10 Jul 2019 16:09:20 +0100
changeset 55645 3203e857fa71
parent 55644 556313991cac
child 55646 c9b6be61b7a0
8226756: Replace wildcard address with loopback or local host in tests - part 18 Reviewed-by: dfuchs Contributed-by: Patrick Concannon <patrick.concannon@oracle.com>
test/jdk/java/net/Socket/InheritTimeout.java
test/jdk/java/net/Socket/ReadTimeout.java
test/jdk/java/net/Socket/ShutdownInput.java
test/jdk/java/net/SocketInputStream/SocketTimeout.java
test/jdk/java/net/URLConnection/ChunkedEncoding.java
test/jdk/java/net/URLConnection/GetResponseCode.java
test/jdk/java/net/URLConnection/SetIfModifiedSince.java
test/jdk/sun/net/www/protocol/http/NoCache.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsSocketFacTest.java
--- a/test/jdk/java/net/Socket/InheritTimeout.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/Socket/InheritTimeout.java	Wed Jul 10 16:09:20 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
@@ -56,10 +56,11 @@
     }
 
    InheritTimeout() throws Exception {
-        ServerSocket ss = new ServerSocket(0);
+        InetAddress ia = InetAddress.getLocalHost();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(ia, 0));
         ss.setSoTimeout(1000);
 
-        InetAddress ia = InetAddress.getLocalHost();
         InetSocketAddress isa =
             new InetSocketAddress(ia, ss.getLocalPort());
 
--- a/test/jdk/java/net/Socket/ReadTimeout.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/Socket/ReadTimeout.java	Wed Jul 10 16:09:20 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 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
@@ -47,7 +47,8 @@
     int          tout = 1000;
 
     sin = InetAddress.getLocalHost();
-    srv = new ServerSocket(port);
+    srv = new ServerSocket();
+    srv.bind(new InetSocketAddress(sin, 0));
     port = srv.getLocalPort();
     soc = new Socket(sin, port);
     soc1 = srv.accept();
--- a/test/jdk/java/net/Socket/ShutdownInput.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/Socket/ShutdownInput.java	Wed Jul 10 16:09:20 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 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
@@ -47,9 +47,9 @@
     public static void main(String args[]) throws Exception {
         IPSupport.throwSkippedExceptionIfNonOperational();
 
-        InetAddress iaddr = InetAddress.getLocalHost();
+        InetAddress iaddr = InetAddress.getLoopbackAddress();
 
-        try ( ServerSocket ss = new ServerSocket(0);
+        try (ServerSocket ss = new ServerSocket(0, 0, iaddr);
               Socket s1 = new Socket(iaddr, ss.getLocalPort());
               Socket s2 = ss.accept() ) {
 
@@ -57,7 +57,8 @@
         }
 
         // check the NIO socket adapter
-        try (ServerSocketChannel sc = ServerSocketChannel.open().bind(null);
+        InetSocketAddress socketAddress = new InetSocketAddress(iaddr, 0);
+        try (ServerSocketChannel sc = ServerSocketChannel.open().bind(socketAddress);
              SocketChannel s1 = SocketChannel.open(
                      new InetSocketAddress(iaddr, sc.socket().getLocalPort()));
              SocketChannel s2 = sc.accept() ) {
--- a/test/jdk/java/net/SocketInputStream/SocketTimeout.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/SocketInputStream/SocketTimeout.java	Wed Jul 10 16:09:20 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, 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
@@ -45,7 +45,8 @@
     ServerSocket srv = null;
     int          port = 0;
 
-    srv = new ServerSocket(0);
+    srv = new ServerSocket();
+    srv.bind(new InetSocketAddress(sin, 0));
     port = srv.getLocalPort();
     soc = new Socket(sin, port);
     soc1 = srv.accept();
--- a/test/jdk/java/net/URLConnection/ChunkedEncoding.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/URLConnection/ChunkedEncoding.java	Wed Jul 10 16:09:20 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
@@ -22,9 +22,10 @@
  */
 
 /**
- *
+ * @test
  * @bug 4333920
  * @bug 4394548
+ * @library /test/lib
  * @summary Check that chunked encoding response doesn't cause
  *          getInputStream to block until last chunk arrives.
  *          Also regression against NPE in ChunkedInputStream.
@@ -32,6 +33,7 @@
 import java.net.*;
 import java.io.*;
 import java.util.Random;
+import jdk.test.lib.net.URIBuilder;
 
 public class ChunkedEncoding implements Runnable {
 
@@ -115,6 +117,11 @@
             out.print("\r\n");
             out.flush();
 
+            /*
+             * Sleep added to avoid connection reset
+             * on the client side
+             */
+            Thread.sleep(1000);
             s.close();
             ss.close();
         } catch (Exception e) {
@@ -125,15 +132,17 @@
     ChunkedEncoding() throws Exception {
 
         /* start the server */
-        ss = new ServerSocket(0);
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         (new Thread(this)).start();
-
         /* establish http connection to server */
-        String uri = "http://localhost:" +
-                     Integer.toString(ss.getLocalPort()) +
-                     "/foo";
-        URL url = new URL(uri);
-        HttpURLConnection http = (HttpURLConnection)url.openConnection();
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .path("/foo")
+                .toURL();
+        HttpURLConnection http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
 
         /*
          * Server should only send headers if TE:trailers
--- a/test/jdk/java/net/URLConnection/GetResponseCode.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/URLConnection/GetResponseCode.java	Wed Jul 10 16:09:20 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
@@ -22,14 +22,17 @@
  */
 
 /**
- *
+ * @test
  * @bug 4191815
+ * @library /test/lib
  * @summary Check that getResponseCode doesn't throw exception if http
  *          respone code is >= 400.
  */
 import java.net.*;
 import java.io.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class GetResponseCode implements Runnable {
 
     ServerSocket ss;
@@ -56,6 +59,11 @@
             out.print("</HTML>");
             out.flush();
 
+            /*
+             * Sleep added to avoid connection reset
+             * on the client side
+             */
+            Thread.sleep(1000);
             s.close();
             ss.close();
         } catch (Exception e) {
@@ -66,16 +74,19 @@
     GetResponseCode() throws Exception {
 
         /* start the server */
-        ss = new ServerSocket(0);
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         (new Thread(this)).start();
 
         /* establish http connection to server */
-        String uri = "http://localhost:" +
-                     Integer.toString(ss.getLocalPort()) +
-                     "/missing.nothtml";
-        URL url = new URL(uri);
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .path("/missing.nohtml")
+                .toURL();
 
-        HttpURLConnection http = (HttpURLConnection)url.openConnection();
+        HttpURLConnection http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
 
         int respCode = http.getResponseCode();
 
--- a/test/jdk/java/net/URLConnection/SetIfModifiedSince.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/java/net/URLConnection/SetIfModifiedSince.java	Wed Jul 10 16:09:20 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2011, 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 4397096
+ * @library /test/lib
  * @run main/othervm SetIfModifiedSince
  * @summary setIfModifiedSince() of HttpURLConnection sets invalid date of default locale
  */
@@ -31,7 +32,10 @@
 import java.io.*;
 import java.util.*;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class SetIfModifiedSince {
+    static volatile boolean successfulHeaderCheck = false;
 
     static class XServer extends Thread {
         ServerSocket srv;
@@ -66,28 +70,42 @@
                         break;
                     }
                 }
+                successfulHeaderCheck = true;
                 s.close ();
                 srv.close (); // or else the HTTPURLConnection will retry
             } catch (IOException e) {}
         }
     }
 
-    public static void main (String[] args) {
+    public static void main(String[] args) throws Exception {
         Locale reservedLocale = Locale.getDefault();
         try {
             Locale.setDefault(Locale.JAPAN);
-            ServerSocket serversocket = new ServerSocket (0);
+            ServerSocket serversocket = new ServerSocket();
+            serversocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
             int port = serversocket.getLocalPort ();
             XServer server = new XServer (serversocket);
             server.start ();
             Thread.sleep (2000);
-            URL url = new URL ("http://localhost:"+port+"/index.html");
-            URLConnection urlc = url.openConnection ();
+            URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .loopback()
+                    .port(port)
+                    .path("/index.html")
+                    .toURLUnchecked();
+            URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
             urlc.setIfModifiedSince (10000000);
             InputStream is = urlc.getInputStream ();
-            int i=0, c;
-            Thread.sleep (5000);
-        } catch (Exception e) {
+            int i = 0, c;
+            Thread.sleep(5000);
+            if (!successfulHeaderCheck) {
+                throw new RuntimeException("Header check was unsuccessful");
+            }
+        } catch (SocketException ce) {
+            if (!successfulHeaderCheck) {
+                throw ce;
+            }
+            System.out.println("ConnectionException expected on successful check of If-modified-since header");
         } finally {
             // restore the reserved locale
             Locale.setDefault(reservedLocale);
--- a/test/jdk/sun/net/www/protocol/http/NoCache.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/http/NoCache.java	Wed Jul 10 16:09:20 2019 +0100
@@ -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
@@ -25,9 +25,11 @@
  * @test
  * @bug 7133367
  * @modules jdk.httpserver
+ * @library /test/lib
  * @summary ResponseCache.put should not be called when setUseCaches(false)
  */
 
+
 import java.net.*;
 import java.io.IOException;
 import java.util.List;
@@ -35,6 +37,7 @@
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
 import com.sun.net.httpserver.HttpServer;
+import jdk.test.lib.net.URIBuilder;
 
 public class NoCache
 {
@@ -43,9 +46,13 @@
 
         HttpServer server = startHttpServer();
         try {
-            URL url = new URL("http://" + InetAddress.getLocalHost().getHostAddress()
-                          + ":" + server.getAddress().getPort() + "/NoCache/");
-            URLConnection uc = url.openConnection();
+            URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .host(server.getAddress().getAddress())
+                    .port(server.getAddress().getPort())
+                    .path("/NoCache/")
+                    .toURLUnchecked();
+            URLConnection uc = url.openConnection(Proxy.NO_PROXY);
             uc.setUseCaches(false);
             uc.getInputStream().close();
         } finally {
@@ -70,7 +77,7 @@
 
     // HTTP Server
     static HttpServer startHttpServer() throws IOException {
-        HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
+        HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 0), 0);
         httpServer.createContext("/NoCache/", new SimpleHandler());
         httpServer.start();
         return httpServer;
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsSocketFacTest.java	Wed Jul 10 07:27:58 2019 -0700
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/HttpsSocketFacTest.java	Wed Jul 10 16:09:20 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
  * @bug 6614957
  * @summary HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
  * @modules jdk.httpserver
+ * @library /test/lib
  * @run main/othervm HttpsSocketFacTest
  *
  *     SunJSSE does not support dynamic system properties, no way to re-use
@@ -38,11 +39,12 @@
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
 import javax.net.ssl.SSLSocketFactory;
-import java.security.NoSuchAlgorithmException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.URL;
+import java.net.Proxy;
+import java.security.NoSuchAlgorithmException;
 import java.io.BufferedWriter;
 import java.io.InputStream;
 import java.io.IOException;
@@ -50,6 +52,7 @@
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
 import com.sun.net.httpserver.HttpsConfigurator;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * This class tests that the HTTPS protocol handler is using its socket factory for
@@ -103,10 +106,15 @@
 
     void doClient() throws IOException {
         InetSocketAddress address = httpsServer.getAddress();
-        URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/");
+        URL url = URIBuilder.newBuilder()
+                .scheme("https")
+                .loopback()
+                .port(address.getPort())
+                .path("/test6614957/")
+                .toURLUnchecked();
         System.out.println("trying to connect to " + url + "...");
 
-        HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
+        HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);
         SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory();
         uc.setSSLSocketFactory(sssf);
         uc.setHostnameVerifier(new AllHostnameVerifier());
@@ -129,7 +137,8 @@
      * Https Server
      */
     public void startHttpsServer() throws IOException, NoSuchAlgorithmException  {
-        httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(0), 0);
+        httpsServer = com.sun.net.httpserver.HttpsServer
+                .create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
         httpsServer.createContext("/test6614957/", new MyHandler());
         httpsServer.setHttpsConfigurator(new HttpsConfigurator(SSLContext.getDefault()));
         httpsServer.start();