InetAddressTest.java revision d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0
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;
24a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fullerimport java.util.Arrays;
259b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughesimport java.util.Collections;
26a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fullerimport java.util.HashSet;
27a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fullerimport java.util.Set;
28b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilsonimport libcore.util.SerializationTester;
29f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes
30f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughespublic class InetAddressTest extends junit.framework.TestCase {
31a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    private static final byte[] LOOPBACK4_BYTES = new byte[] { 127, 0, 0, 1 };
3219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
3319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
34fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    private static final String[] INVALID_IPv4_NUMERIC_ADDRESSES = new String[] {
35fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // IPv4 addresses may not be surrounded by square brackets.
36fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "[127.0.0.1]",
37fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
38fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Trailing dots are not allowed.
39fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.2.3.4.",
40fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Nor is any kind of trailing junk.
41fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.2.3.4hello",
42fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
43fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Out of range.
44fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "256.2.3.4",
45fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.256.3.4",
46fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.2.256.4",
47fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.2.3.256",
48fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
49fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Deprecated.
50fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.2.3",
51fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.2",
52fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1",
53fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1234",
54fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "0", // Single out the deprecated form of the ANY address.
55fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
56e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // Hex. Not supported by Android but supported by the RI.
57fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "0x1.0x2.0x3.0x4",
58fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "0x7f.0x00.0x00.0x01",
59fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "7f.0.0.1",
60fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
61e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // Octal. Not supported by Android but supported by the RI. In the RI, if any of the numbers
62e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // cannot be treated as a decimal the entire IP is interpreted differently, leading to
63e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // "0177.00.00.01" -> 177.0.0.1, but "0177.0x0.00.01" -> 127.0.0.1.
64e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // Android does not do this.
65e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        "0256.00.00.01", // Historically, this could have been interpreted as 174.0.0.1.
66fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
67fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // Negative numbers.
68fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "-1.0.0.1",
69fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.-1.0.1",
70fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.0.-1.1",
71fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        "1.0.0.-1",
72fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    };
73fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
7419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    private static Inet6Address loopback6() throws Exception {
7519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES);
7619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    }
7719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
7819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    private static Inet6Address localhost6() throws Exception {
79a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        return (Inet6Address) InetAddress.getByAddress("ip6-localhost", LOOPBACK6_BYTES);
8019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    }
8119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
82f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    public void test_parseNumericAddress() throws Exception {
83ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes        // Regular IPv4.
84ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes        assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString());
85ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes        // Regular IPv6.
86ede17863e71079d082ff8fb9d1d6f8299bea04b6Elliott Hughes        assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString());
872ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // Mapped IPv4
882ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("::ffff:127.0.0.1").toString());
891c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        // Optional square brackets around IPv6 addresses, including mapped IPv4.
901c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString());
911c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString());
92fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
931c039d71d3879f39e3a75b8788e656f7b4f88f08Elliott Hughes        try {
94fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            InetAddress.parseNumericAddress("example.com"); // Not numeric.
95f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes            fail();
96f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes        } catch (IllegalArgumentException expected) {
97f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes        }
98fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
99e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // Android does not recognize Octal (leading 0) cases: they are treated as decimal.
100e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        assertEquals("/177.0.0.1", InetAddress.parseNumericAddress("0177.00.00.01").toString());
101e5dc23234307897754529a782a6c546c496a4acfNeil Fuller
102fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
103fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            try {
104fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes                InetAddress.parseNumericAddress(invalid);
105fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes                fail(invalid);
106fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            } catch (IllegalArgumentException expected) {
107fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            }
108f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes        }
109fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
11046bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        // Strange special cases, for compatibility with InetAddress.getByName.
11146bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress());
11246bed6a47a20d8105f0e099d162d547a7964b4feElliott Hughes        assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress());
113f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes    }
1145d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes
115fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    public void test_isNumeric() throws Exception {
1162ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // IPv4
117fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        assertTrue(InetAddress.isNumeric("1.2.3.4"));
118fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        assertTrue(InetAddress.isNumeric("127.0.0.1"));
119fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
1202ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // IPv6
1212ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertTrue(InetAddress.isNumeric("::1"));
1222ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertTrue(InetAddress.isNumeric("2001:4860:800d::68"));
1232ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root
1242ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // Mapped IPv4
1252ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertTrue(InetAddress.isNumeric("::ffff:127.0.0.1"));
1262ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root
1272ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // Optional square brackets around IPv6 addresses, including mapped IPv4.
1282ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertTrue(InetAddress.isNumeric("[2001:4860:800d::68]"));
1292ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertTrue(InetAddress.isNumeric("[::ffff:127.0.0.1]"));
1302ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root
1312ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // Negative test
132fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        assertFalse(InetAddress.isNumeric("example.com"));
133fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
134e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        // Android does not handle Octal (leading 0) cases: they are treated as decimal.
135e5dc23234307897754529a782a6c546c496a4acfNeil Fuller        assertTrue(InetAddress.isNumeric("0177.00.00.01")); // Interpreted as 177.0.0.1
136e5dc23234307897754529a782a6c546c496a4acfNeil Fuller
137fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
138fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            assertFalse(invalid, InetAddress.isNumeric(invalid));
139fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        }
1405d3f5562c167120b5ec00e509af0f0ab9308bff5Elliott Hughes    }
141775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes
142a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public void test_isLinkLocalAddress() throws Exception {
143a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress());
1442ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertFalse(InetAddress.getByName("::ffff:127.0.0.1").isLinkLocalAddress());
145a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress());
146a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
147a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress());
148a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress());
149a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    }
150a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
151a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public void test_isMCSiteLocalAddress() throws Exception {
152a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal());
153a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal());
154a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal());
155a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal());
156a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
157a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal());
158a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal());
159a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal());
160a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    }
161a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
1629b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes    public void test_isReachable() throws Exception {
1639b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes        // http://code.google.com/p/android/issues/detail?id=20203
16410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson        String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af"
16510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737"
16610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300"
16710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                + "00000274000e7777772e676f6f676c652e636f6d";
16810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson        InetAddress inetAddress = InetAddress.getByName("www.google.com");
169b416ef5dc224630af2b9393a15ae120b27e4864aJesse Wilson        new SerializationTester<InetAddress>(inetAddress, s) {
17010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson            @Override protected void verify(InetAddress deserialized) throws Exception {
17110078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                deserialized.isReachable(500);
17210078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                for (NetworkInterface nif
17310078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                        : Collections.list(NetworkInterface.getNetworkInterfaces())) {
17410078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                    deserialized.isReachable(nif, 20, 500);
17510078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                }
17610078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson            }
17710078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson            @Override protected boolean equals(InetAddress a, InetAddress b) {
17810078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson                return a.getHostName().equals(b.getHostName());
17910078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson            }
18010078dabb17441ce2721a8e5e10f275c5d0a426aJesse Wilson        }.test();
1819b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes    }
1829b4a8ec37805be3eabf3a4b443bbdb692ffa2608Elliott Hughes
183a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    public void test_isSiteLocalAddress() throws Exception {
184a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress());
185a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress());
186a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress());
187a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress());
188a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress());
189a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
190a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress());
191a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes        assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress());
192a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes    }
193a7428d68453f6a74633221e8714f8d3d9597b2b4Elliott Hughes
194fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    public void test_getByName() throws Exception {
195fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
196fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            try {
197fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes                InetAddress.getByName(invalid);
198fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes                fail(invalid);
199fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            } catch (UnknownHostException expected) {
200fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            }
201fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        }
202fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    }
203fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
204fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes    public void test_getLoopbackAddress() throws Exception {
205fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress());
206775095bf6a763e6a9b4e858011a812425d949af5Elliott Hughes    }
20719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
20819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    public void test_equals() throws Exception {
20919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        InetAddress addr = InetAddress.getByName("239.191.255.255");
21019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertTrue(addr.equals(addr));
21119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertTrue(loopback6().equals(localhost6()));
21219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertFalse(addr.equals(loopback6()));
21319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
2145bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes        assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK));
215fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes
216fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances.
217fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        byte[] bs = new byte[16];
218fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2));
21919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    }
22019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
22119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    public void test_getHostAddress() throws Exception {
22219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertEquals("::1", localhost6().getHostAddress());
22319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertEquals("::1", InetAddress.getByName("::1").getHostAddress());
22419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
2255bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes        assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress());
2265bf7fa5b431ebfa093c2a389148be3faa62e8237Elliott Hughes
2272ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        // IPv4 mapped address
2282ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root        assertEquals("127.0.0.1", InetAddress.getByName("::ffff:127.0.0.1").getHostAddress());
2292ea0b34ba1e3bf712e052304747a3bc6c78e8208Kenny Root
23019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        InetAddress aAddr = InetAddress.getByName("224.0.0.0");
23119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertEquals("224.0.0.0", aAddr.getHostAddress());
23219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
23319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
234fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        try {
235fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            InetAddress.getByName("1");
236fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            fail();
237fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        } catch (UnknownHostException expected) {
238fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        }
23919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
24019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        byte[] bAddr = {
24119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00,
24219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
24319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF,
24419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2
24519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
24619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        aAddr = Inet6Address.getByAddress(bAddr);
24719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        String aString = aAddr.getHostAddress();
24819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2"));
24919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
25019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        byte[] cAddr = {
25119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
25219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
25319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
25419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
25519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
25619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        aAddr = Inet6Address.getByAddress(cAddr);
25719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
25819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
25919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        byte[] dAddr = {
26019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
26119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
26219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
26319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
26419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
26519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        aAddr = Inet6Address.getByAddress(dAddr);
26619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        aString = aAddr.getHostAddress();
26719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::"));
26819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
26919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        byte[] eAddr = {
27019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
27119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
27219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
27319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f
27419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
27519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        aAddr = Inet6Address.getByAddress(eAddr);
27619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
27719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
27819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        byte[] fAddr = {
27919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
28019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
28119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
28219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0
28319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
28419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        aAddr = Inet6Address.getByAddress(fAddr);
28519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
28619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    }
28719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
28819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    public void test_hashCode() throws Exception {
289fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        InetAddress addr1 = InetAddress.getByName("1.0.0.1");
290fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes        InetAddress addr2 = InetAddress.getByName("1.0.0.1");
29119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertTrue(addr1.hashCode() == addr2.hashCode());
29219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
29319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        assertTrue(loopback6().hashCode() == localhost6().hashCode());
29419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    }
29519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
29619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    public void test_toString() throws Exception {
29719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        String validIPAddresses[] = {
298fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            "::1.2.3.4", "::", "::", "1::0", "1::", "::1",
29919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
30019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
30119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0"
30219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
30319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
30419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        String [] resultStrings = {
305fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1",
306fc041ff241f9a7556e72236f130de0215ecd17dbElliott Hughes            "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
30719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::",
30819fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            "/::"
30919fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        };
31019fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes
31119fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        for(int i = 0; i < validIPAddresses.length; i++) {
31219fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
31319fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            String result = ia.toString();
31419fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            assertNotNull(result);
31519fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes            assertEquals(resultStrings[i], result);
31619fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes        }
31719fa10017f9f8904dea97afcb48caf13d8c2dde2Elliott Hughes    }
318ee458cf73d150be182b1797bc59043b131706f7fNeil Fuller
319a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getHostNameCaches() throws Exception {
320a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES);
321b2f612dfda4bf2362413e3e205880ae7c1addde9Przemyslaw Szczepaniak        // TODO(narayan): Investigate why these tests are suppressed.
322b2f612dfda4bf2362413e3e205880ae7c1addde9Przemyslaw Szczepaniak        // assertEquals("::1", inetAddress.getHostString());
323a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals("ip6-localhost", inetAddress.getHostName());
324a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        // getHostString() should now be different.
325b2f612dfda4bf2362413e3e205880ae7c1addde9Przemyslaw Szczepaniak        // assertEquals("ip6-localhost", inetAddress.getHostString());
326a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
327a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
328a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getByAddress_loopbackIpv4() throws Exception {
329a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK4_BYTES);
330a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress);
331a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(inetAddress.isLoopbackAddress());
332a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
333a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
334a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getByAddress_loopbackIpv6() throws Exception {
335a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES);
336a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress);
337a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(inetAddress.isLoopbackAddress());
338a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
339a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
340a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getByName_loopbackIpv4() throws Exception {
341a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
342a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress);
343a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(inetAddress.isLoopbackAddress());
344a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
345a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
346a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getByName_loopbackIpv6() throws Exception {
347a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = InetAddress.getByName("::1");
348a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress);
349a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(inetAddress.isLoopbackAddress());
350a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
351a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
352db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong    public void test_getByName_empty() throws Exception {
353db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong        InetAddress inetAddress = InetAddress.getByName("");
354db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong        assertEquals(LOOPBACK6_BYTES, "localhost", inetAddress);
355db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong        assertTrue(inetAddress.isLoopbackAddress());
356db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong    }
357db6fe533dcb9e7df4d3a2024de37e5193b3685d6Yi Kong
358a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getAllByName_localhost() throws Exception {
359a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress[] inetAddresses = InetAddress.getAllByName("localhost");
360a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(1, inetAddresses.length);
361a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = inetAddresses[0];
362a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(LOOPBACK4_BYTES, "localhost", inetAddress);
363a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(inetAddress.isLoopbackAddress());
364a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
365a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
366a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getAllByName_ip6_localhost() throws Exception {
367a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress[] inetAddresses = InetAddress.getAllByName("ip6-localhost");
368a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(1, inetAddresses.length);
369a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = inetAddresses[0];
370a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress);
371a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(inetAddress.isLoopbackAddress());
372a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
373a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
374d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath    public void test_getByName_v6loopback() throws Exception {
375a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress inetAddress = InetAddress.getByName("::1");
376a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
377a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        Set<InetAddress> expectedLoopbackAddresses =
378a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller                createSet(Inet4Address.LOOPBACK, Inet6Address.LOOPBACK);
379a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue(expectedLoopbackAddresses.contains(inetAddress));
380a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
381a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
382d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath    public void test_getByName_cloning() throws Exception {
383d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        InetAddress[] addresses = InetAddress.getAllByName(null);
384d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        InetAddress[] addresses2 = InetAddress.getAllByName(null);
385d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        assertNotNull(addresses[0]);
386d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        assertNotNull(addresses[1]);
387d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        assertNotSame(addresses, addresses2);
388d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath
389d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        // Also assert that changes to the return value do not affect the cache
390d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        // etc. i.e, that we return a copy.
391d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        addresses[0] = null;
392d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        addresses2 = InetAddress.getAllByName(null);
393d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        assertNotNull(addresses2[0]);
394d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath        assertNotNull(addresses2[1]);
395d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath    }
396d960ef32a57a91e4ea0976a2bbd97b8ec0fd2cf0Narayan Kamath
397a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    public void test_getAllByName_null() throws Exception {
398a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        InetAddress[] inetAddresses = InetAddress.getAllByName(null);
399a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(2, inetAddresses.length);
400a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        Set<InetAddress> expectedLoopbackAddresses =
401a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller                createSet(Inet4Address.LOOPBACK, Inet6Address.LOOPBACK);
402a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(expectedLoopbackAddresses, createSet(inetAddresses));
403a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
404a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
405a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    private static void assertEquals(
406a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        byte[] expectedAddressBytes, String expectedHostname, InetAddress actual) {
407a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertArrayEquals(expectedAddressBytes, actual.getAddress());
408a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertEquals(expectedHostname, actual.getHostName());
409a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
410a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
411a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller
412a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    private static void assertArrayEquals(byte[] expected, byte[] actual) {
413a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        assertTrue("Expected=" + Arrays.toString(expected) + ", actual=" + Arrays.toString(actual),
414a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller                Arrays.equals(expected, actual));
415a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    }
416ee458cf73d150be182b1797bc59043b131706f7fNeil Fuller
417a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller    private static Set<InetAddress> createSet(InetAddress... members) {
418a43c5da446e8f02ebe23b6a026c25f4e25d89318Neil Fuller        return new HashSet<InetAddress>(Arrays.asList(members));
419ee458cf73d150be182b1797bc59043b131706f7fNeil Fuller    }
420f39b892d87e85835f021e8ad77ffdd215735604bElliott Hughes}
421