diff options
Diffstat (limited to 'harmony-tests')
3 files changed, 458 insertions, 103 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java index 0b8b26f..731e907 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/DatagramChannelTest.java @@ -25,6 +25,7 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketException; import java.nio.ByteBuffer; +import java.nio.channels.AlreadyBoundException; import java.nio.channels.AsynchronousCloseException; import java.nio.channels.ClosedChannelException; import java.nio.channels.DatagramChannel; @@ -71,11 +72,11 @@ public class DatagramChannelTest extends TestCase { channel1 = DatagramChannel.open(); channel2 = DatagramChannel.open(); - channel1.socket().bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); - channel2.socket().bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); + channel1.bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); + channel2.bind(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); - channel1Address = (InetSocketAddress) channel1.socket().getLocalSocketAddress(); - channel2Address = (InetSocketAddress) channel2.socket().getLocalSocketAddress(); + channel1Address = (InetSocketAddress) channel1.getLocalAddress(); + channel2Address = (InetSocketAddress) channel2.getLocalAddress(); this.datagramSocket1 = new DatagramSocket(0, Inet6Address.LOOPBACK); this.datagramSocket2 = new DatagramSocket(0, Inet6Address.LOOPBACK); @@ -379,7 +380,8 @@ public class DatagramChannelTest extends TestCase { try { s.connect(datagramSocket2Address); fail(); - } catch (IllegalStateException expected) {} + } catch (IllegalStateException expected) { + } assertTrue(this.channel1.isConnected()); assertTrue(s.isConnected()); @@ -1253,8 +1255,7 @@ public class DatagramChannelTest extends TestCase { Thread.sleep(TIME_UNIT); channel2.send(ByteBuffer.wrap(str.getBytes()), datagramSocket1Address); fail("Should throw SocketException!"); - } catch (SocketException e) { - //expected + } catch (SocketException expected) { } } @@ -1942,7 +1943,7 @@ public class DatagramChannelTest extends TestCase { sourceArray[i] = (byte) i; } - this.channel1.connect(channel1.socket().getLocalSocketAddress()); + this.channel1.connect(channel1.getLocalAddress()); this.channel2.connect(datagramSocket1Address); // the different addr // write @@ -1972,7 +1973,7 @@ public class DatagramChannelTest extends TestCase { assertEquals(CAPACITY_NORMAL, dc.write(sourceBuf)); // Connect channel2 after data has been written. - channel2.connect(dc.socket().getLocalSocketAddress()); + channel2.connect(dc.getLocalAddress()); // read ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); @@ -2164,7 +2165,7 @@ public class DatagramChannelTest extends TestCase { assertEquals(CAPACITY_NORMAL, dc.write(sourceBuf)); // Connect channel2 after data has been written. - channel2.connect(dc.socket().getLocalSocketAddress()); + channel2.connect(dc.getLocalAddress()); // read ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); @@ -2382,8 +2383,8 @@ public class DatagramChannelTest extends TestCase { public void test_bounded_harmony6493() throws IOException { DatagramChannel server = DatagramChannel.open(); InetSocketAddress addr = new InetSocketAddress("localhost", 0); - server.socket().bind(addr); - SocketAddress boundedAddress = server.socket().getLocalSocketAddress(); + server.bind(addr); + SocketAddress boundedAddress = server.getLocalAddress(); DatagramChannel client = DatagramChannel.open(); ByteBuffer sent = ByteBuffer.allocate(1024); @@ -2395,4 +2396,146 @@ public class DatagramChannelTest extends TestCase { server.close(); client.close(); } + + public void test_bind_null() throws Exception { + DatagramChannel dc = DatagramChannel.open(); + try { + assertNull(dc.getLocalAddress()); + + dc.bind(null); + + InetSocketAddress localAddress = (InetSocketAddress) dc.getLocalAddress(); + assertTrue(localAddress.getAddress().isAnyLocalAddress()); + assertTrue(localAddress.getPort() > 0); + } finally { + dc.close(); + } + } + + public void test_bind_failure() throws Exception { + DatagramChannel dc = DatagramChannel.open(); + try { + // Bind to a local address that is in use + dc.bind(channel1Address); + fail(); + } catch (IOException expected) { + } finally { + dc.close(); + } + } + + public void test_bind_closed() throws Exception { + DatagramChannel dc = DatagramChannel.open(); + dc.close(); + + try { + dc.bind(null); + fail(); + } catch (ClosedChannelException expected) { + } finally { + dc.close(); + } + } + + public void test_bind_twice() throws Exception { + DatagramChannel dc = DatagramChannel.open(); + dc.bind(null); + + try { + dc.bind(null); + fail(); + } catch (AlreadyBoundException expected) { + } finally { + dc.close(); + } + } + + public void test_bind_explicitPort() throws Exception { + InetSocketAddress address = (InetSocketAddress) channel1.getLocalAddress(); + assertTrue(address.getPort() > 0); + + DatagramChannel dc = DatagramChannel.open(); + // Allow the socket to bind to a port we know is already in use. + dc.socket().setReuseAddress(true); + InetSocketAddress bindAddress = new InetSocketAddress("localhost", address.getPort()); + dc.bind(bindAddress); + + InetSocketAddress boundAddress = (InetSocketAddress) dc.getLocalAddress(); + assertEquals(bindAddress.getHostName(), boundAddress.getHostName()); + assertEquals(bindAddress.getPort(), boundAddress.getPort()); + + dc.close(); + channel1.close(); + } + + /** Checks that the SocketChannel and associated Socket agree on the socket state. */ + public void test_bind_socketSync() throws IOException { + DatagramChannel dc = DatagramChannel.open(); + assertNull(dc.getLocalAddress()); + + DatagramSocket socket = dc.socket(); + assertNull(socket.getLocalSocketAddress()); + assertFalse(socket.isBound()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + dc.bind(bindAddr); + + InetSocketAddress actualAddr = (InetSocketAddress) dc.getLocalAddress(); + assertEquals(actualAddr, socket.getLocalSocketAddress()); + assertEquals(bindAddr.getHostName(), actualAddr.getHostName()); + assertTrue(socket.isBound()); + assertFalse(socket.isConnected()); + assertFalse(socket.isClosed()); + + dc.close(); + + assertFalse(dc.isOpen()); + assertTrue(socket.isClosed()); + } + + /** + * Checks that the SocketChannel and associated Socket agree on the socket state, even if + * the Socket object is requested/created after bind(). + */ + public void test_bind_socketSyncAfterBind() throws IOException { + DatagramChannel dc = DatagramChannel.open(); + assertNull(dc.getLocalAddress()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + dc.bind(bindAddr); + + // Socket creation after bind(). + DatagramSocket socket = dc.socket(); + InetSocketAddress actualAddr = (InetSocketAddress) dc.getLocalAddress(); + assertEquals(actualAddr, socket.getLocalSocketAddress()); + assertEquals(bindAddr.getHostName(), actualAddr.getHostName()); + assertTrue(socket.isBound()); + assertFalse(socket.isConnected()); + assertFalse(socket.isClosed()); + + dc.close(); + + assertFalse(dc.isOpen()); + assertTrue(socket.isClosed()); + } + + public void test_getLocalSocketAddress_afterClose() throws IOException { + DatagramChannel dc = DatagramChannel.open(); + assertNull(dc.getLocalAddress()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + dc.bind(bindAddr); + + assertNotNull(dc.getLocalAddress()); + + dc.close(); + + assertFalse(dc.isOpen()); + + try { + dc.getLocalAddress(); + fail(); + } catch (ClosedChannelException expected) { + } + } } diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java index 828ab30..b417adc 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/ServerSocketChannelTest.java @@ -24,6 +24,7 @@ import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; +import java.nio.channels.AlreadyBoundException; import java.nio.channels.AsynchronousCloseException; import java.nio.channels.ClosedChannelException; import java.nio.channels.IllegalBlockingModeException; @@ -108,6 +109,157 @@ public class ServerSocketChannelTest extends TestCase { } // ------------------------------------------------------------------- + // Tests for bind() + // ------------------------------------------------------------------- + + public void test_bind_null() throws Exception { + ServerSocketChannel ssc = ServerSocketChannel.open(); + try { + assertNull(ssc.getLocalAddress()); + + ssc.bind(null); + + InetSocketAddress localAddress = (InetSocketAddress) ssc.getLocalAddress(); + assertTrue(localAddress.getAddress().isAnyLocalAddress()); + assertTrue(localAddress.getPort() > 0); + } finally { + ssc.close(); + } + } + + public void test_bind_failure() throws Exception { + ServerSocketChannel portHog = ServerSocketChannel.open(); + portHog.bind(null); + + ServerSocketChannel ssc = ServerSocketChannel.open(); + try { + // Bind to a local address that is in use + ssc.bind(portHog.getLocalAddress()); + fail(); + } catch (IOException expected) { + } finally { + ssc.close(); + portHog.close(); + } + } + + public void test_bind_closed() throws Exception { + ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.close(); + + try { + ssc.bind(null); + fail(); + } catch (ClosedChannelException expected) { + } finally { + ssc.close(); + } + } + + public void test_bind_twice() throws Exception { + ServerSocketChannel ssc = ServerSocketChannel.open(); + ssc.bind(null); + + try { + ssc.bind(null); + fail(); + } catch (AlreadyBoundException expected) { + } finally { + ssc.close(); + } + } + + public void test_bind_explicitPort() throws Exception { + ServerSocketChannel portPickingChannel = ServerSocketChannel.open(); + // Have the OS find a free port. + portPickingChannel.bind(null); + + InetSocketAddress address = (InetSocketAddress) portPickingChannel.getLocalAddress(); + assertTrue(address.getPort() > 0); + portPickingChannel.close(); + + // There is a risk of flakiness here if the port is allocated to something else between + // close() and bind(). + ServerSocketChannel ssc = ServerSocketChannel.open(); + InetSocketAddress bindAddress = new InetSocketAddress("localhost", address.getPort()); + ssc.bind(bindAddress); + + InetSocketAddress boundAddress = (InetSocketAddress) ssc.getLocalAddress(); + assertEquals(bindAddress.getHostName(), boundAddress.getHostName()); + assertEquals(bindAddress.getPort(), boundAddress.getPort()); + + ssc.close(); + } + + public void test_bind_socketSync() throws IOException { + ServerSocketChannel ssc = ServerSocketChannel.open(); + assertNull(ssc.getLocalAddress()); + + ServerSocket socket = ssc.socket(); + assertNull(socket.getLocalSocketAddress()); + assertFalse(socket.isBound()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + ssc.bind(bindAddr); + + InetSocketAddress actualAddr = (InetSocketAddress) ssc.getLocalAddress(); + assertEquals(actualAddr, socket.getLocalSocketAddress()); + assertEquals(bindAddr.getHostName(), actualAddr.getHostName()); + assertTrue(socket.isBound()); + assertFalse(socket.isClosed()); + + ssc.close(); + + assertFalse(ssc.isOpen()); + assertTrue(socket.isClosed()); + } + + public void test_bind_socketSyncAfterBind() throws IOException { + ServerSocketChannel ssc = ServerSocketChannel.open(); + assertNull(ssc.getLocalAddress()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + ssc.bind(bindAddr); + + // Socket creation after bind(). + ServerSocket socket = ssc.socket(); + InetSocketAddress actualAddr = (InetSocketAddress) ssc.getLocalAddress(); + assertEquals(actualAddr, socket.getLocalSocketAddress()); + assertEquals(bindAddr.getHostName(), actualAddr.getHostName()); + assertTrue(socket.isBound()); + assertFalse(socket.isClosed()); + + ssc.close(); + + assertFalse(ssc.isOpen()); + assertTrue(socket.isClosed()); + } + + // ------------------------------------------------------------------- + // Test for getLocalSocketAddress() + // ------------------------------------------------------------------- + + public void test_getLocalSocketAddress_afterClose() throws IOException { + ServerSocketChannel ssc = ServerSocketChannel.open(); + assertNull(ssc.getLocalAddress()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + ssc.bind(bindAddr); + + assertNotNull(ssc.getLocalAddress()); + + ssc.close(); + + assertFalse(ssc.isOpen()); + + try { + ssc.getLocalAddress(); + fail(); + } catch (ClosedChannelException expected) { + } + } + + // ------------------------------------------------------------------- // Test for socket() // ------------------------------------------------------------------- @@ -235,8 +387,7 @@ public class ServerSocketChannelTest extends TestCase { public void testAccept_Block_NoConnect() throws IOException { assertTrue(this.serverChannel.isBlocking()); - ServerSocket gotSocket = this.serverChannel.socket(); - gotSocket.bind(null); + serverChannel.bind(null); // blocking mode , will block and wait for ever... // so must close the server channel with another thread. new Thread() { @@ -259,8 +410,7 @@ public class ServerSocketChannelTest extends TestCase { } public void testAccept_NonBlock_NoConnect() throws IOException { - ServerSocket gotSocket = this.serverChannel.socket(); - gotSocket.bind(null); + this.serverChannel.bind(null); this.serverChannel.configureBlocking(false); // non-blocking mode , will immediately return assertNull(this.serverChannel.accept()); @@ -270,13 +420,13 @@ public class ServerSocketChannelTest extends TestCase { * @tests ServerSocketChannel#accept().socket() */ public void test_read_Blocking_RealData() throws IOException { - serverChannel.socket().bind(null); + serverChannel.bind(null); ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); for (int i = 0; i < CAPACITY_NORMAL; i++) { buf.put((byte) i); } - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); Socket serverSocket = serverChannel.accept().socket(); InputStream in = serverSocket.getInputStream(); buf.flip(); @@ -309,13 +459,13 @@ public class ServerSocketChannelTest extends TestCase { */ public void test_read_NonBlocking_RealData() throws Exception { serverChannel.configureBlocking(false); - serverChannel.socket().bind(null); + serverChannel.bind(null); ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); for (int i = 0; i < CAPACITY_NORMAL; i++) { buf.put((byte) i); } buf.flip(); - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); Socket serverSocket = serverChannel.accept().socket(); InputStream in = serverSocket.getInputStream(); clientChannel.write(buf); @@ -328,14 +478,13 @@ public class ServerSocketChannelTest extends TestCase { */ public void test_write_Blocking_RealData() throws IOException { assertTrue(serverChannel.isBlocking()); - ServerSocket serverSocket = serverChannel.socket(); - serverSocket.bind(null); + serverChannel.bind(null); byte[] writeContent = new byte[CAPACITY_NORMAL]; for (int i = 0; i < writeContent.length; i++) { writeContent[i] = (byte) i; } - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); Socket socket = serverChannel.accept().socket(); OutputStream out = socket.getOutputStream(); out.write(writeContent); @@ -350,14 +499,13 @@ public class ServerSocketChannelTest extends TestCase { */ public void test_write_NonBlocking_RealData() throws Exception { serverChannel.configureBlocking(false); - ServerSocket serverSocket = serverChannel.socket(); - serverSocket.bind(null); + serverChannel.bind(null); byte[] writeContent = new byte[CAPACITY_NORMAL]; for (int i = 0; i < CAPACITY_NORMAL; i++) { writeContent[i] = (byte) i; } - clientChannel.connect(serverSocket.getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); Socket clientSocket = serverChannel.accept().socket(); OutputStream out = clientSocket.getOutputStream(); out.write(writeContent); @@ -371,13 +519,13 @@ public class ServerSocketChannelTest extends TestCase { */ public void test_read_LByteBuffer_Blocking_ReadWriteRealLargeData() throws IOException, InterruptedException { - serverChannel.socket().bind(null); + serverChannel.bind(null); ByteBuffer buf = ByteBuffer.allocate(CAPACITY_64KB); for (int i = 0; i < CAPACITY_64KB; i++) { buf.put((byte) i); } buf.flip(); - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); WriteChannelThread writeThread = new WriteChannelThread(clientChannel, buf); writeThread.start(); Socket socket = serverChannel.accept().socket(); @@ -416,13 +564,13 @@ public class ServerSocketChannelTest extends TestCase { public void test_read_LByteBuffer_NonBlocking_ReadWriteRealLargeData() throws Exception { serverChannel.configureBlocking(false); - serverChannel.socket().bind(null); + serverChannel.bind(null); ByteBuffer buf = ByteBuffer.allocate(CAPACITY_64KB); for (int i = 0; i < CAPACITY_64KB; i++) { buf.put((byte) i); } buf.flip(); - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); WriteChannelThread writeThread = new WriteChannelThread(clientChannel, buf); writeThread.start(); Socket socket = serverChannel.accept().socket(); @@ -441,12 +589,12 @@ public class ServerSocketChannelTest extends TestCase { public void test_write_LByteBuffer_NonBlocking_ReadWriteRealLargeData() throws Exception { serverChannel.configureBlocking(false); - serverChannel.socket().bind(null); + serverChannel.bind(null); byte[] writeContent = new byte[CAPACITY_64KB]; for (int i = 0; i < writeContent.length; i++) { writeContent[i] = (byte) i; } - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); Socket socket = serverChannel.accept().socket(); WriteSocketThread writeThread = new WriteSocketThread(socket, writeContent); writeThread.start(); @@ -484,12 +632,12 @@ public class ServerSocketChannelTest extends TestCase { */ public void test_write_LByteBuffer_Blocking_ReadWriteRealLargeData() throws Exception { - serverChannel.socket().bind(null); + serverChannel.bind(null); byte[] writeContent = new byte[CAPACITY_64KB]; for (int i = 0; i < writeContent.length; i++) { writeContent[i] = (byte) i; } - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + clientChannel.connect(serverChannel.getLocalAddress()); Socket socket = serverChannel.accept().socket(); WriteSocketThread writeThread = new WriteSocketThread(socket, writeContent); writeThread.start(); @@ -531,9 +679,9 @@ public class ServerSocketChannelTest extends TestCase { final int SO_TIMEOUT = 10; ServerSocketChannel sc = ServerSocketChannel.open(); try { - ServerSocket ss = sc.socket(); - ss.bind(null); + sc.bind(null); sc.configureBlocking(false); + ServerSocket ss = sc.socket(); ss.setSoTimeout(SO_TIMEOUT); SocketChannel client = sc.accept(); // non blocking mode, returns null since there are no pending connections. @@ -556,15 +704,13 @@ public class ServerSocketChannelTest extends TestCase { try { gotSocket.accept(); fail("Should throw an IllegalBlockingModeException"); - } catch (IllegalBlockingModeException e) { - // expected + } catch (IllegalBlockingModeException expected) { } serverChannel.close(); try { gotSocket.accept(); fail("Should throw an IllegalBlockingModeException"); - } catch (IllegalBlockingModeException e) { - // expected + } catch (IllegalBlockingModeException expected) { } } @@ -578,15 +724,13 @@ public class ServerSocketChannelTest extends TestCase { try { gotSocket.accept(); fail("Should throw an IllegalBlockingModeException"); - } catch (IllegalBlockingModeException e) { - // expected + } catch (IllegalBlockingModeException expected) { } serverChannel.close(); try { gotSocket.accept(); fail("Should throw an IllegalBlockingModeException"); - } catch (IllegalBlockingModeException e) { - // expected + } catch (IllegalBlockingModeException expected) { } } @@ -596,20 +740,18 @@ public class ServerSocketChannelTest extends TestCase { public void test_socket_accept_Nonblocking_Bound() throws IOException { // regression test for Harmony-748 serverChannel.configureBlocking(false); + serverChannel.bind(null); ServerSocket gotSocket = serverChannel.socket(); - gotSocket.bind(null); try { gotSocket.accept(); fail("Should throw an IllegalBlockingModeException"); - } catch (IllegalBlockingModeException e) { - // expected + } catch (IllegalBlockingModeException expected) { } serverChannel.close(); try { gotSocket.accept(); fail("Should throw a ClosedChannelException"); - } catch (ClosedChannelException e) { - // expected + } catch (ClosedChannelException expected) { } } @@ -619,22 +761,20 @@ public class ServerSocketChannelTest extends TestCase { public void test_socket_accept_Blocking_Bound() throws IOException { // regression test for Harmony-748 serverChannel.configureBlocking(true); - ServerSocket gotSocket = serverChannel.socket(); - gotSocket.bind(null); + serverChannel.bind(null); serverChannel.close(); try { - gotSocket.accept(); + serverChannel.socket().accept(); fail("Should throw a ClosedChannelException"); - } catch (ClosedChannelException e) { - // expected + } catch (ClosedChannelException expected) { } } /** * Regression test for HARMONY-4961 */ public void test_socket_getLocalPort() throws IOException { - serverChannel.socket().bind(null); - clientChannel.connect(serverChannel.socket().getLocalSocketAddress()); + serverChannel.bind(null); + clientChannel.connect(serverChannel.getLocalAddress()); SocketChannel myChannel = serverChannel.accept(); int port = myChannel.socket().getLocalPort(); assertEquals(serverChannel.socket().getLocalPort(), port); @@ -648,7 +788,7 @@ public class ServerSocketChannelTest extends TestCase { */ public void test_accept_configureBlocking() throws Exception { InetSocketAddress localAddr = new InetSocketAddress("localhost", 0); - serverChannel.socket().bind(localAddr); + serverChannel.bind(localAddr); // configure the channel non-blocking // when it is accepting in main thread @@ -667,8 +807,7 @@ public class ServerSocketChannelTest extends TestCase { try { serverChannel.accept(); fail("should throw AsynchronousCloseException"); - } catch (AsynchronousCloseException e) { - // expected + } catch (AsynchronousCloseException expected) { } serverChannel.close(); } diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java index 2d52bc5..52dff79 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/channels/SocketChannelTest.java @@ -30,6 +30,7 @@ import java.net.SocketAddress; import java.net.SocketException; import java.nio.Buffer; import java.nio.ByteBuffer; +import java.nio.channels.AlreadyBoundException; import java.nio.channels.AlreadyConnectedException; import java.nio.channels.ClosedChannelException; import java.nio.channels.ConnectionPendingException; @@ -153,6 +154,87 @@ public class SocketChannelTest extends TestCase { } } + public void testBind_Null() throws Exception { + assertNull(channel1.getLocalAddress()); + + channel1.bind(null); + + InetSocketAddress localAddress = (InetSocketAddress) channel1.getLocalAddress(); + assertTrue(localAddress.getAddress().isAnyLocalAddress()); + assertTrue(localAddress.getPort() > 0); + } + + public void testBind_Failure() throws Exception { + assertNull(channel1.getLocalAddress()); + + try { + // Bind to a local address that is in use + channel1.bind(localAddr1); + fail(); + } catch (IOException expected) { + } + } + + public void testBind_Closed() throws Exception { + channel1.close(); + + try { + channel1.bind(null); + fail(); + } catch (ClosedChannelException expected) { + } + } + + public void testBind_Twice() throws Exception { + channel1.bind(null); + + try { + channel1.bind(null); + fail(); + } catch (AlreadyBoundException expected) { + } + } + + public void testBind_explicitPort() throws Exception { + ServerSocketChannel portPickingChannel = ServerSocketChannel.open(); + // Have the OS find a free port. + portPickingChannel.bind(null); + InetSocketAddress address = (InetSocketAddress) portPickingChannel.getLocalAddress(); + assertTrue(address.getPort() > 0); + portPickingChannel.close(); + + // There is a risk of flakiness here if the port is allocated to something else between + // close() and bind(). + InetSocketAddress bindAddress = new InetSocketAddress("localhost", address.getPort()); + // Allow the socket to bind to a port we know is already in use. + channel1.socket().setReuseAddress(true); + channel1.bind(bindAddress); + + InetSocketAddress boundAddress = (InetSocketAddress) channel1.getLocalAddress(); + assertEquals(bindAddress.getHostName(), boundAddress.getHostName()); + assertEquals(bindAddress.getPort(), boundAddress.getPort()); + } + + public void test_getLocalSocketAddress_afterClose() throws IOException { + SocketChannel sc = SocketChannel.open(); + assertNull(sc.getLocalAddress()); + + InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); + sc.bind(bindAddr); + + assertNotNull(sc.getLocalAddress()); + + sc.close(); + + assertFalse(sc.isOpen()); + + try { + sc.getLocalAddress(); + fail(); + } catch (ClosedChannelException expected) { + } + } + /* * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])' */ @@ -1800,7 +1882,7 @@ public class SocketChannelTest extends TestCase { ServerSocket serversocket = theServerChannel.socket(); serversocket.setReuseAddress(true); // Bind the socket - serversocket.bind(address); + theServerChannel.bind(address); boolean doneNonBlockingConnect = false; // Loop so that we make sure we're definitely testing finishConnect() @@ -2121,8 +2203,7 @@ public class SocketChannelTest extends TestCase { ByteBuffer buffer = ByteBuffer.allocateDirect(128); ServerSocketChannel server = ServerSocketChannel.open(); - server.socket().bind( - new InetSocketAddress(InetAddress.getLocalHost(), 0), 5); + server.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 5); Socket client = new Socket(InetAddress.getLocalHost(), server.socket() .getLocalPort()); client.setTcpNoDelay(false); @@ -2740,9 +2821,9 @@ public class SocketChannelTest extends TestCase { */ public void test_writev() throws Exception { ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); - sc.connect(ssc.socket().getLocalSocketAddress()); + sc.connect(ssc.getLocalAddress()); SocketChannel sock = ssc.accept(); ByteBuffer[] buf = { ByteBuffer.allocate(10), ByteBuffer.allocateDirect(20) }; @@ -2767,10 +2848,10 @@ public class SocketChannelTest extends TestCase { public void test_writev2() throws Exception { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); - boolean connected = sc.connect(ssc.socket().getLocalSocketAddress()); + boolean connected = sc.connect(ssc.getLocalAddress()); SocketChannel sock = ssc.accept(); if (!connected) { sc.finishConnect(); @@ -2805,10 +2886,10 @@ public class SocketChannelTest extends TestCase { public void test_write$NonBlockingException() throws Exception { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); - boolean connected = sc.connect(ssc.socket().getLocalSocketAddress()); + boolean connected = sc.connect(ssc.getLocalAddress()); SocketChannel sock = ssc.accept(); if (!connected) { sc.finishConnect(); @@ -2841,9 +2922,9 @@ public class SocketChannelTest extends TestCase { public void test_write$LByteBuffer2() throws IOException { // Set-up ServerSocketChannel server = ServerSocketChannel.open(); - server.socket().bind(null); + server.bind(null); SocketChannel client = SocketChannel.open(); - client.connect(server.socket().getLocalSocketAddress()); + client.connect(server.getLocalAddress()); SocketChannel worker = server.accept(); // Test overlapping buffers @@ -2873,9 +2954,9 @@ public class SocketChannelTest extends TestCase { public void test_write$LByteBuffer_buffers() throws IOException { // Set-up ServerSocketChannel server = ServerSocketChannel.open(); - server.socket().bind(null); + server.bind(null); SocketChannel client = SocketChannel.open(); - client.connect(server.socket().getLocalSocketAddress()); + client.connect(server.getLocalAddress()); SocketChannel worker = server.accept(); // A variety of buffer types to write @@ -2915,9 +2996,9 @@ public class SocketChannelTest extends TestCase { public void test_write$LByteBuffer_writes() throws IOException { // Set-up ServerSocketChannel server = ServerSocketChannel.open(); - server.socket().bind(null); + server.bind(null); SocketChannel client = SocketChannel.open(); - client.connect(server.socket().getLocalSocketAddress()); + client.connect(server.getLocalAddress()); SocketChannel worker = server.accept(); // Data to write @@ -2942,7 +3023,7 @@ public class SocketChannelTest extends TestCase { // Read what we wrote and check it ByteBuffer readBuffer = ByteBuffer.allocate(1024); - while (EOF != worker.read(readBuffer)) {}; + while (EOF != worker.read(readBuffer)) {} readBuffer.flip(); assertEquals(ByteBuffer.wrap(data), readBuffer); @@ -2957,10 +3038,10 @@ public class SocketChannelTest extends TestCase { public void test_write$LByteBuffer_invalid() throws IOException { // Set-up ServerSocketChannel server = ServerSocketChannel.open(); - server.socket().bind(null); + server.bind(null); SocketChannel client = SocketChannel.open(); - client.connect(server.socket().getLocalSocketAddress()); + client.connect(server.getLocalAddress()); SocketChannel worker = server.accept(); @@ -2968,32 +3049,27 @@ public class SocketChannelTest extends TestCase { try { client.write((ByteBuffer[]) null); fail("Should throw a NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } try { client.write((ByteBuffer[]) null, 0, 0); fail("Should throw a NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } try { client.write((ByteBuffer[]) null, 1, 0); fail("Should throw a NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } try { client.write((ByteBuffer[]) null, 0, 1); fail("Should throw a NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } try { client.write((ByteBuffer[]) null, 1, 1); fail("Should throw a NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } ByteBuffer[] buffers = new ByteBuffer[1]; @@ -3065,9 +3141,9 @@ public class SocketChannelTest extends TestCase { throws Exception { // regression 1 for HARMONY-549 ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); - sc.connect(ssc.socket().getLocalSocketAddress()); + sc.connect(ssc.getLocalAddress()); ssc.accept().close(); ByteBuffer[] buf = { ByteBuffer.allocate(10) }; assertEquals(-1, sc.read(buf, 0, 1)); @@ -3081,16 +3157,15 @@ public class SocketChannelTest extends TestCase { public void test_socketChannel_write_ByteBufferII() throws Exception { // regression 2 for HARMONY-549 ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); - sc.connect(ssc.socket().getLocalSocketAddress()); + sc.connect(ssc.getLocalAddress()); SocketChannel sock = ssc.accept(); ByteBuffer[] buf = { ByteBuffer.allocate(10), null }; try { sc.write(buf, 0, 2); fail("should throw NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } ssc.close(); sc.close(); @@ -3104,9 +3179,9 @@ public class SocketChannelTest extends TestCase { public void test_socketChannel_read_ByteBufferII_bufNULL() throws Exception { // regression 3 for HARMONY-549 ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); - sc.connect(ssc.socket().getLocalSocketAddress()); + sc.connect(ssc.getLocalAddress()); ssc.accept(); ByteBuffer[] buf = new ByteBuffer[2]; buf[0] = ByteBuffer.allocate(1); @@ -3114,8 +3189,7 @@ public class SocketChannelTest extends TestCase { try { sc.read(buf, 0, 2); fail("should throw NullPointerException"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } ssc.close(); sc.close(); @@ -3127,9 +3201,9 @@ public class SocketChannelTest extends TestCase { public void test_socketChannel_write_close() throws Exception { // regression 4 for HARMONY-549 ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); - sc.connect(ssc.socket().getLocalSocketAddress()); + sc.connect(ssc.getLocalAddress()); SocketChannel sock = ssc.accept(); ByteBuffer buf = null; ssc.close(); @@ -3137,8 +3211,7 @@ public class SocketChannelTest extends TestCase { try { sc.write(buf); fail("should throw NPE"); - } catch (NullPointerException e) { - // expected + } catch (NullPointerException expected) { } sock.close(); } @@ -3153,9 +3226,9 @@ public class SocketChannelTest extends TestCase { ByteBuffer readBuf = ByteBuffer.allocate(11); ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes()); ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.socket().bind(null); + ssc.bind(null); SocketChannel sc = SocketChannel.open(); - sc.connect(ssc.socket().getLocalSocketAddress()); + sc.connect(ssc.getLocalAddress()); buf.position(2); ssc.accept().write(buf); assertEquals(9, sc.read(readBuf)); |