8230858: Replace wildcard address with loopback or local host in tests - part 23
authordfuchs
Thu, 12 Sep 2019 15:46:11 +0100
changeset 58108 5302477c8285
parent 58106 79186d82463e
child 58110 85e1de070bef
8230858: Replace wildcard address with loopback or local host in tests - part 23 Summary: Add new traces for better diagnosis, refrain binding to the wildcard address when possible. Reviewed-by: chegar, xuelei
test/jdk/java/net/CookieHandler/CookieManagerTest.java
test/jdk/java/net/Socket/HttpProxy.java
test/jdk/java/net/Socket/NullHost.java
test/jdk/sun/net/www/http/KeepAliveCache/B5045306.java
test/jdk/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/DNSIdentities.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPIdentities.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Identities.java
test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ImpactOnSNI.java
test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHostnameVerifier.java
test/jdk/sun/net/www/protocol/jar/B4957695.java
--- a/test/jdk/java/net/CookieHandler/CookieManagerTest.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/java/net/CookieHandler/CookieManagerTest.java	Thu Sep 12 15:46:11 2019 +0100
@@ -26,7 +26,8 @@
  * @summary Unit test for java.net.CookieManager
  * @bug 6244040 7150552 7051862
  * @modules jdk.httpserver
- * @run main/othervm -ea CookieManagerTest
+ *          java.logging
+ * @run main/othervm -ea -esa CookieManagerTest
  * @author Edward Wang
  */
 
@@ -36,6 +37,8 @@
 import java.util.List;
 import java.io.IOException;
 import java.net.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import static java.net.Proxy.NO_PROXY;
 
 public class CookieManagerTest {
@@ -63,6 +66,11 @@
     }
 
     public static void main(String[] args) throws Exception {
+        // logs everything...
+        Logger root = Logger.getLogger("");
+        root.setLevel(Level.ALL);
+        root.getHandlers()[0].setLevel(Level.ALL);
+
         startHttpServer();
         makeHttpCall();
 
--- a/test/jdk/java/net/Socket/HttpProxy.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/java/net/Socket/HttpProxy.java	Thu Sep 12 15:46:11 2019 +0100
@@ -160,6 +160,7 @@
             public void run() {
                 try { simpleWrite(os, start); }
                 catch (Exception e) {unexpected(e); }
+                finally { out.println(threadName + ": done"); }
             }}, threadName)).start();
     }
 
@@ -170,6 +171,7 @@
             b[1] = (byte) (i % 256);
             os.write(b);
         }
+        out.println("Wrote " + start + " -> " + (start + 100));
     }
 
     void simpleRead(InputStream is, int start) throws Exception {
@@ -184,6 +186,7 @@
             if (r != i)
                 throw new Exception("read " + r + " expected " +i);
         }
