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 19fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamathimport java.io.IOException; 2019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughesimport java.net.Inet4Address; 2119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughesimport java.net.Inet6Address; 229b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.net.InetAddress; 239b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.net.NetworkInterface; 24fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamathimport java.net.SocketException; 25fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughesimport java.net.UnknownHostException; 26a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fullerimport java.util.Arrays; 279b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.util.Collections; 28a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fullerimport java.util.HashSet; 29a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fullerimport java.util.Set; 30b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilsonimport libcore.util.SerializationTester; 31f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes 32f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughespublic class InetAddressTest extends junit.framework.TestCase { 33a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller private static final byte[] LOOPBACK4_BYTES = new byte[] { 127, 0, 0, 1 }; 3419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 3519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 36fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes private static final String[] INVALID_IPv4_NUMERIC_ADDRESSES = new String[] { 37fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // IPv4 addresses may not be surrounded by square brackets. 38fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "[127.0.0.1]", 39fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 40fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Trailing dots are not allowed. 41fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3.4.", 42fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Nor is any kind of trailing junk. 43fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3.4hello", 44fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 45fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Out of range. 46fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "256.2.3.4", 47fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.256.3.4", 48fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.256.4", 49fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3.256", 50fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 51fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Deprecated. 52fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2.3", 53fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.2", 54fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1", 55fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1234", 56fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0", // Single out the deprecated form of the ANY address. 57fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 58e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Hex. Not supported by Android but supported by the RI. 59fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0x1.0x2.0x3.0x4", 60fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "0x7f.0x00.0x00.0x01", 61fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "7f.0.0.1", 62fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 63e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Octal. Not supported by Android but supported by the RI. In the RI, if any of the numbers 64e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // cannot be treated as a decimal the entire IP is interpreted differently, leading to 65e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // "0177.00.00.01" -> 177.0.0.1, but "0177.0x0.00.01" -> 127.0.0.1. 66e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Android does not do this. 67e5dc23234307897754529a782a6c546c496a4acfNeil Fuller "0256.00.00.01", // Historically, this could have been interpreted as 174.0.0.1. 68fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 69fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // Negative numbers. 70fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "-1.0.0.1", 71fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.-1.0.1", 72fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.0.-1.1", 73fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "1.0.0.-1", 74fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes }; 75fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 7619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static Inet6Address loopback6() throws Exception { 7719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES); 7819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 7919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 8019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes private static Inet6Address localhost6() throws Exception { 81a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller return (Inet6Address) InetAddress.getByAddress("ip6-localhost", LOOPBACK6_BYTES); 8219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 8319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 84f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes public void test_parseNumericAddress() throws Exception { 85ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes // Regular IPv4. 86ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString()); 87ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes // Regular IPv6. 88ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString()); 892ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Mapped IPv4 902ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("::ffff:127.0.0.1").toString()); 911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes // Optional square brackets around IPv6 addresses, including mapped IPv4. 921c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString()); 931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString()); 94fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 951c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes try { 96fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress("example.com"); // Not numeric. 97f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes fail(); 98f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } catch (IllegalArgumentException expected) { 99f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 100fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 101e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Android does not recognize Octal (leading 0) cases: they are treated as decimal. 102e5dc23234307897754529a782a6c546c496a4acfNeil Fuller assertEquals("/177.0.0.1", InetAddress.parseNumericAddress("0177.00.00.01").toString()); 103e5dc23234307897754529a782a6c546c496a4acfNeil Fuller 104fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 105fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 106fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.parseNumericAddress(invalid); 107fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 108fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (IllegalArgumentException expected) { 109fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 110f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 111fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 11246bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes // Strange special cases, for compatibility with InetAddress.getByName. 11346bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress()); 11446bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress()); 115f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes } 1165d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes 117fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_isNumeric() throws Exception { 1182ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv4 119fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("1.2.3.4")); 120fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.isNumeric("127.0.0.1")); 121fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 1222ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv6 1232ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("::1")); 1242ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("2001:4860:800d::68")); 1252ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1262ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Mapped IPv4 1272ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("::ffff:127.0.0.1")); 1282ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1292ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Optional square brackets around IPv6 addresses, including mapped IPv4. 1302ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("[2001:4860:800d::68]")); 1312ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertTrue(InetAddress.isNumeric("[::ffff:127.0.0.1]")); 1322ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 1332ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // Negative test 134fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(InetAddress.isNumeric("example.com")); 135fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 136e5dc23234307897754529a782a6c546c496a4acfNeil Fuller // Android does not handle Octal (leading 0) cases: they are treated as decimal. 137e5dc23234307897754529a782a6c546c496a4acfNeil Fuller assertTrue(InetAddress.isNumeric("0177.00.00.01")); // Interpreted as 177.0.0.1 138e5dc23234307897754529a782a6c546c496a4acfNeil Fuller 139fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 140fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertFalse(invalid, InetAddress.isNumeric(invalid)); 141fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 1425d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes } 143775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes 144a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isLinkLocalAddress() throws Exception { 145a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress()); 1462ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertFalse(InetAddress.getByName("::ffff:127.0.0.1").isLinkLocalAddress()); 147a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress()); 148a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 149a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress()); 150a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress()); 151a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 152a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 153a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isMCSiteLocalAddress() throws Exception { 154a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal()); 155a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal()); 156a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal()); 157a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal()); 158a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 159a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal()); 160a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal()); 161a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal()); 162a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 163a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 1649b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes public void test_isReachable() throws Exception { 1659b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes // http://code.google.com/p/android/issues/detail?id=20203 16610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af" 16710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737" 16810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300" 16910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson + "00000274000e7777772e676f6f676c652e636f6d"; 17010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson InetAddress inetAddress = InetAddress.getByName("www.google.com"); 171b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilson new SerializationTester<InetAddress>(inetAddress, s) { 17210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected void verify(InetAddress deserialized) throws Exception { 17310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(500); 17410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson for (NetworkInterface nif 17510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson : Collections.list(NetworkInterface.getNetworkInterfaces())) { 17610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson deserialized.isReachable(nif, 20, 500); 17710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 17810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 17910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson @Override protected boolean equals(InetAddress a, InetAddress b) { 18010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson return a.getHostName().equals(b.getHostName()); 18110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson } 18210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson }.test(); 1839b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes } 1849b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes 185fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath public void test_isReachable_neverThrows() throws Exception { 186fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath InetAddress inetAddress = InetAddress.getByName("www.google.com"); 187fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath 188fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath final NetworkInterface netIf; 189fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath try { 190fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath netIf = NetworkInterface.getByName("dummy0"); 191fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath } catch (SocketException e) { 192fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath System.logI("Skipping test_isReachable_neverThrows because dummy0 isn't available"); 193fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath return; 194fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath } 195fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath 196fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath assertFalse(inetAddress.isReachable(netIf, 256, 500)); 197fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath } 198fe8d46c16e53822545b3c37bb7636abf2c342074Narayan Kamath 199a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes public void test_isSiteLocalAddress() throws Exception { 200a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress()); 201a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress()); 202a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress()); 203a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress()); 204a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress()); 205a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 206a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress()); 207a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress()); 208a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes } 209a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes 210fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getByName() throws Exception { 211fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) { 212fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 213fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName(invalid); 214fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(invalid); 215fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 216fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 217fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 218fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 219fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 220fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes public void test_getLoopbackAddress() throws Exception { 221fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress()); 222775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes } 22319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 22419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_equals() throws Exception { 22519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress addr = InetAddress.getByName("239.191.255.255"); 22619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr.equals(addr)); 22719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().equals(localhost6())); 22819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertFalse(addr.equals(loopback6())); 22919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 2305bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK)); 231fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes 232fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances. 233fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes byte[] bs = new byte[16]; 234fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2)); 23519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 23619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 23719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_getHostAddress() throws Exception { 23819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", localhost6().getHostAddress()); 23919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("::1", InetAddress.getByName("::1").getHostAddress()); 24019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 2415bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress()); 2425bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes 2432ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root // IPv4 mapped address 2442ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root assertEquals("127.0.0.1", InetAddress.getByName("::ffff:127.0.0.1").getHostAddress()); 2452ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root 24619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress aAddr = InetAddress.getByName("224.0.0.0"); 24719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("224.0.0.0", aAddr.getHostAddress()); 24819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 24919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 250fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes try { 251fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress.getByName("1"); 252fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes fail(); 253fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } catch (UnknownHostException expected) { 254fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes } 25519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 25619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] bAddr = { 25719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00, 25819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 25919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF, 26019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2 26119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 26219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(bAddr); 26319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String aString = aAddr.getHostAddress(); 26419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2")); 26519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 26619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] cAddr = { 26719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 26819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 26919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 27019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF 27119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 27219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(cAddr); 27319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress()); 27419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 27519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] dAddr = { 27619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 27719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 27819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 27919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 28019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 28119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(dAddr); 28219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aString = aAddr.getHostAddress(); 28319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::")); 28419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 28519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] eAddr = { 28619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, 28719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, 28819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b, 28919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f 29019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 29119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(eAddr); 29219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress()); 29319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 29419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes byte[] fAddr = { 29519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30, 29619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70, 29719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0, 29819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0 29919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 30019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes aAddr = Inet6Address.getByAddress(fAddr); 30119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress()); 30219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 30319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 30419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_hashCode() throws Exception { 305fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr1 = InetAddress.getByName("1.0.0.1"); 306fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes InetAddress addr2 = InetAddress.getByName("1.0.0.1"); 30719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(addr1.hashCode() == addr2.hashCode()); 30819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 30919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertTrue(loopback6().hashCode() == localhost6().hashCode()); 31019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 31119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 31219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes public void test_toString() throws Exception { 31319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String validIPAddresses[] = { 314fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "::1.2.3.4", "::", "::", "1::0", "1::", "::1", 31519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 31619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", 31719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" 31819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 31919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 32019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String [] resultStrings = { 321fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1", 322fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 32319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::", 32419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes "/::" 32519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes }; 32619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes 32719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes for(int i = 0; i < validIPAddresses.length; i++) { 32819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes InetAddress ia = InetAddress.getByName(validIPAddresses[i]); 32919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes String result = ia.toString(); 33019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertNotNull(result); 33119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes assertEquals(resultStrings[i], result); 33219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 33319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes } 334ee458cf73d150be182b1797bc59043b131706f7fNeil Fuller 335a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getHostNameCaches() throws Exception { 336a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES); 337b2f612dfda4bf2362413e3e205880ae7c1addde9Przemyslaw Szczepaniak // TODO(narayan): Investigate why these tests are suppressed. 338b2f612dfda4bf2362413e3e205880ae7c1addde9Przemyslaw Szczepaniak // assertEquals("::1", inetAddress.getHostString()); 339a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals("ip6-localhost", inetAddress.getHostName()); 340a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller // getHostString() should now be different. 341b2f612dfda4bf2362413e3e205880ae7c1addde9Przemyslaw Szczepaniak // assertEquals("ip6-localhost", inetAddress.getHostString()); 342a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 343a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 344a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getByAddress_loopbackIpv4() throws Exception { 345a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK4_BYTES); 346a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress); 347a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(inetAddress.isLoopbackAddress()); 348a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 349a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 350a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getByAddress_loopbackIpv6() throws Exception { 351a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES); 352a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress); 353a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(inetAddress.isLoopbackAddress()); 354a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 355a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 356a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getByName_loopbackIpv4() throws Exception { 357a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); 358a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress); 359a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(inetAddress.isLoopbackAddress()); 360a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 361a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 362a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getByName_loopbackIpv6() throws Exception { 363a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = InetAddress.getByName("::1"); 364a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress); 365a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(inetAddress.isLoopbackAddress()); 366a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 367a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 368db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong public void test_getByName_empty() throws Exception { 369db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong InetAddress inetAddress = InetAddress.getByName(""); 370db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong assertEquals(LOOPBACK6_BYTES, "localhost", inetAddress); 371db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong assertTrue(inetAddress.isLoopbackAddress()); 372db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong } 373db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong 374a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getAllByName_localhost() throws Exception { 375a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress[] inetAddresses = InetAddress.getAllByName("localhost"); 376a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(1, inetAddresses.length); 377a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = inetAddresses[0]; 378a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress); 379a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(inetAddress.isLoopbackAddress()); 380a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 381a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 382a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getAllByName_ip6_localhost() throws Exception { 383a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress[] inetAddresses = InetAddress.getAllByName("ip6-localhost"); 384a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(1, inetAddresses.length); 385a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = inetAddresses[0]; 386a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress); 387a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(inetAddress.isLoopbackAddress()); 388a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 389a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 390d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath public void test_getByName_v6loopback() throws Exception { 391a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress inetAddress = InetAddress.getByName("::1"); 392a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 393a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller Set<InetAddress> expectedLoopbackAddresses = 394a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller createSet(Inet4Address.LOOPBACK, Inet6Address.LOOPBACK); 395a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue(expectedLoopbackAddresses.contains(inetAddress)); 396a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 397a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 398d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath public void test_getByName_cloning() throws Exception { 399d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath InetAddress[] addresses = InetAddress.getAllByName(null); 400d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath InetAddress[] addresses2 = InetAddress.getAllByName(null); 401d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath assertNotNull(addresses[0]); 402d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath assertNotNull(addresses[1]); 403d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath assertNotSame(addresses, addresses2); 404d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath 405d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath // Also assert that changes to the return value do not affect the cache 406d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath // etc. i.e, that we return a copy. 407d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath addresses[0] = null; 408d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath addresses2 = InetAddress.getAllByName(null); 409d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath assertNotNull(addresses2[0]); 410d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath assertNotNull(addresses2[1]); 411d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath } 412d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath 413a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller public void test_getAllByName_null() throws Exception { 414a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller InetAddress[] inetAddresses = InetAddress.getAllByName(null); 415a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(2, inetAddresses.length); 416a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller Set<InetAddress> expectedLoopbackAddresses = 417a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller createSet(Inet4Address.LOOPBACK, Inet6Address.LOOPBACK); 418a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(expectedLoopbackAddresses, createSet(inetAddresses)); 419a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 420a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 421a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller private static void assertEquals( 422a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller byte[] expectedAddressBytes, String expectedHostname, InetAddress actual) { 423a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertArrayEquals(expectedAddressBytes, actual.getAddress()); 424a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertEquals(expectedHostname, actual.getHostName()); 425a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 426a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 427a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller 428a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller private static void assertArrayEquals(byte[] expected, byte[] actual) { 429a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller assertTrue("Expected=" + Arrays.toString(expected) + ", actual=" + Arrays.toString(actual), 430a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller Arrays.equals(expected, actual)); 431a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller } 432ee458cf73d150be182b1797bc59043b131706f7fNeil Fuller 433a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller private static Set<InetAddress> createSet(InetAddress... members) { 434a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller return new HashSet<InetAddress>(Arrays.asList(members)); 435ee458cf73d150be182b1797bc59043b131706f7fNeil Fuller } 436f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes} 437