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 java.io.Serializable; 21import java.net.Inet6Address; 22import java.net.InetAddress; 23import java.net.NetworkInterface; 24import java.net.UnknownHostException; 25import java.util.Locale; 26 27import org.apache.harmony.testframework.serialization.SerializationTest; 28import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 29 30public class Inet6AddressTest extends junit.framework.TestCase { 31 public void test_isMulticastAddress() throws Exception { 32 33 String addrName = ""; 34 InetAddress addr = null; 35 36 // IP V6 regular multicast and non-multicast tests 37 // 38 // Create 2 IP v6 addresses and call "isMulticastAddress()" 39 // A prefix of "11111111" means that the address is multicast 40 // The first one will be one with the prefix the second without 41 42 addrName = "FFFF::42:42"; // 11111111 = FFFF 43 addr = InetAddress.getByName(addrName); 44 assertTrue("Multicast address " + addrName + " not detected.", addr 45 .isMulticastAddress()); 46 47 addrName = "42::42:42"; // an non-multicast address 48 addr = InetAddress.getByName(addrName); 49 assertTrue("Non multicast address " + addrName 50 + " reporting as a multicast address.", !addr 51 .isMulticastAddress()); 52 53 // IPv4-compatible IPv6 address tests 54 // 55 // Now create 2 IP v6 addresses that are IP v4 compatable 56 // to IP v6 addresses. The address prefix for a multicast ip v4 57 // address is 1110 for the last 16 bits ::d.d.d.d 58 // We expect these to be false 59 60 addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224 61 addr = InetAddress.getByName(addrName); 62 assertTrue("IPv4 compatable address " + addrName 63 + " reported incorrectly as multicast.", !addr 64 .isMulticastAddress()); 65 66 addrName = "::42.42.42.42"; // an ipv4 non-multicast address 67 addr = InetAddress.getByName(addrName); 68 assertTrue("IPv4 compatable address " + addrName 69 + " reported incorrectly as multicast.", !addr 70 .isMulticastAddress()); 71 72 // IPv4-mapped IPv6 address tests 73 // 74 // Now create 2 IP v6 addresses that are IP v4 compatable 75 // to IP v6 addresses. The address prefix for a multicast ip v4 76 // address is 1110 for the last 16 bits ::FFFF:d.d.d.d 77 78 addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 = 79 // 224 80 addr = InetAddress.getByName(addrName); 81 assertTrue("IPv4-mapped IPv6 multicast address " + addrName 82 + " not detected.", addr.isMulticastAddress()); 83 84 addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address 85 addr = InetAddress.getByName(addrName); 86 assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName 87 + " reporting as a multicast address.", !addr 88 .isMulticastAddress()); 89 } 90 91 public void test_isAnyLocalAddress() throws Exception { 92 93 String addrName = ""; 94 InetAddress addr = null; 95 96 // test to ensure that the unspecified address returns tru 97 addrName = "::0"; // The unspecified address 98 addr = InetAddress.getByName(addrName); 99 assertTrue( 100 "The unspecified (also known as wildcard and any local address) " 101 + addrName + " not detected.", addr 102 .isAnyLocalAddress()); 103 104 addrName = "::"; // another form of the unspecified address 105 addr = InetAddress.getByName(addrName); 106 assertTrue( 107 "The unspecified (also known as wildcard and any local address) " 108 + addrName + " not detected.", addr 109 .isAnyLocalAddress()); 110 111 addrName = "::1"; // The loopback address 112 addr = InetAddress.getByName(addrName); 113 assertTrue("The addresses " + addrName 114 + " incorrectly reporting an the unspecified address.", 115 !addr.isAnyLocalAddress()); 116 } 117 118 public void test_isLoopbackAddress() throws Exception { 119 120 String addrName = ""; 121 // IP V6 regular address tests for loopback 122 // The loopback address for IPv6 is ::1 123 124 addrName = "::1"; 125 InetAddress addr = InetAddress.getByName(addrName); 126 assertTrue("IPv6 loopback address " + addrName + " not detected.", 127 addr.isLoopbackAddress()); 128 129 addrName = "::2"; 130 addr = InetAddress.getByName(addrName); 131 assertTrue("IPv6 address incorrectly " + addrName 132 + " detected as a loopback address.", !addr 133 .isLoopbackAddress()); 134 135 // a loopback address should be 127.d.d.d 136 addrName = "42:42::42:42"; 137 addr = InetAddress.getByName(addrName); 138 assertTrue("IPv6 address incorrectly " + addrName 139 + " detected as a loopback address.", !addr 140 .isLoopbackAddress()); 141 142 // IPv4-compatible IPv6 address tests 143 // 144 // Now create 2 IP v6 addresses that are IP v4 compatable 145 // to IP v6 addresses. The address prefix for a multicast ip v4 146 // address is 1110 for the last 16 bits ::d.d.d.d 147 // We expect these to be false, as they are not IPv4 addresses 148 149 // a loopback address should be 127.d.d.d 150 addrName = "::127.0.0.0"; 151 addr = InetAddress.getByName(addrName); 152 assertTrue("IPv4-compatible IPv6 address " + addrName 153 + " detected incorrectly as a loopback.", !addr 154 .isLoopbackAddress()); 155 156 addrName = "::127.42.42.42"; // a loopback address should be 157 // 127.d.d.d 158 addr = InetAddress.getByName(addrName); 159 assertTrue("IPv4-compatible IPv6 address " + addrName 160 + " detected incorrectly as a loopback.", !addr 161 .isLoopbackAddress()); 162 163 // a loopback address should be 127.d.d.d 164 addrName = "::42.42.42.42"; 165 addr = InetAddress.getByName(addrName); 166 assertTrue("IPv4-compatible IPv6 address " + addrName 167 + " detected incorrectly as a loopback.", !addr 168 .isLoopbackAddress()); 169 170 // IPv4-mapped IPv6 address tests 171 // 172 // Now create 2 IP v6 addresses that are IP v4 compatable 173 // to IP v6 addresses. The address prefix for a multicast ip v4 174 // address is 1110 for the last 16 bits ::FFFF:d.d.d.d 175 176 // a loopback address should be 127.d.d.d 177 addrName = "::FFFF:127.0.0.0"; 178 addr = InetAddress.getByName(addrName); 179 assertTrue("IPv4-compatible IPv6 loopback address " + addrName 180 + " not detected.", addr.isLoopbackAddress()); 181 182 // a loopback address should be 127.d.d.d 183 addrName = "::FFFF:127.42.42.42"; 184 addr = InetAddress.getByName(addrName); 185 assertTrue("IPv4-compatible IPv6 loopback address " + addrName 186 + " not detected.", addr.isLoopbackAddress()); 187 188 // a loopback address should be 127.d.d.d 189 addrName = "::FFFF:42.42.42.42"; 190 addr = InetAddress.getByName(addrName); 191 assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName 192 + " detected as a loopback address.", !addr 193 .isLoopbackAddress()); 194 } 195 196 public void test_isLinkLocalAddress() throws Exception { 197 198 String addrName = ""; 199 // IP V6 regular address tests for link local addresses 200 // 201 // Link local addresses are FE80:: - 202 // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 203 204 addrName = "FE80::0"; 205 InetAddress addr = InetAddress.getByName(addrName); 206 assertTrue( 207 "IPv6 link local address " + addrName + " not detected.", 208 addr.isLinkLocalAddress()); 209 210 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF"; 211 addr = InetAddress.getByName(addrName); 212 assertTrue( 213 "IPv6 link local address " + addrName + " not detected.", 214 addr.isLinkLocalAddress()); 215 216 addrName = "FEC0::1"; 217 addr = InetAddress.getByName(addrName); 218 assertTrue("IPv6 address " + addrName 219 + " detected incorrectly as a link local address.", !addr 220 .isLinkLocalAddress()); 221 222 addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF"; 223 addr = InetAddress.getByName(addrName); 224 assertTrue("IPv6 address " + addrName 225 + " detected incorrectly as a link local address.", !addr 226 .isLinkLocalAddress()); 227 228 addrName = "FE7F::FFFF:FFFF:FFFF:FFFF"; 229 addr = InetAddress.getByName(addrName); 230 assertTrue("IPv6 address " + addrName 231 + " detected incorrectly as a link local address.", !addr 232 .isLinkLocalAddress()); 233 } 234 235 public void test_isSiteLocalAddress() throws Exception { 236 String addrName = ""; 237 238 // IP V6 regular address tests for link local addresses 239 // 240 // Link local addresses are FEC0::0 through to 241 // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 242 243 addrName = "FEC0::0"; 244 InetAddress addr = InetAddress.getByName(addrName); 245 assertTrue( 246 "IPv6 site local address " + addrName + " not detected.", 247 addr.isSiteLocalAddress()); 248 249 addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF"; 250 addr = InetAddress.getByName(addrName); 251 assertTrue( 252 "IPv6 site local address " + addrName + " not detected.", 253 addr.isSiteLocalAddress()); 254 255 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF"; 256 addr = InetAddress.getByName(addrName); 257 assertTrue("IPv6 address " + addrName 258 + " detected incorrectly as a site local address.", !addr 259 .isSiteLocalAddress()); 260 261 addrName = "FFC0::0"; 262 addr = InetAddress.getByName(addrName); 263 assertTrue("IPv6 address " + addrName 264 + " detected incorrectly as a site local address.", !addr 265 .isSiteLocalAddress()); 266 } 267 268 public void test_isMCGlobal() throws Exception { 269 String addrName = ""; 270 // IP V6 regular address tests for Mulitcase Global addresses 271 // 272 // Multicast global addresses are FFxE:/112 where x is 273 // a set of flags, and the addition 112 bits make up 274 // the global address space 275 276 addrName = "FF0E::0"; 277 InetAddress addr = InetAddress.getByName(addrName); 278 assertTrue("IPv6 global mutlicast address " + addrName 279 + " not detected.", addr.isMCGlobal()); 280 281 addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 282 addr = InetAddress.getByName(addrName); 283 assertTrue("IPv6 global multicast address " + addrName 284 + " not detected.", addr.isMCGlobal()); 285 286 // a currently invalid address as the prefix FFxE 287 // is only valid for x = {1,0} as the rest are reserved 288 addrName = "FFFE::0"; 289 addr = InetAddress.getByName(addrName); 290 assertTrue("IPv6 global mutlicast address " + addrName 291 + " not detected.", addr.isMCGlobal()); 292 293 // a currently invalid address as the prefix FFxE 294 // is only valid for x = {1,0} as the rest are reserved 295 addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 296 addr = InetAddress.getByName(addrName); 297 assertTrue("IPv6 global multicast address " + addrName 298 + " not detected.", addr.isMCGlobal()); 299 300 // a sample MC organizational address 301 addrName = "FF08:42:42:42:42:42:42:42"; 302 addr = InetAddress.getByName(addrName); 303 assertTrue("IPv6 mulitcast organizational " + addrName 304 + " incorrectly indicated as a global address.", !addr 305 .isMCGlobal()); 306 307 // a sample MC site address 308 addrName = "FF05:42:42:42:42:42:42:42"; 309 addr = InetAddress.getByName(addrName); 310 assertTrue("IPv6 mulitcast site address " + addrName 311 + " incorrectly indicated as a global address.", !addr 312 .isMCGlobal()); 313 314 // a sample MC link address 315 addrName = "FF02:42:42:42:42:42:42:42"; 316 addr = InetAddress.getByName(addrName); 317 assertTrue("IPv6 mulitcast link address " + addrName 318 + " incorrectly indicated as a global address.", !addr 319 .isMCGlobal()); 320 321 // a sample MC Node 322 addrName = "FF01:42:42:42:42:42:42:42"; 323 addr = InetAddress.getByName(addrName); 324 assertTrue("IPv6 mulitcast node address " + addrName 325 + " incorrectly indicated as a global address.", !addr 326 .isMCGlobal()); 327 328 // IPv4-mapped IPv6 address tests 329 addrName = "::FFFF:224.0.1.0"; 330 addr = InetAddress.getByName(addrName); 331 assertTrue("IPv4 global multicast address " + addrName 332 + " not identified as a global multicast address.", addr 333 .isMCGlobal()); 334 335 addrName = "::FFFF:238.255.255.255"; 336 addr = InetAddress.getByName(addrName); 337 assertTrue("IPv4 global multicast address " + addrName 338 + " not identified as a global multicast address.", addr 339 .isMCGlobal()); 340 } 341 342 public void test_isMCNodeLocal() throws Exception { 343 String addrName = ""; 344 // IP V6 regular address tests for Mulitcase node local addresses 345 // 346 // Multicast node local addresses are FFx1:/112 where x is 347 // a set of flags, and the addition 112 bits make up 348 // the global address space 349 350 addrName = "FF01::0"; 351 InetAddress addr = InetAddress.getByName(addrName); 352 assertTrue("IPv6 node-local mutlicast address " + addrName 353 + " not detected.", addr.isMCNodeLocal()); 354 355 addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 356 addr = InetAddress.getByName(addrName); 357 assertTrue("IPv6 node-local multicast address " + addrName 358 + " not detected.", addr.isMCNodeLocal()); 359 360 // a currently invalid address as the prefix FFxE 361 // is only valid for x = {1,0} as the rest are reserved 362 addrName = "FFF1::0"; 363 addr = InetAddress.getByName(addrName); 364 assertTrue("IPv6 node-local mutlicast address " + addrName 365 + " not detected.", addr.isMCNodeLocal()); 366 367 // a currently invalid address as the prefix FFxE 368 // is only valid for x = {1,0} as the rest are reserved 369 addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 370 addr = InetAddress.getByName(addrName); 371 assertTrue("IPv6 node-local multicast address " + addrName 372 + " not detected.", addr.isMCNodeLocal()); 373 374 // a sample MC organizational address 375 addrName = "FF08:42:42:42:42:42:42:42"; 376 addr = InetAddress.getByName(addrName); 377 assertTrue("IPv6 mulitcast organizational address " + addrName 378 + " incorrectly indicated as a node-local address.", !addr 379 .isMCNodeLocal()); 380 381 // a sample MC site address 382 addrName = "FF05:42:42:42:42:42:42:42"; 383 addr = InetAddress.getByName(addrName); 384 assertTrue("IPv6 mulitcast site address " + addrName 385 + " incorrectly indicated as a node-local address.", !addr 386 .isMCNodeLocal()); 387 388 // a sample MC link address 389 addrName = "FF02:42:42:42:42:42:42:42"; 390 addr = InetAddress.getByName(addrName); 391 assertTrue("IPv6 mulitcast link address " + addrName 392 + " incorrectly indicated as a node-local address.", !addr 393 .isMCNodeLocal()); 394 395 // a sample MC global address 396 addrName = "FF0E:42:42:42:42:42:42:42"; 397 addr = InetAddress.getByName(addrName); 398 assertTrue("IPv6 mulitcast node address " + addrName 399 + " incorrectly indicated as a node-local address.", !addr 400 .isMCNodeLocal()); 401 } 402 403 public void test_isMCLinkLocal() throws Exception { 404 String addrName = ""; 405 // IP V6 regular address tests for Mulitcase link local addresses 406 // 407 // Multicast link local addresses are FFx2:/112 where x is 408 // a set of flags, and the addition 112 bits make up 409 // the global address space 410 411 addrName = "FF02::0"; 412 InetAddress addr = InetAddress.getByName(addrName); 413 assertTrue("IPv6 link local multicast address " + addrName 414 + " not detected.", addr.isMCLinkLocal()); 415 416 addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 417 addr = InetAddress.getByName(addrName); 418 assertTrue("IPv6 link local multicast address " + addrName 419 + " not detected.", addr.isMCLinkLocal()); 420 421 // a currently invalid address as the prefix FFxE 422 // is only valid for x = {1,0} as the rest are reserved 423 addrName = "FFF2::0"; 424 addr = InetAddress.getByName(addrName); 425 assertTrue("IPv6 link local multicast address " + addrName 426 + " not detected.", addr.isMCLinkLocal()); 427 428 // a currently invalid address as the prefix FFxE 429 // is only valid for x = {1,0} as the rest are reserved 430 addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 431 addr = InetAddress.getByName(addrName); 432 assertTrue("IPv6 link local multicast address " + addrName 433 + " not detected.", addr.isMCLinkLocal()); 434 435 // a sample MC organizational address 436 addrName = "FF08:42:42:42:42:42:42:42"; 437 addr = InetAddress.getByName(addrName); 438 assertTrue( 439 "IPv6 organization multicast address " 440 + addrName 441 + " incorrectly indicated as a link-local mulitcast address.", 442 !addr.isMCLinkLocal()); 443 444 // a sample MC site address 445 addrName = "FF05:42:42:42:42:42:42:42"; 446 addr = InetAddress.getByName(addrName); 447 assertTrue( 448 "IPv6 site-local mulitcast address " 449 + addrName 450 + " incorrectly indicated as a link-local mulitcast address.", 451 !addr.isMCLinkLocal()); 452 453 // a sample MC global address 454 addrName = "FF0E:42:42:42:42:42:42:42"; 455 addr = InetAddress.getByName(addrName); 456 assertTrue( 457 "IPv6 global multicast address " 458 + addrName 459 + " incorrectly indicated as a link-local mulitcast address.", 460 !addr.isMCLinkLocal()); 461 462 // a sample MC Node 463 addrName = "FF01:42:42:42:42:42:42:42"; 464 addr = InetAddress.getByName(addrName); 465 assertTrue( 466 "IPv6 mulitcast node address " 467 + addrName 468 + " incorrectly indicated as a link-local mulitcast address.", 469 !addr.isMCLinkLocal()); 470 471 // Ipv4-mapped IPv6 addresses 472 473 addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110 474 addr = InetAddress.getByName(addrName); 475 assertTrue("IPv4 link-local multicast address " + addrName 476 + " not identified as a link-local multicast address.", 477 addr.isMCLinkLocal()); 478 479 addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110 480 addr = InetAddress.getByName(addrName); 481 assertTrue("IPv4 link-local multicast address " + addrName 482 + " not identified as a link-local multicast address.", 483 addr.isMCLinkLocal()); 484 } 485 486 public void test_isMCSiteLocal() throws Exception { 487 String addrName = ""; 488 // IP V6 regular address tests for Multicast site-local addresses 489 // 490 // Multicast global addresses are FFx5:/112 where x is 491 // a set of flags, and the addition 112 bits make up 492 // the global address space 493 494 addrName = "FF05::0"; 495 InetAddress addr = InetAddress.getByName(addrName); 496 assertTrue("IPv6 site-local mutlicast address " + addrName 497 + " not detected.", addr.isMCSiteLocal()); 498 499 addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 500 addr = InetAddress.getByName(addrName); 501 assertTrue("IPv6 site-local multicast address " + addrName 502 + " not detected.", addr.isMCSiteLocal()); 503 504 // a currently invalid address as the prefix FFxE 505 // is only valid for x = {1,0} as the rest are reserved 506 addrName = "FFF5::0"; 507 addr = InetAddress.getByName(addrName); 508 assertTrue("IPv6 site-local mutlicast address " + addrName 509 + " not detected.", addr.isMCSiteLocal()); 510 511 // a currently invalid address as the prefix FFxE 512 // is only valid for x = {1,0} as the rest are reserved 513 addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 514 addr = InetAddress.getByName(addrName); 515 assertTrue("IPv6 site-local multicast address " + addrName 516 + " not detected.", addr.isMCSiteLocal()); 517 518 // a sample MC organizational address 519 addrName = "FF08:42:42:42:42:42:42:42"; 520 addr = InetAddress.getByName(addrName); 521 assertTrue( 522 "IPv6 organization multicast address " 523 + addrName 524 + " incorrectly indicated as a site-local mulitcast address.", 525 !addr.isMCSiteLocal()); 526 527 // a sample MC global address 528 addrName = "FF0E:42:42:42:42:42:42:42"; 529 addr = InetAddress.getByName(addrName); 530 assertTrue( 531 "IPv6 global mulitcast address " 532 + addrName 533 + " incorrectly indicated as a site-local mulitcast address.", 534 !addr.isMCSiteLocal()); 535 536 // a sample MC link address 537 addrName = "FF02:42:42:42:42:42:42:42"; 538 addr = InetAddress.getByName(addrName); 539 assertTrue( 540 "IPv6 link-local multicast address " 541 + addrName 542 + " incorrectly indicated as a site-local mulitcast address.", 543 !addr.isMCSiteLocal()); 544 545 // a sample MC Node 546 addrName = "FF01:42:42:42:42:42:42:42"; 547 addr = InetAddress.getByName(addrName); 548 assertTrue( 549 "IPv6 mulitcast node address " 550 + addrName 551 + " incorrectly indicated as a site-local mulitcast address.", 552 !addr.isMCSiteLocal()); 553 554 // IPv4-mapped IPv6 addresses 555 addrName = "::FFFF:239.255.0.0"; 556 addr = InetAddress.getByName(addrName); 557 assertTrue("IPv4 site-local multicast address " + addrName 558 + " not identified as a site-local multicast address.", 559 addr.isMCSiteLocal()); 560 561 addrName = "::FFFF:239.255.255.255"; 562 addr = InetAddress.getByName(addrName); 563 assertTrue("IPv4 site-local multicast address " + addrName 564 + " not identified as a site-local multicast address.", 565 addr.isMCSiteLocal()); 566 } 567 568 public void test_isMCOrgLocal() throws Exception { 569 String addrName = ""; 570 // IP V6 regular address tests for Mulitcase organization-local 571 // addresses 572 // 573 // Multicast global addresses are FFxE:/112 where x is 574 // a set of flags, and the addition 112 bits make up 575 // the global address space 576 577 addrName = "FF08::0"; 578 InetAddress addr = InetAddress.getByName(addrName); 579 assertTrue("IPv6 organization-local mutlicast address " + addrName 580 + " not detected.", addr.isMCOrgLocal()); 581 582 addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 583 addr = InetAddress.getByName(addrName); 584 assertTrue("IPv6 organization-local multicast address " + addrName 585 + " not detected.", addr.isMCOrgLocal()); 586 587 // a currently invalid address as the prefix FFxE 588 // is only valid for x = {1,0} as the rest are reserved 589 addrName = "FFF8::0"; 590 addr = InetAddress.getByName(addrName); 591 assertTrue("IPv6 organization-local mutlicast address " + addrName 592 + " not detected.", addr.isMCOrgLocal()); 593 594 // a currently invalid address as the prefix FFxE 595 // is only valid for x = {1,0} as the rest are reserved 596 addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 597 addr = InetAddress.getByName(addrName); 598 assertTrue("IPv6 organization-local multicast address " + addrName 599 + " not detected.", addr.isMCOrgLocal()); 600 601 // a sample MC global address 602 addrName = "FF0E:42:42:42:42:42:42:42"; 603 addr = InetAddress.getByName(addrName); 604 assertTrue( 605 "IPv6 global multicast address " 606 + addrName 607 + " incorrectly indicated as an organization-local mulitcast address.", 608 !addr.isMCOrgLocal()); 609 610 // a sample MC site address 611 addrName = "FF05:42:42:42:42:42:42:42"; 612 addr = InetAddress.getByName(addrName); 613 assertTrue( 614 "IPv6 site-local mulitcast address " 615 + addrName 616 + " incorrectly indicated as an organization-local mulitcast address.", 617 !addr.isMCOrgLocal()); 618 619 // a sample MC link address 620 addrName = "FF02:42:42:42:42:42:42:42"; 621 addr = InetAddress.getByName(addrName); 622 assertTrue( 623 "IPv6 link-local multicast address " 624 + addrName 625 + " incorrectly indicated as an organization-local mulitcast address.", 626 !addr.isMCOrgLocal()); 627 628 // a sample MC Node 629 addrName = "FF01:42:42:42:42:42:42:42"; 630 addr = InetAddress.getByName(addrName); 631 assertTrue( 632 "IPv6 mulitcast node address " 633 + addrName 634 + " incorrectly indicated as an organization-local mulitcast address.", 635 !addr.isMCOrgLocal()); 636 637 // IPv4-mapped IPv6 addresses 638 639 addrName = "::FFFF:239.192.0.0"; 640 addr = InetAddress.getByName(addrName); 641 assertTrue("IPv4 org-local multicast address " + addrName 642 + " not identified as a org-local multicast address.", addr 643 .isMCOrgLocal()); 644 645 addrName = "::FFFF:239.195.255.255"; 646 addr = InetAddress.getByName(addrName); 647 assertTrue("IPv4 org-local multicast address " + addrName 648 + " not identified as a org-local multicast address.", addr 649 .isMCOrgLocal()); 650 } 651 652 public void test_isIPv4CompatibleAddress() throws Exception { 653 String addrName = ""; 654 Inet6Address addr = null; 655 656 // Tests a number of addresses to see if they are compatable with 657 // IPv6 addresses 658 659 addrName = "FFFF::42:42"; // 11111111 = FFFF 660 addr = (Inet6Address) InetAddress.getByName(addrName); 661 assertTrue("A non-compatable IPv6 address " + addrName 662 + " incorrectly identified as a IPv4 compatable address.", 663 !addr.isIPv4CompatibleAddress()); 664 665 // IPv4-compatible IPv6 address tests 666 // 667 // Now create 2 IP v6 addresses that are IP v4 compatable 668 // to IP v6 addresses. 669 670 addrName = "::0.0.0.0"; 671 addr = (Inet6Address) InetAddress.getByName(addrName); 672 assertTrue("IPv4 compatable address " + addrName 673 + " not detected correctly.", addr 674 .isIPv4CompatibleAddress()); 675 676 addrName = "::255.255.255.255"; // an ipv4 non-multicast address 677 addr = (Inet6Address) InetAddress.getByName(addrName); 678 assertTrue("IPv4 compatable address " + addrName 679 + " not detected correctly.", addr 680 .isIPv4CompatibleAddress()); 681 } 682 683 public void test_getByNameLjava_lang_String() throws Exception { 684 // ones to add "::255.255.255.255", "::FFFF:0.0.0.0", 685 // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F", 686 // "[F:F:F:F:F:F:F:F]" 687 String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::", 688 "::1", "0", /* jdk1.5 accepts 0 as valid */ 689 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 690 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", 691 "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" }; 692 693 String invalidIPAddresses[] = { "FFFF:FFFF" }; 694 695 for (int i = 0; i < validIPAddresses.length; i++) { 696 697 InetAddress.getByName(validIPAddresses[i]); 698 699 //exercise positive cache 700 InetAddress.getByName(validIPAddresses[i]); 701 702 if (!validIPAddresses[i].equals("0")) { 703 String tempIPAddress = "[" + validIPAddresses[i] + "]"; 704 InetAddress.getByName(tempIPAddress); 705 } 706 } 707 708 for (int i = 0; i < invalidIPAddresses.length; i++) { 709 try { 710 InetAddress.getByName(invalidIPAddresses[i]); 711 fail("Invalid IP address incorrectly recognized as valid: " 712 + invalidIPAddresses[i]); 713 } catch (Exception e) { 714 } 715 716 //exercise negative cache 717 try { 718 InetAddress.getByName(invalidIPAddresses[i]); 719 fail("Invalid IP address incorrectly recognized as valid: " 720 + invalidIPAddresses[i]); 721 } catch (Exception e) { 722 } 723 } 724 } 725 726 public void test_getByAddressLString$BI() throws UnknownHostException{ 727 try { 728 Inet6Address.getByAddress("123", null, 0); 729 fail("should throw UnknownHostException"); 730 } catch (UnknownHostException uhe) { 731 // expected 732 } 733 byte[] addr1 = { (byte) 127, 0, 0, 1 }; 734 try { 735 Inet6Address.getByAddress("123", addr1, 0); 736 fail("should throw UnknownHostException"); 737 } catch (UnknownHostException uhe) { 738 // expected 739 } 740 741 byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 742 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, 743 (byte) 0xB2 }; 744 745 // should not throw any exception 746 Inet6Address.getByAddress("123", addr2, 3); 747 Inet6Address.getByAddress("123", addr2, 0); 748 Inet6Address.getByAddress("123", addr2, -1); 749 } 750 751 public void test_getByAddressLString$BLNetworkInterface() 752 throws UnknownHostException { 753 NetworkInterface nif = null; 754 try { 755 Inet6Address.getByAddress("123", null, nif); 756 fail("should throw UnknownHostException"); 757 } catch (UnknownHostException uhe) { 758 // expected 759 } 760 byte[] addr1 = { (byte) 127, 0, 0, 1 }; 761 try { 762 Inet6Address.getByAddress("123", addr1, nif); 763 fail("should throw UnknownHostException"); 764 } catch (UnknownHostException uhe) { 765 // expected 766 } 767 byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 768 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 769 770 0x7C, (byte) 0xB2 }; 771 // should not throw any exception 772 Inet6Address.getByAddress("123", addr2, nif); 773 } 774 775 public void test_getHostAddress_() throws Exception { 776 byte[] ipAddress = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; 777 InetAddress ia = InetAddress.getByAddress(ipAddress); 778 assertEquals("::1", ia.getHostAddress().toLowerCase(Locale.US)); 779 780 ipAddress = new byte[] {-2, -128, 0, 0, 0, 0, 0, 0, 2, 17, 37, -1, -2, -8, 124, -79}; 781 ia = InetAddress.getByAddress(ipAddress); 782 assertEquals("fe80::211:25ff:fef8:7cb1", ia.getHostAddress().toLowerCase(Locale.US)); 783 } 784 785 public void test_getScopeID() throws UnknownHostException { 786 Inet6Address v6ia; 787 byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11, 788 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, 789 (byte) 0xB2 }; 790 791 v6ia = Inet6Address.getByAddress("123", addr, 3); 792 assertEquals(3, v6ia.getScopeId()); 793 794 v6ia = Inet6Address.getByAddress("123", addr, 0); 795 assertEquals(0, v6ia.getScopeId()); 796 797 v6ia = Inet6Address.getByAddress("123", addr, -1); 798 assertEquals(0, v6ia.getScopeId()); 799 } 800 801 public void test_getScopedInterface() throws UnknownHostException { 802 byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5, 803 (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09, 804 (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 }; 805 Inet6Address v6Addr; 806 v6Addr = Inet6Address.getByAddress("123", addr, null); 807 assertNull(v6Addr.getScopedInterface()); 808 } 809 810 public void test_hashCode() throws UnknownHostException{ 811 byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11, 812 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, 813 (byte) 0xB2 }; 814 Inet6Address address1, address2; 815 816 address1 = Inet6Address.getByAddress("123", addr, 0); 817 address2 = Inet6Address.getByAddress("1234", addr, 0); 818 assertEquals(address1.hashCode(), address2.hashCode()); 819 } 820 821 int bytesToInt(byte bytes[], int start) { 822 823 int byteMask = 255; 824 int value = ((bytes[start + 3] & byteMask)) 825 | ((bytes[start + 2] & byteMask) << 8) 826 | ((bytes[start + 1] & byteMask) << 16) 827 | ((bytes[start] & byteMask) << 24); 828 return value; 829 830 } 831 832 String byteArrayToHexString(byte bytes[], boolean leadingZeros) { 833 834 String fullString = ""; 835 int times = bytes.length / 4; 836 int intArray[] = new int[times]; 837 for (int i = 0; i < times; i++) { 838 intArray[i] = bytesToInt(bytes, i * 4); 839 } 840 841 return intArrayToHexString(intArray, leadingZeros); 842 } 843 844 void intToBytes(int value, byte bytes[], int start) { 845 846 int byteMask = 255; 847 bytes[start + 3] = (byte) (value & byteMask); 848 bytes[start + 2] = (byte) ((value >> 8) & byteMask); 849 bytes[start + 1] = (byte) ((value >> 16) & byteMask); 850 bytes[start] = (byte) ((value >> 24) & byteMask); 851 } 852 853 String intArrayToHexString(int ints[], boolean leadingZeros) { 854 855 String fullString = ""; 856 String tempString; 857 int intsLength = ints.length; 858 for (int i = 0; i < intsLength; i++) { 859 tempString = Integer.toHexString(ints[i]); 860 while (tempString.length() < 4 && leadingZeros) { 861 tempString = "0" + tempString; 862 } 863 if (i + 1 < intsLength) { 864 tempString += ":"; 865 } 866 fullString += tempString; 867 } 868 869 return fullString.toUpperCase(); 870 } 871 872 // comparator for Inet6Address objects 873 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 874 public void assertDeserialized(Serializable initial, 875 Serializable deserialized) { 876 877 Inet6Address initAddr = (Inet6Address) initial; 878 Inet6Address desrAddr = (Inet6Address) deserialized; 879 880 byte[] iaAddresss = initAddr.getAddress(); 881 byte[] deIAAddresss = desrAddr.getAddress(); 882 for (int i = 0; i < iaAddresss.length; i++) { 883 assertEquals(iaAddresss[i], deIAAddresss[i]); 884 } 885 assertEquals(initAddr.getScopeId(), desrAddr.getScopeId()); 886 assertEquals(initAddr.getScopedInterface(), desrAddr 887 .getScopedInterface()); 888 } 889 }; 890 891 /** 892 * Tests serialization/deserialization compatibility with ourselves. 893 */ 894 public void testSerializationSelf() throws Exception { 895 896 byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 897 898 SerializationTest.verifySelf(InetAddress.getByAddress(localv6), 899 COMPARATOR); 900 } 901 902 /** 903 * Tests serialization/deserialization compatibility with RI. 904 */ 905 public void testSerializationCompatibility() throws Exception { 906 907 byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 908 909 Object[] addresses = { InetAddress.getByAddress(localv6), 910 // Regression for Harmony-1039: ser-form has 911 // null interface name 912 InetAddress.getByAddress(localv6) }; 913 914 SerializationTest.verifyGolden(this, addresses, COMPARATOR); 915 } 916} 917