diff options
Diffstat (limited to 'luni/src/test/java/libcore/java/net/InetAddressTest.java')
-rw-r--r-- | luni/src/test/java/libcore/java/net/InetAddressTest.java | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/luni/src/test/java/libcore/java/net/InetAddressTest.java b/luni/src/test/java/libcore/java/net/InetAddressTest.java index 4b656cc..8bdcf64 100644 --- a/luni/src/test/java/libcore/java/net/InetAddressTest.java +++ b/luni/src/test/java/libcore/java/net/InetAddressTest.java @@ -21,10 +21,14 @@ import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; +import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; +import java.util.Set; import libcore.util.SerializationTester; public class InetAddressTest extends junit.framework.TestCase { + private static final byte[] LOOPBACK4_BYTES = new byte[] { 127, 0, 0, 1 }; private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; private static final String[] INVALID_IPv4_NUMERIC_ADDRESSES = new String[] { @@ -72,7 +76,7 @@ public class InetAddressTest extends junit.framework.TestCase { } private static Inet6Address localhost6() throws Exception { - return (Inet6Address) InetAddress.getByAddress("localhost", LOOPBACK6_BYTES); + return (Inet6Address) InetAddress.getByAddress("ip6-localhost", LOOPBACK6_BYTES); } public void test_parseNumericAddress() throws Exception { @@ -311,4 +315,84 @@ public class InetAddressTest extends junit.framework.TestCase { assertEquals(resultStrings[i], result); } } + + public void test_getHostNameCaches() throws Exception { + InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES); + assertEquals("::1", inetAddress.getHostString()); + assertEquals("ip6-localhost", inetAddress.getHostName()); + // getHostString() should now be different. + assertEquals("ip6-localhost", inetAddress.getHostString()); + } + + public void test_getByAddress_loopbackIpv4() throws Exception { + InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK4_BYTES); + assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress); + assertTrue(inetAddress.isLoopbackAddress()); + } + + public void test_getByAddress_loopbackIpv6() throws Exception { + InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES); + assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress); + assertTrue(inetAddress.isLoopbackAddress()); + } + + public void test_getByName_loopbackIpv4() throws Exception { + InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); + assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress); + assertTrue(inetAddress.isLoopbackAddress()); + } + + public void test_getByName_loopbackIpv6() throws Exception { + InetAddress inetAddress = InetAddress.getByName("::1"); + assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress); + assertTrue(inetAddress.isLoopbackAddress()); + } + + public void test_getAllByName_localhost() throws Exception { + InetAddress[] inetAddresses = InetAddress.getAllByName("localhost"); + assertEquals(1, inetAddresses.length); + InetAddress inetAddress = inetAddresses[0]; + assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress); + assertTrue(inetAddress.isLoopbackAddress()); + } + + public void test_getAllByName_ip6_localhost() throws Exception { + InetAddress[] inetAddresses = InetAddress.getAllByName("ip6-localhost"); + assertEquals(1, inetAddresses.length); + InetAddress inetAddress = inetAddresses[0]; + assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress); + assertTrue(inetAddress.isLoopbackAddress()); + } + + public void test_getByName_null() throws Exception { + InetAddress inetAddress = InetAddress.getByName("::1"); + + Set<InetAddress> expectedLoopbackAddresses = + createSet(Inet4Address.LOOPBACK, Inet6Address.LOOPBACK); + assertTrue(expectedLoopbackAddresses.contains(inetAddress)); + } + + public void test_getAllByName_null() throws Exception { + InetAddress[] inetAddresses = InetAddress.getAllByName(null); + assertEquals(2, inetAddresses.length); + Set<InetAddress> expectedLoopbackAddresses = + createSet(Inet4Address.LOOPBACK, Inet6Address.LOOPBACK); + assertEquals(expectedLoopbackAddresses, createSet(inetAddresses)); + } + + private static void assertEquals( + byte[] expectedAddressBytes, String expectedHostname, InetAddress actual) { + assertArrayEquals(expectedAddressBytes, actual.getAddress()); + assertEquals(expectedHostname, actual.getHostName()); + + } + + private static void assertArrayEquals(byte[] expected, byte[] actual) { + assertTrue("Expected=" + Arrays.toString(expected) + ", actual=" + Arrays.toString(actual), + Arrays.equals(expected, actual)); + } + + private static Set<InetAddress> createSet(InetAddress... members) { + return new HashSet<InetAddress>(Arrays.asList(members)); + } } |