8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently
authordfuchs
Fri, 26 Apr 2019 16:25:43 +0100
changeset 54634 59c01214e478
parent 54633 84261c6b227b
child 54635 14615b8ac24c
8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently Summary: tests are updated to use the loopback address instead of the wildcard to avoid traffic being routed to a different server than what was intended by the test. Reviewed-by: chegar
test/jdk/java/net/Socket/LingerTest.java
test/jdk/java/net/Socket/ShutdownBoth.java
test/jdk/java/net/Socks/SocksIPv6Test.java
test/jdk/java/net/Socks/SocksServer.java
test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java
--- a/test/jdk/java/net/Socket/LingerTest.java	Fri Apr 26 12:35:26 2019 +0200
+++ b/test/jdk/java/net/Socket/LingerTest.java	Fri Apr 26 16:25:43 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2018, 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
@@ -71,11 +71,13 @@
     }
 
     static class Other implements Runnable {
-        int port;
-        long delay;
+        final InetAddress address;
+        final int port;
+        final long delay;
         boolean connected = false;
 
-        public Other(int port, long delay) {
+        public Other(InetAddress address, int port, long delay) {
+            this.address = address;
             this.port = port;
             this.delay = delay;
         }
@@ -85,7 +87,7 @@
             try {
                 Thread.sleep(delay);
                 System.out.println ("Other opening socket");
-                Socket s = new Socket("localhost", port);
+                Socket s = new Socket(address, port);
                 synchronized (this) {
                     connected = true;
                 }
@@ -103,9 +105,10 @@
     }
 
     public static void main(String args[]) throws Exception {
-        ServerSocket ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket(0, 50, loopback);
 
-        Socket s1 = new Socket("localhost", ss.getLocalPort());
+        Socket s1 = new Socket(loopback, ss.getLocalPort());
         Socket s2 = ss.accept();
 
         // setup conditions for untransmitted data and lengthy
@@ -119,7 +122,7 @@
         senderThread.start();
 
         // other thread that will connect after 5 seconds.
-        Other other = new Other(ss.getLocalPort(), 5000);
+        Other other = new Other(loopback, ss.getLocalPort(), 5000);
         Thread otherThread = new Thread(other);
         otherThread.start();
 
--- a/test/jdk/java/net/Socket/ShutdownBoth.java	Fri Apr 26 12:35:26 2019 +0200
+++ b/test/jdk/java/net/Socket/ShutdownBoth.java	Fri Apr 26 16:25:43 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
@@ -32,7 +32,8 @@
 public class ShutdownBoth {
 
     public static void main(String args[]) throws Exception {
-        ServerSocket ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket(0, 50, loopback);
         Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
         Socket s2 = ss.accept();
 
--- a/test/jdk/java/net/Socks/SocksIPv6Test.java	Fri Apr 26 12:35:26 2019 +0200
+++ b/test/jdk/java/net/Socks/SocksIPv6Test.java	Fri Apr 26 16:25:43 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
@@ -30,7 +30,6 @@
 
 import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.net.Authenticator;
@@ -43,7 +42,6 @@
 import java.net.ServerSocket;
 import java.net.SocketException;
 import java.net.NetworkInterface;
-import java.net.UnknownHostException;
 import java.util.Collections;
 import java.util.List;
 import com.sun.net.httpserver.*;
@@ -65,7 +63,7 @@
     public void setUp() throws Exception {
         shouldRun = ensureInet6AddressFamily() && ensureIPv6OnLoopback();
 
-        server = HttpServer.create(new InetSocketAddress(0), 0);
+        server = HttpServer.create(new InetSocketAddress("::1", 0), 0);
         server.createContext("/", ex -> {
             ex.sendResponseHeaders(200, response.length());
             try (BufferedWriter writer = new BufferedWriter(
@@ -76,7 +74,7 @@
         });
         server.start();
 
-        socks = new SocksServer(0, false);
+        socks = new SocksServer(InetAddress.getByName("::1"), 0, false);
         socks.addUser("user", "pass");
         socks.start();
 
@@ -140,21 +138,45 @@
     public void testSocksOverIPv6Hostname() throws Exception {
         if (!shouldRun) return;
 
-        String ipv6Hostname = InetAddress.getByName("::1").getHostName();
-        String ipv4Hostname = InetAddress.getByName("127.0.0.1").getHostName();
+        InetAddress ipv6Loopback = InetAddress.getByName("::1");
+        String ipv6Hostname = ipv6Loopback.getHostName();
+        String ipv6HostAddress = ipv6Loopback.getHostAddress();
+        InetAddress ipv4Loopback;
+        String ipv4Hostname;
+        String ipv4HostAddress;
+        try {
+            ipv4Loopback = InetAddress.getByName("127.0.0.1");
+            ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName();
+            ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress();
+        } catch (IOException io) {
+            ipv4Hostname = null;
+            ipv4HostAddress = null;
+        }
 
-        if (ipv6Hostname.equals(InetAddress.getByName("::1").getHostAddress())) {
+        System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress);
+        System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress);
+
+        if (ipv6Hostname.equals(ipv6HostAddress)) {
             System.out.println("Unable to get the hostname of the IPv6 loopback "
                     + "address. Skipping test case.");
             return;
         }
 
-        if (ipv6Hostname.equals(ipv4Hostname)) {
+        if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) {
             System.out.println("IPv6 and IPv4 loopback addresses map to the"
                     + " same hostname. Skipping test case.");
             return;
         }
 
+        if (!InetAddress.getByName(ipv6Hostname).getHostAddress()
+                .equals(ipv6HostAddress)) {
+            System.out.println(ipv6Hostname + " resolves to \""
+                    + InetAddress.getByName(ipv6Hostname).getHostAddress()
+                    + "\", not \"" + ipv6HostAddress +
+                    "\". Skipping test case.");
+            return;
+        }
+
         Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,
                 socks.getPort()));
         URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());
--- a/test/jdk/java/net/Socks/SocksServer.java	Fri Apr 26 12:35:26 2019 +0200
+++ b/test/jdk/java/net/Socks/SocksServer.java	Fri Apr 26 16:25:43 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2012, 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
@@ -491,6 +491,25 @@
         }
     }
 
+    public SocksServer(InetAddress addr, int port, boolean useV4) throws IOException {
+        this.port = port;
+        this.useV4 = useV4;
+        server = new ServerSocket();
+        if (port == 0 && addr == null) {
+            server.bind(null);
+            this.port = server.getLocalPort();
+        } else if (port == 0 && addr != null) {
+            server.bind(new InetSocketAddress(addr, 0));
+            this.port = server.getLocalPort();
+        } else if (addr == null) {
+            assert port != 0;
+            server.bind(new InetSocketAddress(port));
+        } else {
+            assert port != 0;
+            server.bind(new InetSocketAddress(addr, port));
+        }
+    }
+
     public SocksServer() throws IOException {
         this (DEFAULT_PORT);
     }
--- a/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java	Fri Apr 26 12:35:26 2019 +0200
+++ b/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java	Fri Apr 26 16:25:43 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
@@ -49,7 +49,7 @@
         try {
             s = new Server();
             s.startServer();
-            URL url = new URL("http://localhost:" + s.getPort());
+            URL url = new URL("http://" + s.getAuthority());
             HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
             urlConnection.setRequestMethod("DELETE");
             urlConnection.setDoOutput(true);
@@ -70,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) {
@@ -81,6 +82,12 @@
             server.start();
         }
 
+        public String getAuthority() {
+            String address = server.getAddress().getHostString();
+            address =  (address.indexOf(':') >= 0) ? ("[" + address + "]") : address;
+            return address + ":" + getPort();
+        }
+
         public int getPort() {
             return server.getAddress().getPort();
         }