InetAddressTest.java revision b416ef5dc224630af2b9393a15ae120b27e4864a
1f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes/* 2f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * Copyright (C) 2011 The Android Open Source Project 3f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * 4f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * you may not use this file except in compliance with the License. 6f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * You may obtain a copy of the License at 7f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * 8f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * 10f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * Unless required by applicable law or agreed to in writing, software 11f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * See the License for the specific language governing permissions and 14f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes * limitations under the License. 15f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes */ 16f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes 17f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughespackage libcore.java.net; 18f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes 1919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughesimport java.net.Inet4Address; 2019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughesimport java.net.Inet6Address; 219b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.net.InetAddress; 229b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.net.NetworkInterface; 23fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughesimport java.net.UnknownHostException; 249b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.util.Collections; 25b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilsonimport libcore.util.SerializationTester; 26f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes 27f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughespublic class InetAddressTest extends junit.framework.TestCase { 2819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 2919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 30fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes private static final String[] INVALID_IPv4_NUMERIC_ADDRESSES = new String[] { 31fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // IPv4 addresses may not be surrounded by square brackets. 32fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "[127.0.0.1]", 33fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 34fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Trailing dots are not allowed. 35fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3.4.", 36fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Nor is any kind of trailing junk. 37fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3.4hello", 38fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 39fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Out of range. 40fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "256.2.3.4", 41fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.256.3.4", 42fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.256.4", 43fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3.256", 44fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 45fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Deprecated. 46fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3", 47fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2", 48fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1", 49fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1234", 50fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0", // Single out the deprecated form of the ANY address. 51fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 52fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Hex. 53fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0x1.0x2.0x3.0x4", 54fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0x7f.0x00.0x00.0x01", 55fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "7f.0.0.1", 56fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 57fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Octal. 58fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0177.00.00.01", // Historically, this would have been interpreted as 127.0.0.1. 59fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 60fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Negative numbers. 61fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "-1.0.0.1", 62fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.-1.0.1", 63fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.0.-1.1", 64fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.0.0.-1", 65fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes }; 66fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 6719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static Inet6Address loopback6() throws Exception { 6819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES); 6919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 7019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 7119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static Inet6Address localhost6() throws Exception { 7219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes return (Inet6Address) InetAddress.getByAddress("localhost", LOOPBACK6_BYTES); 7319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 7419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 75f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes public void test_parseNumericAddress() throws Exception { 76ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes // Regular IPv4. 77ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString()); 78ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes // Regular IPv6. 79ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString()); 801c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Optional square brackets around IPv6 addresses, including mapped IPv4. 811c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString()); 821c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString()); 83fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 841c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes try { 85fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress("example.com"); // Not numeric. 86f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes fail(); 87f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } catch (IllegalArgumentException expected) { 88f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 89fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 90fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 91fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 92fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress(invalid); 93fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 94fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (IllegalArgumentException expected) { 95fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 96f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 97fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 9846bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes // Strange special cases, for compatibility with InetAddress.getByName. 9946bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress()); 10046bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress()); 101f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 1025d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes 103fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_isNumeric() throws Exception { 104fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("1.2.3.4")); 105fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("127.0.0.1")); 106fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 107fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(InetAddress.isNumeric("example.com")); 108fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 109fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 110fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(invalid, InetAddress.isNumeric(invalid)); 111fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 1125d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes } 113775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes 114a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isLinkLocalAddress() throws Exception { 115a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress()); 116a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress()); 117a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 118a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress()); 119a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress()); 120a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 121a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 122a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isMCSiteLocalAddress() throws Exception { 123a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal()); 124a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal()); 125a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal()); 126a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal()); 127a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 128a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal()); 129a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal()); 130a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal()); 131a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 132a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 1339b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes public void test_isReachable() throws Exception { 1349b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes // http://code.google.com/p/android/issues/detail?id=20203 13510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af" 13610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737" 13710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300" 13810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "00000274000e7777772e676f6f676c652e636f6d"; 13910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson InetAddress inetAddress = InetAddress.getByName("www.google.com"); 140b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilson new SerializationTester<InetAddress>(inetAddress, s) { 14110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected void verify(InetAddress deserialized) throws Exception { 14210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(500); 14310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson for (NetworkInterface nif 14410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson : Collections.list(NetworkInterface.getNetworkInterfaces())) { 14510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(nif, 20, 500); 14610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 14710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 14810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected boolean equals(InetAddress a, InetAddress b) { 14910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson return a.getHostName().equals(b.getHostName()); 15010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 15110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson }.test(); 1529b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes } 1539b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes 154a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isSiteLocalAddress() throws Exception { 155a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress()); 156a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress()); 157a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress()); 158a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress()); 159a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress()); 160a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 161a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress()); 162a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress()); 163a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 164a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 165fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getByName() throws Exception { 166fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 167fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 168fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName(invalid); 169fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 170fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 171fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 172fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 173fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 174fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 175fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getLoopbackAddress() throws Exception { 176fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress()); 177775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes } 17819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 17919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_equals() throws Exception { 18019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress addr = InetAddress.getByName("239.191.255.255"); 18119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr.equals(addr)); 18219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().equals(localhost6())); 18319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertFalse(addr.equals(loopback6())); 18419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 1855bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK)); 186fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 187fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances. 188fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes byte[] bs = new byte[16]; 189fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2)); 19019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 19119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 19219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_getHostAddress() throws Exception { 19319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", localhost6().getHostAddress()); 19419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", InetAddress.getByName("::1").getHostAddress()); 19519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 1965bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress()); 1975bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes 19819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress aAddr = InetAddress.getByName("224.0.0.0"); 19919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("224.0.0.0", aAddr.getHostAddress()); 20019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 20119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 202fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 203fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName("1"); 204fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(); 205fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 206fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 20719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 20819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] bAddr = { 20919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00, 21019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 21119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF, 21219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2 21319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 21419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(bAddr); 21519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String aString = aAddr.getHostAddress(); 21619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2")); 21719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 21819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] cAddr = { 21919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 22019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 22119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 22219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF 22319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 22419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(cAddr); 22519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress()); 22619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 22719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] dAddr = { 22819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 22919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 23019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 23119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 23219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 23319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(dAddr); 23419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aString = aAddr.getHostAddress(); 23519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::")); 23619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 23719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] eAddr = { 23819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, 23919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, 24019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b, 24119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f 24219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 24319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(eAddr); 24419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress()); 24519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 24619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] fAddr = { 24719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30, 24819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70, 24919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0, 25019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0 25119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 25219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(fAddr); 25319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress()); 25419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 25519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 25619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_hashCode() throws Exception { 257fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr1 = InetAddress.getByName("1.0.0.1"); 258fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr2 = InetAddress.getByName("1.0.0.1"); 25919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr1.hashCode() == addr2.hashCode()); 26019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 26119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().hashCode() == localhost6().hashCode()); 26219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 26319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 26419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_toString() throws Exception { 26519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String validIPAddresses[] = { 266fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "::1.2.3.4", "::", "::", "1::0", "1::", "::1", 26719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 26819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", 26919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" 27019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 27119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 27219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String [] resultStrings = { 273fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1", 274fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 27519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::", 27619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/::" 27719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 27819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 27919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes for(int i = 0; i < validIPAddresses.length; i++) { 28019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress ia = InetAddress.getByName(validIPAddresses[i]); 28119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String result = ia.toString(); 28219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertNotNull(result); 28319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals(resultStrings[i], result); 28419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 28519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 286f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes} 287