8223463: Replace wildcard address with loopback or local host in tests - part 2
authordfuchs
Tue, 07 May 2019 18:10:59 +0100
changeset 54746 61049e91eae5
parent 54745 87d01c0d7b45
child 54747 0082ede5dc53
child 54870 0d49e7c0b4fa
8223463: Replace wildcard address with loopback or local host in tests - part 2 Summary: Removes (or documents) some usages of the wildcard address in intermittently failing tests. Reviewed-by: alanb
test/jdk/java/net/ServerSocket/AcceptInheritHandle.java
test/jdk/java/net/URLConnection/Responses.java
test/jdk/java/net/ipv6tests/TcpTest.java
test/jdk/sun/net/ftp/FtpURL.java
--- a/test/jdk/java/net/ServerSocket/AcceptInheritHandle.java	Tue May 07 09:37:02 2019 -0700
+++ b/test/jdk/java/net/ServerSocket/AcceptInheritHandle.java	Tue May 07 18:10:59 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -32,6 +32,7 @@
 import java.net.*;
 import java.nio.channels.ServerSocketChannel;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Supplier;
@@ -41,8 +42,8 @@
     enum ServerSocketProducer {
         JAVA_NET(() -> {
             try {
-                return new ServerSocket(); }
-            catch(IOException x) {
+                return new ServerSocket();
+            } catch(IOException x) {
                 throw new UncheckedIOException(x);
             }
         }),
@@ -86,13 +87,13 @@
         test(ServerSocketProducer.NIO_CHANNELS);
     }
 
