8223573: Replace wildcard address with loopback or local host in tests - part 4
Makes a few intermittently failing tests more resilient to port reuse issues by ensuring they bind to the loopback address instead of the wildcard. Reviewed-by: alanb, chegar
This commit is contained in:
parent
c90f3ee68b
commit
205fd87f2d
test/jdk
java/net/Socket
sun/net
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2012, 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
|
||||
@ -29,6 +29,8 @@
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.Phaser;
|
||||
@ -42,7 +44,9 @@ public class Streams {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
try (ServerSocket ss = new ServerSocket(0)) {
|
||||
try (ServerSocket ss = new ServerSocket()) {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
ss.bind(new InetSocketAddress(loopback, 0));
|
||||
runTest(OutputStreamGetter.class, ss);
|
||||
runTest(InputStreamGetter.class, ss);
|
||||
}
|
||||
@ -55,9 +59,12 @@ public class Streams {
|
||||
throws Exception
|
||||
{
|
||||
final int port = ss.getLocalPort();
|
||||
final InetAddress address = ss.getInetAddress();
|
||||
Socket[] sockets = new Socket[NUM_THREADS];
|
||||
for (int i=0; i<NUM_THREADS; i++) {
|
||||
sockets[i] = new Socket("localhost", port);
|
||||
sockets[i] = address.isAnyLocalAddress()
|
||||
? new Socket("localhost", port)
|
||||
: new Socket(address, port);
|
||||
try (Socket socket = ss.accept()) {}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 2018, 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
|
||||
@ -32,17 +32,19 @@
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URL;
|
||||
|
||||
public class FtpURLConnectionLeak {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
FtpServer server = new FtpServer(0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
FtpServer server = new FtpServer(loopback, 0);
|
||||
server.setFileSystemHandler(new CustomFileSystemHandler("/"));
|
||||
server.setAuthHandler(new MyAuthHandler());
|
||||
int port = server.getLocalPort();
|
||||
String authority = server.getAuthority();
|
||||
server.start();
|
||||
URL url = new URL("ftp://localhost:" + port + "/filedoesNotExist.txt");
|
||||
URL url = new URL("ftp://" + authority + "/filedoesNotExist.txt");
|
||||
try (server) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
|
@ -96,10 +96,11 @@ public class AuthHeaderTest implements HttpCallback {
|
||||
public static void main (String[] args) throws Exception {
|
||||
MyAuthenticator auth = new MyAuthenticator ();
|
||||
Authenticator.setDefault (auth);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
try {
|
||||
server = new TestHttpServer (new AuthHeaderTest(), 1, 10, 0);
|
||||
System.out.println ("Server: listening on port: " + server.getLocalPort());
|
||||
client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
|
||||
server = new TestHttpServer (new AuthHeaderTest(), 1, 10, loopback, 0);
|
||||
System.out.println ("Server: listening on port: " + server.getAuthority());
|
||||
client ("http://" + server.getAuthority() + "/d1/foo.html");
|
||||
} catch (Exception e) {
|
||||
if (server != null) {
|
||||
server.terminate();
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2006, 2018, 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
|
||||
@ -54,6 +54,16 @@ public class FtpServer extends Thread implements AutoCloseable {
|
||||
private boolean done = false;
|
||||
private ArrayList<FtpCommandHandler> clients = new ArrayList<FtpCommandHandler>();
|
||||
|
||||
/**
|
||||
* Creates an instance of an FTP server which will listen for incoming
|
||||
* connections on the specified port. If the port is set to 0, it will
|
||||
* automatically select an available ephemeral port.
|
||||
*/
|
||||
public FtpServer(InetAddress addr, int port) throws IOException {
|
||||
listener = new ServerSocket();
|
||||
listener.bind(new InetSocketAddress(addr, port));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of an FTP server which will listen for incoming
|
||||
* connections on the specified port. If the port is set to 0, it will
|
||||
@ -100,6 +110,17 @@ public class FtpServer extends Thread implements AutoCloseable {
|
||||
return listener.getLocalPort();
|
||||
}
|
||||
|
||||
public String getAuthority() {
|
||||
InetAddress address = listener.getInetAddress();
|
||||
String hostaddr = address.isAnyLocalAddress()
|
||||
? "localhost" : address.getHostAddress();
|
||||
if (hostaddr.indexOf(':') > -1) {
|
||||
hostaddr = "[" + hostaddr + "]";
|
||||
}
|
||||
return hostaddr + ":" + getLocalPort();
|
||||
}
|
||||
|
||||
|
||||
void addClient(Socket client) {
|
||||
FtpCommandHandler h = new FtpCommandHandler(client, this);
|
||||
h.setHandlers(fsh, auth);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2004, 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,6 +25,7 @@
|
||||
* @test
|
||||
* @bug 4333920
|
||||
* @modules jdk.httpserver
|
||||
* @library /test/lib
|
||||
* @run main ChunkedEncodingTest
|
||||
* @summary ChunkedEncodingTest unit test
|
||||
*/
|
||||
@ -36,6 +37,7 @@ import com.sun.net.httpserver.HttpServer;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import static java.lang.System.out;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class ChunkedEncodingTest{
|
||||
private static MessageDigest serverDigest, clientDigest;
|
||||
@ -61,7 +63,13 @@ public class ChunkedEncodingTest{
|
||||
|
||||
int port = server.getAddress().getPort();
|
||||
out.println ("Server listening on port: " + port);
|
||||
client("http://localhost:" + port + "/chunked/");
|
||||
String url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.host(server.getAddress().getAddress())
|
||||
.port(port)
|
||||
.path("/chunked/")
|
||||
.build().toString();
|
||||
client(url);
|
||||
|
||||
if (!MessageDigest.isEqual(clientMac, serverMac)) {
|
||||
throw new RuntimeException(
|
||||
@ -83,7 +91,8 @@ public class ChunkedEncodingTest{
|
||||
* Http Server
|
||||
*/
|
||||
static HttpServer startHttpServer() throws IOException {
|
||||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
|
||||
HttpHandler httpHandler = new SimpleHandler();
|
||||
httpServer.createContext("/chunked/", httpHandler);
|
||||
httpServer.start();
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2004, 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
|
||||
@ -27,6 +27,7 @@
|
||||
* @summary ChunkedEncoding unit test; MeteredStream/ProgressData problem
|
||||
* @modules java.base/sun.net
|
||||
* jdk.httpserver
|
||||
* @library /test/lib
|
||||
* @run main ChunkedEncodingWithProgressMonitorTest
|
||||
*/
|
||||
|
||||
|
@ -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
|
||||
@ -25,6 +25,7 @@
|
||||
* @test
|
||||
* @bug 6446990
|
||||
* @modules jdk.httpserver
|
||||
* @library /test/lib
|
||||
* @run main/othervm TestAvailable
|
||||
* @summary HttpURLConnection#available() reads more and more data into memory
|
||||
*/
|
||||
@ -35,6 +36,7 @@ import java.io.*;
|
||||
import com.sun.net.httpserver.*;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class TestAvailable
|
||||
{
|
||||
@ -60,7 +62,13 @@ public class TestAvailable
|
||||
try {
|
||||
InetSocketAddress address = httpServer.getAddress();
|
||||
|
||||
URL url = new URL("http://localhost:" + address.getPort() + "/testAvailable/");
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.host(address.getAddress())
|
||||
.port(address.getPort())
|
||||
.path("/testAvailable/")
|
||||
.toURLUnchecked();
|
||||
|
||||
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
|
||||
|
||||
uc.setDoOutput(true);
|
||||
@ -102,7 +110,9 @@ public class TestAvailable
|
||||
* Http Server
|
||||
*/
|
||||
public void startHttpServer() throws IOException {
|
||||
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
InetSocketAddress sockaddr = new InetSocketAddress(loopback, 0);
|
||||
httpServer = com.sun.net.httpserver.HttpServer.create(sockaddr, 0);
|
||||
|
||||
// create HttpServer context
|
||||
HttpContext ctx = httpServer.createContext("/testAvailable/", new MyHandler());
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2002, 2018, 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
|
||||
@ -84,8 +84,8 @@ public class MultiThreadTest extends Thread {
|
||||
byte[] b;
|
||||
int requests;
|
||||
|
||||
MultiThreadTest(int port, int requests) throws Exception {
|
||||
uri = "http://localhost:" + port + "/foo.html";
|
||||
MultiThreadTest(String authority, int requests) throws Exception {
|
||||
uri = "http://" + authority + "/foo.html";
|
||||
|
||||
b = new byte [256];
|
||||
this.requests = requests;
|
||||
@ -134,14 +134,16 @@ public class MultiThreadTest extends Thread {
|
||||
}
|
||||
|
||||
/* start the server */
|
||||
ServerSocket ss = new ServerSocket(0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
ServerSocket ss = new ServerSocket();
|
||||
ss.bind(new InetSocketAddress(loopback, 0));
|
||||
Server svr = new Server(ss);
|
||||
svr.start();
|
||||
|
||||
Object lock = MultiThreadTest.getLock();
|
||||
synchronized (lock) {
|
||||
for (int i=0; i<threads; i++) {
|
||||
MultiThreadTest t = new MultiThreadTest(ss.getLocalPort(), requests);
|
||||
MultiThreadTest t = new MultiThreadTest(svr.getAuthority(), requests);
|
||||
t.start ();
|
||||
}
|
||||
try {
|
||||
@ -185,6 +187,16 @@ public class MultiThreadTest extends Thread {
|
||||
this.ss = ss;
|
||||
}
|
||||
|
||||
public String getAuthority() {
|
||||
InetAddress address = ss.getInetAddress();
|
||||
String hostaddr = address.isAnyLocalAddress()
|
||||
? "localhost" : address.getHostAddress();
|
||||
if (hostaddr.indexOf(':') > -1) {
|
||||
hostaddr = "[" + hostaddr + "]";
|
||||
}
|
||||
return hostaddr + ":" + ss.getLocalPort();
|
||||
}
|
||||
|
||||
public Queue<Worker> workers() {
|
||||
return workers;
|
||||
}
|
||||
|
@ -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
|
||||
@ -29,6 +29,7 @@ import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Authenticator;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.net.URL;
|
||||
@ -54,7 +55,6 @@ import java.util.List;
|
||||
*/
|
||||
public class DigestAuth {
|
||||
|
||||
static final String LOCALHOST = "localhost";
|
||||
static final String EXPECT_FAILURE = null;
|
||||
static final String EXPECT_DIGEST = "Digest";
|
||||
static final String REALM = "testrealm@host.com";
|
||||
@ -119,8 +119,7 @@ public class DigestAuth {
|
||||
AuthenticatorImpl auth = new AuthenticatorImpl();
|
||||
Authenticator.setDefault(auth);
|
||||
|
||||
String url = String.format("http://%s:%d/test/",
|
||||
LOCALHOST, server.getPort());
|
||||
String url = String.format("http://%s/test/", server.getAuthority());
|
||||
|
||||
boolean success = true;
|
||||
switch (testcase) {
|
||||
@ -322,6 +321,16 @@ public class DigestAuth {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public String getAuthority() {
|
||||
InetAddress address = server.getAddress().getAddress();
|
||||
String hostaddr = address.isAnyLocalAddress()
|
||||
? "localhost" : address.getHostAddress();
|
||||
if (hostaddr.indexOf(':') > -1) {
|
||||
hostaddr = "[" + hostaddr + "]";
|
||||
}
|
||||
return hostaddr + ":" + getPort();
|
||||
}
|
||||
|
||||
void setWWWAuthHeader(String wwwAuthHeader) {
|
||||
this.wwwAuthHeader = wwwAuthHeader;
|
||||
}
|
||||
@ -331,8 +340,9 @@ public class DigestAuth {
|
||||
}
|
||||
|
||||
static LocalHttpServer startServer() throws IOException {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
HttpServer httpServer = HttpServer.create(
|
||||
new InetSocketAddress(0), 0);
|
||||
new InetSocketAddress(loopback, 0), 0);
|
||||
LocalHttpServer localHttpServer = new LocalHttpServer(httpServer);
|
||||
localHttpServer.start();
|
||||
|
||||
@ -342,7 +352,7 @@ public class DigestAuth {
|
||||
void start() {
|
||||
server.createContext("/test", this);
|
||||
server.start();
|
||||
System.out.println("HttpServer: started on port " + getPort());
|
||||
System.out.println("HttpServer: started on port " + getAuthority());
|
||||
}
|
||||
|
||||
void stop() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2010, 2016, 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
|
||||
@ -43,14 +43,15 @@ public class test {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
TestCache.reset();
|
||||
HttpServer s = HttpServer.create (new InetSocketAddress(0), 10);
|
||||
s.createContext ("/", new HttpHandler () {
|
||||
public void handle (HttpExchange e) {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
|
||||
s.createContext("/", new HttpHandler() {
|
||||
public void handle(HttpExchange e) {
|
||||
try {
|
||||
byte[] buf = new byte [LEN];
|
||||
OutputStream o = e.getResponseBody();
|
||||
e.sendResponseHeaders(200, LEN);
|
||||
o.write (buf);
|
||||
o.write(buf);
|
||||
e.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
@ -91,10 +92,10 @@ public class test {
|
||||
}
|
||||
|
||||
if (TestCache.fail) {
|
||||
System.out.println ("TEST FAILED");
|
||||
throw new RuntimeException ();
|
||||
System.out.println("TEST FAILED");
|
||||
throw new RuntimeException();
|
||||
} else {
|
||||
System.out.println ("TEST OK");
|
||||
System.out.println("TEST OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2004, 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,6 +25,7 @@
|
||||
* @test
|
||||
* @bug 6189206
|
||||
* @modules java.base/sun.net.www
|
||||
* @library /test/lib
|
||||
* @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader
|
||||
* @summary HTTP client should set "Connection: close" header in request when keepalive is disabled
|
||||
*/
|
||||
@ -33,7 +34,7 @@ import java.net.*;
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import sun.net.www.MessageHeader;
|
||||
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class CloseOptionHeader implements Runnable {
|
||||
static ServerSocket ss;
|
||||
@ -79,14 +80,20 @@ public class CloseOptionHeader implements Runnable {
|
||||
Thread tester = new Thread(new CloseOptionHeader());
|
||||
|
||||
/* start the server */
|
||||
ss = new ServerSocket(0);
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
ss = new ServerSocket();
|
||||
ss.bind(new InetSocketAddress(loopback, 0));
|
||||
tester.start();
|
||||
|
||||
/* connect to the server just started
|
||||
* server then check the request to see whether
|
||||
* there is a close connection option header in it
|
||||
*/
|
||||
URL url = new URL("http://localhost:" + ss.getLocalPort());
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.host(ss.getInetAddress())
|
||||
.port(ss.getLocalPort())
|
||||
.toURL();
|
||||
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
|
||||
huc.connect();
|
||||
huc.getResponseCode();
|
||||
|
Loading…
x
Reference in New Issue
Block a user