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