You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/15 12:47:39 UTC
svn commit: r386058 [26/49] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/archive/make/common/ modules/archive/src/test/java/tests/
modules/archive/src/test/java/tests/api/
modules/archive/src/test/java/tests/api/java/ modules/archive/...
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramSocketTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,1921 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.BindException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.PortUnreachableException;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.util.Date;
+
+import tests.support.Support_Configuration;
+import tests.support.Support_PortManager;
+
+public class DatagramSocketTest extends SocketTestCase {
+
+ java.net.DatagramSocket ds;
+
+ java.net.DatagramPacket dp;
+
+ DatagramSocket sds = null;
+
+ String retval;
+
+ String testString = "Test String";
+
+ boolean interrupted;
+
+ class DatagramServer extends Thread {
+
+ public DatagramSocket ms;
+
+ boolean running = true;
+
+ public volatile byte[] rbuf = new byte[512];
+
+ volatile DatagramPacket rdp = null;
+
+ public void run() {
+ try {
+ while (running) {
+ try {
+ ms.receive(rdp);
+ // echo the packet back
+ ms.send(rdp);
+ } catch (java.io.InterruptedIOException e) {
+ Thread.yield();
+ }
+ ;
+ }
+ ;
+ } catch (java.io.IOException e) {
+ System.out.println("DatagramServer server failed: " + e);
+ } finally {
+ ms.close();
+ }
+ }
+
+ public void stopServer() {
+ running = false;
+ }
+
+ public DatagramServer(int aPort, InetAddress address)
+ throws java.io.IOException {
+ rbuf = new byte[512];
+ rbuf[0] = -1;
+ rdp = new DatagramPacket(rbuf, rbuf.length);
+ ms = new DatagramSocket(aPort, address);
+ ms.setSoTimeout(2000);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#DatagramSocket()
+ */
+ public void test_Constructor() {
+ // Test for method java.net.DatagramSocket()
+ try {
+ ds = new java.net.DatagramSocket();
+ } catch (Exception e) {
+ fail("Could not create DatagramSocket : " + e.getMessage());
+ }
+
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#DatagramSocket(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.net.DatagramSocket(int)
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ assertTrue("Created socket with incorrect port",
+ ds.getLocalPort() == portNumber);
+ } catch (Exception e) {
+ fail("Could not create DatagramSocket : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#DatagramSocket(int, java.net.InetAddress)
+ */
+ public void test_ConstructorILjava_net_InetAddress() {
+ // Test for method java.net.DatagramSocket(int, java.net.InetAddress)
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber, InetAddress
+ .getLocalHost());
+ assertTrue("Created socket with incorrect port",
+ ds.getLocalPort() == portNumber);
+ assertTrue("Created socket with incorrect address", ds
+ .getLocalAddress().equals(InetAddress.getLocalHost()));
+ } catch (Exception e) {
+ fail("Could not create DatagramSocket : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#close()
+ */
+ public void test_close() {
+ // Test for method void java.net.DatagramSocket.close()
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ dp = new DatagramPacket("Test String".getBytes(), 11, InetAddress
+ .getLocalHost(), 0);
+ ds.close();
+ ds.send(dp);
+ } catch (Exception e) {
+ return;
+ }
+ fail("Data sent after close");
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#connect(java.net.InetAddress, int)
+ */
+ public void test_connectLjava_net_InetAddressI() {
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress
+ .getByName(Support_Configuration.HomeAddress);
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(
+ inetAddress));
+ assertTrue("Incorrect Port", ds.getPort() == portNumber);
+ ds.disconnect();
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+
+ if ("true".equals(System.getProperty("run.ipv6tests"))) {
+ System.out
+ .println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6GlobalAddressJcl4: "
+ + Support_Configuration.IPv6GlobalAddressJcl4);
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress
+ .getByName(Support_Configuration.IPv6GlobalAddressJcl4);
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(
+ inetAddress));
+ assertTrue("Incorrect Port", ds.getPort() == portNumber);
+ ds.disconnect();
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ }
+
+ try {
+ // Create a connected datagram socket to test
+ // PlainDatagramSocketImpl.peek()
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ int port = ds.getLocalPort();
+ ds.connect(localHost, port);
+ DatagramPacket send = new DatagramPacket(new byte[10], 10,
+ localHost, port);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("Wrong size: " + receive.getLength(), receive
+ .getLength() == 10);
+ assertTrue("Wrong receiver", receive.getAddress().equals(localHost));
+ } catch (IOException e) {
+ fail("Unexpected IOException : " + e.getMessage());
+ }
+
+ class DatagramServer extends Thread {
+
+ public DatagramSocket ms;
+
+ boolean running = true;
+
+ public byte[] rbuf = new byte[512];
+
+ DatagramPacket rdp = null;
+
+ public void run() {
+ try {
+ while (running) {
+ try {
+ ms.receive(rdp);
+ // echo the packet back
+ ms.send(rdp);
+ } catch (java.io.InterruptedIOException e) {
+ Thread.yield();
+ }
+ ;
+ }
+ ;
+ } catch (java.io.IOException e) {
+ System.out.println("Multicast server failed: " + e);
+ } finally {
+ ms.close();
+ }
+ }
+
+ public void stopServer() {
+ running = false;
+ }
+
+ public DatagramServer(int aPort, InetAddress address)
+ throws java.io.IOException {
+ rbuf = new byte[512];
+ rbuf[0] = -1;
+ rdp = new DatagramPacket(rbuf, rbuf.length);
+ ms = new DatagramSocket(aPort, address);
+ ms.setSoTimeout(2000);
+ }
+ }
+
+ // validate that we get the PortUnreachable exception if we try to
+ // send a dgram to a server that is not running and then do a recv
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress.getLocalHost();
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ DatagramPacket send = new DatagramPacket(new byte[10], 10);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(10000);
+ ds.receive(receive);
+ ds.close();
+ fail(
+ "No PortUnreachableException when connected at native level on recv ");
+ } catch (Exception e) {
+ assertTrue(
+ "Wrong exception when trying to connect at native level on recv: "
+ + e.toString(),
+ (e instanceof PortUnreachableException));
+ }
+
+ // validate that we can send/receive with datagram sockets connected at
+ // the native level
+ DatagramServer server = null;
+ int serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(localHost, serverPortNumber);
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("Wrong size data received: " + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("Wrong receiver:" + receive.getAddress() + ":"
+ + localHost, receive.getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level:"
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // validate that we can disconnect
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress.getLocalHost();
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ ds.disconnect();
+ ds.close();
+ } catch (Exception e) {
+ assertTrue("Unexpected exception when trying to connect at native"
+ + e.toString(), (e instanceof PortUnreachableException));
+ }
+
+ // validate that once connected we cannot send to another address
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress.getLocalHost();
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ DatagramPacket send = new DatagramPacket(new byte[10], 10,
+ inetAddress, portNumber + 1);
+ ds.send(send);
+ ds.close();
+ fail(
+ "No Exception when trying to send to a different address on a connected socket ");
+ } catch (Exception e) {
+ assertTrue(
+ "Wrong exception when trying to send to a different address on a connected socket: "
+ + e.toString(),
+ (e instanceof IllegalArgumentException));
+ }
+
+ // validate that we can connect, then disconnect, then connect then
+ // send/recv
+ server = null;
+ serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(localHost, serverPortNumber + 1);
+ ds.disconnect();
+ ds.connect(localHost, serverPortNumber);
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue(
+ "connect/disconnect/connect - Wrong size data received: "
+ + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("connect/disconnect/connect - Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("connect/disconnect/connect - Wrong receiver:"
+ + receive.getAddress() + ":" + localHost, receive
+ .getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:"
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // validate that we can connect/disconnect then send/recv to any address
+ server = null;
+ serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(localHost, serverPortNumber + 1);
+ ds.disconnect();
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length, localHost, serverPortNumber);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("connect/disconnect - Wrong size data received: "
+ + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("connect/disconnect - Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("connect/disconnect - Wrong receiver:"
+ + receive.getAddress() + ":" + localHost, receive
+ .getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level after connect/disconnect:"
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // validate that we can connect on an allready connected socket and then
+ // send/recv
+ server = null;
+ serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(localHost, serverPortNumber + 1);
+ ds.connect(localHost, serverPortNumber);
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("connect/connect - Wrong size data received: "
+ + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("connect/connect - Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("connect/connect - Wrong receiver:"
+ + receive.getAddress() + ":" + localHost, receive
+ .getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level after connect/connect: "
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // test for when we fail to connect at the native level. Even though we
+ // fail at the native level there is no way to return an exception so
+ // there should be no exception
+ try {
+ ds = new java.net.DatagramSocket();
+ byte[] addressBytes = { 0, 0, 0, 0 };
+ InetAddress inetAddress = InetAddress.getByAddress(addressBytes);
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ } catch (Exception e) {
+ fail(
+ "Unexcpected exception when trying to connect at native level with bad address for signature with no exception to be returned: "
+ + e.toString());
+ }
+
+ if ("true".equals(System.getProperty("run.ipv6tests"))) {
+ System.out
+ .println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6 address");
+ try {
+ ds = new java.net.DatagramSocket();
+ byte[] addressBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0 };
+ InetAddress inetAddress = InetAddress
+ .getByAddress(addressBytes);
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ } catch (Exception e) {
+ fail(
+ "Unexcpected exception when trying to connect at native level with bad IPv6 address for signature with no exception to be returned: "
+ + e.toString());
+ }
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#disconnect()
+ */
+ public void test_disconnect() {
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress
+ .getByName(Support_Configuration.HomeAddress);
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ ds.disconnect();
+ assertTrue("Incorrect InetAddress", ds.getInetAddress() == null);
+ assertTrue("Incorrect Port", ds.getPort() == -1);
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+
+ if ("true".equals(System.getProperty("run.ipv6tests"))) {
+ System.out
+ .println("Running test_disconnect(DatagramSocketTest) with IPv6GlobalAddressJcl4: "
+ + Support_Configuration.IPv6GlobalAddressJcl4);
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress
+ .getByName(Support_Configuration.IPv6GlobalAddressJcl4);
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(inetAddress, portNumber);
+ ds.disconnect();
+ assertTrue("Incorrect InetAddress", ds.getInetAddress() == null);
+ assertTrue("Incorrect Port", ds.getPort() == -1);
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ }
+
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getInetAddress()
+ */
+ public void test_getInetAddress() {
+ assertTrue("Used to test", true);
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getLocalAddress()
+ */
+ public void test_getLocalAddress() {
+ // Test for method java.net.InetAddress
+ // java.net.DatagramSocket.getLocalAddress()
+ InetAddress local = null;
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ local = InetAddress.getLocalHost();
+ ds = new java.net.DatagramSocket(portNumber, local);
+ assertTrue("Returned incorrect address. Got:"
+ + ds.getLocalAddress()
+ + " wanted: "
+ + InetAddress.getByName(InetAddress.getLocalHost()
+ .getHostName()), InetAddress.getByName(
+ InetAddress.getLocalHost().getHostName()).equals(
+ ds.getLocalAddress()));
+
+ // now validate thet behaviour when the any address is returned
+ String preferIPv4StackValue = System
+ .getProperty("java.net.preferIPv4Stack");
+ String preferIPv6AddressesValue = System
+ .getProperty("java.net.preferIPv6Addresses");
+ DatagramSocket s = new DatagramSocket(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: " + local + " - " + e);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getLocalPort()
+ */
+ public void test_getLocalPort() {
+ // Test for method int java.net.DatagramSocket.getLocalPort()
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ assertTrue("Returned incorrect port",
+ ds.getLocalPort() == portNumber);
+ } catch (Exception e) {
+ fail("Exception during getLocalAddress : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getPort()
+ */
+ public void test_getPort() {
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ DatagramSocket theSocket = new DatagramSocket(portNumber);
+ assertTrue("Expected -1 for remote port as not connected",
+ theSocket.getPort() == -1);
+
+ // now connect the socket and validate that we get the right port
+ theSocket.connect(InetAddress.getLocalHost(), portNumber);
+ assertTrue("getPort returned wrong value:" + theSocket.getPort()
+ + ":Expected:" + portNumber,
+ theSocket.getPort() == portNumber);
+ } catch (Exception e) {
+ fail("unexpected exception during getPort test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getReceiveBufferSize()
+ */
+ public void test_getReceiveBufferSize() {
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setReceiveBufferSize(130);
+ assertTrue("Incorrect buffer size",
+ ds.getReceiveBufferSize() >= 130);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
+ } catch (Exception e) {
+ handleException(e, SO_RCVBUF);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getSendBufferSize()
+ */
+ public void test_getSendBufferSize() {
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setSendBufferSize(134);
+ assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
+ } catch (Exception e) {
+ handleException(e, SO_SNDBUF);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getSoTimeout()
+ */
+ public void test_getSoTimeout() {
+ // Test for method int java.net.DatagramSocket.getSoTimeout()
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setSoTimeout(100);
+ assertTrue("Returned incorrect timeout", ds.getSoTimeout() == 100);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
+ } catch (Exception e) {
+ handleException(e, SO_TIMEOUT);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#receive(java.net.DatagramPacket)
+ */
+ public void test_receiveLjava_net_DatagramPacket() {
+ // Test for method void
+ // java.net.DatagramSocket.receive(java.net.DatagramPacket)
+
+ receive_oversize_java_net_DatagramPacket();
+
+ final int portNumber = Support_PortManager.getNextPort();
+
+ class TestDGRcv implements Runnable {
+ public void run() {
+ InetAddress localHost = null;
+ try {
+ localHost = InetAddress.getLocalHost();
+ Thread.currentThread().sleep(1000);
+ DatagramSocket sds = new DatagramSocket(Support_PortManager
+ .getNextPort());
+ DatagramPacket rdp = new DatagramPacket("Test String"
+ .getBytes(), 11, localHost, portNumber);
+ sds.send(rdp);
+ sds.close();
+ } catch (Exception e) {
+ System.err.println("host " + localHost + " port "
+ + portNumber + " failed to send data: " + e);
+ e.printStackTrace();
+ }
+ }
+ }
+
+ try {
+ new Thread(new TestDGRcv(), "DGSender").start();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setSoTimeout(6000);
+ byte rbuf[] = new byte[1000];
+ DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
+ ;
+ ds.receive(rdp);
+ ds.close();
+ assertTrue("Send/Receive failed to return correct data: "
+ + new String(rbuf, 0, 11), new String(rbuf, 0, 11)
+ .equals("Test String"));
+ } catch (Exception e) {
+ System.err.println("Exception during send test: " + e);
+ e.printStackTrace();
+ fail("port " + portNumber + " Exception during send test: "
+ + e);
+ } finally {
+ ds.close();
+ }
+
+ try {
+ interrupted = false;
+ final DatagramSocket ds = new DatagramSocket();
+ ds.setSoTimeout(12000);
+ Runnable runnable = new Runnable() {
+ public void run() {
+ try {
+ ds.receive(new DatagramPacket(new byte[1], 1));
+ } catch (InterruptedIOException e) {
+ interrupted = true;
+ } catch (IOException e) {
+ }
+ }
+ };
+ Thread thread = new Thread(runnable, "DatagramSocket.receive1");
+ thread.start();
+ try {
+ do {
+ Thread.sleep(500);
+ } while (!thread.isAlive());
+ } catch (InterruptedException e) {
+ }
+ ds.close();
+ int c = 0;
+ do {
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ }
+ if (interrupted) {
+ fail("received interrupt");
+ }
+ if (++c > 4) {
+ fail("read call did not exit");
+ }
+ } while (thread.isAlive());
+
+ interrupted = false;
+ final int portNum = Support_PortManager.getNextPort();
+ final DatagramSocket ds2 = new DatagramSocket();
+ ds2.setSoTimeout(12000);
+ Runnable runnable2 = new Runnable() {
+ public void run() {
+ try {
+ ds2.receive(new DatagramPacket(new byte[1], 1,
+ InetAddress.getLocalHost(), portNum));
+ } catch (InterruptedIOException e) {
+ interrupted = true;
+ } catch (IOException e) {
+ }
+ }
+ };
+ Thread thread2 = new Thread(runnable2, "DatagramSocket.receive2");
+ thread2.start();
+ try {
+ do {
+ Thread.sleep(500);
+ } while (!thread2.isAlive());
+ } catch (InterruptedException e) {
+ }
+ ds2.close();
+ int c2 = 0;
+ do {
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ }
+ if (interrupted) {
+ fail("receive2 was interrupted");
+ }
+ if (++c2 > 4) {
+ fail("read2 call did not exit");
+ }
+ } while (thread2.isAlive());
+
+ interrupted = false;
+ DatagramSocket ds3 = new DatagramSocket();
+ ds3.setSoTimeout(500);
+ Date start = new Date();
+ try {
+ ds3.receive(new DatagramPacket(new byte[1], 1));
+ } catch (InterruptedIOException e) {
+ interrupted = true;
+ }
+ ds3.close();
+ assertTrue("receive not interrupted", interrupted);
+ int delay = (int) (new Date().getTime() - start.getTime());
+ assertTrue("timeout too soon: " + delay, delay >= 490);
+ } catch (IOException e) {
+ fail("Unexpected IOException : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#send(java.net.DatagramPacket)
+ */
+ public void test_sendLjava_net_DatagramPacket() {
+ // Test for method void
+ // java.net.DatagramSocket.send(java.net.DatagramPacket)
+
+ final int portNumber = Support_PortManager.getNextPort();
+
+ class TestDGSend implements Runnable {
+ Thread pThread;
+
+ public TestDGSend(Thread t) {
+ pThread = t;
+ }
+
+ public void run() {
+ try {
+ byte[] rbuf = new byte[1000];
+
+ sds = new DatagramSocket(portNumber);
+ DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
+ sds.setSoTimeout(6000);
+ sds.receive(sdp);
+ retval = new String(rbuf, 0, testString.length());
+ pThread.interrupt();
+ } catch (java.io.InterruptedIOException e) {
+ System.out.println("Recv operation timed out");
+ pThread.interrupt();
+ ds.close();
+ return;
+ } catch (Exception e) {
+ System.out
+ .println("Failed to establish Dgram server: " + e);
+ }
+ }
+ }
+ try {
+ new Thread(new TestDGSend(Thread.currentThread()), "DGServer")
+ .start();
+ ds = new java.net.DatagramSocket();
+ dp = new DatagramPacket(testString.getBytes(), testString.length(),
+ InetAddress.getLocalHost(), portNumber);
+ // Wait to allow send to occur
+ try {
+ Thread.sleep(500);
+ ds.send(dp);
+ Thread.currentThread().sleep(5000);
+ } catch (InterruptedException e) {
+ ds.close();
+ assertTrue("Incorrect data sent: " + retval, retval
+ .equals(testString));
+ }
+ } catch (Exception e) {
+ fail("Exception during send test : " + e.getMessage());
+ } finally {
+ ds.close();
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#setSendBufferSize(int)
+ */
+ public void test_setSendBufferSizeI() {
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setSendBufferSize(134);
+ assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
+ } catch (Exception e) {
+ handleException(e, SO_SNDBUF);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#setReceiveBufferSize(int)
+ */
+ public void test_setReceiveBufferSizeI() {
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setReceiveBufferSize(130);
+ assertTrue("Incorrect buffer size",
+ ds.getReceiveBufferSize() >= 130);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
+ } catch (Exception e) {
+ handleException(e, SO_RCVBUF);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#setSoTimeout(int)
+ */
+ public void test_setSoTimeoutI() {
+ // Test for method void java.net.DatagramSocket.setSoTimeout(int)
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setSoTimeout(100);
+ assertTrue("Set incorrect timeout", ds.getSoTimeout() >= 100);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
+ } catch (Exception e) {
+ handleException(e, SO_TIMEOUT);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#DatagramSocket(java.net.SocketAddress)
+ */
+ public void test_ConstructorLjava_net_SocketAddress() {
+ class mySocketAddress extends SocketAddress {
+
+ public mySocketAddress() {
+ }
+ }
+
+ try {
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(new InetSocketAddress(
+ InetAddress.getLocalHost(), portNumber));
+ assertTrue("Created socket with incorrect port", ds
+ .getLocalPort() == portNumber);
+ assertTrue("Created socket with incorrect address", ds
+ .getLocalAddress().equals(InetAddress.getLocalHost()));
+ } catch (Exception e) {
+ fail("Could not create DatagramSocket : " + e.getMessage());
+ }
+
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ ds = new java.net.DatagramSocket(new mySocketAddress());
+ fail(
+ "No exception when constucting datagramSocket with unsupported SocketAddress type");
+ } catch (IllegalArgumentException e) {
+
+ }
+ } catch (Exception ex) {
+ fail(
+ "unexpected exception when datagramSocket SocketAddress constructor test");
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#bind(java.net.SocketAddress)
+ */
+ public void test_bindLjava_net_SocketAddress() {
+ class mySocketAddress extends SocketAddress {
+
+ public mySocketAddress() {
+ }
+ }
+
+ DatagramServer server = null;
+ try {
+ // now create a socket that is not bound and then bind it
+ int portNumber = Support_PortManager.getNextPort();
+ int serverPortNumber = Support_PortManager.getNextPort();
+ DatagramSocket theSocket = new DatagramSocket(
+ 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)));
+
+ // now make sure that datagrams sent from this socket appear to come
+ // from the address we bound to
+ InetAddress localHost = InetAddress.getLocalHost();
+ portNumber = Support_PortManager.getNextPort();
+ DatagramSocket ds = new DatagramSocket(null);
+ ds.bind(new InetSocketAddress(localHost, portNumber));
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for bin datagram socket test ");
+ }
+
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber));
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ Thread.sleep(1000);
+ ds.close();
+ assertTrue(
+ "Address in packet sent does not match address bound to:"
+ + server.rdp.getAddress() + ":"
+ + server.rdp.getPort() + ":" + localHost + ":"
+ + portNumber, (server.rdp.getAddress()
+ .equals(localHost))
+ && (server.rdp.getPort() == portNumber));
+
+ // validate if we pass in null that it picks an address for us and
+ // all is ok
+ theSocket = new DatagramSocket(null);
+ theSocket.bind(null);
+ assertTrue("Bind with null did not work", theSocket
+ .getLocalSocketAddress() != null);
+ theSocket.close();
+
+ // now check the error conditions
+
+ // Address we cannot bind to
+ theSocket = new DatagramSocket(null);
+ try {
+ theSocket
+ .bind(new InetSocketAddress(
+ InetAddress
+ .getByAddress(Support_Configuration.nonLocalAddressBytes),
+ Support_PortManager.getNextPort()));
+ fail("No exception when binding to bad address");
+ } catch (SocketException ex) {
+ }
+ theSocket.close();
+
+ // Address that we have allready bound to
+ theSocket = new DatagramSocket(null);
+ DatagramSocket theSocket2 = new DatagramSocket();
+ try {
+ InetSocketAddress 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 (SocketException ex) {
+ }
+ theSocket.close();
+ theSocket2.close();
+
+ // unsupported SocketAddress subclass
+ theSocket = new DatagramSocket(null);
+ try {
+ theSocket.bind(new mySocketAddress());
+ assertFalse(
+ "No exception when binding using unsupported SocketAddress subclass",
+ true);
+ } catch (IllegalArgumentException ex) {
+ }
+ theSocket.close();
+
+ } catch (Exception e) {
+ fail("Unexpected exception during bind test : " + e.getMessage());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#connect(java.net.SocketAddress)
+ */
+ public void test_connectLjava_net_SocketAddress() {
+
+ // validate that we get the PortUnreachable exception if we try to
+ // send a dgram to a server that is not running and then do a recv
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress.getLocalHost();
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(new InetSocketAddress(inetAddress, portNumber));
+ DatagramPacket send = new DatagramPacket(new byte[10], 10);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(10000);
+ ds.receive(receive);
+ ds.close();
+ fail(
+ "No PortUnreachableException when connected at native level on recv ");
+ } catch (Exception e) {
+ assertTrue(
+ "Wrong exception when trying to connect at native level on recv: "
+ + e.toString(),
+ (e instanceof PortUnreachableException));
+ }
+
+ // validate that we can send/receive with datagram sockets connected at
+ // the native level
+ DatagramServer server = null;
+ int serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber));
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("Wrong size data received: " + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("Wrong receiver:" + receive.getAddress() + ":"
+ + localHost, receive.getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level:"
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // validate that we can disconnect
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress.getLocalHost();
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(new InetSocketAddress(inetAddress, portNumber));
+ ds.disconnect();
+ ds.close();
+ } catch (Exception e) {
+ assertTrue("Unexpected exception when trying to connect at native"
+ + e.toString(), (e instanceof PortUnreachableException));
+ }
+
+ // validate that once connected we cannot send to another address
+ try {
+ ds = new java.net.DatagramSocket();
+ InetAddress inetAddress = InetAddress.getLocalHost();
+ int portNumber = Support_PortManager.getNextPort();
+ ds.connect(new InetSocketAddress(inetAddress, portNumber));
+ DatagramPacket send = new DatagramPacket(new byte[10], 10,
+ inetAddress, portNumber + 1);
+ ds.send(send);
+ ds.close();
+ fail(
+ "No Exception when trying to send to a different address on a connected socket ");
+ } catch (Exception e) {
+ assertTrue(
+ "Wrong exception when trying to send to a different address on a connected socket: "
+ + e.toString(),
+ (e instanceof IllegalArgumentException));
+ }
+
+ // validate that we can connect, then disconnect, then connect then
+ // send/recv
+ server = null;
+ serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
+ ds.disconnect();
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber));
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue(
+ "connect/disconnect/connect - Wrong size data received: "
+ + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("connect/disconnect/connect - Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("connect/disconnect/connect - Wrong receiver:"
+ + receive.getAddress() + ":" + localHost, receive
+ .getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:"
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // validate that we can connect/disconnect then send/recv to any address
+ server = null;
+ serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
+ ds.disconnect();
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length, localHost, serverPortNumber);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("connect/disconnect - Wrong size data received: "
+ + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("connect/disconnect - Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("connect/disconnect - Wrong receiver:"
+ + receive.getAddress() + ":" + localHost, receive
+ .getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level after connect/disconnect:"
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // validate that we can connect on an allready connected socket and then
+ // send/recv
+ server = null;
+ serverPortNumber = Support_PortManager.getNextPort();
+ try {
+ InetAddress localHost = InetAddress.getLocalHost();
+ DatagramSocket ds = new DatagramSocket();
+ DatagramSocket ds2 = new DatagramSocket();
+
+ try {
+ server = new DatagramServer(serverPortNumber, localHost);
+ server.start();
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail(
+ "Failed to set up datagram server for native connected Dgram socket test ");
+ }
+
+ int port = ds.getLocalPort();
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
+ ds.connect(new InetSocketAddress(localHost, serverPortNumber));
+
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length);
+ ds.send(send);
+ DatagramPacket receive = new DatagramPacket(new byte[20], 20);
+ ds.setSoTimeout(2000);
+ ds.receive(receive);
+ ds.close();
+ assertTrue("connect/connect - Wrong size data received: "
+ + receive.getLength(),
+ receive.getLength() == sendBytes.length);
+ assertTrue("connect/connect - Wrong data received"
+ + new String(receive.getData(), 0, receive.getLength())
+ + ":" + new String(sendBytes), new String(
+ receive.getData(), 0, receive.getLength())
+ .equals(new String(sendBytes)));
+ assertTrue("connect/connect - Wrong receiver:"
+ + receive.getAddress() + ":" + localHost, receive
+ .getAddress().equals(localHost));
+ } catch (Exception e) {
+ fail(
+ "Unexpected exception when sending data on dgram connected at native level after connect/connect: "
+ + e.toString());
+ }
+
+ if (server != null) {
+ server.stopServer();
+ }
+
+ // test for when we fail to connect at the native level. It seems to
+ // fail for the any address so we use this. Now to be compatible we
+ // don't throw the exception but eat it and then act as if we were
+ // connected at the Java level.
+ try {
+ ds = new java.net.DatagramSocket();
+ byte[] addressBytes = { 0, 0, 0, 0 };
+ InetAddress inetAddress = InetAddress.getByAddress(addressBytes);
+ int portNumber = Support_PortManager.getNextPort();
+ InetAddress localHost = InetAddress.getLocalHost();
+ ds.connect(new InetSocketAddress(inetAddress, portNumber));
+ assertTrue("Is not connected after connect to inaddr any", ds
+ .isConnected());
+ byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
+ DatagramPacket send = new DatagramPacket(sendBytes,
+ sendBytes.length, localHost, portNumber);
+ ds.send(send);
+ fail(
+ "No exception when trying to connect at native level with bad address (exception from send) ");
+ } catch (Exception e) {
+ assertTrue(
+ "Wrong exception when trying to connect at native level with bad address (exception from send): "
+ + e.toString(),
+ (e instanceof IllegalArgumentException));
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#isBound()
+ */
+ public void test_isBound() {
+ try {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+
+ DatagramSocket theSocket = new DatagramSocket();
+ assertTrue("Socket indicated not bound when it should be (1)",
+ theSocket.isBound());
+ theSocket.close();
+
+ theSocket = new DatagramSocket(new InetSocketAddress(addr, port));
+ assertTrue("Socket indicated not bound when it should be (2)",
+ theSocket.isBound());
+ theSocket.close();
+
+ theSocket = new DatagramSocket(null);
+ assertFalse("Socket indicated bound when it should not be (1)",
+ theSocket.isBound());
+ theSocket.close();
+
+ // connect causes implicit bind
+ theSocket = new DatagramSocket(null);
+ theSocket.connect(new InetSocketAddress(addr, port));
+ assertTrue("Socket indicated not bound when it should be (3)",
+ theSocket.isBound());
+ theSocket.close();
+
+ // now test when we bind explicitely
+ InetSocketAddress theLocalAddress = new InetSocketAddress(
+ InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket = new DatagramSocket(null);
+ assertFalse("Socket indicated bound when it should not be (2)",
+ theSocket.isBound());
+ theSocket.bind(theLocalAddress);
+ assertTrue("Socket indicated not bound when it should be (4)",
+ theSocket.isBound());
+ theSocket.close();
+ assertTrue("Socket indicated not bound when it should be (5)",
+ theSocket.isBound());
+ } catch (Exception e) {
+ fail("Got exception during isBound tests" + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#isConnected()
+ */
+ public void test_isConnected() {
+ try {
+ InetAddress addr = InetAddress.getLocalHost();
+ int port = Support_PortManager.getNextPort();
+
+ // base test
+ DatagramSocket theSocket = new DatagramSocket();
+ assertFalse("Socket indicated connected when it should not be",
+ theSocket.isConnected());
+ theSocket.connect(new InetSocketAddress(addr, port));
+ assertTrue("Socket indicated not connected when it should be",
+ theSocket.isConnected());
+
+ // reconnect the socket and make sure we get the right answer
+ theSocket.connect(new InetSocketAddress(addr, Support_PortManager
+ .getNextPort()));
+ assertTrue("Socket indicated not connected when it should be",
+ theSocket.isConnected());
+
+ // now disconnect the socket and make sure we get the right answer
+ theSocket.disconnect();
+ assertFalse("Socket indicated connected when it should not be",
+ theSocket.isConnected());
+ theSocket.close();
+
+ // now check behavior when socket is closed when connected
+ theSocket = new DatagramSocket();
+ theSocket.connect(new InetSocketAddress(addr, port));
+ theSocket.close();
+ assertTrue("Socket indicated not connected when it should be",
+ theSocket.isConnected());
+ } catch (Exception e) {
+ fail("Got exception during isConnected tests" + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getRemoteSocketAddress()
+ */
+ public void test_getRemoteSocketAddress() {
+ try {
+ int sport = Support_PortManager.getNextPort();
+ int portNumber = Support_PortManager.getNextPort();
+ DatagramSocket s = new DatagramSocket(new InetSocketAddress(
+ InetAddress.getLocalHost(), portNumber));
+ s.connect(new InetSocketAddress(InetAddress.getLocalHost(), sport));
+ assertTrue("Returned incorrect InetSocketAddress(1):"
+ + s.getLocalSocketAddress().toString(), s
+ .getRemoteSocketAddress().equals(
+ new InetSocketAddress(InetAddress.getLocalHost(),
+ sport)));
+ s.close();
+
+ // now create one that is not connected and validate that we get the
+ // right answer
+ DatagramSocket theSocket = new DatagramSocket(null);
+ portNumber = Support_PortManager.getNextPort();
+ theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
+ portNumber));
+ assertTrue(
+ "Returned incorrect InetSocketAddress -unconnected socket:"
+ + "Expected: NULL", theSocket
+ .getRemoteSocketAddress() == null);
+
+ // 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.DatagramSocket#getLocalSocketAddress()
+ */
+ public void test_getLocalSocketAddress() {
+
+ try {
+ int portNumber = Support_PortManager.getNextPort();
+ DatagramSocket s = new DatagramSocket(new InetSocketAddress(
+ InetAddress.getLocalHost(), 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();
+
+ InetSocketAddress remoteAddress = (InetSocketAddress) s
+ .getRemoteSocketAddress();
+
+ // now create a socket that is not bound and validate we get the
+ // right answer
+ DatagramSocket theSocket = new DatagramSocket(null);
+ assertTrue(
+ "Returned incorrect InetSocketAddress -unbound socket- Expected null",
+ theSocket.getLocalSocketAddress() == null);
+
+ // 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 DatagramSocket(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();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Exception during getLocalSocketAddress test: " + e);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#setReuseAddress(boolean)
+ */
+ public void test_setReuseAddressZ() {
+ try {
+ // test case were we set it to false
+ DatagramSocket theSocket1 = null;
+ DatagramSocket theSocket2 = null;
+ try {
+ InetSocketAddress theAddress = new InetSocketAddress(
+ InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket1 = new DatagramSocket(null);
+ theSocket2 = new DatagramSocket(null);
+ theSocket1.setReuseAddress(false);
+ theSocket2.setReuseAddress(false);
+ theSocket1.bind(theAddress);
+ theSocket2.bind(theAddress);
+ fail(
+ "No exception when trying to connect to do duplicate socket bind with re-useaddr set to false");
+ } catch (BindException e) {
+
+ }
+ if (theSocket1 != null)
+ theSocket1.close();
+ if (theSocket2 != null)
+ theSocket2.close();
+
+ // test case were we set it to true
+ try {
+ InetSocketAddress theAddress = new InetSocketAddress(
+ InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket1 = new DatagramSocket(null);
+ theSocket2 = new DatagramSocket(null);
+ theSocket1.setReuseAddress(true);
+ theSocket2.setReuseAddress(true);
+ theSocket1.bind(theAddress);
+ theSocket2.bind(theAddress);
+ } catch (Exception e) {
+ fail(
+ "unexpected exception when trying to connect to do duplicate socket bind with re-useaddr set to true");
+ }
+ if (theSocket1 != null)
+ theSocket1.close();
+ if (theSocket2 != null)
+ theSocket2.close();
+
+ // test the default case which we expect to be the same on all
+ // platforms
+ try {
+ InetSocketAddress theAddress = new InetSocketAddress(
+ InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket1 = new DatagramSocket(null);
+ theSocket2 = new DatagramSocket(null);
+ theSocket1.bind(theAddress);
+ theSocket2.bind(theAddress);
+ fail(
+ "No exception when trying to connect to do duplicate socket bind with re-useaddr left as default");
+ } catch (BindException e) {
+
+ }
+ if (theSocket1 != null)
+ theSocket1.close();
+ if (theSocket2 != null)
+ theSocket2.close();
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
+ } catch (Exception e) {
+ handleException(e, SO_REUSEADDR);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getReuseAddress()
+ */
+ public void test_getReuseAddress() {
+ try {
+ DatagramSocket theSocket = new DatagramSocket();
+ theSocket.setReuseAddress(true);
+ assertTrue("getReuseAddress false when it should be true",
+ theSocket.getReuseAddress());
+ theSocket.setReuseAddress(false);
+ assertFalse("getReuseAddress true when it should be False",
+ theSocket.getReuseAddress());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
+ } catch (Exception e) {
+ handleException(e, SO_REUSEADDR);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#setBroadcast(boolean)
+ */
+ public void test_setBroadcastZ() {
+
+ try {
+ DatagramSocket theSocket = new DatagramSocket();
+ theSocket.setBroadcast(false);
+ byte theBytes[] = { -1, -1, -1, -1 };
+
+ // validate we cannot connect to the broadcast address when
+ // setBroadcast is false
+ try {
+ theSocket.connect(new InetSocketAddress(InetAddress
+ .getByAddress(theBytes), Support_PortManager
+ .getNextPort()));
+ assertFalse(
+ "No exception when connecting to broadcast address with setBroadcast(false)",
+ theSocket.getBroadcast());
+ } catch (Exception ex) {
+ }
+
+ // now validate that we can connect to the broadcast address when
+ // setBroadcast is true
+ theSocket.setBroadcast(true);
+ theSocket
+ .connect(new InetSocketAddress(InetAddress
+ .getByAddress(theBytes), Support_PortManager
+ .getNextPort()));
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_BROADCAST);
+ } catch (Exception e) {
+ handleException(e, SO_BROADCAST);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getBroadcast()
+ */
+ public void test_getBroadcast() {
+ try {
+ DatagramSocket theSocket = new DatagramSocket();
+ theSocket.setBroadcast(true);
+ assertTrue("getBroadcast false when it should be true", theSocket
+ .getBroadcast());
+ theSocket.setBroadcast(false);
+ assertFalse("getBroadcast true when it should be False", theSocket
+ .getBroadcast());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_BROADCAST);
+ } catch (Exception e) {
+ handleException(e, SO_BROADCAST);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#setTrafficClass(int)
+ */
+ public void test_setTrafficClassI() {
+ try {
+ int IPTOS_LOWCOST = 0x2;
+ int IPTOS_RELIABILTY = 0x4;
+ int IPTOS_THROUGHPUT = 0x8;
+ int IPTOS_LOWDELAY = 0x10;
+
+ new InetSocketAddress(InetAddress.getLocalHost(),
+ Support_PortManager.getNextPort());
+ DatagramSocket theSocket = new DatagramSocket();
+
+ // validate that value set must be between 0 and 255
+ try {
+ theSocket.setTrafficClass(256);
+ fail("No exception when traffic class set to 256");
+ } catch (IllegalArgumentException e) {
+ }
+
+ try {
+ theSocket.setTrafficClass(-1);
+ fail("No exception when traffic class set to -1");
+ } catch (IllegalArgumentException e) {
+ }
+
+ // now validate that we can set it to some good values
+ theSocket.setTrafficClass(IPTOS_LOWCOST);
+ theSocket.setTrafficClass(IPTOS_THROUGHPUT);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS);
+ } catch (Exception e) {
+ handleException(e, IP_TOS);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#getTrafficClass()
+ */
+ public void test_getTrafficClass() {
+ try {
+ int IPTOS_LOWCOST = 0x2;
+ int IPTOS_RELIABILTY = 0x4;
+ int IPTOS_THROUGHPUT = 0x8;
+ int IPTOS_LOWDELAY = 0x10;
+
+ new InetSocketAddress(InetAddress.getLocalHost(),
+ Support_PortManager.getNextPort());
+ DatagramSocket theSocket = new DatagramSocket();
+
+ /*
+ * we cannot actually check that the values are set as if a platform
+ * does not support the option then it may come back unset even
+ * though we set it so just get the value to make sure we can get it
+ */
+ int trafficClass = theSocket.getTrafficClass();
+ } catch (Exception e) {
+ handleException(e, IP_TOS);
+ }
+ }
+
+ /**
+ * @tests java.net.DatagramSocket#isClosed()
+ */
+ public void test_isClosed() {
+ try {
+ DatagramSocket theSocket = new DatagramSocket();
+
+ // validate isClosed returns expected values
+ assertFalse("Socket should indicate it is not closed(1):",
+ theSocket.isClosed());
+ theSocket.close();
+ assertTrue("Socket should indicate it is not closed(1):", theSocket
+ .isClosed());
+
+ InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort());
+ theSocket = new DatagramSocket(theAddress);
+ assertFalse("Socket should indicate it is not closed(2):",
+ theSocket.isClosed());
+ theSocket.close();
+ assertTrue("Socket should indicate it is not closed(2):", theSocket
+ .isClosed());
+ } catch (Exception e) {
+ fail("Got exception during isClosed tests" + e.toString());
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ retval = "Bogus retval";
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ try {
+ ds.close();
+ sds.close();
+ } catch (Exception e) {
+ }
+ }
+
+ protected void receive_oversize_java_net_DatagramPacket() {
+
+ final int portNumber = Support_PortManager.getNextPort();
+
+ class TestDGRcvOver implements Runnable {
+ public void run() {
+ InetAddress localHost = null;
+ try {
+ localHost = InetAddress.getLocalHost();
+ Thread.sleep(1000);
+ DatagramSocket sds = new DatagramSocket(Support_PortManager
+ .getNextPort());
+ DatagramPacket rdp = new DatagramPacket("0123456789"
+ .getBytes(), 10, localHost, portNumber);
+ sds.send(rdp);
+ sds.close();
+ } catch (Exception e) {
+ System.err.println("host " + localHost + " port "
+ + portNumber + " failed to send oversize data: "
+ + e);
+ e.printStackTrace();
+ }
+ }
+ }
+
+ try {
+ new Thread(new TestDGRcvOver(), "DGSenderOver").start();
+ ds = new java.net.DatagramSocket(portNumber);
+ ds.setSoTimeout(6000);
+ byte rbuf[] = new byte[5];
+ DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
+ ;
+ ds.receive(rdp);
+ ds.close();
+ assertTrue("Send/Receive oversize failed to return correct data: "
+ + new String(rbuf, 0, 5), new String(rbuf, 0, 5)
+ .equals("01234"));
+ } catch (Exception e) {
+ System.err.println("Exception during send test: " + e);
+ e.printStackTrace();
+ fail("port " + portNumber + " Exception: " + e
+ + " during oversize send test");
+ } finally {
+ ds.close();
+ }
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,243 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.UnknownHostException;
+import java.util.List;
+import java.util.Map;
+
+import tests.support.Support_Configuration;
+import tests.support.resource.Support_Resources;
+
+public class HttpURLConnectionTest extends junit.framework.TestCase {
+
+ URL url;
+
+ HttpURLConnection uc;
+
+ /**
+ * @tests java.net.HttpURLConnection#getResponseCode()
+ */
+ public void test_getResponseCode() {
+ try {
+ assertTrue("Wrong response", uc.getResponseCode() == 200);
+ } catch (IOException e) {
+ fail("Unexpected exception : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.HttpURLConnection#getResponseMessage()
+ */
+ public void test_getResponseMessage() {
+ try {
+ assertTrue("Wrong response: " + uc.getResponseMessage(), uc
+ .getResponseMessage().equals("OK"));
+ } catch (IOException e) {
+ fail("Unexpected exception : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.HttpURLConnection#getHeaderFields()
+ */
+ public void test_getHeaderFields() {
+ try {
+ uc.getInputStream();
+ } catch (IOException e) {
+ fail();
+ }
+ Map headers = uc.getHeaderFields();
+ List list = (List) headers.get("Content-Length");
+ if (list == null) {
+ list = (List) headers.get("content-length");
+ }
+ assertNotNull(list);
+
+ // content-length should always appear
+ String contentLength = (String) list.get(0);
+ assertNotNull(contentLength);
+
+ // there should be at least 2 headers
+ assertTrue(headers.size() > 1);
+
+ try {
+ // the map should be unmodifiable
+ headers.put("hi", "bye");
+ fail();
+ } catch (UnsupportedOperationException e) {
+ }
+
+ try {
+ // the list should be unmodifiable
+ list.set(0, "whatever");
+ fail();
+ } catch (UnsupportedOperationException e) {
+ }
+ }
+
+ /**
+ * @tests java.net.HttpURLConnection#getRequestProperties()
+ */
+ public void test_getRequestProperties() {
+ uc.setRequestProperty("whatever", "you like");
+ Map headers = uc.getRequestProperties();
+
+ List newHeader = (List) headers.get("whatever");
+ assertNotNull(newHeader);
+
+ assertEquals(newHeader.get(0), "you like");
+
+ try {
+ // the map should be unmodifiable
+ headers.put("hi", "bye");
+ fail();
+ } catch (UnsupportedOperationException e) {
+ }
+ }
+
+ /**
+ * @tests java.net.HttpURLConnection#usingProxy()
+ */
+ public void test_usingProxy() {
+ try {
+ try {
+ System.setProperty("http.proxyHost",
+ Support_Configuration.ProxyServerTestHost);
+
+ URL u = new URL("http://" + Support_Configuration.HomeAddress);
+ URLConnection conn = u.openConnection();
+ conn.getInputStream();
+ } catch (ConnectException e) {
+ fail("ConnectException connecting to proxy : " + e.getMessage());
+ } catch (UnknownHostException e) {
+ fail("UnknownHostException connecting to proxy : "
+ + e.getMessage());
+ } catch (Exception e) {
+ fail("Unexpected exception connecting to proxy : "
+ + e.getMessage());
+ }
+
+ boolean exception = false;
+ try {
+ System.setProperty("http.proxyPort", "81");
+ URL u = new URL("http://"
+ + Support_Configuration.InetTestAddress);
+ URLConnection conn = u.openConnection();
+ conn.getInputStream();
+ } catch (ConnectException e) {
+ exception = true;
+ } catch (UnknownHostException e) {
+ exception = true;
+ } catch (Exception e) {
+ exception = false;
+ }
+ assertTrue("No exception or wrong exception thrown", exception);
+
+ System.setProperty("http.proxyPort", "80");
+
+ try {
+ URL u = new URL("http://"
+ + Support_Configuration.ProxyServerTestHost
+ + "/cgi-bin/test.pl");
+ java.net.HttpURLConnection conn = (java.net.HttpURLConnection) u
+ .openConnection();
+ conn.setDoOutput(true);
+ conn.setRequestMethod("POST");
+ OutputStream out = conn.getOutputStream();
+ String posted = "this is a test";
+ out.write(posted.getBytes());
+ out.close();
+ conn.getResponseCode();
+ InputStream is = conn.getInputStream();
+ String response = "";
+ byte[] b = new byte[1024];
+ int count = 0;
+ while ((count = is.read(b)) > 0)
+ response += new String(b, 0, count);
+ assertTrue("Response to POST method invalid", response
+ .equals(posted));
+ } catch (ConnectException e) {
+ fail("ConnectException connecting to proxy : " + e.getMessage());
+ } catch (UnknownHostException e) {
+ fail("UnknownHostException connecting to proxy : "
+ + e.getMessage());
+ } catch (Exception e) {
+ fail("Unexpected exception connecting to proxy : "
+ + e.getMessage());
+ }
+
+ try {
+ String posted = "just a test";
+ URL u = new URL("http://"
+ + Support_Configuration.ProxyServerTestHost
+ + "/cgi-bin/test.pl");
+ java.net.HttpURLConnection conn = (java.net.HttpURLConnection) u
+ .openConnection();
+ conn.setDoOutput(true);
+ conn.setRequestMethod("POST");
+ conn.setRequestProperty("Content-length", String.valueOf(posted
+ .length()));
+ OutputStream out = conn.getOutputStream();
+ out.write(posted.getBytes());
+ out.close();
+ conn.getResponseCode();
+ InputStream is = conn.getInputStream();
+ String response = "";
+ byte[] b = new byte[1024];
+ int count = 0;
+ while ((count = is.read(b)) > 0)
+ response += new String(b, 0, count);
+ assertTrue("Response to POST method invalid", response
+ .equals(posted));
+ } catch (ConnectException e) {
+ fail("ConnectException connecting to proxy : " + e.getMessage());
+ } catch (UnknownHostException e) {
+ fail("UnknownHostException connecting to proxy : "
+ + e.getMessage());
+ } catch (Exception e) {
+ fail("Unexpected exception connecting to proxy : "
+ + e.getMessage());
+ }
+ } finally {
+ System.setProperties(null);
+ }
+ }
+
+ protected void setUp() {
+ try {
+ url = new URL(Support_Resources
+ .getResourceURL("/URLConnectionTest/Harmony.html"));
+ uc = (HttpURLConnection) url.openConnection();
+ } catch (Exception e) {
+ fail("Exception during setup : " + e.getMessage());
+ }
+ }
+
+ protected void tearDown() {
+ uc.disconnect();
+ }
+
+ protected void doneSuite() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet4AddressTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet4AddressTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet4AddressTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/Inet4AddressTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,364 @@
+/* Copyright 2003, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.net;
+
+import java.net.InetAddress;
+
+public class Inet4AddressTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.Inet4Address#isMulticastAddress()
+ */
+ public void test_isMulticastAddress() {
+
+ // Create 2 IP v4 addresses and call "isMulticastAddress()"
+ // result should return true if the first 4 bits of the
+ // address are: 1110, false otherwise
+ // Make 1 address with 1110, and 1 without
+ String addrName = "";
+ try {
+ addrName = "224.0.0.0"; // a multicast addr 1110 = 224-239
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("Multicast address " + addrName + " not detected.", addr
+ .isMulticastAddress());
+
+ addrName = "239.255.255.255"; // a multicast addr 1110 = 224-239
+ addr = InetAddress.getByName(addrName);
+ assertTrue("Multicast address " + addrName + " not detected.", addr
+ .isMulticastAddress());
+
+ addrName = "42.42.42.42"; // a non-multicast address
+ addr = InetAddress.getByName(addrName);
+ assertTrue("Non multicast address " + addrName
+ + " reporting as a multicast address.", !addr
+ .isMulticastAddress());
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isAnyLocalAddress()
+ */
+ public void test_isAnyLocalAddress() {
+ String addrName = "";
+ try {
+ addrName = "0.0.0.0";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("ANY address " + addrName + " not detected.", addr
+ .isAnyLocalAddress());
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isLoopbackAddress()
+ */
+ public void test_isLoopbackAddress() {
+ // Create some IP V4 addresses and test if they are local...
+
+ String addrName = "";
+ try {
+ addrName = "127.0.0.0"; // a loopback address should be 127.d.d.d
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("Loopback address " + addrName + " not detected.", addr
+ .isLoopbackAddress());
+
+ addrName = "127.42.42.42"; // a loopback address should be
+ // 127.d.d.d
+ addr = InetAddress.getByName(addrName);
+ assertTrue("Loopback address " + addrName + " not detected.", addr
+ .isLoopbackAddress());
+
+ addrName = "42.42.42.42"; // a loopback address should be
+ // 127.d.d.d
+ addr = InetAddress.getByName(addrName);
+ assertTrue("Address incorrectly " + addrName
+ + " detected as a loopback address.", !addr
+ .isLoopbackAddress());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isLinkLocalAddress()
+ */
+ public void test_isLinkLocalAddress() {
+
+ String addrName = "";
+ try {
+ // There are no link local addresses for IPv4
+ // We'll test one to ensure we get "false"
+
+ addrName = "42.42.42.42";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 address " + addrName
+ + " incorrectly reporting as a link local address.", !addr
+ .isLinkLocalAddress());
+ } catch (Exception e) {
+ fail("Unknown address : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isSiteLocalAddress()
+ */
+ public void test_isSiteLocalAddress() {
+ String addrName = "";
+ try {
+ // There are no site local addresses for IPv4
+ // We'll test one to ensure we get "false"
+
+ addrName = "42.42.42.42";
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 address " + addrName
+ + " incorrectly reporting as a site local address.", !addr
+ .isSiteLocalAddress());
+ } catch (Exception e) {
+ fail("Unknown address : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isMCGlobal()
+ */
+ public void test_isMCGlobal() {
+
+ // Create an IPv4 mulitcast address. It should return
+ // false for globabl mutlicast. There are no valid IPv4
+ // global multicast addresses
+
+ String addrName = "";
+ try {
+ addrName = "224.0.0.0"; // a multicast addr 1110
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 link-local multicast address " + addrName
+ + " incorrectly identified as a global multicast address.",
+ !addr.isMCGlobal());
+
+ addrName = "224.0.0.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 link-local multicast address " + addrName
+ + " incorrectly identified as a global multicast address.",
+ !addr.isMCGlobal());
+
+ addrName = "224.0.1.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 global multicast address " + addrName
+ + " not identified as a global multicast address.", addr
+ .isMCGlobal());
+
+ addrName = "238.255.255.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 global multicast address " + addrName
+ + " not identified as a global multicast address.", addr
+ .isMCGlobal());
+
+ addrName = "239.0.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 reserved multicast address " + addrName
+ + " incorrectly identified as a global multicast address.",
+ !addr.isMCGlobal());
+
+ addrName = "239.191.255.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 reserved multicast address " + addrName
+ + " incorrectly identified as a global multicast address.",
+ !addr.isMCGlobal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isMCNodeLocal()
+ */
+ public void test_isMCNodeLocal() {
+ // Create an IPv4 mulitcast address. It should return
+ // false for node-local mutlicast. There are no valid IPv4
+ // node-local multicast addresses
+
+ String addrName = "";
+ try {
+ addrName = "224.42.42.42"; // a multicast addr 1110 = 224
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 multicast address "
+ + addrName
+ + " incorrectly identified as a node-local multicast address.",
+ !addr.isMCNodeLocal());
+
+ addrName = "239.0.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 reserved multicast address "
+ + addrName
+ + " incorrectly identified as a node-local multicast address.",
+ !addr.isMCNodeLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isMCLinkLocal()
+ */
+ public void test_isMCLinkLocal() {
+ // Create an IPv4 mulitcast address. It should return
+ // false for link-local mutlicast. There are no valid IPv4
+ // link-local multicast addresses
+
+ String addrName = "";
+ try {
+ addrName = "224.0.0.0"; // a multicast addr 1110
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 link-local multicast address " + addrName
+ + " not identified as a link-local multicast address.",
+ addr.isMCLinkLocal());
+
+ addrName = "224.0.0.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 link-local multicast address " + addrName
+ + " not identified as a link-local multicast address.",
+ addr.isMCLinkLocal());
+
+ addrName = "224.0.1.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 global multicast address "
+ + addrName
+ + " incorrectly identified as a link-local multicast address.",
+ !addr.isMCLinkLocal());
+
+ addrName = "239.0.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 reserved multicast address "
+ + addrName
+ + " incorrectly identified as a link-local multicast address.",
+ !addr.isMCLinkLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isMCSiteLocal()
+ */
+ public void test_isMCSiteLocal() {
+ // Create an IPv4 mulitcast address. It should return
+ // false for site-local mutlicast. There are no valid IPv4
+ // site-local multicast addresses
+
+ String addrName = "";
+ try {
+ addrName = "240.0.0.0"; // a multicast addr 1110 = 224
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 multicast address "
+ + addrName
+ + " incorrectly identified as a site-local multicast address.",
+ !addr.isMCSiteLocal());
+
+ addrName = "239.0.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 reserved multicast address "
+ + addrName
+ + " incorrectly identified as a site-local multicast address.",
+ !addr.isMCSiteLocal());
+
+ addrName = "239.255.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 site-local multicast address " + addrName
+ + " not identified as a site-local multicast address.",
+ addr.isMCSiteLocal());
+
+ addrName = "239.255.255.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 site-local multicast address " + addrName
+ + " not identified as a site-local multicast address.",
+ addr.isMCSiteLocal());
+
+ addrName = "239.255.2.2"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 site-local multicast address " + addrName
+ + " not identified as a site-local multicast address.",
+ addr.isMCSiteLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ /**
+ * @tests java.net.Inet4Address#isMCOrgLocal()
+ */
+ public void test_isMCOrgLocal() {
+ // Create an IPv4 mulitcast address. It should return
+ // false for organization-local mutlicast. There are no valid IPv4
+ // organization-local multicast addresses
+
+ String addrName = "";
+ try {
+
+ addrName = "239.191.255.255"; // a multicast addr 1110
+ InetAddress addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 reserved multicast address "
+ + addrName
+ + " incorrectly identified as a org-local multicast address.",
+ !addr.isMCOrgLocal());
+
+ addrName = "239.252.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue(
+ "IPv4 site-local multicast address "
+ + addrName
+ + " incorrectly identified as a org-local multicast address.",
+ !addr.isMCOrgLocal());
+
+ addrName = "239.192.0.0"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 org-local multicast address " + addrName
+ + " not identified as a org-local multicast address.", addr
+ .isMCOrgLocal());
+
+ addrName = "239.195.255.255"; // a multicast addr 1110
+ addr = InetAddress.getByName(addrName);
+ assertTrue("IPv4 org-local multicast address " + addrName
+ + " not identified as a org-local multicast address.", addr
+ .isMCOrgLocal());
+
+ } catch (Exception e) {
+ fail("Unknown address : " + addrName);
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+}