You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/04/27 13:35:35 UTC
svn commit: r397522 [2/2] -
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java?rev=397522&r1=397521&r2=397522&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java Thu Apr 27 04:35:28 2006
@@ -74,51 +74,41 @@
* @tests java.net.Socket#Socket()
*/
public void test_Constructor() {
- try {
- // 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());
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
+ // 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() {
+ public void test_ConstructorLjava_lang_StringI() throws IOException {
// Test for method java.net.Socket(java.lang.String, int)
- try {
- int sport = startServer("Cons String,I");
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport);
- assertTrue("Failed to create socket", s.getPort() == sport);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
+ int sport = startServer("Cons String,I");
+ s = new Socket(InetAddress.getLocalHost().getHostName(), sport);
+ assertTrue("Failed to create socket", s.getPort() == sport);
}
/**
* @tests java.net.Socket#Socket(java.lang.String, int,
* java.net.InetAddress, int)
*/
- public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI() {
+ public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI()
+ throws IOException {
// Test for method java.net.Socket(java.lang.String, int,
// java.net.InetAddress, int)
- try {
- 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);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
+ 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
@@ -153,8 +143,7 @@
warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
}
- fail("Exception creating 1st socket" + warning + ": "
- + e);
+ fail("Exception creating 1st socket" + warning + ": " + e);
}
boolean exception = false;
try {
@@ -184,7 +173,7 @@
e.printStackTrace();
// check here if InetAddress.getLocalHost() is returning the
- // loopback address.
+ // loopback address.
// if so that is likely the cause of the failure
String warning = "";
try {
@@ -199,8 +188,7 @@
warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
}
- fail(
- "Exception creating 1st socket" + warning + ": " + e);
+ fail("Exception creating 1st socket" + warning + ": " + e);
}
boolean exception = false;
try {
@@ -222,192 +210,154 @@
/**
* @tests java.net.Socket#Socket(java.lang.String, int, boolean)
*/
- public void test_ConstructorLjava_lang_StringIZ() {
+ 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");
- try {
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- true);
- assertTrue("Failed to create socket", s.getPort() == sport);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
+ s = new Socket(InetAddress.getLocalHost().getHostName(), sport, true);
+ assertTrue("Failed to create socket", s.getPort() == sport);
+
+ s = new Socket(InetAddress.getLocalHost().getHostName(), sport, false);
- try {
- s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
- false);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
}
/**
* @tests java.net.Socket#Socket(java.net.InetAddress, int)
*/
- public void test_ConstructorLjava_net_InetAddressI() {
+ public void test_ConstructorLjava_net_InetAddressI() throws IOException {
// Test for method java.net.Socket(java.net.InetAddress, int)
- try {
- int sport = startServer("Cons InetAddress,I");
- s = new Socket(InetAddress.getLocalHost(), sport);
- assertTrue("Failed to create socket", s.getPort() == sport);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
+ 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() {
+ public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI()
+ throws IOException {
// Test for method java.net.Socket(java.net.InetAddress, int,
// java.net.InetAddress, int)
- try {
- 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);
- } catch (Exception e) {
- fail("Exception during Constructor test : " + e.getMessage());
- }
+ 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() {
+ 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");
- try {
- s = new Socket(InetAddress.getLocalHost(), sport, true);
- assertTrue("Failed to create socket", s.getPort() == sport);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
+ s = new Socket(InetAddress.getLocalHost(), sport, true);
+ assertTrue("Failed to create socket", s.getPort() == sport);
+
+ s = new Socket(InetAddress.getLocalHost(), sport, false);
- try {
- s = new Socket(InetAddress.getLocalHost(), sport, false);
- } catch (Exception e) {
- fail("Exception during Constructor test" + e.toString());
- }
}
/**
* @tests java.net.Socket#close()
*/
- public void test_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 {
- 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");
+ s.setSoLinger(false, 100);
} catch (IOException e) {
- fail("unexpected: " + 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() {
+ public void test_getInetAddress() throws IOException {
// Test for method java.net.InetAddress java.net.Socket.getInetAddress()
- try {
- 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()));
- } catch (Exception e) {
- fail("Exception during getInetAddress test : " + e.getMessage());
- }
+ 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() {
+ 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 {
- 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());
- } catch (Exception e) {
- fail("Exception during getInputStream test " + e.toString());
+ do {
+ Thread.sleep(200);
+ } while (!thread.isAlive());
+ } catch (InterruptedException e) {
}
-
try {
- 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) {
- }
+ Thread.sleep(200);
+ } catch (InterruptedException e) {
+ }
+ sock.close();
+ int c = 0;
+ do {
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());
- } catch (IOException e) {
- fail("Unexpected IOException : " + e);
- }
+ if (interrupted) {
+ fail("read interrupted");
+ }
+ if (++c > 4) {
+ fail("read call did not exit");
+ }
+ } while (thread.isAlive());
+
}
/**
@@ -434,151 +384,130 @@
/**
* @tests java.net.Socket#getLocalAddress()
*/
- public void test_getLocalAddress() {
+ public void test_getLocalAddress() throws IOException {
// Test for method java.net.InetAddress
// java.net.Socket.getLocalAddress()
- try {
- 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();
-
- } catch (Exception e) {
- fail("Exception during getLocalAddress test : " + e.getMessage());
+ 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() {
+ public void test_getLocalPort() throws IOException {
// Test for method int java.net.Socket.getLocalPort()
- try {
- 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);
- } catch (Exception e) {
- fail("Exception during getLocalPort test : " + e.getMessage());
- }
+ 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()
*/
- public void test_getOutputStream() {
+ public void test_getOutputStream() throws IOException {
// Test for method java.io.OutputStream
// java.net.Socket.getOutputStream()
- try {
- 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();
- } catch (Exception e) {
- fail("Exception during getOutputStream test" + e.toString());
- }
-
- try {
- 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 {
+ 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 {
- Thread.sleep(200);
- } catch (InterruptedException e) {
- }
- if (++c > 4) {
- fail("read call did not exit");
+ Socket as = ss.accept();
+ ss.close();
+ InputStream in = as.getInputStream();
+ in.read();
+ in.close();
+ } catch (IOException e) {
+ System.out.println(Thread.currentThread() + ": " + e);
}
- } while (thread.isAlive());
-
- boolean exception = false;
+ }
+ };
+ Thread thread = new Thread(runnable, "Socket.getOutputStream");
+ thread.start();
+ int c = 0;
+ do {
try {
- for (int i = 0; i < 400; i++)
- out.write(data);
- } catch (IOException e) {
- exception = true;
+ 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");
}
- out.close();
- assertTrue("write to closed socket did not cause exception",
- exception);
+ } while (thread.isAlive());
+
+ boolean exception = false;
+ try {
+ for (int i = 0; i < 400; i++)
+ out.write(data);
} catch (IOException e) {
- fail("Unexpected IOException : " + e.getMessage());
+ exception = true;
}
+ out.close();
+ assertTrue("write to closed socket did not cause exception", exception);
+
}
/**
* @tests java.net.Socket#getPort()
*/
- public void test_getPort() {
+ public void test_getPort() throws IOException {
// Test for method int java.net.Socket.getPort()
- try {
- 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);
- } catch (Exception e) {
- fail("Exception during getPort test : " + e.getMessage());
- }
+ 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);
}
/**
@@ -652,7 +581,7 @@
/**
* @tests java.net.Socket#getTcpNoDelay()
*/
- public void test_getTcpNoDelay() {
+ public void test_getTcpNoDelay() {
// Test for method boolean java.net.Socket.getTcpNoDelay()
try {
int sport = startServer("SServer getTcpNoDelay");
@@ -693,7 +622,7 @@
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
}
@@ -742,7 +671,7 @@
s.setSoLinger(true, 500);
assertEquals("Set incorrect linger", 500, s.getSoLinger());
ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
- s.setSoLinger(false, 0);
+ s.setSoLinger(false, 0);
} catch (Exception e) {
handleException(e, SO_LINGER);
}
@@ -751,7 +680,7 @@
/**
* @tests java.net.Socket#setSoTimeout(int)
*/
- public void test_setSoTimeoutI() {
+ public void test_setSoTimeoutI() {
// Test for method void java.net.Socket.setSoTimeout(int)
try {
int sport = startServer("SServer seSoTimeoutI");
@@ -787,364 +716,330 @@
/**
* @tests java.net.Socket#toString()
*/
- public void test_toString() {
+ public void test_toString() throws IOException {
// Test for method java.lang.String java.net.Socket.toString()
- try {
- 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() + "]"));
- } catch (Exception e) {
- fail("Exception during toString test : " + e.getMessage());
- }
+ 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() {
- try {
- 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);
-
- int totalBytesRead = 0;
- byte[] myBytes = new byte[100];
- while (theInput.available() > 0) {
- int bytesRead = theInput.read(myBytes, totalBytesRead,
- myBytes.length - totalBytesRead);
- totalBytesRead = totalBytesRead + bytesRead;
- }
+ 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);
+
+ 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("We should have received no data on shutdown input:"
+ + receivedString, 0 == totalBytesRead);
- String receivedString = new String(myBytes, 0, totalBytesRead);
- assertTrue("We should have received no data on shutdown input:"
- + receivedString, 0 == totalBytesRead);
-
- theSocket.close();
- serverSocket.close();
- } catch (Exception e) {
- fail("Got exception during shutdownInput tests"
- + e.toString());
- }
+ theSocket.close();
+ serverSocket.close();
}
/**
* @tests java.net.Socket#shutdownOutput()
*/
- public void test_shutdownOutput() {
- try {
- 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();
+ 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();
+ InputStream theInput = theSocket.getInputStream();
+ OutputStream theOutput = servSock.getOutputStream();
- // shutdown the output
- servSock.shutdownOutput();
+ // 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();
+ // 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) {
- fail("Got exception during shutdownOutput tests"
- + e.toString());
}
+
+ theSocket.close();
+ serverSocket.close();
}
/**
* @tests java.net.Socket#getLocalSocketAddress()
*/
- public void test_getLocalSocketAddress() {
+ public void test_getLocalSocketAddress() throws IOException {
// set up server connect and then validate that we get the right
// response for the local address
- try {
- 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();
+ 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 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();
- } catch (Exception e) {
- fail("Exception during getLocalSocketAddress test: " + e);
+ // 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() {
+ public void test_getRemoteSocketAddress() throws IOException {
// set up server connect and then validate that we get the right
// response for the remote address
- try {
- 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();
+ 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();
+ // 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();
- } catch (Exception e) {
- fail("Exception during getRemoteSocketAddress test: " + e);
- }
}
/**
* @tests java.net.Socket#isBound()
*/
- public void test_isBound() {
- try {
- 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());
- } catch (Exception e) {
- fail("Got exception during isBound tests" + e.toString());
- }
+ 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() {
- try {
- 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();
- } catch (Exception e) {
- fail("Got exception during isConnected tests" + e.toString());
- }
+ 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() {
- try {
- 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());
- } catch (Exception e) {
- fail("Got exception during isClosed tests" + e.toString());
- }
+ 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() {
+ public void test_bindLjava_net_SocketAddress() throws IOException {
class mySocketAddress extends SocketAddress {
@@ -1152,103 +1047,97 @@
}
}
+ // Address we cannot bind to
+ Socket theSocket = new Socket();
try {
- // Address we cannot bind to
- Socket theSocket = new Socket();
- try {
- theSocket
- .bind(new InetSocketAddress(
- InetAddress
- .getByAddress(Support_Configuration.nonLocalAddressBytes),
- Support_PortManager.getNextPort()));
- assertFalse("No exception when binding to bad address:"
- + theSocket.getLocalSocketAddress().toString(), true);
- } 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();
+ theSocket.bind(new InetSocketAddress(InetAddress
+ .getByAddress(Support_Configuration.nonLocalAddressBytes),
+ Support_PortManager.getNextPort()));
+ assertFalse("No exception when binding to bad address:"
+ + theSocket.getLocalSocketAddress().toString(), true);
+ } catch (IOException ex) {
+ }
+ theSocket.close();
- // now check the error conditions
+ // 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));
- // 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);
- assertFalse(
- "No exception binding to address that is not available",
- true);
- } catch (IOException ex) {
- }
- theSocket.close();
- theSocket2.close();
+ // 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);
+ assertFalse(
+ "No exception binding to address that is not available",
+ true);
+ } 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();
- } catch (Exception e) {
- fail("Unexpected exception during bind test : " + e.getMessage());
+ // 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#connect(java.net.SocketAddress)
*/
- public void test_connectLjava_net_SocketAddress() {
+ public void test_connectLjava_net_SocketAddress() throws Exception {
// needed for some tests
class mySocketAddress extends SocketAddress {
@@ -1288,20 +1177,15 @@
SocketAddress invalidType = null;
// byte[] theBytes = {-1,-1,-1,-1};
byte[] theBytes = { 0, 0, 0, 0 };
- try {
- theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber);
- nonConnectableAddress = new InetSocketAddress(InetAddress
- .getByAddress(theBytes), portNumber);
- nonReachableAddress = new InetSocketAddress(InetAddress
- .getByName(Support_Configuration.ResolvedNotExistingHost),
- portNumber);
+ 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();
- } catch (Exception e) {
- fail(
- "Exception when setting up for connect with timeout tests ");
- }
+ invalidType = new mySocketAddress();
try {
theSocket = new Socket();
@@ -1315,8 +1199,7 @@
try {
theSocket = new Socket();
theSocket.connect(invalidType);
- fail(
- "No exception when invalid socket address type passed in: ");
+ fail("No exception when invalid socket address type passed in: ");
} catch (Exception e) {
assertTrue(
"Wrong exception when when invalid socket address type passed in: "
@@ -1340,8 +1223,7 @@
theSocket = new Socket();
theSocket.connect(theAddress);
theSocket.close();
- fail(
- "No exception when connecting to address nobody listening on: ");
+ fail("No exception when connecting to address nobody listening on: ");
} catch (Exception e) {
assertTrue(
"Wrong exception when connecting to address nobody listening on: "
@@ -1349,47 +1231,37 @@
}
// now validate that we can acutally connect when sombody is listening
- try {
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- theSocket.close();
- serverSocket.close();
- } catch (Exception e) {
- fail("Got exception when connect should be successful: "
- + e.toString());
- }
+ 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
- try {
- 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();
- } catch (Exception e) {
- fail("Got exception when connect should be successful: "
- + e.toString());
- }
+ 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
@@ -1401,8 +1273,7 @@
theSocket.connect(theAddress);
theSocket.close();
serverSocket.close();
- fail(
- "No exception when we try to connect on a connected socket: ");
+ fail("No exception when we try to connect on a connected socket: ");
} catch (Exception e) {
assertTrue(
@@ -1421,67 +1292,61 @@
}
// now validate that connected socket can be used to read/write
- try {
- 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();
+ 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));
- 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();
- } catch (Exception e) {
- fail(
- "Got exception trying to validate that connected socket can be used for read/write"
- + e.toString());
- }
+ theSocket.close();
+ serverSocket.close();
}
/**
* @tests java.net.Socket#connect(java.net.SocketAddress, int)
*/
- public void test_connectLjava_net_SocketAddressI() {
+ public void test_connectLjava_net_SocketAddressI() throws Exception {
// needed for some tests
class mySocketAddress extends SocketAddress {
@@ -1548,23 +1413,18 @@
SocketAddress invalidType = null;
byte[] theBytes = { 0, 0, 0, 0 };
- try {
- theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
- portNumber);
- nonConnectableAddress = new InetSocketAddress(InetAddress
- .getByAddress(theBytes), portNumber);
- nonReachableAddress = new InetSocketAddress(InetAddress
- .getByName(Support_Configuration.ResolvedNotExistingHost),
- portNumber);
- // make sure we get another port
- Thread.sleep(7000);
- nonListeningAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- invalidType = new mySocketAddress();
- } catch (Exception e) {
- fail(
- "Exception when setting up for connect with timeout tests ");
- }
+ theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
+ portNumber);
+ nonConnectableAddress = new InetSocketAddress(InetAddress
+ .getByAddress(theBytes), portNumber);
+ nonReachableAddress = new InetSocketAddress(InetAddress
+ .getByName(Support_Configuration.ResolvedNotExistingHost),
+ portNumber);
+ // make sure we get another port
+ Thread.sleep(7000);
+ nonListeningAddress = new InetSocketAddress(InetAddress.getLocalHost(),
+ Support_PortManager.getNextPort());
+ invalidType = new mySocketAddress();
try {
theSocket = new Socket();
@@ -1587,8 +1447,7 @@
try {
theSocket = new Socket();
theSocket.connect(invalidType, 100000);
- fail(
- "No exception when invalid socket address type passed in: ");
+ fail("No exception when invalid socket address type passed in: ");
} catch (Exception e) {
assertTrue(
"Wrong exception when when invalid socket address type passed in: "
@@ -1612,8 +1471,7 @@
theSocket = new Socket();
theSocket.connect(theAddress, 0);
theSocket.close();
- fail(
- "No timeout:No exception when connecting to address nobody listening on: ");
+ fail("No timeout:No exception when connecting to address nobody listening on: ");
} catch (Exception e) {
assertTrue(
"No timeout:Wrong exception when connecting to address nobody listening on: "
@@ -1621,18 +1479,12 @@
}
// now validate that we can acutally connect when sombody is listening
- try {
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress, 0);
- theSocket.close();
- serverSocket.close();
- } catch (Exception e) {
- fail(
- "No timeout:Got exception when connect should be successful: "
- + e.toString());
- }
+ theSocket = new Socket();
+ serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress, 0);
+ theSocket.close();
+ serverSocket.close();
// now validate that we get a connect exception if we try to connect to
// an address on which nobody is listening
@@ -1640,8 +1492,7 @@
theSocket = new Socket();
theSocket.connect(nonListeningAddress, 100000);
theSocket.close();
- fail(
- "No exception when connecting to address nobody listening on: ");
+ fail("No exception when connecting to address nobody listening on: ");
} catch (Exception e) {
assertTrue(
"Wrong exception when connecting to address nobody listening on: "
@@ -1655,8 +1506,7 @@
theSocket = new Socket();
theSocket.connect(nonReachableAddress, 200);
theSocket.close();
- fail(
- "No interrupted exception when connecting to address nobody listening on with short timeout 200: ");
+ fail("No interrupted exception when connecting to address nobody listening on with short timeout 200: ");
} catch (Exception e) {
assertTrue(
"Wrong exception when connecting to address nobody listening on with short timeout 200: "
@@ -1671,8 +1521,7 @@
theSocket = new Socket();
theSocket.connect(nonReachableAddress, 40);
theSocket.close();
- fail(
- "No interrupted exception when connecting to address nobody listening on with short timeout 40: ");
+ fail("No interrupted exception when connecting to address nobody listening on with short timeout 40: ");
} catch (Exception e) {
assertTrue(
"Wrong exception when connecting to address nobody listening on with short timeout 40: "
@@ -1681,36 +1530,31 @@
}
// now validate that we can acutally connect when sombody is listening
- try {
- new InetSocketAddress(InetAddress.getLocalHost(),
- Support_PortManager.getNextPort());
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress, 100000);
-
- // 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();
- } catch (Exception e) {
- fail("Got exception when connect should be successful: "
- + e.toString());
- }
+ new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket = new Socket();
+ serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress, 100000);
+
+ // 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
@@ -1724,8 +1568,7 @@
theSocket.connect(theAddress, 100000);
theSocket.close();
serverSocket.close();
- fail(
- "No exception when we try to connect on a connected socket: ");
+ fail("No exception when we try to connect on a connected socket: ");
} catch (Exception e) {
assertTrue(
@@ -1744,168 +1587,145 @@
}
// now validate that connected socket can be used to read/write
- try {
- new InetSocketAddress(InetAddress.getLocalHost(),
- Support_PortManager.getNextPort());
- theSocket = new Socket();
- serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress, 100000);
- 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);
+ new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket = new Socket();
+ serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress, 100000);
+ 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();
+
+ totalBytesRead = 0;
+ myBytes = new byte[100];
+ Thread.sleep(1000);
+ 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));
- 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();
-
- totalBytesRead = 0;
- myBytes = new byte[100];
- Thread.sleep(1000);
- 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();
- } catch (Exception e) {
- fail(
- "Got exception trying to validate that connected socket can be used for read/write"
- + e.toString());
- }
+ theSocket.close();
+ serverSocket.close();
// now try to set options while we are connecting
- try {
- theSocket = new Socket();
- SocketConnector connector = new SocketConnector(5000, theSocket,
- nonReachableAddress);
- connector.start();
- theSocket.setSoTimeout(100);
- Thread.sleep(10);
- assertEquals("Socket option not set during connect: 10 ", 100, theSocket
- .getSoTimeout());
- Thread.sleep(50);
- theSocket.setSoTimeout(200);
- assertEquals("Socket option not set during connect: 50 ", 200, theSocket
- .getSoTimeout());
- Thread.sleep(5000);
- theSocket.close();
- } catch (Exception e) {
- fail(
- "Unexpected exception while testing set/get socket options during connect:"
- + e.toString());
- }
+ theSocket = new Socket();
+ SocketConnector connector = new SocketConnector(5000, theSocket,
+ nonReachableAddress);
+ connector.start();
+ theSocket.setSoTimeout(100);
+ Thread.sleep(10);
+ assertEquals("Socket option not set during connect: 10 ", 100,
+ theSocket.getSoTimeout());
+ Thread.sleep(50);
+ theSocket.setSoTimeout(200);
+ assertEquals("Socket option not set during connect: 50 ", 200,
+ theSocket.getSoTimeout());
+ Thread.sleep(5000);
+ theSocket.close();
}
/**
* @tests java.net.Socket#isInputShutdown()
*/
- public void test_isInputShutdown() {
- try {
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- Socket theSocket = new Socket();
- ServerSocket serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- Socket servSock = serverSocket.accept();
- InputStream theInput = theSocket.getInputStream();
- OutputStream theOutput = servSock.getOutputStream();
-
- // make sure we get the right answer with newly connected socket
- assertFalse(
- "Socket indicated input shutdown when it should not have",
- theSocket.isInputShutdown());
-
- // shutdown the output
- theSocket.shutdownInput();
-
- // make sure we get the right answer once it is shut down
- assertTrue(
- "Socket indicated input was NOT shutdown when it should have been",
- theSocket.isInputShutdown());
-
- theSocket.close();
- serverSocket.close();
-
- // make sure we get the right answer for closed sockets
- assertFalse(
- "Socket indicated input was shutdown when socket was closed",
- servSock.isInputShutdown());
+ public void test_isInputShutdown() throws IOException {
+ InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort());
+ Socket theSocket = new Socket();
+ ServerSocket serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress);
+ Socket servSock = serverSocket.accept();
+ InputStream theInput = theSocket.getInputStream();
+ OutputStream theOutput = servSock.getOutputStream();
+
+ // make sure we get the right answer with newly connected socket
+ assertFalse("Socket indicated input shutdown when it should not have",
+ theSocket.isInputShutdown());
+
+ // shutdown the output
+ theSocket.shutdownInput();
+
+ // make sure we get the right answer once it is shut down
+ assertTrue(
+ "Socket indicated input was NOT shutdown when it should have been",
+ theSocket.isInputShutdown());
+
+ theSocket.close();
+ serverSocket.close();
+
+ // make sure we get the right answer for closed sockets
+ assertFalse(
+ "Socket indicated input was shutdown when socket was closed",
+ servSock.isInputShutdown());
- } catch (Exception e) {
- fail("Got exception during isInputShutdown tests"
- + e.toString());
- }
}
/**
* @tests java.net.Socket#isOutputShutdown()
*/
- public void test_isOutputShutdown() {
- try {
- InetSocketAddress theAddress = new InetSocketAddress(InetAddress
- .getLocalHost(), Support_PortManager.getNextPort());
- Socket theSocket = new Socket();
- ServerSocket serverSocket = new ServerSocket();
- serverSocket.bind(theAddress);
- theSocket.connect(theAddress);
- Socket servSock = serverSocket.accept();
- InputStream theInput = theSocket.getInputStream();
- OutputStream theOutput = servSock.getOutputStream();
-
- // make sure we get the right answer with newly connected socket
- assertFalse(
- "Socket indicated output shutdown when it should not have",
- servSock.isOutputShutdown());
-
- // shutdown the output
- servSock.shutdownOutput();
-
- // make sure we get the right answer once it is shut down
- assertTrue(
- "Socket indicated output was NOT shutdown when it should have been",
- servSock.isOutputShutdown());
-
- theSocket.close();
- serverSocket.close();
+ public void test_isOutputShutdown() throws IOException {
+ InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort());
+ Socket theSocket = new Socket();
+ ServerSocket serverSocket = new ServerSocket();
+ serverSocket.bind(theAddress);
+ theSocket.connect(theAddress);
+ Socket servSock = serverSocket.accept();
+ InputStream theInput = theSocket.getInputStream();
+ OutputStream theOutput = servSock.getOutputStream();
+
+ // make sure we get the right answer with newly connected socket
+ assertFalse("Socket indicated output shutdown when it should not have",
+ servSock.isOutputShutdown());
+
+ // shutdown the output
+ servSock.shutdownOutput();
+
+ // make sure we get the right answer once it is shut down
+ assertTrue(
+ "Socket indicated output was NOT shutdown when it should have been",
+ servSock.isOutputShutdown());
+
+ theSocket.close();
+ serverSocket.close();
+
+ // make sure we get the right answer for closed sockets
+ assertFalse(
+ "Socket indicated output was output shutdown when the socket was closed",
+ theSocket.isOutputShutdown());
- // make sure we get the right answer for closed sockets
- assertFalse(
- "Socket indicated output was output shutdown when the socket was closed",
- theSocket.isOutputShutdown());
- } catch (Exception e) {
- fail("Got exception during isOutputShutdown tests"
- + e.toString());
- }
}
/**
@@ -1954,19 +1774,16 @@
// addresses we bind to is an IPv6 address and we
// are therefore using the IPv6 stack.
!((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
- fail(
- "No exception when setReuseAddress is false and we bind:"
- + theLocalAddress.toString() + ":"
- + theOtherLocalAddress.toString());
+ fail("No exception when setReuseAddress is false and we bind:"
+ + theLocalAddress.toString()
+ + ":"
+ + theOtherLocalAddress.toString());
}
} catch (IOException ex) {
if ((platform.startsWith("Linux"))
|| ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
- fail(
- "Got unexpected exception when binding with setReuseAddress false on windows platform:"
- + theAddress.toString()
- + ":"
- + ex.toString());
+ fail("Got unexpected exception when binding with setReuseAddress false on windows platform:"
+ + theAddress.toString() + ":" + ex.toString());
}
}
theSocket.close();
@@ -1990,9 +1807,8 @@
theSocket2.bind(theOtherLocalAddress);
theSocket2.close();
} catch (IOException ex) {
- fail(
- "IOException when setReuseAddress is true and we bind :"
- + ex.toString());
+ fail("IOException when setReuseAddress is true and we bind :"
+ + ex.toString());
}
theSocket.close();
serverSocket.close();
@@ -2014,19 +1830,16 @@
theSocket2.close();
if ((!platform.startsWith("Linux"))
&& ((!platform.startsWith("Windows")) || !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
- fail(
- "No exception when setReuseAddress is default and we bind:"
- + theLocalAddress.toString() + ":"
- + theOtherLocalAddress.toString());
+ fail("No exception when setReuseAddress is default and we bind:"
+ + theLocalAddress.toString()
+ + ":"
+ + theOtherLocalAddress.toString());
}
} catch (IOException ex) {
if ((platform.startsWith("Linux"))
|| ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
- fail(
- "Got unexpected exception when binding with setReuseAddress default on windows platform:"
- + theAddress.toString()
- + ":"
- + ex.toString());
+ fail("Got unexpected exception when binding with setReuseAddress default on windows platform:"
+ + theAddress.toString() + ":" + ex.toString());
}
}
theSocket.close();
@@ -2170,7 +1983,7 @@
public void test_getChannel() throws Exception {
assertNull(new Socket().getChannel());
}
-
+
/**
* @tests java.net.Socket#sendUrgentData(int)
*/
@@ -2417,8 +2230,8 @@
receivedString = new String(myBytes, 0, totalBytesRead);
// depending on the platform we may get the previously sent
- // urgent data or not (examples windows-yes, Linux-no).
- // So accept either so long as we get the urgent data from
+ // urgent data or not (examples windows-yes, Linux-no).
+ // So accept either so long as we get the urgent data from
// when it was on.
assertTrue(
"Did not get urgent data when turning on/off(3) GOT:"
@@ -2478,11 +2291,76 @@
}
}
+ /*
+ * @tests java.net.Socket#setPerformancePreference()
+ */
+ public void test_setPerformancePreference_Int_Int_Int() throws Exception {
+ Socket theSocket = new Socket();
+ theSocket.setPerformancePreferences(1, 1, 1);
+ }
+
+ /**
+ * @tests java.net.Socket#Socket(Proxy)
+ */
+ public void test_ConstructorLjava_net_Proxy_Exception() {
+
+ 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 {
+ new Socket(proxy1);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ 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 {
+ 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 {
+ System.setSecurityManager(originalSecurityManager);
+ }
+
+ }
+
/**
* Sets up the fixture, for example, open a network connection. This method
* is called before a test is executed.
+ *
+ * @throws Exception
*/
- protected void setUp() {
+ protected void setUp() throws Exception {
+ super.setUp();
}
/**
@@ -2507,96 +2385,26 @@
}
}
- /*
- * @tests java.net.Socket#setPerformancePreference()
- */
- public void test_setPerformancePreference_Int_Int_Int() throws Exception {
- Socket theSocket = new Socket();
- theSocket.setPerformancePreferences(1,1,1);
- }
-
- /**
- * @tests java.net.Socket#Socket(Proxy)
- */
- public void test_ConstructorLjava_net_Proxy_Exception() {
-
- 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 {
- new Socket(proxy1);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
- try {
- new Socket(proxy2);
- } catch (IllegalArgumentException e) {
- fail("should not throw IllegalArgumentException");
- }
-
- try {
- new Socket(Proxy.NO_PROXY);
- } catch (IllegalArgumentException e) {
- fail("should not throw IllegalArgumentException");
- }
-
- // SecurityException test
- SecurityManager originalSecurityManager = System.getSecurityManager();
- try {
- 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 {
- System.setSecurityManager(originalSecurityManager);
- }
-
- }
-
static 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");
- }
- }
-
[... 31 lines stripped ...]