8226825: Replace wildcard address with loopback or local host in tests - part 19
authordfuchs
Thu, 27 Jun 2019 16:12:39 +0100
changeset 55512 61e03d5d6bcb
parent 55511 91b38bfb9079
child 55513 be05771cdfdf
8226825: Replace wildcard address with loopback or local host in tests - part 19 Summary: Replace use of wildcard by the loopback address, or possibly the local host address, wherever possible, to improve test stability. Reviewed-by: chegar, bpb Contributed-by: julia.boes@oracle.com
test/jdk/java/net/Socket/SetSoLinger.java
test/jdk/sun/net/www/protocol/http/AsyncDisconnect.java
test/jdk/sun/net/www/protocol/http/B6641309.java
test/jdk/sun/net/www/protocol/http/B6660405.java
test/jdk/sun/net/www/protocol/http/B6890349.java
test/jdk/sun/net/www/protocol/http/Modified.java
--- a/test/jdk/java/net/Socket/SetSoLinger.java	Thu Jun 27 18:00:54 2019 +0800
+++ b/test/jdk/java/net/Socket/SetSoLinger.java	Thu Jun 27 16:12:39 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
@@ -28,6 +28,7 @@
  * @summary Test Socket.setSoLinger
  * @run main SetSoLinger
  * @run main/othervm -Djava.net.preferIPv4Stack=true SetSoLinger
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true SetSoLinger
  */
 
 import java.net.*;
@@ -41,7 +42,10 @@
 
         int value;
         InetAddress addr = InetAddress.getLocalHost();
-        ServerSocket ss = new ServerSocket(0);
+        ServerSocket ss = new ServerSocket();
+
+        InetSocketAddress socketAddress = new InetSocketAddress(addr, 0);
+        ss.bind(socketAddress);
         int port = ss.getLocalPort();
 
         Socket s = new Socket(addr, port);
--- a/test/jdk/sun/net/www/protocol/http/AsyncDisconnect.java	Thu Jun 27 18:00:54 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/AsyncDisconnect.java	Thu Jun 27 16:12:39 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2016, 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
@@ -24,18 +24,21 @@
 /*
  * @test
  * @bug 6358532
+ * @library /test/lib
  * @modules jdk.httpserver
  * @run main/othervm AsyncDisconnect
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true AsyncDisconnect
  * @summary HttpURLConnection.disconnect doesn't really do the job
  */
 
 import java.net.*;
-import java.util.*;
 import java.io.*;
 import com.sun.net.httpserver.*;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class AsyncDisconnect implements Runnable
 {
     com.sun.net.httpserver.HttpServer httpServer;
@@ -43,27 +46,30 @@
     ExecutorService executorService;
     HttpURLConnection uc;
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         new AsyncDisconnect();
     }
 
