8225430: Replace wildcard address with loopback or local host in tests - part 14
Reviewed-by: dfuchs, chegar, vtewari
--- a/test/jdk/java/net/DatagramSocket/ReuseAddressTest.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/DatagramSocket/ReuseAddressTest.java Thu Aug 08 21:58:11 2019 +0100
@@ -30,6 +30,9 @@
/*
* @test
* @bug 8153674
+ * @key intermittent
+ * @summary This test might fail intermittently as it needs a UDP socket that
+ * binds to the wildcard address.
* @summary Expected SocketException not thrown when calling bind() with
* setReuseAddress(false)
* @run main/othervm ReuseAddressTest
--- a/test/jdk/java/net/DatagramSocket/SendSize.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/DatagramSocket/SendSize.java Thu Aug 08 21:58:11 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 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,13 +36,16 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
public class SendSize {
static final int bufferLength = 512;
static final int packetLength = 256;
public static void main(String[] args) throws Exception {
- DatagramSocket serverSocket = new DatagramSocket();
+ DatagramSocket serverSocket = new DatagramSocket(
+ new InetSocketAddress(InetAddress.getLocalHost(), 0)
+ );
Thread server = new ServerThread(serverSocket);
server.start();
Thread client = new ClientThread(serverSocket.getLocalPort());
--- a/test/jdk/java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/DatagramSocket/SetDatagramSocketImplFactory/ADatagramSocket.java Thu Aug 08 21:58:11 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 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
@@ -31,9 +31,10 @@
import java.io.*;
import java.net.*;
import java.util.*;
+import java.util.concurrent.CountDownLatch;
public class ADatagramSocket {
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws Exception {
// testing out setDatagramSocketImplFactory
System.err.println("setting DatagramSocketImplFactory...");
try {
@@ -46,6 +47,8 @@
int port = server.getPort();
System.out.println("Server port is " + port);
server.start();
+ // Wait server thread to reach receive call
+ server.readyToStart.await();
// get a datagram socket
DatagramSocket socket = new DatagramSocket();
@@ -72,6 +75,7 @@
protected DatagramSocket socket = null;
private final int port;
+ final CountDownLatch readyToStart = new CountDownLatch(1);
public QuoteServerThread() throws IOException {
this("QuoteServerThread");
@@ -79,7 +83,7 @@
public QuoteServerThread(String name) throws IOException {
super(name);
- socket = new DatagramSocket(0);
+ socket = new DatagramSocket(0, InetAddress.getLocalHost());
port = socket.getLocalPort();
}
public int getPort(){
@@ -92,6 +96,8 @@
// receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
+ // Notify client that server is ready to receive packet
+ readyToStart.countDown();
socket.receive(packet);
// figure out response
--- a/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java Thu Aug 08 21:58:11 2019 +0100
@@ -26,8 +26,11 @@
*
* @test
* @bug 6368984
+ * @key intermittent
* @summary Configuring unconnected Socket before passing to implAccept
- * can cause fd leak
+ * can cause fd leak.
+ * This test may fail intermittently if foreign processes will
+ * try to establish connection to the test server socket.
* @requires (os.family != "windows")
* @library /test/lib
* @build jdk.test.lib.Utils
@@ -43,6 +46,7 @@
import java.io.IOException;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
@@ -82,7 +86,7 @@
}
}
- final ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()) {
+ final ServerSocket ss = new ServerSocket() {
public Socket accept() throws IOException {
Socket s = new Socket() {
};
@@ -91,23 +95,29 @@
return s;
}
};
+ ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
Thread t = new Thread(new Runnable() {
public void run() {
+ int repsCompleted = 0;
try {
- for (int i = 0; i < REPS; i++) {
+ for (; repsCompleted < REPS; repsCompleted++) {
(new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort())).close();
}
} catch (IOException e) {
e.printStackTrace();
+ } finally {
+ System.out.println("Client iterations completed:" + repsCompleted);
}
}
});
t.start();
+ int repsCompleted = 0;
try {
- for (int i = 0; i < REPS; i++) {
+ for (; repsCompleted < REPS; repsCompleted++) {
ss.accept().close();
}
} finally {
+ System.out.println("Server iterations completed:" + repsCompleted);
ss.close();
}
t.join();
--- a/test/jdk/java/net/Socket/NullHost.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/Socket/NullHost.java Thu Aug 08 21:58:11 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
@@ -36,7 +36,10 @@
public Server() throws IOException {
svr = new ServerSocket();
- svr.bind(new InetSocketAddress(0));
+ // The client side calls Socket((String) null, ...) which
+ // resolves to InetAddress.getByName((String)null) which in
+ // turns will resolve to the loopback address
+ svr.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
}
public int getPort() {
--- a/test/jdk/java/net/Socket/ProxyCons.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/Socket/ProxyCons.java Thu Aug 08 21:58:11 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
@@ -64,12 +64,14 @@
}
void test() throws Exception {
- ServerSocket ss = new ServerSocket(0);
+ InetAddress localHost = InetAddress.getLocalHost();
+ ServerSocket ss = new ServerSocket();
+ ss.bind(new InetSocketAddress(localHost, 0));
try {
Server s = new Server(ss);
s.start();
Socket sock = new Socket(Proxy.NO_PROXY);
- sock.connect(new InetSocketAddress("localhost", ss.getLocalPort()));
+ sock.connect(new InetSocketAddress(localHost, ss.getLocalPort()));
s.done();
sock.close();
} catch (java.io.IOException e) {
--- a/test/jdk/java/net/Socket/SocksConnectTimeout.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/Socket/SocksConnectTimeout.java Thu Aug 08 21:58:11 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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
@@ -53,7 +53,9 @@
IPSupport.throwSkippedExceptionIfNonOperational();
try {
- serverSocket = new ServerSocket(0);
+ serverSocket = new ServerSocket();
+ InetAddress localHost = InetAddress.getLocalHost();
+ serverSocket.bind(new InetSocketAddress(localHost, 0));
(new Thread() {
@Override
@@ -61,7 +63,7 @@
}).start();
Proxy socksProxy = new Proxy(Proxy.Type.SOCKS,
- new InetSocketAddress(InetAddress.getLocalHost(), serverSocket.getLocalPort()));
+ new InetSocketAddress(localHost, serverSocket.getLocalPort()));
test(socksProxy);
} catch (IOException e) {
--- a/test/jdk/java/net/Socket/TestClose.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/Socket/TestClose.java Thu Aug 08 21:58:11 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
@@ -44,9 +44,11 @@
InetAddress ad1, ad2;
int port1, port2, serverport;
- ss = new ServerSocket(0);
+ InetAddress loopback = InetAddress.getLoopbackAddress();
+ ss = new ServerSocket();
+ ss.bind(new InetSocketAddress(loopback, 0));
serverport = ss.getLocalPort();
- s = new Socket("localhost", serverport);
+ s = new Socket(loopback, serverport);
s.close();
ss.close();
ad1 = ss.getInetAddress();
--- a/test/jdk/java/net/URLClassLoader/ClassLoad.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/URLClassLoader/ClassLoad.java Thu Aug 08 21:58:11 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2016, 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
@@ -25,24 +25,29 @@
* @test
* @bug 4151665
* @modules jdk.httpserver
+ * @library /test/lib
* @summary Test for FileNotFoundException when loading bogus class
*/
import java.io.InputStream;
import java.io.IOException;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLClassLoader;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
+import jdk.test.lib.net.URIBuilder;
public class ClassLoad {
public static void main(String[] args) throws Exception {
boolean error = true;
// Start a dummy server to return 404
- HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
+ HttpServer server = HttpServer.create();
+ server.bind(new InetSocketAddress(
+ InetAddress.getLoopbackAddress(), 0), 0);
HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
@@ -56,7 +61,11 @@
// Client request
try {
- URL url = new URL("http://localhost:" + server.getAddress().getPort());
+ URL url = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(server.getAddress().getPort())
+ .toURL();
String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
ClassLoader loader = new URLClassLoader(new URL[] { url });
System.out.println(url);
--- a/test/jdk/java/net/URLClassLoader/closetest/CloseTest.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/URLClassLoader/closetest/CloseTest.java Thu Aug 08 21:58:11 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, 2017, 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
@@ -168,7 +168,9 @@
}
static void startHttpServer(String docroot) throws Exception {
- httpServer = HttpServer.create(new InetSocketAddress(0), 10);
+ httpServer = HttpServer.create(
+ new InetSocketAddress(InetAddress.getLoopbackAddress(), 0),
+ 10);
HttpContext ctx = httpServer.createContext(
"/", new FileServerHandler(docroot)
);
--- a/test/jdk/java/net/URLConnection/HandleContentTypeWithAttrs.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/URLConnection/HandleContentTypeWithAttrs.java Thu Aug 08 21:58:11 2019 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1998, 2010, 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,8 +24,9 @@
/*
* @test
* @bug 4160200
- * @summary Make sure URLConnection.getContnentHandler
+ * @summary Make sure URLConnection.getContentHandler
* can handle MIME types with attributes
+ * @library /test/lib
* @modules java.base/sun.net.www java.base/sun.net.www.content.text
*/
import java.net.*;
@@ -34,6 +35,8 @@
import sun.net.www.MessageHeader;
import static java.net.Proxy.NO_PROXY;
+import jdk.test.lib.net.URIBuilder;
+
public class HandleContentTypeWithAttrs {
URL url;
@@ -43,7 +46,12 @@
// Request echo.html from myHttpServer.
// In the header of the response, we make
// the content type have some attributes.
- url = new URL("http://localhost:" + port + "/echo.html");
+ url = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(port)
+ .path("/echo.html")
+ .toURL();
URLConnection urlConn = url.openConnection(NO_PROXY);
// the method getContent() calls the method
@@ -135,7 +143,8 @@
/** Start a server on port <i>port</i>. It will call serviceRequest()
for each new connection. */
final public void startServer(int port) throws IOException {
- serverSocket = new ServerSocket(port, 50);
+ serverSocket = new ServerSocket(port, 50,
+ InetAddress.getLoopbackAddress());
serverInstance = new Thread(this);
serverInstance.start();
}
@@ -219,10 +228,13 @@
public myHttpServer () {
try {
- defaultContext
- = new URL("http", InetAddress.getLocalHost().getHostName(), "/");
+ defaultContext = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .path("/")
+ .toURL();
} catch(Exception e) {
- System.out.println("Failed to construct defauit URL context: "
+ System.out.println("Failed to construct default URL context: "
+ e);
e.printStackTrace();
}
--- a/test/jdk/java/net/URLConnection/RedirectLimit.java Thu Aug 08 18:44:11 2019 +0100
+++ b/test/jdk/java/net/URLConnection/RedirectLimit.java Thu Aug 08 21:58:11 2019 +0100
@@ -35,11 +35,12 @@
import java.io.*;
import java.net.*;
+import java.util.concurrent.CountDownLatch;
import jdk.test.lib.net.URIBuilder;
class RedirLimitServer extends Thread {
- static final int TIMEOUT = 10 * 1000;
+ static final int TIMEOUT = 20 * 1000;
static final int NUM_REDIRECTS = 9;
static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
@@ -59,6 +60,7 @@
final ServerSocket ss;
final int port;
+ final CountDownLatch readyToStart = new CountDownLatch(1);
RedirLimitServer(ServerSocket ss) throws IOException {
this.ss = ss;
@@ -85,6 +87,7 @@
public void run() {
try {
+ readyToStart.countDown();
for (int i=0; i<NUM_REDIRECTS; i++) {
try (Socket s = ss.accept()) {
s.setSoTimeout(TIMEOUT);
@@ -100,33 +103,32 @@
}
} catch (Exception e) {
e.printStackTrace();
- } finally {
- try { ss.close(); } catch (IOException unused) {}
}
}
};
public class RedirectLimit {
public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
- int port = ss.getLocalPort();
- RedirLimitServer server = new RedirLimitServer(ss);
- server.start();
+ try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress())) {
+ int port = ss.getLocalPort();
+ RedirLimitServer server = new RedirLimitServer(ss);
+ server.start();
+ server.readyToStart.await();
+ URL url = URIBuilder.newBuilder()
+ .scheme("http")
+ .loopback()
+ .port(port)
+ .toURL();
+ URLConnection conURL = url.openConnection(Proxy.NO_PROXY);
- URL url = URIBuilder.newBuilder()
- .scheme("http")
- .loopback()
- .port(port)
- .toURL();
- URLConnection conURL = url.openConnection();
+ conURL.setDoInput(true);
+ conURL.setAllowUserInteraction(false);
+ conURL.setUseCaches(false);
- conURL.setDoInput(true);
- conURL.setAllowUserInteraction(false);
- conURL.setUseCaches(false);
-
- try (InputStream in = conURL.getInputStream()) {
- if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
- throw new RuntimeException("Unexpected string read");
+ try (InputStream in = conURL.getInputStream()) {
+ if ((in.read() != (int) 'W') || (in.read() != (int) 'o')) {
+ throw new RuntimeException("Unexpected string read");
+ }
}
}
}