Inet6AddressTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.net; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestInfo; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTarget; 24 25import java.io.Serializable; 26import java.net.Inet6Address; 27import java.net.InetAddress; 28import java.net.NetworkInterface; 29import java.net.UnknownHostException; 30 31import org.apache.harmony.testframework.serialization.SerializationTest; 32import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 33 34@TestTargetClass(Inet6Address.class) 35public class Inet6AddressTest extends junit.framework.TestCase { 36 37 /** 38 * @tests java.net.Inet6Address#isMulticastAddress() 39 */ 40@TestInfo( 41 level = TestLevel.COMPLETE, 42 purpose = "", 43 targets = { 44 @TestTarget( 45 methodName = "isMulticastAddress", 46 methodArgs = {} 47 ) 48 }) 49 public void test_isMulticastAddress() { 50 51 String addrName = ""; 52 InetAddress addr = null; 53 54 try { 55 56 // IP V6 regular multicast and non-multicast tests 57 // 58 // Create 2 IP v6 addresses and call "isMulticastAddress()" 59 // A prefix of "11111111" means that the address is multicast 60 // The first one will be one with the prefix the second without 61 62 addrName = "FFFF::42:42"; // 11111111 = FFFF 63 addr = InetAddress.getByName(addrName); 64 assertTrue("Multicast address " + addrName + " not detected.", addr 65 .isMulticastAddress()); 66 67 addrName = "42::42:42"; // an non-multicast address 68 addr = InetAddress.getByName(addrName); 69 assertTrue("Non multicast address " + addrName 70 + " reporting as a multicast address.", !addr 71 .isMulticastAddress()); 72 73 // IPv4-compatible IPv6 address tests 74 // 75 // Now create 2 IP v6 addresses that are IP v4 compatable 76 // to IP v6 addresses. The address prefix for a multicast ip v4 77 // address is 1110 for the last 16 bits ::d.d.d.d 78 // We expect these to be false 79 80 addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224 81 addr = InetAddress.getByName(addrName); 82 assertTrue("IPv4 compatable address " + addrName 83 + " reported incorrectly as multicast.", !addr 84 .isMulticastAddress()); 85 86 addrName = "::42.42.42.42"; // an ipv4 non-multicast address 87 addr = InetAddress.getByName(addrName); 88 assertTrue("IPv4 compatable address " + addrName 89 + " reported incorrectly as multicast.", !addr 90 .isMulticastAddress()); 91 92 // IPv4-mapped IPv6 address tests 93 // 94 // Now create 2 IP v6 addresses that are IP v4 compatable 95 // to IP v6 addresses. The address prefix for a multicast ip v4 96 // address is 1110 for the last 16 bits ::FFFF:d.d.d.d 97 98 addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 = 99 // 224 100 addr = InetAddress.getByName(addrName); 101 assertTrue("IPv4-mapped IPv6 multicast address " + addrName 102 + " not detected.", addr.isMulticastAddress()); 103 104 addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address 105 addr = InetAddress.getByName(addrName); 106 assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName 107 + " reporting as a multicast address.", !addr 108 .isMulticastAddress()); 109 } catch (Exception e) { 110 fail("Unknown address : " + addrName); 111 } 112 } 113 114 /** 115 * @tests java.net.Inet6Address#isAnyLocalAddress() 116 */ 117@TestInfo( 118 level = TestLevel.COMPLETE, 119 purpose = "", 120 targets = { 121 @TestTarget( 122 methodName = "isAnyLocalAddress", 123 methodArgs = {} 124 ) 125 }) 126 public void test_isAnyLocalAddress() { 127 128 String addrName = ""; 129 InetAddress addr = null; 130 131 try { 132 133 // test to ensure that the unspecified address returns tru 134 addrName = "::0"; // The unspecified address 135 addr = InetAddress.getByName(addrName); 136 assertTrue( 137 "The unspecified (also known as wildcard and any local address) " 138 + addrName + " not detected.", addr 139 .isAnyLocalAddress()); 140 141 addrName = "::"; // another form of the unspecified address 142 addr = InetAddress.getByName(addrName); 143 assertTrue( 144 "The unspecified (also known as wildcard and any local address) " 145 + addrName + " not detected.", addr 146 .isAnyLocalAddress()); 147 148 addrName = "::1"; // The loopback address 149 addr = InetAddress.getByName(addrName); 150 assertTrue("The addresses " + addrName 151 + " incorrectly reporting an the unspecified address.", 152 !addr.isAnyLocalAddress()); 153 154 } catch (Exception e) { 155 fail("Unknown address : " + addrName); 156 } 157 } 158 159 /** 160 * @tests java.net.Inet6Address#isLoopbackAddress() 161 */ 162@TestInfo( 163 level = TestLevel.COMPLETE, 164 purpose = "", 165 targets = { 166 @TestTarget( 167 methodName = "isLoopbackAddress", 168 methodArgs = {} 169 ) 170 }) 171 public void test_isLoopbackAddress() { 172 173 String addrName = ""; 174 try { 175 176 // IP V6 regular address tests for loopback 177 // The loopback address for IPv6 is ::1 178 179 addrName = "::1"; 180 InetAddress addr = InetAddress.getByName(addrName); 181 assertTrue("IPv6 loopback address " + addrName + " not detected.", 182 addr.isLoopbackAddress()); 183 184 addrName = "::2"; 185 addr = InetAddress.getByName(addrName); 186 assertTrue("IPv6 address incorrectly " + addrName 187 + " detected as a loopback address.", !addr 188 .isLoopbackAddress()); 189 190 // a loopback address should be 127.d.d.d 191 addrName = "42:42::42:42"; 192 addr = InetAddress.getByName(addrName); 193 assertTrue("IPv6 address incorrectly " + addrName 194 + " detected as a loopback address.", !addr 195 .isLoopbackAddress()); 196 197 // IPv4-compatible IPv6 address tests 198 // 199 // Now create 2 IP v6 addresses that are IP v4 compatable 200 // to IP v6 addresses. The address prefix for a multicast ip v4 201 // address is 1110 for the last 16 bits ::d.d.d.d 202 // We expect these to be false, as they are not IPv4 addresses 203 204 // a loopback address should be 127.d.d.d 205 addrName = "::127.0.0.0"; 206 addr = InetAddress.getByName(addrName); 207 assertTrue("IPv4-compatible IPv6 address " + addrName 208 + " detected incorrectly as a loopback.", !addr 209 .isLoopbackAddress()); 210 211 addrName = "::127.42.42.42"; // a loopback address should be 212 // 127.d.d.d 213 addr = InetAddress.getByName(addrName); 214 assertTrue("IPv4-compatible IPv6 address " + addrName 215 + " detected incorrectly as a loopback.", !addr 216 .isLoopbackAddress()); 217 218 // a loopback address should be 127.d.d.d 219 addrName = "::42.42.42.42"; 220 addr = InetAddress.getByName(addrName); 221 assertTrue("IPv4-compatible IPv6 address " + addrName 222 + " detected incorrectly as a loopback.", !addr 223 .isLoopbackAddress()); 224 225 // IPv4-mapped IPv6 address tests 226 // 227 // Now create 2 IP v6 addresses that are IP v4 compatable 228 // to IP v6 addresses. The address prefix for a multicast ip v4 229 // address is 1110 for the last 16 bits ::FFFF:d.d.d.d 230 231 // a loopback address should be 127.d.d.d 232 addrName = "::FFFF:127.0.0.0"; 233 addr = InetAddress.getByName(addrName); 234 assertTrue("IPv4-compatible IPv6 loopback address " + addrName 235 + " not detected.", addr.isLoopbackAddress()); 236 237 // a loopback address should be 127.d.d.d 238 addrName = "::FFFF:127.42.42.42"; 239 addr = InetAddress.getByName(addrName); 240 assertTrue("IPv4-compatible IPv6 loopback address " + addrName 241 + " not detected.", addr.isLoopbackAddress()); 242 243 // a loopback address should be 127.d.d.d 244 addrName = "::FFFF:42.42.42.42"; 245 addr = InetAddress.getByName(addrName); 246 assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName 247 + " detected as a loopback address.", !addr 248 .isLoopbackAddress()); 249 250 } catch (UnknownHostException e) { 251 fail("Unknown address : " + addrName); 252 } 253 } 254 255 /** 256 * @tests java.net.Inet6Address#isLinkLocalAddress() 257 */ 258@TestInfo( 259 level = TestLevel.COMPLETE, 260 purpose = "", 261 targets = { 262 @TestTarget( 263 methodName = "isLinkLocalAddress", 264 methodArgs = {} 265 ) 266 }) 267 public void test_isLinkLocalAddress() { 268 269 String addrName = ""; 270 try { 271 // IP V6 regular address tests for link local addresses 272 // 273 // Link local addresses are FE80:: - 274 // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 275 276 addrName = "FE80::0"; 277 InetAddress addr = InetAddress.getByName(addrName); 278 assertTrue( 279 "IPv6 link local address " + addrName + " not detected.", 280 addr.isLinkLocalAddress()); 281 282 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF"; 283 addr = InetAddress.getByName(addrName); 284 assertTrue( 285 "IPv6 link local address " + addrName + " not detected.", 286 addr.isLinkLocalAddress()); 287 288 addrName = "FEC0::1"; 289 addr = InetAddress.getByName(addrName); 290 assertTrue("IPv6 address " + addrName 291 + " detected incorrectly as a link local address.", !addr 292 .isLinkLocalAddress()); 293 294 addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF"; 295 addr = InetAddress.getByName(addrName); 296 assertTrue("IPv6 address " + addrName 297 + " detected incorrectly as a link local address.", !addr 298 .isLinkLocalAddress()); 299 300 addrName = "FE7F::FFFF:FFFF:FFFF:FFFF"; 301 addr = InetAddress.getByName(addrName); 302 assertTrue("IPv6 address " + addrName 303 + " detected incorrectly as a link local address.", !addr 304 .isLinkLocalAddress()); 305 } catch (Exception e) { 306 fail("Unknown address : " + addrName); 307 } 308 309 } 310 311 /** 312 * @tests java.net.Inet6Address#isSiteLocalAddress() 313 */ 314@TestInfo( 315 level = TestLevel.COMPLETE, 316 purpose = "", 317 targets = { 318 @TestTarget( 319 methodName = "isSiteLocalAddress", 320 methodArgs = {} 321 ) 322 }) 323 public void test_isSiteLocalAddress() { 324 String addrName = ""; 325 try { 326 // IP V6 regular address tests for link local addresses 327 // 328 // Link local addresses are FEC0::0 through to 329 // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 330 331 addrName = "FEC0::0"; 332 InetAddress addr = InetAddress.getByName(addrName); 333 assertTrue( 334 "IPv6 site local address " + addrName + " not detected.", 335 addr.isSiteLocalAddress()); 336 337 addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF"; 338 addr = InetAddress.getByName(addrName); 339 assertTrue( 340 "IPv6 site local address " + addrName + " not detected.", 341 addr.isSiteLocalAddress()); 342 343 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF"; 344 addr = InetAddress.getByName(addrName); 345 assertTrue("IPv6 address " + addrName 346 + " detected incorrectly as a site local address.", !addr 347 .isSiteLocalAddress()); 348 349 addrName = "FFC0::0"; 350 addr = InetAddress.getByName(addrName); 351 assertTrue("IPv6 address " + addrName 352 + " detected incorrectly as a site local address.", !addr 353 .isSiteLocalAddress()); 354 355 } catch (Exception e) { 356 fail("Unknown address : " + addrName); 357 } 358 } 359 360 /** 361 * @tests java.net.Inet6Address#isMCGlobal() 362 */ 363@TestInfo( 364 level = TestLevel.COMPLETE, 365 purpose = "", 366 targets = { 367 @TestTarget( 368 methodName = "isMCGlobal", 369 methodArgs = {} 370 ) 371 }) 372 public void test_isMCGlobal() { 373 String addrName = ""; 374 try { 375 // IP V6 regular address tests for Mulitcase Global addresses 376 // 377 // Multicast global addresses are FFxE:/112 where x is 378 // a set of flags, and the addition 112 bits make up 379 // the global address space 380 381 addrName = "FF0E::0"; 382 InetAddress addr = InetAddress.getByName(addrName); 383 assertTrue("IPv6 global mutlicast address " + addrName 384 + " not detected.", addr.isMCGlobal()); 385 386 addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 387 addr = InetAddress.getByName(addrName); 388 assertTrue("IPv6 global multicast address " + addrName 389 + " not detected.", addr.isMCGlobal()); 390 391 // a currently invalid address as the prefix FFxE 392 // is only valid for x = {1,0} as the rest are reserved 393 addrName = "FFFE::0"; 394 addr = InetAddress.getByName(addrName); 395 assertTrue("IPv6 global mutlicast address " + addrName 396 + " not detected.", addr.isMCGlobal()); 397 398 // a currently invalid address as the prefix FFxE 399 // is only valid for x = {1,0} as the rest are reserved 400 addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 401 addr = InetAddress.getByName(addrName); 402 assertTrue("IPv6 global multicast address " + addrName 403 + " not detected.", addr.isMCGlobal()); 404 405 // a sample MC organizational address 406 addrName = "FF08:42:42:42:42:42:42:42"; 407 addr = InetAddress.getByName(addrName); 408 assertTrue("IPv6 mulitcast organizational " + addrName 409 + " incorrectly indicated as a global address.", !addr 410 .isMCGlobal()); 411 412 // a sample MC site address 413 addrName = "FF05:42:42:42:42:42:42:42"; 414 addr = InetAddress.getByName(addrName); 415 assertTrue("IPv6 mulitcast site address " + addrName 416 + " incorrectly indicated as a global address.", !addr 417 .isMCGlobal()); 418 419 // a sample MC link address 420 addrName = "FF02:42:42:42:42:42:42:42"; 421 addr = InetAddress.getByName(addrName); 422 assertTrue("IPv6 mulitcast link address " + addrName 423 + " incorrectly indicated as a global address.", !addr 424 .isMCGlobal()); 425 426 // a sample MC Node 427 addrName = "FF01:42:42:42:42:42:42:42"; 428 addr = InetAddress.getByName(addrName); 429 assertTrue("IPv6 mulitcast node address " + addrName 430 + " incorrectly indicated as a global address.", !addr 431 .isMCGlobal()); 432 433 // IPv4-mapped IPv6 address tests 434 addrName = "::FFFF:224.0.1.0"; 435 addr = InetAddress.getByName(addrName); 436 assertTrue("IPv4 global multicast address " + addrName 437 + " not identified as a global multicast address.", addr 438 .isMCGlobal()); 439 440 addrName = "::FFFF:238.255.255.255"; 441 addr = InetAddress.getByName(addrName); 442 assertTrue("IPv4 global multicast address " + addrName 443 + " not identified as a global multicast address.", addr 444 .isMCGlobal()); 445 446 } catch (Exception e) { 447 fail("Unknown address : " + addrName); 448 } 449 } 450 451 /** 452 * @tests java.net.Inet6Address#isMCNodeLocal() 453 */ 454@TestInfo( 455 level = TestLevel.COMPLETE, 456 purpose = "", 457 targets = { 458 @TestTarget( 459 methodName = "isMCNodeLocal", 460 methodArgs = {} 461 ) 462 }) 463 public void test_isMCNodeLocal() { 464 String addrName = ""; 465 try { 466 // IP V6 regular address tests for Mulitcase node local addresses 467 // 468 // Multicast node local addresses are FFx1:/112 where x is 469 // a set of flags, and the addition 112 bits make up 470 // the global address space 471 472 addrName = "FF01::0"; 473 InetAddress addr = InetAddress.getByName(addrName); 474 assertTrue("IPv6 node-local mutlicast address " + addrName 475 + " not detected.", addr.isMCNodeLocal()); 476 477 addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 478 addr = InetAddress.getByName(addrName); 479 assertTrue("IPv6 node-local multicast address " + addrName 480 + " not detected.", addr.isMCNodeLocal()); 481 482 // a currently invalid address as the prefix FFxE 483 // is only valid for x = {1,0} as the rest are reserved 484 addrName = "FFF1::0"; 485 addr = InetAddress.getByName(addrName); 486 assertTrue("IPv6 node-local mutlicast address " + addrName 487 + " not detected.", addr.isMCNodeLocal()); 488 489 // a currently invalid address as the prefix FFxE 490 // is only valid for x = {1,0} as the rest are reserved 491 addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 492 addr = InetAddress.getByName(addrName); 493 assertTrue("IPv6 node-local multicast address " + addrName 494 + " not detected.", addr.isMCNodeLocal()); 495 496 // a sample MC organizational address 497 addrName = "FF08:42:42:42:42:42:42:42"; 498 addr = InetAddress.getByName(addrName); 499 assertTrue("IPv6 mulitcast organizational address " + addrName 500 + " incorrectly indicated as a node-local address.", !addr 501 .isMCNodeLocal()); 502 503 // a sample MC site address 504 addrName = "FF05:42:42:42:42:42:42:42"; 505 addr = InetAddress.getByName(addrName); 506 assertTrue("IPv6 mulitcast site address " + addrName 507 + " incorrectly indicated as a node-local address.", !addr 508 .isMCNodeLocal()); 509 510 // a sample MC link address 511 addrName = "FF02:42:42:42:42:42:42:42"; 512 addr = InetAddress.getByName(addrName); 513 assertTrue("IPv6 mulitcast link address " + addrName 514 + " incorrectly indicated as a node-local address.", !addr 515 .isMCNodeLocal()); 516 517 // a sample MC global address 518 addrName = "FF0E:42:42:42:42:42:42:42"; 519 addr = InetAddress.getByName(addrName); 520 assertTrue("IPv6 mulitcast node address " + addrName 521 + " incorrectly indicated as a node-local address.", !addr 522 .isMCNodeLocal()); 523 524 } catch (Exception e) { 525 fail("Unknown address : " + addrName); 526 } 527 } 528 529 /** 530 * @tests java.net.Inet6Address#isMCLinkLocal() 531 */ 532@TestInfo( 533 level = TestLevel.COMPLETE, 534 purpose = "", 535 targets = { 536 @TestTarget( 537 methodName = "isMCLinkLocal", 538 methodArgs = {} 539 ) 540 }) 541 public void test_isMCLinkLocal() { 542 String addrName = ""; 543 try { 544 // IP V6 regular address tests for Mulitcase link local addresses 545 // 546 // Multicast link local addresses are FFx2:/112 where x is 547 // a set of flags, and the addition 112 bits make up 548 // the global address space 549 550 addrName = "FF02::0"; 551 InetAddress addr = InetAddress.getByName(addrName); 552 assertTrue("IPv6 link local multicast address " + addrName 553 + " not detected.", addr.isMCLinkLocal()); 554 555 addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 556 addr = InetAddress.getByName(addrName); 557 assertTrue("IPv6 link local multicast address " + addrName 558 + " not detected.", addr.isMCLinkLocal()); 559 560 // a currently invalid address as the prefix FFxE 561 // is only valid for x = {1,0} as the rest are reserved 562 addrName = "FFF2::0"; 563 addr = InetAddress.getByName(addrName); 564 assertTrue("IPv6 link local multicast address " + addrName 565 + " not detected.", addr.isMCLinkLocal()); 566 567 // a currently invalid address as the prefix FFxE 568 // is only valid for x = {1,0} as the rest are reserved 569 addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 570 addr = InetAddress.getByName(addrName); 571 assertTrue("IPv6 link local multicast address " + addrName 572 + " not detected.", addr.isMCLinkLocal()); 573 574 // a sample MC organizational address 575 addrName = "FF08:42:42:42:42:42:42:42"; 576 addr = InetAddress.getByName(addrName); 577 assertTrue( 578 "IPv6 organization multicast address " 579 + addrName 580 + " incorrectly indicated as a link-local mulitcast address.", 581 !addr.isMCLinkLocal()); 582 583 // a sample MC site address 584 addrName = "FF05:42:42:42:42:42:42:42"; 585 addr = InetAddress.getByName(addrName); 586 assertTrue( 587 "IPv6 site-local mulitcast address " 588 + addrName 589 + " incorrectly indicated as a link-local mulitcast address.", 590 !addr.isMCLinkLocal()); 591 592 // a sample MC global address 593 addrName = "FF0E:42:42:42:42:42:42:42"; 594 addr = InetAddress.getByName(addrName); 595 assertTrue( 596 "IPv6 global multicast address " 597 + addrName 598 + " incorrectly indicated as a link-local mulitcast address.", 599 !addr.isMCLinkLocal()); 600 601 // a sample MC Node 602 addrName = "FF01:42:42:42:42:42:42:42"; 603 addr = InetAddress.getByName(addrName); 604 assertTrue( 605 "IPv6 mulitcast node address " 606 + addrName 607 + " incorrectly indicated as a link-local mulitcast address.", 608 !addr.isMCLinkLocal()); 609 610 // Ipv4-mapped IPv6 addresses 611 612 addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110 613 addr = InetAddress.getByName(addrName); 614 assertTrue("IPv4 link-local multicast address " + addrName 615 + " not identified as a link-local multicast address.", 616 addr.isMCLinkLocal()); 617 618 addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110 619 addr = InetAddress.getByName(addrName); 620 assertTrue("IPv4 link-local multicast address " + addrName 621 + " not identified as a link-local multicast address.", 622 addr.isMCLinkLocal()); 623 624 } catch (Exception e) { 625 fail("Unknown address : " + addrName); 626 } 627 } 628 629 /** 630 * @tests java.net.Inet6Address#isMCSiteLocal() 631 */ 632@TestInfo( 633 level = TestLevel.COMPLETE, 634 purpose = "", 635 targets = { 636 @TestTarget( 637 methodName = "isMCSiteLocal", 638 methodArgs = {} 639 ) 640 }) 641 public void test_isMCSiteLocal() { 642 String addrName = ""; 643 try { 644 // IP V6 regular address tests for Multicast site-local addresses 645 // 646 // Multicast global addresses are FFx5:/112 where x is 647 // a set of flags, and the addition 112 bits make up 648 // the global address space 649 650 addrName = "FF05::0"; 651 InetAddress addr = InetAddress.getByName(addrName); 652 assertTrue("IPv6 site-local mutlicast address " + addrName 653 + " not detected.", addr.isMCSiteLocal()); 654 655 addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 656 addr = InetAddress.getByName(addrName); 657 assertTrue("IPv6 site-local multicast address " + addrName 658 + " not detected.", addr.isMCSiteLocal()); 659 660 // a currently invalid address as the prefix FFxE 661 // is only valid for x = {1,0} as the rest are reserved 662 addrName = "FFF5::0"; 663 addr = InetAddress.getByName(addrName); 664 assertTrue("IPv6 site-local mutlicast address " + addrName 665 + " not detected.", addr.isMCSiteLocal()); 666 667 // a currently invalid address as the prefix FFxE 668 // is only valid for x = {1,0} as the rest are reserved 669 addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 670 addr = InetAddress.getByName(addrName); 671 assertTrue("IPv6 site-local multicast address " + addrName 672 + " not detected.", addr.isMCSiteLocal()); 673 674 // a sample MC organizational address 675 addrName = "FF08:42:42:42:42:42:42:42"; 676 addr = InetAddress.getByName(addrName); 677 assertTrue( 678 "IPv6 organization multicast address " 679 + addrName 680 + " incorrectly indicated as a site-local mulitcast address.", 681 !addr.isMCSiteLocal()); 682 683 // a sample MC global address 684 addrName = "FF0E:42:42:42:42:42:42:42"; 685 addr = InetAddress.getByName(addrName); 686 assertTrue( 687 "IPv6 global mulitcast address " 688 + addrName 689 + " incorrectly indicated as a site-local mulitcast address.", 690 !addr.isMCSiteLocal()); 691 692 // a sample MC link address 693 addrName = "FF02:42:42:42:42:42:42:42"; 694 addr = InetAddress.getByName(addrName); 695 assertTrue( 696 "IPv6 link-local multicast address " 697 + addrName 698 + " incorrectly indicated as a site-local mulitcast address.", 699 !addr.isMCSiteLocal()); 700 701 // a sample MC Node 702 addrName = "FF01:42:42:42:42:42:42:42"; 703 addr = InetAddress.getByName(addrName); 704 assertTrue( 705 "IPv6 mulitcast node address " 706 + addrName 707 + " incorrectly indicated as a site-local mulitcast address.", 708 !addr.isMCSiteLocal()); 709 710 // IPv4-mapped IPv6 addresses 711 addrName = "::FFFF:239.255.0.0"; 712 addr = InetAddress.getByName(addrName); 713 assertTrue("IPv4 site-local multicast address " + addrName 714 + " not identified as a site-local multicast address.", 715 addr.isMCSiteLocal()); 716 717 addrName = "::FFFF:239.255.255.255"; 718 addr = InetAddress.getByName(addrName); 719 assertTrue("IPv4 site-local multicast address " + addrName 720 + " not identified as a site-local multicast address.", 721 addr.isMCSiteLocal()); 722 723 } catch (Exception e) { 724 fail("Unknown address : " + addrName); 725 } 726 } 727 728 /** 729 * @tests java.net.Inet6Address#isMCOrgLocal() 730 */ 731@TestInfo( 732 level = TestLevel.COMPLETE, 733 purpose = "", 734 targets = { 735 @TestTarget( 736 methodName = "isMCOrgLocal", 737 methodArgs = {} 738 ) 739 }) 740 public void test_isMCOrgLocal() { 741 String addrName = ""; 742 try { 743 // IP V6 regular address tests for Mulitcase organization-local 744 // addresses 745 // 746 // Multicast global addresses are FFxE:/112 where x is 747 // a set of flags, and the addition 112 bits make up 748 // the global address space 749 750 addrName = "FF08::0"; 751 InetAddress addr = InetAddress.getByName(addrName); 752 assertTrue("IPv6 organization-local mutlicast address " + addrName 753 + " not detected.", addr.isMCOrgLocal()); 754 755 addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 756 addr = InetAddress.getByName(addrName); 757 assertTrue("IPv6 organization-local multicast address " + addrName 758 + " not detected.", addr.isMCOrgLocal()); 759 760 // a currently invalid address as the prefix FFxE 761 // is only valid for x = {1,0} as the rest are reserved 762 addrName = "FFF8::0"; 763 addr = InetAddress.getByName(addrName); 764 assertTrue("IPv6 organization-local mutlicast address " + addrName 765 + " not detected.", addr.isMCOrgLocal()); 766 767 // a currently invalid address as the prefix FFxE 768 // is only valid for x = {1,0} as the rest are reserved 769 addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 770 addr = InetAddress.getByName(addrName); 771 assertTrue("IPv6 organization-local multicast address " + addrName 772 + " not detected.", addr.isMCOrgLocal()); 773 774 // a sample MC global address 775 addrName = "FF0E:42:42:42:42:42:42:42"; 776 addr = InetAddress.getByName(addrName); 777 assertTrue( 778 "IPv6 global multicast address " 779 + addrName 780 + " incorrectly indicated as an organization-local mulitcast address.", 781 !addr.isMCOrgLocal()); 782 783 // a sample MC site address 784 addrName = "FF05:42:42:42:42:42:42:42"; 785 addr = InetAddress.getByName(addrName); 786 assertTrue( 787 "IPv6 site-local mulitcast address " 788 + addrName 789 + " incorrectly indicated as an organization-local mulitcast address.", 790 !addr.isMCOrgLocal()); 791 792 // a sample MC link address 793 addrName = "FF02:42:42:42:42:42:42:42"; 794 addr = InetAddress.getByName(addrName); 795 assertTrue( 796 "IPv6 link-local multicast address " 797 + addrName 798 + " incorrectly indicated as an organization-local mulitcast address.", 799 !addr.isMCOrgLocal()); 800 801 // a sample MC Node 802 addrName = "FF01:42:42:42:42:42:42:42"; 803 addr = InetAddress.getByName(addrName); 804 assertTrue( 805 "IPv6 mulitcast node address " 806 + addrName 807 + " incorrectly indicated as an organization-local mulitcast address.", 808 !addr.isMCOrgLocal()); 809 810 // IPv4-mapped IPv6 addresses 811 812 addrName = "::FFFF:239.192.0.0"; 813 addr = InetAddress.getByName(addrName); 814 assertTrue("IPv4 org-local multicast address " + addrName 815 + " not identified as a org-local multicast address.", addr 816 .isMCOrgLocal()); 817 818 addrName = "::FFFF:239.195.255.255"; 819 addr = InetAddress.getByName(addrName); 820 assertTrue("IPv4 org-local multicast address " + addrName 821 + " not identified as a org-local multicast address.", addr 822 .isMCOrgLocal()); 823 824 } catch (Exception e) { 825 fail("Unknown address : " + addrName); 826 } 827 } 828 829 /** 830 * @tests java.net.Inet6Address#isIPv4CompatibleAddress() 831 */ 832@TestInfo( 833 level = TestLevel.COMPLETE, 834 purpose = "", 835 targets = { 836 @TestTarget( 837 methodName = "isIPv4CompatibleAddress", 838 methodArgs = {} 839 ) 840 }) 841 public void test_isIPv4CompatibleAddress() { 842 String addrName = ""; 843 Inet6Address addr = null; 844 845 try { 846 847 // Tests a number of addresses to see if they are compatable with 848 // IPv6 addresses 849 850 addrName = "FFFF::42:42"; // 11111111 = FFFF 851 addr = (Inet6Address) InetAddress.getByName(addrName); 852 assertTrue("A non-compatable IPv6 address " + addrName 853 + " incorrectly identified as a IPv4 compatable address.", 854 !addr.isIPv4CompatibleAddress()); 855 856 // IPv4-compatible IPv6 address tests 857 // 858 // Now create 2 IP v6 addresses that are IP v4 compatable 859 // to IP v6 addresses. 860 861 addrName = "::0.0.0.0"; 862 addr = (Inet6Address) InetAddress.getByName(addrName); 863 assertTrue("IPv4 compatable address " + addrName 864 + " not detected correctly.", addr 865 .isIPv4CompatibleAddress()); 866 867 addrName = "::255.255.255.255"; // an ipv4 non-multicast address 868 addr = (Inet6Address) InetAddress.getByName(addrName); 869 assertTrue("IPv4 compatable address " + addrName 870 + " not detected correctly.", addr 871 .isIPv4CompatibleAddress()); 872 873 } catch (Exception e) { 874 e.printStackTrace(); 875 fail("Unknown address : " + addrName); 876 } 877 } 878 879 /** 880 * @tests java.net.Inet6Address#getAddress() 881 */ 882@TestInfo( 883 level = TestLevel.TODO, 884 purpose = "Test is empty.", 885 targets = { 886 @TestTarget( 887 methodName = "getAddress", 888 methodArgs = {} 889 ) 890 }) 891 public void test_getAddress() { 892 // TODO : Implementation 893 } 894 895 /** 896 * @tests java.net.Inet6Address#getByName(java.lang.String) 897 */ 898@TestInfo( 899 level = TestLevel.PARTIAL, 900 purpose = "SecurityException checking missed.", 901 targets = { 902 @TestTarget( 903 methodName = "getByName", 904 methodArgs = {String.class} 905 ) 906 }) 907 public void test_getByNameLjava_lang_String() throws Exception { 908 // ones to add "::255.255.255.255", "::FFFF:0.0.0.0", 909 // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F", 910 // "[F:F:F:F:F:F:F:F]" 911 String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::", 912 "::1", "0", /* jdk1.5 accepts 0 as valid */ 913 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 914 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", 915 "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" }; 916 917 String invalidIPAddresses[] = { "FFFF:FFFF" }; 918 919 for (int i = 0; i < validIPAddresses.length; i++) { 920 921 InetAddress.getByName(validIPAddresses[i]); 922 923 //exercise positive cache 924 InetAddress.getByName(validIPAddresses[i]); 925 926 if (!validIPAddresses[i].equals("0")) { 927 String tempIPAddress = "[" + validIPAddresses[i] + "]"; 928 InetAddress.getByName(tempIPAddress); 929 } 930 } 931 932 for (int i = 0; i < invalidIPAddresses.length; i++) { 933 try { 934 InetAddress.getByName(invalidIPAddresses[i]); 935 fail("Invalid IP address incorrectly recognized as valid: " 936 + invalidIPAddresses[i]); 937 } catch (Exception e) { 938 } 939 940 //exercise negative cache 941 try { 942 InetAddress.getByName(invalidIPAddresses[i]); 943 fail("Invalid IP address incorrectly recognized as valid: " 944 + invalidIPAddresses[i]); 945 } catch (Exception e) { 946 } 947 } 948 } 949 950 /** 951 * @tests java.net.Inet6Address#getByAddress(String, byte[], int) 952 */ 953@TestInfo( 954 level = TestLevel.COMPLETE, 955 purpose = "", 956 targets = { 957 @TestTarget( 958 methodName = "getByAddress", 959 methodArgs = {String.class, byte[].class, int.class} 960 ) 961 }) 962 public void test_getByAddressLString$BI() throws UnknownHostException{ 963 try { 964 Inet6Address.getByAddress("123", null, 0); 965 fail("should throw UnknownHostException"); 966 } catch (UnknownHostException uhe) { 967 // expected 968 } 969 byte[] addr1 = { (byte) 127, 0, 0, 1 }; 970 try { 971 Inet6Address.getByAddress("123", addr1, 0); 972 fail("should throw UnknownHostException"); 973 } catch (UnknownHostException uhe) { 974 // expected 975 } 976 977 byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 978 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, 979 (byte) 0xB2 }; 980 981 // should not throw any exception 982 Inet6Address.getByAddress("123", addr2, 3); 983 Inet6Address.getByAddress("123", addr2, 0); 984 Inet6Address.getByAddress("123", addr2, -1); 985 } 986 987 /** 988 * @tests java.net.Inet6Address#getByAddress(String, byte[], 989 * NetworkInterface) 990 */ 991@TestInfo( 992 level = TestLevel.COMPLETE, 993 purpose = "", 994 targets = { 995 @TestTarget( 996 methodName = "getByAddress", 997 methodArgs = {String.class, byte[].class, NetworkInterface.class} 998 ) 999 }) 1000 public void test_getByAddressLString$BLNetworkInterface() 1001 throws UnknownHostException { 1002 NetworkInterface nif = null; 1003 try { 1004 Inet6Address.getByAddress("123", null, nif); 1005 fail("should throw UnknownHostException"); 1006 } catch (UnknownHostException uhe) { 1007 // expected 1008 } 1009 byte[] addr1 = { (byte) 127, 0, 0, 1 }; 1010 try { 1011 Inet6Address.getByAddress("123", addr1, nif); 1012 fail("should throw UnknownHostException"); 1013 } catch (UnknownHostException uhe) { 1014 // expected 1015 } 1016 byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 1017 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 1018 1019 0x7C, (byte) 0xB2 }; 1020 // should not throw any exception 1021 Inet6Address.getByAddress("123", addr2, nif); 1022 } 1023 1024 /** 1025 * @throws UnknownHostException 1026 * @tests java.net.Inet6Address#getScopeID() 1027 */ 1028@TestInfo( 1029 level = TestLevel.COMPLETE, 1030 purpose = "", 1031 targets = { 1032 @TestTarget( 1033 methodName = "getScopeId", 1034 methodArgs = {} 1035 ) 1036 }) 1037 public void test_getScopeID() throws UnknownHostException { 1038 Inet6Address v6ia; 1039 byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11, 1040 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, 1041 (byte) 0xB2 }; 1042 1043 v6ia = Inet6Address.getByAddress("123", addr, 3); 1044 assertEquals(3, v6ia.getScopeId()); 1045 1046 v6ia = Inet6Address.getByAddress("123", addr, 0); 1047 assertEquals(0, v6ia.getScopeId()); 1048 1049 v6ia = Inet6Address.getByAddress("123", addr, -1); 1050 assertEquals(0, v6ia.getScopeId()); 1051 } 1052 1053 /** 1054 * @tests java.net.Inet6Address#getScopedInterface() 1055 */ 1056@TestInfo( 1057 level = TestLevel.COMPLETE, 1058 purpose = "", 1059 targets = { 1060 @TestTarget( 1061 methodName = "getScopedInterface", 1062 methodArgs = {} 1063 ) 1064 }) 1065 public void test_getScopedInterface() throws UnknownHostException { 1066 byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5, 1067 (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09, 1068 (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 }; 1069 Inet6Address v6Addr; 1070 v6Addr = Inet6Address.getByAddress("123", addr, null); 1071 assertNull(v6Addr.getScopedInterface()); 1072 } 1073 1074 1075 int bytesToInt(byte bytes[], int start) { 1076 1077 int byteMask = 255; 1078 int value = ((bytes[start + 3] & byteMask)) 1079 | ((bytes[start + 2] & byteMask) << 8) 1080 | ((bytes[start + 1] & byteMask) << 16) 1081 | ((bytes[start] & byteMask) << 24); 1082 return value; 1083 1084 } 1085 1086 String byteArrayToHexString(byte bytes[], boolean leadingZeros) { 1087 1088 String fullString = ""; 1089 int times = bytes.length / 4; 1090 int intArray[] = new int[times]; 1091 for (int i = 0; i < times; i++) { 1092 intArray[i] = bytesToInt(bytes, i * 4); 1093 } 1094 1095 return intArrayToHexString(intArray, leadingZeros); 1096 } 1097 1098 void intToBytes(int value, byte bytes[], int start) { 1099 1100 int byteMask = 255; 1101 bytes[start + 3] = (byte) (value & byteMask); 1102 bytes[start + 2] = (byte) ((value >> 8) & byteMask); 1103 bytes[start + 1] = (byte) ((value >> 16) & byteMask); 1104 bytes[start] = (byte) ((value >> 24) & byteMask); 1105 } 1106 1107 String intArrayToHexString(int ints[], boolean leadingZeros) { 1108 1109 String fullString = ""; 1110 String tempString; 1111 int intsLength = ints.length; 1112 for (int i = 0; i < intsLength; i++) { 1113 tempString = Integer.toHexString(ints[i]); 1114 while (tempString.length() < 4 && leadingZeros) { 1115 tempString = "0" + tempString; 1116 } 1117 if (i + 1 < intsLength) { 1118 tempString += ":"; 1119 } 1120 fullString += tempString; 1121 } 1122 1123 return fullString.toUpperCase(); 1124 } 1125 1126 // comparator for Inet6Address objects 1127 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 1128 public void assertDeserialized(Serializable initial, 1129 Serializable deserialized) { 1130 1131 Inet6Address initAddr = (Inet6Address) initial; 1132 Inet6Address desrAddr = (Inet6Address) deserialized; 1133 1134 byte[] iaAddresss = initAddr.getAddress(); 1135 byte[] deIAAddresss = desrAddr.getAddress(); 1136 for (int i = 0; i < iaAddresss.length; i++) { 1137 assertEquals(iaAddresss[i], deIAAddresss[i]); 1138 } 1139 assertEquals(initAddr.getScopeId(), desrAddr.getScopeId()); 1140 assertEquals(initAddr.getScopedInterface(), desrAddr 1141 .getScopedInterface()); 1142 } 1143 }; 1144 1145 /** 1146 * @tests serialization/deserialization compatibility. 1147 */ 1148@TestInfo( 1149 level = TestLevel.COMPLETE, 1150 purpose = "Checks serialization", 1151 targets = { 1152 @TestTarget( 1153 methodName = "!SerializationSelf", 1154 methodArgs = {} 1155 ) 1156 }) 1157 public void testSerializationSelf() throws Exception { 1158 1159 byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 1160 1161 SerializationTest.verifySelf(InetAddress.getByAddress(localv6), 1162 COMPARATOR); 1163 } 1164 1165 /** 1166 * @tests serialization/deserialization compatibility with RI. 1167 */ 1168@TestInfo( 1169 level = TestLevel.COMPLETE, 1170 purpose = "Checks serialization", 1171 targets = { 1172 @TestTarget( 1173 methodName = "!SerializationGolden", 1174 methodArgs = {} 1175 ) 1176 }) 1177 public void testSerializationCompatibility() throws Exception { 1178 1179 byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 1180 1181 Object[] addresses = { InetAddress.getByAddress(localv6), 1182 // Regression for Harmony-1039: ser-form has 1183 // null interface name 1184 InetAddress.getByAddress(localv6) }; 1185 1186 SerializationTest.verifyGolden(this, addresses, COMPARATOR); 1187 } 1188} 1189