changeset 55649:ad8e3b295615

8227539: Replace wildcard address with loopback or local host in tests - part 20 Summary: Update some tests to stop using the wildcard address. Reviewed-by: michaelm
author dfuchs
date Wed, 10 Jul 2019 22:33:23 +0200
parents ba72dac556c3
children 56e8c0a3fe9a
files test/jdk/java/net/HttpURLConnection/HttpResponseCode.java test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java test/jdk/java/net/Inet6Address/B6558853.java test/jdk/java/net/InetAddress/CheckJNI.java test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java test/jdk/java/net/Socket/AsyncShutdown.java test/jdk/java/net/Socket/B6210227.java test/jdk/java/net/Socket/LinkLocal.java test/jdk/java/net/Socket/asyncClose/BrokenPipe.java test/jdk/java/net/ipv6tests/B6521014.java
diffstat 10 files changed, 143 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/java/net/HttpURLConnection/HttpResponseCode.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/HttpURLConnection/HttpResponseCode.java	Wed Jul 10 22:33:23 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 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
@@ -24,10 +24,14 @@
 /**
  * @test
  * @bug 4473092
+ * @library /test/lib
  * @summary Method throws IOException when object should be returned
+ * @run main HttpResponseCode
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true HttpResponseCode
  */
 import java.net.*;
 import java.io.*;
