InetAddressTest.java revision 19fa10017f9f8904dea97afcb48caf13d8c2dde2
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.net;
18
19import java.net.InetAddress;
20import java.net.Inet4Address;
21import java.net.Inet6Address;
22
23public class InetAddressTest extends junit.framework.TestCase {
24    private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
25
26    private static Inet6Address loopback6() throws Exception {
27        return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES);
28    }
29
30    private static Inet6Address localhost6() throws Exception {
31        return (Inet6Address) InetAddress.getByAddress("localhost", LOOPBACK6_BYTES);
32    }
33
34    public void test_parseNumericAddress() throws Exception {
35        // Regular IPv4.
36        assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString());
37        // Regular IPv6.
38        assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString());
39        // Weird IPv4 special cases.
40        assertEquals("/1.2.0.3", InetAddress.parseNumericAddress("1.2.3").toString());
41        assertEquals("/1.0.0.2", InetAddress.parseNumericAddress("1.2").toString());
42        assertEquals("/0.0.0.1", InetAddress.parseNumericAddress("1").toString());
43        assertEquals("/0.0.4.210", InetAddress.parseNumericAddress("1234").toString());
44        // Optional square brackets around IPv6 addresses, including mapped IPv4.
45        assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString());
46        assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString());
47        try {
48            // Actual IPv4 addresses may not be surrounded by square brackets.
49            assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[127.0.0.1]").toString());
50            fail();
51        } catch (IllegalArgumentException expected) {
52        }
53        try {
54            // Almost numeric but invalid...
55            InetAddress.parseNumericAddress("1.");
56            fail();
57        } catch (IllegalArgumentException expected) {
58        }
59        try {
60            // Not even close to numeric...
61            InetAddress.parseNumericAddress("www.google.com");
62            fail();
63        } catch (IllegalArgumentException expected) {
64        }
65        // Strange special cases, for compatibility with InetAddress.getByName.
66        assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress());
67        assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress());
68    }
69
70    public void test_getLoopbackAddress() throws Exception {
71        assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress());
72    }
73
74    public void test_0() throws Exception {
75        // The RI special-cases "0" for legacy IPv4 applications.
76        assertTrue(InetAddress.getByName("0").isAnyLocalAddress());
77    }
78
79    public void test_equals() throws Exception {
80        InetAddress addr = InetAddress.getByName("239.191.255.255");
81        assertTrue(addr.equals(addr));
82        assertTrue(loopback6().equals(localhost6()));
83        assertFalse(addr.equals(loopback6()));
84
85        InetAddress addr3 = InetAddress.getByName("127.0.0");
86        assertFalse(loopback6().equals(addr3));
87    }
88
89    public void test_getHostAddress() throws Exception {
90        assertEquals("::1", localhost6().getHostAddress());
91        assertEquals("::1", InetAddress.getByName("::1").getHostAddress());
92
93        InetAddress aAddr = InetAddress.getByName("224.0.0.0");
94        assertEquals("224.0.0.0", aAddr.getHostAddress());
95
96        aAddr = InetAddress.getByName("1");
97        assertEquals("0.0.0.1", aAddr.getHostAddress());
98
99        aAddr = InetAddress.getByName("1.1");
100        assertEquals("1.0.0.1", aAddr.getHostAddress());
101
102        aAddr = InetAddress.getByName("1.1.1");
103        assertEquals("1.1.0.1", aAddr.getHostAddress());
104
105        byte[] bAddr = {
106            (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00,
107            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
108            (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF,
109            (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2
110        };
111        aAddr = Inet6Address.getByAddress(bAddr);
112        String aString = aAddr.getHostAddress();
113        assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2"));
114
115        byte[] cAddr = {
116            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
117            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
118            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
119            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
120        };
121        aAddr = Inet6Address.getByAddress(cAddr);
122        assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
123
124        byte[] dAddr = {
125            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
126            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
127            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
128            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
129        };
130        aAddr = Inet6Address.getByAddress(dAddr);
131        aString = aAddr.getHostAddress();
132        assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::"));
133
134        byte[] eAddr = {
135            (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
136            (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
137            (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
138            (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f
139        };
140        aAddr = Inet6Address.getByAddress(eAddr);
141        assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
142
143        byte[] fAddr = {
144            (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
145            (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
146            (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
147            (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0
148        };
149        aAddr = Inet6Address.getByAddress(fAddr);
150        assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
151    }
152
153    public void test_hashCode() throws Exception {
154        InetAddress addr1 = InetAddress.getByName("1.1");
155        InetAddress addr2 = InetAddress.getByName("1.1.1");
156        assertFalse(addr1.hashCode() == addr2.hashCode());
157
158        addr2 = InetAddress.getByName("1.0.0.1");
159        assertTrue(addr1.hashCode() == addr2.hashCode());
160
161        assertTrue(loopback6().hashCode() == localhost6().hashCode());
162    }
163
164    public void test_toString() throws Exception {
165        String validIPAddresses[] = {
166            "::1.2.3.4", "::", "::", "1::0", "1::",
167            "::1", "0", /* jdk1.5 accepts 0 as valid */
168            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
169            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
170            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0"
171        };
172
173        String [] resultStrings = {
174            "/::1.2.3.4", "/::", "/::", "/1::", "/1::",
175            "/::1",
176            "/0.0.0.0", "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
177            "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::",
178            "/::"
179        };
180
181        for(int i = 0; i < validIPAddresses.length; i++) {
182            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
183            String result = ia.toString();
184            assertNotNull(result);
185            assertEquals(resultStrings[i], result);
186        }
187    }
188}
189