InetAddressTest.java revision b416ef5dc224630af2b9393a15ae120b27e4864a
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.Inet4Address;
20import java.net.Inet6Address;
21import java.net.InetAddress;
22import java.net.NetworkInterface;
23import java.net.UnknownHostException;
24import java.util.Collections;
25import libcore.util.SerializationTester;
26
27public class InetAddressTest extends junit.framework.TestCase {
28    private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
29
30    private static final String[] INVALID_IPv4_NUMERIC_ADDRESSES = new String[] {
31        // IPv4 addresses may not be surrounded by square brackets.
32        "[127.0.0.1]",
33
34        // Trailing dots are not allowed.
35        "1.2.3.4.",
36        // Nor is any kind of trailing junk.
37        "1.2.3.4hello",
38
39        // Out of range.
40        "256.2.3.4",
41        "1.256.3.4",
42        "1.2.256.4",
43        "1.2.3.256",
44
45        // Deprecated.
46        "1.2.3",
47        "1.2",
48        "1",
49        "1234",
50        "0", // Single out the deprecated form of the ANY address.
51
52        // Hex.
53        "0x1.0x2.0x3.0x4",
54        "0x7f.0x00.0x00.0x01",
55        "7f.0.0.1",
56
57        // Octal.
58        "0177.00.00.01", // Historically, this would have been interpreted as 127.0.0.1.
59
60        // Negative numbers.
61        "-1.0.0.1",
62        "1.-1.0.1",
63        "1.0.-1.1",
64        "1.0.0.-1",
65    };
66
67    private static Inet6Address loopback6() throws Exception {
68        return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES);
69    }
70
71    private static Inet6Address localhost6() throws Exception {
72        return (Inet6Address) InetAddress.getByAddress("localhost", LOOPBACK6_BYTES);
73    }
74
75    public void test_parseNumericAddress() throws Exception {
76        // Regular IPv4.
77        assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString());
78        // Regular IPv6.
79        assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString());
80        // Optional square brackets around IPv6 addresses, including mapped IPv4.
81        assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString());
82        assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString());
83
84        try {
85            InetAddress.parseNumericAddress("example.com"); // Not numeric.
86            fail();
87        } catch (IllegalArgumentException expected) {
88        }
89
90        for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
91            try {
92                InetAddress.parseNumericAddress(invalid);
93                fail(invalid);
94            } catch (IllegalArgumentException expected) {
95            }
96        }
97
98        // Strange special cases, for compatibility with InetAddress.getByName.
99        assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress());
100        assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress());
101    }
102
103    public void test_isNumeric() throws Exception {
104        assertTrue(InetAddress.isNumeric("1.2.3.4"));
105        assertTrue(InetAddress.isNumeric("127.0.0.1"));
106
107        assertFalse(InetAddress.isNumeric("example.com"));
108
109        for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
110            assertFalse(invalid, InetAddress.isNumeric(invalid));
111        }
112    }
113
114    public void test_isLinkLocalAddress() throws Exception {
115        assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress());
116        assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress());
117
118        assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress());
119        assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress());
120    }
121
122    public void test_isMCSiteLocalAddress() throws Exception {
123        assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal());
124        assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal());
125        assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal());
126        assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal());
127
128        assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal());
129        assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal());
130        assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal());
131    }
132
133    public void test_isReachable() throws Exception {
134        // http://code.google.com/p/android/issues/detail?id=20203
135        String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af"
136                + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737"
137                + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300"
138                + "00000274000e7777772e676f6f676c652e636f6d";
139        InetAddress inetAddress = InetAddress.getByName("www.google.com");
140        new SerializationTester<InetAddress>(inetAddress, s) {
141            @Override protected void verify(InetAddress deserialized) throws Exception {
142                deserialized.isReachable(500);
143                for (NetworkInterface nif
144                        : Collections.list(NetworkInterface.getNetworkInterfaces())) {
145                    deserialized.isReachable(nif, 20, 500);
146                }
147            }
148            @Override protected boolean equals(InetAddress a, InetAddress b) {
149                return a.getHostName().equals(b.getHostName());
150            }
151        }.test();
152    }
153
154    public void test_isSiteLocalAddress() throws Exception {
155        assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress());
156        assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress());
157        assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress());
158        assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress());
159        assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress());
160
161        assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress());
162        assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress());
163    }
164
165    public void test_getByName() throws Exception {
166        for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
167            try {
168                InetAddress.getByName(invalid);
169                fail(invalid);
170            } catch (UnknownHostException expected) {
171            }
172        }
173    }
174
175    public void test_getLoopbackAddress() throws Exception {
176        assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress());
177    }
178
179    public void test_equals() throws Exception {
180        InetAddress addr = InetAddress.getByName("239.191.255.255");
181        assertTrue(addr.equals(addr));
182        assertTrue(loopback6().equals(localhost6()));
183        assertFalse(addr.equals(loopback6()));
184
185        assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK));
186
187        // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances.
188        byte[] bs = new byte[16];
189        assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2));
190    }
191
192    public void test_getHostAddress() throws Exception {
193        assertEquals("::1", localhost6().getHostAddress());
194        assertEquals("::1", InetAddress.getByName("::1").getHostAddress());
195
196        assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress());
197
198        InetAddress aAddr = InetAddress.getByName("224.0.0.0");
199        assertEquals("224.0.0.0", aAddr.getHostAddress());
200
201
202        try {
203            InetAddress.getByName("1");
204            fail();
205        } catch (UnknownHostException expected) {
206        }
207
208        byte[] bAddr = {
209            (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00,
210            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
211            (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF,
212            (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2
213        };
214        aAddr = Inet6Address.getByAddress(bAddr);
215        String aString = aAddr.getHostAddress();
216        assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2"));
217
218        byte[] cAddr = {
219            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
220            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
221            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
222            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
223        };
224        aAddr = Inet6Address.getByAddress(cAddr);
225        assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
226
227        byte[] dAddr = {
228            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
229            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
230            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
231            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
232        };
233        aAddr = Inet6Address.getByAddress(dAddr);
234        aString = aAddr.getHostAddress();
235        assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::"));
236
237        byte[] eAddr = {
238            (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
239            (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
240            (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
241            (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f
242        };
243        aAddr = Inet6Address.getByAddress(eAddr);
244        assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
245
246        byte[] fAddr = {
247            (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
248            (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
249            (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
250            (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0
251        };
252        aAddr = Inet6Address.getByAddress(fAddr);
253        assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
254    }
255
256    public void test_hashCode() throws Exception {
257        InetAddress addr1 = InetAddress.getByName("1.0.0.1");
258        InetAddress addr2 = InetAddress.getByName("1.0.0.1");
259        assertTrue(addr1.hashCode() == addr2.hashCode());
260
261        assertTrue(loopback6().hashCode() == localhost6().hashCode());
262    }
263
264    public void test_toString() throws Exception {
265        String validIPAddresses[] = {
266            "::1.2.3.4", "::", "::", "1::0", "1::", "::1",
267            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
268            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
269            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0"
270        };
271
272        String [] resultStrings = {
273            "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1",
274            "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
275            "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::",
276            "/::"
277        };
278
279        for(int i = 0; i < validIPAddresses.length; i++) {
280            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
281            String result = ia.toString();
282            assertNotNull(result);
283            assertEquals(resultStrings[i], result);
284        }
285    }
286}
287