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