8223465: Replace wildcard address with loopback or local host in tests - part 3
authoraefimov
Fri, 10 May 2019 15:34:17 +0100
changeset 54809 351da897f409
parent 54808 cf94f5c214f6
child 54810 258170da6d3a
child 57356 2cf72293e1db
8223465: Replace wildcard address with loopback or local host in tests - part 3 Reviewed-by: dfuchs
test/jdk/java/net/DatagramSocket/PortUnreachable.java
test/jdk/java/net/Socket/HttpProxy.java
test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java
test/jdk/java/net/Socks/SocksV4Test.java
test/jdk/sun/net/www/protocol/http/ChunkedErrorStream.java
--- a/test/jdk/java/net/DatagramSocket/PortUnreachable.java	Fri May 10 09:07:53 2019 -0400
+++ b/test/jdk/java/net/DatagramSocket/PortUnreachable.java	Fri May 10 15:34:17 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 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
@@ -29,9 +29,10 @@
  *           exception on Windows 2000.
  */
 import java.net.BindException;
-import java.net.InetAddress;
+import java.net.DatagramPacket;
 import java.net.DatagramSocket;
-import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 
 public class PortUnreachable {
 
@@ -67,7 +68,7 @@
                 serverPort);
         while (serverSocket == null) {
             try {
-                serverSocket = new DatagramSocket(serverPort);
+                serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost());
             } catch (BindException bEx) {
                 if (retryCount++ < 5) {
                     Thread.sleep(500);
@@ -84,8 +85,7 @@
     }
 
     PortUnreachable() throws Exception {
-
-        clientSock = new DatagramSocket();
+        clientSock = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));
         clientPort = clientSock.getLocalPort();
 
     }
@@ -93,7 +93,7 @@
     void execute () throws Exception{
 
         // pick a port for the server
-        DatagramSocket sock2 = new DatagramSocket();
+        DatagramSocket sock2 = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));
         serverPort = sock2.getLocalPort();
 
         // send a burst of packets to the unbound port - we should get back
--- a/test/jdk/java/net/Socket/HttpProxy.java	Fri May 10 09:07:53 2019 -0400
+++ b/test/jdk/java/net/Socket/HttpProxy.java	Fri May 10 15:34:17 2019 +0100
@@ -25,7 +25,9 @@
  * @test
  * @bug 6370908 8220663
  * @library /test/lib
- * @summary Add support for HTTP_CONNECT proxy in Socket class
+ * @summary Add support for HTTP_CONNECT proxy in Socket class.
+ * This test uses the wildcard address and is susceptible to fail intermittently.
+ * @key intermittent
  * @modules java.base/sun.net.www
  * @run main HttpProxy
  * @run main/othervm -Djava.net.preferIPv4Stack=true HttpProxy
@@ -63,7 +65,7 @@
             // Start internal proxy
             proxy = new ConnectProxyTunnelServer();
             proxy.start();
