You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2008/01/24 17:56:16 UTC
svn commit: r614926 [2/2] -
/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java?rev=614926&r1=614925&r2=614926&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java Thu Jan 24 08:56:14 2008
@@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.InputStream;
-import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Inet4Address;
@@ -40,89 +39,32 @@
import tests.support.Support_PortManager;
public class SocketTest extends SocketTestCase {
- ServerSocket ss;
-
- Socket client;
-
- Thread t;
-
- boolean interrupted;
-
- String host = "localhost";
-
- int port;
-
- Exception failureException;
-
- private class SServer2 extends Thread implements Runnable {
- private Socket s1;
+ private class ClientThread implements Runnable {
public void run() {
try {
- ss.setSoTimeout(5000);
- s1 = ss.accept();
- ss.close();
- Thread.sleep(4000);
- } catch (InterruptedIOException x) {
- System.out.println(Thread.currentThread()
- + ", accept() timeout fired: " + x);
- } catch (InterruptedException x) {
- } catch (Exception e) {
- System.out.println("Unable to accept: " + e.toString());
- } finally {
- try {
- if (s1 != null)
- s1.close();
- } catch (IOException e) {
- }
- }
- }
- }
-
- private static class SServer extends Thread {
-
- final private ServerSocket server;
-
- public SServer(ServerSocket server) {
- super();
- this.server = server;
- }
+ Socket socket = new Socket();
+ InetSocketAddress addr = new InetSocketAddress(host, port);
+ socket.connect(addr);
- public void run() {
- Socket worker = null;
- try {
- server.setSoTimeout(5000);
- worker = server.accept();
- server.close();
- Thread.sleep(4000);
- } catch (InterruptedIOException x) {
- System.out.println(Thread.currentThread()
- + ", accept() timeout fired: " + x);
- } catch (InterruptedException x) {
- } catch (Exception e) {
- System.out.println("Unable to accept: " + e.toString());
- } finally {
- try {
- if (worker != null)
- worker.close();
- } catch (IOException e) {
- // Ignored
- }
+ socket.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
}
}
private class ServerThread implements Runnable {
- public boolean ready = false;
-
- private int serverSocketConstructor = 0;
-
private static final int FIRST_TIME = 1;
private static final int SECOND_TIME = 2;
private int backlog = 10;
+ public boolean ready = false;
+
+ private int serverSocketConstructor = 0;
+
public void run() {
try {
@@ -166,20 +108,12 @@
}
}
- private class ClientThread implements Runnable {
+ boolean interrupted;
- public void run() {
- try {
- Socket socket = new Socket();
- InetSocketAddress addr = new InetSocketAddress(host, port);
- socket.connect(addr);
+ String host = "localhost";
+ int port;
- socket.close();
- } catch (Exception e) {
- failureException = e;
- }
- }
- }
+ Thread t;
private void connectTestImpl(int ssConsType) throws Exception {
ServerThread server = new ServerThread();
@@ -199,220 +133,124 @@
}
}
- /**
- * @tests java.net.Socket#Socket()
- */
- public void test_Constructor() {
- // create the socket and then validate some basic state
- Socket s = new Socket();
- assertFalse("new socket should not be connected", s.isConnected());
- assertFalse("new socket should not be bound", s.isBound());
- assertFalse("new socket should not be closed", s.isClosed());
- assertFalse("new socket should not be in InputShutdown", s
- .isInputShutdown());
- assertFalse("new socket should not be in OutputShutdown", s
- .isOutputShutdown());
- }
-
- /**
- * @tests java.net.Socket#Socket(java.lang.String, int)
- */
- public void test_ConstructorLjava_lang_StringI() throws IOException {
- int sport = startServer("Cons String,I");
- Socket client = new Socket(InetAddress.getLocalHost().getHostName(),
- sport);
- assertEquals("Failed to create socket", sport, client.getPort());
-
- // regression for HARMONY-946
- ServerSocket ss = null;
- Socket s = null;
+ protected void tearDown() {
try {
- ss = new ServerSocket(0);
- s = new Socket("0.0.0.0 ", ss.getLocalPort());
- } finally {
- try {
- ss.close();
- } catch (Exception e) {
- // ignore
- }
- try {
- s.close();
- } catch (Exception e) {
- // ignore
+ if (t != null) {
+ t.interrupt();
}
+ } catch (Exception e) {
}
+ this.t = null;
+ this.interrupted = false;
}
/**
- * @tests java.net.Socket#Socket(java.lang.String, int,
- * java.net.InetAddress, int)
+ * @tests java.net.Socket#bind(java.net.SocketAddress)
*/
- public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI()
- throws IOException {
- // Test for method java.net.Socket(java.lang.String, int,
- // java.net.InetAddress, int)
- int sport = startServer("Cons String,I,InetAddress,I");
- int portNumber = Support_PortManager.getNextPort();
- client = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertTrue("Failed to create socket", client.getPort() == sport);
-
- if (("true".equals(System.getProperty("java.net.preferIPv6Addresses")))
- && !("true".equals(System
- .getProperty("java.net.preferIPv4Stack")))) {
-
- // ALTERNATE IPv6 TEST
- if ("true".equals(System.getProperty("run.ipv6tests"))) {
- System.out
- .println("Running testConstructorLjava_lang_StringILjava_net_InetAddressI(SocketTest) with IPv6GlobalAddressJcl4: "
- + Support_Configuration.IPv6GlobalAddressJcl4);
- int testPort = Support_PortManager.getNextPort();
- Socket s1 = null, s2 = null;
- try {
- s1 = new Socket(
- Support_Configuration.IPv6GlobalAddressJcl4, 80,
- InetAddress.getLocalHost(), testPort);
- } catch (IOException e) {
- // check here if InetAddress.getLocalHost() is returning the
- // loopback address.
- // if so that is likely the cause of the failure
- String warning = "";
- try {
- InetAddress returnedLocalHost = InetAddress
- .getLocalHost();
- // don't use isLoopbackAddress for some configurations
- // as they do not have it
- if (returnedLocalHost.isLoopbackAddress()) {
- warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE";
-
- }
- } catch (Exception ex) {
- warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
- }
+ public void test_bindLjava_net_SocketAddress() throws IOException {
- fail("Exception creating 1st socket" + warning + ": " + e);
- }
- boolean exception = false;
- try {
- s2 = new Socket(
- Support_Configuration.IPv6GlobalAddressJcl4, 80,
- InetAddress.getLocalHost(), testPort);
- } catch (IOException e) {
- exception = true;
- }
- try {
- s1.close();
- if (!exception)
- s2.close();
- } catch (IOException e) {
- }
- assertTrue("Was able to create two sockets on same port",
- exception);
+ @SuppressWarnings("serial")
+ class UnsupportedSocketAddress extends SocketAddress {
+ public UnsupportedSocketAddress() {
}
+ }
- } else {
- int testPort = Support_PortManager.getNextPort();
- Socket s1 = null, s2 = null;
- int serverPort = ss.getLocalPort();
- try {
- s1 = new Socket("127.0.0.1", serverPort, InetAddress
- .getLocalHost(), testPort);
- } catch (IOException e) {
- e.printStackTrace();
+ // Address we cannot bind to
+ Socket theSocket = new Socket();
+ InetSocketAddress bogusAddress = new InetSocketAddress(InetAddress
+ .getByAddress(Support_Configuration.nonLocalAddressBytes), 42);
+ try {
+ theSocket.bind(bogusAddress);
+ fail("No exception when binding to bad address");
+ } catch (IOException ex) {
+ // Expected
+ }
+ theSocket.close();
- // check here if InetAddress.getLocalHost() is returning the
- // loopback address.
- // if so that is likely the cause of the failure
- String warning = "";
- try {
- InetAddress returnedLocalHost = InetAddress.getLocalHost();
- // don't use isLoopbackAddress for some configurations as
- // they do not have it
- if (returnedLocalHost.isLoopbackAddress()) {
- warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE";
-
- }
- } catch (Exception ex) {
- warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
- }
+ // Now create a socket that is not bound and then bind it
+ theSocket = new Socket();
+ theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
+ int portNumber = theSocket.getLocalPort();
- fail("Exception creating 1st socket" + warning + ": " + e);
- }
- boolean exception = false;
- try {
- s2 = new Socket("127.0.0.1", serverPort, InetAddress
- .getLocalHost(), testPort);
- } catch (IOException e) {
- exception = true;
- }
- try {
- s1.close();
- if (!exception)
- s2.close();
- } catch (IOException e) {
- }
- assertTrue("Was able to create two sockets on same port", exception);
- }
- }
+ // Validate that the localSocketAddress reflects the address we
+ // bound to
+ assertEquals("Local address not correct after bind",
+ new InetSocketAddress(InetAddress.getLocalHost(), portNumber),
+ theSocket.getLocalSocketAddress());
- /**
- * @tests java.net.Socket#Socket(java.lang.String, int, boolean)
- */
- @SuppressWarnings("deprecation")
- public void test_ConstructorLjava_lang_StringIZ() throws IOException {
- ServerSocket server = new ServerSocket(0);
+ // Make sure we can now connect and that connections appear to come
+ // from the address we bound to.
+ InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), 0);
+ ServerSocket server = new ServerSocket();
+ server.bind(theAddress);
int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost().getHostAddress(),
- sport, true);
+ InetSocketAddress boundAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), sport);
- assertEquals("Failed to create socket", sport, client.getPort());
- client.close();
-
- client = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- false);
- client.close();
+ theSocket.connect(boundAddress);
+ Socket worker = server.accept();
+ assertEquals(
+ "Returned Remote address from server connected to does not match expected local address",
+ new InetSocketAddress(InetAddress.getLocalHost(), portNumber),
+ worker.getRemoteSocketAddress());
+ theSocket.close();
+ worker.close();
server.close();
- }
- /**
- * @tests java.net.Socket#Socket(java.net.InetAddress, int)
- */
- public void test_ConstructorLjava_net_InetAddressI() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ // Validate if we pass in null that it picks an address for us and
+ // all is ok
+ theSocket = new Socket();
+ theSocket.bind(null);
+ assertNotNull("Bind with null did not work", theSocket
+ .getLocalSocketAddress());
+ theSocket.close();
- assertEquals("Failed to create socket", sport, client.getPort());
+ // now check the error conditions
- client.close();
- server.close();
- }
+ // Address that we have already bound to
+ theSocket = new Socket();
+ theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
+ theSocket.bind(theAddress);
- /**
- * @tests java.net.Socket#Socket(java.net.InetAddress, int,
- * java.net.InetAddress, int)
- */
- public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI()
- throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport,
- InetAddress.getLocalHost(), 0);
- assertNotSame("Failed to create socket", 0, client.getLocalPort());
+ Socket theSocket2 = new Socket();
+ try {
+ theSocket2.bind(theSocket.getLocalSocketAddress());
+ fail("No exception binding to address that is not available");
+ } catch (IOException ex) {
+ // Expected
+ }
+ theSocket.close();
+ theSocket2.close();
+
+ // Unsupported SocketAddress subclass
+ theSocket = new Socket();
+ try {
+ theSocket.bind(new UnsupportedSocketAddress());
+ fail("No exception when binding using unsupported SocketAddress subclass");
+ } catch (IllegalArgumentException ex) {
+ // Expected
+ }
+ theSocket.close();
}
/**
- * @tests java.net.Socket#Socket(java.net.InetAddress, int, boolean)
+ * @tests java.net.Socket#bind(java.net.SocketAddress)
*/
- @SuppressWarnings("deprecation")
- public void test_ConstructorLjava_net_InetAddressIZ() throws IOException {
- // Test for method java.net.Socket(java.net.InetAddress, int, boolean)
- int sport = startServer("Cons InetAddress,I,Z");
- client = new Socket(InetAddress.getLocalHost(), sport, true);
- assertTrue("Failed to create socket", client.getPort() == sport);
+ public void test_bindLjava_net_SocketAddress_Proxy() throws IOException {
+ // The Proxy will not impact on the bind operation. It can be assigned
+ // with any address.
+ Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(
+ "127.0.0.1", 0));
+ Socket socket = new Socket(proxy);
- client = new Socket(InetAddress.getLocalHost(), sport, false);
+ InetAddress address = InetAddress.getByName("localhost");
+ socket.bind(new InetSocketAddress(address, 0));
+ assertEquals(address, socket.getLocalAddress());
+ assertTrue(0 != socket.getLocalPort());
+
+ socket.close();
}
/**
@@ -420,8 +258,8 @@
*/
public void test_close() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
try {
client.setSoLinger(false, 100);
@@ -441,570 +279,665 @@
}
/**
- * @tests java.net.Socket#getInetAddress()
+ * @tests Socket#connect(SocketAddress) try an unknownhost
*/
- public void test_getInetAddress() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
-
- assertTrue("Returned incorrect InetAdrees", client.getInetAddress()
- .equals(InetAddress.getLocalHost()));
-
- client.close();
- server.close();
+ public void test_connect_unknownhost() throws Exception {
+ Socket socket = new Socket();
+ try {
+ socket.connect(new InetSocketAddress("unknownhost.invalid", 12345));
+ fail("Should throw UnknownHostException");
+ } catch (UnknownHostException e) {
+ // expected
+ }
}
/**
- * @tests java.net.Socket#getInputStream()
+ * @tests Socket#connect(SocketAddress)
*/
- public void test_getInputStream() throws IOException {
- // Test for method java.io.InputStream java.net.Socket.getInputStream()
- int sport = startServer("SServer getInputStream");
- int portNumber = Support_PortManager.getNextPort();
- Socket s = new Socket(InetAddress.getLocalHost(), sport, null,
- portNumber);
- (t = new SServer2()).start();
- java.io.InputStream is = s.getInputStream();
- assertNotNull("Failed to get stream", is);
- s.setSoTimeout(6000);
- is.read();
- s.close();
- assertEquals("Invalid after close", -1, is.read());
+ public void test_connect_unresolved() throws IOException {
+ Socket socket = new Socket();
- interrupted = false;
- int portNum = Support_PortManager.getNextPort();
- final ServerSocket ss = new ServerSocket(portNum);
- Socket sock = new Socket(InetAddress.getLocalHost(), portNum);
- Runnable runnable = new Runnable() {
- public void run() {
- try {
- Socket as = ss.accept();
- ss.close();
- as.setSoTimeout(12000);
- InputStream in = as.getInputStream();
- in.read();
- in.close();
- } catch (InterruptedIOException e) {
- interrupted = true;
- } catch (IOException e) {
- }
- }
- };
- Thread thread = new Thread(runnable, "Socket.getInputStream");
- thread.start();
+ // Try a known host created by createUnresolved()
try {
- do {
- Thread.sleep(200);
- } while (!thread.isAlive());
- } catch (InterruptedException e) {
+ socket.connect(InetSocketAddress.createUnresolved("www.apache.org",
+ 80));
+ fail("Should throw UnknownHostException");
+ } catch (UnknownHostException e) {
+ // expected
}
+
+ // Try an unknown host created by createUnresolved()
try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
+ socket.connect(InetSocketAddress.createUnresolved(
+ "unknownhost.invalid", 12345));
+ fail("Should throw UnknownHostException");
+ } catch (UnknownHostException e) {
+ // expected
}
- sock.close();
- int c = 0;
- do {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- if (interrupted) {
- fail("read interrupted");
- }
- if (++c > 4) {
- fail("read call did not exit");
- }
- } while (thread.isAlive());
}
/**
- * @tests java.net.Socket#getKeepAlive()
+ * @tests java.net.Socket#connect(java.net.SocketAddress)
*/
- public void test_getKeepAlive() {
- try {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport, null,
- 0);
+ public void test_connectLjava_net_SocketAddress() throws Exception {
- client.setKeepAlive(true);
- assertTrue("getKeepAlive false when it should be true", client
- .getKeepAlive());
+ @SuppressWarnings("serial")
+ class UnsupportedSocketAddress extends SocketAddress {
+ public UnsupportedSocketAddress() {
+ }
+ }
- client.setKeepAlive(false);
- assertFalse("getKeepAlive true when it should be False", client
- .getKeepAlive());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
- } catch (Exception e) {
- handleException(e, SO_KEEPALIVE);
+ Socket theSocket = new Socket();
+ try {
+ theSocket.connect(null);
+ fail("No exception for null arg");
+ } catch (IllegalArgumentException e) {
+ // Expected
}
- }
- /**
- * @tests java.net.Socket#getLocalAddress()
- */
- public void test_getLocalAddress() throws IOException {
+ try {
+ theSocket.connect(new UnsupportedSocketAddress());
+ fail("No exception for invalid socket address");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+
+ try {
+ theSocket.connect(new InetSocketAddress(InetAddress
+ .getByAddress(new byte[] { 0, 0, 0, 0 }), 42));
+ fail("No exception with non-connectable address");
+ } catch (ConnectException e) {
+ // Expected
+ }
+
+ // now validate that we get a connect exception if we try to connect to
+ // an address on which nobody is listening
+ theSocket = new Socket();
+ try {
+ theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
+ 0));
+ fail("No exception when connecting to address nobody listening on");
+ } catch (ConnectException e) {
+ // Expected
+ }
+
+ // Now validate that we can actually connect when somebody is listening
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ InetSocketAddress boundAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), server.getLocalPort());
+ Socket client = new Socket();
+ client.connect(boundAddress);
- assertTrue("Returned incorrect InetAddress", client.getLocalAddress()
- .equals(InetAddress.getLocalHost()));
+ // validate that when a socket is connected that it answers
+ // correctly to related queries
+ assertTrue("Wrong connected status", client.isConnected());
+ assertFalse("Wrong closed status", client.isClosed());
+ assertTrue("Wrong bound status", client.isBound());
+ assertFalse("Wrong input shutdown status", client.isInputShutdown());
+ assertFalse("Wrong output shutdown status", client.isOutputShutdown());
+ assertTrue("Local port was 0", client.getLocalPort() != 0);
- // now validate that behaviour when the any address is returned
- String preferIPv4StackValue = System
- .getProperty("java.net.preferIPv4Stack");
- String preferIPv6AddressesValue = System
- .getProperty("java.net.preferIPv6Addresses");
+ client.close();
+ server.close();
+ // Now validate that we get the right exception if we connect when we
+ // are already connected
+ server = new ServerSocket(0);
+ boundAddress = new InetSocketAddress(InetAddress.getLocalHost(), server
+ .getLocalPort());
client = new Socket();
- client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
+ client.connect(boundAddress);
- if (((preferIPv4StackValue == null) || preferIPv4StackValue
- .equalsIgnoreCase("false"))
- && (preferIPv6AddressesValue != null)
- && (preferIPv6AddressesValue.equals("true"))) {
- assertTrue(
- "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false "
- + client.getLocalSocketAddress(), client
- .getLocalAddress() instanceof Inet6Address);
- } else {
- assertTrue(
- "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true "
- + client.getLocalSocketAddress(), client
- .getLocalAddress() instanceof Inet4Address);
+ try {
+ client.connect(boundAddress);
+ fail("No exception when we try to connect on a connected socket: ");
+ } catch (SocketException e) {
+ // Expected
}
client.close();
server.close();
}
/**
- * @tests java.net.Socket#getLocalPort()
+ * Regression for Harmony-2503
*/
- public void test_getLocalPort() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
-
- assertNotSame("Returned incorrect port", 0, client.getLocalPort());
-
- client.close();
- server.close();
+ public void test_connectLjava_net_SocketAddress_AnyAddress()
+ throws Exception {
+ connectTestImpl(ServerThread.FIRST_TIME);
+ connectTestImpl(ServerThread.SECOND_TIME);
}
/**
- * @tests java.net.Socket#getOutputStream()
+ * @tests java.net.Socket#connect(java.net.SocketAddress, int)
*/
- @SuppressWarnings("deprecation")
- public void test_getOutputStream() throws IOException {
- int sport = startServer("SServer getOutputStream");
- int portNumber = Support_PortManager.getNextPort();
- client = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- java.io.OutputStream os = client.getOutputStream();
- assertNotNull("Failed to get stream", os);
- tearDown();
+ public void test_connectLjava_net_SocketAddressI() throws Exception {
- int portNum = Support_PortManager.getNextPort();
- final ServerSocket ss = new ServerSocket(portNum);
- Socket sock = new Socket(InetAddress.getLocalHost(), portNum);
- Runnable runnable = new Runnable() {
- public void run() {
- try {
- Socket as = ss.accept();
- ss.close();
- InputStream in = as.getInputStream();
- in.read();
- in.close();
- } catch (IOException e) {
- System.out.println(Thread.currentThread() + ": " + e);
- }
- }
- };
- Thread thread = new Thread(runnable, "Socket.getOutputStream");
- thread.start();
- int c = 0;
- do {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- if (++c > 4)
- fail("thread is not alive");
- } while (!thread.isAlive());
- OutputStream out = sock.getOutputStream();
- byte[] data = new byte[256];
- out.write(data);
- c = 0;
- do {
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- if (++c > 4) {
- fail("read call did not exit");
+ @SuppressWarnings("serial")
+ class UnsupportedSocketAddress extends SocketAddress {
+ public UnsupportedSocketAddress() {
}
- } while (thread.isAlive());
+ }
- boolean exception = false;
+ // Start by validating the error checks
+ Socket theSocket = new Socket();
try {
- for (int i = 0; i < 400; i++)
- out.write(data);
- } catch (IOException e) {
- exception = true;
+ theSocket.connect(new InetSocketAddress(0), -100);
+ fail("No exception for negative timeout");
+ } catch (IllegalArgumentException e) {
+ // Expected
}
- out.close();
- assertTrue("write to closed socket did not cause exception", exception);
- // Regression test for harmony-2934
- client = new Socket("127.0.0.1", Support_PortManager
- .getNextPortForUDP(), false);
- OutputStream o = client.getOutputStream();
- o.write(1);
+ try {
+ theSocket.connect(null, 0);
+ fail("No exception for null address");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
- // Regression test for harmony-2942
- client = new Socket("0.0.0.0", Support_PortManager.getNextPortForUDP(),
- false);
- o = client.getOutputStream();
- o.write(1);
- }
+ try {
+ theSocket.connect(new UnsupportedSocketAddress(), 1000);
+ fail("No exception for invalid socket address type");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
- /**
- * @tests java.net.Socket#getPort()
- */
- public void test_getPort() throws IOException {
+ SocketAddress nonConnectableAddress = new InetSocketAddress(InetAddress
+ .getByAddress(new byte[] { 0, 0, 0, 0 }), 0);
+ try {
+ theSocket.connect(nonConnectableAddress, 1000);
+ fail("No exception when non Connectable Address passed in: ");
+ } catch (SocketException e) {
+ // Expected
+ }
+
+ // Now validate that we get a connect exception if we try to connect to
+ // an address on which nobody is listening
+ theSocket = new Socket();
+ try {
+ theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
+ 0), 0);
+ fail("No exception when connecting to address nobody listening on");
+ } catch (ConnectException e) {
+ // Expected
+ }
+ theSocket.close();
+
+ // Now validate that we can actually connect when somebody is listening
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ InetSocketAddress boundAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), server.getLocalPort());
+ Socket client = new Socket();
+ client.connect(boundAddress, 0);
- assertEquals("Returned incorrect port", sport, client.getPort());
+ // Validate that when a socket is connected that it answers
+ // correctly to related queries
+ assertTrue("Wrong connected status", client.isConnected());
+ assertFalse("Wrong closed status", client.isClosed());
+ assertTrue("Wrong bound status", client.isBound());
+ assertFalse("Wrong input shutdown status", client.isInputShutdown());
+ assertFalse("Wrong output shutdown status", client.isOutputShutdown());
+ assertTrue("Local port was 0", client.getLocalPort() != 0);
client.close();
server.close();
- }
- /**
- * @tests java.net.Socket#getSoLinger()
- */
- public void test_getSoLinger() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ // Now validate that we get a connect exception if we try to connect to
+ // an address on which nobody is listening
+ theSocket = new Socket();
+ SocketAddress nonListeningAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), 42);
+ try {
+ theSocket.connect(nonListeningAddress, 1000);
+ fail("No exception when connecting to address nobody listening on");
+ } catch (ConnectException e) {
+ // Expected
+ }
+ theSocket.close();
+ // Now validate that we get a interrupted exception if we try to connect
+ // to an address on which nobody is accepting connections and the
+ // timeout expired
+ theSocket = new Socket();
try {
- client.setSoLinger(true, 200);
- assertEquals("Returned incorrect linger", 200, client.getSoLinger());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
- client.setSoLinger(false, 0);
- } catch (Exception e) {
- handleException(e, SO_LINGER);
- } finally {
- client.close();
- server.close();
+ theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
+ 1), 200);
+ fail("No interrupted exception when connecting to address nobody listening on with short timeout 200");
+ } catch (SocketTimeoutException e) {
+ // Expected
}
- }
+ theSocket.close();
- /**
- * @tests java.net.Socket#getReceiveBufferSize()
- */
- public void test_getReceiveBufferSize() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ // Now validate that we get the right exception if we connect when we
+ // are already connected
+ server = new ServerSocket(0);
+ boundAddress = new InetSocketAddress(InetAddress.getLocalHost(), server
+ .getLocalPort());
+ client = new Socket();
+ client.connect(boundAddress, 10000);
try {
- client.setReceiveBufferSize(130);
- assertTrue("Incorrect buffer size",
- client.getReceiveBufferSize() >= 130);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
- } catch (Exception e) {
- handleException(e, SO_RCVBUF);
- } finally {
- client.close();
- server.close();
+ client.connect(boundAddress, 10000);
+ fail("No exception when we try to connect on a connected socket: ");
+ } catch (SocketException e) {
+ // Expected
}
+ client.close();
+ server.close();
}
/**
- * @tests java.net.Socket#getSendBufferSize()
+ * @tests java.net.Socket#Socket()
*/
- public void test_getSendBufferSize() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
-
- try {
- client.setSendBufferSize(134);
- assertTrue("Incorrect buffer size",
- client.getSendBufferSize() >= 134);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
- } catch (Exception e) {
- handleException(e, SO_SNDBUF);
- } finally {
- client.close();
- server.close();
- }
+ public void test_Constructor() {
+ // create the socket and then validate some basic state
+ Socket s = new Socket();
+ assertFalse("new socket should not be connected", s.isConnected());
+ assertFalse("new socket should not be bound", s.isBound());
+ assertFalse("new socket should not be closed", s.isClosed());
+ assertFalse("new socket should not be in InputShutdown", s
+ .isInputShutdown());
+ assertFalse("new socket should not be in OutputShutdown", s
+ .isOutputShutdown());
}
/**
- * @tests java.net.Socket#getSoTimeout()
+ * @tests java.net.Socket#Socket(java.lang.String, int)
*/
- public void test_getSoTimeout() throws IOException {
+ public void test_ConstructorLjava_lang_StringI() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
- try {
- client.setSoTimeout(100);
- assertEquals("Returned incorrect sotimeout", 100, client
- .getSoTimeout());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
- } catch (Exception e) {
- handleException(e, SO_TIMEOUT);
- } finally {
- client.close();
- server.close();
- }
+ assertEquals("Failed to create socket", server.getLocalPort(), client
+ .getPort());
+
+ // Regression for HARMONY-946
+ ServerSocket ss = new ServerSocket(0);
+ Socket s = new Socket("0.0.0.0 ", ss.getLocalPort());
+ ss.close();
+ s.close();
}
/**
- * @tests java.net.Socket#getTcpNoDelay()
- */
- public void test_getTcpNoDelay() throws IOException {
+ * @tests java.net.Socket#Socket(java.lang.String, int,
+ * java.net.InetAddress, int)
+ */
+ public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI()
+ throws IOException {
+
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ int serverPort = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost().getHostName(),
+ serverPort, InetAddress.getLocalHost(), 0);
+ assertTrue("Failed to create socket", client.getPort() == serverPort);
+ client.close();
+ Socket theSocket = null;
try {
- boolean bool = !client.getTcpNoDelay();
- client.setTcpNoDelay(bool);
- assertTrue("Failed to get no delay setting: "
- + client.getTcpNoDelay(), client.getTcpNoDelay() == bool);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
- } catch (Exception e) {
- handleException(e, TCP_NODELAY);
- } finally {
- client.close();
- server.close();
+ theSocket = new Socket("127.0.0.1", serverPort, InetAddress
+ .getLocalHost(), 0);
+ } catch (IOException e) {
+ // check here if InetAddress.getLocalHost() is returning the
+ // loopback address, if so that is likely the cause of the failure
+ assertFalse(
+ "Misconfiguration - local host is the loopback address",
+ InetAddress.getLocalHost().isLoopbackAddress());
+ throw e;
+ }
+
+ assertTrue(theSocket.isConnected());
+
+ try {
+ new Socket("127.0.0.1", serverPort, theSocket.getLocalAddress(),
+ theSocket.getLocalPort());
+ fail("Was able to create two sockets on same port");
+ } catch (IOException e) {
+ // Expected
}
+
+ theSocket.close();
+ server.close();
}
/**
- * @tests java.net.Socket#setKeepAlive(boolean)
+ * @tests java.net.Socket#Socket(java.lang.String, int,
+ * java.net.InetAddress, int)
*/
- public void test_setKeepAliveZ() throws IOException {
+ public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI_ipv6()
+ throws IOException {
- class TestSocket extends Socket {
- public TestSocket(SocketImpl impl) throws SocketException {
- super(impl);
- }
+ boolean preferIPv6 = "true".equals(System
+ .getProperty("java.net.preferIPv6Addresses"));
+ boolean preferIPv4 = "true".equals(System
+ .getProperty("java.net.preferIPv4Stack"));
+ boolean runIPv6 = "true".equals(System.getProperty("run.ipv6tests"));
+
+ if (!runIPv6 || !preferIPv6 || preferIPv4) {
+ // This test is not for me
+ return;
}
- // There is not really a good test for this as it is there to detect
- // crashed machines. Just make sure we can set it
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ int serverPort = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost().getHostName(),
+ serverPort, InetAddress.getLocalHost(), 0);
+ assertTrue("Failed to create socket", client.getPort() == serverPort);
+ client.close();
+ Socket theSocket = null;
try {
- client.setKeepAlive(true);
- client.setKeepAlive(false);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
- } catch (Exception e) {
- handleException(e, SO_KEEPALIVE);
- } finally {
- client.close();
- server.close();
+ theSocket = new Socket(Support_Configuration.IPv6GlobalAddressJcl4,
+ serverPort, InetAddress.getLocalHost(), 0);
+ } catch (IOException e) {
+ // check here if InetAddress.getLocalHost() is returning the
+ // loopback address, if so that is likely the cause of the failure
+ assertFalse(
+ "Misconfiguration - local host is the loopback address",
+ InetAddress.getLocalHost().isLoopbackAddress());
+ throw e;
}
- // Regression test for HARMONY-1136
- new TestSocket((SocketImpl) null).setKeepAlive(true);
+ assertTrue(theSocket.isConnected());
+
+ try {
+ new Socket(Support_Configuration.IPv6GlobalAddressJcl4, serverPort,
+ theSocket.getLocalAddress(), theSocket.getLocalPort());
+ fail("Was able to create two sockets on same port");
+ } catch (IOException e) {
+ // Expected
+ }
+
+ theSocket.close();
+ server.close();
}
/**
- * @tests java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
+ * @tests java.net.Socket#Socket(java.lang.String, int, boolean)
*/
- public void test_setSocketImplFactoryLjava_net_SocketImplFactory() {
- // Cannot test as setting will cause the factory to be changed for
- // all subsequent sockets
+ @SuppressWarnings("deprecation")
+ public void test_ConstructorLjava_lang_StringIZ() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ int serverPort = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost().getHostAddress(),
+ serverPort, true);
+
+ assertEquals("Failed to create socket", serverPort, client.getPort());
+ client.close();
+
+ client = new Socket(InetAddress.getLocalHost().getHostName(),
+ serverPort, false);
+ client.close();
+ server.close();
}
/**
- * @tests java.net.Socket#setSendBufferSize(int)
+ * @tests java.net.Socket#Socket(java.net.InetAddress, int)
*/
- public void test_setSendBufferSizeI() throws IOException {
+ public void test_ConstructorLjava_net_InetAddressI() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
- try {
- client.setSendBufferSize(134);
- assertTrue("Incorrect buffer size",
- client.getSendBufferSize() >= 134);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
- } catch (Exception e) {
- handleException(e, SO_SNDBUF);
- } finally {
- client.close();
- server.close();
- }
+ assertEquals("Failed to create socket", server.getLocalPort(), client
+ .getPort());
+
+ client.close();
+ server.close();
}
/**
- * @tests java.net.Socket#setReceiveBufferSize(int)
+ * @tests java.net.Socket#Socket(java.net.InetAddress, int,
+ * java.net.InetAddress, int)
*/
- public void test_setReceiveBufferSizeI() throws IOException {
+ public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI()
+ throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
-
- try {
- client.setReceiveBufferSize(130);
- assertTrue("Incorrect buffer size",
- client.getReceiveBufferSize() >= 130);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
- } catch (Exception e) {
- handleException(e, SO_RCVBUF);
- } finally {
- client.close();
- server.close();
- }
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort(), InetAddress.getLocalHost(), 0);
+ assertNotSame("Failed to create socket", 0, client.getLocalPort());
}
/**
- * @tests java.net.Socket#setSoLinger(boolean, int)
+ * @tests java.net.Socket#Socket(java.net.InetAddress, int, boolean)
*/
- public void test_setSoLingerZI() throws IOException {
+ @SuppressWarnings("deprecation")
+ public void test_ConstructorLjava_net_InetAddressIZ() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ int serverPort = server.getLocalPort();
- try {
- client.setSoLinger(true, 500);
- assertEquals("Set incorrect linger", 500, client.getSoLinger());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
- client.setSoLinger(false, 0);
- } catch (Exception e) {
- handleException(e, SO_LINGER);
- } finally {
- client.close();
- server.close();
- }
+ Socket client = new Socket(InetAddress.getLocalHost(), serverPort, true);
+ assertEquals("Failed to create socket", serverPort, client.getPort());
+
+ client = new Socket(InetAddress.getLocalHost(), serverPort, false);
+ client.close();
}
/**
- * @tests java.net.Socket#setSoTimeout(int)
+ * @tests java.net.Socket#Socket(Proxy)
*/
- public void test_setSoTimeoutI() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ public void test_ConstructorLjava_net_Proxy_Exception() {
+
+ class MockSecurityManager extends SecurityManager {
+
+ public void checkConnect(String host, int port) {
+ if ("127.0.0.1".equals(host)) {
+ throw new SecurityException("permission is not allowed");
+ }
+ }
+
+ public void checkPermission(Permission permission) {
+ return;
+ }
+ }
+ SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1",
+ 80);
+ SocketAddress addr2 = new InetSocketAddress("localhost", 80);
+
+ Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
+ // IllegalArgumentException test
try {
- client.setSoTimeout(100);
- assertEquals("Set incorrect sotimeout", 100, client.getSoTimeout());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
- } catch (Exception e) {
- handleException(e, SO_TIMEOUT);
- } finally {
- client.close();
- server.close();
+ new Socket(proxy1);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
}
- }
- /**
- * @tests java.net.Socket#setTcpNoDelay(boolean)
- */
- public void test_setTcpNoDelayZ() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
+ // should not throw any exception
+ new Socket(proxy2);
+ new Socket(Proxy.NO_PROXY);
+ // SecurityException test
+ SecurityManager originalSecurityManager = System.getSecurityManager();
try {
- boolean bool;
- client.setTcpNoDelay(bool = !client.getTcpNoDelay());
- assertTrue("Failed to set no delay setting: "
- + client.getTcpNoDelay(), client.getTcpNoDelay() == bool);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
- } catch (Exception e) {
- handleException(e, TCP_NODELAY);
+ System.setSecurityManager(new MockSecurityManager());
+ } catch (SecurityException e) {
+ System.err
+ .println("No permission to setSecurityManager, security related test in test_ConstructorLjava_net_Proxy_Security is ignored");
+ return;
+ }
+
+ Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, addr1);
+ Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, addr2);
+ try {
+ try {
+ new Socket(proxy3);
+ fail("should throw SecurityException");
+ } catch (SecurityException e) {
+ // expected
+ }
+ try {
+ new Socket(proxy4);
+ fail("should throw SecurityException");
+ } catch (SecurityException e) {
+ // expected
+ }
} finally {
- client.close();
- server.close();
+ System.setSecurityManager(originalSecurityManager);
}
+
}
/**
- * @tests java.net.Socket#toString()
+ * @tests java.net.Socket#getChannel()
*/
- public void test_toString() throws IOException {
+ public void test_getChannel() {
+ assertNull(new Socket().getChannel());
+ }
+
+ /**
+ * @tests java.net.Socket#getInetAddress()
+ */
+ public void test_getInetAddress() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ assertTrue("Returned incorrect InetAdrees", client.getInetAddress()
+ .equals(InetAddress.getLocalHost()));
- assertTrue("Returned incorrect string: " + client.toString()
- + " localHost: " + InetAddress.getLocalHost(), client
- .toString().equals(
- "Socket[addr=" + InetAddress.getLocalHost() + ",port="
- + client.getPort() + ",localport="
- + client.getLocalPort() + "]"));
client.close();
server.close();
}
/**
- * @tests java.net.Socket#shutdownInput()
+ * @tests java.net.Socket#getInputStream()
*/
- public void test_shutdownInput() throws IOException {
+ public void test_getInputStream() throws IOException {
+ // Simple fetch test
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
-
- Socket worker = server.accept();
- worker.setTcpNoDelay(true);
-
- InputStream theInput = client.getInputStream();
- OutputStream theOutput = worker.getOutputStream();
-
- // shutdown the input
- client.shutdownInput();
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+ InputStream is = client.getInputStream();
+ assertNotNull("Failed to get stream", is);
+ is.close();
+ client.close();
+ server.close();
- // send the regular data
- String sendString = new String("Test");
- theOutput.write(sendString.getBytes());
- theOutput.flush();
+ // Simple read/write test over the IO streams
+ final ServerSocket pingServer = new ServerSocket(0);
+ Runnable runnable = new Runnable() {
+ public void run() {
+ try {
+ Socket worker = pingServer.accept();
+ pingServer.close();
+ InputStream in = worker.getInputStream();
+ in.read();
+ OutputStream out = worker.getOutputStream();
+ out.write(new byte[42]);
+ worker.close();
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ }
+ };
+ Thread thread = new Thread(runnable, "Socket.getInputStream");
+ thread.start();
- // RI fails here. It is a RI bug not to return 0 to indicate EOF
- assertEquals(0, theInput.available());
+ Socket pingClient = new Socket(InetAddress.getLocalHost(), pingServer
+ .getLocalPort());
- client.close();
- server.close();
+ // Busy wait until the client is connected.
+ int c = 0;
+ while (!pingClient.isConnected()) {
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ }
+ if (++c > 4) {
+ fail("thread is not alive");
+ }
+ }
+
+ // Write some data to the server to provoke it
+ OutputStream out = pingClient.getOutputStream();
+ out.write(new byte[256]);
+
+ InputStream in = pingClient.getInputStream();
+ in.read(new byte[42]);
+
+ // Check EOF
+ assertEquals(-1, in.read());
+
+ in.close();
+
+ // No exception when reading a closed stream
+ assertEquals(-1, in.read());
+
+ pingClient.close();
+ pingServer.close();
}
/**
- * @tests java.net.Socket#shutdownOutput()
+ * @tests java.net.Socket#getKeepAlive()
*/
- public void test_shutdownOutput() throws IOException {
+ public void test_getKeepAlive() {
+ try {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort(), null, 0);
+
+ client.setKeepAlive(true);
+ assertTrue("getKeepAlive false when it should be true", client
+ .getKeepAlive());
+
+ client.setKeepAlive(false);
+ assertFalse("getKeepAlive true when it should be False", client
+ .getKeepAlive());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
+ } catch (Exception e) {
+ handleException(e, SO_KEEPALIVE);
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getLocalAddress()
+ */
+ public void test_getLocalAddress() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
- Socket worker = server.accept();
- OutputStream theOutput = worker.getOutputStream();
+ assertTrue("Returned incorrect InetAddress", client.getLocalAddress()
+ .equals(InetAddress.getLocalHost()));
- // shutdown the output
- worker.shutdownOutput();
+ // now validate that behaviour when the any address is returned
+ String preferIPv4StackValue = System
+ .getProperty("java.net.preferIPv4Stack");
+ String preferIPv6AddressesValue = System
+ .getProperty("java.net.preferIPv6Addresses");
- // send the regular data
- String sendString = new String("Test");
- try {
- theOutput.write(sendString.getBytes());
- theOutput.flush();
- fail("No exception when writing on socket with output shutdown");
- } catch (IOException e) {
- // Expected
+ client = new Socket();
+ client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
+
+ if (((preferIPv4StackValue == null) || preferIPv4StackValue
+ .equalsIgnoreCase("false"))
+ && (preferIPv6AddressesValue != null)
+ && (preferIPv6AddressesValue.equals("true"))) {
+ assertTrue(
+ "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false "
+ + client.getLocalSocketAddress(), client
+ .getLocalAddress() instanceof Inet6Address);
+ } else {
+ assertTrue(
+ "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true "
+ + client.getLocalSocketAddress(), client
+ .getLocalAddress() instanceof Inet4Address);
}
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getLocalPort()
+ */
+ public void test_getLocalPort() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ assertNotSame("Returned incorrect port", 0, client.getLocalPort());
client.close();
server.close();
@@ -1017,8 +950,8 @@
// set up server connect and then validate that we get the right
// response for the local address
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
int clientPort = client.getLocalPort();
assertEquals("Returned incorrect InetSocketAddress(1):",
@@ -1091,17 +1024,177 @@
}
/**
+ * @tests java.net.Socket#getOOBInline()
+ */
+ public void test_getOOBInline() {
+ try {
+ Socket theSocket = new Socket();
+
+ theSocket.setOOBInline(true);
+ assertTrue("expected OOBIline to be true", theSocket.getOOBInline());
+
+ theSocket.setOOBInline(false);
+ assertFalse("expected OOBIline to be false", theSocket
+ .getOOBInline());
+
+ theSocket.setOOBInline(false);
+ assertFalse("expected OOBIline to be false", theSocket
+ .getOOBInline());
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE);
+ } catch (Exception e) {
+ handleException(e, SO_OOBINLINE);
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getOutputStream()
+ */
+ @SuppressWarnings("deprecation")
+ public void test_getOutputStream() throws IOException {
+ // Simple fetch test
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+ OutputStream os = client.getOutputStream();
+ assertNotNull("Failed to get stream", os);
+ os.close();
+ client.close();
+ server.close();
+
+ // Simple read/write test over the IO streams
+ final ServerSocket sinkServer = new ServerSocket(0);
+ Runnable runnable = new Runnable() {
+ public void run() {
+ try {
+ Socket worker = sinkServer.accept();
+ sinkServer.close();
+ InputStream in = worker.getInputStream();
+ in.read();
+ in.close();
+ worker.close();
+ } catch (IOException e) {
+ fail();
+ }
+ }
+ };
+ Thread thread = new Thread(runnable, "Socket.getOutputStream");
+ thread.start();
+
+ Socket pingClient = new Socket(InetAddress.getLocalHost(), sinkServer
+ .getLocalPort());
+
+ // Busy wait until the client is connected.
+ int c = 0;
+ while (!pingClient.isConnected()) {
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ }
+ if (++c > 4) {
+ fail("thread is not alive");
+ }
+ }
+
+ // Write some data to the server
+ OutputStream out = pingClient.getOutputStream();
+ out.write(new byte[256]);
+
+ // Wait for the server to finish
+ Thread.yield();
+ c = 0;
+ while (thread.isAlive()) {
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ }
+ if (++c > 4) {
+ fail("read call did not exit");
+ }
+ }
+
+ // Subsequent writes should throw an exception
+ try {
+ // The output buffer may remain valid until the close completes
+ for (int i = 0; i < 400; i++) {
+ out.write(new byte[256]);
+ }
+ fail("write to closed socket did not cause exception");
+ } catch (IOException e) {
+ // Expected
+ }
+
+ out.close();
+ pingClient.close();
+ sinkServer.close();
+
+ // Regression test for HARMONY-2934
+ Socket socket = new Socket("127.0.0.1", 0, false);
+ OutputStream o = socket.getOutputStream();
+ o.write(1);
+ socket.close();
+
+ // Regression test for HARMONY-873
+ ServerSocket ss2 = new ServerSocket(0);
+ Socket s = new Socket("127.0.0.1", ss2.getLocalPort());
+ ss2.accept();
+ s.shutdownOutput();
+ try {
+ s.getOutputStream();
+ fail("should throw SocketException");
+ } catch (SocketException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getPort()
+ */
+ public void test_getPort() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ int serverPort = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), serverPort);
+
+ assertEquals("Returned incorrect port", serverPort, client.getPort());
+
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getReceiveBufferSize()
+ */
+ public void test_getReceiveBufferSize() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ try {
+ client.setReceiveBufferSize(130);
+ assertTrue("Incorrect buffer size",
+ client.getReceiveBufferSize() >= 130);
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
+ } catch (Exception e) {
+ handleException(e, SO_RCVBUF);
+ } finally {
+ client.close();
+ server.close();
+ }
+ }
+
+ /**
* @tests java.net.Socket#getRemoteSocketAddress()
*/
public void test_getRemoteSocketAddress() throws IOException {
// set up server connect and then validate that we get the right
// response for the remote address
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ int serverPort = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), serverPort);
assertEquals("Returned incorrect InetSocketAddress(1):",
- new InetSocketAddress(InetAddress.getLocalHost(), sport),
+ new InetSocketAddress(InetAddress.getLocalHost(), serverPort),
client.getRemoteSocketAddress());
client.close();
@@ -1114,9 +1207,9 @@
// now connect and validate we get the right answer
theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
- sport));
+ serverPort));
assertEquals("Returned incorrect InetSocketAddress(2):",
- new InetSocketAddress(InetAddress.getLocalHost(), sport),
+ new InetSocketAddress(InetAddress.getLocalHost(), serverPort),
theSocket.getRemoteSocketAddress());
theSocket.close();
@@ -1124,30 +1217,154 @@
}
/**
- * @tests java.net.Socket#isBound()
+ * @tests java.net.Socket#getReuseAddress()
*/
- public void test_isBound() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
- Socket worker = server.accept();
-
- assertTrue("Socket indicated not bound when it should be (1)", client
- .isBound());
- worker.close();
- client.close();
- server.close();
+ public void test_getReuseAddress() {
+ try {
+ Socket theSocket = new Socket();
+ theSocket.setReuseAddress(true);
+ assertTrue("getReuseAddress false when it should be true",
+ theSocket.getReuseAddress());
+ theSocket.setReuseAddress(false);
+ assertFalse("getReuseAddress true when it should be False",
+ theSocket.getReuseAddress());
- // now do it with the new constructors and revalidate. Connect causes
- // the socket to be bound
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), 0);
- client = new Socket();
- assertFalse("Socket indicated bound when it was not (2)", client
- .isBound());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
+ } catch (Exception e) {
+ handleException(e, SO_REUSEADDR);
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getSendBufferSize()
+ */
+ public void test_getSendBufferSize() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ try {
+ client.setSendBufferSize(134);
+ assertTrue("Incorrect buffer size",
+ client.getSendBufferSize() >= 134);
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
+ } catch (Exception e) {
+ handleException(e, SO_SNDBUF);
+ } finally {
+ client.close();
+ server.close();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getSoLinger()
+ */
+ public void test_getSoLinger() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ try {
+ client.setSoLinger(true, 200);
+ assertEquals("Returned incorrect linger", 200, client.getSoLinger());
+ client.setSoLinger(false, 0);
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
+ } catch (Exception e) {
+ handleException(e, SO_LINGER);
+ } finally {
+ client.close();
+ server.close();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getSoTimeout()
+ */
+ public void test_getSoTimeout() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ try {
+ client.setSoTimeout(100);
+ assertEquals("Returned incorrect sotimeout", 100, client
+ .getSoTimeout());
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
+ } catch (Exception e) {
+ handleException(e, SO_TIMEOUT);
+ } finally {
+ client.close();
+ server.close();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getTcpNoDelay()
+ */
+ public void test_getTcpNoDelay() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+
+ try {
+ boolean bool = !client.getTcpNoDelay();
+ client.setTcpNoDelay(bool);
+ assertTrue("Failed to get no delay setting: "
+ + client.getTcpNoDelay(), client.getTcpNoDelay() == bool);
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
+ } catch (Exception e) {
+ handleException(e, TCP_NODELAY);
+ } finally {
+ client.close();
+ server.close();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getTrafficClass()
+ */
+ public void test_getTrafficClass() {
+ try {
+ /*
+ * We cannot actually check that the values are set as if a platform
+ * does not support the option then it may come back unset even
+ * though we set it so just get the value to make sure we can get it
+ */
+ int trafficClass = new Socket().getTrafficClass();
+ assertTrue(0 <= trafficClass);
+ assertTrue(trafficClass <= 255);
+
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS);
+ } catch (Exception e) {
+ handleException(e, IP_TOS);
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#isBound()
+ */
+ public void test_isBound() throws IOException {
+ ServerSocket server = new ServerSocket(0);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
+ Socket worker = server.accept();
+
+ assertTrue("Socket indicated not bound when it should be (1)", client
+ .isBound());
+ worker.close();
+ client.close();
+ server.close();
+
+ client = new Socket();
+ assertFalse("Socket indicated bound when it was not (2)", client
+ .isBound());
server = new ServerSocket();
- server.bind(theAddress);
+ server.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
InetSocketAddress boundAddress = new InetSocketAddress(server
.getInetAddress(), server.getLocalPort());
client.connect(boundAddress);
@@ -1173,47 +1390,12 @@
}
/**
- * @tests java.net.Socket#isConnected()
- */
- public void test_isConnected() throws IOException {
- ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
- Socket worker = server.accept();
-
- assertTrue("Socket indicated not connected when it should be", client
- .isConnected());
- client.close();
- worker.close();
- server.close();
-
- // now do it with the new constructors and revalidate
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), 0);
- client = new Socket();
- assertFalse("Socket indicated connected when it was not", client
- .isConnected());
-
- server = new ServerSocket();
- server.bind(theAddress);
- InetSocketAddress boundAddress = new InetSocketAddress(server
- .getInetAddress(), server.getLocalPort());
- client.connect(boundAddress);
- worker = server.accept();
- assertTrue("Socket indicated not connected when it should be", client
- .isConnected());
- client.close();
- worker.close();
- server.close();
- }
-
- /**
* @tests java.net.Socket#isClosed()
*/
public void test_isClosed() throws IOException {
ServerSocket server = new ServerSocket(0);
- int sport = server.getLocalPort();
- Socket client = new Socket(InetAddress.getLocalHost(), sport);
+ Socket client = new Socket(InetAddress.getLocalHost(), server
+ .getLocalPort());
Socket worker = server.accept();
// validate isClosed returns expected values
@@ -1230,628 +1412,47 @@
assertTrue("Accepted Socket should indicate it is closed:", worker
.isClosed());
+ // and finally for the server socket
assertFalse("Server Socket should indicate it is not closed:", server
.isClosed());
server.close();
- assertTrue("Server Socket should indicate it is closed:", server
- .isClosed());
-
- }
-
- /**
- * @tests java.net.Socket#bind(java.net.SocketAddress)
- */
- public void test_bindLjava_net_SocketAddress() throws IOException {
-
- class mySocketAddress extends SocketAddress {
-
- public mySocketAddress() {
- }
- }
-
- // Address we cannot bind to
- Socket theSocket = new Socket();
- try {
- theSocket.bind(new InetSocketAddress(InetAddress
- .getByAddress(Support_Configuration.nonLocalAddressBytes),
- Support_PortManager.getNextPort()));
- fail("No exception when binding to bad address:"
- + theSocket.getLocalSocketAddress().toString());
- } catch (IOException ex) {
- }
- theSocket.close();
-
- // now create a socket that is not bound and then bind it
- theSocket = new Socket();
- int portNumber = Support_PortManager.getNextPort();
- theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber));
-
- // validate that the localSocketAddress reflects the address we
- // bound to
- assertTrue(
- "Local address not correct after bind:"
- + theSocket.getLocalSocketAddress().toString()
- + "Expected: "
- + (new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber)).toString(), theSocket
- .getLocalSocketAddress().equals(
- new InetSocketAddress(InetAddress
- .getLocalHost(), portNumber)));
-
- // make sure we can now connect and that connections appear to come
- // from the address we bound to.
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- ServerSocket serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- Socket servSock = serverSocket.accept();
- assertTrue(
- "Returned Remote address from server connected to does not match expected local address:"
- + servSock.getRemoteSocketAddress().toString()
- + "Expected: "
- + (new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber)).toString(), servSock
- .getRemoteSocketAddress().equals(
- new InetSocketAddress(InetAddress
- .getLocalHost(), portNumber)));
- theSocket.close();
- servSock.close();
- serverSocket.close();
-
- // validate if we pass in null that it picks an address for us and
- // all is ok
- theSocket = new Socket();
- theSocket.bind(null);
- assertNotNull("Bind with null did not work", theSocket
- .getLocalSocketAddress());
- theSocket.close();
-
- // now check the error conditions
-
- // Address that we have allready bound to
- theSocket = new Socket();
- Socket theSocket2 = new Socket();
- try {
- theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
- Support_PortManager.getNextPort());
- theSocket.bind(theAddress);
- theSocket2.bind(theAddress);
- fail("No exception binding to address that is not available");
- } catch (IOException ex) {
- }
- theSocket.close();
- theSocket2.close();
-
- // unsupported SocketAddress subclass
- theSocket = new Socket();
- try {
- theSocket.bind(new mySocketAddress());
- fail("No exception when binding using unsupported SocketAddress subclass");
- } catch (IllegalArgumentException ex) {
- }
- theSocket.close();
- }
-
- /**
- * @tests java.net.Socket#bind(java.net.SocketAddress)
- */
- public void test_bindLjava_net_SocketAddress_Proxy() throws IOException {
- // The Proxy will not impact on the bind operation.It can be assigned
- // with any address.
- Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(
- "127.0.0.1", 0));
- Socket socket = new Socket(proxy);
-
- try {
- InetAddress address = InetAddress.getByName("localhost");
- int port = 0;
- socket.bind(new InetSocketAddress(address, port));
-
- assertEquals(address, socket.getLocalAddress());
- assertTrue(port != socket.getLocalPort());
-
- } finally {
- socket.close();
- }
- }
-
- /**
- * @tests java.net.Socket#connect(java.net.SocketAddress)
- */
- public void test_connectLjava_net_SocketAddress() throws Exception {
- // needed for some tests
- class mySocketAddress extends SocketAddress {
-
- public mySocketAddress() {
- }
- }
-
- class SocketCloser extends Thread {
-
- int timeout = 0;
-
- Socket theSocket = null;
-
- public void run() {
- try {
- Thread.sleep(timeout);
- theSocket.close();
- } catch (Exception e) {
- }
- ;
- return;
- }
-
- public SocketCloser(int timeout, Socket theSocket) {
- this.timeout = timeout;
- this.theSocket = theSocket;
- }
- }
-
- // start by validating the error checks
- int portNumber = Support_PortManager.getNextPort();
- Socket theSocket = null;
- ServerSocket serverSocket = null;
- SocketAddress theAddress = null;
- SocketAddress nonConnectableAddress = null;
- SocketAddress nonReachableAddress = null;
- SocketAddress invalidType = null;
- // byte[] theBytes = {-1,-1,-1,-1};
- byte[] theBytes = { 0, 0, 0, 0 };
- theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber);
- nonConnectableAddress = new InetSocketAddress(InetAddress
- .getByAddress(theBytes), portNumber);
- nonReachableAddress = new InetSocketAddress(InetAddress
- .getByName(Support_Configuration.ResolvedNotExistingHost),
- portNumber);
-
- invalidType = new mySocketAddress();
-
- try {
- theSocket = new Socket();
- theSocket.connect(null);
- fail("No exception after null address passed in");
- } catch (IllegalArgumentException e) {
- // Expected
- }
-
- try {
- theSocket = new Socket();
- theSocket.connect(invalidType);
- fail("No exception when invalid socket address type passed in: ");
- } catch (IllegalArgumentException e) {
- // Expected
- }
-
- try {
- theSocket = new Socket();
- theSocket.connect(nonConnectableAddress);
- fail("No exception when non Connectable Address passed in: ");
- } catch (ConnectException e) {
- // Expected
- }
-
- // now validate that we get a connect exception if we try to connect to
- // an address on which nobody is listening
- try {
- theSocket = new Socket();
- theSocket.connect(theAddress);
- theSocket.close();
- fail("No exception when connecting to address nobody listening on: ");
- } catch (ConnectException e) {
- // Expected
- }
-
- // now validate that we can acutally connect when sombody is listening
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- theSocket.close();
- serverSocket.close();
-
- // now validate that we can acutally connect when sombody is listening
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
-
- // validate that when a socket is connected that it answers
- // correctly to related queries
- assertTrue("Socket did not returned connected when it is: ", theSocket
- .isConnected());
- assertFalse("Socket returned closed when it should be connected ",
- theSocket.isClosed());
- assertTrue("Socket returned not bound when it should be: ", theSocket
- .isBound());
- assertFalse(
- "Socket returned input Shutdown when it should be connected ",
- theSocket.isInputShutdown());
- assertFalse(
- "Socket returned output Shutdown when it should be connected ",
- theSocket.isOutputShutdown());
- assertTrue("Local port on connected socket was 0", theSocket
- .getLocalPort() != 0);
- theSocket.close();
- serverSocket.close();
-
- // now validate that we get the right exception if we connect when we
- // are already connected
- try {
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- theSocket.connect(theAddress);
- theSocket.close();
- serverSocket.close();
- fail("No exception when we try to connect on a connected socket: ");
-
- } catch (Exception e) {
- assertTrue(
- "Wrong exception when connecting on socket that is allready connected"
- + e.toString(), (e instanceof SocketException));
- assertFalse(
- "Wrong exception when connecting on socket that is allready connected"
- + e.toString(),
- (e instanceof SocketTimeoutException));
- try {
- theSocket.close();
- serverSocket.close();
- } catch (Exception e2) {
- }
-
- }
-
- // now validate that connected socket can be used to read/write
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- Socket servSock = serverSocket.accept();
- InputStream theInput = theSocket.getInputStream();
- OutputStream theOutput = servSock.getOutputStream();
- InputStream theInput2 = servSock.getInputStream();
- OutputStream theOutput2 = theSocket.getOutputStream();
-
- String sendString = new String("Test");
- theOutput.write(sendString.getBytes());
- theOutput.flush();
-
- Thread.sleep(1000);
-
- int totalBytesRead = 0;
- byte[] myBytes = new byte[100];
- while (theInput.available() > 0) {
- int bytesRead = theInput.read(myBytes, totalBytesRead,
- myBytes.length - totalBytesRead);
- totalBytesRead = totalBytesRead + bytesRead;
- }
-
- String receivedString = new String(myBytes, 0, totalBytesRead);
- assertTrue("Could not recv on socket connected with timeout:"
- + receivedString + ":" + sendString, receivedString
- .equals(sendString));
-
- sendString = new String("SEND - Test");
- theOutput2.write(sendString.getBytes());
- theOutput2.flush();
- Thread.sleep(1000);
-
- totalBytesRead = 0;
- myBytes = new byte[100];
- while (theInput2.available() > 0) {
- int bytesRead = theInput2.read(myBytes, totalBytesRead,
- myBytes.length - totalBytesRead);
- totalBytesRead = totalBytesRead + bytesRead;
- }
-
- receivedString = new String(myBytes, 0, totalBytesRead);
- assertTrue("Could not send on socket connected with timeout:"
- + receivedString + ":" + sendString, receivedString
- .equals(sendString));
-
- theSocket.close();
- serverSocket.close();
- }
-
- /**
- * @tests java.net.Socket#connect(java.net.SocketAddress, int)
- */
- public void test_connectLjava_net_SocketAddressI() throws Exception {
-
- // needed for some tests
- class mySocketAddress extends SocketAddress {
-
- public mySocketAddress() {
- }
- }
-
- class SocketCloser extends Thread {
-
- int timeout = 0;
-
- Socket theSocket = null;
-
- public void run() {
- try {
- Thread.sleep(timeout);
- theSocket.close();
- } catch (Exception e) {
- }
- ;
- return;
- }
-
- public SocketCloser(int timeout, Socket theSocket) {
- this.timeout = timeout;
- this.theSocket = theSocket;
- }
- }
-
- class SocketConnector extends Thread {
-
- int timeout = 0;
-
- Socket theSocket = null;
-
- SocketAddress address = null;
-
- public void run() {
[... 1041 lines stripped ...]