-    public AsyncDisconnect() {
-        try {
-            startHttpServer();
-            doClient();
-        } catch (IOException ioe) {
-            System.err.println(ioe);
-        }
+    public AsyncDisconnect() throws Exception {
+        startHttpServer();
+        doClient();
     }
 
-    void doClient() {
+    void doClient() throws Exception {
+        Thread t = new Thread(this);
+
         try {
             InetSocketAddress address = httpServer.getAddress();
-            URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
-            uc = (HttpURLConnection)url.openConnection();
+            URL url = URIBuilder.newBuilder()
+                    .scheme("http")
+                    .host(address.getAddress())
+                    .port(address.getPort())
+                    .path("/test/")
+                    .toURL();
+            uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
 
             // create a thread that will disconnect the connection
-            (new Thread(this)).start();
+            t.start();
 
             uc.getInputStream();
 
@@ -73,11 +79,11 @@
         } catch (SocketException se) {
             // this is what we expect to happen and is OK.
             //System.out.println(se);
-        } catch (IOException e) {
-            e.printStackTrace();
         } finally {
             httpServer.stop(1);
+            t.join();
             executorService.shutdown();
+
         }
     }
 
@@ -93,7 +99,9 @@
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, 0);
+        httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
         httpHandler = new MyHandler();
 
         HttpContext ctx = httpServer.createContext("/test/", httpHandler);
--- a/test/jdk/sun/net/www/protocol/http/B6641309.java	Thu Jun 27 18:00:54 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/B6641309.java	Thu Jun 27 16:12:39 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 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,7 +25,10 @@
  * @test
  * @bug 6641309
  * @modules jdk.httpserver
- * @summary Wrong Cookie separator used in HttpURLConnection
+ * @library /test/lib
+ * @run main/othervm B6641309
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B6641309
+ * @summary Wrong Cookie separator used in HttpURLConnection B6641309
  */
 
 import java.net.*;
@@ -35,65 +38,65 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
 
+import jdk.test.lib.net.URIBuilder;
+
 public class B6641309
 {
     com.sun.net.httpserver.HttpServer httpServer;
     ExecutorService executorService;
 
-    public static void main(String[] args)
-    {
+    public static void main(String[] args) throws Exception {
         new B6641309();
     }
 
-    public B6641309()
-    {
-        try {
-            startHttpServer();
-            doClient();
-        } catch (IOException ioe) {
-            System.err.println(ioe);
-        }
+    public B6641309() throws Exception {
+        startHttpServer();
+        doClient();
     }
 
-    void doClient() {
+    void doClient() throws Exception {
         CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
-        try {
-            InetSocketAddress address = httpServer.getAddress();
+        ProxySelector.setDefault(ProxySelector.of(null));
+
+        InetSocketAddress address = httpServer.getAddress();
 
-            // GET Request
-            URL url = new URL("http://localhost:" + address.getPort() + "/test/");
-            CookieHandler ch = CookieHandler.getDefault();
-            Map<String,List<String>> header = new HashMap<String,List<String>>();
-            List<String> values = new LinkedList<String>();
-            values.add("Test1Cookie=TEST1; path=/test/");
-            values.add("Test2Cookie=TEST2; path=/test/");
-            header.put("Set-Cookie", values);
+        // GET Request
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .host(address.getAddress())
+                .port(address.getPort())
+                .path("/test/")
+                .toURL();
 
-            // preload the CookieHandler with a cookie for our URL
-            // so that it will be sent during the first request
-            ch.put(url.toURI(), header);
-            HttpURLConnection uc = (HttpURLConnection)url.openConnection();
-            int resp = uc.getResponseCode();
-            if (resp != 200)
-                throw new RuntimeException("Failed: Response code from GET is not 200");
+        CookieHandler ch = CookieHandler.getDefault();
+        Map<String,List<String>> header = new HashMap<String,List<String>>();
+        List<String> values = new LinkedList<String>();
+        values.add("Test1Cookie=TEST1; path=/test/");
+        values.add("Test2Cookie=TEST2; path=/test/");
+        header.put("Set-Cookie", values);
 
-            System.out.println("Response code from GET = 200 OK");
+        // preload the CookieHandler with a cookie for our URL
+        // so that it will be sent during the first request
+        ch.put(url.toURI(), header);
+        HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+        int resp = uc.getResponseCode();
+        if (resp != 200) {
+            throw new RuntimeException("Failed: Response code from GET is not 200: "
+                    + resp);
+        }
+        System.out.println("Response code from GET = 200 OK");
 
-        } catch (IOException e) {
-            e.printStackTrace();
-        } catch (URISyntaxException e) {
-            e.printStackTrace();
-        } finally {
-            httpServer.stop(1);
-            executorService.shutdown();
-        }
+        httpServer.stop(1);
+        executorService.shutdown();
     }
 
     /**
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, 0);
+        httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
 
         // create HttpServer context
         HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
--- a/test/jdk/sun/net/www/protocol/http/B6660405.java	Thu Jun 27 18:00:54 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/B6660405.java	Thu Jun 27 16:12:39 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 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,7 +25,10 @@
  * @test
  * @bug 6660405
  * @modules jdk.httpserver
- * @summary HttpURLConnection returns the wrong InputStream
+ * @library /test/lib
+ * @run main/othervm B6660405
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B6660405
+ * @summary HttpURLConnection returns the wrong InputStream B6660405
  */
 
 import java.net.*;
@@ -34,6 +37,8 @@
 import com.sun.net.httpserver.*;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
+import jdk.test.lib.net.URIBuilder;
+
 
 public class B6660405
 {
@@ -72,7 +77,8 @@
         }
 
         @Override
-        public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException
+        public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)
+                throws IOException
         {
             if (uri.getPath().equals("/redirect/index.html")) {
                 return new MyCacheResponse();
@@ -88,53 +94,61 @@
 
     }
 
-    public static void main(String[] args)
+    public static void main(String[] args) throws Exception
     {
         new B6660405();
     }
 
-    public B6660405()
-    {
-        try {
-            startHttpServer();
-            doClient();
-        } catch (IOException ioe) {
-            System.err.println(ioe);
-        }
+    public B6660405() throws Exception {
+        startHttpServer();
+        doClient();
     }
 
-    void doClient() {
+    void doClient() throws Exception {
         ResponseCache.setDefault(new MyResponseCache());
-        try {
-            InetSocketAddress address = httpServer.getAddress();
+        InetSocketAddress address = httpServer.getAddress();
+
+        // GET Request
+        URL url = URIBuilder.newBuilder()
+                .scheme("http")
+                .host(address.getAddress())
+                .port(address.getPort())
+                .path("/test/index.html")
+                .toURL();
 
-            // GET Request
-            URL url = new URL("http://localhost:" + address.getPort() + "/test/index.html");
-            HttpURLConnection uc = (HttpURLConnection)url.openConnection();
-            int code = uc.getResponseCode();
-            System.err.println("response code = " + code);
-            int l = uc.getContentLength();
-            System.err.println("content-length = " + l);
-            InputStream in = uc.getInputStream();
-            int i = 0;
-            // Read till end of stream
-            do {
-                i = in.read();
-            } while (i != -1);
-            in.close();
-        } catch (IOException e) {
-            throw new RuntimeException("Got the wrong InputStream after checking headers");
-        } finally {
-            httpServer.stop(1);
-            executorService.shutdown();
+        HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
+        int code = uc.getResponseCode();
+        System.err.println("response code = " + code);
+        int l = uc.getContentLength();
+        System.err.println("content-length = " + l);
+        if (l != 1024) {
+            throw new AssertionError("Bad content length: " + l);
         }
+
+        InputStream in = uc.getInputStream();
+        int i = 0;
+        // Read till end of stream
+        do {
+            l--;
+            i = in.read();
+        } while (i != -1);
+        in.close();
+        if (l != -1) {
+            throw new AssertionError("Only " + (1024 - (l + 1))
+                    + " bytes read from stream.");
+        }
+
+        httpServer.stop(1);
+        executorService.shutdown();
     }
 
     /**
      * Http Server
      */
     public void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback,0);
+        httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
 
         // create HttpServer context
         HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
--- a/test/jdk/sun/net/www/protocol/http/B6890349.java	Thu Jun 27 18:00:54 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/B6890349.java	Thu Jun 27 16:12:39 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 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,7 +23,9 @@
 /**
  * @test
  * @bug 6890349
+ * @library /test/lib
  * @run main/othervm B6890349
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B6890349
  * @summary  Light weight HTTP server
  */
 
@@ -34,7 +36,11 @@
     public static final void main(String[] args) throws Exception {
 
         try {
-            ServerSocket server = new ServerSocket (0);
+            ServerSocket server = new ServerSocket();
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            InetSocketAddress address = new InetSocketAddress(loopback, 0);
+            server.bind(address);
+
             int port = server.getLocalPort();
             System.out.println ("listening on "  + port);
             B6890349 t = new B6890349 (server);
@@ -44,11 +50,11 @@
                 port,
                 "/foo\nbar");
             System.out.println("URL: " + u);
-            HttpURLConnection urlc = (HttpURLConnection)u.openConnection ();
+            HttpURLConnection urlc = (HttpURLConnection)u.openConnection(Proxy.NO_PROXY);
             InputStream is = urlc.getInputStream();
             throw new RuntimeException ("Test failed");
         } catch (IOException e) {
-            System.out.println ("OK");
+            System.out.println ("Caught expected exception: " + e);
         }
     }
 
--- a/test/jdk/sun/net/www/protocol/http/Modified.java	Thu Jun 27 18:00:54 2019 +0800
+++ b/test/jdk/sun/net/www/protocol/http/Modified.java	Thu Jun 27 16:12:39 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2001, 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
@@ -24,12 +24,16 @@
 /*
  * @test
  * @bug 4092605
+ * @library /test/lib
+ * @run main/othervm Modified
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true Modified
  * @summary Test HttpURLConnection setIfModifiedSince
  *
  */
 
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class Modified implements Runnable {
 
@@ -78,13 +82,22 @@
 
     Modified() throws Exception {
 
-        ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, 0);
+        ss = new ServerSocket();
+        ss.bind(address);
+        int port = ss.getLocalPort();
+
         Thread thr = new Thread(this);
         thr.start();
 
-        URL testURL = new URL("http://localhost:" + ss.getLocalPort() +
-                              "/index.html");
-        URLConnection URLConn = testURL.openConnection();
+        URL testURL = URIBuilder.newBuilder()
+                .scheme("http")
+                .host(loopback)
+                .port(port)
+                .path("/index.html")
+                .toURL();
+        URLConnection URLConn = testURL.openConnection(Proxy.NO_PROXY);
         HttpURLConnection httpConn;
 
         if (URLConn instanceof HttpURLConnection) {