-    static void test(ServerSocketProducer ssp, String... sysProps) throws Exception {
+    static void test(ServerSocketProducer ssp, String... jvmArgs) throws Exception {
         System.out.println("\nStarting test for " + ssp.name());
 
         List<String> commands = new ArrayList<>();
         commands.add(JAVA);
-        for (String prop : sysProps)
-            commands.add(prop);
+        for (String arg : jvmArgs)
+            commands.add(arg);
         commands.add("-cp");
         commands.add(CLASSPATH);
         commands.add("AcceptInheritHandle");
@@ -107,7 +108,14 @@
         int port = dis.readInt();
         System.out.println("Server process listening on " + port + ", connecting...");
 
-        Socket socket = new Socket("localhost", port);
+        String address;
+        if (Arrays.stream(jvmArgs).anyMatch("-Djava.net.preferIPv4Stack=true"::equals)) {
+            address = "127.0.0.1";
+        } else {
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            address = loopback.getHostAddress();
+        }
+        Socket socket = new Socket(address, port);
         String s = dis.readUTF();
         System.out.println("Server process said " + s);
 
@@ -128,7 +136,8 @@
 
     static void server(ServerSocketProducer producer) throws Exception {
         try (ServerSocket ss = producer.supplier().get()) {
-            ss.bind(new InetSocketAddress(0));
+            InetAddress loopback = InetAddress.getLoopbackAddress();
+            ss.bind(new InetSocketAddress(loopback, 0));
             int port = ss.getLocalPort();
             DataOutputStream dos = new DataOutputStream(System.out);
             dos.writeInt(port);
--- a/test/jdk/java/net/URLConnection/Responses.java	Tue May 07 09:37:02 2019 -0700
+++ b/test/jdk/java/net/URLConnection/Responses.java	Tue May 07 18:10:59 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 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
@@ -60,7 +60,9 @@
 
         public HttpServer() {
             try {
-                ss = new ServerSocket(0);
+                InetAddress loopback = InetAddress.getLoopbackAddress();
+                ss = new ServerSocket();
+                ss.bind(new InetSocketAddress(loopback, 0));
             } catch (IOException ioe) {
                 throw new Error("Unable to create ServerSocket: " + ioe);
             }
@@ -70,6 +72,16 @@
             return ss.getLocalPort();
         }
 
+        public String authority() {
+            InetAddress address = ss.getInetAddress();
+            String hostaddr = address.isAnyLocalAddress()
+                ? "localhost" : address.getHostAddress();
+            if (hostaddr.indexOf(':') > -1) {
+                hostaddr = "[" + hostaddr + "]";
+            }
+            return hostaddr + ":" + port();
+        }
+
         public void shutdown() throws IOException {
             ss.close();
         }
@@ -116,7 +128,8 @@
         HttpServer svr = new HttpServer();
         (new Thread(svr)).start();
 
-        int port = svr.port();
+        String authority = svr.authority();
+        System.out.println("Server listening on: " + authority);
 
         /*
          * Iterate through each test case and check that getResponseCode
@@ -129,7 +142,7 @@
             System.out.println("******************");
             System.out.println("Test with response: >" + tests[i][0] + "<");
 
-            URL url = new URL("http://localhost:" + port + "/" + i);
+            URL url = new URL("http://" + authority + "/" + i);
             HttpURLConnection http = (HttpURLConnection)url.openConnection();
 
             try {
--- a/test/jdk/java/net/ipv6tests/TcpTest.java	Tue May 07 09:37:02 2019 -0700
+++ b/test/jdk/java/net/ipv6tests/TcpTest.java	Tue May 07 18:10:59 2019 +0100
@@ -25,7 +25,9 @@
  * @test
  * @bug 4868820
  * @key intermittent
- * @summary IPv6 support for Windows XP and 2003 server
+ * @summary IPv6 support for Windows XP and 2003 server. This test requires
+ *          binding to the wildcard address, and as such is susceptible
+ *          of intermittent failures caused by port reuse policy.
  * @library /test/lib
  * @build jdk.test.lib.NetworkConfiguration
  *        jdk.test.lib.Platform
@@ -216,4 +218,3 @@
         System.out.println ("Test4: OK");
     }
 }
-
--- a/test/jdk/sun/net/ftp/FtpURL.java	Tue May 07 09:37:02 2019 -0700
+++ b/test/jdk/sun/net/ftp/FtpURL.java	Tue May 07 18:10:59 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2010, 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
@@ -36,8 +36,8 @@
      */
 
     private class FtpServer extends Thread {
-        private ServerSocket    server;
-        private int port;
+        private final ServerSocket server;
+        private final int port;
         private boolean done = false;
         private boolean portEnabled = true;
         private boolean pasvEnabled = true;
@@ -253,8 +253,12 @@
                                 continue;
                             }
                             try {
-                                if (pasv == null)
-                                    pasv = new ServerSocket(0);
+                                if (pasv == null) {
+                                    // Not sure how to support PASV mode over
+                                    // IPv6
+                                    pasv = new ServerSocket();
+                                    pasv.bind(new InetSocketAddress("127.0.0.1", 0));
+                                }
                                 int port = pasv.getLocalPort();
                                 out.println("227 Entering Passive Mode (127,0,0,1," +
                                             (port >> 8) + "," + (port & 0xff) +")");
@@ -369,21 +373,39 @@
         }
 
         public FtpServer(int port) {
+            this(InetAddress.getLoopbackAddress(), port);
+        }
+
+        public FtpServer(InetAddress address, int port) {
             this.port = port;
             try {
-              server = new ServerSocket(port);
+                if (address == null) {
+                    server = new ServerSocket(port);
+                } else {
+                    server = new ServerSocket();
+                    server.bind(new InetSocketAddress(address, port));
+                }
             } catch (IOException e) {
+                throw new UncheckedIOException(e);
             }
         }
 
         public FtpServer() {
-            this(21);
+            this(null, 21);
         }
 
         public int getPort() {
-            if (server != null)
-                return server.getLocalPort();
-            return 0;
+             return server.getLocalPort();
+        }
+
+        public String getAuthority() {
+            InetAddress address = server.getInetAddress();
+            String hostaddr = address.isAnyLocalAddress()
+                ? "localhost" : address.getHostAddress();
+            if (hostaddr.indexOf(':') > -1) {
+                hostaddr = "[" + hostaddr +"]";
+            }
+            return hostaddr + ":" + getPort();
         }
 
         /**
@@ -449,15 +471,17 @@
     }
 
     public FtpURL() throws Exception {
-        FtpServer server = new FtpServer(0);
+        FtpServer server = new FtpServer(InetAddress.getLoopbackAddress(), 0);
         BufferedReader in = null;
         try {
             server.start();
-            int port = server.getPort();
+            String authority = server.getAuthority();
+            System.out.println("FTP server waiting for connections at: " + authority);
+            assert authority != null;
 
             // Now let's check the URL handler
 
-            URL url = new URL("ftp://user:password@localhost:" + port + "/%2Fetc/motd;type=a");
+            URL url = new URL("ftp://user:password@" + authority + "/%2Fetc/motd;type=a");
             URLConnection con = url.openConnection();
             in = new BufferedReader(new InputStreamReader(con.getInputStream()));
             String s;
@@ -479,11 +503,10 @@
             // We're done!
 
             // Second URL test
-            port = server.getPort();
 
             // Now let's check the URL handler
 
-            url = new URL("ftp://user2@localhost:" + port + "/%2Fusr/bin;type=d");
+            url = new URL("ftp://user2@" + authority + "/%2Fusr/bin;type=d");
             con = url.openConnection();
             in = new BufferedReader(new InputStreamReader(con.getInputStream()));
             do {