NetworkInterfaceTest.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 tests.api.java.net; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestInfo; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTarget; 24 25import java.net.InetAddress; 26import java.net.NetworkInterface; 27import java.net.SocketException; 28import java.util.ArrayList; 29import java.util.Enumeration; 30 31@TestTargetClass(NetworkInterface.class) 32public class NetworkInterfaceTest extends junit.framework.TestCase { 33 34 // private member variables used for tests 35 boolean atLeastOneInterface = false; 36 37 boolean atLeastTwoInterfaces = false; 38 39 private NetworkInterface networkInterface1 = null; 40 41 private NetworkInterface sameAsNetworkInterface1 = null; 42 43 private NetworkInterface networkInterface2 = null; 44 45 /** 46 * @tests java.net.NetworkInterface#getName() 47 */ 48@TestInfo( 49 level = TestLevel.COMPLETE, 50 purpose = "", 51 targets = { 52 @TestTarget( 53 methodName = "getName", 54 methodArgs = {} 55 ) 56 }) 57 public void test_getName() { 58 if (atLeastOneInterface) { 59 assertNotNull("validate that non null name is returned", 60 networkInterface1.getName()); 61 assertFalse("validate that non-zero length name is generated", 62 networkInterface1.getName().equals("")); 63 } 64 if (atLeastTwoInterfaces) { 65 assertFalse( 66 "Validate strings are different for different interfaces", 67 networkInterface1.getName().equals( 68 networkInterface2.getName())); 69 } 70 } 71 72 /** 73 * @tests java.net.NetworkInterface#getInetAddresses() 74 */ 75@TestInfo( 76 level = TestLevel.COMPLETE, 77 purpose = "", 78 targets = { 79 @TestTarget( 80 methodName = "getInetAddresses", 81 methodArgs = {} 82 ) 83 }) 84 public void test_getInetAddresses() { 85 86 // security manager that allows us to check that we only return the 87 // addresses that we should 88 class mySecurityManager extends SecurityManager { 89 90 ArrayList disallowedNames = null; 91 92 public mySecurityManager(ArrayList addresses) { 93 disallowedNames = new ArrayList(); 94 for (int i = 0; i < addresses.size(); i++) { 95 disallowedNames.add(((InetAddress) addresses.get(i)) 96 .getHostName()); 97 disallowedNames.add(((InetAddress) addresses.get(i)) 98 .getHostAddress()); 99 } 100 } 101 102 public void checkConnect(String host, int port) { 103 104 if (host == null) { 105 throw new NullPointerException("host was null)"); 106 } 107 108 for (int i = 0; i < disallowedNames.size(); i++) { 109 if (((String) disallowedNames.get(i)).equals(host)) { 110 throw new SecurityException("not allowed"); 111 } 112 } 113 } 114 115 } 116 117 if (atLeastOneInterface) { 118 Enumeration theAddresses = networkInterface1.getInetAddresses(); 119 if (theAddresses != null) { 120 while (theAddresses.hasMoreElements()) { 121 InetAddress theAddress = (InetAddress) theAddresses 122 .nextElement(); 123 assertTrue("validate that address is not null", 124 null != theAddress); 125 } 126 } 127 } 128 129 if (atLeastTwoInterfaces) { 130 Enumeration theAddresses = networkInterface2.getInetAddresses(); 131 if (theAddresses != null) { 132 while (theAddresses.hasMoreElements()) { 133 InetAddress theAddress = (InetAddress) theAddresses 134 .nextElement(); 135 assertTrue("validate that address is not null", 136 null != theAddress); 137 } 138 } 139 } 140 141 // create the list of ok and not ok addresses to return 142 if (atLeastOneInterface) { 143 ArrayList okAddresses = new ArrayList(); 144 Enumeration addresses = networkInterface1.getInetAddresses(); 145 int index = 0; 146 ArrayList notOkAddresses = new ArrayList(); 147 if (addresses != null) { 148 while (addresses.hasMoreElements()) { 149 InetAddress theAddress = (InetAddress) addresses 150 .nextElement(); 151 if (index != 0) { 152 okAddresses.add(theAddress); 153 } else { 154 notOkAddresses.add(theAddress); 155 } 156 index++; 157 } 158 } 159 160 // do the same for network interface 2 it it exists 161 if (atLeastTwoInterfaces) { 162 addresses = networkInterface2.getInetAddresses(); 163 index = 0; 164 if (addresses != null) { 165 while (addresses.hasMoreElements()) { 166 InetAddress theAddress = (InetAddress) addresses 167 .nextElement(); 168 if (index != 0) { 169 okAddresses.add(theAddress); 170 } else { 171 notOkAddresses.add(theAddress); 172 } 173 index++; 174 } 175 } 176 } 177 178 // set the security manager that will make the first address not 179 // visible 180 System.setSecurityManager(new mySecurityManager(notOkAddresses)); 181 182 // validate not ok addresses are not returned 183 for (int i = 0; i < notOkAddresses.size(); i++) { 184 Enumeration reducedAddresses = networkInterface1 185 .getInetAddresses(); 186 if (reducedAddresses != null) { 187 while (reducedAddresses.hasMoreElements()) { 188 InetAddress nextAddress = (InetAddress) reducedAddresses 189 .nextElement(); 190 assertTrue( 191 "validate that address without permission is not returned", 192 !nextAddress.equals(notOkAddresses.get(i))); 193 } 194 } 195 if (atLeastTwoInterfaces) { 196 reducedAddresses = networkInterface2.getInetAddresses(); 197 if (reducedAddresses != null) { 198 while (reducedAddresses.hasMoreElements()) { 199 InetAddress nextAddress = (InetAddress) reducedAddresses 200 .nextElement(); 201 assertTrue( 202 "validate that address without permission is not returned", 203 !nextAddress.equals(notOkAddresses.get(i))); 204 } 205 } 206 } 207 } 208 209 // validate that ok addresses are returned 210 for (int i = 0; i < okAddresses.size(); i++) { 211 boolean addressReturned = false; 212 Enumeration reducedAddresses = networkInterface1 213 .getInetAddresses(); 214 if (reducedAddresses != null) { 215 while (reducedAddresses.hasMoreElements()) { 216 InetAddress nextAddress = (InetAddress) reducedAddresses 217 .nextElement(); 218 if (nextAddress.equals(okAddresses.get(i))) { 219 addressReturned = true; 220 } 221 } 222 } 223 if (atLeastTwoInterfaces) { 224 reducedAddresses = networkInterface2.getInetAddresses(); 225 if (reducedAddresses != null) { 226 while (reducedAddresses.hasMoreElements()) { 227 InetAddress nextAddress = (InetAddress) reducedAddresses 228 .nextElement(); 229 if (nextAddress.equals(okAddresses.get(i))) { 230 addressReturned = true; 231 } 232 } 233 } 234 } 235 assertTrue("validate that address with permission is returned", 236 addressReturned); 237 } 238 239 // validate that we can get the interface by specifying the address. 240 // This is to be compatible 241 for (int i = 0; i < notOkAddresses.size(); i++) { 242 try { 243 assertNotNull( 244 "validate we cannot get the NetworkInterface with an address for which we have no privs", 245 NetworkInterface 246 .getByInetAddress((InetAddress) notOkAddresses 247 .get(i))); 248 } catch (Exception e) { 249 fail("get NetworkInterface for address with no perm - exception"); 250 } 251 } 252 253 // validate that we can get the network interface for the good 254 // addresses 255 try { 256 for (int i = 0; i < okAddresses.size(); i++) { 257 assertNotNull( 258 "validate we cannot get the NetworkInterface with an address fro which we have no privs", 259 NetworkInterface 260 .getByInetAddress((InetAddress) okAddresses 261 .get(i))); 262 } 263 } catch (Exception e) { 264 fail("get NetworkInterface for address with perm - exception"); 265 } 266 267 System.setSecurityManager(null); 268 } 269 } 270 271 /** 272 * @tests java.net.NetworkInterface#getDisplayName() 273 */ 274@TestInfo( 275 level = TestLevel.COMPLETE, 276 purpose = "", 277 targets = { 278 @TestTarget( 279 methodName = "getDisplayName", 280 methodArgs = {} 281 ) 282 }) 283 public void test_getDisplayName() { 284 if (atLeastOneInterface) { 285 assertNotNull("validate that non null display name is returned", 286 networkInterface1.getDisplayName()); 287 assertFalse( 288 "validate that non-zero lengtj display name is generated", 289 networkInterface1.getDisplayName().equals("")); 290 } 291 if (atLeastTwoInterfaces) { 292 assertFalse( 293 "Validate strings are different for different interfaces", 294 networkInterface1.getDisplayName().equals( 295 networkInterface2.getDisplayName())); 296 } 297 } 298 299 /** 300 * @tests java.net.NetworkInterface#getByName(java.lang.String) 301 */ 302@TestInfo( 303 level = TestLevel.PARTIAL, 304 purpose = "SocketException checking missed.", 305 targets = { 306 @TestTarget( 307 methodName = "getByName", 308 methodArgs = {String.class} 309 ) 310 }) 311 public void test_getByNameLjava_lang_String() { 312 try { 313 assertNull("validate null handled ok", 314 NetworkInterface.getByName(null)); 315 fail("getByName did not throw NullPointerException for null argument"); 316 } catch (NullPointerException e) { 317 } catch (Exception e) { 318 fail("getByName, null inetAddress - raised exception : " 319 + e.getMessage()); 320 } 321 322 try { 323 assertNull("validate handled ok if we ask for name not associated with any interface", 324 NetworkInterface.getByName("8not a name4")); 325 } catch (Exception e) { 326 fail("getByName, unknown inetAddress - raised exception : " 327 + e.getMessage()); 328 } 329 330 // for each address in an interface validate that we get the right 331 // interface for that name 332 if (atLeastOneInterface) { 333 String theName = networkInterface1.getName(); 334 if (theName != null) { 335 try { 336 assertTrue( 337 "validate that Interface can be obtained with its name", 338 NetworkInterface.getByName(theName).equals( 339 networkInterface1)); 340 } catch (Exception e) { 341 fail("validate to get network interface using name - socket exception"); 342 } 343 } 344 } 345 346 // validate that we get the right interface with the second interface as 347 // well (ie we just don't always get the first interface 348 if (atLeastTwoInterfaces) { 349 String theName = networkInterface2.getName(); 350 if (theName != null) { 351 try { 352 assertTrue( 353 "validate that Interface can be obtained with its name", 354 NetworkInterface.getByName(theName).equals( 355 networkInterface2)); 356 } catch (Exception e) { 357 fail("validate to get network interface using name - socket exception"); 358 } 359 } 360 } 361 } 362 363 /** 364 * @tests java.net.NetworkInterface#getByInetAddress(java.net.InetAddress) 365 */ 366@TestInfo( 367 level = TestLevel.PARTIAL, 368 purpose = "SocketException checking missed.", 369 targets = { 370 @TestTarget( 371 methodName = "getByInetAddress", 372 methodArgs = {InetAddress.class} 373 ) 374 }) 375 public void test_getByInetAddressLjava_net_InetAddress() { 376 377 byte addressBytes[] = new byte[4]; 378 addressBytes[0] = 0; 379 addressBytes[1] = 0; 380 addressBytes[2] = 0; 381 addressBytes[3] = 0; 382 383 try { 384 assertNull("validate null handled ok", 385 NetworkInterface.getByInetAddress(null)); 386 fail("should not get here if getByInetAddress throws " 387 + "NullPointerException if null passed in"); 388 } catch (NullPointerException e) { 389 } catch (Exception e) { 390 fail("getByInetAddress, null inetAddress should have raised NPE" 391 + " but instead threw a : " + e.getMessage()); 392 } 393 394 try { 395 assertNull("validate handled ok if we ask for address not associated with any interface", 396 NetworkInterface.getByInetAddress(InetAddress 397 .getByAddress(addressBytes))); 398 } catch (Exception e) { 399 fail("getByInetAddress, unknown inetAddress threw exception : " + e); 400 } 401 402 // for each address in an interface validate that we get the right 403 // interface for that address 404 if (atLeastOneInterface) { 405 Enumeration addresses = networkInterface1.getInetAddresses(); 406 if (addresses != null) { 407 while (addresses.hasMoreElements()) { 408 InetAddress theAddress = (InetAddress) addresses 409 .nextElement(); 410 try { 411 assertTrue( 412 "validate that Interface can be obtained with any one of its addresses", 413 NetworkInterface.getByInetAddress(theAddress) 414 .equals(networkInterface1)); 415 } catch (Exception e) { 416 fail("validate to get address using inetAddress " + 417 "threw exception : " + e); 418 } 419 } 420 } 421 } 422 423 // validate that we get the right interface with the second interface as 424 // well (ie we just don't always get the first interface 425 if (atLeastTwoInterfaces) { 426 Enumeration addresses = networkInterface2.getInetAddresses(); 427 if (addresses != null) { 428 while (addresses.hasMoreElements()) { 429 InetAddress theAddress = (InetAddress) addresses 430 .nextElement(); 431 try { 432 assertTrue( 433 "validate that Interface can be obtained with any one of its addresses", 434 NetworkInterface.getByInetAddress(theAddress) 435 .equals(networkInterface2)); 436 } catch (Exception e) { 437 fail("validate to get address using inetAddress " 438 + "threw exception : " + e); 439 } 440 } 441 } 442 } 443 } 444 445 /** 446 * @tests java.net.NetworkInterface#getNetworkInterfaces() 447 */ 448@TestInfo( 449 level = TestLevel.PARTIAL, 450 purpose = "SocketException checking missed.", 451 targets = { 452 @TestTarget( 453 methodName = "getNetworkInterfaces", 454 methodArgs = {} 455 ) 456 }) 457 public void test_getNetworkInterfaces() { 458 459 // really this is tested by all of the other calls but just make sure we 460 // can call it and get a list of interfaces if they exist 461 try { 462 Enumeration theInterfaces = NetworkInterface.getNetworkInterfaces(); 463 } catch (Exception e) { 464 fail("get Network Interfaces - raised exception : " 465 + e.getMessage()); 466 } 467 } 468 469 /** 470 * @tests java.net.NetworkInterface#equals(java.lang.Object) 471 */ 472@TestInfo( 473 level = TestLevel.COMPLETE, 474 purpose = "", 475 targets = { 476 @TestTarget( 477 methodName = "equals", 478 methodArgs = {Object.class} 479 ) 480 }) 481 public void test_equalsLjava_lang_Object() { 482 // Test for method boolean 483 // java.net.SocketPermission.equals(java.lang.Object) 484 if (atLeastOneInterface) { 485 assertTrue("If objects are the same true is returned", 486 networkInterface1.equals(sameAsNetworkInterface1)); 487 assertFalse("Validate Null handled ok", networkInterface1 488 .equals(null)); 489 } 490 if (atLeastTwoInterfaces) { 491 assertFalse("If objects are different false is returned", 492 networkInterface1.equals(networkInterface2)); 493 } 494 } 495 496 /** 497 * @tests java.net.NetworkInterface#hashCode() 498 */ 499@TestInfo( 500 level = TestLevel.COMPLETE, 501 purpose = "", 502 targets = { 503 @TestTarget( 504 methodName = "hashCode", 505 methodArgs = {} 506 ) 507 }) 508 public void test_hashCode() { 509 510 if (atLeastOneInterface) { 511 assertTrue( 512 "validate that hash codes are the same for two calls on the same object", 513 networkInterface1.hashCode() == networkInterface1 514 .hashCode()); 515 assertTrue( 516 "validate that hash codes are the same for two objects for which equals is true", 517 networkInterface1.hashCode() == sameAsNetworkInterface1 518 .hashCode()); 519 } 520 } 521 522 /** 523 * @tests java.net.NetworkInterface#toString() 524 */ 525@TestInfo( 526 level = TestLevel.COMPLETE, 527 purpose = "", 528 targets = { 529 @TestTarget( 530 methodName = "toString", 531 methodArgs = {} 532 ) 533 }) 534 public void test_toString() { 535 if (atLeastOneInterface) { 536 assertNotNull("validate that non null string is generated", 537 networkInterface1.toString()); 538 assertFalse("validate that non-zero length string is generated", 539 networkInterface1.toString().equals("")); 540 } 541 if (atLeastTwoInterfaces) { 542 assertFalse( 543 "Validate strings are different for different interfaces", 544 networkInterface1.toString().equals( 545 networkInterface2.toString())); 546 } 547 } 548 549 protected void setUp() { 550 551 Enumeration theInterfaces = null; 552 try { 553 theInterfaces = NetworkInterface.getNetworkInterfaces(); 554 } catch (Exception e) { 555 fail("Exception occurred getting network interfaces : " + e); 556 } 557 558 // Set up NetworkInterface instance members. Note that because the call 559 // to NetworkInterface.getNetworkInterfaces() returns *all* of the 560 // interfaces on the test machine it is possible that one or more of 561 // them will not currently be bound to an InetAddress. e.g. a laptop 562 // running connected by a wire to the local network may also have a 563 // wireless interface that is not active and so has no InetAddress 564 // bound to it. For these tests only work with NetworkInterface objects 565 // that are bound to an InetAddress. 566 if ((theInterfaces != null) && (theInterfaces.hasMoreElements())) { 567 while ((theInterfaces.hasMoreElements()) 568 && (atLeastOneInterface == false)) { 569 NetworkInterface theInterface = (NetworkInterface) theInterfaces 570 .nextElement(); 571 if (theInterface.getInetAddresses() != null) { 572 // Ensure that the current NetworkInterface has at least 573 // one InetAddress bound to it. 574 Enumeration addrs = theInterface.getInetAddresses(); 575 if ((addrs != null) && (addrs.hasMoreElements())) { 576 atLeastOneInterface = true; 577 networkInterface1 = theInterface; 578 }// end if 579 } 580 } 581 582 while ((theInterfaces.hasMoreElements()) 583 && (atLeastTwoInterfaces == false)) { 584 NetworkInterface theInterface = (NetworkInterface) theInterfaces 585 .nextElement(); 586 if (theInterface.getInetAddresses() != null) { 587 // Ensure that the current NetworkInterface has at least 588 // one InetAddress bound to it. 589 Enumeration addrs = theInterface.getInetAddresses(); 590 if ((addrs != null) && (addrs.hasMoreElements())) { 591 atLeastTwoInterfaces = true; 592 networkInterface2 = theInterface; 593 }// end if 594 } 595 } 596 597 // Only set sameAsNetworkInterface1 if we succeeded in finding 598 // at least one good NetworkInterface 599 if (atLeastOneInterface) { 600 Enumeration addresses = networkInterface1.getInetAddresses(); 601 if (addresses != null) { 602 try { 603 if (addresses.hasMoreElements()) { 604 sameAsNetworkInterface1 = NetworkInterface 605 .getByInetAddress((InetAddress) addresses 606 .nextElement()); 607 } 608 } catch (SocketException e) { 609 fail("SocketException occurred : " + e); 610 } 611 } 612 }// end if atLeastOneInterface 613 } 614 } 615 616 protected void tearDown() { 617 System.setSecurityManager(null); 618 } 619} 620