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/22 16:06:54 UTC
svn commit: r614215 [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=614215&r1=614214&r2=614215&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 Tue Jan 22 07:06:53 2008
@@ -42,7 +42,7 @@
public class SocketTest extends SocketTestCase {
ServerSocket ss;
- Socket s;
+ Socket client;
Thread t;
@@ -54,2431 +54,2543 @@
Exception failureException;
- private class SServer extends Thread implements Runnable {
- private Socket s1;
+ private class SServer2 extends Thread implements Runnable {
+ private Socket s1;
- public void run() {
- try {
- ss.setSoTimeout(5000);
- s1 = ss.accept();
- ss.close();
- Thread.sleep(4000);
- } catch (java.io.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 (java.io.IOException 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 void run() {
- try {
-
- ServerSocket socket = null;
- switch (serverSocketConstructor) {
- case FIRST_TIME:
- socket = new ServerSocket(port, backlog,
- new InetSocketAddress(host, port).getAddress());
+ public void run() {
+ try {
+ ss.setSoTimeout(5000);
+ s1 = ss.accept();
+ ss.close();
+ Thread.sleep(4000);
+ } catch (java.io.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 (java.io.IOException e) {
+ }
+ }
+ }
+ }
+
+ private static class SServer extends Thread {
+
+ final private ServerSocket server;
+
+ public SServer(ServerSocket server) {
+ super();
+ this.server = server;
+ }
+
+ 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
+ }
+ }
+ }
+ }
+
+ 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 void run() {
+ try {
+
+ ServerSocket socket = null;
+ switch (serverSocketConstructor) {
+ case FIRST_TIME:
+ socket = new ServerSocket(port, backlog,
+ new InetSocketAddress(host, port).getAddress());
port = socket.getLocalPort();
- break;
- case SECOND_TIME:
- socket = new ServerSocket(port, backlog);
- host = socket.getInetAddress().getHostName();
+ break;
+ case SECOND_TIME:
+ socket = new ServerSocket(port, backlog);
+ host = socket.getInetAddress().getHostName();
port = socket.getLocalPort();
- break;
- default:
- socket = new ServerSocket();
- break;
- }
-
- synchronized (this) {
- ready = true;
- this.notifyAll();
- }
-
- socket.setSoTimeout(5000);
- socket.accept();
-
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
-
- public synchronized void waitCreated() throws Exception{
- while (!ready) {
- this.wait();
- }
- }
- }
-
- private class ClientThread implements Runnable {
-
- public void run() {
- try {
- Socket socket = new Socket();
- InetSocketAddress addr = new InetSocketAddress(host, port);
- socket.connect(addr);
+ break;
+ default:
+ socket = new ServerSocket();
+ break;
+ }
+
+ synchronized (this) {
+ ready = true;
+ this.notifyAll();
+ }
+
+ socket.setSoTimeout(5000);
+ socket.accept();
+
+ socket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+
+ public synchronized void waitCreated() throws Exception {
+ while (!ready) {
+ this.wait();
+ }
+ }
+ }
+
+ private class ClientThread implements Runnable {
+
+ public void run() {
+ try {
+ Socket socket = new Socket();
+ InetSocketAddress addr = new InetSocketAddress(host, port);
+ socket.connect(addr);
+
+ socket.close();
+ } catch (Exception e) {
+ failureException = e;
+ }
+ }
+ }
+
+ private void connectTestImpl(int ssConsType) throws Exception {
+ ServerThread server = new ServerThread();
+ server.serverSocketConstructor = ssConsType;
+ Thread serverThread = new Thread(server);
+ serverThread.start();
+ server.waitCreated();
+
+ ClientThread client = new ClientThread();
+ Thread clientThread = new Thread(client);
+ clientThread.start();
+ try {
+ serverThread.join();
+ clientThread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * @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;
+ 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
+ }
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#Socket(java.lang.String, int,
+ * java.net.InetAddress, int)
+ */
+ 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;
+ }
+
+ 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);
+ }
+
+ } 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();
+
+ // 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;
+ }
+
+ 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);
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#Socket(java.lang.String, int, boolean)
+ */
+ @SuppressWarnings("deprecation")
+ public void test_ConstructorLjava_lang_StringIZ() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost().getHostAddress(), sport, true);
+
+ assertEquals("Failed to create socket", sport, client.getPort());
+ client.close();
+
+ client = new Socket(InetAddress.getLocalHost().getHostName(), sport, false);
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#Socket(java.net.InetAddress, int)
+ */
+ public void test_ConstructorLjava_net_InetAddressI() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport);
+
+ assertEquals("Failed to create socket", sport, client.getPort());
+
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @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();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport,
+ InetAddress.getLocalHost(), 0);
+ assertNotSame("Failed to create socket", 0, client.getLocalPort());
+ }
+
+ /**
+ * @tests java.net.Socket#Socket(java.net.InetAddress, int, boolean)
+ */
+ @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);
+
+ client = new Socket(InetAddress.getLocalHost(), sport, false);
+
+ }
+
+ /**
+ * @tests java.net.Socket#close()
+ */
+ public void test_close() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ try {
+ client.setSoLinger(false, 100);
+ } catch (IOException e) {
+ handleException(e, SO_LINGER);
+ }
+
+ client.close();
+ try {
+ client.getOutputStream();
+ fail("Failed to close socket");
+ } catch (IOException e) { // Caught Exception after close.
+ // Expected
+ }
+
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getInetAddress()
+ */
+ public void test_getInetAddress() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ assertTrue("Returned incorrect InetAdrees", client.getInetAddress()
+ .equals(InetAddress.getLocalHost()));
+
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getInputStream()
+ */
+ 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());
+
+ 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 {
+ do {
+ Thread.sleep(200);
+ } while (!thread.isAlive());
+ } catch (InterruptedException e) {
+ }
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ }
+ 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()
+ */
+ public void test_getKeepAlive() {
+ try {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, 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();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ assertTrue("Returned incorrect InetAddress", client.getLocalAddress()
+ .equals(InetAddress.getLocalHost()));
+
+ // 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 = 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();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ assertNotSame("Returned incorrect port", 0, client.getLocalPort());
+
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getOutputStream()
+ */
+ @SuppressWarnings("deprecation")
+ public void test_getOutputStream() throws IOException {
+ // Test for method java.io.OutputStream
+ // java.net.Socket.getOutputStream()
+ 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();
+
+ 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");
+ }
+ } while (thread.isAlive());
+
+ boolean exception = false;
+ try {
+ for (int i = 0; i < 400; i++)
+ out.write(data);
+ } catch (IOException e) {
+ exception = true;
+ }
+ 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);
+
+ // Regression test for harmony-2942
+ client = new Socket("0.0.0.0", Support_PortManager.getNextPortForUDP(),
+ false);
+ o = client.getOutputStream();
+ o.write(1);
+ }
+
+ /**
+ * @tests java.net.Socket#getPort()
+ */
+ public void test_getPort() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ assertEquals("Returned incorrect port", sport, client.getPort());
+
+ client.close();
+ server.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getSoLinger()
+ */
+ public void test_getSoLinger() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#getReceiveBufferSize()
+ */
+ public void test_getReceiveBufferSize() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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#getSendBufferSize()
+ */
+ public void test_getSendBufferSize() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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#getSoTimeout()
+ */
+ public void test_getSoTimeout() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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#setKeepAlive(boolean)
+ */
+ public void test_setKeepAliveZ() throws IOException {
+ // 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();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ try {
+ client.setKeepAlive(true);
+ client.setKeepAlive(false);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
+ } catch (Exception e) {
+ handleException(e, SO_KEEPALIVE);
+ } finally {
+ client.close();
+ server.close();
+ }
+
+ // Regression test for HARMONY-1136
+ new TestSocket((SocketImpl) null).setKeepAlive(true);
+ }
+
+ class TestSocket extends Socket {
+ public TestSocket(SocketImpl impl) throws SocketException {
+ super(impl);
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
+ */
+ public void test_setSocketImplFactoryLjava_net_SocketImplFactory() {
+ // Cannot test as setting will cause the factory to be changed for
+ // all subsequent sockets
+ }
+
+ /**
+ * @tests java.net.Socket#setSendBufferSize(int)
+ */
+ public void test_setSendBufferSizeI() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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#setReceiveBufferSize(int)
+ */
+ public void test_setReceiveBufferSizeI() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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#setSoLinger(boolean, int)
+ */
+ public void test_setSoLingerZI() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#setSoTimeout(int)
+ */
+ public void test_setSoTimeoutI() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#setTcpNoDelay(boolean)
+ */
+ public void test_setTcpNoDelayZ() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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);
+ } finally {
+ client.close();
+ server.close();
+ }
+ }
+
+ /**
+ * @tests java.net.Socket#toString()
+ */
+ public void test_toString() throws IOException {
+ ServerSocket server = new ServerSocket();
+ server.bind(null);
+ int sport = server.getLocalPort();
+ Socket client = new Socket(InetAddress.getLocalHost(), sport, null, 0);
+
+ 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()
+ */
+ public void test_shutdownInput() throws Exception {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+ ServerSocket serverSocket = new ServerSocket(port, 5, addr);
+ Socket theSocket = new Socket(addr, port);
+ Socket servSock = serverSocket.accept();
+
+ InputStream theInput = theSocket.getInputStream();
+ OutputStream theOutput = servSock.getOutputStream();
+
+ // shutdown the input
+ theSocket.shutdownInput();
+
+ // send the regular data
+ String sendString = new String("Test");
+ theOutput.write(sendString.getBytes());
+ theOutput.flush();
+
+ // give things some time to settle
+ Thread.sleep(1000);
+
+ // RI fails here. It is a RI bug not to return 0 to indicate EOF
+ assertEquals(0, theInput.available());
+
+ theSocket.close();
+ serverSocket.close();
+ }
+
+ /**
+ * @tests java.net.Socket#shutdownOutput()
+ */
+ public void test_shutdownOutput() throws IOException {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+ ServerSocket serverSocket = new ServerSocket(port, 5, addr);
+ Socket theSocket = new Socket(addr, port);
+ Socket servSock = serverSocket.accept();
+
+ InputStream theInput = theSocket.getInputStream();
+ OutputStream theOutput = servSock.getOutputStream();
+
+ // shutdown the output
+ servSock.shutdownOutput();
+
+ // 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 (Exception e) {
+ }
+
+ theSocket.close();
+ serverSocket.close();
+ }
+
+ /**
+ * @tests java.net.Socket#getLocalSocketAddress()
+ */
+ public void test_getLocalSocketAddress() throws IOException {
+ // set up server connect and then validate that we get the right
+ // response for the local address
+ int sport = startServer("SServer getLocSocketAddress");
+ int portNumber = Support_PortManager.getNextPort();
+ client = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
+ assertTrue(
+ "Returned incorrect InetSocketAddress(1):"
+ + client.getLocalSocketAddress().toString()
+ + "Expected: "
+ + (new InetSocketAddress(InetAddress.getLocalHost(),
+ portNumber)).toString(), client
+ .getLocalSocketAddress().equals(
+ new InetSocketAddress(InetAddress
+ .getLocalHost(), portNumber)));
+ client.close();
+
+ // now create a socket that is not bound and validate we get the
+ // right answer
+ Socket theSocket = new Socket();
+ assertNull(
+ "Returned incorrect InetSocketAddress -unbound socket- Expected null",
+ theSocket.getLocalSocketAddress());
+
+ // now bind the socket and make sure we get the right answer
+ portNumber = Support_PortManager.getNextPort();
+ theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
+ portNumber));
+ assertTrue(
+ "Returned incorrect InetSocketAddress(2):"
+ + theSocket.getLocalSocketAddress().toString()
+ + "Expected: "
+ + (new InetSocketAddress(InetAddress.getLocalHost(),
+ portNumber)).toString(), theSocket
+ .getLocalSocketAddress().equals(
+ new InetSocketAddress(InetAddress
+ .getLocalHost(), portNumber)));
+ theSocket.close();
+
+ // now validate thet behaviour when the any address is returned
+ client = new Socket();
+ client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
+
+ String preferIPv4StackValue = System
+ .getProperty("java.net.preferIPv4Stack");
+ String preferIPv6AddressesValue = System
+ .getProperty("java.net.preferIPv6Addresses");
+ if (((preferIPv4StackValue == null) || preferIPv4StackValue
+ .equalsIgnoreCase("false"))
+ && (preferIPv6AddressesValue != null)
+ && (preferIPv6AddressesValue.equals("true"))) {
+ assertTrue(
+ "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false "
+ + client.getLocalSocketAddress(),
+ ((InetSocketAddress) client.getLocalSocketAddress())
+ .getAddress() instanceof Inet6Address);
+ } else {
+ assertTrue(
+ "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
+ + client.getLocalSocketAddress(),
+ ((InetSocketAddress) client.getLocalSocketAddress())
+ .getAddress() instanceof Inet4Address);
+ }
+ client.close();
+
+ // now validate the same for getLocalAddress
+ 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 with preferIPv6Addresses=true, preferIPv4Stack=false "
+ + client.getLocalSocketAddress(),
+ ((InetSocketAddress) client.getLocalSocketAddress())
+ .getAddress() instanceof Inet6Address);
+ } else {
+ assertTrue(
+ "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
+ + client.getLocalSocketAddress(),
+ ((InetSocketAddress) client.getLocalSocketAddress())
+ .getAddress() instanceof Inet4Address);
+ }
+ client.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
+ int sport = startServer("SServer getLocRemoteAddress");
+ int portNumber = Support_PortManager.getNextPort();
+ client = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
+ assertTrue("Returned incorrect InetSocketAddress(1):"
+ + client.getLocalSocketAddress().toString(),
+ client.getRemoteSocketAddress()
+ .equals(
+ new InetSocketAddress(InetAddress
+ .getLocalHost(), sport)));
+ client.close();
+
+ // now create one that is not connect and validate that we get the
+ // right answer
+ Socket theSocket = new Socket();
+ portNumber = Support_PortManager.getNextPort();
+ theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
+ portNumber));
+
+ assertNull("Returned incorrect InetSocketAddress -unconnected socket:"
+ + "Expected: NULL", theSocket.getRemoteSocketAddress());
+
+ // now connect and validate we get the right answer
+ theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
+ sport));
+ assertTrue("Returned incorrect InetSocketAddress(2):"
+ + theSocket.getRemoteSocketAddress().toString(),
+ theSocket.getRemoteSocketAddress()
+ .equals(
+ new InetSocketAddress(InetAddress
+ .getLocalHost(), sport)));
+ theSocket.close();
+
+ }
- socket.close();
- }catch (Exception e) {
- failureException = e;
- }
- }
- }
-
- private void connectTestImpl(int ssConsType) throws Exception {
- ServerThread server = new ServerThread();
- server.serverSocketConstructor = ssConsType;
- Thread serverThread = new Thread(server);
- serverThread.start();
- server.waitCreated();
-
- ClientThread client = new ClientThread();
- Thread clientThread = new Thread(client);
- clientThread.start();
- try {
- serverThread.join();
- clientThread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * @tests java.net.Socket#Socket()
- */
- public void test_Constructor() {
- // create the socket and then validate some basic state
- 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 {
- // Test for method java.net.Socket(java.lang.String, int)
- int sport = startServer("Cons String,I");
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport);
- assertTrue("Failed to create socket", s.getPort() == sport);
-
- //regression for HARMONY-946
- ServerSocket ss = null;
- Socket s = null;
- 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
+ /**
+ * @tests java.net.Socket#isBound()
+ */
+ public void test_isBound() throws IOException {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+ ServerSocket serverSocket = new ServerSocket(port, 5, addr);
+ Socket theSocket = new Socket(addr, port);
+ Socket servSock = serverSocket.accept();
+ assertTrue("Socket indicated not bound when it should be (1)",
+ theSocket.isBound());
+ theSocket.close();
+ serverSocket.close();
+
+ // now do it with the new constructors and revalidate. Connect causes
+ // the socket to be bound
+ InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort());
+ theSocket = new Socket();
+ assertFalse("Socket indicated bound when it was not (2)", theSocket
+ .isBound());
+ serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress);
+ servSock = serverSocket.accept();
+ assertTrue("Socket indicated not bound when it should be (2)",
+ theSocket.isBound());
+ theSocket.close();
+ serverSocket.close();
+
+ // now test when we bind explicitely
+ InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort());
+ theSocket = new Socket();
+ assertFalse("Socket indicated bound when it was not (3)", theSocket
+ .isBound());
+ theSocket.bind(theLocalAddress);
+ assertTrue("Socket indicated not bound when it should be (3a)",
+ theSocket.isBound());
+ theSocket.close();
+ assertTrue("Socket indicated not bound when it should be (3b)",
+ theSocket.isBound());
+ }
+
+ /**
+ * @tests java.net.Socket#isConnected()
+ */
+ public void test_isConnected() throws IOException {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+ ServerSocket serverSocket = new ServerSocket(port, 5, addr);
+ Socket theSocket = new Socket(addr, port);
+ Socket servSock = serverSocket.accept();
+ assertTrue("Socket indicated not connected when it should be",
+ theSocket.isConnected());
+ theSocket.close();
+ serverSocket.close();
+
+ // now do it with the new constructors and revalidate
+ InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort());
+ theSocket = new Socket();
+ assertFalse("Socket indicated connected when it was not", theSocket
+ .isConnected());
+ serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress);
+ servSock = serverSocket.accept();
+ assertTrue("Socket indicated not connected when it should be",
+ theSocket.isConnected());
+ theSocket.close();
+ serverSocket.close();
+ }
+
+ /**
+ * @tests java.net.Socket#isClosed()
+ */
+ public void test_isClosed() throws IOException {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+ ServerSocket serverSocket = new ServerSocket(port, 5, addr);
+ Socket theSocket = new Socket(addr, port);
+ Socket servSock = serverSocket.accept();
+
+ // validate isClosed returns expected values
+ assertFalse("Socket should indicate it is not closed(1):", theSocket
+ .isClosed());
+ theSocket.close();
+ assertTrue("Socket should indicate it is closed(1):", theSocket
+ .isClosed());
+
+ theSocket = new Socket(addr, port);
+ assertFalse("Socket should indicate it is not closed(2):", theSocket
+ .isClosed());
+ theSocket.close();
+ assertTrue("Socket should indicate it is closed(2):", theSocket
+ .isClosed());
+
+ // validate that isClosed works ok for sockets returned from
+ // ServerSocket.accept()
+ assertFalse("Server Socket should indicate it is not closed:", servSock
+ .isClosed());
+ servSock.close();
+ assertTrue("Server Socket should indicate it is closed:", servSock
+ .isClosed());
+ }
+
+ /**
+ * @tests java.net.Socket#bind(java.net.SocketAddress)
+ */
+ public void test_bindLjava_net_SocketAddress() throws IOException {
+
+ class mySocketAddress extends SocketAddress {
+
+ public mySocketAddress() {
}
}
- }
- /**
- * @tests java.net.Socket#Socket(java.lang.String, int,
- * java.net.InetAddress, int)
- */
- 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();
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertTrue("Failed to create socket", s.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;
- }
-
- 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);
- }
-
- } 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();
+ 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();
- // 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;
- }
-
- 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);
- }
- }
-
- /**
- * @tests java.net.Socket#Socket(java.lang.String, int, boolean)
- */
- public void test_ConstructorLjava_lang_StringIZ() throws IOException {
- // Test for method java.net.Socket(java.lang.String, int, boolean)
- int sport = startServer("Cons String,I,Z");
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport, true);
- assertTrue("Failed to create socket", s.getPort() == sport);
-
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport, false);
- }
-
- /**
- * @tests java.net.Socket#Socket(java.net.InetAddress, int)
- */
- public void test_ConstructorLjava_net_InetAddressI() throws IOException {
- // Test for method java.net.Socket(java.net.InetAddress, int)
- int sport = startServer("Cons InetAddress,I");
- s = new Socket(InetAddress.getLocalHost(), sport);
- assertTrue("Failed to create socket", s.getPort() == sport);
- }
-
- /**
- * @tests java.net.Socket#Socket(java.net.InetAddress, int,
- * java.net.InetAddress, int)
- */
- public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI()
- throws IOException {
- // Test for method java.net.Socket(java.net.InetAddress, int,
- // java.net.InetAddress, int)
- int sport = startServer("Cons InetAddress,I,InetAddress,I");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertTrue("Failed to create socket", s.getLocalPort() == portNumber);
- }
-
- /**
- * @tests java.net.Socket#Socket(java.net.InetAddress, int, boolean)
- */
- 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");
- s = new Socket(InetAddress.getLocalHost(), sport, true);
- assertTrue("Failed to create socket", s.getPort() == sport);
-
- s = new Socket(InetAddress.getLocalHost(), sport, false);
-
- }
-
- /**
- * @tests java.net.Socket#close()
- */
- public void test_close() throws IOException {
- // Test for method void java.net.Socket.close()
- int sport = startServer("SServer close");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- try {
- s.setSoLinger(false, 100);
- } catch (IOException e) {
- handleException(e, SO_LINGER);
- }
- try {
- s.close();
- s.getOutputStream();
- } catch (java.io.IOException e) { // Caught Exception after close.
- return;
- } catch (NullPointerException e) { // Caught Exception after close.
- return;
- }
- fail("Failed to close socket");
-
- }
-
- /**
- * @tests java.net.Socket#getInetAddress()
- */
- public void test_getInetAddress() throws IOException {
- // Test for method java.net.InetAddress java.net.Socket.getInetAddress()
- int sport = startServer("SServer getInetAddress");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- assertTrue("Returned incorrect InetAdrees", s.getInetAddress().equals(
- InetAddress.getLocalHost()));
-
- }
-
- /**
- * @tests java.net.Socket#getInputStream()
- */
- 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();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- (t = new SServer()).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());
-
- 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 {
- do {
- Thread.sleep(200);
- } while (!thread.isAlive());
- } catch (InterruptedException e) {
- }
- try {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- 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()
- */
- public void test_getKeepAlive() {
- try {
- int sport = startServer("SServer getKeepAlive");
- int portNumber = Support_PortManager.getNextPort();
- Socket theSocket = new Socket(InetAddress.getLocalHost(), sport,
- null, portNumber);
- theSocket.setKeepAlive(true);
- assertTrue("getKeepAlive false when it should be true", theSocket
- .getKeepAlive());
- theSocket.setKeepAlive(false);
- assertFalse("getKeepAlive true when it should be False", theSocket
- .getKeepAlive());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
- } catch (Exception e) {
- handleException(e, SO_KEEPALIVE);
- }
- }
-
- /**
- * @tests java.net.Socket#getLocalAddress()
- */
- public void test_getLocalAddress() throws IOException {
- // Test for method java.net.InetAddress
- // java.net.Socket.getLocalAddress()
- int sport = startServer("SServer getLocAddress");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- assertTrue("Returned incorrect InetAddress", s.getLocalAddress()
- .equals(InetAddress.getLocalHost()));
-
- // now validate thet behaviour when the any address is returned
- String preferIPv4StackValue = System
- .getProperty("java.net.preferIPv4Stack");
- String preferIPv6AddressesValue = System
- .getProperty("java.net.preferIPv6Addresses");
-
- s = new Socket();
- s.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 "
- + s.getLocalSocketAddress(),
- s.getLocalAddress() instanceof Inet6Address);
- } else {
- assertTrue(
- "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true "
- + s.getLocalSocketAddress(),
- s.getLocalAddress() instanceof Inet4Address);
- }
- s.close();
-
- }
-
- /**
- * @tests java.net.Socket#getLocalPort()
- */
- public void test_getLocalPort() throws IOException {
- // Test for method int java.net.Socket.getLocalPort()
- int sport = startServer("SServer getLocalPort");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertTrue("Returned incorrect port", s.getLocalPort() == portNumber);
- }
-
- /**
- * @tests java.net.Socket#getOutputStream()
- */
- @SuppressWarnings("deprecation")
- public void test_getOutputStream() throws IOException {
- // Test for method java.io.OutputStream
- // java.net.Socket.getOutputStream()
- int sport = startServer("SServer getOutputStream");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- java.io.OutputStream os = s.getOutputStream();
- assertNotNull("Failed to get stream", os);
- tearDown();
-
- 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");
- }
- } while (thread.isAlive());
-
- boolean exception = false;
- try {
- for (int i = 0; i < 400; i++)
- out.write(data);
- } catch (IOException e) {
- exception = true;
- }
- out.close();
- assertTrue("write to closed socket did not cause exception", exception);
-
- // Regression test for harmony-2934
- s = new Socket("127.0.0.1", Support_PortManager.getNextPortForUDP(),
- false);
- OutputStream o = s.getOutputStream();
- o.write(1);
-
- // Regression test for harmony-2942
- s = new Socket("0.0.0.0", Support_PortManager.getNextPortForUDP(),
- false);
- o = s.getOutputStream();
- o.write(1);
- }
-
- /**
- * @tests java.net.Socket#getPort()
- */
- public void test_getPort() throws IOException {
- // Test for method int java.net.Socket.getPort()
- int sport = startServer("SServer getPort");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- assertTrue("Returned incorrect port" + s.getPort(),
- s.getPort() == sport);
- }
-
- /**
- * @tests java.net.Socket#getSoLinger()
- */
- public void test_getSoLinger() {
- // Test for method int java.net.Socket.getSoLinger()
- try {
- int sport = startServer("SServer getSoLinger");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- s.setSoLinger(true, 200);
- assertEquals("Returned incorrect linger", 200, s.getSoLinger());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
- s.setSoLinger(false, 0);
- } catch (Exception e) {
- handleException(e, SO_LINGER);
- }
- }
-
- /**
- * @tests java.net.Socket#getReceiveBufferSize()
- */
- public void test_getReceiveBufferSize() {
- try {
- int sport = startServer("SServer getReceiveBufferSize");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- null, portNumber);
- s.setReceiveBufferSize(130);
- assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
- } catch (Exception e) {
- handleException(e, SO_RCVBUF);
- }
- }
-
- /**
- * @tests java.net.Socket#getSendBufferSize()
- */
- public void test_getSendBufferSize() {
- try {
- int sport = startServer("SServer setSendBufferSize");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- null, portNumber);
- s.setSendBufferSize(134);
- assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
- } catch (Exception e) {
- handleException(e, SO_SNDBUF);
- }
- }
-
- /**
- * @tests java.net.Socket#getSoTimeout()
- */
- public void test_getSoTimeout() {
- // Test for method int java.net.Socket.getSoTimeout()
- try {
- int sport = startServer("SServer getSoTimeout");
- s = new Socket(InetAddress.getLocalHost(), sport);
- s.setSoTimeout(100);
- assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
- } catch (Exception e) {
- handleException(e, SO_TIMEOUT);
- }
- }
-
- /**
- * @tests java.net.Socket#getTcpNoDelay()
- */
- public void test_getTcpNoDelay() {
- // Test for method boolean java.net.Socket.getTcpNoDelay()
- try {
- int sport = startServer("SServer getTcpNoDelay");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- boolean bool = !s.getTcpNoDelay();
- s.setTcpNoDelay(bool);
- assertTrue("Failed to get no delay setting: " + s.getTcpNoDelay(),
- s.getTcpNoDelay() == bool);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
- } catch (Exception e) {
- handleException(e, TCP_NODELAY);
- }
- }
-
- /**
- * @tests java.net.Socket#setKeepAlive(boolean)
- */
- public void test_setKeepAliveZ() throws Exception {
- // There is not really a good test for this as it is there to detect
- // crashed machines. Just make sure we can set it
- try {
- int sport = startServer("SServer setKeepAlive");
- int portNumber = Support_PortManager.getNextPort();
- Socket theSocket = new Socket(InetAddress.getLocalHost(), sport,
- null, portNumber);
- theSocket.setKeepAlive(true);
- theSocket.setKeepAlive(false);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
- } catch (Exception e) {
- handleException(e, SO_KEEPALIVE);
- }
- // regression test for HARMONY-1136
- new testSocket((SocketImpl) null).setKeepAlive(true);
- }
- class testSocket extends Socket {
- public testSocket(SocketImpl impl) throws SocketException {
- super(impl);
- }
- }
-
- /**
- * @tests java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
- */
- public void test_setSocketImplFactoryLjava_net_SocketImplFactory() {
- // Test for method void
- // java.net.Socket.setSocketImplFactory(java.net.SocketImplFactory)
-
- // Cannot test as setting will cause the factory to be changed for
- // all subsequent sockets
- }
-
- /**
- * @tests java.net.Socket#setSendBufferSize(int)
- */
- public void test_setSendBufferSizeI() {
- try {
- int sport = startServer("SServer setSendBufferSizeI");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- s.setSendBufferSize(134);
- assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
- } catch (Exception e) {
- handleException(e, SO_SNDBUF);
- }
- }
-
- /**
- * @tests java.net.Socket#setReceiveBufferSize(int)
- */
- public void test_setReceiveBufferSizeI() {
- try {
- int sport = startServer("SServer setReceiveBufferSizeI");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- s.setReceiveBufferSize(130);
- assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
- } catch (Exception e) {
- handleException(e, SO_RCVBUF);
- }
- }
-
- /**
- * @tests java.net.Socket#setSoLinger(boolean, int)
- */
- public void test_setSoLingerZI() {
- // Test for method void java.net.Socket.setSoLinger(boolean, int)
- try {
- int sport = startServer("SServer setSoLingerZI");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- s.setSoLinger(true, 500);
- assertEquals("Set incorrect linger", 500, s.getSoLinger());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
- s.setSoLinger(false, 0);
- } catch (Exception e) {
- handleException(e, SO_LINGER);
- }
- }
-
- /**
- * @tests java.net.Socket#setSoTimeout(int)
- */
- public void test_setSoTimeoutI() {
- // Test for method void java.net.Socket.setSoTimeout(int)
- try {
- int sport = startServer("SServer seSoTimeoutI");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- s.setSoTimeout(100);
- assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout());
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
- } catch (Exception e) {
- handleException(e, SO_TIMEOUT);
- }
- }
-
- /**
- * @tests java.net.Socket#setTcpNoDelay(boolean)
- */
- public void test_setTcpNoDelayZ() {
- // Test for method void java.net.Socket.setTcpNoDelay(boolean)
- try {
- int sport = startServer("SServer setTcpNoDelayZ");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- boolean bool;
- s.setTcpNoDelay(bool = !s.getTcpNoDelay());
- assertTrue("Failed to set no delay setting: " + s.getTcpNoDelay(),
- s.getTcpNoDelay() == bool);
- ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
- } catch (Exception e) {
- handleException(e, TCP_NODELAY);
- }
- }
-
- /**
- * @tests java.net.Socket#toString()
- */
- public void test_toString() throws IOException {
- // Test for method java.lang.String java.net.Socket.toString()
- int sport = startServer("SServer toString");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- InetAddress.getLocalHost(), portNumber);
- assertTrue("Returned incorrect string: " + s.toString()
- + " localHost: " + InetAddress.getLocalHost(), s.toString()
- .equals(
- "Socket[addr=" + InetAddress.getLocalHost() + ",port="
- + s.getPort() + ",localport="
- + s.getLocalPort() + "]"));
- }
-
- /**
- * @tests java.net.Socket#shutdownInput()
- */
- public void test_shutdownInput() throws Exception {
- InetAddress addr = InetAddress.getLocalHost();
- int port = Support_PortManager.getNextPort();
- ServerSocket serverSocket = new ServerSocket(port, 5, addr);
- Socket theSocket = new Socket(addr, port);
- Socket servSock = serverSocket.accept();
-
- InputStream theInput = theSocket.getInputStream();
- OutputStream theOutput = servSock.getOutputStream();
-
- // shutdown the input
- theSocket.shutdownInput();
-
- // send the regular data
- String sendString = new String("Test");
- theOutput.write(sendString.getBytes());
- theOutput.flush();
-
- // give things some time to settle
- Thread.sleep(1000);
-
- // RI fails here. It is a RI bug not to return 0 to indicate EOF
- assertEquals(0, theInput.available());
-
- theSocket.close();
- serverSocket.close();
- }
-
- /**
- * @tests java.net.Socket#shutdownOutput()
- */
- public void test_shutdownOutput() throws IOException {
- InetAddress addr = InetAddress.getLocalHost();
- int port = Support_PortManager.getNextPort();
- ServerSocket serverSocket = new ServerSocket(port, 5, addr);
- Socket theSocket = new Socket(addr, port);
- Socket servSock = serverSocket.accept();
-
- InputStream theInput = theSocket.getInputStream();
- OutputStream theOutput = servSock.getOutputStream();
-
- // shutdown the output
- servSock.shutdownOutput();
-
- // 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 (Exception e) {
- }
-
- theSocket.close();
- serverSocket.close();
- }
-
- /**
- * @tests java.net.Socket#getLocalSocketAddress()
- */
- public void test_getLocalSocketAddress() throws IOException {
- // set up server connect and then validate that we get the right
- // response for the local address
- int sport = startServer("SServer getLocSocketAddress");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- assertTrue(
- "Returned incorrect InetSocketAddress(1):"
- + s.getLocalSocketAddress().toString()
- + "Expected: "
- + (new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber)).toString(), s
- .getLocalSocketAddress().equals(
- new InetSocketAddress(InetAddress
- .getLocalHost(), portNumber)));
- s.close();
-
- // now create a socket that is not bound and validate we get the
- // right answer
- Socket theSocket = new Socket();
- assertNull(
- "Returned incorrect InetSocketAddress -unbound socket- Expected null",
- theSocket.getLocalSocketAddress());
-
- // now bind the socket and make sure we get the right answer
- portNumber = Support_PortManager.getNextPort();
- theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber));
- assertTrue(
- "Returned incorrect InetSocketAddress(2):"
- + theSocket.getLocalSocketAddress().toString()
- + "Expected: "
- + (new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber)).toString(), theSocket
- .getLocalSocketAddress().equals(
- new InetSocketAddress(InetAddress
- .getLocalHost(), portNumber)));
- theSocket.close();
-
- // now validate thet behaviour when the any address is returned
- s = new Socket();
- s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
-
- String preferIPv4StackValue = System
- .getProperty("java.net.preferIPv4Stack");
- String preferIPv6AddressesValue = System
- .getProperty("java.net.preferIPv6Addresses");
- if (((preferIPv4StackValue == null) || preferIPv4StackValue
- .equalsIgnoreCase("false"))
- && (preferIPv6AddressesValue != null)
- && (preferIPv6AddressesValue.equals("true"))) {
- assertTrue(
- "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false "
- + s.getLocalSocketAddress(),
- ((InetSocketAddress) s.getLocalSocketAddress())
- .getAddress() instanceof Inet6Address);
- } else {
- assertTrue(
- "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
- + s.getLocalSocketAddress(),
- ((InetSocketAddress) s.getLocalSocketAddress())
- .getAddress() instanceof Inet4Address);
- }
- s.close();
-
- // now validate the same for getLocalAddress
- s = new Socket();
- s.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 with preferIPv6Addresses=true, preferIPv4Stack=false "
- + s.getLocalSocketAddress(),
- ((InetSocketAddress) s.getLocalSocketAddress())
- .getAddress() instanceof Inet6Address);
- } else {
- assertTrue(
- "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
- + s.getLocalSocketAddress(),
- ((InetSocketAddress) s.getLocalSocketAddress())
- .getAddress() instanceof Inet4Address);
- }
- s.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
- int sport = startServer("SServer getLocRemoteAddress");
- int portNumber = Support_PortManager.getNextPort();
- s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
- assertTrue("Returned incorrect InetSocketAddress(1):"
- + s.getLocalSocketAddress().toString(),
- s.getRemoteSocketAddress()
- .equals(
- new InetSocketAddress(InetAddress
- .getLocalHost(), sport)));
- s.close();
-
- // now create one that is not connect and validate that we get the
- // right answer
- Socket theSocket = new Socket();
- portNumber = Support_PortManager.getNextPort();
- theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber));
-
- assertNull("Returned incorrect InetSocketAddress -unconnected socket:"
- + "Expected: NULL", theSocket.getRemoteSocketAddress());
-
- // now connect and validate we get the right answer
- theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
- sport));
- assertTrue("Returned incorrect InetSocketAddress(2):"
- + theSocket.getRemoteSocketAddress().toString(),
- theSocket.getRemoteSocketAddress()
- .equals(
- new InetSocketAddress(InetAddress
- .getLocalHost(), sport)));
- theSocket.close();
-
- }
-
- /**
- * @tests java.net.Socket#isBound()
- */
- public void test_isBound() throws IOException {
- InetAddress addr = InetAddress.getLocalHost();
- int port = Support_PortManager.getNextPort();
- ServerSocket serverSocket = new ServerSocket(port, 5, addr);
- Socket theSocket = new Socket(addr, port);
- Socket servSock = serverSocket.accept();
- assertTrue("Socket indicated not bound when it should be (1)",
- theSocket.isBound());
- theSocket.close();
- serverSocket.close();
-
- // now do it with the new constructors and revalidate. Connect causes
- // the socket to be bound
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- theSocket = new Socket();
- assertFalse("Socket indicated bound when it was not (2)", theSocket
- .isBound());
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- servSock = serverSocket.accept();
- assertTrue("Socket indicated not bound when it should be (2)",
- theSocket.isBound());
- theSocket.close();
- serverSocket.close();
-
- // now test when we bind explicitely
- InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- theSocket = new Socket();
- assertFalse("Socket indicated bound when it was not (3)", theSocket
- .isBound());
- theSocket.bind(theLocalAddress);
- assertTrue("Socket indicated not bound when it should be (3a)",
- theSocket.isBound());
- theSocket.close();
- assertTrue("Socket indicated not bound when it should be (3b)",
- theSocket.isBound());
- }
-
- /**
- * @tests java.net.Socket#isConnected()
- */
- public void test_isConnected() throws IOException {
- InetAddress addr = InetAddress.getLocalHost();
- int port = Support_PortManager.getNextPort();
- ServerSocket serverSocket = new ServerSocket(port, 5, addr);
- Socket theSocket = new Socket(addr, port);
- Socket servSock = serverSocket.accept();
- assertTrue("Socket indicated not connected when it should be",
- theSocket.isConnected());
- theSocket.close();
- serverSocket.close();
-
- // now do it with the new constructors and revalidate
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- theSocket = new Socket();
- assertFalse("Socket indicated connected when it was not", theSocket
- .isConnected());
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- servSock = serverSocket.accept();
- assertTrue("Socket indicated not connected when it should be",
- theSocket.isConnected());
- theSocket.close();
- serverSocket.close();
- }
-
- /**
- * @tests java.net.Socket#isClosed()
- */
- public void test_isClosed() throws IOException {
- InetAddress addr = InetAddress.getLocalHost();
- int port = Support_PortManager.getNextPort();
- ServerSocket serverSocket = new ServerSocket(port, 5, addr);
- Socket theSocket = new Socket(addr, port);
- Socket servSock = serverSocket.accept();
-
- // validate isClosed returns expected values
- assertFalse("Socket should indicate it is not closed(1):", theSocket
- .isClosed());
- theSocket.close();
- assertTrue("Socket should indicate it is closed(1):", theSocket
- .isClosed());
-
- theSocket = new Socket(addr, port);
- assertFalse("Socket should indicate it is not closed(2):", theSocket
- .isClosed());
- theSocket.close();
- assertTrue("Socket should indicate it is closed(2):", theSocket
- .isClosed());
-
- // validate that isClosed works ok for sockets returned from
- // ServerSocket.accept()
- assertFalse("Server Socket should indicate it is not closed:", servSock
- .isClosed());
- servSock.close();
- assertTrue("Server Socket should indicate it is closed:", servSock
- .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();
- }
+ // 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();
[... 2559 lines stripped ...]