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