8223632: Replace wildcard address with loopback or local host in tests - part 5
authordfuchs
Mon, 13 May 2019 17:31:23 +0100
changeset 54822 f542a3a135bd
parent 54821 85ccac8a8c13
child 54823 1b940da275d2
8223632: Replace wildcard address with loopback or local host in tests - part 5 Summary: Replaces wildcard usage by loopback, when possible, adds intermittent keyword and a comment, when not. Reviewed-by: chegar
test/jdk/java/net/ProxySelector/LoopbackAddresses.java
test/jdk/java/net/ProxySelector/ProxyTest.java
test/jdk/java/net/ResponseCache/B6181108.java
test/jdk/java/net/SocketInputStream/SocketClosedException.java
test/jdk/java/net/SocketPermission/SocketPermissionTest.java
test/jdk/java/net/URLConnection/DisconnectAfterEOF.java
test/jdk/java/net/URLConnection/contentHandler/UserContentHandler.java
test/jdk/sun/net/www/http/HttpClient/B7025238.java
test/jdk/sun/net/www/http/HttpClient/ProxyFromCache.java
--- a/test/jdk/java/net/ProxySelector/LoopbackAddresses.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/ProxySelector/LoopbackAddresses.java	Mon May 13 17:31:23 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, 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,9 +23,12 @@
 
 /* @test
  * @bug 4924226
- * @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server
+ * @key intermittent
+ * @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server.
+ *          This test might fail intermittently as it needs a server that
+ *          binds to the wildcard address.
  * @modules java.base/sun.net.www
- * @library ../../../sun/net/www/httptest/
+ * @library ../../../sun/net/www/httptest/ /test/lib
  * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
  * @compile LoopbackAddresses.java
  * @run main/othervm LoopbackAddresses
@@ -33,6 +36,7 @@
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * Our default proxy selector should bypass localhost and loopback
@@ -53,12 +57,18 @@
 
     public static void main(String[] args) {
         try {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+
+            // This server needs to bind to the wildcard address as we want it
+            // to answer both for the loopback and "localhost".
+            // Though "localhost" usually point to the loopback there is no
+            // hard guarantee.
             server = new TestHttpServer (new LoopbackAddresses(), 1, 10, 0);
             ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
             // start proxy server
             new Thread(pserver).start();
 
-            System.setProperty("http.proxyHost", "localhost");
+            System.setProperty("http.proxyHost", loopback.getHostAddress());
             System.setProperty("http.proxyPort", pserver.getPort()+"");
 
             URL url = new URL("http://localhost:"+server.getLocalPort());
@@ -72,7 +82,11 @@
             }
 
             try {
-                url = new URL("http://127.0.0.1:"+server.getLocalPort());
+                url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .host(loopback.getHostAddress())
+                      .port(server.getLocalPort())
+                      .toURL();
                 HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
                 int respCode = urlc.getResponseCode();
                 urlc.disconnect();
@@ -104,7 +118,8 @@
         public ProxyServer(InetAddress server, int port) throws IOException {
             serverInetAddr = server;
             serverPort = port;
-            ss = new ServerSocket(0);
+            ss = new ServerSocket();
+            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
         }
 
         public void run() {
--- a/test/jdk/java/net/ProxySelector/ProxyTest.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/ProxySelector/ProxyTest.java	Mon May 13 17:31:23 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2012, 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
@@ -26,7 +26,7 @@
  * @bug 4696512
  * @summary HTTP client: Improve proxy server configuration and selection
  * @modules java.base/sun.net.www
- * @library ../../../sun/net/www/httptest/
+ * @library ../../../sun/net/www/httptest/ /test/lib
  * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
  * @compile ProxyTest.java
  * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest
@@ -35,6 +35,7 @@
 import java.net.*;
 import java.io.*;
 import java.util.ArrayList;
+import jdk.test.lib.net.URIBuilder;
 
 public class ProxyTest implements HttpCallback {
     static TestHttpServer server;
@@ -52,8 +53,8 @@
     }
 
     static public class MyProxySelector extends ProxySelector {
-        private ProxySelector def = null;
-        private ArrayList<Proxy> noProxy;
+        private static volatile URI lastURI;
+        private final ArrayList<Proxy> noProxy;
 
         public MyProxySelector() {
             noProxy = new ArrayList<Proxy>(1);
@@ -61,26 +62,38 @@
         }
 
         public java.util.List<Proxy> select(URI uri) {
+            System.out.println("Selecting no proxy for " + uri);
+            lastURI = uri;
             return noProxy;
         }
 
         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
         }
+
+        public static URI lastURI() { return lastURI; }
     }
 
-
     public static void main(String[] args) {
         ProxySelector defSelector = ProxySelector.getDefault();
         if (defSelector == null)
             throw new RuntimeException("Default ProxySelector is null");
         ProxySelector.setDefault(new MyProxySelector());
         try {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
             server = new TestHttpServer (new ProxyTest(), 1, 10, 0);
-            URL url = new URL("http://localhost:"+server.getLocalPort());
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .loopback()
+                      .port(server.getLocalPort())
+                      .toURL();
             System.out.println ("client opening connection to: " + url);
             HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
             InputStream is = urlc.getInputStream ();
             is.close();
+            URI lastURI = MyProxySelector.lastURI();
+            if (!String.valueOf(lastURI).equals(url + "/")) {
+                throw new AssertionError("Custom proxy was not used: last URI was " + lastURI);
+            }
         } catch (Exception e) {
                 throw new RuntimeException(e);
         } finally {
--- a/test/jdk/java/net/ResponseCache/B6181108.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/ResponseCache/B6181108.java	Mon May 13 17:31:23 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2010, 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
@@ -24,6 +24,7 @@
 /**
  * @test
  * @bug 6181108
+ * @library /test/lib
  * @summary double encoded URL passed to ResponseCache
  * @author Edward Wang
  */
