Skip to content

Commit

Permalink
8230435: Replace wildcard address with loopback or local host in test…
Browse files Browse the repository at this point in the history
…s - part 22

Fixes tests to use the loopback address whenever possible. It also fixes some safe publishing issues, or add diagnostics in some of the tests.

Reviewed-by: michaelm
  • Loading branch information
dfuch committed Sep 4, 2019
1 parent 7b49c40 commit f71db30
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static void main(String[] args) throws Exception {
String defaultCharset = System.getProperty("file.encoding");
boolean isUTF8 = defaultCharset.equalsIgnoreCase("UTF-8");
testHandler = new Handler();
InetSocketAddress addr = new InetSocketAddress(0);
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
testHttpServer = HttpServer.create(addr, 0);

// Set the passing credentials OLD client
Expand Down
13 changes: 10 additions & 3 deletions test/jdk/java/net/Authenticator/B4678055.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

public class B4678055 implements HttpCallback {

static int count = 0;
static String authstring;
static volatile int count = 0;
static volatile String authstring;

void errorReply (HttpTransaction req, String reply) throws IOException {
req.addResponseHeader ("Connection", "close");
Expand All @@ -56,6 +56,7 @@ void okReply (HttpTransaction req) throws IOException {

public void request (HttpTransaction req) {
try {
System.out.println("Server handling case: "+ count);
authstring = req.getRequestHeader ("Authorization");
System.out.println (authstring);
switch (count) {
Expand Down Expand Up @@ -95,6 +96,7 @@ public void request (HttpTransaction req) {
}
count ++;
} catch (IOException e) {
System.err.println("Unexpected exception for case " + count + ": " + e);
e.printStackTrace();
}
}
Expand Down Expand Up @@ -143,6 +145,8 @@ public static void main (String[] args) throws Exception {
client(serverURL + "d2/foo.html");
client(serverURL + "d2/foo.html");
} catch (Exception e) {
System.out.println("Client got exception: " + e);
System.out.println("Terminating server");
if (server != null) {
server.terminate();
}
Expand All @@ -156,10 +160,13 @@ public static void main (String[] args) throws Exception {
if (!checkFinalAuth()) {
except ("Wrong authorization string received from client");
}
System.out.println("Terminating server");
server.terminate();
}

public static void except (String s) {
System.out.println("Check failed: " + s);
System.out.println("Terminating server");
server.terminate();
throw new RuntimeException (s);
}
Expand All @@ -169,7 +176,7 @@ static class MyAuthenticator extends Authenticator {
super ();
}

int count = 0;
volatile int count = 0;

public PasswordAuthentication getPasswordAuthentication () {
PasswordAuthentication pw;
Expand Down
54 changes: 49 additions & 5 deletions test/jdk/java/net/DatagramSocket/PortUnreachable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/**
* @test
* @bug 4361783
* @key intermittent
* @summary Test to see if ICMP Port Unreachable on non-connected
* DatagramSocket causes a SocketException "socket closed"
* exception on Windows 2000.
Expand All @@ -43,7 +44,7 @@ public class PortUnreachable {
public void serverSend() {
try {
InetAddress addr = InetAddress.getLocalHost();
Thread.currentThread().sleep(1000);
Thread.sleep(1000);
// send a delayed packet which should mean a delayed icmp
// port unreachable
byte b[] = "A late msg".getBytes();
Expand All @@ -64,26 +65,70 @@ public void serverSend() {
DatagramSocket recreateServerSocket (int serverPort) throws Exception {
DatagramSocket serverSocket = null;
int retryCount = 0;
long sleeptime = 0;
System.out.println("Attempting to recreate server socket with port: " +
serverPort);
// it's possible that this method intermittently fails, if some other
// process running on the machine grabs the port we want before us,
// and doesn't release it before the 5 * 500 ms are elapsed...
while (serverSocket == null) {
try {
serverSocket = new DatagramSocket(serverPort, InetAddress.getLocalHost());
} catch (BindException bEx) {
if (retryCount++ < 5) {
Thread.sleep(500);
sleeptime += sleepAtLeast(500);
} else {
System.out.println("Give up after 5 retries");
System.out.println("Give up after 5 retries and " + sleeptime(sleeptime));
System.out.println("Has some other process grabbed port " + serverPort + "?");
throw bEx;
}
}
}

System.out.println("PortUnreachableTest.recreateServerSocket: returning socket == "
+ serverSocket.getLocalAddress() + ":" + serverSocket.getLocalPort());
+ serverSocket.getLocalAddress() + ":" + serverSocket.getLocalPort()
+ " obtained at " + attempt(retryCount) + " attempt with " + sleeptime(sleeptime));
return serverSocket;
}

long sleepAtLeast(long millis) throws Exception {
long start = System.nanoTime();
long ms = millis;
while (ms > 0) {
assert ms < Long.MAX_VALUE/1000_000L;
Thread.sleep(ms);
long elapsedms = (System.nanoTime() - start)/1000_000L;
ms = millis - elapsedms;
}
return millis - ms;
}

String attempt(int retry) {
switch (retry) {
case 0: return "first";
case 1: return "second";
case 2: return "third";
default: return retry + "th";
}
}

String sleeptime(long millis) {
if (millis == 0) return "no sleep";
long sec = millis / 1000L;
long ms = millis % 1000L;
String sleeptime = "";
if (millis > 0) {
if (sec > 0) {
sleeptime = "" + sec + " s" +
(ms > 0 ? " " : "");
}
if (ms > 0 ) {
sleeptime += ms + " ms";
}
} else sleeptime = millis + " ms"; // should not happen
return sleeptime + " of sleep time";
}

PortUnreachable() throws Exception {
clientSock = new DatagramSocket(new InetSocketAddress(InetAddress.getLocalHost(), 0));
clientPort = clientSock.getLocalPort();
Expand Down Expand Up @@ -126,4 +171,3 @@ public static void main(String[] args) throws Exception {
}

}

7 changes: 7 additions & 0 deletions test/jdk/java/net/URLConnection/RedirectLimit.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class RedirLimitServer extends Thread {
// Read until the end of a HTTP request
void readOneRequest(InputStream is) throws IOException {
int requestEndCount = 0, r;
StringBuilder sb = new StringBuilder();
while ((r = is.read()) != -1) {
sb.append((char)r);
if (r == requestEnd[requestEndCount]) {
requestEndCount++;
if (requestEndCount == 4) {
Expand All @@ -83,22 +85,27 @@ void readOneRequest(InputStream is) throws IOException {
requestEndCount = 0;
}
}
System.out.println("Server got request: " + sb.toString());
}

public void run() {
try {
readyToStart.countDown();
for (int i=0; i<NUM_REDIRECTS; i++) {
try (Socket s = ss.accept()) {
System.out.println("Server accepted socket: " + s);
s.setSoTimeout(TIMEOUT);
readOneRequest(s.getInputStream());
System.out.println("Redirecting to: /redirect" + i);
String reply = reply1 + port + "/redirect" + i + reply2;
s.getOutputStream().write(reply.getBytes());
}
}
try (Socket s = ss.accept()) {
System.out.println("Server accepted socket: " + s);
s.setSoTimeout(TIMEOUT);
readOneRequest(s.getInputStream());
System.out.println("Replying...");
s.getOutputStream().write(reply3.getBytes());
}
} catch (Exception e) {
Expand Down
11 changes: 9 additions & 2 deletions test/jdk/java/net/URLConnection/Responses.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ static Object[][] getTests() {
* "HTTP/1.1 404 "
*/
static class HttpServer implements Runnable {
ServerSocket ss;
final ServerSocket ss;
volatile boolean shutdown;

public HttpServer() {
try {
Expand All @@ -83,14 +84,15 @@ public String authority() {
}

public void shutdown() throws IOException {
shutdown = true;
ss.close();
}

public void run() {
Object[][] tests = getTests();

try {
for (;;) {
while(!shutdown) {
Socket s = ss.accept();

BufferedReader in = new BufferedReader(
Expand All @@ -101,6 +103,7 @@ public void run() {
int pos2 = req.indexOf(' ', pos1+1);

int i = Integer.parseInt(req.substring(pos1+2, pos2));
System.out.println("Server replying to >" + tests[i][0] + "<");

PrintStream out = new PrintStream(
new BufferedOutputStream(
Expand All @@ -117,6 +120,9 @@ public void run() {
s.close();
}
} catch (Exception e) {
if (!shutdown) {
e.printStackTrace();
}
}
}
}
Expand Down Expand Up @@ -170,6 +176,7 @@ public static void main(String args[]) throws Exception {
actualPhrase + ", expected: " + expectedPhrase);
}
} catch (IOException e) {
System.err.println("Test failed for >" + tests[i][0] + "<: " + e);
e.printStackTrace();
failures++;
}
Expand Down
22 changes: 18 additions & 4 deletions test/jdk/javax/net/ssl/templates/SSLSocketTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketTimeoutException;
import java.security.KeyStore;
Expand Down Expand Up @@ -221,15 +222,25 @@ protected void configureServerSocket(SSLServerSocket socket) {
*/
protected volatile int serverPort = 0;

/*
* What's the server address? null means binding to the wildcard.
*/
protected volatile InetAddress serverAddress = null;

/*
* Define the server side of the test.
*/
protected void doServerSide() throws Exception {
// kick start the server side service
SSLContext context = createServerSSLContext();
SSLServerSocketFactory sslssf = context.getServerSocketFactory();
SSLServerSocket sslServerSocket =
(SSLServerSocket)sslssf.createServerSocket(serverPort);
InetAddress serverAddress = this.serverAddress;
SSLServerSocket sslServerSocket = serverAddress == null ?
(SSLServerSocket)sslssf.createServerSocket(serverPort)
: (SSLServerSocket)sslssf.createServerSocket();
if (serverAddress != null) {
sslServerSocket.bind(new InetSocketAddress(serverAddress, serverPort));
}
configureServerSocket(sslServerSocket);
serverPort = sslServerSocket.getLocalPort();

Expand Down Expand Up @@ -317,8 +328,11 @@ protected void doClientSide() throws Exception {
try (SSLSocket sslSocket = (SSLSocket)sslsf.createSocket()) {
try {
configureClientSocket(sslSocket);
sslSocket.connect(
new InetSocketAddress("localhost", serverPort), 15000);
InetAddress serverAddress = this.serverAddress;
InetSocketAddress connectAddress = serverAddress == null
? new InetSocketAddress("localhost", serverPort)
: new InetSocketAddress(serverAddress, serverPort);
sslSocket.connect(connectAddress, 15000);
} catch (IOException ioe) {
// The server side may be impacted by naughty test cases or
// third party routines, and cannot accept connections.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,22 @@ static void test(String host, String address,

static void sleep(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {}
sleepms(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

static long sleepms(long millis) throws InterruptedException {
long start = System.nanoTime();
long ms = millis;
while (ms > 0) {
assert ms < Long.MAX_VALUE/1000_000L;
Thread.sleep(ms);
long elapsedms = (System.nanoTime() - start)/1000_000L;
ms = millis - elapsedms;
}
return millis - ms;
}

static void test(String host, String address, boolean shouldSucceed) {
Expand All @@ -114,7 +128,7 @@ static void test(String host, String address, boolean shouldSucceed) {
+ addr + ")");
}
if (!address.equals(addr.getHostAddress())) {
throw new RuntimeException(host+":"+address+": compare failed (found "
throw new RuntimeException(host+"/"+address+": compare failed (found "
+ addr + ")");
}
System.out.println("test: " + host + "/" + address
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/sun/net/www/AuthHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void read (InputStream is) throws IOException {
static void client (String u) throws Exception {
URL url = new URL (u);
System.out.println ("client opening connection to: " + u);
URLConnection urlc = url.openConnection ();
URLConnection urlc = url.openConnection (Proxy.NO_PROXY);
InputStream is = urlc.getInputStream ();
read (is);
is.close();
Expand Down
8 changes: 6 additions & 2 deletions test/jdk/sun/net/www/http/HttpClient/RetryPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ void doClient() throws Exception {
throw new RuntimeException("Failed: POST request being retried");

} catch (SocketException se) {
System.out.println("Got expected exception: " + se);
// this is what we expect to happen and is OK.
if (shouldRetry && httpHandler.getCallCount() != 2)
if (shouldRetry && httpHandler.getCallCount() != 2) {
se.printStackTrace(System.out);
throw new RuntimeException("Failed: Handler should have been called twice. " +
"It was called "+ httpHandler.getCallCount() + " times");
else if (!shouldRetry && httpHandler.getCallCount() != 1)
} else if (!shouldRetry && httpHandler.getCallCount() != 1) {
se.printStackTrace(System.out);
throw new RuntimeException("Failed: Handler should have only been called once" +
"It was called "+ httpHandler.getCallCount() + " times");
}
} finally {
httpServer.stop(1);
executorService.shutdown();
Expand Down
Loading

0 comments on commit f71db30

Please sign in to comment.