InetAddressTest.java revision 111aeba7174a62829dc25155f53133691768ec44
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.BrokenTest; 21import dalvik.annotation.TestTargetClass; 22import dalvik.annotation.TestTargets; 23import dalvik.annotation.TestLevel; 24import dalvik.annotation.TestTargetNew; 25 26import java.io.IOException; 27import java.io.Serializable; 28import java.net.DatagramSocket; 29import java.net.Inet4Address; 30import java.net.Inet6Address; 31import java.net.InetAddress; 32import java.net.NetworkInterface; 33import java.net.UnknownHostException; 34import java.security.Permission; 35import java.util.ArrayList; 36import java.util.Enumeration; 37 38import org.apache.harmony.testframework.serialization.SerializationTest; 39import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 40 41import tests.support.Support_Configuration; 42 43@TestTargetClass(InetAddress.class) 44public class InetAddressTest extends junit.framework.TestCase { 45 46 private static boolean someoneDone[] = new boolean[2]; 47 48 protected static boolean threadedTestSucceeded; 49 50 protected static String threadedTestErrorString; 51 52 /** 53 * This class is used to test inet_ntoa, gethostbyaddr and gethostbyname 54 * functions in the VM to make sure they're threadsafe. getByName will cause 55 * the gethostbyname function to be called. getHostName will cause the 56 * gethostbyaddr to be called. getHostAddress will cause inet_ntoa to be 57 * called. 58 */ 59 static class threadsafeTestThread extends Thread { 60 private String lookupName; 61 62 private InetAddress testAddress; 63 64 private int testType; 65 66 /* 67 * REP_NUM can be adjusted if desired. Since this error is 68 * non-deterministic it may not always occur. Setting REP_NUM higher, 69 * increases the chances of an error being detected, but causes the test 70 * to take longer. Because the Java threads spend a lot of time 71 * performing operations other than running the native code that may not 72 * be threadsafe, it is quite likely that several thousand iterations 73 * will elapse before the first error is detected. 74 */ 75 private static final int REP_NUM = 20000; 76 77 public threadsafeTestThread(String name, String lookupName, 78 InetAddress testAddress, int type) { 79 super(name); 80 this.lookupName = lookupName; 81 this.testAddress = testAddress; 82 testType = type; 83 } 84 85 public void run() { 86 try { 87 String correctName = testAddress.getHostName(); 88 String correctAddress = testAddress.getHostAddress(); 89 long startTime = System.currentTimeMillis(); 90 91 synchronized (someoneDone) { 92 } 93 94 for (int i = 0; i < REP_NUM; i++) { 95 if (someoneDone[testType]) { 96 break; 97 } else if ((i % 25) == 0 98 && System.currentTimeMillis() - startTime > 240000) { 99 System.out 100 .println("Exiting due to time limitation after " 101 + i + " iterations"); 102 break; 103 } 104 105 InetAddress ia = InetAddress.getByName(lookupName); 106 String hostName = ia.getHostName(); 107 String hostAddress = ia.getHostAddress(); 108 109 // Intentionally not looking for exact name match so that 110 // the test works across different platforms that may or 111 // may not include a domain suffix on the hostname 112 if (!hostName.startsWith(correctName)) { 113 threadedTestSucceeded = false; 114 threadedTestErrorString = (testType == 0 ? "gethostbyname" 115 : "gethostbyaddr") 116 + ": getHostName() returned " 117 + hostName 118 + " instead of " + correctName; 119 break; 120 } 121 // IP addresses should match exactly 122 if (!correctAddress.equals(hostAddress)) { 123 threadedTestSucceeded = false; 124 threadedTestErrorString = (testType == 0 ? "gethostbyname" 125 : "gethostbyaddr") 126 + ": getHostName() returned " 127 + hostAddress 128 + " instead of " + correctAddress; 129 break; 130 } 131 132 } 133 someoneDone[testType] = true; 134 } catch (Exception e) { 135 threadedTestSucceeded = false; 136 threadedTestErrorString = e.toString(); 137 } 138 } 139 } 140 141 /** 142 * @tests java.net.InetAddress#equals(java.lang.Object) 143 */ 144 @TestTargetNew( 145 level = TestLevel.COMPLETE, 146 notes = "", 147 method = "equals", 148 args = {java.lang.Object.class} 149 ) 150 public void test_equalsLjava_lang_Object() { 151 // Test for method boolean java.net.InetAddress.equals(java.lang.Object) 152 try { 153 InetAddress ia1 = InetAddress 154 .getByName(Support_Configuration.InetTestAddress); 155 InetAddress ia2 = InetAddress 156 .getByName(Support_Configuration.InetTestIP); 157 assertTrue("Equals returned incorrect result - " + ia1 + " != " 158 + ia2, ia1.equals(ia2)); 159 } catch (Exception e) { 160 fail("Exception during equals test : " + e.getMessage()); 161 } 162 } 163 164 /** 165 * @tests java.net.InetAddress#getAddress() 166 */ 167 @TestTargetNew( 168 level = TestLevel.COMPLETE, 169 notes = "", 170 method = "getAddress", 171 args = {} 172 ) 173 public void test_getAddress() { 174 // Test for method byte [] java.net.InetAddress.getAddress() 175 try { 176 InetAddress ia = InetAddress 177 .getByName(Support_Configuration.InetTestIP); 178 // BEGIN android-changed 179 // using different address. The old one was { 9, 26, -56, -111 } 180 // this lead to a crash, also in RI. 181 byte[] caddr = Support_Configuration.InetTestAddr; 182 // END android-changed 183 byte[] addr = ia.getAddress(); 184 for (int i = 0; i < addr.length; i++) 185 assertTrue("Incorrect address returned", caddr[i] == addr[i]); 186 } catch (java.net.UnknownHostException e) { 187 } 188 } 189 190 /** 191 * @tests java.net.InetAddress#getAllByName(java.lang.String) 192 */ 193 @TestTargetNew( 194 level = TestLevel.COMPLETE, 195 notes = "", 196 method = "getAllByName", 197 args = {java.lang.String.class} 198 ) 199 public void test_getAllByNameLjava_lang_String() throws Exception { 200 // Test for method java.net.InetAddress [] 201 // java.net.InetAddress.getAllByName(java.lang.String) 202 InetAddress[] all = InetAddress 203 .getAllByName(Support_Configuration.SpecialInetTestAddress); 204 assertNotNull(all); 205 // Number of aliases depends on individual test machine 206 assertTrue(all.length >= 1); 207 for (InetAddress alias : all) { 208 // Check that each alias has the same hostname. Intentionally not 209 // checking for exact string match. 210 assertTrue(alias.getHostName().startsWith( 211 Support_Configuration.SpecialInetTestAddress)); 212 }// end for all aliases 213 214 // check the getByName if there is a security manager. 215 SecurityManager oldman = System.getSecurityManager(); 216 System.setSecurityManager(new MockSecurityManager()); 217 try { 218 boolean exception = false; 219 try { 220 InetAddress.getByName("3d.com"); 221 } catch (SecurityException ex) { 222 exception = true; 223 } catch (Exception ex) { 224 fail("getByName threw wrong exception : " + ex.getMessage()); 225 } 226 assertTrue("expected SecurityException", exception); 227 } finally { 228 System.setSecurityManager(oldman); 229 } 230 231 //Regression for HARMONY-56 232 InetAddress[] ia = InetAddress.getAllByName(null); 233 assertEquals("Assert 0: No loopback address", 1, ia.length); 234 assertTrue("Assert 1: getAllByName(null) not loopback", 235 ia[0].isLoopbackAddress()); 236 237 try { 238 InetAddress.getAllByName("unknown.host"); 239 fail("UnknownHostException was not thrown."); 240 } catch(UnknownHostException uhe) { 241 //expected 242 } 243 } 244 245 /** 246 * @tests java.net.InetAddress#getByName(java.lang.String) 247 */ 248 @TestTargetNew( 249 level = TestLevel.PARTIAL_COMPLETE, 250 notes = "", 251 method = "getByName", 252 args = {java.lang.String.class} 253 ) 254 public void test_getByNameLjava_lang_String() throws Exception { 255 // Test for method java.net.InetAddress 256 // java.net.InetAddress.getByName(java.lang.String) 257 InetAddress ia2 = InetAddress 258 .getByName(Support_Configuration.InetTestIP); 259 260 // Intentionally not testing for exact string match 261 /* FIXME: comment the assertion below because it is platform/configuration dependent 262 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) 263 * for details 264 */ 265// assertTrue( 266// "Expected " + Support_Configuration.InetTestAddress + "*", 267// ia2.getHostName().startsWith(Support_Configuration.InetTestAddress)); 268 269 // TODO : Test to ensure all the address formats are recognized 270 InetAddress i = InetAddress.getByName("1.2.3"); 271 assertEquals("1.2.0.3",i.getHostAddress()); 272 i = InetAddress.getByName("1.2"); 273 assertEquals("1.0.0.2",i.getHostAddress()); 274 i = InetAddress.getByName(String.valueOf(0xffffffffL)); 275 assertEquals("255.255.255.255",i.getHostAddress()); 276 // BEGIN android-removed 277 // This test checks a bug in the RI that allows any number of '.' after 278 // a valid ipv4 address. This bug doesn't exist in this implementation. 279 // String s = "222.222.222.222...."; 280 // i = InetAddress.getByName(s); 281 // assertEquals("222.222.222.222",i.getHostAddress()); 282 // END android-removed 283 284 class MockSecurityManager extends SecurityManager { 285 public void checkPermission(Permission permission) { 286 if (permission.getName().equals("setSecurityManager")){ 287 return; 288 } 289 if (permission.getName().equals("3d.com")){ 290 throw new SecurityException(); 291 } 292 super.checkPermission(permission); 293 } 294 295 public void checkConnect(String host, int port) { 296 if(host.equals("google.com")) { 297 throw new SecurityException(); 298 } 299 } 300 } 301 SecurityManager oldman = System.getSecurityManager(); 302 System.setSecurityManager(new MockSecurityManager()); 303 try { 304 boolean exception = false; 305 try { 306 InetAddress.getByName("google.com"); 307 fail("SecurityException was not thrown."); 308 } catch (SecurityException ex) { 309 //expected 310 } catch (Exception ex) { 311 fail("getByName threw wrong exception : " + ex.getMessage()); 312 } 313 } finally { 314 System.setSecurityManager(oldman); 315 } 316 317 try { 318 InetAddress.getByName("0.0.0.0.0"); 319 fail("UnknownHostException was not thrown."); 320 } catch(UnknownHostException ue) { 321 //expected 322 } 323 } 324 325 /** 326 * @tests java.net.InetAddress#getHostAddress() 327 */ 328 @TestTargetNew( 329 level = TestLevel.COMPLETE, 330 notes = "", 331 method = "getHostAddress", 332 args = {} 333 ) 334 public void test_getHostAddress() { 335 // Test for method java.lang.String 336 // java.net.InetAddress.getHostAddress() 337 try { 338 InetAddress ia2 = InetAddress 339 .getByName(Support_Configuration.InetTestAddress); 340 assertTrue("getHostAddress returned incorrect result: " 341 + ia2.getHostAddress() + " != " 342 + Support_Configuration.InetTestIP, ia2.getHostAddress() 343 .equals(Support_Configuration.InetTestIP)); 344 } catch (Exception e) { 345 fail("Exception during getHostAddress test : " + e.getMessage()); 346 } 347 } 348 349 /** 350 * @tests java.net.InetAddress#getHostName() 351 */ 352 @TestTargetNew( 353 level = TestLevel.COMPLETE, 354 notes = "", 355 method = "getHostName", 356 args = {} 357 ) 358 public void test_getHostName() throws Exception { 359 // Test for method java.lang.String java.net.InetAddress.getHostName() 360 InetAddress ia = InetAddress 361 .getByName(Support_Configuration.InetTestIP); 362 363 // Intentionally not testing for exact string match 364 /* FIXME: comment the assertion below because it is platform/configuration dependent 365 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) 366 * for details 367 */ 368// assertTrue( 369// "Expected " + Support_Configuration.InetTestAddress + "*", 370// ia.getHostName().startsWith(Support_Configuration.InetTestAddress)); 371 372 // Test for any of the host lookups, where the default SecurityManager 373 // is installed. 374 375 SecurityManager oldman = System.getSecurityManager(); 376 try { 377 String exp = Support_Configuration.InetTestIP; 378 System.setSecurityManager(new MockSecurityManager()); 379 ia = InetAddress.getByName(exp); 380 String ans = ia.getHostName(); 381 /* FIXME: comment the assertion below because it is platform/configuration dependent 382 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) 383 * for details 384 */ 385 // assertEquals(Support_Configuration.InetTestIP, ans); 386 } finally { 387 System.setSecurityManager(oldman); 388 } 389 390 // Make sure there is no caching 391 String originalPropertyValue = System 392 .getProperty("networkaddress.cache.ttl"); 393 System.setProperty("networkaddress.cache.ttl", "0"); 394 395 // Test for threadsafety 396 try { 397 InetAddress lookup1 = InetAddress 398 .getByName(Support_Configuration.InetTestAddress); 399 assertTrue(lookup1 + " expected " 400 + Support_Configuration.InetTestIP, 401 Support_Configuration.InetTestIP.equals(lookup1 402 .getHostAddress())); 403 InetAddress lookup2 = InetAddress 404 .getByName(Support_Configuration.InetTestAddress2); 405 assertTrue(lookup2 + " expected " 406 + Support_Configuration.InetTestIP2, 407 Support_Configuration.InetTestIP2.equals(lookup2 408 .getHostAddress())); 409 threadsafeTestThread thread1 = new threadsafeTestThread("1", 410 lookup1.getHostName(), lookup1, 0); 411 threadsafeTestThread thread2 = new threadsafeTestThread("2", 412 lookup2.getHostName(), lookup2, 0); 413 threadsafeTestThread thread3 = new threadsafeTestThread("3", 414 lookup1.getHostAddress(), lookup1, 1); 415 threadsafeTestThread thread4 = new threadsafeTestThread("4", 416 lookup2.getHostAddress(), lookup2, 1); 417 418 // initialize the flags 419 threadedTestSucceeded = true; 420 synchronized (someoneDone) { 421 thread1.start(); 422 thread2.start(); 423 thread3.start(); 424 thread4.start(); 425 } 426 thread1.join(); 427 thread2.join(); 428 thread3.join(); 429 thread4.join(); 430 /* FIXME: comment the assertion below because it is platform/configuration dependent 431 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) 432 * for details 433 */ 434// assertTrue(threadedTestErrorString, threadedTestSucceeded); 435 } finally { 436 // restore the old value of the property 437 if (originalPropertyValue == null) 438 // setting the property to -1 has the same effect as having the 439 // property be null 440 System.setProperty("networkaddress.cache.ttl", "-1"); 441 else 442 System.setProperty("networkaddress.cache.ttl", 443 originalPropertyValue); 444 } 445 } 446 447 /** 448 * @tests java.net.InetAddress#getLocalHost() 449 */ 450 @TestTargetNew( 451 level = TestLevel.SUFFICIENT, 452 notes = "UnknownHostException should be thrown if no IP address for the host could be found.", 453 method = "getLocalHost", 454 args = {} 455 ) 456 public void test_getLocalHost() { 457 // Test for method java.net.InetAddress 458 // java.net.InetAddress.getLocalHost() 459 try { 460 // We don't know the host name or ip of the machine 461 // running the test, so we can't build our own address 462 DatagramSocket dg = new DatagramSocket(0, InetAddress 463 .getLocalHost()); 464 assertTrue("Incorrect host returned", InetAddress.getLocalHost() 465 .equals(dg.getLocalAddress())); 466 dg.close(); 467 } catch (Exception e) { 468 fail("Exception during getLocalHost test : " + e.getMessage()); 469 } 470 } 471 472 /** 473 * @tests java.net.InetAddress#hashCode() 474 */ 475 @TestTargetNew( 476 level = TestLevel.COMPLETE, 477 notes = "", 478 method = "hashCode", 479 args = {} 480 ) 481 public void test_hashCode() { 482 // Test for method int java.net.InetAddress.hashCode() 483 try { 484 InetAddress host = InetAddress 485 .getByName(Support_Configuration.InetTestAddress); 486 int hashcode = host.hashCode(); 487 assertTrue("Incorrect hash returned: " + hashcode + " from host: " 488 + host, hashcode == Support_Configuration.InetTestHashcode); 489 } catch (java.net.UnknownHostException e) { 490 fail("Exception during test : " + e.getMessage()); 491 } 492 } 493 494 /** 495 * @tests java.net.InetAddress#isMulticastAddress() 496 */ 497 @TestTargetNew( 498 level = TestLevel.COMPLETE, 499 notes = "", 500 method = "isMulticastAddress", 501 args = {} 502 ) 503 public void test_isMulticastAddress() { 504 // Test for method boolean java.net.InetAddress.isMulticastAddress() 505 try { 506 InetAddress ia2 = InetAddress.getByName("239.255.255.255"); 507 assertTrue("isMulticastAddress returned incorrect result", ia2 508 .isMulticastAddress()); 509 } catch (Exception e) { 510 fail("Exception during isMulticastAddress test : " + e.getMessage()); 511 } 512 } 513 514 /** 515 * @tests java.net.InetAddress#toString() 516 */ 517 @TestTargetNew( 518 level = TestLevel.COMPLETE, 519 notes = "", 520 method = "toString", 521 args = {} 522 ) 523 public void test_toString() throws Exception { 524 // Test for method java.lang.String java.net.InetAddress.toString() 525 InetAddress ia2 = InetAddress 526 .getByName(Support_Configuration.InetTestIP); 527 assertEquals("/" + Support_Configuration.InetTestIP, ia2.toString()); 528 // Regression for HARMONY-84 529 InetAddress addr = InetAddress.getByName("localhost"); 530 assertEquals("Assert 0: wrong string from name", "localhost/127.0.0.1", addr.toString()); 531 InetAddress addr2 = InetAddress.getByAddress(new byte[]{127, 0, 0, 1}); 532 assertEquals("Assert 1: wrong string from address", "/127.0.0.1", addr2.toString()); 533 } 534 535 /** 536 * @tests java.net.InetAddress#getByAddress(java.lang.String, byte[]) 537 */ 538 @TestTargetNew( 539 level = TestLevel.COMPLETE, 540 notes = "", 541 method = "getByAddress", 542 args = {java.lang.String.class, byte[].class} 543 ) 544 public void test_getByAddressLjava_lang_String$B() { 545 // Check an IPv4 address with an IPv6 hostname 546 byte ipAddress[] = { 127, 0, 0, 1 }; 547 String addressStr = "::1"; 548 try { 549 InetAddress addr = InetAddress.getByAddress(addressStr, ipAddress); 550 addr = InetAddress.getByAddress(ipAddress); 551 } catch (UnknownHostException e) { 552 fail("Unexpected problem creating IP Address " 553 + ipAddress.length); 554 } 555 556 byte ipAddress2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 127, 0, 0, 557 1 }; 558 addressStr = "::1"; 559 try { 560 InetAddress addr = InetAddress.getByAddress(addressStr, ipAddress2); 561 addr = InetAddress.getByAddress(ipAddress); 562 } catch (UnknownHostException e) { 563 fail("Unexpected problem creating IP Address " 564 + ipAddress.length); 565 } 566 567 try { 568 InetAddress addr = InetAddress.getByAddress(addressStr, 569 new byte [] {0, 0, 0, 0, 0}); 570 fail("UnknownHostException was thrown."); 571 } catch(UnknownHostException uhe) { 572 //expected 573 } 574 } 575 576 /** 577 * @tests java.net.InetAddress#getCanonicalHostName() 578 */ 579 @TestTargetNew( 580 level = TestLevel.COMPLETE, 581 notes = "", 582 method = "getCanonicalHostName", 583 args = {} 584 ) 585 public void test_getCanonicalHostName() throws Exception { 586 InetAddress theAddress = null; 587 theAddress = InetAddress.getLocalHost(); 588 assertTrue("getCanonicalHostName returned a zero length string ", 589 theAddress.getCanonicalHostName().length() != 0); 590 assertTrue("getCanonicalHostName returned an empty string ", 591 !theAddress.equals("")); 592 593 // test against an expected value 594 InetAddress ia = InetAddress 595 .getByName(Support_Configuration.InetTestIP); 596 597 // Intentionally not testing for exact string match 598 /* FIXME: comment the assertion below because it is platform/configuration dependent 599 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664) 600 * for details 601 */ 602// assertTrue( 603// "Expected " + Support_Configuration.InetTestAddress + "*", 604// ia.getCanonicalHostName().startsWith(Support_Configuration.InetTestAddress)); 605 } 606 607 /** 608 * @tests java.net.InetAddress#isReachableI 609 */ 610 @TestTargetNew( 611 level = TestLevel.SUFFICIENT, 612 notes = "IOException checking missed (if network error occurs).", 613 method = "isReachable", 614 args = {int.class} 615 ) 616 public void test_isReachableI() throws Exception { 617 InetAddress ia = Inet4Address.getByName("127.0.0.1"); 618 assertTrue(ia.isReachable(10000)); 619 ia = Inet4Address.getByName("127.0.0.1"); 620 try { 621 ia.isReachable(-1); 622 fail("Should throw IllegalArgumentException"); 623 } catch (IllegalArgumentException e) { 624 // correct 625 } 626 } 627 628 /** 629 * @tests java.net.InetAddress#isReachableLjava_net_NetworkInterfaceII 630 */ 631 @TestTargetNew( 632 level = TestLevel.SUFFICIENT, 633 notes = "IOException checking missed (if network error occurs).", 634 method = "isReachable", 635 args = {java.net.NetworkInterface.class, int.class, int.class} 636 ) 637 @BrokenTest("Depends on external network address and shows different" + 638 "behavior with WLAN and 3G networks") 639 public void test_isReachableLjava_net_NetworkInterfaceII() throws Exception { 640 // tests local address 641 InetAddress ia = Inet4Address.getByName("127.0.0.1"); 642 assertTrue(ia.isReachable(null, 0, 10000)); 643 ia = Inet4Address.getByName("127.0.0.1"); 644 try { 645 ia.isReachable(null, -1, 10000); 646 fail("Should throw IllegalArgumentException"); 647 } catch (IllegalArgumentException e) { 648 // correct 649 } 650 try { 651 ia.isReachable(null, 0, -1); 652 fail("Should throw IllegalArgumentException"); 653 } catch (IllegalArgumentException e) { 654 // correct 655 } 656 try { 657 ia.isReachable(null, -1, -1); 658 fail("Should throw IllegalArgumentException"); 659 } catch (IllegalArgumentException e) { 660 // correct 661 } 662 // tests nowhere 663 ia = Inet4Address.getByName("1.1.1.1"); 664 assertFalse(ia.isReachable(1000)); 665 assertFalse(ia.isReachable(null, 0, 1000)); 666 667 // Regression test for HARMONY-1842. 668 ia = InetAddress.getByName("localhost"); //$NON-NLS-1$ 669 Enumeration<NetworkInterface> nif = NetworkInterface.getNetworkInterfaces(); 670 NetworkInterface netif; 671 while(nif.hasMoreElements()) { 672 netif = nif.nextElement(); 673 ia.isReachable(netif, 10, 1000); 674 } 675 } 676 677 // comparator for InetAddress objects 678 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 679 public void assertDeserialized(Serializable initial, 680 Serializable deserialized) { 681 682 InetAddress initAddr = (InetAddress) initial; 683 InetAddress desrAddr = (InetAddress) deserialized; 684 685 byte[] iaAddresss = initAddr.getAddress(); 686 byte[] deIAAddresss = desrAddr.getAddress(); 687 for (int i = 0; i < iaAddresss.length; i++) { 688 assertEquals(iaAddresss[i], deIAAddresss[i]); 689 } 690 assertEquals(initAddr.getHostName(), desrAddr.getHostName()); 691 } 692 }; 693 694 // Regression Test for Harmony-2290 695 @TestTargetNew( 696 level = TestLevel.ADDITIONAL, 697 notes = "Regeression test. Functional test.", 698 method = "isReachable", 699 args = {java.net.NetworkInterface.class, int.class, int.class} 700 ) 701 public void test_isReachableLjava_net_NetworkInterfaceII_loopbackInterface() throws IOException { 702 final int TTL = 20; 703 final int TIME_OUT = 3000; 704 705 NetworkInterface loopbackInterface = null; 706 ArrayList<InetAddress> localAddresses = new ArrayList<InetAddress>(); 707 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface 708 .getNetworkInterfaces(); 709 while (networkInterfaces.hasMoreElements()) { 710 NetworkInterface networkInterface = networkInterfaces.nextElement(); 711 Enumeration<InetAddress> addresses = networkInterface 712 .getInetAddresses(); 713 while (addresses.hasMoreElements()) { 714 InetAddress address = addresses.nextElement(); 715 if (address.isLoopbackAddress()) { 716 loopbackInterface = networkInterface; 717 } else { 718 localAddresses.add(address); 719 } 720 } 721 } 722 723 //loopbackInterface can reach local address 724 if (null != loopbackInterface) { 725 for (InetAddress destAddress : localAddresses) { 726 assertTrue(destAddress.isReachable(loopbackInterface, TTL, TIME_OUT)); 727 } 728 } 729 730 //loopback Interface cannot reach outside address 731 InetAddress destAddress = InetAddress.getByName("www.google.com"); 732 assertFalse(destAddress.isReachable(loopbackInterface, TTL, TIME_OUT)); 733 } 734 735 /** 736 * @tests serialization/deserialization compatibility. 737 */ 738 @TestTargetNew( 739 level = TestLevel.COMPLETE, 740 notes = "Checks serialization.", 741 method = "!SerializationSelf", 742 args = {} 743 ) 744 public void testSerializationSelf() throws Exception { 745 746 SerializationTest.verifySelf(InetAddress.getByName("localhost"), 747 COMPARATOR); 748 } 749 750 /** 751 * @tests serialization/deserialization compatibility with RI. 752 */ 753 @TestTargetNew( 754 level = TestLevel.COMPLETE, 755 notes = "Checks serialization.", 756 method = "!SerializationGolden", 757 args = {} 758 ) 759 public void testSerializationCompatibility() throws Exception { 760 761 SerializationTest.verifyGolden(this, 762 InetAddress.getByName("localhost"), COMPARATOR); 763 } 764 765 /** 766 * @tests java.net.InetAddress#getByAddress(byte[]) 767 */ 768 @TestTargetNew( 769 level = TestLevel.COMPLETE, 770 notes = "", 771 method = "getByAddress", 772 args = {byte[].class} 773 ) 774 public void test_getByAddress() { 775 byte ipAddress[] = { 127, 0, 0, 1 }; 776 try { 777 InetAddress.getByAddress(ipAddress); 778 } catch (UnknownHostException e) { 779 fail("Unexpected problem creating IP Address " 780 + ipAddress.length); 781 } 782 783 byte ipAddress2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 127, 0, 0, 784 1 }; 785 try { 786 InetAddress.getByAddress(ipAddress2); 787 } catch (UnknownHostException e) { 788 fail("Unexpected problem creating IP Address " 789 + ipAddress.length); 790 } 791 792 // Regression for HARMONY-61 793 try { 794 InetAddress.getByAddress(null); 795 fail("Assert 0: UnknownHostException must be thrown"); 796 } catch (UnknownHostException e) { 797 // Expected 798 } 799 800 try { 801 byte [] byteArray = new byte[] {}; 802 InetAddress.getByAddress(byteArray); 803 fail("Assert 1: UnknownHostException must be thrown"); 804 } catch (UnknownHostException e) { 805 // Expected 806 } 807 } 808 809 @TestTargetNew( 810 level = TestLevel.COMPLETE, 811 notes = "", 812 method = "isAnyLocalAddress", 813 args = {} 814 ) 815 public void test_isAnyLocalAddress() throws Exception { 816 byte [] ipAddress1 = { 127, 42, 42, 42 }; 817 InetAddress ia1 = InetAddress.getByAddress(ipAddress1); 818 assertFalse(ia1.isAnyLocalAddress()); 819 820 byte [] ipAddress2 = { 0, 0, 0, 0 }; 821 InetAddress ia2 = InetAddress.getByAddress(ipAddress2); 822 assertTrue(ia2.isAnyLocalAddress()); 823 } 824 825 @TestTargetNew( 826 level = TestLevel.COMPLETE, 827 notes = "", 828 method = "isLinkLocalAddress", 829 args = {} 830 ) 831 public void test_isLinkLocalAddress() throws Exception { 832 String addrName = "FE80::0"; 833 InetAddress addr = InetAddress.getByName(addrName); 834 assertTrue( 835 "IPv6 link local address " + addrName + " not detected.", 836 addr.isLinkLocalAddress()); 837 838 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF"; 839 addr = Inet6Address.getByName(addrName); 840 assertTrue( 841 "IPv6 link local address " + addrName + " not detected.", 842 addr.isLinkLocalAddress()); 843 844 addrName = "FEC0::1"; 845 addr = Inet6Address.getByName(addrName); 846 assertTrue("IPv6 address " + addrName 847 + " detected incorrectly as a link local address.", !addr 848 .isLinkLocalAddress()); 849 850 addrName = "42.42.42.42"; 851 addr = Inet4Address.getByName(addrName); 852 assertTrue("IPv4 address " + addrName 853 + " incorrectly reporting as a link local address.", !addr 854 .isLinkLocalAddress()); 855 } 856 857 @TestTargetNew( 858 level = TestLevel.COMPLETE, 859 notes = "", 860 method = "isLoopbackAddress", 861 args = {} 862 ) 863 public void test_isLoopbackAddress() throws Exception { 864 String addrName = "127.0.0.0"; 865 InetAddress addr = InetAddress.getByName(addrName); 866 assertTrue("Loopback address " + addrName + " not detected.", addr 867 .isLoopbackAddress()); 868 869 addrName = "127.42.42.42"; 870 addr = InetAddress.getByName(addrName); 871 assertTrue("Loopback address " + addrName + " not detected.", addr 872 .isLoopbackAddress()); 873 874 addrName = "42.42.42.42"; 875 addr = Inet4Address.getByName(addrName); 876 assertTrue("Address incorrectly " + addrName 877 + " detected as a loopback address.", !addr 878 .isLoopbackAddress()); 879 880 881 addrName = "::FFFF:127.42.42.42"; 882 addr = InetAddress.getByName(addrName); 883 assertTrue("IPv4-compatible IPv6 loopback address " + addrName 884 + " not detected.", addr.isLoopbackAddress()); 885 886 addrName = "::FFFF:42.42.42.42"; 887 addr = InetAddress.getByName(addrName); 888 assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName 889 + " detected as a loopback address.", !addr 890 .isLoopbackAddress()); 891 } 892 893 @TestTargetNew( 894 level = TestLevel.COMPLETE, 895 notes = "", 896 method = "isMCGlobal", 897 args = {} 898 ) 899 public void test_isMCGlobal() throws Exception { 900 String addrName = "224.0.0.255"; 901 InetAddress addr = InetAddress.getByName(addrName); 902 assertTrue("IPv4 link-local multicast address " + addrName 903 + " incorrectly identified as a global multicast address.", 904 !addr.isMCGlobal()); 905 906 addrName = "224.0.1.0"; // a multicast addr 1110 907 addr = Inet4Address.getByName(addrName); 908 assertTrue("IPv4 global multicast address " + addrName 909 + " not identified as a global multicast address.", addr 910 .isMCGlobal()); 911 912 addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 913 addr = InetAddress.getByName(addrName); 914 assertTrue("IPv6 global multicast address " + addrName 915 + " not detected.", addr.isMCGlobal()); 916 917 addrName = "FF08:42:42:42:42:42:42:42"; 918 addr = InetAddress.getByName(addrName); 919 assertTrue("IPv6 mulitcast organizational " + addrName 920 + " incorrectly indicated as a global address.", !addr 921 .isMCGlobal()); 922 } 923 924 @TestTargetNew( 925 level = TestLevel.COMPLETE, 926 notes = "", 927 method = "isMCLinkLocal", 928 args = {} 929 ) 930 public void test_isMCLinkLocal() throws Exception { 931 String addrName = "224.0.0.255"; 932 InetAddress addr = InetAddress.getByName(addrName); 933 assertTrue("IPv4 link-local multicast address " + addrName 934 + " not identified as a link-local multicast address.", 935 addr.isMCLinkLocal()); 936 937 addrName = "224.0.1.0"; 938 addr = InetAddress.getByName(addrName); 939 assertTrue( 940 "IPv4 global multicast address " 941 + addrName 942 + " incorrectly identified as a link-local " + 943 "multicast address.", 944 !addr.isMCLinkLocal()); 945 946 addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 947 addr = InetAddress.getByName(addrName); 948 assertTrue("IPv6 link local multicast address " + addrName 949 + " not detected.", addr.isMCLinkLocal()); 950 951 addrName = "FF08:42:42:42:42:42:42:42"; 952 addr = InetAddress.getByName(addrName); 953 assertTrue( 954 "IPv6 organization multicast address " 955 + addrName 956 + " incorrectly indicated as a link-local " + 957 "mulitcast address.", 958 !addr.isMCLinkLocal()); 959 } 960 961 @TestTargetNew( 962 level = TestLevel.COMPLETE, 963 notes = "", 964 method = "isMCNodeLocal", 965 args = {} 966 ) 967 public void test_isMCNodeLocal() throws Exception { 968 String addrName = "224.42.42.42"; 969 InetAddress addr = InetAddress.getByName(addrName); 970 assertTrue( 971 "IPv4 multicast address " 972 + addrName 973 + " incorrectly identified as a node-local " + 974 "multicast address.", 975 !addr.isMCNodeLocal()); 976 977 addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 978 addr = InetAddress.getByName(addrName); 979 assertTrue("IPv6 node-local multicast address " + addrName 980 + " not detected.", addr.isMCNodeLocal()); 981 982 addrName = "FF08:42:42:42:42:42:42:42"; 983 addr = InetAddress.getByName(addrName); 984 assertTrue("IPv6 mulitcast organizational address " + addrName 985 + " incorrectly indicated as a node-local address.", !addr 986 .isMCNodeLocal()); 987 } 988 989 @TestTargetNew( 990 level = TestLevel.COMPLETE, 991 notes = "", 992 method = "isMCOrgLocal", 993 args = {} 994 ) 995 public void test_isMCOrgLocal() throws Exception { 996 String addrName = "239.252.0.0"; // a multicast addr 1110 997 InetAddress addr = InetAddress.getByName(addrName); 998 assertTrue( 999 "IPv4 site-local multicast address " 1000 + addrName 1001 + " incorrectly identified as a org-local multicast address.", 1002 !addr.isMCOrgLocal()); 1003 1004 addrName = "239.192.0.0"; // a multicast addr 1110 1005 addr = InetAddress.getByName(addrName); 1006 assertTrue("IPv4 org-local multicast address " + addrName 1007 + " not identified as a org-local multicast address.", addr 1008 .isMCOrgLocal()); 1009 1010 addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 1011 addr = InetAddress.getByName(addrName); 1012 assertTrue("IPv6 organization-local multicast address " + addrName 1013 + " not detected.", addr.isMCOrgLocal()); 1014 1015 addrName = "FF0E:42:42:42:42:42:42:42"; 1016 addr = InetAddress.getByName(addrName); 1017 assertTrue( 1018 "IPv6 global multicast address " 1019 + addrName 1020 + " incorrectly indicated as an organization-local mulitcast address.", 1021 !addr.isMCOrgLocal()); 1022 } 1023 1024 @TestTargetNew( 1025 level = TestLevel.COMPLETE, 1026 notes = "", 1027 method = "isMCSiteLocal", 1028 args = {} 1029 ) 1030 public void test_isMCSiteLocal() throws Exception { 1031 String addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"; 1032 InetAddress addr = InetAddress.getByName(addrName); 1033 assertTrue("IPv6 site-local multicast address " + addrName 1034 + " not detected.", addr.isMCSiteLocal()); 1035 1036 // a sample MC organizational address 1037 addrName = "FF08:42:42:42:42:42:42:42"; 1038 addr = Inet6Address.getByName(addrName); 1039 assertTrue( 1040 "IPv6 organization multicast address " 1041 + addrName 1042 + " incorrectly indicated as a site-local " + 1043 "mulitcast address.", 1044 !addr.isMCSiteLocal()); 1045 1046 addrName = "239.0.0.0"; 1047 addr = Inet4Address.getByName(addrName); 1048 assertTrue( 1049 "IPv4 reserved multicast address " 1050 + addrName 1051 + " incorrectly identified as a site-local " + 1052 "multicast address.", 1053 !addr.isMCSiteLocal()); 1054 1055 addrName = "239.255.0.0"; 1056 addr = Inet4Address.getByName(addrName); 1057 assertTrue("IPv4 site-local multicast address " + addrName 1058 + " not identified as a site-local multicast address.", 1059 addr.isMCSiteLocal()); 1060 } 1061 1062 @TestTargetNew( 1063 level = TestLevel.COMPLETE, 1064 notes = "", 1065 method = "isSiteLocalAddress", 1066 args = {} 1067 ) 1068 public void test_isSiteLocalAddress() throws Exception { 1069 String addrName = "42.42.42.42"; 1070 InetAddress addr = InetAddress.getByName(addrName); 1071 assertTrue("IPv4 address " + addrName 1072 + " incorrectly reporting as a site local address.", !addr 1073 .isSiteLocalAddress()); 1074 1075 addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF"; 1076 addr = InetAddress.getByName(addrName); 1077 assertTrue( 1078 "IPv6 site local address " + addrName + " not detected.", 1079 addr.isSiteLocalAddress()); 1080 1081 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF"; 1082 addr = InetAddress.getByName(addrName); 1083 assertTrue("IPv6 address " + addrName 1084 + " detected incorrectly as a site local address.", !addr 1085 .isSiteLocalAddress()); 1086 } 1087 1088 class MockSecurityManager extends SecurityManager { 1089 public void checkPermission(Permission permission) { 1090 if (permission.getName().equals("setSecurityManager")){ 1091 return; 1092 } 1093 if (permission.getName().equals("3d.com")){ 1094 throw new SecurityException(); 1095 } 1096 super.checkPermission(permission); 1097 } 1098 } 1099} 1100