7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate

Reviewed-by: alanb
This commit is contained in:
Chris Hegarty 2012-03-13 09:33:50 +00:00
parent bfbf85b7a7
commit 015d7657e7
2 changed files with 33 additions and 24 deletions
jdk/test/java/net/Socks

@ -22,6 +22,7 @@
*/ */
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import java.util.HashMap;
public class SocksServer extends Thread { public class SocksServer extends Thread {
// Some useful SOCKS constant // Some useful SOCKS constant
@ -56,8 +57,8 @@ public class SocksServer extends Thread {
private int port; private int port;
private ServerSocket server; private ServerSocket server;
private boolean useV4 = false; private boolean useV4 = false;
private java.util.Hashtable users = new java.util.Hashtable(); private HashMap<String,String> users = new HashMap<>();
private boolean done = false; private volatile boolean done = false;
// Inner class to handle protocol with client // Inner class to handle protocol with client
// This is the bulk of the work (protocol handler) // This is the bulk of the work (protocol handler)
class ClientHandler extends Thread { class ClientHandler extends Thread {
@ -136,7 +137,7 @@ public class SocksServer extends Thread {
System.err.println("User: '" + uname); System.err.println("User: '" + uname);
System.err.println("PSWD: '" + password); System.err.println("PSWD: '" + password);
if (users.containsKey(uname)) { if (users.containsKey(uname)) {
String p1 = (String) users.get(uname); String p1 = users.get(uname);
System.err.println("p1 = " + p1); System.err.println("p1 = " + p1);
if (p1.equals(password)) { if (p1.equals(password)) {
out.write(PROTO_VERS); out.write(PROTO_VERS);
@ -492,8 +493,13 @@ public class SocksServer extends Thread {
public SocksServer(int port) throws IOException { public SocksServer(int port) throws IOException {
this.port = port; this.port = port;
server = new ServerSocket(); server = new ServerSocket();
if (port == 0) {
server.bind(null);
this.port = server.getLocalPort();
} else {
server.bind(new InetSocketAddress(port)); server.bind(new InetSocketAddress(port));
} }
}
public SocksServer() throws IOException { public SocksServer() throws IOException {
this (DEFAULT_PORT); this (DEFAULT_PORT);
@ -503,8 +509,13 @@ public class SocksServer extends Thread {
users.put(user, passwd); users.put(user, passwd);
} }
public synchronized void terminate() { public int getPort() {
return port;
}
public void terminate() {
done = true; done = true;
try { server.close(); } catch (IOException unused) {}
} }
public void run() { public void run() {

@ -26,22 +26,21 @@
* @bug 4727547 * @bug 4727547
* @summary SocksSocketImpl throws NullPointerException * @summary SocksSocketImpl throws NullPointerException
* @build SocksServer * @build SocksServer
* @run main SocksV4Test
*/ */
import java.net.*; import java.net.*;
import java.io.*;
public class SocksV4Test { public class SocksV4Test {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws Exception {
// Create a SOCKS V4 proxy on port 8888 // Create a SOCKS V4 proxy
SocksServer srvr = new SocksServer(8888, true); SocksServer srvr = new SocksServer(0, true);
srvr.start(); srvr.start();
System.setProperty("socksProxyHost", "localhost"); Proxy sp = new Proxy(Proxy.Type.SOCKS,
System.setProperty("socksProxyPort", "8888"); new InetSocketAddress("localhost", srvr.getPort()));
// Let's create an unresolved address // Let's create an unresolved address
InetSocketAddress ad = new InetSocketAddress("doesnt.exist.name", 1234); InetSocketAddress ad = new InetSocketAddress("doesnt.exist.name", 1234);
Socket s = new Socket(); try (Socket s = new Socket(sp)) {
try {
s.connect(ad, 10000); s.connect(ad, 10000);
} catch (UnknownHostException ex) { } catch (UnknownHostException ex) {
// OK, that's what we expected // OK, that's what we expected
@ -50,7 +49,6 @@ public class SocksV4Test {
throw new RuntimeException("Got a NUllPointerException"); throw new RuntimeException("Got a NUllPointerException");
} finally { } finally {
srvr.terminate(); srvr.terminate();
srvr.interrupt();
} }
} }
} }