+        out.println("Read " + start + " -> " + (start + 100));
     }
 
     int bytes(byte b1, byte b2) {
@@ -249,6 +252,7 @@
 
             // retrieve the host and port info from the status-line
             InetSocketAddress serverAddr = getConnectInfo(statusLine);
+            out.println("Proxy serving CONNECT request to " + serverAddr);
 
             //open socket to the server
             try (Socket serverSocket = new Socket(serverAddr.getAddress(),
--- a/test/jdk/java/net/Socket/NullHost.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/java/net/Socket/NullHost.java	Thu Sep 12 15:46:11 2019 +0100
@@ -46,8 +46,10 @@
             return svr.getLocalPort();
         }
 
+        volatile boolean done;
         public void shutdown() {
             try {
+                done = true;
                 svr.close();
             } catch (IOException e) {
             }
@@ -56,11 +58,12 @@
         public void run() {
             Socket s;
             try {
-                while (true) {
+                while (!done) {
                     s = svr.accept();
                     s.close();
                 }
             } catch (IOException e) {
+                if (!done) e.printStackTrace();
             }
         }
     }
@@ -74,13 +77,9 @@
         int port = s.getPort();
         s.start();
         try {
-            Socket sock = new Socket((String)null, port);
-            sock.close();
-            sock = new Socket((String)null, port, true);
-            sock.close();
-            sock = new Socket((String)null, port, null, 0);
-            sock.close();
-
+            try (var sock = new Socket((String)null, port)) {}
+            try (var sock = new Socket((String)null, port, true)) {}
+            try (var sock = new Socket((String)null, port, null, 0)) {}
         } catch (NullPointerException e) {
             throw new RuntimeException("Got a NPE");
         } finally {
--- a/test/jdk/sun/net/www/http/KeepAliveCache/B5045306.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/http/KeepAliveCache/B5045306.java	Thu Sep 12 15:46:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 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
@@ -62,7 +62,7 @@
     public static void startHttpServer() {
         try {
             httpTrans = new SimpleHttpTransaction();
-            server = new TestHttpServer(httpTrans, 1, 10, 0);
+            server = new TestHttpServer(httpTrans, 1, 10, InetAddress.getLocalHost(), 0);
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -71,13 +71,14 @@
     public static void clientHttpCalls() {
         try {
             System.out.println("http server listen on: " + server.getLocalPort());
-            String baseURLStr = "http://" + InetAddress.getLocalHost().getHostAddress() + ":" +
-                                  server.getLocalPort() + "/";
+            String hostAddr =  InetAddress.getLocalHost().getHostAddress();
+            if (hostAddr.indexOf(':') > -1) hostAddr = "[" + hostAddr + "]";
+            String baseURLStr = "http://" + hostAddr + ":" + server.getLocalPort() + "/";
 
             URL bigDataURL = new URL (baseURLStr + "firstCall");
             URL smallDataURL = new URL (baseURLStr + "secondCall");
 
-            HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection();
+            HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection(Proxy.NO_PROXY);
 
             //Only read 1 byte of response data and close the stream
             InputStream is = uc.getInputStream();
@@ -88,7 +89,7 @@
             // Allow the KeepAliveStreamCleaner thread to read the data left behind and cache the connection.
             try { Thread.sleep(2000); } catch (Exception e) {}
 
-            uc = (HttpURLConnection)smallDataURL.openConnection();
+            uc = (HttpURLConnection)smallDataURL.openConnection(Proxy.NO_PROXY);
             uc.getResponseCode();
 
             if (SimpleHttpTransaction.failed)
@@ -96,7 +97,7 @@
 
             // Part 2
             URL part2Url = new URL (baseURLStr + "part2");
-            uc = (HttpURLConnection)part2Url.openConnection();
+            uc = (HttpURLConnection)part2Url.openConnection(Proxy.NO_PROXY);
             is = uc.getInputStream();
             is.close();
 
--- a/test/jdk/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java	Thu Sep 12 15:46:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2016, 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
@@ -42,7 +42,10 @@
 import java.io.BufferedWriter;
 import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
+import java.net.InetAddress;
+import java.net.Proxy;
 import java.net.URL;
+import java.net.UnknownHostException;
 
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLContext;
@@ -64,6 +67,10 @@
         (new ServerIdentityTest()).run();
     }
 
+    ServerIdentityTest() throws UnknownHostException {
+        serverAddress = InetAddress.getByName(hostname);
+    }
+
     @Override
     protected boolean isCustomizedClientConnection() {
         return true;
@@ -88,7 +95,7 @@
         HttpURLConnection urlc = null;
         InputStream is = null;
         try {
-            urlc = (HttpURLConnection)url.openConnection();
+            urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
             is = urlc.getInputStream();
         } finally {
             if (is != null) {
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/DNSIdentities.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/DNSIdentities.java	Thu Sep 12 15:46:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015, 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
@@ -651,8 +651,13 @@
             serverModulus, serverPrivateExponent, passphrase);
         SSLServerSocketFactory sslssf = context.getServerSocketFactory();
 
+        // doClientSide() connects to "localhost"
+        InetAddress localHost = InetAddress.getByName("localhost");
+        InetSocketAddress address = new InetSocketAddress(localHost, serverPort);
+
         sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(address);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -717,7 +722,7 @@
             System.out.println("url is "+url.toString());
 
             try {
-                http = (HttpsURLConnection)url.openConnection();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
 
                 int respCode = http.getResponseCode();
                 System.out.println("respCode = "+respCode);
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressDNSIdentities.java	Thu Sep 12 15:46:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2011, 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
@@ -650,8 +650,13 @@
             serverModulus, serverPrivateExponent, passphrase);
         SSLServerSocketFactory sslssf = context.getServerSocketFactory();
 
+        // doClientSide() connects to the loopback address
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, serverPort);
+
         sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(address);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -721,7 +726,7 @@
             System.out.println("url is "+url.toString());
 
             try {
-                http = (HttpsURLConnection)url.openConnection();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
 
                 int respCode = http.getResponseCode();
                 System.out.println("respCode = " + respCode);
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPAddressIPIdentities.java	Thu Sep 12 15:46:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015, 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
@@ -654,8 +654,13 @@
             serverModulus, serverPrivateExponent, passphrase);
         SSLServerSocketFactory sslssf = context.getServerSocketFactory();
 
+        // doClientSide() connects to the loopback address
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, serverPort);
+
         sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(address);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -725,7 +730,7 @@
             System.out.println("url is "+url.toString());
 
             try {
-                http = (HttpsURLConnection)url.openConnection();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
 
                 int respCode = http.getResponseCode();
                 System.out.println("respCode = "+respCode);
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPIdentities.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/IPIdentities.java	Thu Sep 12 15:46:11 2019 +0100
@@ -29,6 +29,7 @@
 /* @test
  * @summary X509 certificate hostname checking is broken in JDK1.6.0_10
  * @bug 6766775
+ * @library /test/lib
  * @run main/othervm IPIdentities
  * @author Xuelei Fan
  */
@@ -45,6 +46,7 @@
 import java.security.spec.*;
 import java.security.interfaces.*;
 import java.math.BigInteger;
+import jdk.test.lib.net.URIBuilder;
 
 /*
  * Certificates and key used in the test.
@@ -652,8 +654,13 @@
             serverModulus, serverPrivateExponent, passphrase);
         SSLServerSocketFactory sslssf = context.getServerSocketFactory();
 
+        // doClientSide() connects to the loopback address
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, serverPort);
+
         sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(address);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -713,11 +720,16 @@
             HttpsURLConnection http = null;
 
             /* establish http connection to server */
-            URL url = new URL("https://localhost:" + serverPort+"/");
+            URL url = URIBuilder.newBuilder()
+                .scheme("https")
+                .loopback()
+                .port(serverPort)
+                .path("/")
+                .toURL();
             System.out.println("url is "+url.toString());
 
             try {
-                http = (HttpsURLConnection)url.openConnection();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
 
                 int respCode = http.getResponseCode();
                 System.out.println("respCode = "+respCode);
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Identities.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/Identities.java	Thu Sep 12 15:46:11 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015, 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
@@ -651,8 +651,13 @@
             serverModulus, serverPrivateExponent, passphrase);
         SSLServerSocketFactory sslssf = context.getServerSocketFactory();
 
+        // doClientSide() connects to "localhost"
+        InetAddress localHost = InetAddress.getByName("localhost");
+        InetSocketAddress address = new InetSocketAddress(localHost, serverPort);
+
         sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(address);
         serverPort = sslServerSocket.getLocalPort();
 
         /*
@@ -717,7 +722,7 @@
             System.out.println("url is "+url.toString());
 
             try {
-                http = (HttpsURLConnection)url.openConnection();
+                http = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
 
                 int respCode = http.getResponseCode();
                 System.out.println("respCode = "+respCode);
--- a/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ImpactOnSNI.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ImpactOnSNI.java	Thu Sep 12 15:46:11 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
@@ -95,6 +95,16 @@
      * smart about it....
      */
 
+    private SSLServerSocket createServerSocket(SSLServerSocketFactory sslssf)
+        throws Exception {
+        SSLServerSocket sslServerSocket =
+            (SSLServerSocket)sslssf.createServerSocket();
+        InetAddress localHost = InetAddress.getLocalHost();
+        InetSocketAddress address = new InetSocketAddress(localHost, serverPort);
+        sslServerSocket.bind(address);
+        return sslServerSocket;
+    }
+
     /*
      * Define the server side of the test.
      *
@@ -104,8 +114,7 @@
     private void doServerSide() throws Exception {
         SSLServerSocketFactory sslssf =
             (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
-        try (SSLServerSocket sslServerSocket =
-                (SSLServerSocket)sslssf.createServerSocket(serverPort)) {
+        try (SSLServerSocket sslServerSocket = createServerSocket(sslssf)) {
 
             serverPort = sslServerSocket.getLocalPort();
 
--- a/test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHostnameVerifier.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/https/NewImpl/JavaxHostnameVerifier.java	Thu Sep 12 15:46:11 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
@@ -135,8 +135,14 @@
 
         SSLServerSocketFactory sslssf =
           (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
+
+        // doClientSide() connects to "localhost"
+        InetAddress localHost = InetAddress.getByName("localhost");
+        InetSocketAddress address = new InetSocketAddress(localHost, serverPort);
+
         SSLServerSocket sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
+            (SSLServerSocket) sslssf.createServerSocket();
+        sslServerSocket.bind(address);
         serverPort = sslServerSocket.getLocalPort();
 
         String ciphers[]= { "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA" };
@@ -205,7 +211,7 @@
 
         URL url = new URL("https://" + "localhost:" + serverPort +
                                 "/etc/hosts");
-        URLConnection urlc = url.openConnection();
+        URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
 
         if (!(urlc instanceof javax.net.ssl.HttpsURLConnection)) {
             throw new Exception(
--- a/test/jdk/sun/net/www/protocol/jar/B4957695.java	Tue Sep 03 09:28:42 2019 +0200
+++ b/test/jdk/sun/net/www/protocol/jar/B4957695.java	Thu Sep 12 15:46: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
@@ -96,7 +96,10 @@
     public static void main (String[] args) throws Exception {
         String tmpdir = System.getProperty("java.io.tmpdir");
         String[] list1 = listTmpFiles(tmpdir);
-        ServerSocket serverSocket = new ServerSocket(0);
+        InetAddress localHost = InetAddress.getByName("localhost");
+        InetSocketAddress address = new InetSocketAddress(localHost, 0);
+        ServerSocket serverSocket = new ServerSocket();
+        serverSocket.bind(address);
         server = new Server(serverSocket);
         server.start();
         int port = serverSocket.getLocalPort();
@@ -108,7 +111,9 @@
             read (is);
             is.close();
         } catch (IOException e) {
-            System.out.println ("Received IOException as expected");
+            System.out.println ("Received IOException as expected: " + e);
+        } finally {
+            try {serverSocket.close();} catch (IOException x) {}
         }
         String[] list2 = listTmpFiles(tmpdir);
         if (!sameList (list1, list2)) {