@@ -31,7 +32,7 @@
 import java.net.*;
 import java.util.*;
 import java.io.*;
-
+import jdk.test.lib.net.URIBuilder;
 
 public class B6181108 implements Runnable {
     ServerSocket ss;
@@ -44,7 +45,7 @@
         try {
             Socket s = ss.accept();
 
-            InputStream is = s.getInputStream ();
+            InputStream is = s.getInputStream();
             BufferedReader r = new BufferedReader(new InputStreamReader(is));
             String x;
             while ((x=r.readLine()) != null) {
@@ -75,32 +76,38 @@
     }
 
     static class ResponseCache extends java.net.ResponseCache {
-        public CacheResponse get (URI uri, String method, Map<String,List<String>> hdrs) {
-            System.out.println ("get uri = " + uri);
+        public CacheResponse get(URI uri, String method, Map<String,List<String>> hdrs) {
+            System.out.println("get uri = " + uri);
             if (!urlWithSpace.equals(uri.toString())) {
                 throw new RuntimeException("test failed");
             }
             return null;
         }
-        public CacheRequest put (URI uri,  URLConnection urlc) {
-            System.out.println ("put uri = " + uri);
+        public CacheRequest put(URI uri,  URLConnection urlc) {
+            System.out.println("put uri = " + uri);
             return null;
         }
     }
 
     B6181108() throws Exception {
         /* start the server */
-        ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         (new Thread(this)).start();
 
-        ResponseCache.setDefault (new ResponseCache());
-        urlWithSpace = "http://localhost:" +
-                        Integer.toString(ss.getLocalPort()) +
-                        "/space%20test/page1.html";
-        URL url = new URL (urlWithSpace);
+        ResponseCache.setDefault(new ResponseCache());
+        String base = URIBuilder.newBuilder()
+                   .scheme("http")
+                   .loopback()
+                   .port(ss.getLocalPort())
+                   .build()
+                   .toString();
+        urlWithSpace = base + "/space%20test/page1.html";
+        URL url = new URL(urlWithSpace);
         URLConnection urlc = url.openConnection();
         int i = ((HttpURLConnection)(urlc)).getResponseCode();
-        System.out.println ("response code = " + i);
+        System.out.println("response code = " + i);
         ResponseCache.setDefault(null);
     }
 
--- a/test/jdk/java/net/SocketInputStream/SocketClosedException.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/SocketInputStream/SocketClosedException.java	Mon May 13 17:31:23 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
@@ -51,7 +51,8 @@
     }
 
     static void doClientSide(int port) throws Exception {
-        Socket socket = new Socket("localhost", port);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        Socket socket = new Socket(loopback, port);
         InputStream is = socket.getInputStream();
 
         is.read();
@@ -64,7 +65,9 @@
 
     public static void main(String[] args) throws Exception {
         IPSupport.throwSkippedExceptionIfNonOperational();
-        serverSocket = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        serverSocket = new ServerSocket();
+        serverSocket.bind(new InetSocketAddress(loopback, 0));
         startServer();
         try {
             doClientSide(serverSocket.getLocalPort());
--- a/test/jdk/java/net/SocketPermission/SocketPermissionTest.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/SocketPermission/SocketPermissionTest.java	Mon May 13 17:31:23 2019 +0100
@@ -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
@@ -24,7 +24,10 @@
 /*
  * @test
  * @bug 8047031
- * @summary SocketPermission tests for legacy socket types
+ * @key intermittent
+ * @summary SocketPermission tests for legacy socket types.
+ *          This test needs to bind its servers to the wildcard
+ *          address and as such may fail intermittently.
  * @library /test/lib
  * @build jdk.test.lib.NetworkConfiguration
  *        jdk.test.lib.Platform
--- a/test/jdk/java/net/URLConnection/DisconnectAfterEOF.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/URLConnection/DisconnectAfterEOF.java	Mon May 13 17:31:23 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 4774503
+ * @library /test/lib
  * @summary Calling HttpURLConnection's disconnect method after the
  *          response has been received causes havoc with persistent
  *          connections.
@@ -31,6 +32,9 @@
 import java.net.*;
 import java.io.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
+
+
 
 public class DisconnectAfterEOF {
 
@@ -239,13 +243,18 @@
 
     public static void main(String args[]) throws Exception {
         // start 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();
 
-        String uri = "http://localhost:" +
-                     Integer.toString(ss.getLocalPort()) +
-                     "/foo.html";
+        String uri = URIBuilder.newBuilder()
+                     .scheme("http")
+                     .loopback()
+                     .port(ss.getLocalPort())
+                     .path("/foo.html")
+                     .build().toString();
 
         /*
          * The following is the test scenario we create here :-
--- a/test/jdk/java/net/URLConnection/contentHandler/UserContentHandler.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/java/net/URLConnection/contentHandler/UserContentHandler.java	Mon May 13 17:31:23 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2010, 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
@@ -24,6 +24,7 @@
 /* @test
  * @bug 4191147
  * @summary 1.2beta4 does not load user defined content handlers
+ * @library /test/lib
  * @build UserContentHandler
  * @run main/othervm UserContentHandler
  */
@@ -38,6 +39,7 @@
 import java.net.*;
 import java.io.*;
 import java.util.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class UserContentHandler implements Runnable {
 
@@ -74,7 +76,9 @@
 
     UserContentHandler() throws Exception {
 
-        ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         Thread thr = new Thread(this);
         thr.start();
 
@@ -87,8 +91,13 @@
         props.put("java.content.handler.pkgs", "COM.foo.content");
         System.setProperties(props);
 
-        URL u = new URL("http://localhost:" + ss.getLocalPort() +
-                        "/anything.txt");
+        URL u = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .path("/anything.txt")
+                .toURL();
+
         if (!(u.openConnection().getContent() instanceof String)) {
             throw new RuntimeException("Load user defined content handler failed.");
         } else {
--- a/test/jdk/sun/net/www/http/HttpClient/B7025238.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpClient/B7025238.java	Mon May 13 17:31:23 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
@@ -26,11 +26,13 @@
 import java.io.OutputStream;
 import java.net.*;
 import java.util.concurrent.Executors;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * @test
  * @bug 7025238
  * @modules jdk.httpserver
+ * @library /test/lib
  * @summary HttpURLConnection does not handle URLs with an empty path component
  */
 public class B7025238 {
@@ -44,7 +46,13 @@
         try {
             s = new Server();
             s.startServer();
-            URL url = new URL("http://localhost:" + s.getPort() + "?q=test");
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .loopback()
+                      .port(s.getPort())
+                      .query("q=test")
+                      .toURL();
+            System.out.println("Connecting to: " + url);
             HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
             urlConnection.setRequestMethod("GET");
             urlConnection.connect();
@@ -62,7 +70,8 @@
         HttpServer server;
 
         public void startServer() {
-            InetSocketAddress addr = new InetSocketAddress(0);
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            InetSocketAddress addr = new InetSocketAddress(loopback, 0);
             try {
                 server = HttpServer.create(addr, 0);
             } catch (IOException ioe) {
@@ -101,4 +110,3 @@
         }
     }
 }
-
--- a/test/jdk/sun/net/www/http/HttpClient/ProxyFromCache.java	Mon May 13 16:43:47 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpClient/ProxyFromCache.java	Mon May 13 17:31:23 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 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
@@ -26,12 +26,14 @@
  * @bug 6498566
  * @summary URL.openConnection(Proxy.NO_PROXY) may connect through a proxy.
  * @modules java.base/sun.net.www
+ * @library /test/lib
  * @run main/othervm ProxyFromCache
  */
 
 import java.net.*;
 import java.io.*;
 import sun.net.www.MessageHeader;
+import jdk.test.lib.net.URIBuilder;
 
 /* Creates a simple proxy and http server that just return 200 OK.
  * Open a URL pointing to the http server and specify that the
@@ -43,18 +45,21 @@
 
 public class ProxyFromCache
 {
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         ServerSocket proxySSocket, httpSSocket;
         int proxyPort, httpPort;
+        InetAddress loopback = InetAddress.getLoopbackAddress();
 
         try {
-            proxySSocket = new ServerSocket(0);
+            proxySSocket = new ServerSocket();
+            proxySSocket.bind(new InetSocketAddress(loopback, 0));
             proxyPort = proxySSocket.getLocalPort();
-            httpSSocket = new ServerSocket(0);
+            httpSSocket = new ServerSocket();
+            httpSSocket.bind(new InetSocketAddress(loopback, 0));
             httpPort = httpSSocket.getLocalPort();
         } catch (Exception e) {
             System.out.println ("Exception: " + e);
-            return;
+            throw e;
         }
 
         SimpleServer proxyServer = new SimpleServer(proxySSocket);
@@ -62,12 +67,18 @@
         SimpleServer httpServer = new SimpleServer(httpSSocket);
         httpServer.start();
 
-        InetSocketAddress addr = new InetSocketAddress("localhost", proxyPort);
+        InetSocketAddress addr = new InetSocketAddress(loopback, proxyPort);
         Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
 
         try {
-            String urlStr = "http://localhost:" + httpPort + "/";
-            URL url = new URL(urlStr);
+            URL url = URIBuilder.newBuilder()
+                      .scheme("http")
+                      .loopback()
+                      .port(httpPort)
+                      .path("/")
+                      .toURL();
+
+            String urlStr = url.toString();
 
             // 1st connection.
             HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
@@ -100,7 +111,7 @@
                 throw new RuntimeException("Failed: Proxy being sent " + proxyCount  + " requests");
             }
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw e;
         }
     }
 }