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 [28/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/MulticastSocketTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/MulticastSocketTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,1161 @@
+/* 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.net.BindException;
+import java.net.DatagramPacket;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.MulticastSocket;
+import java.net.NetworkInterface;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.Enumeration;
+
+import tests.support.Support_NetworkInterface;
+import tests.support.Support_PortManager;
+
+public class MulticastSocketTest extends SocketTestCase {
+
+ Thread t;
+
+ MulticastSocket mss;
+
+ MulticastServer server;
+
+ // private member variables used for tests
+ boolean atLeastOneInterface = false;
+
+ boolean atLeastTwoInterfaces = false;
+
+ private NetworkInterface networkInterface1 = null;
+
+ private NetworkInterface sameAsNetworkInterface1 = null;
+
+ private NetworkInterface networkInterface2 = null;
+
+ private NetworkInterface IPV6networkInterface1 = null;
+
+ static class MulticastServer extends Thread {
+
+ public MulticastSocket ms;
+
+ boolean running = true;
+
+ volatile public byte[] rbuf = new byte[512];
+
+ volatile DatagramPacket rdp = null;
+
+ public void run() {
+ try {
+ while (running) {
+ try {
+ ms.receive(rdp);
+ } catch (java.io.InterruptedIOException e) {
+ Thread.yield();
+ }
+ ;
+ }
+ ;
+ } catch (java.io.IOException e) {
+ System.out.println("Multicast server failed: " + e);
+ } finally {
+ ms.close();
+ }
+ }
+
+ public synchronized void leaveGroup(InetAddress aGroup)
+ throws java.io.IOException {
+ ms.leaveGroup(aGroup);
+ }
+
+ public void stopServer() {
+ running = false;
+ }
+
+ public MulticastServer(InetAddress anAddress, int aPort)
+ throws java.io.IOException {
+ rbuf = new byte[512];
+ rbuf[0] = -1;
+ rdp = new DatagramPacket(rbuf, rbuf.length);
+ ms = new MulticastSocket(aPort);
+ ms.setSoTimeout(2000);
+ ms.joinGroup(anAddress);
+ }
+
+ public MulticastServer(SocketAddress anAddress, int aPort,
+ NetworkInterface netInterface) throws java.io.IOException {
+ rbuf = new byte[512];
+ rbuf[0] = -1;
+ rdp = new DatagramPacket(rbuf, rbuf.length);
+ ms = new MulticastSocket(aPort);
+ ms.setSoTimeout(2000);
+ ms.joinGroup(anAddress, netInterface);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#MulticastSocket()
+ */
+ public void test_Constructor() {
+ // Test for method java.net.MulticastSocket()
+ // Used in tests
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#MulticastSocket(int)
+ */
+ public void test_ConstructorI() {
+ // Test for method java.net.MulticastSocket(int)
+ // Used in tests
+ MulticastSocket dup = null;
+ try {
+ mss = new MulticastSocket();
+ int port = mss.getLocalPort();
+ dup = new MulticastSocket(port);
+ } catch (IOException e) {
+ fail("duplicate binding not allowed: " + e);
+ }
+ if (dup != null)
+ dup.close();
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#getInterface()
+ */
+ public void test_getInterface() {
+ // Test for method java.net.InetAddress
+ // java.net.MulticastSocket.getInterface()
+ assertTrue("Used for testing.", true);
+
+ int groupPort = Support_PortManager.getNextPort();
+ try {
+ if (atLeastOneInterface) {
+ // validate that we get the expected response when one was not
+ // set
+ mss = new MulticastSocket(groupPort);
+ 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"))) {
+ // we expect an IPv6 ANY in this case
+ assertTrue("inet Address returned when not set:"
+ + mss.getInterface().toString(), InetAddress
+ .getByName("::0").equals(mss.getInterface()));
+ } else {
+ // we expect an IPv4 ANY in this case
+ assertTrue("inet Address returned when not set:"
+ + mss.getInterface().toString(), InetAddress
+ .getByName("0.0.0.0").equals(mss.getInterface()));
+ }
+
+ // validate that we get the expected response when we set via
+ // setInterface
+ Enumeration addresses = networkInterface1.getInetAddresses();
+ if (addresses != null) {
+ InetAddress firstAddress = (InetAddress) addresses
+ .nextElement();
+ mss.setInterface(firstAddress);
+ assertTrue(
+ "getNetworkInterface did not return interface set by setInterface. Expected:"
+ + firstAddress + " Got:"
+ + mss.getInterface(), firstAddress
+ .equals(mss.getInterface()));
+
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ mss.setNetworkInterface(networkInterface1);
+ assertTrue(
+ "getInterface did not return interface set by setNeworkInterface Expected: "
+ + firstAddress + "Got:"
+ + mss.getInterface(), NetworkInterface
+ .getByInetAddress(mss.getInterface())
+ .equals(networkInterface1));
+ }
+
+ }
+ } catch (Exception e) {
+ fail("Exception during getInterface test: " + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#getNetworkInterface()
+ */
+ public void test_getNetworkInterface() {
+ int groupPort = Support_PortManager.getNextPort();
+ try {
+ if (atLeastOneInterface) {
+
+ // validate that we get the expected response when one was not
+ // set
+ mss = new MulticastSocket(groupPort);
+ NetworkInterface theInterface = mss.getNetworkInterface();
+ assertTrue(
+ "network interface returned wrong network interface when not set:"
+ + theInterface,
+ theInterface.getInetAddresses() != null);
+ InetAddress firstAddress = (InetAddress) theInterface
+ .getInetAddresses().nextElement();
+ // validate we the first address in the network interface is the
+ // ANY address
+ 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(
+ "network interface returned wrong network interface when not set:"
+ + theInterface, InetAddress
+ .getByName("::0").equals(firstAddress));
+
+ } else {
+ assertTrue(
+ "network interface returned wrong network interface when not set:"
+ + theInterface, InetAddress.getByName(
+ "0.0.0.0").equals(firstAddress));
+ }
+
+ mss.setNetworkInterface(networkInterface1);
+ assertTrue(
+ "getNetworkInterface did not return interface set by setNeworkInterface",
+ networkInterface1.equals(mss.getNetworkInterface()));
+
+ if (atLeastTwoInterfaces) {
+ mss.setNetworkInterface(networkInterface2);
+ assertTrue(
+ "getNetworkInterface did not return network interface set by second setNetworkInterface call",
+ networkInterface2.equals(mss.getNetworkInterface()));
+ }
+
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ if (IPV6networkInterface1 != null) {
+ mss.setNetworkInterface(IPV6networkInterface1);
+ assertTrue(
+ "getNetworkInterface did not return interface set by setNeworkInterface",
+ IPV6networkInterface1.equals(mss
+ .getNetworkInterface()));
+ }
+
+ // validate that we get the expected response when we set via
+ // setInterface
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ Enumeration addresses = networkInterface1.getInetAddresses();
+ if (addresses != null) {
+ firstAddress = (InetAddress) addresses.nextElement();
+ mss.setInterface(firstAddress);
+ assertTrue(
+ "getNetworkInterface did not return interface set by setInterface",
+ networkInterface1.equals(mss.getNetworkInterface()));
+ }
+ }
+ } catch (Exception e) {
+ fail("Exception during getNetworkInterface test: "
+ + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#getTimeToLive()
+ */
+ public void test_getTimeToLive() {
+ try {
+ mss = new MulticastSocket();
+ mss.setTimeToLive(120);
+ assertTrue("Returned incorrect 1st TTL: " + mss.getTimeToLive(),
+ mss.getTimeToLive() == 120);
+ mss.setTimeToLive(220);
+ assertTrue("Returned incorrect 2nd TTL: " + mss.getTimeToLive(),
+ mss.getTimeToLive() == 220);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#getTTL()
+ */
+ public void test_getTTL() {
+ // Test for method byte java.net.MulticastSocket.getTTL()
+
+ try {
+ mss = new MulticastSocket();
+ mss.setTTL((byte) 120);
+ assertTrue("Returned incorrect TTL: " + mss.getTTL(),
+ mss.getTTL() == 120);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#joinGroup(java.net.InetAddress)
+ */
+ public void test_joinGroupLjava_net_InetAddress() {
+ // Test for method void
+ // java.net.MulticastSocket.joinGroup(java.net.InetAddress)
+ String msg = null;
+ InetAddress group = null;
+ int groupPort = Support_PortManager.getNextPort();
+ try {
+ group = InetAddress.getByName("224.0.0.3");
+ server = new MulticastServer(group, groupPort);
+ server.start();
+ Thread.sleep(1000);
+ msg = "Hello World";
+ mss = new MulticastSocket(Support_PortManager.getNextPort());
+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
+ .length(), group, groupPort);
+ mss.send(sdp, (byte) 10);
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ fail("Exception during joinGroup test: " + e.toString());
+ }
+ assertTrue("Group member did not recv data: ", new String(server.rdp
+ .getData(), 0, server.rdp.getLength()).equals(msg));
+
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#joinGroup(java.net.SocketAddress,java.net.NetworkInterface)
+ */
+ public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface() {
+ // security manager that allows us to check that we only return the
+ // addresses that we should
+ class mySecurityManager extends SecurityManager {
+
+ public void checkMulticast(InetAddress address) {
+ throw new SecurityException("not allowed");
+ }
+ }
+
+ String msg = null;
+ InetAddress group = null;
+ SocketAddress groupSockAddr = null;
+ int groupPort = Support_PortManager.getNextPort();
+ int serverPort = Support_PortManager.getNextPort();
+
+ try {
+ Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
+
+ // first validate that we handle a null group ok
+ mss = new MulticastSocket(groupPort);
+ try {
+ mss.joinGroup(null, null);
+ fail("Did not get exception when group was null");
+ } catch (IllegalArgumentException e) {
+ }
+
+ // now validate we get the expected error if the address specified
+ // is not a multicast group
+ try {
+ groupSockAddr = new InetSocketAddress(InetAddress
+ .getByName("255.255.255.255"), groupPort);
+ mss.joinGroup(groupSockAddr, null);
+ assertFalse(
+ "Did not get exception when group is not a multicast address",
+ true);
+ } catch (IOException e) {
+ }
+
+ // now try to join a group if we are not authorized
+ // set the security manager that will make the first address not
+ // visible
+ System.setSecurityManager(new mySecurityManager());
+ try {
+ group = InetAddress.getByName("224.0.0.3");
+ groupSockAddr = new InetSocketAddress(group, groupPort);
+ mss.joinGroup(groupSockAddr, null);
+ assertFalse(
+ "Did not get exception when joining group is not allowed",
+ true);
+ } catch (SecurityException e) {
+ }
+ System.setSecurityManager(null);
+
+ if (atLeastOneInterface) {
+ // now validate that we can properly join a group with a null
+ // network interface
+ groupPort = Support_PortManager.getNextPort();
+ serverPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ mss.joinGroup(groupSockAddr, null);
+ mss.setTimeToLive(2);
+ Thread.sleep(1000);
+
+ // set up the server and join the group on networkInterface1
+ group = InetAddress.getByName("224.0.0.3");
+ groupSockAddr = new InetSocketAddress(group, groupPort);
+ server = new MulticastServer(groupSockAddr, serverPort,
+ networkInterface1);
+ server.start();
+ Thread.sleep(1000);
+ msg = "Hello World";
+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
+ .length(), group, serverPort);
+ mss.setTimeToLive(2);
+ mss.send(sdp);
+ Thread.sleep(1000);
+ // now vaildate that we received the data as expected
+ assertTrue("Group member did not recv data: ", new String(
+ server.rdp.getData(), 0, server.rdp.getLength())
+ .equals(msg));
+ server.stopServer();
+
+ // now validate that we handled the case were we join a
+ // different multicast address.
+ // verify we do not receive the data
+ serverPort = Support_PortManager.getNextPort();
+ server = new MulticastServer(groupSockAddr, serverPort,
+ networkInterface1);
+ server.start();
+ Thread.sleep(1000);
+
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ InetAddress group2 = InetAddress.getByName("224.0.0.4");
+ mss.setTimeToLive(10);
+ msg = "Hello World - Different Group";
+ sdp = new DatagramPacket(msg.getBytes(), msg.length(), group2,
+ serverPort);
+ mss.send(sdp);
+ Thread.sleep(1000);
+ assertFalse(
+ "Group member received data when sent on different group: ",
+ new String(server.rdp.getData(), 0, server.rdp
+ .getLength()).equals(msg));
+ server.stopServer();
+
+ // if there is more than one network interface then check that
+ // we can join on specific interfaces and that we only receive
+ // if data is received on that interface
+ if (atLeastTwoInterfaces) {
+ // set up server on first interfaces
+ NetworkInterface loopbackInterface = NetworkInterface
+ .getByInetAddress(InetAddress
+ .getByName("127.0.0.1"));
+
+ theInterfaces = NetworkInterface.getNetworkInterfaces();
+ while (theInterfaces.hasMoreElements()) {
+
+ NetworkInterface thisInterface = (NetworkInterface) theInterfaces
+ .nextElement();
+ if ((thisInterface.getInetAddresses() != null)
+ && (Support_NetworkInterface
+ .useInterface(thisInterface) == true)) {
+ // get the first address on the interface
+
+ // start server which is joined to the group and has
+ // only asked for packets on this interface
+ Enumeration addresses = thisInterface
+ .getInetAddresses();
+
+ NetworkInterface sendingInterface = null;
+ boolean isLoopback = false;
+ if (addresses != null) {
+ InetAddress firstAddress = (InetAddress) addresses
+ .nextElement();
+ if (firstAddress.isLoopbackAddress()) {
+ isLoopback = true;
+ }
+ if (firstAddress instanceof Inet4Address) {
+ group = InetAddress.getByName("224.0.0.4");
+ if (networkInterface1
+ .equals(NetworkInterface
+ .getByInetAddress(InetAddress
+ .getByName("127.0.0.1")))) {
+ sendingInterface = networkInterface2;
+ } else {
+ sendingInterface = networkInterface1;
+ }
+ } else {
+ // if this interface only seems to support
+ // IPV6 addresses
+ group = InetAddress
+ .getByName("FF01:0:0:0:0:0:2:8001");
+ sendingInterface = IPV6networkInterface1;
+ }
+ }
+
+ InetAddress useAddress = null;
+ addresses = sendingInterface.getInetAddresses();
+ if ((addresses != null)
+ && (addresses.hasMoreElements())) {
+ useAddress = (InetAddress) addresses
+ .nextElement();
+ }
+
+ serverPort = Support_PortManager.getNextPort();
+ groupPort = Support_PortManager.getNextPort();
+ groupSockAddr = new InetSocketAddress(group,
+ serverPort);
+ server = new MulticastServer(groupSockAddr,
+ serverPort, thisInterface);
+ server.start();
+ Thread.sleep(1000);
+
+ // Now send out a package on interface
+ // networkInterface 1. We should
+ // only see the packet if we send it on interface 1
+ InetSocketAddress theAddress = new InetSocketAddress(
+ useAddress, groupPort);
+ mss = new MulticastSocket(groupPort);
+ mss.setNetworkInterface(sendingInterface);
+ msg = "Hello World - Again"
+ + thisInterface.getName();
+ sdp = new DatagramPacket(msg.getBytes(), msg
+ .length(), group, serverPort);
+ mss.send(sdp);
+ Thread.sleep(1000);
+ if (thisInterface.equals(sendingInterface)) {
+ assertTrue(
+ "Group member did not recv data when bound on specific interface: ",
+ new String(server.rdp.getData(), 0,
+ server.rdp.getLength())
+ .equals(msg));
+ } else {
+ assertFalse(
+ "Group member received data on other interface when only asked for it on one interface: ",
+ new String(server.rdp.getData(), 0,
+ server.rdp.getLength())
+ .equals(msg));
+ }
+
+ server.stopServer();
+ }
+ }
+
+ // validate that we can join the same address on two
+ // different interfaces but not on the same interface
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ mss.joinGroup(groupSockAddr, networkInterface1);
+ mss.joinGroup(groupSockAddr, networkInterface2);
+ try {
+ mss.joinGroup(groupSockAddr, networkInterface1);
+ fail(
+ "Did not get expected exception when joining for second time on same interface");
+ } catch (IOException e) {
+ }
+ }
+ }
+ } catch (Exception e) {
+ fail("Exception during joinGroup test: " + e.toString());
+ }
+ System.setSecurityManager(null);
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#leaveGroup(java.net.InetAddress)
+ */
+ public void test_leaveGroupLjava_net_InetAddress() {
+ // Test for method void
+ // java.net.MulticastSocket.leaveGroup(java.net.InetAddress)
+ String msg = null;
+ boolean except = false;
+ InetAddress group = null;
+ int groupPort = Support_PortManager.getNextPort();
+
+ try {
+ group = InetAddress.getByName("224.0.0.3");
+ msg = "Hello World";
+ mss = new MulticastSocket();
+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
+ .length(), group, groupPort);
+ mss.send(sdp, (byte) 10);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST);
+ }
+ try {
+ // Try to leave s group that mss is not a member of
+ mss.leaveGroup(group);
+ } catch (java.io.IOException e) {
+ // Correct
+ except = true;
+ }
+ assertTrue("Failed to throw exception leaving non-member group", except);
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#leaveGroup(java.net.SocketAddress,java.net.NetworkInterface)
+ */
+ public void test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface() {
+ // security manager that allows us to check that we only return the
+ // addresses that we should
+ class mySecurityManager extends SecurityManager {
+
+ public void checkMulticast(InetAddress address) {
+ throw new SecurityException("not allowed");
+ }
+ }
+
+ String msg = null;
+ InetAddress group = null;
+ int groupPort = Support_PortManager.getNextPort();
+ SocketAddress groupSockAddr = null;
+ SocketAddress groupSockAddr2 = null;
+
+ try {
+ Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
+
+ // first validate that we handle a null group ok
+ mss = new MulticastSocket(groupPort);
+ try {
+ mss.leaveGroup(null, null);
+ fail("Did not get exception when group was null");
+ } catch (IllegalArgumentException e) {
+ }
+
+ // now validate we get the expected error if the address specified
+ // is not a multicast group
+ try {
+ group = InetAddress.getByName("255.255.255.255");
+ groupSockAddr = new InetSocketAddress(group, groupPort);
+ mss.leaveGroup(groupSockAddr, null);
+ assertFalse(
+ "Did not get exception when group is not a multicast address",
+ true);
+ } catch (IOException e) {
+ }
+
+ // now try to leave a group if we are not authorized
+ // set the security manager that will make the first address not
+ // visible
+ System.setSecurityManager(new mySecurityManager());
+ try {
+ group = InetAddress.getByName("224.0.0.3");
+ groupSockAddr = new InetSocketAddress(group, groupPort);
+ mss.leaveGroup(groupSockAddr, null);
+ assertFalse(
+ "Did not get exception when joining group is not allowed",
+ true);
+ } catch (SecurityException e) {
+ }
+ System.setSecurityManager(null);
+
+ if (atLeastOneInterface) {
+
+ // now test that we can join and leave a group successfully
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ groupSockAddr = new InetSocketAddress(group, groupPort);
+ mss.joinGroup(groupSockAddr, null);
+ mss.leaveGroup(groupSockAddr, null);
+ try {
+ mss.leaveGroup(groupSockAddr, null);
+ fail(
+ "Did not get exception when trying to leave group that was allready left");
+ } catch (IOException e) {
+ }
+
+ InetAddress group2 = InetAddress.getByName("224.0.0.4");
+ groupSockAddr2 = new InetSocketAddress(group2, groupPort);
+ mss.joinGroup(groupSockAddr, networkInterface1);
+ try {
+ mss.leaveGroup(groupSockAddr2, networkInterface1);
+ fail(
+ "Did not get exception when trying to leave group that was never joined");
+ } catch (IOException e) {
+ }
+
+ mss.leaveGroup(groupSockAddr, networkInterface1);
+ if (atLeastTwoInterfaces) {
+ mss.joinGroup(groupSockAddr, networkInterface1);
+ try {
+ mss.leaveGroup(groupSockAddr, networkInterface2);
+ fail(
+ "Did not get exception when trying to leave group on wrong interface joined on ["
+ + networkInterface1
+ + "] left on ["
+ + networkInterface2 + "]");
+ } catch (IOException e) {
+ }
+ }
+ }
+ } catch (Exception e) {
+ fail("Exception during leaveGroup test: " + e.toString());
+ }
+ System.setSecurityManager(null);
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#send(java.net.DatagramPacket, byte)
+ */
+ public void test_sendLjava_net_DatagramPacketB() {
+ // Test for method void
+ // java.net.MulticastSocket.send(java.net.DatagramPacket, byte)
+
+ String msg = "Hello World";
+ InetAddress group = null;
+ int groupPort = Support_PortManager.getNextPort();
+
+ try {
+ group = InetAddress.getByName("224.0.0.3");
+ mss = new MulticastSocket();
+ server = new MulticastServer(group, groupPort);
+ server.start();
+ Thread.sleep(200);
+ DatagramPacket sdp = new DatagramPacket(msg.getBytes(), msg
+ .length(), group, groupPort);
+ mss.send(sdp, (byte) 10);
+ Thread.sleep(1000);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST);
+ try {
+ mss.close();
+ } catch (Exception ex) {
+ }
+ ;
+ return;
+ }
+ mss.close();
+ byte[] data = server.rdp.getData();
+ int length = server.rdp.getLength();
+ assertTrue("Failed to send data. Received " + length, new String(data,
+ 0, length).equals(msg));
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#setInterface(java.net.InetAddress)
+ */
+ public void test_setInterfaceLjava_net_InetAddress() {
+ // Test for method void
+ // java.net.MulticastSocket.setInterface(java.net.InetAddress)
+ // Note that the machine is not multi-homed
+
+ try {
+ mss = new MulticastSocket();
+ mss.setInterface(InetAddress.getLocalHost());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST_INTERFACE);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST_INTERFACE);
+ return;
+ }
+ try {
+ InetAddress theInterface = mss.getInterface();
+ // under IPV6 we are not guarrenteed to get the same address back as
+ // the address, all we should be guaranteed is that we get an
+ // address on the same interface
+ if (theInterface instanceof Inet6Address) {
+ assertTrue(
+ "Failed to return correct interface IPV6",
+ NetworkInterface
+ .getByInetAddress(mss.getInterface())
+ .equals(
+ NetworkInterface
+ .getByInetAddress(theInterface)));
+ } else {
+ assertTrue("Failed to return correct interface IPV4 got:"
+ + mss.getInterface() + " excpeted: "
+ + InetAddress.getLocalHost(), mss.getInterface()
+ .equals(InetAddress.getLocalHost()));
+ }
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (SocketException e) {
+ handleException(e, SO_MULTICAST);
+ } catch (UnknownHostException e) {
+ fail("Exception during setInterface test: " + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#setNetworkInterface(java.net.NetworkInterface)
+ */
+ public void test_setNetworkInterfaceLjava_net_NetworkInterface() {
+ String msg = null;
+ InetAddress group = null;
+ int groupPort = Support_PortManager.getNextPort();
+ int serverPort = Support_PortManager.getNextPort();
+ try {
+ if (atLeastOneInterface) {
+ // validate that null interface is handled ok
+ mss = new MulticastSocket(groupPort);
+
+ // this should through a socket exception to be compatible
+ try {
+ mss.setNetworkInterface(null);
+ fail(
+ "No socket exception when we set then network interface with NULL");
+ } catch (SocketException ex) {
+ }
+
+ // validate that we can get and set the interface
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ mss.setNetworkInterface(networkInterface1);
+ assertTrue(
+ "Interface did not seem to be set by setNeworkInterface",
+ networkInterface1.equals(mss.getNetworkInterface()));
+
+ // set up the server and join the group
+ group = InetAddress.getByName("224.0.0.3");
+
+ Enumeration theInterfaces = NetworkInterface
+ .getNetworkInterfaces();
+ while (theInterfaces.hasMoreElements()) {
+ NetworkInterface thisInterface = (NetworkInterface) theInterfaces
+ .nextElement();
+ if (thisInterface.getInetAddresses() != null) {
+ if ((!((InetAddress) thisInterface.getInetAddresses()
+ .nextElement()).isLoopbackAddress())
+ &&
+ // for windows we cannot use these pseudo
+ // interfaces for the test as the packets still
+ // come from the actual interface, not the
+ // Pseudo interface that was set
+ (Support_NetworkInterface
+ .useInterface(thisInterface) == true)) {
+ serverPort = Support_PortManager.getNextPort();
+ server = new MulticastServer(group, serverPort);
+ server.start();
+ // give the server some time to start up
+ Thread.sleep(1000);
+
+ // Send the packets on a particular interface. The
+ // source address in the received packet
+ // should be one of the addresses for the interface
+ // set
+ groupPort = Support_PortManager.getNextPort();
+ mss = new MulticastSocket(groupPort);
+ mss.setNetworkInterface(thisInterface);
+ msg = thisInterface.getName();
+ byte theBytes[] = msg.getBytes();
+ DatagramPacket sdp = new DatagramPacket(theBytes,
+ theBytes.length, group, serverPort);
+ mss.send(sdp);
+ Thread.sleep(1000);
+ String receivedMessage = new String(server.rdp
+ .getData(), 0, server.rdp.getLength());
+ assertTrue(
+ "Group member did not recv data when send on a specific interface: ",
+ receivedMessage.equals(msg));
+ assertTrue(
+ "Datagram was not received from expected interface expected["
+ + thisInterface
+ + "] got ["
+ + NetworkInterface
+ .getByInetAddress(server.rdp
+ .getAddress())
+ + "]", NetworkInterface
+ .getByInetAddress(
+ server.rdp.getAddress())
+ .equals(thisInterface));
+
+ // stop the server
+ server.stopServer();
+ }
+ }
+ }
+ }
+ ;
+ } catch (Exception e) {
+ fail("Exception during setNetworkInterface test: "
+ + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#setTimeToLive(int)
+ */
+ public void test_setTimeToLiveI() {
+ try {
+ mss = new MulticastSocket();
+ mss.setTimeToLive(120);
+ assertTrue("Returned incorrect 1st TTL: " + mss.getTimeToLive(),
+ mss.getTimeToLive() == 120);
+ mss.setTimeToLive(220);
+ assertTrue("Returned incorrect 2nd TTL: " + mss.getTimeToLive(),
+ mss.getTimeToLive() == 220);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#setTTL(byte)
+ */
+ public void test_setTTLB() {
+ // Test for method void java.net.MulticastSocket.setTTL(byte)
+ try {
+ mss = new MulticastSocket();
+ mss.setTTL((byte) 120);
+ assertTrue("Failed to set TTL: " + mss.getTTL(),
+ mss.getTTL() == 120);
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_MULTICAST);
+ } catch (Exception e) {
+ handleException(e, SO_MULTICAST);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress)
+ */
+ public void test_ConstructorLjava_net_SocketAddress() {
+ try {
+ MulticastSocket ms = new MulticastSocket((SocketAddress) null);
+ assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
+ && !ms.isConnected());
+ ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
+ Support_PortManager.getNextPort()));
+ assertTrue("should be bound", ms.isBound() && !ms.isClosed()
+ && !ms.isConnected());
+ ms.close();
+ assertTrue("should be closed", ms.isClosed());
+ ms = new MulticastSocket(new InetSocketAddress(InetAddress
+ .getLocalHost(), Support_PortManager.getNextPort()));
+ assertTrue("should be bound", ms.isBound() && !ms.isClosed()
+ && !ms.isConnected());
+ ms.close();
+ assertTrue("should be closed", ms.isClosed());
+ ms = new MulticastSocket(new InetSocketAddress("localhost",
+ Support_PortManager.getNextPort()));
+ assertTrue("should be bound", ms.isBound() && !ms.isClosed()
+ && !ms.isConnected());
+ ms.close();
+ assertTrue("should be closed", ms.isClosed());
+ boolean exception = false;
+ try {
+ ms = new MulticastSocket(new InetSocketAddress(
+ "unresolvedname", Support_PortManager.getNextPort()));
+ } catch (IOException e) {
+ exception = true;
+ }
+ assertTrue("Expected IOException", exception);
+ } catch (IOException e) {
+ fail("Unexpected: " + e);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#getLoopbackMode()
+ */
+ public void test_getLoopbackMode() {
+ try {
+ MulticastSocket ms = new MulticastSocket((SocketAddress) null);
+ assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
+ && !ms.isConnected());
+ ms.getLoopbackMode();
+ assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
+ && !ms.isConnected());
+ ms.close();
+ assertTrue("should be closed", ms.isClosed());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
+ } catch (IOException e) {
+ handleException(e, SO_USELOOPBACK);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#setLoopbackMode(boolean)
+ */
+ public void test_setLoopbackModeZ() {
+ try {
+ MulticastSocket ms = new MulticastSocket();
+ ms.setLoopbackMode(true);
+ assertTrue("loopback should be true", ms.getLoopbackMode());
+ ms.setLoopbackMode(false);
+ assertTrue("loopback should be false", !ms.getLoopbackMode());
+ ms.close();
+ assertTrue("should be closed", ms.isClosed());
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_USELOOPBACK);
+ } catch (IOException e) {
+ handleException(e, SO_USELOOPBACK);
+ }
+ }
+
+ /**
+ * @tests java.net.MulticastSocket#setReuseAddress(boolean)
+ */
+ public void test_setReuseAddressZ() {
+ try {
+ // test case were we set it to false
+ MulticastSocket theSocket1 = null;
+ MulticastSocket theSocket2 = null;
+ try {
+ InetSocketAddress theAddress = new InetSocketAddress(
+ InetAddress.getLocalHost(), Support_PortManager
+ .getNextPort());
+ theSocket1 = new MulticastSocket(null);
+ theSocket2 = new MulticastSocket(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 MulticastSocket(null);
+ theSocket2 = new MulticastSocket(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 MulticastSocket(null);
+ theSocket2 = new MulticastSocket(null);
+ theSocket1.bind(theAddress);
+ theSocket2.bind(theAddress);
+ } catch (BindException e) {
+ fail(
+ "unexpected exception when trying to connect to do duplicate socket bind with re-useaddr left as default");
+ }
+ if (theSocket1 != null)
+ theSocket1.close();
+ if (theSocket2 != null)
+ theSocket2.close();
+ ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
+ } catch (Exception e) {
+ handleException(e, SO_REUSEADDR);
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+
+ Enumeration theInterfaces = null;
+ try {
+ theInterfaces = NetworkInterface.getNetworkInterfaces();
+ } catch (Exception e) {
+ }
+
+ // only consider interfaces that have addresses associated with them.
+ // Otherwise tests don't work so well
+ if (theInterfaces != null) {
+
+ atLeastOneInterface = false;
+ while (theInterfaces.hasMoreElements()
+ && (atLeastOneInterface == false)) {
+ networkInterface1 = (NetworkInterface) theInterfaces
+ .nextElement();
+ if ((networkInterface1.getInetAddresses() != null) &&
+ // we only want real interfaces
+ (Support_NetworkInterface
+ .useInterface(networkInterface1) == true)) {
+ atLeastOneInterface = true;
+ }
+ }
+
+ atLeastTwoInterfaces = false;
+ if (theInterfaces.hasMoreElements()) {
+ while (theInterfaces.hasMoreElements()
+ && (atLeastTwoInterfaces == false)) {
+ networkInterface2 = (NetworkInterface) theInterfaces
+ .nextElement();
+ if ((networkInterface2.getInetAddresses() != null) &&
+ // we only want real interfaces
+ (Support_NetworkInterface
+ .useInterface(networkInterface2) == true)) {
+ atLeastTwoInterfaces = true;
+ }
+ }
+ }
+
+ Enumeration addresses = networkInterface1.getInetAddresses();
+ if (addresses != null) {
+ try {
+ sameAsNetworkInterface1 = NetworkInterface
+ .getByInetAddress((InetAddress) addresses
+ .nextElement());
+ } catch (SocketException e) {
+ }
+ }
+
+ // first the first interface that supports IPV6 if one exists
+ try {
+ theInterfaces = NetworkInterface.getNetworkInterfaces();
+ } catch (Exception e) {
+ }
+ boolean found = false;
+ while (theInterfaces.hasMoreElements() && !found) {
+ NetworkInterface nextInterface = (NetworkInterface) theInterfaces
+ .nextElement();
+ addresses = nextInterface.getInetAddresses();
+ if (addresses != null) {
+ while (addresses.hasMoreElements()) {
+ InetAddress nextAddress = (InetAddress) addresses
+ .nextElement();
+ if (nextAddress instanceof Inet6Address) {
+ IPV6networkInterface1 = nextInterface;
+ found = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+
+ if (t != null)
+ t.interrupt();
+ if (mss != null)
+ mss.close();
+ if (server != null)
+ server.stopServer();
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetPermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetPermissionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetPermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetPermissionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,58 @@
+/* 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.net.NetPermission;
+
+public class NetPermissionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.NetPermission#NetPermission(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.net.NetPermission(java.lang.String)
+ NetPermission n = new NetPermission("requestPasswordAuthentication");
+ assertTrue("Returned incorrect name", n.getName().equals(
+ "requestPasswordAuthentication"));
+ }
+
+ /**
+ * @tests java.net.NetPermission#NetPermission(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_StringLjava_lang_String() {
+ // Test for method java.net.NetPermission(java.lang.String,
+ // java.lang.String)
+ NetPermission n = new NetPermission("requestPasswordAuthentication",
+ null);
+ assertTrue("Returned incorrect name", n.getName().equals(
+ "requestPasswordAuthentication"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetworkInterfaceTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetworkInterfaceTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetworkInterfaceTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NetworkInterfaceTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,518 @@
+/* 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.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+
+public class NetworkInterfaceTest extends junit.framework.TestCase {
+
+ // private member variables used for tests
+ boolean atLeastOneInterface = false;
+
+ boolean atLeastTwoInterfaces = false;
+
+ private NetworkInterface networkInterface1 = null;
+
+ private NetworkInterface sameAsNetworkInterface1 = null;
+
+ private NetworkInterface networkInterface2 = null;
+
+ /**
+ * @tests java.net.NetworkInterface#getName()
+ */
+ public void test_getName() {
+ if (atLeastOneInterface) {
+ assertTrue("validate that non null name is returned",
+ networkInterface1.getName() != null);
+ assertFalse("validate that non-zero length name is generated",
+ networkInterface1.getName().equals(""));
+ }
+ if (atLeastTwoInterfaces) {
+ assertFalse(
+ "Validate strings are different for different interfaces",
+ networkInterface1.getName().equals(
+ networkInterface2.getName()));
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#getInetAddresses()
+ */
+ public void test_getInetAddresses() {
+
+ // security manager that allows us to check that we only return the
+ // addresses that we should
+ class mySecurityManager extends SecurityManager {
+
+ ArrayList disallowedNames = null;
+
+ public mySecurityManager(ArrayList addresses) {
+ disallowedNames = new ArrayList();
+ for (int i = 0; i < addresses.size(); i++) {
+ disallowedNames.add(((InetAddress) addresses.get(i))
+ .getHostName());
+ disallowedNames.add(((InetAddress) addresses.get(i))
+ .getHostAddress());
+ }
+ }
+
+ public void checkConnect(String host, int port) {
+
+ if (host == null) {
+ throw new NullPointerException("host was null)");
+ }
+
+ for (int i = 0; i < disallowedNames.size(); i++) {
+ if (((String) disallowedNames.get(i)).equals(host)) {
+ throw new SecurityException("not allowed");
+ }
+ }
+ }
+
+ }
+
+ if (atLeastOneInterface) {
+ Enumeration theAddresses = networkInterface1.getInetAddresses();
+ if (theAddresses != null) {
+ while (theAddresses.hasMoreElements()) {
+ InetAddress theAddress = (InetAddress) theAddresses
+ .nextElement();
+ assertTrue("validate that address is not null",
+ null != theAddress);
+ }
+ }
+ }
+
+ if (atLeastTwoInterfaces) {
+ Enumeration theAddresses = networkInterface2.getInetAddresses();
+ if (theAddresses != null) {
+ while (theAddresses.hasMoreElements()) {
+ InetAddress theAddress = (InetAddress) theAddresses
+ .nextElement();
+ assertTrue("validate that address is not null",
+ null != theAddress);
+ }
+ }
+ }
+
+ // create the list of ok and not ok addresses to return
+ if (atLeastOneInterface) {
+ ArrayList okAddresses = new ArrayList();
+ Enumeration addresses = networkInterface1.getInetAddresses();
+ int index = 0;
+ ArrayList notOkAddresses = new ArrayList();
+ if (addresses != null) {
+ while (addresses.hasMoreElements()) {
+ InetAddress theAddress = (InetAddress) addresses
+ .nextElement();
+ if (index != 0) {
+ okAddresses.add(theAddress);
+ } else {
+ notOkAddresses.add(theAddress);
+ }
+ index++;
+ }
+ }
+
+ // do the same for network interface 2 it it exists
+ if (atLeastTwoInterfaces) {
+ addresses = networkInterface2.getInetAddresses();
+ index = 0;
+ if (addresses != null) {
+ while (addresses.hasMoreElements()) {
+ InetAddress theAddress = (InetAddress) addresses
+ .nextElement();
+ if (index != 0) {
+ okAddresses.add(theAddress);
+ } else {
+ notOkAddresses.add(theAddress);
+ }
+ index++;
+ }
+ }
+ }
+
+ // set the security manager that will make the first address not
+ // visible
+ System.setSecurityManager(new mySecurityManager(notOkAddresses));
+
+ // validate not ok addresses are not returned
+ for (int i = 0; i < notOkAddresses.size(); i++) {
+ Enumeration reducedAddresses = networkInterface1
+ .getInetAddresses();
+ if (reducedAddresses != null) {
+ while (reducedAddresses.hasMoreElements()) {
+ InetAddress nextAddress = (InetAddress) reducedAddresses
+ .nextElement();
+ assertTrue(
+ "validate that address without permission is not returned",
+ !nextAddress.equals(notOkAddresses.get(i)));
+ }
+ }
+ if (atLeastTwoInterfaces) {
+ reducedAddresses = networkInterface2.getInetAddresses();
+ if (reducedAddresses != null) {
+ while (reducedAddresses.hasMoreElements()) {
+ InetAddress nextAddress = (InetAddress) reducedAddresses
+ .nextElement();
+ assertTrue(
+ "validate that address without permission is not returned",
+ !nextAddress.equals(notOkAddresses.get(i)));
+ }
+ }
+ }
+ }
+
+ // validate that ok addresses are returned
+ for (int i = 0; i < okAddresses.size(); i++) {
+ boolean addressReturned = false;
+ Enumeration reducedAddresses = networkInterface1
+ .getInetAddresses();
+ if (reducedAddresses != null) {
+ while (reducedAddresses.hasMoreElements()) {
+ InetAddress nextAddress = (InetAddress) reducedAddresses
+ .nextElement();
+ if (nextAddress.equals(okAddresses.get(i))) {
+ addressReturned = true;
+ }
+ }
+ }
+ if (atLeastTwoInterfaces) {
+ reducedAddresses = networkInterface2.getInetAddresses();
+ if (reducedAddresses != null) {
+ while (reducedAddresses.hasMoreElements()) {
+ InetAddress nextAddress = (InetAddress) reducedAddresses
+ .nextElement();
+ if (nextAddress.equals(okAddresses.get(i))) {
+ addressReturned = true;
+ }
+ }
+ }
+ }
+ assertTrue("validate that address with permission is returned",
+ addressReturned);
+ }
+
+ // validate that we can get the interface by specifying the address.
+ // This is to be compatible
+ for (int i = 0; i < notOkAddresses.size(); i++) {
+ try {
+ assertTrue(
+ "validate we cannot get the NetworkInterface with an address for which we have no privs",
+ NetworkInterface
+ .getByInetAddress((InetAddress) notOkAddresses
+ .get(i)) != null);
+ } catch (Exception e) {
+ assertFalse(
+ "get NetworkInterface for address with no perm - exception",
+ true);
+ }
+ }
+
+ // validate that we can get the network interface for the good
+ // addresses
+ try {
+ for (int i = 0; i < okAddresses.size(); i++) {
+ assertTrue(
+ "validate we cannot get the NetworkInterface with an address fro which we have no privs",
+ NetworkInterface
+ .getByInetAddress((InetAddress) okAddresses
+ .get(i)) != null);
+ }
+ } catch (Exception e) {
+ assertFalse(
+ "get NetworkInterface for address with perm - exception",
+ true);
+ }
+
+ System.setSecurityManager(null);
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#getDisplayName()
+ */
+ public void test_getDisplayName() {
+ if (atLeastOneInterface) {
+ assertTrue("validate that non null display name is returned",
+ networkInterface1.getDisplayName() != null);
+ assertFalse(
+ "validate that non-zero lengtj display name is generated",
+ networkInterface1.getDisplayName().equals(""));
+ }
+ if (atLeastTwoInterfaces) {
+ assertFalse(
+ "Validate strings are different for different interfaces",
+ networkInterface1.getDisplayName().equals(
+ networkInterface2.getDisplayName()));
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#getByName(java.lang.String)
+ */
+ public void test_getByNameLjava_lang_String() {
+ try {
+ assertTrue("validate null handled ok", null == NetworkInterface
+ .getByName(null));
+ fail("getByName did not throw NullPointerException for null argument");
+ } catch (NullPointerException e) {
+ } catch (Exception e) {
+ fail("getByName, null inetAddress - raised exception : "
+ + e.getMessage());
+ }
+
+ try {
+ assertTrue(
+ "validate handled ok if we ask for name not associated with any interface",
+ null == NetworkInterface.getByName("8not a name4"));
+ } catch (Exception e) {
+ fail("getByName, unknown inetAddress - raised exception : "
+ + e.getMessage());
+ }
+
+ // for each address in an interface validate that we get the right
+ // interface for that name
+ if (atLeastOneInterface) {
+ String theName = networkInterface1.getName();
+ if (theName != null) {
+ try {
+ assertTrue(
+ "validate that Interface can be obtained with its name",
+ NetworkInterface.getByName(theName).equals(
+ networkInterface1));
+ } catch (Exception e) {
+ assertFalse(
+ "validate to get network interface using name - socket exception",
+ true);
+ }
+ }
+ }
+
+ // validate that we get the right interface with the second interface as
+ // well (ie we just don't always get the first interface
+ if (atLeastTwoInterfaces) {
+ String theName = networkInterface2.getName();
+ if (theName != null) {
+ try {
+ assertTrue(
+ "validate that Interface can be obtained with its name",
+ NetworkInterface.getByName(theName).equals(
+ networkInterface2));
+ } catch (Exception e) {
+ assertFalse(
+ "validate to get network interface using name - socket exception",
+ true);
+ }
+ }
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#getByInetAddress(java.net.InetAddress)
+ */
+ public void test_getByInetAddressLjava_net_InetAddress() {
+
+ byte addressBytes[] = new byte[4];
+ addressBytes[0] = 0;
+ addressBytes[1] = 0;
+ addressBytes[2] = 0;
+ addressBytes[3] = 0;
+
+ try {
+ assertTrue("validate null handled ok", null == NetworkInterface
+ .getByInetAddress(null));
+ assertFalse(
+ "should not get here if getByInetAddress throws NullPointerException if null passed in",
+ true);
+ } catch (NullPointerException e) {
+ } catch (Exception e) {
+ fail("getByInetAddress, null inetAddress - raised exception : "
+ + e.getMessage());
+ }
+
+ try {
+ assertTrue(
+ "validate handled ok if we ask for address not associated with any interface",
+ null == NetworkInterface.getByInetAddress(InetAddress
+ .getByAddress(addressBytes)));
+ } catch (Exception e) {
+ assertFalse("getByInetAddress, unknown inetAddress - exception",
+ true);
+ }
+
+ // for each address in an interface validate that we get the right
+ // interface for that address
+ if (atLeastOneInterface) {
+ Enumeration addresses = networkInterface1.getInetAddresses();
+ if (addresses != null) {
+ while (addresses.hasMoreElements()) {
+ InetAddress theAddress = (InetAddress) addresses
+ .nextElement();
+ try {
+ assertTrue(
+ "validate that Interface can be obtained with any one of its addresses",
+ NetworkInterface.getByInetAddress(theAddress)
+ .equals(networkInterface1));
+ } catch (Exception e) {
+ assertFalse(
+ "validate to get address using inetAddress - socket exception",
+ true);
+ }
+ }
+ }
+ }
+
+ // validate that we get the right interface with the second interface as
+ // well (ie we just don't always get the first interface
+ if (atLeastTwoInterfaces) {
+ Enumeration addresses = networkInterface2.getInetAddresses();
+ if (addresses != null) {
+ while (addresses.hasMoreElements()) {
+ InetAddress theAddress = (InetAddress) addresses
+ .nextElement();
+ try {
+ assertTrue(
+ "validate that Interface can be obtained with any one of its addresses",
+ NetworkInterface.getByInetAddress(theAddress)
+ .equals(networkInterface2));
+ } catch (Exception e) {
+ assertFalse(
+ "validate to get address using inetAddress - socket exception",
+ true);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#getNetworkInterfaces()
+ */
+ public void test_getNetworkInterfaces() {
+
+ // really this is tested by all of the other calls but just make sure we
+ // can call it and get a list of interfaces if they exist
+ try {
+ Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces();
+ } catch (Exception e) {
+ fail("get Network Interfaces - raised exception : "
+ + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean
+ // java.net.SocketPermission.equals(java.lang.Object)
+ if (atLeastOneInterface) {
+ assertTrue("If objects are the same true is returned",
+ networkInterface1.equals(sameAsNetworkInterface1));
+ assertFalse("Validate Null handled ok", networkInterface1
+ .equals(null));
+ }
+ if (atLeastTwoInterfaces) {
+ assertFalse("If objects are different false is returned",
+ networkInterface1.equals(networkInterface2));
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#hashCode()
+ */
+ public void test_hashCode() {
+
+ if (atLeastOneInterface) {
+ assertTrue(
+ "validate that hash codes are the same for two calls on the same object",
+ networkInterface1.hashCode() == networkInterface1
+ .hashCode());
+ assertTrue(
+ "validate that hash codes are the same for two objects for which equals is true",
+ networkInterface1.hashCode() == sameAsNetworkInterface1
+ .hashCode());
+ }
+ }
+
+ /**
+ * @tests java.net.NetworkInterface#toString()
+ */
+ public void test_toString() {
+ if (atLeastOneInterface) {
+ assertTrue("validate that non null string is generated",
+ networkInterface1.toString() != null);
+ assertFalse("validate that non-zero length string is generated",
+ networkInterface1.toString().equals(""));
+ }
+ if (atLeastTwoInterfaces) {
+ assertFalse(
+ "Validate strings are different for different interfaces",
+ networkInterface1.toString().equals(
+ networkInterface2.toString()));
+ }
+ }
+
+ protected void setUp() {
+
+ Enumeration theInterfaces = null;
+ try {
+ theInterfaces = NetworkInterface.getNetworkInterfaces();
+ } catch (Exception e) {
+ }
+ if ((theInterfaces != null) && (theInterfaces.hasMoreElements())) {
+ while ((theInterfaces.hasMoreElements())
+ && (atLeastOneInterface == false)) {
+ NetworkInterface theInterface = (NetworkInterface) theInterfaces
+ .nextElement();
+ if (theInterface.getInetAddresses() != null) {
+ atLeastOneInterface = true;
+ networkInterface1 = theInterface;
+ }
+ }
+
+ while ((theInterfaces.hasMoreElements())
+ && (atLeastTwoInterfaces == false)) {
+ NetworkInterface theInterface = (NetworkInterface) theInterfaces
+ .nextElement();
+ if (theInterface.getInetAddresses() != null) {
+ atLeastTwoInterfaces = true;
+ networkInterface2 = theInterface;
+ }
+ }
+
+ Enumeration addresses = networkInterface1.getInetAddresses();
+ if (addresses != null) {
+ try {
+ sameAsNetworkInterface1 = NetworkInterface
+ .getByInetAddress((InetAddress) addresses
+ .nextElement());
+ } catch (SocketException e) {
+ }
+ }
+ }
+
+ }
+
+ protected void tearDown() {
+ System.setSecurityManager(null);
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NoRouteToHostExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NoRouteToHostExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NoRouteToHostExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/NoRouteToHostExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,68 @@
+/* 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.net.NoRouteToHostException;
+
+public class NoRouteToHostExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.NoRouteToHostException#NoRouteToHostException()
+ */
+ public void test_Constructor() {
+ // Test for method java.net.NoRouteToHostException()
+
+ try {
+ if (true)
+ throw new NoRouteToHostException();
+ } catch (NoRouteToHostException e) {
+ return;
+ }
+ fail("Failed to generate expected exception");
+ }
+
+ /**
+ * @tests java.net.NoRouteToHostException#NoRouteToHostException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.net.NoRouteToHostException(java.lang.String)
+ // Cannot test correctly without changing some routing tables !!
+
+ try {
+ if (true)
+ throw new NoRouteToHostException("test");
+ } catch (NoRouteToHostException e) {
+ assertTrue("Threw exception with incorrect message", e.getMessage()
+ .equals("test"));
+ return;
+ }
+ fail("Failed to generate expected exception");
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/PasswordAuthenticationTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/PasswordAuthenticationTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/PasswordAuthenticationTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/PasswordAuthenticationTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,72 @@
+/* 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.net.PasswordAuthentication;
+
+public class PasswordAuthenticationTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.PasswordAuthentication#PasswordAuthentication(java.lang.String,
+ * char[])
+ */
+ public void test_ConstructorLjava_lang_String$C() {
+ // Test for method java.net.PasswordAuthentication(java.lang.String,
+ // char [])
+ char[] password = new char[] { 'd', 'r', 'o', 'w', 's', 's', 'a', 'p' };
+ final String name = "Joe Blow";
+ PasswordAuthentication pa = new PasswordAuthentication(name, password);
+ char[] returnedPassword = pa.getPassword();
+ assertTrue("Incorrect name", pa.getUserName().equals(name));
+ assertTrue("Password was not cloned", returnedPassword != password);
+ assertTrue("Passwords not equal length",
+ returnedPassword.length == password.length);
+ for (int counter = password.length - 1; counter >= 0; counter--)
+ assertTrue("Passwords not equal",
+ returnedPassword[counter] == password[counter]);
+ }
+
+ /**
+ * @tests java.net.PasswordAuthentication#getPassword()
+ */
+ public void test_getPassword() {
+ // Test for method char [] java.net.PasswordAuthentication.getPassword()
+ assertTrue("Used to test", true);
+ }
+
+ /**
+ * @tests java.net.PasswordAuthentication#getUserName()
+ */
+ public void test_getUserName() {
+ // Test for method java.lang.String
+ // java.net.PasswordAuthentication.getUserName()
+ assertTrue("Used to test", true);
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProtocolExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProtocolExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProtocolExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ProtocolExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,65 @@
+/* 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.net.ProtocolException;
+
+public class ProtocolExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.net.ProtocolException#ProtocolException()
+ */
+ public void test_Constructor() {
+ // Test for method java.net.ProtocolException()
+ try {
+ throw new ProtocolException();
+ } catch (ProtocolException e) {
+ return;
+ } catch (Exception e) {
+ fail("Exception during ProtocolException test : " + e.getMessage());
+ }
+ fail("Failed to generate expected exception");
+ }
+
+ /**
+ * @tests java.net.ProtocolException#ProtocolException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.net.ProtocolException(java.lang.String)
+ try {
+ throw new ProtocolException("Some error message");
+ } catch (ProtocolException e) {
+ return;
+ } catch (Exception e) {
+ fail("Exception during ProtocolException test : " + e.getMessage());
+ }
+ fail("Failed to generate expected exception");
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}