8225430: Replace wildcard address with loopback or local host in tests - part 14

Reviewed-by: dfuchs, chegar, vtewari
This commit is contained in:
Aleksei Efimov 2019-08-08 21:58:11 +01:00
parent 9f7cbf60e4
commit 4312f54e51
12 changed files with 108 additions and 52 deletions

View file

@ -30,6 +30,9 @@ import java.net.SocketException;
/* /*
* @test * @test
* @bug 8153674 * @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 * @summary Expected SocketException not thrown when calling bind() with
* setReuseAddress(false) * setReuseAddress(false)
* @run main/othervm ReuseAddressTest * @run main/othervm ReuseAddressTest

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -36,13 +36,16 @@ import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress;
public class SendSize { public class SendSize {
static final int bufferLength = 512; static final int bufferLength = 512;
static final int packetLength = 256; static final int packetLength = 256;
public static void main(String[] args) throws Exception { 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); Thread server = new ServerThread(serverSocket);
server.start(); server.start();
Thread client = new ClientThread(serverSocket.getLocalPort()); Thread client = new ClientThread(serverSocket.getLocalPort());

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -31,9 +31,10 @@
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
import java.util.concurrent.CountDownLatch;
public class ADatagramSocket { public class ADatagramSocket {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws Exception {
// testing out setDatagramSocketImplFactory // testing out setDatagramSocketImplFactory
System.err.println("setting DatagramSocketImplFactory..."); System.err.println("setting DatagramSocketImplFactory...");
try { try {
@ -46,6 +47,8 @@ public class ADatagramSocket {
int port = server.getPort(); int port = server.getPort();
System.out.println("Server port is " + port); System.out.println("Server port is " + port);
server.start(); server.start();
// Wait server thread to reach receive call
server.readyToStart.await();
// get a datagram socket // get a datagram socket
DatagramSocket socket = new DatagramSocket(); DatagramSocket socket = new DatagramSocket();
@ -72,6 +75,7 @@ class QuoteServerThread extends Thread {
protected DatagramSocket socket = null; protected DatagramSocket socket = null;
private final int port; private final int port;
final CountDownLatch readyToStart = new CountDownLatch(1);
public QuoteServerThread() throws IOException { public QuoteServerThread() throws IOException {
this("QuoteServerThread"); this("QuoteServerThread");
@ -79,7 +83,7 @@ class QuoteServerThread extends Thread {
public QuoteServerThread(String name) throws IOException { public QuoteServerThread(String name) throws IOException {
super(name); super(name);
socket = new DatagramSocket(0); socket = new DatagramSocket(0, InetAddress.getLocalHost());
port = socket.getLocalPort(); port = socket.getLocalPort();
} }
public int getPort(){ public int getPort(){
@ -92,6 +96,8 @@ class QuoteServerThread extends Thread {
// receive request // receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length); DatagramPacket packet = new DatagramPacket(buf, buf.length);
// Notify client that server is ready to receive packet
readyToStart.countDown();
socket.receive(packet); socket.receive(packet);
// figure out response // figure out response

View file

@ -26,8 +26,11 @@
* *
* @test * @test
* @bug 6368984 * @bug 6368984
* @key intermittent
* @summary Configuring unconnected Socket before passing to implAccept * @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") * @requires (os.family != "windows")
* @library /test/lib * @library /test/lib
* @build jdk.test.lib.Utils * @build jdk.test.lib.Utils
@ -43,6 +46,7 @@
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.List; import java.util.List;
@ -82,7 +86,7 @@ public class AcceptCauseFileDescriptorLeak {
} }
} }
final ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()) { final ServerSocket ss = new ServerSocket() {
public Socket accept() throws IOException { public Socket accept() throws IOException {
Socket s = new Socket() { Socket s = new Socket() {
}; };
@ -91,23 +95,29 @@ public class AcceptCauseFileDescriptorLeak {
return s; return s;
} }
}; };
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
Thread t = new Thread(new Runnable() { Thread t = new Thread(new Runnable() {
public void run() { public void run() {
int repsCompleted = 0;
try { try {
for (int i = 0; i < REPS; i++) { for (; repsCompleted < REPS; repsCompleted++) {
(new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort())).close(); (new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort())).close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
System.out.println("Client iterations completed:" + repsCompleted);
} }
} }
}); });
t.start(); t.start();
int repsCompleted = 0;
try { try {
for (int i = 0; i < REPS; i++) { for (; repsCompleted < REPS; repsCompleted++) {
ss.accept().close(); ss.accept().close();
} }
} finally { } finally {
System.out.println("Server iterations completed:" + repsCompleted);
ss.close(); ss.close();
} }
t.join(); t.join();

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -36,7 +36,10 @@ public class NullHost {
public Server() throws IOException { public Server() throws IOException {
svr = new ServerSocket(); 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() { public int getPort() {

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -64,12 +64,14 @@ public class ProxyCons {
} }
void test() throws Exception { void test() throws Exception {
ServerSocket ss = new ServerSocket(0); InetAddress localHost = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress(localHost, 0));
try { try {
Server s = new Server(ss); Server s = new Server(ss);
s.start(); s.start();
Socket sock = new Socket(Proxy.NO_PROXY); Socket sock = new Socket(Proxy.NO_PROXY);
sock.connect(new InetSocketAddress("localhost", ss.getLocalPort())); sock.connect(new InetSocketAddress(localHost, ss.getLocalPort()));
s.done(); s.done();
sock.close(); sock.close();
} catch (java.io.IOException e) { } catch (java.io.IOException e) {

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -53,7 +53,9 @@ public class SocksConnectTimeout {
IPSupport.throwSkippedExceptionIfNonOperational(); IPSupport.throwSkippedExceptionIfNonOperational();
try { try {
serverSocket = new ServerSocket(0); serverSocket = new ServerSocket();
InetAddress localHost = InetAddress.getLocalHost();
serverSocket.bind(new InetSocketAddress(localHost, 0));
(new Thread() { (new Thread() {
@Override @Override
@ -61,7 +63,7 @@ public class SocksConnectTimeout {
}).start(); }).start();
Proxy socksProxy = new Proxy(Proxy.Type.SOCKS, Proxy socksProxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(InetAddress.getLocalHost(), serverSocket.getLocalPort())); new InetSocketAddress(localHost, serverSocket.getLocalPort()));
test(socksProxy); test(socksProxy);
} catch (IOException e) { } catch (IOException e) {

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -44,9 +44,11 @@ public class TestClose {
InetAddress ad1, ad2; InetAddress ad1, ad2;
int port1, port2, serverport; int port1, port2, serverport;
ss = new ServerSocket(0); InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
serverport = ss.getLocalPort(); serverport = ss.getLocalPort();
s = new Socket("localhost", serverport); s = new Socket(loopback, serverport);
s.close(); s.close();
ss.close(); ss.close();
ad1 = ss.getInetAddress(); ad1 = ss.getInetAddress();

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,24 +25,29 @@
* @test * @test
* @bug 4151665 * @bug 4151665
* @modules jdk.httpserver * @modules jdk.httpserver
* @library /test/lib
* @summary Test for FileNotFoundException when loading bogus class * @summary Test for FileNotFoundException when loading bogus class
*/ */
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
import jdk.test.lib.net.URIBuilder;
public class ClassLoad { public class ClassLoad {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
boolean error = true; boolean error = true;
// Start a dummy server to return 404 // 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() { HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange t) throws IOException { public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody(); InputStream is = t.getRequestBody();
@ -56,7 +61,11 @@ public class ClassLoad {
// Client request // Client request
try { 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"; String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
ClassLoader loader = new URLClassLoader(new URL[] { url }); ClassLoader loader = new URLClassLoader(new URL[] { url });
System.out.println(url); System.out.println(url);

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -168,7 +168,9 @@ public class CloseTest extends Common {
} }
static void startHttpServer(String docroot) throws Exception { 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( HttpContext ctx = httpServer.createContext(
"/", new FileServerHandler(docroot) "/", new FileServerHandler(docroot)
); );

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,8 +24,9 @@
/* /*
* @test * @test
* @bug 4160200 * @bug 4160200
* @summary Make sure URLConnection.getContnentHandler * @summary Make sure URLConnection.getContentHandler
* can handle MIME types with attributes * can handle MIME types with attributes
* @library /test/lib
* @modules java.base/sun.net.www java.base/sun.net.www.content.text * @modules java.base/sun.net.www java.base/sun.net.www.content.text
*/ */
import java.net.*; import java.net.*;
@ -34,6 +35,8 @@ import sun.net.www.content.text.*;
import sun.net.www.MessageHeader; import sun.net.www.MessageHeader;
import static java.net.Proxy.NO_PROXY; import static java.net.Proxy.NO_PROXY;
import jdk.test.lib.net.URIBuilder;
public class HandleContentTypeWithAttrs { public class HandleContentTypeWithAttrs {
URL url; URL url;
@ -43,7 +46,12 @@ public class HandleContentTypeWithAttrs {
// Request echo.html from myHttpServer. // Request echo.html from myHttpServer.
// In the header of the response, we make // In the header of the response, we make
// the content type have some attributes. // 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); URLConnection urlConn = url.openConnection(NO_PROXY);
// the method getContent() calls the method // the method getContent() calls the method
@ -135,7 +143,8 @@ class myHttpServer implements Runnable, Cloneable {
/** Start a server on port <i>port</i>. It will call serviceRequest() /** Start a server on port <i>port</i>. It will call serviceRequest()
for each new connection. */ for each new connection. */
final public void startServer(int port) throws IOException { 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 = new Thread(this);
serverInstance.start(); serverInstance.start();
} }
@ -219,10 +228,13 @@ class myHttpServer implements Runnable, Cloneable {
public myHttpServer () { public myHttpServer () {
try { try {
defaultContext defaultContext = URIBuilder.newBuilder()
= new URL("http", InetAddress.getLocalHost().getHostName(), "/"); .scheme("http")
.loopback()
.path("/")
.toURL();
} catch(Exception e) { } catch(Exception e) {
System.out.println("Failed to construct defauit URL context: " System.out.println("Failed to construct default URL context: "
+ e); + e);
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -35,11 +35,12 @@
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.concurrent.CountDownLatch;
import jdk.test.lib.net.URIBuilder; import jdk.test.lib.net.URIBuilder;
class RedirLimitServer extends Thread { class RedirLimitServer extends Thread {
static final int TIMEOUT = 10 * 1000; static final int TIMEOUT = 20 * 1000;
static final int NUM_REDIRECTS = 9; static final int NUM_REDIRECTS = 9;
static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" + static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
@ -59,6 +60,7 @@ class RedirLimitServer extends Thread {
final ServerSocket ss; final ServerSocket ss;
final int port; final int port;
final CountDownLatch readyToStart = new CountDownLatch(1);
RedirLimitServer(ServerSocket ss) throws IOException { RedirLimitServer(ServerSocket ss) throws IOException {
this.ss = ss; this.ss = ss;
@ -85,6 +87,7 @@ class RedirLimitServer extends Thread {
public void run() { public void run() {
try { try {
readyToStart.countDown();
for (int i=0; i<NUM_REDIRECTS; i++) { for (int i=0; i<NUM_REDIRECTS; i++) {
try (Socket s = ss.accept()) { try (Socket s = ss.accept()) {
s.setSoTimeout(TIMEOUT); s.setSoTimeout(TIMEOUT);
@ -100,33 +103,32 @@ class RedirLimitServer extends Thread {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try { ss.close(); } catch (IOException unused) {}
} }
} }
}; };
public class RedirectLimit { public class RedirectLimit {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress()); try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress())) {
int port = ss.getLocalPort(); int port = ss.getLocalPort();
RedirLimitServer server = new RedirLimitServer(ss); RedirLimitServer server = new RedirLimitServer(ss);
server.start(); 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() conURL.setDoInput(true);
.scheme("http") conURL.setAllowUserInteraction(false);
.loopback() conURL.setUseCaches(false);
.port(port)
.toURL();
URLConnection conURL = url.openConnection();
conURL.setDoInput(true); try (InputStream in = conURL.getInputStream()) {
conURL.setAllowUserInteraction(false); if ((in.read() != (int) 'W') || (in.read() != (int) 'o')) {
conURL.setUseCaches(false); 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");
} }
} }
} }