+import jdk.test.lib.net.URIBuilder;
 
 public class HttpResponseCode implements Runnable {
     ServerSocket ss;
@@ -61,14 +65,19 @@
 
     HttpResponseCode() throws Exception {
         /* start the server */
-        ss = new ServerSocket(0);
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
         (new Thread(this)).start();
 
         /* establish http connection to server */
-        String url = "http://localhost:" +
-            Integer.toString(ss.getLocalPort()) +
-            "/missing.nothtml";
-        URLConnection uc = new URL(url).openConnection();
+        URL url = URIBuilder.newBuilder()
+            .scheme("http")
+            .loopback()
+            .port(ss.getLocalPort())
+            .path("/missing.nothtml")
+            .toURL();
+        URLConnection uc = url.openConnection(Proxy.NO_PROXY);
         int respCode1 = ((HttpURLConnection)uc).getResponseCode();
         ((HttpURLConnection)uc).disconnect();
         int respCode2 = ((HttpURLConnection)uc).getResponseCode();
--- a/test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java	Wed Jul 10 22:33:23 2019 +0200
@@ -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
@@ -24,10 +24,12 @@
  /*
  * @test
  * @bug 8161016
+ * @library /test/lib
  * @summary When proxy is set HttpURLConnection should not use DIRECT connection.
  * @run main/othervm HttpURLConWithProxy
  */
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Proxy;
 import java.net.ProxySelector;
@@ -38,10 +40,11 @@
 import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.List;
+import jdk.test.lib.net.URIBuilder;
 
 public class HttpURLConWithProxy {
 
-    public static void main(String... arg) {
+    public static void main(String... arg) throws Exception {
         // Remove the default nonProxyHosts to use localhost for testing
         System.setProperty("http.nonProxyHosts", "");
 
@@ -51,11 +54,18 @@
         ServerSocket ss;
         URL url;
         URLConnection con;
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, 0);
 
         // Test1: using Proxy set by System Property:
         try {
-            ss = new ServerSocket(0);
-            url = new URL("http://localhost:" + ss.getLocalPort());
+            ss = new ServerSocket();
+            ss.bind(address);
+            url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .toURL();
             con = url.openConnection();
             con.setConnectTimeout(10 * 1000);
             con.connect();
@@ -69,8 +79,13 @@
         MyProxySelector myProxySel = new MyProxySelector();
         ProxySelector.setDefault(myProxySel);
         try {
-            ss = new ServerSocket(0);
-            url = new URL("http://localhost:" + ss.getLocalPort());
+            ss = new ServerSocket();
+            ss.bind(address);
+            url = URIBuilder.newBuilder()
+                .scheme("http")
+                .loopback()
+                .port(ss.getLocalPort())
+                .toURL();
             con = url.openConnection();
             con.setConnectTimeout(10 * 1000);
             con.connect();
--- a/test/jdk/java/net/Inet6Address/B6558853.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/Inet6Address/B6558853.java	Wed Jul 10 22:33:23 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2010, 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
@@ -23,8 +23,11 @@
 
 /*
  * @test
+ * @key intermittent
  * @bug 6558853
  * @summary  getHostAddress() on connections using IPv6 link-local addrs should have zone id
+ *           This test needs to bind to the wildcard address and as such is succeptible to
+ *           fail intermittently because of port reuse issues.
  * @library /test/lib
  * @build jdk.test.lib.NetworkConfiguration
  *        jdk.test.lib.Platform
--- a/test/jdk/java/net/InetAddress/CheckJNI.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/InetAddress/CheckJNI.java	Wed Jul 10 22:33:23 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2010, 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
@@ -74,7 +74,7 @@
     }
 
     static void testSocket(InetAddress ia) throws Exception {
-        ServerSocket server = new ServerSocket(0);
+        ServerSocket server = new ServerSocket(0, 0, ia);
         Socket s = new Socket(ia, server.getLocalPort());
         s.close();
         server.close();
--- a/test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java	Wed Jul 10 22:33:23 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 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
@@ -40,6 +40,7 @@
         return osname.contains("Windows");
     }
 
+    private static final String MESSAGE = "hello world (" + System.nanoTime() + ")";
     public static void main(String[] args) throws Exception {
         if (isWindows()) {
             System.out.println("The test only run on non-Windows OS. Bye.");
@@ -49,6 +50,7 @@
         MulticastSocket msock = null;
         List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
         Sender sender = null;
+        Thread senderThread = null;
         try {
             msock = new MulticastSocket();
             int port = msock.getLocalPort();
@@ -69,7 +71,8 @@
             }
 
             sender = new Sender(groups);
-            new Thread(sender).start();
+            senderThread = new Thread(sender);
+            senderThread.start();
 
             // Now try to receive multicast packets. we should not see any of them
             // since we disable loopback mode.
@@ -77,20 +80,41 @@
             msock.setSoTimeout(5000);       // 5 seconds
 
             byte[] buf = new byte[1024];
+            for (int i = 0; i < buf.length; i++) {
+                buf[i] = (byte) 'z';
+            }
             DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
+            byte[] expected = MESSAGE.getBytes();
+            assert expected.length <= buf.length;
             for (SocketAddress group : groups) {
+                System.out.println("joining group: " + group);
                 msock.joinGroup(group, null);
 
                 try {
-                    msock.receive(packet);
+                    do {
+                        for (int i = 0; i < buf.length; i++) {
+                            buf[i] = (byte) 'a';
+                        }
+                        msock.receive(packet);
+                        byte[] data = packet.getData();
+                        int len = packet.getLength();
 
-                    // it is an error if we receive something
-                    failedGroups.add(group);
+                        if (expected(data, len, expected)) {
+                            failedGroups.add(group);
+                            break;
+                        } else {
+                            System.err.println("WARNING: Unexpected packet received from "
+                                               + group + ": "
+                                               + len + " bytes");
+                            System.err.println("\t as text: " + new String(data, 0, len));
+                        }
+                    } while (true);
                 } catch (SocketTimeoutException e) {
                     // we expect this
+                    System.out.println("Received expected exception from " + group + ": " + e);
+                } finally {
+                    msock.leaveGroup(group, null);
                 }
-
-                msock.leaveGroup(group, null);
             }
         } finally {
             if (msock != null) try { msock.close(); } catch (Exception e) {}
@@ -98,13 +122,26 @@
                 sender.stop();
             }
         }
+        try {
+            if (failedGroups.size() > 0) {
+                System.out.println("We should not receive anything from following groups, but we did:");
+                for (SocketAddress group : failedGroups)
+                    System.out.println(group);
+                throw new RuntimeException("test failed.");
+            }
+        } finally {
+            if (senderThread != null) {
+                senderThread.join();
+            }
+        }
+    }
 
-        if (failedGroups.size() > 0) {
-            System.out.println("We should not receive anything from following groups, but we did:");
-            for (SocketAddress group : failedGroups)
-                System.out.println(group);
-            throw new RuntimeException("test failed.");
+    static boolean expected(byte[] data, int len, byte[] expected) {
+        if (len != expected.length) return false;
+        for (int i = 0; i < len; i++) {
+            if (data[i] != expected[i]) return false;
         }
+        return true;
     }
 
     static class Sender implements Runnable {
@@ -116,16 +153,15 @@
         }
 
         public void run() {
-            byte[] buf = "hello world".getBytes();
+            byte[] buf = MESSAGE.getBytes();
             List<DatagramPacket> packets = new ArrayList<DatagramPacket>();
 
-            try {
+            try (MulticastSocket msock = new MulticastSocket()) {
                 for (SocketAddress group : sendToGroups) {
                     DatagramPacket packet = new DatagramPacket(buf, buf.length, group);
                     packets.add(packet);
                 }
 
-                MulticastSocket msock = new MulticastSocket();
                 msock.setLoopbackMode(true);    // disable loopback mode
                 while (!stop) {
                     for (DatagramPacket packet : packets) {
--- a/test/jdk/java/net/Socket/AsyncShutdown.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/Socket/AsyncShutdown.java	Wed Jul 10 22:33:23 2019 +0200
@@ -29,6 +29,8 @@
  */
 
 import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketTimeoutException;
@@ -123,7 +125,7 @@
     {
         Socket s1 = null;
         Socket s2 = null;
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = createBoundServer()) {
             s1 = new Socket();
             s1.connect(ss.getLocalSocketAddress());
             s2 = ss.accept();
@@ -134,4 +136,12 @@
         }
     }
 
+    static ServerSocket createBoundServer() throws IOException {
+        ServerSocket ss = new ServerSocket();
+        InetAddress loopback = InetAddress.getLoopbackAddress();
+        InetSocketAddress address = new InetSocketAddress(loopback, 0);
+        ss.bind(address);
+        return ss;
+    }
+
 }
--- a/test/jdk/java/net/Socket/B6210227.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/Socket/B6210227.java	Wed Jul 10 22:33:23 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 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,13 +25,17 @@
  * @test
  * @bug 6210227
  * @library /test/lib
- * @summary  REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP
+ * @summary  REGRESSION: Socket.getLocalAddress() returns address of 0.0.0.0 on outbound TCP.
+ *           This test requires binding to the wildcard address.
  * @run main B6210227
  * @run main/othervm -Djava.net.preferIPv4Stack=true B6210227
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true B6210227
  */
 
 import java.util.*;
 import java.net.*;
+import java.util.stream.IntStream;
+import java.util.stream.Collectors;
 import jdk.test.lib.net.IPSupport;
 
 public class B6210227 {
@@ -42,15 +46,21 @@
         ServerSocket ss = new ServerSocket(0);
         int port = ss.getLocalPort();
 
-        byte[] bad = {0,0,0,0};
         try {
             InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
             Socket s = new Socket();
             s.connect( isa, 1000 );
-            InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0. 0.0 this would demonstrate issue
+            InetAddress iaLocal = s.getLocalAddress(); // if this comes back as 0.0.0.0 this would demonstrate issue
             String      sLocalHostname = iaLocal.getHostName();
-            if (Arrays.equals (iaLocal.getAddress(), bad)) {
-                throw new RuntimeException ("0.0.0.0 returned");
+            byte[]      address = iaLocal.getAddress();
+            if (isWildcard(address)) {
+                if (iaLocal instanceof Inet6Address) {
+                    String msg = IntStream.range(0, address.length)
+                        .mapToObj(i -> "0").collect(Collectors.joining(":"));
+                    throw new RuntimeException(msg + " returned");
+                } else {
+                    throw new RuntimeException ("0.0.0.0 returned");
+                }
             }
             System.out.println("local hostname is "+sLocalHostname );
         } catch(Exception e) {
@@ -60,5 +70,11 @@
             ss.close();
         }
     }
+
+    private static boolean isWildcard(byte[] bytes) {
+        for (int i = 0; i < bytes.length ; i++) {
+            if (bytes[i] != 0) return false;
+        }
+        return true;
+    }
 }
-
--- a/test/jdk/java/net/Socket/LinkLocal.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/Socket/LinkLocal.java	Wed Jul 10 22:33:23 2019 +0200
@@ -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
@@ -55,7 +55,8 @@
          * Create ServerSocket on wildcard address and then
          * try to connect Socket to link-local address.
          */
-        ServerSocket ss = new ServerSocket(0);
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(ia, 0));
 
         Socket s = new Socket();
         try {
@@ -87,7 +88,7 @@
         }
 
         DatagramSocket ds1 = new DatagramSocket();
-        DatagramSocket ds2 = new DatagramSocket();
+        DatagramSocket ds2 = new DatagramSocket(0, ia);
 
         try {
             byte b[] = "Hello".getBytes();
--- a/test/jdk/java/net/Socket/asyncClose/BrokenPipe.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/Socket/asyncClose/BrokenPipe.java	Wed Jul 10 22:33:23 2019 +0200
@@ -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
@@ -57,7 +57,7 @@
     public static void main(String[] args) throws Exception {
         IPSupport.throwSkippedExceptionIfNonOperational();
 
-        ServerSocket ss = new ServerSocket(0);
+        ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
         Socket client = new Socket(InetAddress.getLocalHost(),
                                    ss.getLocalPort());
         Socket server = ss.accept();
--- a/test/jdk/java/net/ipv6tests/B6521014.java	Wed Jul 10 16:45:28 2019 +0000
+++ b/test/jdk/java/net/ipv6tests/B6521014.java	Wed Jul 10 22:33:23 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 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,7 +71,7 @@
     }
 
     static void test1(Inet6Address sin) throws Exception {
-        try (ServerSocket ssock = new ServerSocket(0);
+        try (ServerSocket ssock = createBoundServer(sin);
              Socket sock = new Socket()) {
             int port = ssock.getLocalPort();
             sock.connect(new InetSocketAddress(sin, port), 100);
@@ -82,7 +82,7 @@
     }
 
     static void test2(Inet6Address sin) throws Exception {
-        try (ServerSocket ssock = new ServerSocket(0);
+        try (ServerSocket ssock = createBoundServer(sin);
              Socket sock = new Socket()) {
             int port = ssock.getLocalPort();
             ssock.setSoTimeout(100);
@@ -94,6 +94,13 @@
         }
     }
 
+    static ServerSocket createBoundServer(Inet6Address sin) throws IOException {
+        ServerSocket ss = new ServerSocket();
+        InetSocketAddress address = new InetSocketAddress(sin, 0);
+        ss.bind(address);
+        return ss;
+    }
+
     public static void main(String[] args) throws Exception {
         Optional<Inet6Address> oaddr = getLocalAddr();
         if (!oaddr.isPresent()) {