8129315: java/net/Socket/LingerTest.java and java/net/Socket/ShutdownBoth.java timeout intermittently

Tests are updated to use the loopback address instead of the wildcard to avoid traffic being routed to a different server than what was intended by the test.

Reviewed-by: chegar
This commit is contained in:
Daniel Fuchs 2019-04-26 16:25:43 +01:00
parent 63c7f8d0a3
commit 72467d916c
5 changed files with 75 additions and 23 deletions

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.
*
* This code is free software; you can redistribute it and/or modify it
@ -71,11 +71,13 @@ public class LingerTest {
}
static class Other implements Runnable {
int port;
long delay;
final InetAddress address;
final int port;
final long delay;
boolean connected = false;
public Other(int port, long delay) {
public Other(InetAddress address, int port, long delay) {
this.address = address;
this.port = port;
this.delay = delay;
}
@ -85,7 +87,7 @@ public class LingerTest {
try {
Thread.sleep(delay);
System.out.println ("Other opening socket");
Socket s = new Socket("localhost", port);
Socket s = new Socket(address, port);
synchronized (this) {
connected = true;
}
@ -103,9 +105,10 @@ public class LingerTest {
}
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0, 50, loopback);
Socket s1 = new Socket("localhost", ss.getLocalPort());
Socket s1 = new Socket(loopback, ss.getLocalPort());
Socket s2 = ss.accept();
// setup conditions for untransmitted data and lengthy
@ -119,7 +122,7 @@ public class LingerTest {
senderThread.start();
// other thread that will connect after 5 seconds.
Other other = new Other(ss.getLocalPort(), 5000);
Other other = new Other(loopback, ss.getLocalPort(), 5000);
Thread otherThread = new Thread(other);
otherThread.start();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2010, 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
@ -32,7 +32,8 @@ import java.net.*;
public class ShutdownBoth {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
ServerSocket ss = new ServerSocket(0, 50, loopback);
Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
Socket s2 = ss.accept();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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,6 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
@ -43,7 +42,6 @@ import java.net.Inet6Address;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import com.sun.net.httpserver.*;
@ -65,7 +63,7 @@ public class SocksIPv6Test {
public void setUp() throws Exception {
shouldRun = ensureInet6AddressFamily() && ensureIPv6OnLoopback();
server = HttpServer.create(new InetSocketAddress(0), 0);
server = HttpServer.create(new InetSocketAddress("::1", 0), 0);
server.createContext("/", ex -> {
ex.sendResponseHeaders(200, response.length());
try (BufferedWriter writer = new BufferedWriter(
@ -76,7 +74,7 @@ public class SocksIPv6Test {
});
server.start();
socks = new SocksServer(0, false);
socks = new SocksServer(InetAddress.getByName("::1"), 0, false);
socks.addUser("user", "pass");
socks.start();
@ -140,21 +138,45 @@ public class SocksIPv6Test {
public void testSocksOverIPv6Hostname() throws Exception {
if (!shouldRun) return;
String ipv6Hostname = InetAddress.getByName("::1").getHostName();
String ipv4Hostname = InetAddress.getByName("127.0.0.1").getHostName();
InetAddress ipv6Loopback = InetAddress.getByName("::1");
String ipv6Hostname = ipv6Loopback.getHostName();
String ipv6HostAddress = ipv6Loopback.getHostAddress();
InetAddress ipv4Loopback;
String ipv4Hostname;
String ipv4HostAddress;
try {
ipv4Loopback = InetAddress.getByName("127.0.0.1");
ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName();
ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress();
} catch (IOException io) {
ipv4Hostname = null;
ipv4HostAddress = null;
}
if (ipv6Hostname.equals(InetAddress.getByName("::1").getHostAddress())) {
System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress);
System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress);
if (ipv6Hostname.equals(ipv6HostAddress)) {
System.out.println("Unable to get the hostname of the IPv6 loopback "
+ "address. Skipping test case.");
return;
}
if (ipv6Hostname.equals(ipv4Hostname)) {
if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) {
System.out.println("IPv6 and IPv4 loopback addresses map to the"
+ " same hostname. Skipping test case.");
return;
}
if (!InetAddress.getByName(ipv6Hostname).getHostAddress()
.equals(ipv6HostAddress)) {
System.out.println(ipv6Hostname + " resolves to \""
+ InetAddress.getByName(ipv6Hostname).getHostAddress()
+ "\", not \"" + ipv6HostAddress +
"\". Skipping test case.");
return;
}
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,
socks.getPort()));
URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());

View File

@ -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
@ -491,6 +491,25 @@ public class SocksServer extends Thread implements Closeable {
}
}
public SocksServer(InetAddress addr, int port, boolean useV4) throws IOException {
this.port = port;
this.useV4 = useV4;
server = new ServerSocket();
if (port == 0 && addr == null) {
server.bind(null);
this.port = server.getLocalPort();
} else if (port == 0 && addr != null) {
server.bind(new InetSocketAddress(addr, 0));
this.port = server.getLocalPort();
} else if (addr == null) {
assert port != 0;
server.bind(new InetSocketAddress(port));
} else {
assert port != 0;
server.bind(new InetSocketAddress(addr, port));
}
}
public SocksServer() throws IOException {
this (DEFAULT_PORT);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@ -49,7 +49,7 @@ public class PostOnDelete {
try {
s = new Server();
s.startServer();
URL url = new URL("http://localhost:" + s.getPort());
URL url = new URL("http://" + s.getAuthority());
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("DELETE");
urlConnection.setDoOutput(true);
@ -70,7 +70,8 @@ public class PostOnDelete {
HttpServer server;
public void startServer() {
InetSocketAddress addr = new InetSocketAddress(0);
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress(loopback,0);
try {
server = HttpServer.create(addr, 0);
} catch (IOException ioe) {
@ -81,6 +82,12 @@ public class PostOnDelete {
server.start();
}
public String getAuthority() {
String address = server.getAddress().getHostString();
address = (address.indexOf(':') >= 0) ? ("[" + address + "]") : address;
return address + ":" + getPort();
}
public int getPort() {
return server.getAddress().getPort();
}