InetAddressTest.java revision 2ea0b34ba1e3bf712e052304747a3bc6c78e8208
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()); 802ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Mapped IPv4 812ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("::ffff:127.0.0.1").toString()); 821c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Optional square brackets around IPv6 addresses, including mapped IPv4. 831c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString()); 841c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString()); 85fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 861c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes try { 87fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress("example.com"); // Not numeric. 88f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes fail(); 89f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } catch (IllegalArgumentException expected) { 90f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 91fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 92fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 93fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 94fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress(invalid); 95fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 96fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (IllegalArgumentException expected) { 97fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 98f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 99fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 10046bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes // Strange special cases, for compatibility with InetAddress.getByName. 10146bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress()); 10246bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress()); 103f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 1045d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes 105fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_isNumeric() throws Exception { 1062ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv4 107fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("1.2.3.4")); 108fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("127.0.0.1")); 109fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 1102ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv6 1112ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("::1")); 1122ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("2001:4860:800d::68")); 1132ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1142ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Mapped IPv4 1152ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("::ffff:127.0.0.1")); 1162ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1172ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Optional square brackets around IPv6 addresses, including mapped IPv4. 1182ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("[2001:4860:800d::68]")); 1192ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("[::ffff:127.0.0.1]")); 1202ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1212ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Negative test 122fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(InetAddress.isNumeric("example.com")); 123fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 124fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 125fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(invalid, InetAddress.isNumeric(invalid)); 126fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 1275d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes } 128775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes 129a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isLinkLocalAddress() throws Exception { 130a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress()); 1312ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertFalse(InetAddress.getByName("::ffff:127.0.0.1").isLinkLocalAddress()); 132a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress()); 133a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 134a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress()); 135a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress()); 136a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 137a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 138a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isMCSiteLocalAddress() throws Exception { 139a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal()); 140a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal()); 141a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal()); 142a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal()); 143a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 144a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal()); 145a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal()); 146a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal()); 147a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 148a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 1499b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes public void test_isReachable() throws Exception { 1509b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes // http://code.google.com/p/android/issues/detail?id=20203 15110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af" 15210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737" 15310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300" 15410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "00000274000e7777772e676f6f676c652e636f6d"; 15510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson InetAddress inetAddress = InetAddress.getByName("www.google.com"); 156b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilson new SerializationTester<InetAddress>(inetAddress, s) { 15710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected void verify(InetAddress deserialized) throws Exception { 15810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(500); 15910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson for (NetworkInterface nif 16010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson : Collections.list(NetworkInterface.getNetworkInterfaces())) { 16110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(nif, 20, 500); 16210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 16310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 16410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected boolean equals(InetAddress a, InetAddress b) { 16510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson return a.getHostName().equals(b.getHostName()); 16610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 16710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson }.test(); 1689b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes } 1699b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes 170a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isSiteLocalAddress() throws Exception { 171a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress()); 172a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress()); 173a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress()); 174a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress()); 175a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress()); 176a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 177a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress()); 178a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress()); 179a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 180a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 181fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getByName() throws Exception { 182fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 183fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 184fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName(invalid); 185fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 186fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 187fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 188fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 189fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 190fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 191fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getLoopbackAddress() throws Exception { 192fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress()); 193775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes } 19419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 19519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_equals() throws Exception { 19619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress addr = InetAddress.getByName("239.191.255.255"); 19719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr.equals(addr)); 19819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().equals(localhost6())); 19919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertFalse(addr.equals(loopback6())); 20019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 2015bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK)); 202fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 203fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances. 204fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes byte[] bs = new byte[16]; 205fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2)); 20619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 20719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 20819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_getHostAddress() throws Exception { 20919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", localhost6().getHostAddress()); 21019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", InetAddress.getByName("::1").getHostAddress()); 21119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 2125bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress()); 2135bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes 2142ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv4 mapped address 2152ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertEquals("127.0.0.1", InetAddress.getByName("::ffff:127.0.0.1").getHostAddress()); 2162ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 21719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress aAddr = InetAddress.getByName("224.0.0.0"); 21819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("224.0.0.0", aAddr.getHostAddress()); 21919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 22019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 221fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 222fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName("1"); 223fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(); 224fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 225fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 22619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 22719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] bAddr = { 22819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00, 22919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 23019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF, 23119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2 23219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 23319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(bAddr); 23419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String aString = aAddr.getHostAddress(); 23519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2")); 23619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 23719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] cAddr = { 23819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 23919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 24019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 24119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF 24219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 24319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(cAddr); 24419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress()); 24519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 24619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] dAddr = { 24719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 24819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 24919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 25019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 25119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 25219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(dAddr); 25319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aString = aAddr.getHostAddress(); 25419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::")); 25519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 25619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] eAddr = { 25719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, 25819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, 25919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b, 26019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f 26119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 26219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(eAddr); 26319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress()); 26419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 26519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] fAddr = { 26619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30, 26719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70, 26819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0, 26919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0 27019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 27119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(fAddr); 27219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress()); 27319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 27419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 27519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_hashCode() throws Exception { 276fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr1 = InetAddress.getByName("1.0.0.1"); 277fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr2 = InetAddress.getByName("1.0.0.1"); 27819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr1.hashCode() == addr2.hashCode()); 27919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 28019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().hashCode() == localhost6().hashCode()); 28119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 28219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 28319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_toString() throws Exception { 28419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String validIPAddresses[] = { 285fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "::1.2.3.4", "::", "::", "1::0", "1::", "::1", 28619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 28719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", 28819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" 28919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 29019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 29119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String [] resultStrings = { 292fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1", 293fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 29419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::", 29519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/::" 29619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 29719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 29819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes for(int i = 0; i < validIPAddresses.length; i++) { 29919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress ia = InetAddress.getByName(validIPAddresses[i]); 30019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String result = ia.toString(); 30119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertNotNull(result); 30219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals(resultStrings[i], result); 30319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 30419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 305f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes} 306