InetAddressTest.java revision e5dc23234307897754529a782a6c546c496a4acf
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 52e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Hex. Not supported by Android but supported by the RI. 53fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0x1.0x2.0x3.0x4", 54fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0x7f.0x00.0x00.0x01", 55fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "7f.0.0.1", 56fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 57e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Octal. Not supported by Android but supported by the RI. In the RI, if any of the numbers 58e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // cannot be treated as a decimal the entire IP is interpreted differently, leading to 59e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // "0177.00.00.01" -> 177.0.0.1, but "0177.0x0.00.01" -> 127.0.0.1. 60e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Android does not do this. 61e5dc23234307897754529a782a6c546c496a4acfNeil Fuller "0256.00.00.01", // Historically, this could have been interpreted as 174.0.0.1. 62fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 63fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Negative numbers. 64fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "-1.0.0.1", 65fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.-1.0.1", 66fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.0.-1.1", 67fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.0.0.-1", 68fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes }; 69fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 7019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static Inet6Address loopback6() throws Exception { 7119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES); 7219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 7319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 7419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static Inet6Address localhost6() throws Exception { 7519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes return (Inet6Address) InetAddress.getByAddress("localhost", LOOPBACK6_BYTES); 7619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 7719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 78f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes public void test_parseNumericAddress() throws Exception { 79ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes // Regular IPv4. 80ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString()); 81ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes // Regular IPv6. 82ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString()); 832ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Mapped IPv4 842ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("::ffff:127.0.0.1").toString()); 851c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Optional square brackets around IPv6 addresses, including mapped IPv4. 861c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString()); 871c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString()); 88fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 891c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes try { 90fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress("example.com"); // Not numeric. 91f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes fail(); 92f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } catch (IllegalArgumentException expected) { 93f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 94fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 95e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Android does not recognize Octal (leading 0) cases: they are treated as decimal. 96e5dc23234307897754529a782a6c546c496a4acfNeil Fuller assertEquals("/177.0.0.1", InetAddress.parseNumericAddress("0177.00.00.01").toString()); 97e5dc23234307897754529a782a6c546c496a4acfNeil Fuller 98fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 99fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 100fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress(invalid); 101fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 102fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (IllegalArgumentException expected) { 103fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 104f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 105fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 10646bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes // Strange special cases, for compatibility with InetAddress.getByName. 10746bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress()); 10846bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress()); 109f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 1105d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes 111fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_isNumeric() throws Exception { 1122ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv4 113fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("1.2.3.4")); 114fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("127.0.0.1")); 115fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 1162ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv6 1172ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("::1")); 1182ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("2001:4860:800d::68")); 1192ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1202ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Mapped IPv4 1212ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("::ffff:127.0.0.1")); 1222ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1232ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Optional square brackets around IPv6 addresses, including mapped IPv4. 1242ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("[2001:4860:800d::68]")); 1252ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("[::ffff:127.0.0.1]")); 1262ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1272ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Negative test 128fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(InetAddress.isNumeric("example.com")); 129fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 130e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Android does not handle Octal (leading 0) cases: they are treated as decimal. 131e5dc23234307897754529a782a6c546c496a4acfNeil Fuller assertTrue(InetAddress.isNumeric("0177.00.00.01")); // Interpreted as 177.0.0.1 132e5dc23234307897754529a782a6c546c496a4acfNeil Fuller 133fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 134fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(invalid, InetAddress.isNumeric(invalid)); 135fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 1365d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes } 137775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes 138a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isLinkLocalAddress() throws Exception { 139a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress()); 1402ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertFalse(InetAddress.getByName("::ffff:127.0.0.1").isLinkLocalAddress()); 141a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress()); 142a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 143a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress()); 144a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress()); 145a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 146a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 147a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isMCSiteLocalAddress() throws Exception { 148a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal()); 149a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal()); 150a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal()); 151a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal()); 152a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 153a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal()); 154a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal()); 155a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal()); 156a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 157a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 1589b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes public void test_isReachable() throws Exception { 1599b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes // http://code.google.com/p/android/issues/detail?id=20203 16010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af" 16110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737" 16210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300" 16310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "00000274000e7777772e676f6f676c652e636f6d"; 16410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson InetAddress inetAddress = InetAddress.getByName("www.google.com"); 165b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilson new SerializationTester<InetAddress>(inetAddress, s) { 16610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected void verify(InetAddress deserialized) throws Exception { 16710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(500); 16810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson for (NetworkInterface nif 16910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson : Collections.list(NetworkInterface.getNetworkInterfaces())) { 17010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(nif, 20, 500); 17110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 17210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 17310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected boolean equals(InetAddress a, InetAddress b) { 17410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson return a.getHostName().equals(b.getHostName()); 17510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 17610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson }.test(); 1779b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes } 1789b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes 179a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isSiteLocalAddress() throws Exception { 180a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress()); 181a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress()); 182a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress()); 183a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress()); 184a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress()); 185a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 186a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress()); 187a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress()); 188a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 189a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 190fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getByName() throws Exception { 191fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 192fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 193fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName(invalid); 194fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 195fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 196fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 197fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 198fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 199fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 200fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getLoopbackAddress() throws Exception { 201fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress()); 202775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes } 20319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 20419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_equals() throws Exception { 20519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress addr = InetAddress.getByName("239.191.255.255"); 20619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr.equals(addr)); 20719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().equals(localhost6())); 20819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertFalse(addr.equals(loopback6())); 20919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 2105bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK)); 211fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 212fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances. 213fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes byte[] bs = new byte[16]; 214fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2)); 21519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 21619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 21719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_getHostAddress() throws Exception { 21819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", localhost6().getHostAddress()); 21919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", InetAddress.getByName("::1").getHostAddress()); 22019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 2215bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress()); 2225bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes 2232ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv4 mapped address 2242ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertEquals("127.0.0.1", InetAddress.getByName("::ffff:127.0.0.1").getHostAddress()); 2252ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 22619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress aAddr = InetAddress.getByName("224.0.0.0"); 22719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("224.0.0.0", aAddr.getHostAddress()); 22819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 22919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 230fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 231fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName("1"); 232fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(); 233fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 234fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 23519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 23619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] bAddr = { 23719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00, 23819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 23919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF, 24019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2 24119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 24219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(bAddr); 24319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String aString = aAddr.getHostAddress(); 24419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2")); 24519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 24619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] cAddr = { 24719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 24819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 24919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 25019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF 25119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 25219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(cAddr); 25319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress()); 25419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 25519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] dAddr = { 25619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 25719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 25819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 25919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 26019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 26119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(dAddr); 26219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aString = aAddr.getHostAddress(); 26319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::")); 26419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 26519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] eAddr = { 26619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, 26719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, 26819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b, 26919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f 27019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 27119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(eAddr); 27219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress()); 27319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 27419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] fAddr = { 27519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30, 27619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70, 27719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0, 27819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0 27919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 28019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(fAddr); 28119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress()); 28219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 28319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 28419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_hashCode() throws Exception { 285fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr1 = InetAddress.getByName("1.0.0.1"); 286fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr2 = InetAddress.getByName("1.0.0.1"); 28719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr1.hashCode() == addr2.hashCode()); 28819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 28919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().hashCode() == localhost6().hashCode()); 29019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 29119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 29219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_toString() throws Exception { 29319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String validIPAddresses[] = { 294fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "::1.2.3.4", "::", "::", "1::0", "1::", "::1", 29519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 29619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", 29719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" 29819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 29919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 30019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String [] resultStrings = { 301fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1", 302fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 30319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::", 30419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/::" 30519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 30619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 30719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes for(int i = 0; i < validIPAddresses.length; i++) { 30819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress ia = InetAddress.getByName(validIPAddresses[i]); 30919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String result = ia.toString(); 31019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertNotNull(result); 31119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals(resultStrings[i], result); 31219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 31319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 314f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes} 315