-            host = "localhost";
+            host = InetAddress.getLoopbackAddress().getHostAddress();
             port = proxy.getLocalPort();
             out.println("Running with internal proxy: " + host + ":" + port);
         } else if (args.length == 2) {
@@ -93,6 +95,7 @@
         InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
         Proxy httpProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
 
+        // Wildcard address is needed here
         try (ServerSocket ss = new ServerSocket(0)) {
             List<InetSocketAddress> externalAddresses = new ArrayList<>();
             externalAddresses.add(
@@ -195,7 +198,7 @@
         private volatile boolean closed;
 
         public ConnectProxyTunnelServer() throws IOException {
-            ss = new ServerSocket(0);
+            ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
         }
 
         @Override
--- a/test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java	Fri May 10 09:07:53 2019 -0400
+++ b/test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java	Fri May 10 15:34:17 2019 +0100
@@ -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
@@ -26,13 +26,18 @@
  * throws a SocketException if the socket is asynchronously closed.
  */
 import java.io.IOException;
-import java.net.*;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
     private final ServerSocket ss;
     private final int timeout;
     private final CountDownLatch latch;
+    private final AtomicBoolean readyToClose = new AtomicBoolean(false);
 
     public ServerSocket_accept() throws IOException {
        this(0);
@@ -55,8 +60,14 @@
     public void run() {
         try {
             latch.countDown();
-            Socket s = ss.accept();
-            failed("ServerSocket.accept() returned unexpectly!!" + " - " + s);
+            Socket s;
+            // if readyToClose is still false it means some other
+            // process on the system attempted to connect: just
+            // ignore it, and go back to accept again.
+            do {
+                s = ss.accept();
+            } while (!readyToClose.get());
+            failed("ServerSocket.accept() returned unexpectedly!!" + " - " + s);
         } catch (SocketException se) {
             closed();
         } catch (Exception e) {
@@ -70,6 +81,7 @@
             thr.start();
             latch.await();
             Thread.sleep(5000); //sleep, so ServerSocket.accept() can block
+            readyToClose.set(true);
             ss.close();
             thr.join();
 
--- a/test/jdk/java/net/Socks/SocksV4Test.java	Fri May 10 09:07:53 2019 -0400
+++ b/test/jdk/java/net/Socks/SocksV4Test.java	Fri May 10 15:34:17 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
@@ -30,7 +30,14 @@
  */
 
 import java.io.IOException;
-import java.net.*;
+import java.net.Authenticator;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.PasswordAuthentication;
+import java.net.Proxy;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
 
 public class SocksV4Test {
 
@@ -55,8 +62,9 @@
         // We actually use V5 for this test because that is the default
         // protocol version used by the client and it doesn't really handle
         // down grading very well.
-        try (SocksServer srvr = new SocksServer(0, false);
-             ServerSocket ss = new ServerSocket(0)) {
+        InetAddress lba = InetAddress.getLoopbackAddress();
+        try (SocksServer srvr = new SocksServer(lba, 0, false);
+             ServerSocket ss = new ServerSocket(0, 0, lba)) {
 
             srvr.addUser(USER, PASSWORD);
             int serverPort = ss.getLocalPort();
@@ -64,9 +72,9 @@
             int proxyPort = srvr.getPort();
             System.out.printf("Server port %d, Proxy port %d\n", serverPort, proxyPort);
             Proxy sp = new Proxy(Proxy.Type.SOCKS,
-                    new InetSocketAddress("localhost", proxyPort));
+                    new InetSocketAddress(lba, proxyPort));
             // Let's create an unresolved address
-            InetSocketAddress ad = new InetSocketAddress("127.0.0.1", serverPort);
+            InetSocketAddress ad = new InetSocketAddress(lba.getHostAddress(), serverPort);
             try (Socket s = new Socket(sp)) {
                 s.connect(ad, 10000);
                 int pp = s.getLocalPort();
@@ -85,11 +93,12 @@
         // sanity before running the test
         assertUnresolvableHost(HOSTNAME);
 
+        InetAddress lba = InetAddress.getLoopbackAddress();
         // Create a SOCKS V4 proxy
-        try (SocksServer srvr = new SocksServer(0, true)) {
+        try (SocksServer srvr = new SocksServer(lba, 0, true)) {
             srvr.start();
             Proxy sp = new Proxy(Proxy.Type.SOCKS,
-                    new InetSocketAddress("localhost", srvr.getPort()));
+                    new InetSocketAddress(lba, srvr.getPort()));
             // Let's create an unresolved address
             InetSocketAddress ad = new InetSocketAddress(HOSTNAME, 1234);
             try (Socket s = new Socket(sp)) {
--- a/test/jdk/sun/net/www/protocol/http/ChunkedErrorStream.java	Fri May 10 09:07:53 2019 -0400
+++ b/test/jdk/sun/net/www/protocol/http/ChunkedErrorStream.java	Fri May 10 15:34:17 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,6 +24,7 @@
 /*
  * @test
  * @bug 6488669 6595324 6993490
+ * @library /test/lib
  * @modules jdk.httpserver
  * @run main/othervm ChunkedErrorStream
  * @summary Chunked ErrorStream tests
@@ -32,6 +33,7 @@
 import java.net.*;
 import java.io.*;
 import com.sun.net.httpserver.*;
+import jdk.test.lib.net.URIBuilder;
 
 /**
  * Part 1: 6488669
@@ -94,16 +96,21 @@
         for (int times=0; times<3; times++) {
             HttpURLConnection uc = null;
             try {
-                InetSocketAddress address = httpServer.getAddress();
-                String URLStr = "http://localhost:" + address.getPort() + "/test/";
+                String path = "/test/";
                 if (times == 0) {
-                    URLStr += "first";
+                    path += "first";
                 } else {
-                    URLStr += "second";
+                    path += "second";
                 }
 
-                System.out.println("Trying " + URLStr);
-                URL url = new URL(URLStr);
+                URL url = URIBuilder.newBuilder()
+                        .scheme("http")
+                        .host(httpServer.getAddress().getAddress())
+                        .port(httpServer.getAddress().getPort())
+                        .path(path)
+                        .toURLUnchecked();
+
+                System.out.println("Trying " + url);
                 uc = (HttpURLConnection)url.openConnection();
                 uc.getInputStream();
 
@@ -142,7 +149,9 @@
      * Http Server
      */
     void startHttpServer() throws IOException {
-        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
+        InetAddress lba = InetAddress.getLoopbackAddress();
+        InetSocketAddress addr = new InetSocketAddress(lba, 0);
+        httpServer = com.sun.net.httpserver.HttpServer.create(addr, 0);
 
         // create HttpServer context
         httpServer.createContext("/test/first", new FirstHandler());