1 /* 2 * Copyright (C) 2008 The Guava Authors 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 17 package com.google.common.net; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.testing.NullPointerTester; 22 import java.net.Inet4Address; 23 import java.net.Inet6Address; 24 import java.net.InetAddress; 25 import java.net.UnknownHostException; 26 import junit.framework.TestCase; 27 28 /** 29 * Tests for {@link InetAddresses}. 30 * 31 * @author Erik Kline 32 */ 33 public class InetAddressesTest extends TestCase { 34 testNulls()35 public void testNulls() { 36 NullPointerTester tester = new NullPointerTester(); 37 38 tester.testAllPublicStaticMethods(InetAddresses.class); 39 } 40 testForStringBogusInput()41 public void testForStringBogusInput() { 42 String[] bogusInputs = { 43 "", 44 "016.016.016.016", 45 "016.016.016", 46 "016.016", 47 "016", 48 "000.000.000.000", 49 "000", 50 "0x0a.0x0a.0x0a.0x0a", 51 "0x0a.0x0a.0x0a", 52 "0x0a.0x0a", 53 "0x0a", 54 "42.42.42.42.42", 55 "42.42.42", 56 "42.42", 57 "42", 58 "42..42.42", 59 "42..42.42.42", 60 "42.42.42.42.", 61 "42.42.42.42...", 62 ".42.42.42.42", 63 "...42.42.42.42", 64 "42.42.42.-0", 65 "42.42.42.+0", 66 ".", 67 "...", 68 "bogus", 69 "bogus.com", 70 "192.168.0.1.com", 71 "12345.67899.-54321.-98765", 72 "257.0.0.0", 73 "42.42.42.-42", 74 "3ffe::1.net", 75 "3ffe::1::1", 76 "1::2::3::4:5", 77 "::7:6:5:4:3:2:", // should end with ":0" 78 ":6:5:4:3:2:1::", // should begin with "0:" 79 "2001::db:::1", 80 "FEDC:9878", 81 "+1.+2.+3.4", 82 "1.2.3.4e0", 83 "::7:6:5:4:3:2:1:0", // too many parts 84 "7:6:5:4:3:2:1:0::", // too many parts 85 "9:8:7:6:5:4:3::2:1", // too many parts 86 "0:1:2:3::4:5:6:7", // :: must remove at least one 0. 87 "3ffe:0:0:0:0:0:0:0:1", // too many parts (9 instead of 8) 88 "3ffe::10000", // hextet exceeds 16 bits 89 "3ffe::goog", 90 "3ffe::-0", 91 "3ffe::+0", 92 "3ffe::-1", 93 ":", 94 ":::", 95 "::1.2.3", 96 "::1.2.3.4.5", 97 "::1.2.3.4:", 98 "1.2.3.4::", 99 "2001:db8::1:", 100 ":2001:db8::1", 101 ":1:2:3:4:5:6:7", 102 "1:2:3:4:5:6:7:", 103 ":1:2:3:4:5:6:" 104 }; 105 106 for (int i = 0; i < bogusInputs.length; i++) { 107 try { 108 InetAddresses.forString(bogusInputs[i]); 109 fail("IllegalArgumentException expected for '" + bogusInputs[i] + "'"); 110 } catch (IllegalArgumentException expected) { 111 } 112 assertFalse(InetAddresses.isInetAddress(bogusInputs[i])); 113 } 114 } 115 test3ff31()116 public void test3ff31() { 117 try { 118 InetAddresses.forString("3ffe:::1"); 119 fail("IllegalArgumentException expected"); 120 } catch (IllegalArgumentException expected) { 121 } 122 assertFalse(InetAddresses.isInetAddress("016.016.016.016")); 123 } 124 testForStringIPv4Input()125 public void testForStringIPv4Input() throws UnknownHostException { 126 String ipStr = "192.168.0.1"; 127 InetAddress ipv4Addr = null; 128 // Shouldn't hit DNS, because it's an IP string literal. 129 ipv4Addr = InetAddress.getByName(ipStr); 130 assertEquals(ipv4Addr, InetAddresses.forString(ipStr)); 131 assertTrue(InetAddresses.isInetAddress(ipStr)); 132 } 133 testForStringIPv6Input()134 public void testForStringIPv6Input() throws UnknownHostException { 135 String ipStr = "3ffe::1"; 136 InetAddress ipv6Addr = null; 137 // Shouldn't hit DNS, because it's an IP string literal. 138 ipv6Addr = InetAddress.getByName(ipStr); 139 assertEquals(ipv6Addr, InetAddresses.forString(ipStr)); 140 assertTrue(InetAddresses.isInetAddress(ipStr)); 141 } 142 testForStringIPv6EightColons()143 public void testForStringIPv6EightColons() throws UnknownHostException { 144 String[] eightColons = { 145 "::7:6:5:4:3:2:1", "::7:6:5:4:3:2:0", "7:6:5:4:3:2:1::", "0:6:5:4:3:2:1::", 146 }; 147 148 for (int i = 0; i < eightColons.length; i++) { 149 InetAddress ipv6Addr = null; 150 // Shouldn't hit DNS, because it's an IP string literal. 151 ipv6Addr = InetAddress.getByName(eightColons[i]); 152 assertEquals(ipv6Addr, InetAddresses.forString(eightColons[i])); 153 assertTrue(InetAddresses.isInetAddress(eightColons[i])); 154 } 155 } 156 testConvertDottedQuadToHex()157 public void testConvertDottedQuadToHex() throws UnknownHostException { 158 String[] ipStrings = { 159 "7::0.128.0.127", "7::0.128.0.128", "7::128.128.0.127", "7::0.128.128.127" 160 }; 161 162 for (String ipString : ipStrings) { 163 // Shouldn't hit DNS, because it's an IP string literal. 164 InetAddress ipv6Addr = InetAddress.getByName(ipString); 165 assertEquals(ipv6Addr, InetAddresses.forString(ipString)); 166 assertTrue(InetAddresses.isInetAddress(ipString)); 167 } 168 } 169 testToAddrStringIPv4()170 public void testToAddrStringIPv4() { 171 // Don't need to test IPv4 much; it just calls getHostAddress(). 172 assertEquals("1.2.3.4", InetAddresses.toAddrString(InetAddresses.forString("1.2.3.4"))); 173 } 174 testToAddrStringIPv6()175 public void testToAddrStringIPv6() { 176 assertEquals( 177 "1:2:3:4:5:6:7:8", InetAddresses.toAddrString(InetAddresses.forString("1:2:3:4:5:6:7:8"))); 178 assertEquals( 179 "2001:0:0:4::8", InetAddresses.toAddrString(InetAddresses.forString("2001:0:0:4:0:0:0:8"))); 180 assertEquals( 181 "2001::4:5:6:7:8", 182 InetAddresses.toAddrString(InetAddresses.forString("2001:0:0:4:5:6:7:8"))); 183 assertEquals( 184 "2001:0:3:4:5:6:7:8", 185 InetAddresses.toAddrString(InetAddresses.forString("2001:0:3:4:5:6:7:8"))); 186 assertEquals( 187 "0:0:3::ffff", InetAddresses.toAddrString(InetAddresses.forString("0:0:3:0:0:0:0:ffff"))); 188 assertEquals( 189 "::4:0:0:0:ffff", 190 InetAddresses.toAddrString(InetAddresses.forString("0:0:0:4:0:0:0:ffff"))); 191 assertEquals( 192 "::5:0:0:ffff", InetAddresses.toAddrString(InetAddresses.forString("0:0:0:0:5:0:0:ffff"))); 193 assertEquals( 194 "1::4:0:0:7:8", InetAddresses.toAddrString(InetAddresses.forString("1:0:0:4:0:0:7:8"))); 195 assertEquals("::", InetAddresses.toAddrString(InetAddresses.forString("0:0:0:0:0:0:0:0"))); 196 assertEquals("::1", InetAddresses.toAddrString(InetAddresses.forString("0:0:0:0:0:0:0:1"))); 197 assertEquals( 198 "2001:658:22a:cafe::", 199 InetAddresses.toAddrString(InetAddresses.forString("2001:0658:022a:cafe::"))); 200 assertEquals("::102:304", InetAddresses.toAddrString(InetAddresses.forString("::1.2.3.4"))); 201 } 202 testToUriStringIPv4()203 public void testToUriStringIPv4() { 204 String ipStr = "1.2.3.4"; 205 InetAddress ip = InetAddresses.forString(ipStr); 206 assertEquals("1.2.3.4", InetAddresses.toUriString(ip)); 207 } 208 testToUriStringIPv6()209 public void testToUriStringIPv6() { 210 // Unfortunately the InetAddress.toString() method for IPv6 addresses 211 // does not collapse contiguous shorts of zeroes with the :: abbreviation. 212 String ipStr = "3ffe::1"; 213 InetAddress ip = InetAddresses.forString(ipStr); 214 assertEquals("[3ffe::1]", InetAddresses.toUriString(ip)); 215 } 216 testForUriStringIPv4()217 public void testForUriStringIPv4() { 218 Inet4Address expected = (Inet4Address) InetAddresses.forString("192.168.1.1"); 219 assertEquals(expected, InetAddresses.forUriString("192.168.1.1")); 220 } 221 testForUriStringIPv6()222 public void testForUriStringIPv6() { 223 Inet6Address expected = (Inet6Address) InetAddresses.forString("3ffe:0:0:0:0:0:0:1"); 224 assertEquals(expected, InetAddresses.forUriString("[3ffe:0:0:0:0:0:0:1]")); 225 } 226 testForUriStringIPv4Mapped()227 public void testForUriStringIPv4Mapped() { 228 Inet4Address expected = (Inet4Address) InetAddresses.forString("192.0.2.1"); 229 assertEquals(expected, InetAddresses.forUriString("[::ffff:192.0.2.1]")); 230 } 231 testIsUriInetAddress()232 public void testIsUriInetAddress() { 233 assertTrue(InetAddresses.isUriInetAddress("192.168.1.1")); 234 assertTrue(InetAddresses.isUriInetAddress("[3ffe:0:0:0:0:0:0:1]")); 235 assertTrue(InetAddresses.isUriInetAddress("[::ffff:192.0.2.1]")); 236 237 assertFalse(InetAddresses.isUriInetAddress("[192.168.1.1")); 238 assertFalse(InetAddresses.isUriInetAddress("192.168.1.1]")); 239 assertFalse(InetAddresses.isUriInetAddress("")); 240 assertFalse(InetAddresses.isUriInetAddress("192.168.999.888")); 241 assertFalse(InetAddresses.isUriInetAddress("www.google.com")); 242 assertFalse(InetAddresses.isUriInetAddress("1:2e")); 243 assertFalse(InetAddresses.isUriInetAddress("[3ffe:0:0:0:0:0:0:1")); 244 assertFalse(InetAddresses.isUriInetAddress("3ffe:0:0:0:0:0:0:1]")); 245 assertFalse(InetAddresses.isUriInetAddress("3ffe:0:0:0:0:0:0:1")); 246 assertFalse(InetAddresses.isUriInetAddress("::ffff:192.0.2.1")); 247 } 248 testForUriStringBad()249 public void testForUriStringBad() { 250 try { 251 InetAddresses.forUriString(""); 252 fail("expected IllegalArgumentException"); // COV_NF_LINE 253 } catch (IllegalArgumentException expected) { 254 } 255 256 try { 257 InetAddresses.forUriString("192.168.999.888"); 258 fail("expected IllegalArgumentException"); // COV_NF_LINE 259 } catch (IllegalArgumentException expected) { 260 } 261 262 try { 263 InetAddresses.forUriString("www.google.com"); 264 fail("expected IllegalArgumentException"); // COV_NF_LINE 265 } catch (IllegalArgumentException expected) { 266 } 267 268 try { 269 InetAddresses.forUriString("[1:2e]"); 270 fail("expected IllegalArgumentException"); // COV_NF_LINE 271 } catch (IllegalArgumentException expected) { 272 } 273 274 try { 275 InetAddresses.forUriString("[192.168.1.1]"); 276 fail("expected IllegalArgumentException"); // COV_NF_LINE 277 } catch (IllegalArgumentException expected) { 278 } 279 280 try { 281 InetAddresses.forUriString("192.168.1.1]"); 282 fail("expected IllegalArgumentException"); // COV_NF_LINE 283 } catch (IllegalArgumentException expected) { 284 } 285 286 try { 287 InetAddresses.forUriString("[192.168.1.1"); 288 fail("expected IllegalArgumentException"); // COV_NF_LINE 289 } catch (IllegalArgumentException expected) { 290 } 291 292 try { 293 InetAddresses.forUriString("[3ffe:0:0:0:0:0:0:1"); 294 fail("expected IllegalArgumentException"); // COV_NF_LINE 295 } catch (IllegalArgumentException expected) { 296 } 297 298 try { 299 InetAddresses.forUriString("3ffe:0:0:0:0:0:0:1]"); 300 fail("expected IllegalArgumentException"); // COV_NF_LINE 301 } catch (IllegalArgumentException expected) { 302 } 303 304 try { 305 InetAddresses.forUriString("3ffe:0:0:0:0:0:0:1"); 306 fail("expected IllegalArgumentException"); // COV_NF_LINE 307 } catch (IllegalArgumentException expected) { 308 } 309 310 try { 311 InetAddresses.forUriString("::ffff:192.0.2.1"); 312 fail("expected IllegalArgumentException"); // COV_NF_LINE 313 } catch (IllegalArgumentException expected) { 314 } 315 } 316 testCompatIPv4Addresses()317 public void testCompatIPv4Addresses() { 318 String[] nonCompatAddresses = { 319 "3ffe::1", "::", "::1", 320 }; 321 322 for (int i = 0; i < nonCompatAddresses.length; i++) { 323 InetAddress ip = InetAddresses.forString(nonCompatAddresses[i]); 324 assertFalse(InetAddresses.isCompatIPv4Address((Inet6Address) ip)); 325 try { 326 InetAddresses.getCompatIPv4Address((Inet6Address) ip); 327 fail("IllegalArgumentException expected for '" + nonCompatAddresses[i] + "'"); 328 } catch (IllegalArgumentException expected) { 329 } 330 } 331 332 String[] validCompatAddresses = { 333 "::1.2.3.4", "::102:304", 334 }; 335 String compatStr = "1.2.3.4"; 336 InetAddress compat = InetAddresses.forString(compatStr); 337 338 for (int i = 0; i < validCompatAddresses.length; i++) { 339 InetAddress ip = InetAddresses.forString(validCompatAddresses[i]); 340 assertTrue("checking '" + validCompatAddresses[i] + "'", ip instanceof Inet6Address); 341 assertTrue( 342 "checking '" + validCompatAddresses[i] + "'", 343 InetAddresses.isCompatIPv4Address((Inet6Address) ip)); 344 assertEquals( 345 "checking '" + validCompatAddresses[i] + "'", 346 compat, 347 InetAddresses.getCompatIPv4Address((Inet6Address) ip)); 348 } 349 } 350 testMappedIPv4Addresses()351 public void testMappedIPv4Addresses() throws UnknownHostException { 352 /* 353 * Verify that it is not possible to instantiate an Inet6Address 354 * from an "IPv4 mapped" IPv6 address. Our String-based method can 355 * at least identify them, however. 356 */ 357 String mappedStr = "::ffff:192.168.0.1"; 358 assertTrue(InetAddresses.isMappedIPv4Address(mappedStr)); 359 InetAddress mapped = InetAddresses.forString(mappedStr); 360 assertThat(mapped).isNotInstanceOf(Inet6Address.class); 361 assertEquals(InetAddress.getByName("192.168.0.1"), mapped); 362 363 // check upper case 364 mappedStr = "::FFFF:192.168.0.1"; 365 assertTrue(InetAddresses.isMappedIPv4Address(mappedStr)); 366 mapped = InetAddresses.forString(mappedStr); 367 assertThat(mapped).isNotInstanceOf(Inet6Address.class); 368 assertEquals(InetAddress.getByName("192.168.0.1"), mapped); 369 370 mappedStr = "0:00:000:0000:0:ffff:1.2.3.4"; 371 assertTrue(InetAddresses.isMappedIPv4Address(mappedStr)); 372 mapped = InetAddresses.forString(mappedStr); 373 assertThat(mapped).isNotInstanceOf(Inet6Address.class); 374 assertEquals(InetAddress.getByName("1.2.3.4"), mapped); 375 376 mappedStr = "::ffff:0102:0304"; 377 assertTrue(InetAddresses.isMappedIPv4Address(mappedStr)); 378 mapped = InetAddresses.forString(mappedStr); 379 assertThat(mapped).isNotInstanceOf(Inet6Address.class); 380 assertEquals(InetAddress.getByName("1.2.3.4"), mapped); 381 382 assertFalse(InetAddresses.isMappedIPv4Address("::")); 383 assertFalse(InetAddresses.isMappedIPv4Address("::ffff")); 384 assertFalse(InetAddresses.isMappedIPv4Address("::ffff:0")); 385 assertFalse(InetAddresses.isMappedIPv4Address("::fffe:0:0")); 386 assertFalse(InetAddresses.isMappedIPv4Address("::1:ffff:0:0")); 387 assertFalse(InetAddresses.isMappedIPv4Address("foo")); 388 assertFalse(InetAddresses.isMappedIPv4Address("192.0.2.1")); 389 } 390 test6to4Addresses()391 public void test6to4Addresses() { 392 String[] non6to4Addresses = { 393 "::1.2.3.4", "3ffe::1", "::", "::1", 394 }; 395 396 for (int i = 0; i < non6to4Addresses.length; i++) { 397 InetAddress ip = InetAddresses.forString(non6to4Addresses[i]); 398 assertFalse(InetAddresses.is6to4Address((Inet6Address) ip)); 399 try { 400 InetAddresses.get6to4IPv4Address((Inet6Address) ip); 401 fail("IllegalArgumentException expected for '" + non6to4Addresses[i] + "'"); 402 } catch (IllegalArgumentException expected) { 403 } 404 } 405 406 String valid6to4Address = "2002:0102:0304::1"; 407 String ipv4Str = "1.2.3.4"; 408 409 InetAddress ipv4 = InetAddresses.forString(ipv4Str); 410 InetAddress ip = InetAddresses.forString(valid6to4Address); 411 assertTrue(InetAddresses.is6to4Address((Inet6Address) ip)); 412 assertEquals(ipv4, InetAddresses.get6to4IPv4Address((Inet6Address) ip)); 413 } 414 testTeredoAddresses()415 public void testTeredoAddresses() { 416 String[] nonTeredoAddresses = { 417 "::1.2.3.4", "3ffe::1", "::", "::1", 418 }; 419 420 for (int i = 0; i < nonTeredoAddresses.length; i++) { 421 InetAddress ip = InetAddresses.forString(nonTeredoAddresses[i]); 422 assertFalse(InetAddresses.isTeredoAddress((Inet6Address) ip)); 423 try { 424 InetAddresses.getTeredoInfo((Inet6Address) ip); 425 fail("IllegalArgumentException expected for '" + nonTeredoAddresses[i] + "'"); 426 } catch (IllegalArgumentException expected) { 427 } 428 } 429 430 String validTeredoAddress = "2001:0000:4136:e378:8000:63bf:3fff:fdd2"; 431 String serverStr = "65.54.227.120"; 432 String clientStr = "192.0.2.45"; 433 int port = 40000; 434 int flags = 0x8000; 435 436 InetAddress ip = InetAddresses.forString(validTeredoAddress); 437 assertTrue(InetAddresses.isTeredoAddress((Inet6Address) ip)); 438 InetAddresses.TeredoInfo teredo = InetAddresses.getTeredoInfo((Inet6Address) ip); 439 440 InetAddress server = InetAddresses.forString(serverStr); 441 assertEquals(server, teredo.getServer()); 442 443 InetAddress client = InetAddresses.forString(clientStr); 444 assertEquals(client, teredo.getClient()); 445 446 assertEquals(port, teredo.getPort()); 447 assertEquals(flags, teredo.getFlags()); 448 } 449 testTeredoAddress_nullServer()450 public void testTeredoAddress_nullServer() { 451 InetAddresses.TeredoInfo info = new InetAddresses.TeredoInfo(null, null, 80, 1000); 452 assertEquals(InetAddresses.forString("0.0.0.0"), info.getServer()); 453 assertEquals(InetAddresses.forString("0.0.0.0"), info.getClient()); 454 assertEquals(80, info.getPort()); 455 assertEquals(1000, info.getFlags()); 456 } 457 testIsatapAddresses()458 public void testIsatapAddresses() { 459 InetAddress ipv4 = InetAddresses.forString("1.2.3.4"); 460 String[] validIsatapAddresses = { 461 "2001:db8::5efe:102:304", 462 "2001:db8::100:5efe:102:304", // Private Multicast? Not likely. 463 "2001:db8::200:5efe:102:304", 464 "2001:db8::300:5efe:102:304" // Public Multicast? Also unlikely. 465 }; 466 String[] nonIsatapAddresses = { 467 "::1.2.3.4", 468 "3ffe::1", 469 "::", 470 "::1", 471 "2001:db8::0040:5efe:102:304", 472 "2001:db8::5ffe:102:304", 473 "2001:db8::5eff:102:304", 474 "2001:0:102:203:200:5efe:506:708", // Teredo address; not ISATAP 475 }; 476 477 for (int i = 0; i < validIsatapAddresses.length; i++) { 478 InetAddress ip = InetAddresses.forString(validIsatapAddresses[i]); 479 assertTrue(InetAddresses.isIsatapAddress((Inet6Address) ip)); 480 assertEquals( 481 "checking '" + validIsatapAddresses[i] + "'", 482 ipv4, 483 InetAddresses.getIsatapIPv4Address((Inet6Address) ip)); 484 } 485 for (int i = 0; i < nonIsatapAddresses.length; i++) { 486 InetAddress ip = InetAddresses.forString(nonIsatapAddresses[i]); 487 assertFalse(InetAddresses.isIsatapAddress((Inet6Address) ip)); 488 try { 489 InetAddresses.getIsatapIPv4Address((Inet6Address) ip); 490 fail("IllegalArgumentException expected for '" + nonIsatapAddresses[i] + "'"); 491 } catch (IllegalArgumentException expected) { 492 } 493 } 494 } 495 testGetEmbeddedIPv4ClientAddress()496 public void testGetEmbeddedIPv4ClientAddress() { 497 Inet6Address testIp; 498 499 // Test regular global unicast address. 500 testIp = (Inet6Address) InetAddresses.forString("2001:db8::1"); 501 assertFalse(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp)); 502 503 // Test ISATAP address. 504 testIp = (Inet6Address) InetAddresses.forString("2001:db8::5efe:102:304"); 505 assertFalse(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp)); 506 507 // Test compat address. 508 testIp = (Inet6Address) InetAddresses.forString("::1.2.3.4"); 509 assertTrue(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp)); 510 InetAddress ipv4 = InetAddresses.forString("1.2.3.4"); 511 assertEquals(ipv4, InetAddresses.getEmbeddedIPv4ClientAddress(testIp)); 512 513 // Test 6to4 address. 514 testIp = (Inet6Address) InetAddresses.forString("2002:0102:0304::1"); 515 assertTrue(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp)); 516 ipv4 = InetAddresses.forString("1.2.3.4"); 517 assertEquals(ipv4, InetAddresses.getEmbeddedIPv4ClientAddress(testIp)); 518 519 // Test Teredo address. 520 testIp = (Inet6Address) InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2"); 521 assertTrue(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp)); 522 ipv4 = InetAddresses.forString("192.0.2.45"); 523 assertEquals(ipv4, InetAddresses.getEmbeddedIPv4ClientAddress(testIp)); 524 } 525 testGetCoercedIPv4Address()526 public void testGetCoercedIPv4Address() { 527 // Check that a coerced IPv4 address is unaltered. 528 InetAddress localHost4 = InetAddresses.forString("127.0.0.1"); 529 assertEquals(localHost4, InetAddresses.getCoercedIPv4Address(localHost4)); 530 531 // ::1 special case 532 assertEquals(localHost4, InetAddresses.getCoercedIPv4Address(InetAddresses.forString("::1"))); 533 534 // :: special case 535 assertEquals( 536 InetAddresses.forString("0.0.0.0"), 537 InetAddresses.getCoercedIPv4Address(InetAddresses.forString("::"))); 538 539 // test compat address (should be hashed) 540 assertTrue( 541 InetAddresses.forString("1.2.3.4") 542 != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("::1.2.3.4"))); 543 544 // test 6to4 address (should be hashed) 545 assertTrue( 546 InetAddresses.forString("1.2.3.4") 547 != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::1"))); 548 549 // 2 6to4 addresses differing in the embedded IPv4 address should 550 // hash to the different values. 551 assertTrue( 552 InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::1")) 553 != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0506:0708::1"))); 554 555 // 2 6to4 addresses NOT differing in the embedded IPv4 address should 556 // hash to the same value. 557 assertTrue( 558 InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::1")) 559 != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::2"))); 560 561 // test Teredo address (should be hashed) 562 assertTrue( 563 InetAddresses.forString("192.0.2.45") 564 != InetAddresses.getCoercedIPv4Address( 565 InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2"))); 566 567 // 2 Teredo addresses differing in the embedded IPv4 address should 568 // hash to the different values. 569 assertTrue( 570 InetAddresses.getCoercedIPv4Address( 571 InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2")) 572 != InetAddresses.getCoercedIPv4Address( 573 InetAddresses.forString("2001:0000:4136:e379:8000:63bf:3fff:fdd2"))); 574 575 // 2 Teredo addresses NOT differing in the embedded IPv4 address should 576 // hash to the same value. 577 assertEquals( 578 InetAddresses.getCoercedIPv4Address( 579 InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2")), 580 InetAddresses.getCoercedIPv4Address( 581 InetAddresses.forString("2001:0000:4136:e378:9000:63bf:3fff:fdd2"))); 582 583 // Test that an address hashes in to the 224.0.0.0/3 number-space. 584 InetAddress coerced = 585 InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:4860::1")); 586 assertTrue(0xe0000000 <= InetAddresses.coerceToInteger(coerced)); 587 assertTrue(InetAddresses.coerceToInteger(coerced) <= 0xfffffffe); 588 } 589 testToInteger()590 public void testToInteger() { 591 InetAddress ipv4Addr = InetAddresses.forString("127.0.0.1"); 592 assertEquals(0x7f000001, InetAddresses.coerceToInteger(ipv4Addr)); 593 } 594 testFromInteger()595 public void testFromInteger() { 596 assertEquals(InetAddresses.fromInteger(0x7f000001), InetAddresses.forString("127.0.0.1")); 597 } 598 testFromLittleEndianByteArray()599 public void testFromLittleEndianByteArray() throws UnknownHostException { 600 assertEquals( 601 InetAddresses.fromLittleEndianByteArray(new byte[] {1, 2, 3, 4}), 602 InetAddress.getByAddress(new byte[] {4, 3, 2, 1})); 603 604 assertEquals( 605 InetAddresses.fromLittleEndianByteArray( 606 new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), 607 InetAddress.getByAddress( 608 new byte[] {16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1})); 609 610 try { 611 InetAddresses.fromLittleEndianByteArray(new byte[3]); 612 fail("expected exception"); 613 } catch (UnknownHostException expected) { 614 // success 615 } 616 } 617 testIsMaximum()618 public void testIsMaximum() throws UnknownHostException { 619 InetAddress address = InetAddress.getByName("255.255.255.254"); 620 assertFalse(InetAddresses.isMaximum(address)); 621 622 address = InetAddress.getByName("255.255.255.255"); 623 assertTrue(InetAddresses.isMaximum(address)); 624 625 address = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"); 626 assertFalse(InetAddresses.isMaximum(address)); 627 628 address = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); 629 assertTrue(InetAddresses.isMaximum(address)); 630 } 631 testIncrementIPv4()632 public void testIncrementIPv4() throws UnknownHostException { 633 InetAddress address_66_0 = InetAddress.getByName("172.24.66.0"); 634 InetAddress address_66_255 = InetAddress.getByName("172.24.66.255"); 635 InetAddress address_67_0 = InetAddress.getByName("172.24.67.0"); 636 637 InetAddress address = address_66_0; 638 for (int i = 0; i < 255; i++) { 639 address = InetAddresses.increment(address); 640 } 641 assertEquals(address_66_255, address); 642 643 address = InetAddresses.increment(address); 644 assertEquals(address_67_0, address); 645 646 InetAddress address_ffffff = InetAddress.getByName("255.255.255.255"); 647 address = address_ffffff; 648 try { 649 address = InetAddresses.increment(address); 650 fail(); 651 } catch (IllegalArgumentException expected) { 652 } 653 } 654 testIncrementIPv6()655 public void testIncrementIPv6() throws UnknownHostException { 656 InetAddress addressV6_66_0 = InetAddress.getByName("2001:db8::6600"); 657 InetAddress addressV6_66_ff = InetAddress.getByName("2001:db8::66ff"); 658 InetAddress addressV6_67_0 = InetAddress.getByName("2001:db8::6700"); 659 660 InetAddress address = addressV6_66_0; 661 for (int i = 0; i < 255; i++) { 662 address = InetAddresses.increment(address); 663 } 664 assertEquals(addressV6_66_ff, address); 665 666 address = InetAddresses.increment(address); 667 assertEquals(addressV6_67_0, address); 668 669 InetAddress addressV6_ffffff = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); 670 address = addressV6_ffffff; 671 try { 672 address = InetAddresses.increment(address); 673 fail(); 674 } catch (IllegalArgumentException expected) { 675 } 676 } 677 testDecrementIPv4()678 public void testDecrementIPv4() throws UnknownHostException { 679 InetAddress address660 = InetAddress.getByName("172.24.66.0"); 680 InetAddress address66255 = InetAddress.getByName("172.24.66.255"); 681 InetAddress address670 = InetAddress.getByName("172.24.67.0"); 682 683 InetAddress address = address670; 684 address = InetAddresses.decrement(address); 685 686 assertEquals(address66255, address); 687 688 for (int i = 0; i < 255; i++) { 689 address = InetAddresses.decrement(address); 690 } 691 assertEquals(address660, address); 692 693 InetAddress address0000 = InetAddress.getByName("0.0.0.0"); 694 address = address0000; 695 try { 696 address = InetAddresses.decrement(address); 697 fail(); 698 } catch (IllegalArgumentException expected) { 699 } 700 } 701 testDecrementIPv6()702 public void testDecrementIPv6() throws UnknownHostException { 703 InetAddress addressV6660 = InetAddress.getByName("2001:db8::6600"); 704 InetAddress addressV666ff = InetAddress.getByName("2001:db8::66ff"); 705 InetAddress addressV6670 = InetAddress.getByName("2001:db8::6700"); 706 707 InetAddress address = addressV6670; 708 address = InetAddresses.decrement(address); 709 710 assertEquals(addressV666ff, address); 711 712 for (int i = 0; i < 255; i++) { 713 address = InetAddresses.decrement(address); 714 } 715 assertEquals(addressV6660, address); 716 717 InetAddress addressV6000000 = InetAddress.getByName("0:0:0:0:0:0:0:0"); 718 address = addressV6000000; 719 try { 720 address = InetAddresses.decrement(address); 721 fail(); 722 } catch (IllegalArgumentException expected) { 723 } 724 } 725 } 726