diff options
author | Elliott Hughes <enh@google.com> | 2011-05-26 15:43:02 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2011-05-26 15:43:02 -0700 |
commit | 249c40b94b9f5c8944a44be8e30aa01a90f1cf48 (patch) | |
tree | 6ffaaf26a78928fd51a6359b786521bf0d685119 | |
parent | 163222ea86e16dbb3ddaabe1d6c549037f0c1b63 (diff) | |
download | apache-harmony-249c40b94b9f5c8944a44be8e30aa01a90f1cf48.tar.gz |
Fix more networking tests.
Change-Id: I1e8071e9850230c11abc86ab352358f764b50bd4
5 files changed, 30 insertions, 226 deletions
diff --git a/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/DatagramSocketTest.java b/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/DatagramSocketTest.java index bbe751e..1662130 100644 --- a/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/DatagramSocketTest.java +++ b/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/DatagramSocketTest.java @@ -137,9 +137,6 @@ public class DatagramSocketTest extends junit.framework.TestCase { } } - /** - * @tests java.net.DatagramSocket#connect(java.net.InetAddress, int) - */ public void test_connectLjava_net_InetAddressI() throws Exception { DatagramSocket ds = new DatagramSocket(); InetAddress inetAddress = InetAddress.getLocalHost(); @@ -148,22 +145,13 @@ public class DatagramSocketTest extends junit.framework.TestCase { assertEquals("Incorrect Port", 0, ds.getPort()); ds.disconnect(); - int portNumber; - if ("true".equals(System.getProperty("run.ipv6tests"))) { - System.out - .println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6GlobalAddressJcl4: " - + Support_Configuration.IPv6GlobalAddressJcl4); - - ds = new java.net.DatagramSocket(); - inetAddress = InetAddress - .getByName(Support_Configuration.IPv6GlobalAddressJcl4); - portNumber = Support_PortManager.getNextPortForUDP(); - ds.connect(inetAddress, portNumber); - assertTrue("Incorrect InetAddress", ds.getInetAddress().equals( - inetAddress)); - assertTrue("Incorrect Port", ds.getPort() == portNumber); - ds.disconnect(); - } + ds = new java.net.DatagramSocket(); + inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"); + int portNumber = Support_PortManager.getNextPortForUDP(); + ds.connect(inetAddress, portNumber); + assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(inetAddress)); + assertTrue("Incorrect Port", ds.getPort() == portNumber); + ds.disconnect(); // Create a connected datagram socket to test // PlainDatagramSocketImpl.peek() @@ -444,9 +432,6 @@ public class DatagramSocketTest extends junit.framework.TestCase { } } - /** - * @tests java.net.DatagramSocket#disconnect() - */ public void test_disconnect() throws Exception { DatagramSocket ds = new DatagramSocket(); InetAddress inetAddress = InetAddress.getLocalHost(); @@ -455,18 +440,12 @@ public class DatagramSocketTest extends junit.framework.TestCase { assertNull("Incorrect InetAddress", ds.getInetAddress()); assertEquals("Incorrect Port", -1, ds.getPort()); - if ("true".equals(System.getProperty("run.ipv6tests"))) { - System.out - .println("Running test_disconnect(DatagramSocketTest) with IPv6GlobalAddressJcl4: " - + Support_Configuration.IPv6GlobalAddressJcl4); - ds = new DatagramSocket(); - inetAddress = InetAddress - .getByName(Support_Configuration.IPv6GlobalAddressJcl4); - ds.connect(inetAddress, 0); - ds.disconnect(); - assertNull("Incorrect InetAddress", ds.getInetAddress()); - assertEquals("Incorrect Port", -1, ds.getPort()); - } + ds = new DatagramSocket(); + inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"); + ds.connect(inetAddress, 0); + ds.disconnect(); + assertNull("Incorrect InetAddress", ds.getInetAddress()); + assertEquals("Incorrect Port", -1, ds.getPort()); } /** diff --git a/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java b/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java index 10267b4..a606c07 100644 --- a/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java +++ b/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/SocketTest.java @@ -38,8 +38,6 @@ import java.security.Permission; import java.util.Arrays; import java.util.Locale; -import java.net.PlainSocketImpl; - import tests.support.Support_Configuration; public class SocketTest extends junit.framework.TestCase { @@ -277,40 +275,26 @@ public class SocketTest extends junit.framework.TestCase { Socket socket = new Socket(); try { socket.connect(new InetSocketAddress("unknownhost.invalid", 12345)); - fail("Should throw UnknownHostException"); - } catch (UnknownHostException e) { - // expected + fail(); + } catch (UnknownHostException expected) { } } - /** - * @tests Socket#connect(SocketAddress) - */ public void test_connect_unresolved() throws IOException { Socket socket = new Socket(); - - // Try a known host created by createUnresolved() + InetSocketAddress unresolved = InetSocketAddress.createUnresolved("www.apache.org", 80); try { - socket.connect(InetSocketAddress.createUnresolved("www.apache.org", - 80)); - fail("Should throw UnknownHostException"); - } catch (UnknownHostException e) { - // expected + socket.connect(unresolved); + fail(); + } catch (UnknownHostException expected) { } - - // Try an unknown host created by createUnresolved() try { - socket.connect(InetSocketAddress.createUnresolved( - "unknownhost.invalid", 12345)); - fail("Should throw UnknownHostException"); - } catch (UnknownHostException e) { - // expected + socket.connect(unresolved, 123); + fail(); + } catch (UnknownHostException expected) { } } - /** - * @tests java.net.Socket#connect(java.net.SocketAddress) - */ public void test_connectLjava_net_SocketAddress() throws Exception { @SuppressWarnings("serial") @@ -580,61 +564,6 @@ public class SocketTest extends junit.framework.TestCase { server.close(); } - /** - * @tests java.net.Socket#Socket(java.lang.String, int, - * java.net.InetAddress, int) - */ - public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI_ipv6() - throws IOException { - - boolean preferIPv6 = "true".equals(System - .getProperty("java.net.preferIPv6Addresses")); - boolean preferIPv4 = "true".equals(System - .getProperty("java.net.preferIPv4Stack")); - boolean runIPv6 = "true".equals(System.getProperty("run.ipv6tests")); - - if (!runIPv6 || !preferIPv6 || preferIPv4) { - // This test is not for me - return; - } - - ServerSocket server = new ServerSocket(0); - int serverPort = server.getLocalPort(); - Socket client = new Socket(InetAddress.getLocalHost().getHostName(), - serverPort, InetAddress.getLocalHost(), 0); - assertTrue("Failed to create socket", client.getPort() == serverPort); - client.close(); - - Socket theSocket = null; - try { - theSocket = new Socket(Support_Configuration.IPv6GlobalAddressJcl4, - serverPort, InetAddress.getLocalHost(), 0); - } catch (IOException e) { - // check here if InetAddress.getLocalHost() is returning the - // loopback address, if so that is likely the cause of the failure - assertFalse( - "Misconfiguration - local host is the loopback address", - InetAddress.getLocalHost().isLoopbackAddress()); - throw e; - } - - assertTrue(theSocket.isConnected()); - - try { - new Socket(Support_Configuration.IPv6GlobalAddressJcl4, serverPort, - theSocket.getLocalAddress(), theSocket.getLocalPort()); - fail("Was able to create two sockets on same port"); - } catch (IOException e) { - // Expected - } - - theSocket.close(); - server.close(); - } - - /** - * @tests java.net.Socket#Socket(java.lang.String, int, boolean) - */ @SuppressWarnings("deprecation") public void test_ConstructorLjava_lang_StringIZ() throws IOException { ServerSocket server = new ServerSocket(0); @@ -784,29 +713,10 @@ public class SocketTest extends junit.framework.TestCase { assertTrue("Returned incorrect InetAddress", client.getLocalAddress() .equals(InetAddress.getLocalHost())); - // now validate that behaviour when the any address is returned - String preferIPv4StackValue = System - .getProperty("java.net.preferIPv4Stack"); - String preferIPv6AddressesValue = System - .getProperty("java.net.preferIPv6Addresses"); - client = new Socket(); client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); + assertTrue(client.getLocalAddress().isAnyLocalAddress()); - if (((preferIPv4StackValue == null) || preferIPv4StackValue - .equalsIgnoreCase("false")) - && (preferIPv6AddressesValue != null) - && (preferIPv6AddressesValue.equals("true"))) { - assertTrue( - "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false " - + client.getLocalSocketAddress(), client - .getLocalAddress() instanceof Inet6Address); - } else { - assertTrue( - "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true " - + client.getLocalSocketAddress(), client - .getLocalAddress() instanceof Inet4Address); - } client.close(); server.close(); } @@ -825,9 +735,6 @@ public class SocketTest extends junit.framework.TestCase { server.close(); } - /** - * @tests java.net.Socket#getLocalSocketAddress() - */ public void test_getLocalSocketAddress() throws IOException { // set up server connect and then validate that we get the right // response for the local address @@ -860,48 +767,13 @@ public class SocketTest extends junit.framework.TestCase { // now validate the behaviour when the any address is returned client = new Socket(); client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); - - String preferIPv4StackValue = System - .getProperty("java.net.preferIPv4Stack"); - String preferIPv6AddressesValue = System - .getProperty("java.net.preferIPv6Addresses"); - if (((preferIPv4StackValue == null) || preferIPv4StackValue - .equalsIgnoreCase("false")) - && (preferIPv6AddressesValue != null) - && (preferIPv6AddressesValue.equals("true"))) { - assertTrue( - "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " - + client.getLocalSocketAddress(), - ((InetSocketAddress) client.getLocalSocketAddress()) - .getAddress() instanceof Inet6Address); - } else { - assertTrue( - "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " - + client.getLocalSocketAddress(), - ((InetSocketAddress) client.getLocalSocketAddress()) - .getAddress() instanceof Inet4Address); - } + assertTrue(((InetSocketAddress) client.getLocalSocketAddress()).getAddress().isAnyLocalAddress()); client.close(); // now validate the same for getLocalAddress client = new Socket(); client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); - if (((preferIPv4StackValue == null) || preferIPv4StackValue - .equalsIgnoreCase("false")) - && (preferIPv6AddressesValue != null) - && (preferIPv6AddressesValue.equals("true"))) { - assertTrue( - "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " - + client.getLocalSocketAddress(), - ((InetSocketAddress) client.getLocalSocketAddress()) - .getAddress() instanceof Inet6Address); - } else { - assertTrue( - "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " - + client.getLocalSocketAddress(), - ((InetSocketAddress) client.getLocalSocketAddress()) - .getAddress() instanceof Inet4Address); - } + assertTrue(client.getLocalAddress().isAnyLocalAddress()); client.close(); } @@ -1750,51 +1622,15 @@ public class SocketTest extends junit.framework.TestCase { s.close(); } - /** - * @tests java.net.Socket#toString() - */ public void test_toString() throws IOException { ServerSocket server = new ServerSocket(0); - Socket client = new Socket(InetAddress.getLocalHost(), server - .getLocalPort()); - - String expected = "Socket[addr=" + InetAddress.getLocalHost() - + ",port=" + client.getPort() + ",localport=" + Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); + // RI has "addr" and "localport" instead of "address" and "localPort". + String expected = "Socket[address=" + InetAddress.getLocalHost() + + ",port=" + client.getPort() + ",localPort=" + client.getLocalPort() + "]"; - assertEquals("Returned incorrect string", expected, client.toString()); + assertEquals(expected, client.toString()); client.close(); server.close(); } - - /** - * @tests {@link java.net.Socket#setSocketImplFactory(SocketImplFactory)} - */ - public void test_setSocketFactoryLjava_net_SocketImplFactory() - throws IOException { - SocketImplFactory factory = new MockSocketImplFactory(); - // Should not throw SocketException when set DatagramSocketImplFactory - // for the first time. - Socket.setSocketImplFactory(factory); - - try { - Socket.setSocketImplFactory(null); - fail("Should throw SocketException"); - } catch (SocketException e) { - // Expected - } - - try { - Socket.setSocketImplFactory(factory); - fail("Should throw SocketException"); - } catch (SocketException e) { - // Expected - } - } - - private class MockSocketImplFactory implements SocketImplFactory { - - public SocketImpl createSocketImpl() { - return new PlainSocketImpl(); - } - } } diff --git a/luni/src/test/resources/net.resources/README.txt b/luni/src/test/resources/net.resources/README.txt index 5acf249..564b126 100644 --- a/luni/src/test/resources/net.resources/README.txt +++ b/luni/src/test/resources/net.resources/README.txt @@ -129,9 +129,6 @@ The key properties required by the java.net tests are as follows ... present on the local network. This allows us to check the timeouts for socket connections. -* IPv6GlobalAddressJcl4 : the IPv6 address for a host on the network. - - If any of these properties are not set the *hard coded* defaults in the class tests.support.Support_Configuration will be used (see diff --git a/support/src/test/java/tests/support/Support_Configuration.java b/support/src/test/java/tests/support/Support_Configuration.java index be2fc5f..2d924a0 100644 --- a/support/src/test/java/tests/support/Support_Configuration.java +++ b/support/src/test/java/tests/support/Support_Configuration.java @@ -67,8 +67,6 @@ public class Support_Configuration { public static final String HomeAddress6 = "jcltest6.apache.org"; - public static String IPv6GlobalAddressJcl4 = "FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"; // this - public static String ProxyServerTestHostIPv6 = "jcltest6.apache.org"; // ip address that resolves to a host that is not present on the local @@ -217,11 +215,6 @@ public class Support_Configuration { ResolvedNotExistingHost = value; } - value = props.get("IPv6GlobalAddressJcl4"); - if (value != null) { - IPv6GlobalAddressJcl4 = value; - } - } static void load(InputStream in, Hashtable<String, String> result) throws IOException { diff --git a/support/src/test/resources/config/localhosttest.ini b/support/src/test/resources/config/localhosttest.ini index 3c78404..3725567 100644 --- a/support/src/test/resources/config/localhosttest.ini +++ b/support/src/test/resources/config/localhosttest.ini @@ -11,4 +11,3 @@ FTPTestAddress=jcltest:jclpass@localhost URLConnectionLastModifiedString=Sat, 07 Jan 2006 13:50:52 GMT URLConnectionLastModified=1136641852000 ResolvedNotExistingHost=9.26.194.72 -IPv6GlobalAddressJcl4= |