8158525: Update a few java/net tests to use the loopback address instead of the host address
authorchegar
Fri, 03 Jun 2016 13:45:30 +0100
changeset 38780 fa8bf2d62d36
parent 38779 a78d46ad59c1
child 38781 f4e9fc088cdd
8158525: Update a few java/net tests to use the loopback address instead of the host address Reviewed-by: rriggs
jdk/test/java/net/CookieHandler/CookieManagerTest.java
jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java
jdk/test/java/net/URLConnection/HandleContentTypeWithAttrs.java
--- a/jdk/test/java/net/CookieHandler/CookieManagerTest.java	Thu Jun 02 16:39:14 2016 -0700
+++ b/jdk/test/java/net/CookieHandler/CookieManagerTest.java	Fri Jun 03 13:45:30 2016 +0100
@@ -25,6 +25,7 @@
  * @test
  * @summary Unit test for java.net.CookieManager
  * @bug 6244040 7150552 7051862
+ * @modules jdk.httpserver
  * @run main/othervm -ea CookieManagerTest
  * @author Edward Wang
  */
@@ -32,12 +33,31 @@
 import com.sun.net.httpserver.*;
 import java.io.IOException;
 import java.net.*;
+import static java.net.Proxy.NO_PROXY;
 
 public class CookieManagerTest {
 
     static CookieTransactionHandler httpTrans;
     static HttpServer server;
 
+    static final String hostAddress = getAddr();
+
+    /** Returns an IP literal suitable for use by the test. */
+    static String getAddr() {
+        try {
+            InetAddress lh = InetAddress.getLocalHost();
+            System.out.println("Trying: " + lh);
+            if (lh.isReachable(5_000)) {
+                System.out.println("Using: " + lh);
+                return lh.getHostAddress();
+            }
+        } catch (IOException x) {
+            System.out.println("Debug: caught:" + x);
+        }
+        System.out.println("Using: \"127.0.0.1\"");
+        return "127.0.0.1";
+    }
+
     public static void main(String[] args) throws Exception {
         startHttpServer();
         makeHttpCall();
@@ -78,8 +98,8 @@
 
     public static void makeHttpCall() throws IOException {
         try {
-            System.out.println("http server listenining on: "
-                    + server.getAddress().getPort());
+            int port = server.getAddress().getPort();
+            System.out.println("http server listenining on: " + port);
 
             // install CookieManager to use
             CookieHandler.setDefault(new CookieManager());
@@ -92,11 +112,12 @@
                 ((CookieManager)CookieHandler.getDefault())
                     .getCookieStore().removeAll();
                 URL url = new URL("http" ,
-                                  InetAddress.getLocalHost().getHostAddress(),
+                                  hostAddress,
                                   server.getAddress().getPort(),
                                   CookieTransactionHandler.testCases[i][0]
                                                           .serverPath);
-                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+                System.out.println("Requesting " + url);
+                HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);
                 uc.getResponseCode();
                 uc.disconnect();
             }
@@ -116,8 +137,6 @@
     // to send http request
     public static final int testCount = 6;
 
-    private String localHostAddr = "127.0.0.1";
-
     @Override
     public void handle(HttpExchange exchange) throws IOException {
         if (testDone < testCases[testcaseDone].length) {
@@ -188,10 +207,8 @@
         testCases = new CookieTestCase[testCount][];
         testPolicies = new CookiePolicy[testCount];
 
-        try {
-            localHostAddr = InetAddress.getLocalHost().getHostAddress();
-        } catch (Exception ignored) {
-        };
+        String localHostAddr = CookieManagerTest.hostAddress;
+
         int count = 0;
 
         // an http session with Netscape cookies exchanged
--- a/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java	Thu Jun 02 16:39:14 2016 -0700
+++ b/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java	Fri Jun 03 13:45:30 2016 +0100
@@ -24,11 +24,11 @@
 /**
  * @test
  * @bug 7128648
+ * @modules jdk.httpserver
  * @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
  */
 
 import java.io.IOException;
-import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.URI;
 import java.net.HttpURLConnection;
@@ -40,6 +40,7 @@
 import com.sun.net.httpserver.HttpHandler;
 import com.sun.net.httpserver.HttpServer;
 import com.sun.net.httpserver.Headers;
+import static java.net.Proxy.NO_PROXY;
 
 public class UnmodifiableMaps {
 
@@ -47,8 +48,7 @@
         HttpServer server = startHttpServer();
         try {
             InetSocketAddress address = server.getAddress();
-            URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress()
-                              + ":" + address.getPort() + "/foo");
+            URI uri = new URI("http://localhost:" + address.getPort() + "/foo");
             doClient(uri);
         } finally {
             server.stop(0);
@@ -56,7 +56,7 @@
     }
 
     void doClient(URI uri) throws Exception {
-        HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection();
+        HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection(NO_PROXY);
 
         // Test1: getRequestProperties is unmodifiable
         System.out.println("Check getRequestProperties");
--- a/jdk/test/java/net/URLConnection/HandleContentTypeWithAttrs.java	Thu Jun 02 16:39:14 2016 -0700
+++ b/jdk/test/java/net/URLConnection/HandleContentTypeWithAttrs.java	Fri Jun 03 13:45:30 2016 +0100
@@ -32,6 +32,7 @@
 import java.io.*;
 import sun.net.www.content.text.*;
 import sun.net.www.MessageHeader;
+import static java.net.Proxy.NO_PROXY;
 
 public class HandleContentTypeWithAttrs {
 
@@ -39,13 +40,11 @@
 
     public HandleContentTypeWithAttrs (int port) throws Exception {
 
-        String localHostName = InetAddress.getLocalHost().getHostName();
-
         // Request echo.html from myHttpServer.
         // In the header of the response, we make
         // the content type have some attributes.
-        url = new URL("http://" + localHostName + ":" + port + "/echo.html");
-        URLConnection urlConn = url.openConnection();
+        url = new URL("http://localhost:" + port + "/echo.html");
+        URLConnection urlConn = url.openConnection(NO_PROXY);
 
         // the method getContent() calls the method
         // getContentHandler(). With the fix, the method