SocketTest.java revision 5d709784bbf5001012d7f25172927d46f6c1abe1
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.AndroidOnly; 21//import dalvik.annotation.KnownFailure; 22import dalvik.annotation.TestTargetClass; 23import dalvik.annotation.TestTargets; 24import dalvik.annotation.TestLevel; 25import dalvik.annotation.TestTargetNew; 26 27import java.io.IOException; 28import java.io.InputStream; 29import java.io.InterruptedIOException; 30import java.io.OutputStream; 31import java.net.ConnectException; 32import java.net.Inet4Address; 33import java.net.Inet6Address; 34import java.net.InetAddress; 35import java.net.InetSocketAddress; 36import java.net.Proxy; 37import java.net.ServerSocket; 38import java.net.Socket; 39import java.net.SocketAddress; 40import java.net.SocketException; 41import java.net.SocketImpl; 42import java.net.SocketTimeoutException; 43import java.net.UnknownHostException; 44import java.nio.channels.IllegalBlockingModeException; 45import java.nio.channels.SocketChannel; 46import java.security.Permission; 47 48import tests.support.Support_Configuration; 49import tests.support.Support_PortManager; 50 51@TestTargetClass(Socket.class) 52public class SocketTest extends SocketTestCase { 53 54 ServerSocket ss; 55 56 Socket s; 57 58 Thread t; 59 60 boolean interrupted = false; 61 62 class SServer extends Thread implements Runnable { 63 Socket s1 = null; 64 65 public void run() { 66 try { 67 ss.setSoTimeout(5000); 68 s1 = ss.accept(); 69 ss.close(); 70 Thread.sleep(4000); 71 } catch (java.io.InterruptedIOException x) { 72 System.out.println(Thread.currentThread() 73 + ", accept() timeout fired: " + x); 74 } catch (InterruptedException x) { 75 } catch (Exception e) { 76 System.out.println("Unable to accept: " + e.toString()); 77 } finally { 78 try { 79 if (s1 != null) 80 s1.close(); 81 } catch (java.io.IOException e) { 82 } 83 } 84 } 85 } 86 87 SecurityManager sm = new SecurityManager() { 88 89 public void checkPermission(Permission perm) {} 90 91 public void checkConnect(String host, int port) { 92 throw new SecurityException(); 93 } 94 }; 95 96 /** 97 * @tests java.net.Socket#Socket() 98 */ 99 @TestTargetNew( 100 level = TestLevel.COMPLETE, 101 notes = "", 102 method = "Socket", 103 args = {} 104 ) 105 public void test_Constructor() { 106 // create the socket and then validate some basic state 107 s = new Socket(); 108 assertFalse("new socket should not be connected", s.isConnected()); 109 assertFalse("new socket should not be bound", s.isBound()); 110 assertFalse("new socket should not be closed", s.isClosed()); 111 assertFalse("new socket should not be in InputShutdown", s 112 .isInputShutdown()); 113 assertFalse("new socket should not be in OutputShutdown", s 114 .isOutputShutdown()); 115 116 } 117 118 /** 119 * @tests java.net.Socket#Socket(java.lang.String, int) 120 */ 121 @TestTargetNew( 122 level = TestLevel.COMPLETE, 123 notes = "", 124 method = "Socket", 125 args = {java.lang.String.class, int.class} 126 ) 127 public void test_ConstructorLjava_lang_StringI() throws IOException { 128 // Test for method java.net.Socket(java.lang.String, int) 129 int sport = startServer("Cons String,I"); 130 s = new Socket(InetAddress.getLocalHost().getHostName(), sport); 131 assertTrue("Failed to create socket", s.getPort() == sport); 132 133 //regression for HARMONY-946 134 ServerSocket ss = null; 135 Socket s = null; 136 try { 137 ss = new ServerSocket(0); 138 s = new Socket("0.0.0.0", ss.getLocalPort()); 139 } finally { 140 try { 141 ss.close(); 142 } catch(Exception e) { 143 //ignore 144 } 145 try { 146 s.close(); 147 } catch(Exception e) { 148 //ignore 149 } 150 } 151 152 try { 153 new Socket("unknown.host", 0); 154 fail("UnknownHostException was not thrown."); 155 } catch(UnknownHostException uhe) { 156 //expected 157 } 158 Socket socket = null; 159 try { 160 socket = new Socket(InetAddress.getByName(null), sport); 161 assertEquals(InetAddress.getByName("127.0.0.1"), socket.getLocalAddress()); 162 } finally { 163 try { 164 socket.close(); 165 } catch(Exception e) {} 166 } 167 168 SecurityManager oldSm = System.getSecurityManager(); 169 System.setSecurityManager(sm); 170 try { 171 new Socket(InetAddress.getLocalHost().getHostName(), sport); 172 fail("SecurityException should be thrown."); 173 } catch (SecurityException e) { 174 // expected 175 } catch (SocketException e) { 176 fail("SocketException was thrown."); 177 } finally { 178 System.setSecurityManager(oldSm); 179 } 180 } 181 182 /** 183 * @tests java.net.Socket#Socket(java.lang.String, int, 184 * java.net.InetAddress, int) 185 */ 186 @TestTargetNew( 187 level = TestLevel.COMPLETE, 188 notes = "", 189 method = "Socket", 190 args = {java.lang.String.class, int.class, java.net.InetAddress.class, int.class} 191 ) 192 public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI() 193 throws IOException { 194 // Test for method java.net.Socket(java.lang.String, int, 195 // java.net.InetAddress, int) 196 int sport = startServer("Cons String,I,InetAddress,I"); 197 int portNumber = Support_PortManager.getNextPort(); 198 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 199 InetAddress.getLocalHost(), portNumber); 200 assertTrue("Failed to create socket", s.getPort() == sport); 201 202 if (("true".equals(System.getProperty("java.net.preferIPv6Addresses"))) 203 && !("true".equals(System 204 .getProperty("java.net.preferIPv4Stack")))) { 205 206 // ALTERNATE IPv6 TEST 207 if ("true".equals(System.getProperty("run.ipv6tests"))) { 208 System.out 209 .println("Running testConstructorLjava_lang_StringILjava_net_InetAddressI(SocketTest) with IPv6GlobalAddressJcl4: " 210 + Support_Configuration.IPv6GlobalAddressJcl4); 211 int testPort = Support_PortManager.getNextPort(); 212 Socket s1 = null, s2 = null; 213 try { 214 s1 = new Socket( 215 Support_Configuration.IPv6GlobalAddressJcl4, 80, 216 InetAddress.getLocalHost(), testPort); 217 } catch (IOException e) { 218 // check here if InetAddress.getLocalHost() is returning the 219 // loopback address. 220 // if so that is likely the cause of the failure 221 String warning = ""; 222 try { 223 InetAddress returnedLocalHost = InetAddress 224 .getLocalHost(); 225 // don't use isLoopbackAddress for some configurations 226 // as they do not have it 227 if (returnedLocalHost.isLoopbackAddress()) { 228 warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE"; 229 230 } 231 } catch (Exception ex) { 232 warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex; 233 } 234 235 fail("Exception creating 1st socket" + warning + ": " + e); 236 } 237 boolean exception = false; 238 try { 239 s2 = new Socket( 240 Support_Configuration.IPv6GlobalAddressJcl4, 80, 241 InetAddress.getLocalHost(), testPort); 242 } catch (IOException e) { 243 exception = true; 244 } 245 try { 246 s1.close(); 247 if (!exception) 248 s2.close(); 249 } catch (IOException e) { 250 } 251 assertTrue("Was able to create two sockets on same port", 252 exception); 253 } 254 255 } else { 256 int testPort = Support_PortManager.getNextPort(); 257 Socket s1 = null, s2 = null; 258 int serverPort = ss.getLocalPort(); 259 try { 260 s1 = new Socket("127.0.0.1", serverPort, InetAddress 261 .getLocalHost(), testPort); 262 } catch (IOException e) { 263 e.printStackTrace(); 264 265 // check here if InetAddress.getLocalHost() is returning the 266 // loopback address. 267 // if so that is likely the cause of the failure 268 String warning = ""; 269 try { 270 InetAddress returnedLocalHost = InetAddress.getLocalHost(); 271 // don't use isLoopbackAddress for some configurations as 272 // they do not have it 273 if (returnedLocalHost.isLoopbackAddress()) { 274 warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE"; 275 276 } 277 } catch (Exception ex) { 278 warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex; 279 } 280 281 fail("Exception creating 1st socket" + warning + ": " + e); 282 } 283 boolean exception = false; 284 try { 285 s2 = new Socket("127.0.0.1", serverPort, InetAddress 286 .getLocalHost(), testPort); 287 } catch (IOException e) { 288 exception = true; 289 } 290 try { 291 s1.close(); 292 if (!exception) 293 s2.close(); 294 } catch (IOException e) { 295 } 296 assertTrue("Was able to create two sockets on same port", exception); 297 } 298 299 SecurityManager oldSm = System.getSecurityManager(); 300 System.setSecurityManager(sm); 301 try { 302 new Socket("127.0.0.1", 0, InetAddress 303 .getLocalHost(), 0); 304 fail("SecurityException should be thrown."); 305 } catch (SecurityException e) { 306 // expected 307 } catch (SocketException e) { 308 fail("SocketException was thrown."); 309 } finally { 310 System.setSecurityManager(oldSm); 311 } 312 } 313 314 /** 315 * @tests java.net.Socket#Socket(java.lang.String, int, boolean) 316 */ 317 @TestTargetNew( 318 level = TestLevel.COMPLETE, 319 notes = "", 320 method = "Socket", 321 args = {java.lang.String.class, int.class, boolean.class} 322 ) 323 public void test_ConstructorLjava_lang_StringIZ() throws IOException { 324 // Test for method java.net.Socket(java.lang.String, int, boolean) 325 int sport = startServer("Cons String,I,Z"); 326 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, true); 327 assertTrue("Failed to create socket", s.getPort() == sport); 328 329 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, false); 330 331 SecurityManager oldSm = System.getSecurityManager(); 332 System.setSecurityManager(sm); 333 try { 334 new Socket(InetAddress.getLocalHost().getHostName(), sport, true); 335 fail("SecurityException should be thrown."); 336 } catch (SecurityException e) { 337 // expected 338 } catch (SocketException e) { 339 fail("SocketException was thrown."); 340 } finally { 341 System.setSecurityManager(oldSm); 342 } 343 } 344 345 /** 346 * @tests java.net.Socket#Socket(java.net.InetAddress, int) 347 */ 348 @TestTargetNew( 349 level = TestLevel.COMPLETE, 350 notes = "", 351 method = "Socket", 352 args = {java.net.InetAddress.class, int.class} 353 ) 354 public void test_ConstructorLjava_net_InetAddressI() throws IOException { 355 // Test for method java.net.Socket(java.net.InetAddress, int) 356 int sport = startServer("Cons InetAddress,I"); 357 s = new Socket(InetAddress.getLocalHost(), sport); 358 assertTrue("Failed to create socket", s.getPort() == sport); 359 360 SecurityManager oldSm = System.getSecurityManager(); 361 System.setSecurityManager(sm); 362 try { 363 new Socket(InetAddress.getLocalHost(), sport); 364 fail("SecurityException should be thrown."); 365 } catch (SecurityException e) { 366 // expected 367 } catch (SocketException e) { 368 fail("SocketException was thrown."); 369 } finally { 370 System.setSecurityManager(oldSm); 371 } 372 } 373 374 /** 375 * @tests java.net.Socket#Socket(java.net.InetAddress, int, 376 * java.net.InetAddress, int) 377 */ 378 @TestTargetNew( 379 level = TestLevel.COMPLETE, 380 notes = "", 381 method = "Socket", 382 args = {java.net.InetAddress.class, int.class, java.net.InetAddress.class, int.class} 383 ) 384 public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI() 385 throws IOException { 386 // Test for method java.net.Socket(java.net.InetAddress, int, 387 // java.net.InetAddress, int) 388 int sport = startServer("Cons InetAddress,I,InetAddress,I"); 389 int portNumber = Support_PortManager.getNextPort(); 390 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 391 InetAddress.getLocalHost(), portNumber); 392 assertTrue("Failed to create socket", s.getLocalPort() == portNumber); 393 394 SecurityManager oldSm = System.getSecurityManager(); 395 System.setSecurityManager(sm); 396 try { 397 new Socket(InetAddress.getLocalHost().getHostName(), sport, 398 InetAddress.getLocalHost(), portNumber); 399 fail("SecurityException should be thrown."); 400 } catch (SecurityException e) { 401 // expected 402 } catch (SocketException e) { 403 fail("SocketException was thrown."); 404 } finally { 405 System.setSecurityManager(oldSm); 406 } 407 } 408 409 /** 410 * @tests java.net.Socket#Socket(java.net.InetAddress, int, boolean) 411 */ 412 @TestTargetNew( 413 level = TestLevel.COMPLETE, 414 notes = "", 415 method = "Socket", 416 args = {java.net.InetAddress.class, int.class, boolean.class} 417 ) 418 public void test_ConstructorLjava_net_InetAddressIZ() throws IOException { 419 // Test for method java.net.Socket(java.net.InetAddress, int, boolean) 420 int sport = startServer("Cons InetAddress,I,Z"); 421 s = new Socket(InetAddress.getLocalHost(), sport, true); 422 assertTrue("Failed to create socket", s.getPort() == sport); 423 424 s = new Socket(InetAddress.getLocalHost(), sport, false); 425 426 SecurityManager oldSm = System.getSecurityManager(); 427 System.setSecurityManager(sm); 428 try { 429 new Socket(InetAddress.getLocalHost(), sport, true); 430 fail("SecurityException should be thrown."); 431 } catch (SecurityException e) { 432 // expected 433 } catch (SocketException e) { 434 fail("SocketException was thrown."); 435 } finally { 436 System.setSecurityManager(oldSm); 437 } 438 } 439 440 /** 441 * @tests java.net.Socket#close() 442 */ 443 @TestTargetNew( 444 level = TestLevel.SUFFICIENT, 445 notes = "IOException checking missing.", 446 method = "close", 447 args = {} 448 ) 449 public void test_close() throws IOException { 450 // Test for method void java.net.Socket.close() 451 int sport = startServer("SServer close"); 452 int portNumber = Support_PortManager.getNextPort(); 453 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 454 try { 455 s.setSoLinger(false, 100); 456 } catch (IOException e) { 457 handleException(e, SO_LINGER); 458 } 459 s.close(); 460 try { 461 s.getOutputStream(); 462 fail("IOException was not thrown."); 463 } catch (java.io.IOException e) { 464 //expected 465 } 466 } 467 468 /** 469 * @tests java.net.Socket#getInetAddress() 470 */ 471 @TestTargetNew( 472 level = TestLevel.COMPLETE, 473 notes = "", 474 method = "getInetAddress", 475 args = {} 476 ) 477 public void test_getInetAddress() throws IOException { 478 // Test for method java.net.InetAddress java.net.Socket.getInetAddress() 479 int sport = startServer("SServer getInetAddress"); 480 int portNumber = Support_PortManager.getNextPort(); 481 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 482 assertTrue("Returned incorrect InetAdrees", s.getInetAddress().equals( 483 InetAddress.getLocalHost())); 484 485 } 486 487 /** 488 * @tests java.net.Socket#getInputStream() 489 */ 490 @TestTargetNew( 491 level = TestLevel.COMPLETE, 492 notes = "", 493 method = "getInputStream", 494 args = {} 495 ) 496 public void test_getInputStream() throws IOException { 497 // Test for method java.io.InputStream java.net.Socket.getInputStream() 498 int sport = startServer("SServer getInputStream"); 499 int portNumber = Support_PortManager.getNextPort(); 500 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 501 (t = new SServer()).start(); 502 java.io.InputStream is = s.getInputStream(); 503 assertNotNull("Failed to get stream", is); 504 s.setSoTimeout(6000); 505 is.read(); 506 s.close(); 507 assertEquals("Invalid after close", -1, is.read()); 508 509 interrupted = false; 510 int portNum = Support_PortManager.getNextPort(); 511 final ServerSocket ss = new ServerSocket(portNum); 512 Socket sock = new Socket(InetAddress.getLocalHost(), portNum); 513 Runnable runnable = new Runnable() { 514 public void run() { 515 try { 516 Socket as = ss.accept(); 517 ss.close(); 518 as.setSoTimeout(12000); 519 InputStream in = as.getInputStream(); 520 in.read(); 521 in.close(); 522 } catch (InterruptedIOException e) { 523 interrupted = true; 524 } catch (IOException e) { 525 } 526 } 527 }; 528 Thread thread = new Thread(runnable, "Socket.getInputStream"); 529 thread.start(); 530 try { 531 do { 532 Thread.sleep(200); 533 } while (!thread.isAlive()); 534 } catch (InterruptedException e) { 535 } 536 try { 537 Thread.sleep(200); 538 } catch (InterruptedException e) { 539 } 540 sock.close(); 541 try { 542 sock.getInputStream(); 543 fail("IOException was not thrown."); 544 } catch(IOException ioe) { 545 //expected 546 } 547 int c = 0; 548 do { 549 try { 550 Thread.sleep(200); 551 } catch (InterruptedException e) { 552 } 553 if (interrupted) { 554 fail("read interrupted"); 555 } 556 if (++c > 4) { 557 fail("read call did not exit"); 558 } 559 } while (thread.isAlive()); 560 561 } 562 563 /** 564 * @tests java.net.Socket#getKeepAlive() 565 */ 566 @TestTargetNew( 567 level = TestLevel.COMPLETE, 568 notes = "", 569 method = "getKeepAlive", 570 args = {} 571 ) 572 public void test_getKeepAlive() { 573 try { 574 int sport = startServer("SServer getKeepAlive"); 575 int portNumber = Support_PortManager.getNextPort(); 576 Socket theSocket = new Socket(InetAddress.getLocalHost(), sport, 577 null, portNumber); 578 theSocket.setKeepAlive(true); 579 assertTrue("getKeepAlive false when it should be true", theSocket 580 .getKeepAlive()); 581 theSocket.setKeepAlive(false); 582 assertFalse("getKeepAlive true when it should be False", theSocket 583 .getKeepAlive()); 584 theSocket.close(); 585 try { 586 theSocket.setKeepAlive(false); 587 fail("IOException was not thrown after calling setKeepAlive " + 588 "method."); 589 } catch(IOException ioe) { 590 //expected 591 } 592 try { 593 theSocket.getKeepAlive(); 594 fail("IOException was not thrown after calling getKeepAlive +" + 595 "method."); 596 } catch(IOException ioe) { 597 //expected 598 } 599 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE); 600 } catch (Exception e) { 601 handleException(e, SO_KEEPALIVE); 602 } 603 } 604 605 /** 606 * @tests java.net.Socket#getLocalAddress() 607 */ 608 @TestTargetNew( 609 level = TestLevel.COMPLETE, 610 notes = "", 611 method = "getLocalAddress", 612 args = {} 613 ) 614 public void test_getLocalAddress() throws IOException { 615 // Test for method java.net.InetAddress 616 // java.net.Socket.getLocalAddress() 617 int sport = startServer("SServer getLocAddress"); 618 int portNumber = Support_PortManager.getNextPort(); 619 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 620 assertEquals("Returned incorrect InetAddress", 621 InetAddress.getLocalHost(), s.getLocalAddress()); 622 623 // now validate thet behaviour when the any address is returned 624 String preferIPv4StackValue = System 625 .getProperty("java.net.preferIPv4Stack"); 626 String preferIPv6AddressesValue = System 627 .getProperty("java.net.preferIPv6Addresses"); 628 629 s = new Socket(); 630 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 631 632 if (((preferIPv4StackValue == null) || preferIPv4StackValue 633 .equalsIgnoreCase("false")) 634 && (preferIPv6AddressesValue != null) 635 && (preferIPv6AddressesValue.equals("true"))) { 636 assertTrue( 637 "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false " 638 + s.getLocalSocketAddress(), 639 s.getLocalAddress() instanceof Inet6Address); 640 } else { 641 assertTrue( 642 "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true " 643 + s.getLocalSocketAddress(), 644 s.getLocalAddress() instanceof Inet4Address); 645 } 646 s.close(); 647 648 } 649 650 /** 651 * @tests java.net.Socket#getLocalPort() 652 */ 653 @TestTargetNew( 654 level = TestLevel.COMPLETE, 655 notes = "", 656 method = "getLocalPort", 657 args = {} 658 ) 659 public void test_getLocalPort() throws IOException { 660 // Test for method int java.net.Socket.getLocalPort() 661 int sport = startServer("SServer getLocalPort"); 662 int portNumber = Support_PortManager.getNextPort(); 663 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 664 InetAddress.getLocalHost(), portNumber); 665 assertTrue("Returned incorrect port", s.getLocalPort() == portNumber); 666 } 667 668 /** 669 * @tests java.net.Socket#getOutputStream() 670 */ 671 @SuppressWarnings("deprecation") 672 @TestTargetNew( 673 level = TestLevel.PARTIAL_COMPLETE, 674 notes = "IOException checking missing.", 675 method = "getOutputStream", 676 args = {} 677 ) 678 public void test_getOutputStream() throws IOException { 679 // Test for method java.io.OutputStream 680 // java.net.Socket.getOutputStream() 681 int sport = startServer("SServer getOutputStream"); 682 int portNumber = Support_PortManager.getNextPort(); 683 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 684 java.io.OutputStream os = s.getOutputStream(); 685 assertNotNull("Failed to get stream", os); 686 os.write(1); 687 s.close(); 688 // Regression test for harmony-2934 689 s = new Socket("127.0.0.1", Support_PortManager.getNextPort(), 690 false); 691 OutputStream o = s.getOutputStream(); 692 o.write(1); 693 try { 694 Thread.sleep(1000); 695 } catch (InterruptedException e) { 696 } 697 o.close(); 698 s.close(); 699 700 // Regression test for harmony-2942 701 s = new Socket("0.0.0.0", Support_PortManager.getNextPort(), 702 false); 703 o = s.getOutputStream(); 704 o.write(1); 705 try { 706 Thread.sleep(1000); 707 } catch (InterruptedException e) { 708 } 709 o.close(); 710 s.close(); 711 } 712 713 /** 714 * @tests java.net.Socket#getPort() 715 */ 716 @TestTargetNew( 717 level = TestLevel.COMPLETE, 718 notes = "", 719 method = "getPort", 720 args = {} 721 ) 722 public void test_getPort() throws IOException { 723 // Test for method int java.net.Socket.getPort() 724 int sport = startServer("SServer getPort"); 725 int portNumber = Support_PortManager.getNextPort(); 726 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 727 assertTrue("Returned incorrect port" + s.getPort(), 728 s.getPort() == sport); 729 } 730 731 /** 732 * @tests java.net.Socket#getSoLinger() 733 */ 734 @TestTargetNew( 735 level = TestLevel.COMPLETE, 736 notes = "", 737 method = "getSoLinger", 738 args = {} 739 ) 740 public void test_getSoLinger() { 741 // Test for method int java.net.Socket.getSoLinger() 742 int sport = startServer("SServer getSoLinger"); 743 try { 744 int portNumber = Support_PortManager.getNextPort(); 745 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 746 s.setSoLinger(true, 200); 747 assertEquals("Returned incorrect linger", 200, s.getSoLinger()); 748 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER); 749 s.setSoLinger(false, 0); 750 } catch (Exception e) { 751 handleException(e, SO_LINGER); 752 } 753 754 try { 755 int portNumber = Support_PortManager.getNextPort(); 756 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 757 s.close(); 758 try { 759 s.getSoLinger(); 760 fail("SocketException was not thrown."); 761 } catch(SocketException ioe) { 762 //expected 763 } 764 } catch(Exception e) { 765 fail("Unexpected exception was thrown: " + e.toString()); 766 } 767 } 768 769 /** 770 * @tests java.net.Socket#getReceiveBufferSize() 771 */ 772 @TestTargetNew( 773 level = TestLevel.COMPLETE, 774 notes = "", 775 method = "getReceiveBufferSize", 776 args = {} 777 ) 778 public void test_getReceiveBufferSize() { 779 try { 780 int sport = startServer("SServer getReceiveBufferSize"); 781 int portNumber = Support_PortManager.getNextPort(); 782 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 783 null, portNumber); 784 s.setReceiveBufferSize(130); 785 assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130); 786 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 787 } catch (Exception e) { 788 handleException(e, SO_RCVBUF); 789 } 790 791 try { 792 Socket newSocket = new Socket(); 793 newSocket.close(); 794 try { 795 newSocket.getReceiveBufferSize(); 796 fail("SocketException was not thrown."); 797 } catch(SocketException e) { 798 //expected 799 } 800 } catch(Exception e) { 801 fail("Unexpected exception."); 802 } 803 } 804 805 /** 806 * @tests java.net.Socket#getSendBufferSize() 807 */ 808 @TestTargetNew( 809 level = TestLevel.COMPLETE, 810 notes = "", 811 method = "getSendBufferSize", 812 args = {} 813 ) 814 public void test_getSendBufferSize() { 815 int sport = startServer("SServer setSendBufferSize"); 816 try { 817 int portNumber = Support_PortManager.getNextPort(); 818 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 819 null, portNumber); 820 s.setSendBufferSize(134); 821 assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134); 822 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF); 823 } catch (Exception e) { 824 handleException(e, SO_SNDBUF); 825 } 826 try { 827 int portNumber = Support_PortManager.getNextPort(); 828 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 829 s.close(); 830 try { 831 s.getSendBufferSize(); 832 fail("IOException was not thrown."); 833 } catch(IOException ioe) { 834 //expected 835 } 836 } catch(Exception e) { 837 fail("Unexpected exception was thrown: " + e.toString()); 838 } 839 } 840 841 /** 842 * @tests java.net.Socket#getSoTimeout() 843 */ 844 @TestTargetNew( 845 level = TestLevel.COMPLETE, 846 notes = "", 847 method = "getSoTimeout", 848 args = {} 849 ) 850 public void test_getSoTimeout() { 851 // Test for method int java.net.Socket.getSoTimeout() 852 int sport = startServer("SServer getSoTimeout"); 853 try { 854 s = new Socket(InetAddress.getLocalHost(), sport); 855 s.setSoTimeout(100); 856 assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout()); 857 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT); 858 } catch (Exception e) { 859 handleException(e, SO_TIMEOUT); 860 } 861 862 try { 863 int portNumber = Support_PortManager.getNextPort(); 864 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 865 s.close(); 866 try { 867 s.getSoTimeout(); 868 fail("SocketException was not thrown."); 869 } catch(SocketException ioe) { 870 //expected 871 } 872 } catch(Exception e) { 873 fail("Unexpected exception was thrown: " + e.toString()); 874 } 875 } 876 877 /** 878 * @tests java.net.Socket#getTcpNoDelay() 879 */ 880 @TestTargetNew( 881 level = TestLevel.COMPLETE, 882 notes = "", 883 method = "getTcpNoDelay", 884 args = {} 885 ) 886 public void test_getTcpNoDelay() { 887 // Test for method boolean java.net.Socket.getTcpNoDelay() 888 int sport = startServer("SServer getTcpNoDelay"); 889 try { 890 int portNumber = Support_PortManager.getNextPort(); 891 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 892 boolean bool = !s.getTcpNoDelay(); 893 s.setTcpNoDelay(bool); 894 assertTrue("Failed to get no delay setting: " + s.getTcpNoDelay(), 895 s.getTcpNoDelay() == bool); 896 ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY); 897 } catch (Exception e) { 898 handleException(e, TCP_NODELAY); 899 } 900 901 try { 902 int portNumber = Support_PortManager.getNextPort(); 903 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 904 s.close(); 905 try { 906 s.getTcpNoDelay(); 907 fail("SocketException was not thrown."); 908 } catch(SocketException ioe) { 909 //expected 910 } 911 } catch(Exception e) { 912 fail("Unexpected exception was thrown: " + e.toString()); 913 } 914 } 915 916 /** 917 * @tests java.net.Socket#setKeepAlive(boolean) 918 */ 919 @TestTargetNew( 920 level = TestLevel.COMPLETE, 921 notes = "", 922 method = "setKeepAlive", 923 args = {boolean.class} 924 ) 925 public void test_setKeepAliveZ() throws Exception { 926 // There is not really a good test for this as it is there to detect 927 // crashed machines. Just make sure we can set it 928 try { 929 int sport = startServer("SServer setKeepAlive"); 930 int portNumber = Support_PortManager.getNextPort(); 931 Socket theSocket = new Socket(InetAddress.getLocalHost(), sport, 932 null, portNumber); 933 theSocket.setKeepAlive(true); 934 theSocket.setKeepAlive(false); 935 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE); 936 } catch (Exception e) { 937 handleException(e, SO_KEEPALIVE); 938 } 939 // regression test for HARMONY-1136 940 new TestSocket((SocketImpl) null).setKeepAlive(true); 941 942 try { 943 Socket theSocket = new Socket(); 944 theSocket.close(); 945 theSocket.setKeepAlive(true); 946 fail("SocketException was not thrown."); 947 } catch(SocketException ioe) { 948 //expected 949 } 950 } 951 class TestSocket extends Socket { 952 public TestSocket(SocketImpl impl) throws SocketException { 953 super(impl); 954 } 955 } 956 957 /** 958 * @tests java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) 959 */ 960 @TestTargetNew( 961 level = TestLevel.PARTIAL_COMPLETE, 962 notes = "Verifies SecurityException.", 963 method = "setSocketImplFactory", 964 args = {java.net.SocketImplFactory.class} 965 ) 966 public void test_setSocketImplFactoryLjava_net_SocketImplFactory() { 967 // Test for method void 968 // java.net.Socket.setSocketImplFactory(java.net.SocketImplFactory) 969 970 // Cannot test as setting will cause the factory to be changed for 971 // all subsequent sockets 972 973 SecurityManager sm = new SecurityManager() { 974 975 public void checkPermission(Permission perm) { 976 } 977 978 public void checkSetFactory() { 979 throw new SecurityException(); 980 } 981 }; 982 983 SecurityManager oldSm = System.getSecurityManager(); 984 System.setSecurityManager(sm); 985 try { 986 Socket.setSocketImplFactory(null); 987 fail("SecurityException should be thrown."); 988 } catch (SecurityException e) { 989 // expected 990 } catch (IOException e) { 991 fail("IOException was thrown."); 992 } finally { 993 System.setSecurityManager(oldSm); 994 } 995 996 } 997 998 /** 999 * @tests java.net.Socket#setSendBufferSize(int) 1000 */ 1001 @TestTargetNew( 1002 level = TestLevel.COMPLETE, 1003 notes = "", 1004 method = "setSendBufferSize", 1005 args = {int.class} 1006 ) 1007 public void test_setSendBufferSizeI() { 1008 try { 1009 int sport = startServer("SServer setSendBufferSizeI"); 1010 int portNumber = Support_PortManager.getNextPort(); 1011 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1012 s.setSendBufferSize(134); 1013 assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134); 1014 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF); 1015 } catch (Exception e) { 1016 handleException(e, SO_SNDBUF); 1017 } 1018 1019 try { 1020 Socket theSocket = new Socket(); 1021 theSocket.close(); 1022 theSocket.setSendBufferSize(1); 1023 fail("SocketException was not thrown."); 1024 } catch(SocketException ioe) { 1025 //expected 1026 } catch(IOException ioe) { 1027 fail("IOException was thrown."); 1028 } 1029 } 1030 1031 /** 1032 * @tests java.net.Socket#setReceiveBufferSize(int) 1033 */ 1034 @TestTargetNew( 1035 level = TestLevel.COMPLETE, 1036 notes = "", 1037 method = "setReceiveBufferSize", 1038 args = {int.class} 1039 ) 1040 public void test_setReceiveBufferSizeI() { 1041 try { 1042 int sport = startServer("SServer setReceiveBufferSizeI"); 1043 int portNumber = Support_PortManager.getNextPort(); 1044 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1045 s.setReceiveBufferSize(130); 1046 assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130); 1047 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 1048 } catch (Exception e) { 1049 handleException(e, SO_RCVBUF); 1050 } 1051 1052 try { 1053 Socket theSocket = new Socket(); 1054 theSocket.close(); 1055 theSocket.setReceiveBufferSize(1); 1056 fail("SocketException was not thrown."); 1057 } catch(SocketException ioe) { 1058 //expected 1059 } catch(IOException ioe) { 1060 fail("IOException was thrown."); 1061 } 1062 } 1063 1064 /** 1065 * @tests java.net.Socket#setSoLinger(boolean, int) 1066 */ 1067 @TestTargetNew( 1068 level = TestLevel.COMPLETE, 1069 notes = "", 1070 method = "setSoLinger", 1071 args = {boolean.class, int.class} 1072 ) 1073 public void test_setSoLingerZI() { 1074 // Test for method void java.net.Socket.setSoLinger(boolean, int) 1075 try { 1076 int sport = startServer("SServer setSoLingerZI"); 1077 int portNumber = Support_PortManager.getNextPort(); 1078 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1079 s.setSoLinger(true, 500); 1080 assertEquals("Set incorrect linger", 500, s.getSoLinger()); 1081 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER); 1082 s.setSoLinger(false, 0); 1083 } catch (Exception e) { 1084 handleException(e, SO_LINGER); 1085 } 1086 1087 try { 1088 Socket theSocket = new Socket(); 1089 theSocket.close(); 1090 theSocket.setSoLinger(true, 1); 1091 fail("SocketException was not thrown."); 1092 } catch(SocketException ioe) { 1093 //expected 1094 } catch(IOException ioe) { 1095 fail("IOException was thrown."); 1096 } 1097 } 1098 1099 /** 1100 * @tests java.net.Socket#setSoTimeout(int) 1101 */ 1102 @TestTargetNew( 1103 level = TestLevel.COMPLETE, 1104 notes = "", 1105 method = "setSoTimeout", 1106 args = {int.class} 1107 ) 1108 public void test_setSoTimeoutI() { 1109 // Test for method void java.net.Socket.setSoTimeout(int) 1110 try { 1111 int sport = startServer("SServer seSoTimeoutI"); 1112 int portNumber = Support_PortManager.getNextPort(); 1113 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1114 s.setSoTimeout(100); 1115 assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout()); 1116 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT); 1117 } catch (Exception e) { 1118 handleException(e, SO_TIMEOUT); 1119 } 1120 1121 try { 1122 Socket theSocket = new Socket(); 1123 theSocket.close(); 1124 theSocket.setSoTimeout(1); 1125 fail("SocketException was not thrown."); 1126 } catch(SocketException ioe) { 1127 //expected 1128 } catch(IOException ioe) { 1129 fail("IOException was thrown."); 1130 } 1131 } 1132 1133 /** 1134 * @tests java.net.Socket#setTcpNoDelay(boolean) 1135 */ 1136 @TestTargetNew( 1137 level = TestLevel.COMPLETE, 1138 notes = "", 1139 method = "setTcpNoDelay", 1140 args = {boolean.class} 1141 ) 1142 public void test_setTcpNoDelayZ() { 1143 // Test for method void java.net.Socket.setTcpNoDelay(boolean) 1144 try { 1145 int sport = startServer("SServer setTcpNoDelayZ"); 1146 int portNumber = Support_PortManager.getNextPort(); 1147 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1148 boolean bool; 1149 s.setTcpNoDelay(bool = !s.getTcpNoDelay()); 1150 assertTrue("Failed to set no delay setting: " + s.getTcpNoDelay(), 1151 s.getTcpNoDelay() == bool); 1152 ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY); 1153 } catch (Exception e) { 1154 handleException(e, TCP_NODELAY); 1155 } 1156 1157 try { 1158 Socket theSocket = new Socket(); 1159 theSocket.close(); 1160 theSocket.setTcpNoDelay(true); 1161 fail("SocketException was not thrown."); 1162 } catch(SocketException ioe) { 1163 //expected 1164 } catch(IOException ioe) { 1165 fail("IOException was thrown."); 1166 } 1167 } 1168 1169 /** 1170 * @tests java.net.Socket#toString() 1171 */ 1172 @TestTargetNew( 1173 level = TestLevel.COMPLETE, 1174 notes = "", 1175 method = "toString", 1176 args = {} 1177 ) 1178 public void test_toString() throws IOException { 1179 // Test for method java.lang.String java.net.Socket.toString() 1180 int sport = startServer("SServer toString"); 1181 int portNumber = Support_PortManager.getNextPort(); 1182 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 1183 InetAddress.getLocalHost(), portNumber); 1184 assertTrue("Returned incorrect string: " + s.toString() 1185 + " localHost: " + InetAddress.getLocalHost(), s.toString() 1186 .equals( 1187 "Socket[addr=" + InetAddress.getLocalHost() + ",port=" 1188 + s.getPort() + ",localport=" 1189 + s.getLocalPort() + "]")); 1190 } 1191 1192 /** 1193 * @tests java.net.Socket#shutdownInput() 1194 */ 1195 @TestTargetNew( 1196 level = TestLevel.COMPLETE, 1197 notes = "", 1198 method = "shutdownInput", 1199 args = {} 1200 ) 1201 @AndroidOnly("RI returns wrong value for EOF") 1202 public void test_shutdownInput() throws Exception { 1203 InetAddress addr = InetAddress.getLocalHost(); 1204 int port = Support_PortManager.getNextPort(); 1205 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1206 Socket theSocket = new Socket(addr, port); 1207 Socket servSock = serverSocket.accept(); 1208 1209 InputStream theInput = theSocket.getInputStream(); 1210 OutputStream theOutput = servSock.getOutputStream(); 1211 1212 // shutdown the input 1213 theSocket.shutdownInput(); 1214 1215 // send the regular data 1216 String sendString = new String("Test"); 1217 theOutput.write(sendString.getBytes()); 1218 theOutput.flush(); 1219 1220 // give things some time to settle 1221 Thread.sleep(1000); 1222 1223 // RI fails here. It is a RI bug not to return 0 to indicate EOF 1224 assertEquals(0, theInput.available()); 1225 1226 theSocket.close(); 1227 serverSocket.close(); 1228 1229 Socket socket = new Socket(); 1230 socket.close(); 1231 try { 1232 socket.shutdownInput(); 1233 fail("IOException was not thrown."); 1234 } catch(IOException ioe) { 1235 //expected 1236 } 1237 } 1238 1239 /** 1240 * @tests java.net.Socket#shutdownOutput() 1241 */ 1242 @TestTargetNew( 1243 level = TestLevel.COMPLETE, 1244 notes = "", 1245 method = "shutdownOutput", 1246 args = {} 1247 ) 1248 public void test_shutdownOutput() throws IOException { 1249 InetAddress addr = InetAddress.getLocalHost(); 1250 int port = Support_PortManager.getNextPort(); 1251 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1252 Socket theSocket = new Socket(addr, port); 1253 Socket servSock = serverSocket.accept(); 1254 1255 InputStream theInput = theSocket.getInputStream(); 1256 OutputStream theOutput = servSock.getOutputStream(); 1257 1258 // shutdown the output 1259 servSock.shutdownOutput(); 1260 1261 // send the regular data 1262 String sendString = new String("Test"); 1263 try { 1264 theOutput.write(sendString.getBytes()); 1265 theOutput.flush(); 1266 fail("No exception when writing on socket with output shutdown"); 1267 } catch (Exception e) { 1268 } 1269 1270 theSocket.close(); 1271 serverSocket.close(); 1272 1273 try { 1274 theSocket.shutdownInput(); 1275 fail("IOException was not thrown."); 1276 } catch(IOException ioe) { 1277 //expected 1278 } 1279 } 1280 1281 /** 1282 * @tests java.net.Socket#getLocalSocketAddress() 1283 */ 1284 @TestTargetNew( 1285 level = TestLevel.COMPLETE, 1286 notes = "", 1287 method = "getLocalSocketAddress", 1288 args = {} 1289 ) 1290 public void test_getLocalSocketAddress() throws IOException { 1291 // set up server connect and then validate that we get the right 1292 // response for the local address 1293 int sport = startServer("SServer getLocSocketAddress"); 1294 int portNumber = Support_PortManager.getNextPort(); 1295 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1296 assertTrue( 1297 "Returned incorrect InetSocketAddress(1):" 1298 + s.getLocalSocketAddress().toString() 1299 + "Expected: " 1300 + (new InetSocketAddress(InetAddress.getLocalHost(), 1301 portNumber)).toString(), s 1302 .getLocalSocketAddress().equals( 1303 new InetSocketAddress(InetAddress 1304 .getLocalHost(), portNumber))); 1305 s.close(); 1306 1307 // now create a socket that is not bound and validate we get the 1308 // right answer 1309 Socket theSocket = new Socket(); 1310 assertNull( 1311 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 1312 theSocket.getLocalSocketAddress()); 1313 1314 // now bind the socket and make sure we get the right answer 1315 portNumber = Support_PortManager.getNextPort(); 1316 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1317 portNumber)); 1318 assertTrue( 1319 "Returned incorrect InetSocketAddress(2):" 1320 + theSocket.getLocalSocketAddress().toString() 1321 + "Expected: " 1322 + (new InetSocketAddress(InetAddress.getLocalHost(), 1323 portNumber)).toString(), theSocket 1324 .getLocalSocketAddress().equals( 1325 new InetSocketAddress(InetAddress 1326 .getLocalHost(), portNumber))); 1327 theSocket.close(); 1328 1329 // now validate thet behaviour when the any address is returned 1330 s = new Socket(); 1331 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 1332 1333 String preferIPv4StackValue = System 1334 .getProperty("java.net.preferIPv4Stack"); 1335 String preferIPv6AddressesValue = System 1336 .getProperty("java.net.preferIPv6Addresses"); 1337 if (((preferIPv4StackValue == null) || preferIPv4StackValue 1338 .equalsIgnoreCase("false")) 1339 && (preferIPv6AddressesValue != null) 1340 && (preferIPv6AddressesValue.equals("true"))) { 1341 assertTrue( 1342 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " 1343 + s.getLocalSocketAddress(), 1344 ((InetSocketAddress) s.getLocalSocketAddress()) 1345 .getAddress() instanceof Inet6Address); 1346 } else { 1347 assertTrue( 1348 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " 1349 + s.getLocalSocketAddress(), 1350 ((InetSocketAddress) s.getLocalSocketAddress()) 1351 .getAddress() instanceof Inet4Address); 1352 } 1353 s.close(); 1354 1355 // now validate the same for getLocalAddress 1356 s = new Socket(); 1357 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 1358 if (((preferIPv4StackValue == null) || preferIPv4StackValue 1359 .equalsIgnoreCase("false")) 1360 && (preferIPv6AddressesValue != null) 1361 && (preferIPv6AddressesValue.equals("true"))) { 1362 assertTrue( 1363 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " 1364 + s.getLocalSocketAddress(), 1365 ((InetSocketAddress) s.getLocalSocketAddress()) 1366 .getAddress() instanceof Inet6Address); 1367 } else { 1368 assertTrue( 1369 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " 1370 + s.getLocalSocketAddress(), 1371 ((InetSocketAddress) s.getLocalSocketAddress()) 1372 .getAddress() instanceof Inet4Address); 1373 } 1374 s.close(); 1375 } 1376 1377 /** 1378 * @tests java.net.Socket#getRemoteSocketAddress() 1379 */ 1380 @TestTargetNew( 1381 level = TestLevel.COMPLETE, 1382 notes = "", 1383 method = "getRemoteSocketAddress", 1384 args = {} 1385 ) 1386 public void test_getRemoteSocketAddress() throws IOException { 1387 // set up server connect and then validate that we get the right 1388 // response for the remote address 1389 int sport = startServer("SServer getLocRemoteAddress"); 1390 int portNumber = Support_PortManager.getNextPort(); 1391 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 1392 assertTrue("Returned incorrect InetSocketAddress(1):" 1393 + s.getLocalSocketAddress().toString(), 1394 s.getRemoteSocketAddress() 1395 .equals( 1396 new InetSocketAddress(InetAddress 1397 .getLocalHost(), sport))); 1398 s.close(); 1399 1400 // now create one that is not connect and validate that we get the 1401 // right answer 1402 Socket theSocket = new Socket(); 1403 portNumber = Support_PortManager.getNextPort(); 1404 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1405 portNumber)); 1406 1407 assertNull("Returned incorrect InetSocketAddress -unconnected socket:" 1408 + "Expected: NULL", theSocket.getRemoteSocketAddress()); 1409 1410 // now connect and validate we get the right answer 1411 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1412 sport)); 1413 assertTrue("Returned incorrect InetSocketAddress(2):" 1414 + theSocket.getRemoteSocketAddress().toString(), 1415 theSocket.getRemoteSocketAddress() 1416 .equals( 1417 new InetSocketAddress(InetAddress 1418 .getLocalHost(), sport))); 1419 theSocket.close(); 1420 1421 } 1422 1423 /** 1424 * @tests java.net.Socket#isBound() 1425 */ 1426 @TestTargetNew( 1427 level = TestLevel.COMPLETE, 1428 notes = "", 1429 method = "isBound", 1430 args = {} 1431 ) 1432 public void test_isBound() throws IOException { 1433 InetAddress addr = InetAddress.getLocalHost(); 1434 int port = Support_PortManager.getNextPort(); 1435 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1436 Socket theSocket = new Socket(addr, port); 1437 Socket servSock = serverSocket.accept(); 1438 assertTrue("Socket indicated not bound when it should be (1)", 1439 theSocket.isBound()); 1440 theSocket.close(); 1441 serverSocket.close(); 1442 1443 // now do it with the new constructors and revalidate. Connect causes 1444 // the socket to be bound 1445 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1446 .getLocalHost(), Support_PortManager.getNextPort()); 1447 theSocket = new Socket(); 1448 assertFalse("Socket indicated bound when it was not (2)", theSocket 1449 .isBound()); 1450 serverSocket = new ServerSocket(); 1451 serverSocket.bind(theAddress); 1452 theSocket.connect(theAddress); 1453 servSock = serverSocket.accept(); 1454 assertTrue("Socket indicated not bound when it should be (2)", 1455 theSocket.isBound()); 1456 theSocket.close(); 1457 serverSocket.close(); 1458 1459 // now test when we bind explicitely 1460 InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress 1461 .getLocalHost(), Support_PortManager.getNextPort()); 1462 theSocket = new Socket(); 1463 assertFalse("Socket indicated bound when it was not (3)", theSocket 1464 .isBound()); 1465 theSocket.bind(theLocalAddress); 1466 assertTrue("Socket indicated not bound when it should be (3a)", 1467 theSocket.isBound()); 1468 theSocket.close(); 1469 assertTrue("Socket indicated not bound when it should be (3b)", 1470 theSocket.isBound()); 1471 } 1472 1473 /** 1474 * @tests java.net.Socket#isConnected() 1475 */ 1476 @TestTargetNew( 1477 level = TestLevel.COMPLETE, 1478 notes = "", 1479 method = "isConnected", 1480 args = {} 1481 ) 1482 public void test_isConnected() throws IOException { 1483 InetAddress addr = InetAddress.getLocalHost(); 1484 int port = Support_PortManager.getNextPort(); 1485 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1486 Socket theSocket = new Socket(addr, port); 1487 Socket servSock = serverSocket.accept(); 1488 assertTrue("Socket indicated not connected when it should be", 1489 theSocket.isConnected()); 1490 theSocket.close(); 1491 serverSocket.close(); 1492 1493 // now do it with the new constructors and revalidate 1494 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1495 .getLocalHost(), Support_PortManager.getNextPort()); 1496 theSocket = new Socket(); 1497 assertFalse("Socket indicated connected when it was not", theSocket 1498 .isConnected()); 1499 serverSocket = new ServerSocket(); 1500 serverSocket.bind(theAddress); 1501 theSocket.connect(theAddress); 1502 servSock = serverSocket.accept(); 1503 assertTrue("Socket indicated not connected when it should be", 1504 theSocket.isConnected()); 1505 theSocket.close(); 1506 serverSocket.close(); 1507 } 1508 1509 /** 1510 * @tests java.net.Socket#isClosed() 1511 */ 1512 @TestTargetNew( 1513 level = TestLevel.COMPLETE, 1514 notes = "", 1515 method = "isClosed", 1516 args = {} 1517 ) 1518 public void test_isClosed() throws IOException { 1519 InetAddress addr = InetAddress.getLocalHost(); 1520 int port = Support_PortManager.getNextPort(); 1521 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1522 Socket theSocket = new Socket(addr, port); 1523 Socket servSock = serverSocket.accept(); 1524 1525 // validate isClosed returns expected values 1526 assertFalse("Socket should indicate it is not closed(1):", theSocket 1527 .isClosed()); 1528 theSocket.close(); 1529 assertTrue("Socket should indicate it is closed(1):", theSocket 1530 .isClosed()); 1531 1532 theSocket = new Socket(addr, port); 1533 assertFalse("Socket should indicate it is not closed(2):", theSocket 1534 .isClosed()); 1535 theSocket.close(); 1536 assertTrue("Socket should indicate it is closed(2):", theSocket 1537 .isClosed()); 1538 1539 // validate that isClosed works ok for sockets returned from 1540 // ServerSocket.accept() 1541 assertFalse("Server Socket should indicate it is not closed:", servSock 1542 .isClosed()); 1543 servSock.close(); 1544 assertTrue("Server Socket should indicate it is closed:", servSock 1545 .isClosed()); 1546 } 1547 1548 /** 1549 * @tests java.net.Socket#bind(java.net.SocketAddress) 1550 */ 1551 @TestTargetNew( 1552 level = TestLevel.COMPLETE, 1553 notes = "", 1554 method = "bind", 1555 args = {java.net.SocketAddress.class} 1556 ) 1557 public void test_bindLjava_net_SocketAddress() throws IOException { 1558 1559 class mySocketAddress extends SocketAddress { 1560 1561 public mySocketAddress() { 1562 } 1563 } 1564 1565 // Address we cannot bind to 1566 Socket theSocket = new Socket(); 1567 try { 1568 theSocket.bind(new InetSocketAddress(InetAddress 1569 .getByAddress(Support_Configuration.nonLocalAddressBytes), 1570 Support_PortManager.getNextPort())); 1571 fail("No exception when binding to bad address:" 1572 + theSocket.getLocalSocketAddress().toString()); 1573 } catch (IOException ex) { 1574 } 1575 theSocket.close(); 1576 1577 // now create a socket that is not bound and then bind it 1578 theSocket = new Socket(); 1579 int portNumber = Support_PortManager.getNextPort(); 1580 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1581 portNumber)); 1582 1583 // validate that the localSocketAddress reflects the address we 1584 // bound to 1585 assertTrue( 1586 "Local address not correct after bind:" 1587 + theSocket.getLocalSocketAddress().toString() 1588 + " Expected: " 1589 + (new InetSocketAddress(InetAddress.getLocalHost(), 1590 portNumber)).toString(), theSocket 1591 .getLocalSocketAddress().equals( 1592 new InetSocketAddress(InetAddress 1593 .getLocalHost(), portNumber))); 1594 1595 // make sure we can now connect and that connections appear to come 1596 // from the address we bound to. 1597 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1598 .getLocalHost(), Support_PortManager.getNextPort()); 1599 ServerSocket serverSocket = new ServerSocket(); 1600 serverSocket.bind(theAddress); 1601 theSocket.connect(theAddress); 1602 Socket servSock = serverSocket.accept(); 1603 assertTrue( 1604 "Returned Remote address from server connected to does not match expected local address:" 1605 + servSock.getRemoteSocketAddress().toString() 1606 + " Expected: " 1607 + (new InetSocketAddress(InetAddress.getLocalHost(), 1608 portNumber)).toString(), servSock 1609 .getRemoteSocketAddress().equals( 1610 new InetSocketAddress(InetAddress 1611 .getLocalHost(), portNumber))); 1612 theSocket.close(); 1613 servSock.close(); 1614 serverSocket.close(); 1615 1616 // validate if we pass in null that it picks an address for us and 1617 // all is ok 1618 theSocket = new Socket(); 1619 theSocket.bind(null); 1620 assertNotNull("Bind with null did not work", theSocket 1621 .getLocalSocketAddress()); 1622 theSocket.close(); 1623 1624 // now check the error conditions 1625 1626 // Address that we have allready bound to 1627 theSocket = new Socket(); 1628 Socket theSocket2 = new Socket(); 1629 try { 1630 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1631 Support_PortManager.getNextPort()); 1632 theSocket.bind(theAddress); 1633 theSocket2.bind(theAddress); 1634 fail("No exception binding to address that is not available"); 1635 } catch (IOException ex) { 1636 } 1637 theSocket.close(); 1638 theSocket2.close(); 1639 1640 // unsupported SocketAddress subclass 1641 theSocket = new Socket(); 1642 try { 1643 theSocket.bind(new mySocketAddress()); 1644 fail("No exception when binding using unsupported SocketAddress subclass"); 1645 } catch (IllegalArgumentException ex) { 1646 } 1647 theSocket.close(); 1648 } 1649 1650 /** 1651 * @tests java.net.Socket#bind(java.net.SocketAddress) 1652 */ 1653 @TestTargetNew( 1654 level = TestLevel.ADDITIONAL, 1655 notes = "Checks bind on proxy.", 1656 method = "bind", 1657 args = {java.net.SocketAddress.class} 1658 ) 1659 public void test_bindLjava_net_SocketAddress_Proxy() throws IOException { 1660 //The Proxy will not impact on the bind operation.It can be assigned with any address. 1661 Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 0)); 1662 Socket socket = new Socket(proxy); 1663 1664 try { 1665 InetAddress address = InetAddress.getByName("localhost"); 1666 int port = 0; 1667 socket.bind(new InetSocketAddress(address, port)); 1668 1669 assertEquals(address, socket.getLocalAddress()); 1670 assertTrue(port!=socket.getLocalPort()); 1671 1672 } finally { 1673 socket.close(); 1674 } 1675 } 1676 1677 /** 1678 * @tests java.net.Socket#connect(java.net.SocketAddress) 1679 */ 1680 @TestTargetNew( 1681 level = TestLevel.COMPLETE, 1682 notes = "", 1683 method = "connect", 1684 args = {java.net.SocketAddress.class} 1685 ) 1686 public void test_connectLjava_net_SocketAddress() throws Exception { 1687 // needed for some tests 1688 class mySocketAddress extends SocketAddress { 1689 1690 public mySocketAddress() { 1691 } 1692 } 1693 1694 class SocketCloser extends Thread { 1695 1696 int timeout = 0; 1697 1698 Socket theSocket = null; 1699 1700 public void run() { 1701 try { 1702 Thread.sleep(timeout); 1703 theSocket.close(); 1704 } catch (Exception e) { 1705 } 1706 ; 1707 return; 1708 } 1709 1710 public SocketCloser(int timeout, Socket theSocket) { 1711 this.timeout = timeout; 1712 this.theSocket = theSocket; 1713 } 1714 } 1715 1716 // start by validating the error checks 1717 int portNumber = Support_PortManager.getNextPort(); 1718 Socket theSocket = null; 1719 ServerSocket serverSocket = null; 1720 SocketAddress theAddress = null; 1721 SocketAddress nonConnectableAddress = null; 1722 SocketAddress nonReachableAddress = null; 1723 SocketAddress invalidType = null; 1724 // byte[] theBytes = {-1,-1,-1,-1}; 1725 byte[] theBytes = { 0, 0, 0, 0 }; 1726 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1727 portNumber); 1728 nonConnectableAddress = new InetSocketAddress(InetAddress 1729 .getByAddress(theBytes), portNumber); 1730 nonReachableAddress = new InetSocketAddress(InetAddress 1731 .getByName(Support_Configuration.ResolvedNotExistingHost), 1732 portNumber); 1733 1734 invalidType = new mySocketAddress(); 1735 1736 try { 1737 theSocket = new Socket(); 1738 theSocket.connect(null); 1739 fail("No exception after null address passed in"); 1740 } catch (Exception e) { 1741 assertTrue("Wrong exception null address passed in: " 1742 + e.toString(), (e instanceof IllegalArgumentException)); 1743 } 1744 1745 try { 1746 theSocket = new Socket(); 1747 theSocket.connect(invalidType); 1748 fail("No exception when invalid socket address type passed in: "); 1749 } catch (Exception e) { 1750 assertTrue( 1751 "Wrong exception when when invalid socket address type passed in: " 1752 + e.toString(), 1753 (e instanceof IllegalArgumentException)); 1754 } 1755 1756 try { 1757 theSocket = new Socket(); 1758 theSocket.connect(nonConnectableAddress); 1759 fail("No exception when non Connectable Address passed in: "); 1760 } catch (Exception e) { 1761 assertTrue( 1762 "Wrong exception when non Connectable Address passed in: " 1763 + e.toString(), (e instanceof ConnectException)); 1764 } 1765 1766 // now validate that we get a connect exception if we try to connect to 1767 // an address on which nobody is listening 1768 try { 1769 theSocket = new Socket(); 1770 theSocket.connect(theAddress); 1771 theSocket.close(); 1772 fail("No exception when connecting to address nobody listening on: "); 1773 } catch (Exception e) { 1774 assertTrue( 1775 "Wrong exception when connecting to address nobody listening on: " 1776 + e.toString(), (e instanceof ConnectException)); 1777 } 1778 1779 // now validate that we can acutally connect when sombody is listening 1780 theSocket = new Socket(); 1781 serverSocket = new ServerSocket(); 1782 serverSocket.bind(theAddress); 1783 theSocket.connect(theAddress); 1784 theSocket.close(); 1785 serverSocket.close(); 1786 1787 // now validate that we can acutally connect when sombody is listening 1788 theSocket = new Socket(); 1789 serverSocket = new ServerSocket(); 1790 serverSocket.bind(theAddress); 1791 theSocket.connect(theAddress); 1792 1793 // validate that when a socket is connected that it answers 1794 // correctly to related queries 1795 assertTrue("Socket did not returned connected when it is: ", theSocket 1796 .isConnected()); 1797 assertFalse("Socket returned closed when it should be connected ", 1798 theSocket.isClosed()); 1799 assertTrue("Socket returned not bound when it should be: ", theSocket 1800 .isBound()); 1801 assertFalse( 1802 "Socket returned input Shutdown when it should be connected ", 1803 theSocket.isInputShutdown()); 1804 assertFalse( 1805 "Socket returned output Shutdown when it should be connected ", 1806 theSocket.isOutputShutdown()); 1807 assertTrue("Local port on connected socket was 0", theSocket 1808 .getLocalPort() != 0); 1809 theSocket.close(); 1810 serverSocket.close(); 1811 1812 // now validate that we get the right exception if we connect when we 1813 // are already connected 1814 try { 1815 theSocket = new Socket(); 1816 serverSocket = new ServerSocket(); 1817 serverSocket.bind(theAddress); 1818 theSocket.connect(theAddress); 1819 theSocket.connect(theAddress); 1820 theSocket.close(); 1821 serverSocket.close(); 1822 fail("No exception when we try to connect on a connected socket: "); 1823 1824 } catch (Exception e) { 1825 assertTrue( 1826 "Wrong exception when connecting on socket that is allready connected" 1827 + e.toString(), (e instanceof SocketException)); 1828 assertFalse( 1829 "Wrong exception when connecting on socket that is allready connected" 1830 + e.toString(), 1831 (e instanceof SocketTimeoutException)); 1832 try { 1833 theSocket.close(); 1834 serverSocket.close(); 1835 } catch (Exception e2) { 1836 } 1837 1838 } 1839 1840 // now validate that connected socket can be used to read/write 1841 theSocket = new Socket(); 1842 serverSocket = new ServerSocket(); 1843 serverSocket.bind(theAddress); 1844 theSocket.connect(theAddress); 1845 Socket servSock = serverSocket.accept(); 1846 InputStream theInput = theSocket.getInputStream(); 1847 OutputStream theOutput = servSock.getOutputStream(); 1848 InputStream theInput2 = servSock.getInputStream(); 1849 OutputStream theOutput2 = theSocket.getOutputStream(); 1850 1851 String sendString = new String("Test"); 1852 theOutput.write(sendString.getBytes()); 1853 theOutput.flush(); 1854 1855 Thread.sleep(1000); 1856 1857 int totalBytesRead = 0; 1858 byte[] myBytes = new byte[100]; 1859 while (theInput.available() > 0) { 1860 int bytesRead = theInput.read(myBytes, totalBytesRead, 1861 myBytes.length - totalBytesRead); 1862 totalBytesRead = totalBytesRead + bytesRead; 1863 } 1864 1865 String receivedString = new String(myBytes, 0, totalBytesRead); 1866 assertTrue("Could not recv on socket connected with timeout:" 1867 + receivedString + ":" + sendString, receivedString 1868 .equals(sendString)); 1869 1870 sendString = new String("SEND - Test"); 1871 theOutput2.write(sendString.getBytes()); 1872 theOutput2.flush(); 1873 Thread.sleep(1000); 1874 1875 totalBytesRead = 0; 1876 myBytes = new byte[100]; 1877 while (theInput2.available() > 0) { 1878 int bytesRead = theInput2.read(myBytes, totalBytesRead, 1879 myBytes.length - totalBytesRead); 1880 totalBytesRead = totalBytesRead + bytesRead; 1881 } 1882 1883 receivedString = new String(myBytes, 0, totalBytesRead); 1884 assertTrue("Could not send on socket connected with timeout:" 1885 + receivedString + ":" + sendString, receivedString 1886 .equals(sendString)); 1887 1888 theSocket.close(); 1889 serverSocket.close(); 1890 1891 SocketChannel channel = SocketChannel.open(); 1892 channel.configureBlocking(false); 1893 Socket socket = channel.socket(); 1894 int port = Support_PortManager.getNextPort(); 1895 try { 1896 socket.connect( new InetSocketAddress(InetAddress.getLocalHost(), 1897 Support_PortManager.getNextPort())); 1898 fail("IllegalBlockingModeException was not thrown."); 1899 } catch(IllegalBlockingModeException ibme) { 1900 //expected 1901 } 1902 } 1903 1904 /** 1905 * @tests java.net.Socket#connect(java.net.SocketAddress, int) 1906 */ 1907 @TestTargetNew( 1908 level = TestLevel.COMPLETE, 1909 notes = "", 1910 method = "connect", 1911 args = {java.net.SocketAddress.class, int.class} 1912 ) 1913 public void test_connectLjava_net_SocketAddressI() throws Exception { 1914 1915 // needed for some tests 1916 class mySocketAddress extends SocketAddress { 1917 1918 public mySocketAddress() { 1919 } 1920 } 1921 1922 class SocketCloser extends Thread { 1923 1924 int timeout = 0; 1925 1926 Socket theSocket = null; 1927 1928 public void run() { 1929 try { 1930 Thread.sleep(timeout); 1931 theSocket.close(); 1932 } catch (Exception e) { 1933 } 1934 return; 1935 } 1936 1937 public SocketCloser(int timeout, Socket theSocket) { 1938 this.timeout = timeout; 1939 this.theSocket = theSocket; 1940 } 1941 } 1942 1943 class SocketConnector extends Thread { 1944 1945 int timeout = 0; 1946 1947 Socket theSocket = null; 1948 1949 SocketAddress address = null; 1950 1951 public void run() { 1952 try { 1953 theSocket.connect(address, timeout); 1954 } catch (Exception e) { 1955 } 1956 1957 return; 1958 } 1959 1960 public SocketConnector(int timeout, Socket theSocket, 1961 SocketAddress address) { 1962 this.timeout = timeout; 1963 this.theSocket = theSocket; 1964 this.address = address; 1965 } 1966 } 1967 1968 // start by validating the error checks 1969 int portNumber = Support_PortManager.getNextPort(); 1970 Socket theSocket = null; 1971 ServerSocket serverSocket = null; 1972 SocketAddress theAddress = null; 1973 SocketAddress nonConnectableAddress = null; 1974 SocketAddress nonReachableAddress = null; 1975 SocketAddress nonListeningAddress = null; 1976 SocketAddress invalidType = null; 1977 byte[] theBytes = { 0, 0, 0, 0 }; 1978 1979 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1980 portNumber); 1981 nonConnectableAddress = new InetSocketAddress(InetAddress 1982 .getByAddress(theBytes), portNumber); 1983 nonReachableAddress = new InetSocketAddress(InetAddress 1984 .getByName(Support_Configuration.ResolvedNotExistingHost), 1985 portNumber); 1986 // make sure we get another port 1987 Thread.sleep(7000); 1988 nonListeningAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1989 Support_PortManager.getNextPort()); 1990 invalidType = new mySocketAddress(); 1991 1992 try { 1993 theSocket = new Socket(); 1994 theSocket.connect(theAddress, -100); 1995 fail("No exception after negative timeout passed in"); 1996 } catch (Exception e) { 1997 assertTrue("Wrong exception when negative timeout passed in: " 1998 + e.toString(), (e instanceof IllegalArgumentException)); 1999 } 2000 2001 try { 2002 theSocket = new Socket(); 2003 theSocket.connect(null, 0); 2004 fail("No exception after null address passed in"); 2005 } catch (Exception e) { 2006 assertTrue("Wrong exception null address passed in: " 2007 + e.toString(), (e instanceof IllegalArgumentException)); 2008 } 2009 2010 try { 2011 theSocket = new Socket(); 2012 theSocket.connect(invalidType, 100000); 2013 fail("No exception when invalid socket address type passed in: "); 2014 } catch (Exception e) { 2015 assertTrue( 2016 "Wrong exception when when invalid socket address type passed in: " 2017 + e.toString(), 2018 (e instanceof IllegalArgumentException)); 2019 } 2020 2021 try { 2022 theSocket = new Socket(); 2023 theSocket.connect(nonConnectableAddress, 100000); 2024 fail("No exception when non Connectable Address passed in: "); 2025 } catch (Exception e) { 2026 assertTrue( 2027 "Wrong exception when non Connectable Address passed in: " 2028 + e.toString(), (e instanceof SocketException)); 2029 } 2030 2031 // now validate that we get a connect exception if we try to connect to 2032 // an address on which nobody is listening 2033 try { 2034 theSocket = new Socket(); 2035 theSocket.connect(theAddress, 0); 2036 theSocket.close(); 2037 fail("No timeout:No exception when connecting to address nobody listening on: "); 2038 } catch (Exception e) { 2039 assertTrue( 2040 "No timeout:Wrong exception when connecting to address nobody listening on: " 2041 + e.toString(), (e instanceof ConnectException)); 2042 } 2043 2044 // now validate that we can acutally connect when sombody is listening 2045 theSocket = new Socket(); 2046 serverSocket = new ServerSocket(); 2047 serverSocket.bind(theAddress); 2048 theSocket.connect(theAddress, 0); 2049 theSocket.close(); 2050 serverSocket.close(); 2051 2052 // now validate that we get a connect exception if we try to connect to 2053 // an address on which nobody is listening 2054 try { 2055 theSocket = new Socket(); 2056 theSocket.connect(nonListeningAddress, 100000); 2057 theSocket.close(); 2058 fail("No exception when connecting to address nobody listening on: "); 2059 } catch (Exception e) { 2060 assertTrue( 2061 "Wrong exception when connecting to address nobody listening on: " 2062 + e.toString(), (e instanceof ConnectException)); 2063 } 2064 2065 // now validate that we get a interrupted exception if we try to connect 2066 // to an address on which nobody is accepting connections and the 2067 // timeout expired 2068 try { 2069 theSocket = new Socket(); 2070 theSocket.connect(nonReachableAddress, 200); 2071 theSocket.close(); 2072 fail("No interrupted exception when connecting to address nobody listening on with short timeout 200: "); 2073 } catch (Exception e) { 2074 assertTrue( 2075 "Wrong exception when connecting to address nobody listening on with short timeout 200: " 2076 + e.toString(), 2077 (e instanceof SocketTimeoutException)); 2078 } 2079 2080 // now validate that we get a interrupted exception if we try to connect 2081 // to an address on which nobody is accepting connections and the 2082 // timeout expired 2083 try { 2084 theSocket = new Socket(); 2085 theSocket.connect(nonReachableAddress, 40); 2086 theSocket.close(); 2087 fail("No interrupted exception when connecting to address nobody listening on with short timeout 40: "); 2088 } catch (Exception e) { 2089 assertTrue( 2090 "Wrong exception when connecting to address nobody listening on with short timeout 40: " 2091 + e.toString(), 2092 (e instanceof SocketTimeoutException)); 2093 } 2094 2095 // now validate that we can acutally connect when sombody is listening 2096 new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager 2097 .getNextPort()); 2098 theSocket = new Socket(); 2099 serverSocket = new ServerSocket(); 2100 serverSocket.bind(theAddress); 2101 theSocket.connect(theAddress, 100000); 2102 2103 // validate that when a socket is connected that it answers 2104 // correctly to related queries 2105 assertTrue("Socket did not returned connected when it is: ", theSocket 2106 .isConnected()); 2107 assertFalse("Socket returned closed when it should be connected ", 2108 theSocket.isClosed()); 2109 assertTrue("Socket returned not bound when it should be: ", theSocket 2110 .isBound()); 2111 assertFalse( 2112 "Socket returned input Shutdown when it should be connected ", 2113 theSocket.isInputShutdown()); 2114 assertFalse( 2115 "Socket returned output Shutdown when it should be connected ", 2116 theSocket.isOutputShutdown()); 2117 assertTrue("Local port on connected socket was 0", theSocket 2118 .getLocalPort() != 0); 2119 theSocket.close(); 2120 serverSocket.close(); 2121 2122 // now validate that we get the right exception if we connect when we 2123 // are already connected 2124 try { 2125 new InetSocketAddress(InetAddress.getLocalHost(), 2126 Support_PortManager.getNextPort()); 2127 theSocket = new Socket(); 2128 serverSocket = new ServerSocket(); 2129 serverSocket.bind(theAddress); 2130 theSocket.connect(theAddress, 100000); 2131 theSocket.connect(theAddress, 100000); 2132 theSocket.close(); 2133 serverSocket.close(); 2134 fail("No exception when we try to connect on a connected socket: "); 2135 2136 } catch (Exception e) { 2137 assertTrue( 2138 "Wrong exception when connecting on socket that is allready connected" 2139 + e.toString(), (e instanceof SocketException)); 2140 assertFalse( 2141 "Wrong exception when connecting on socket that is allready connected" 2142 + e.toString(), 2143 (e instanceof SocketTimeoutException)); 2144 try { 2145 theSocket.close(); 2146 serverSocket.close(); 2147 } catch (Exception e2) { 2148 } 2149 2150 } 2151 2152 // now validate that connected socket can be used to read/write 2153 new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager 2154 .getNextPort()); 2155 theSocket = new Socket(); 2156 serverSocket = new ServerSocket(); 2157 serverSocket.bind(theAddress); 2158 theSocket.connect(theAddress, 100000); 2159 Socket servSock = serverSocket.accept(); 2160 InputStream theInput = theSocket.getInputStream(); 2161 OutputStream theOutput = servSock.getOutputStream(); 2162 InputStream theInput2 = servSock.getInputStream(); 2163 OutputStream theOutput2 = theSocket.getOutputStream(); 2164 2165 String sendString = new String("Test"); 2166 theOutput.write(sendString.getBytes()); 2167 theOutput.flush(); 2168 2169 Thread.sleep(1000); 2170 2171 int totalBytesRead = 0; 2172 byte[] myBytes = new byte[100]; 2173 while (theInput.available() > 0) { 2174 int bytesRead = theInput.read(myBytes, totalBytesRead, 2175 myBytes.length - totalBytesRead); 2176 totalBytesRead = totalBytesRead + bytesRead; 2177 } 2178 2179 String receivedString = new String(myBytes, 0, totalBytesRead); 2180 assertTrue("Could not recv on socket connected with timeout:" 2181 + receivedString + ":" + sendString, receivedString 2182 .equals(sendString)); 2183 2184 sendString = new String("SEND - Test"); 2185 theOutput2.write(sendString.getBytes()); 2186 theOutput2.flush(); 2187 2188 totalBytesRead = 0; 2189 myBytes = new byte[100]; 2190 Thread.sleep(1000); 2191 while (theInput2.available() > 0) { 2192 int bytesRead = theInput2.read(myBytes, totalBytesRead, 2193 myBytes.length - totalBytesRead); 2194 totalBytesRead = totalBytesRead + bytesRead; 2195 } 2196 2197 receivedString = new String(myBytes, 0, totalBytesRead); 2198 assertTrue("Could not send on socket connected with timeout:" 2199 + receivedString + ":" + sendString, receivedString 2200 .equals(sendString)); 2201 2202 theSocket.close(); 2203 serverSocket.close(); 2204 2205 // now try to set options while we are connecting 2206 theSocket = new Socket(); 2207 SocketConnector connector = new SocketConnector(5000, theSocket, 2208 nonReachableAddress); 2209 connector.start(); 2210 theSocket.setSoTimeout(100); 2211 Thread.sleep(10); 2212 assertEquals("Socket option not set during connect: 10 ", 100, 2213 theSocket.getSoTimeout()); 2214 Thread.sleep(50); 2215 theSocket.setSoTimeout(200); 2216 assertEquals("Socket option not set during connect: 50 ", 200, 2217 theSocket.getSoTimeout()); 2218 Thread.sleep(5000); 2219 theSocket.close(); 2220 2221 SocketChannel channel = SocketChannel.open(); 2222 channel.configureBlocking(false); 2223 Socket socket = channel.socket(); 2224 int port = Support_PortManager.getNextPort(); 2225 try { 2226 socket.connect( new InetSocketAddress(InetAddress.getLocalHost(), 2227 Support_PortManager.getNextPort()), port); 2228 fail("IllegalBlockingModeException was not thrown."); 2229 } catch(IllegalBlockingModeException ibme) { 2230 //expected 2231 } 2232 } 2233 2234 /** 2235 * @tests java.net.Socket#isInputShutdown() 2236 */ 2237 @TestTargetNew( 2238 level = TestLevel.COMPLETE, 2239 notes = "", 2240 method = "isInputShutdown", 2241 args = {} 2242 ) 2243 public void test_isInputShutdown() throws IOException { 2244 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 2245 .getLocalHost(), Support_PortManager.getNextPort()); 2246 Socket theSocket = new Socket(); 2247 ServerSocket serverSocket = new ServerSocket(); 2248 serverSocket.bind(theAddress); 2249 theSocket.connect(theAddress); 2250 Socket servSock = serverSocket.accept(); 2251 InputStream theInput = theSocket.getInputStream(); 2252 OutputStream theOutput = servSock.getOutputStream(); 2253 2254 // make sure we get the right answer with newly connected socket 2255 assertFalse("Socket indicated input shutdown when it should not have", 2256 theSocket.isInputShutdown()); 2257 2258 // shutdown the output 2259 theSocket.shutdownInput(); 2260 2261 // make sure we get the right answer once it is shut down 2262 assertTrue( 2263 "Socket indicated input was NOT shutdown when it should have been", 2264 theSocket.isInputShutdown()); 2265 2266 theSocket.close(); 2267 serverSocket.close(); 2268 2269 // make sure we get the right answer for closed sockets 2270 assertFalse( 2271 "Socket indicated input was shutdown when socket was closed", 2272 servSock.isInputShutdown()); 2273 2274 } 2275 2276 /** 2277 * @tests java.net.Socket#isOutputShutdown() 2278 */ 2279 @TestTargetNew( 2280 level = TestLevel.COMPLETE, 2281 notes = "", 2282 method = "isOutputShutdown", 2283 args = {} 2284 ) 2285 public void test_isOutputShutdown() throws IOException { 2286 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 2287 .getLocalHost(), Support_PortManager.getNextPort()); 2288 Socket theSocket = new Socket(); 2289 ServerSocket serverSocket = new ServerSocket(); 2290 serverSocket.bind(theAddress); 2291 theSocket.connect(theAddress); 2292 Socket servSock = serverSocket.accept(); 2293 InputStream theInput = theSocket.getInputStream(); 2294 OutputStream theOutput = servSock.getOutputStream(); 2295 2296 // make sure we get the right answer with newly connected socket 2297 assertFalse("Socket indicated output shutdown when it should not have", 2298 servSock.isOutputShutdown()); 2299 2300 // shutdown the output 2301 servSock.shutdownOutput(); 2302 2303 // make sure we get the right answer once it is shut down 2304 assertTrue( 2305 "Socket indicated output was NOT shutdown when it should have been", 2306 servSock.isOutputShutdown()); 2307 2308 theSocket.close(); 2309 serverSocket.close(); 2310 2311 // make sure we get the right answer for closed sockets 2312 assertFalse( 2313 "Socket indicated output was output shutdown when the socket was closed", 2314 theSocket.isOutputShutdown()); 2315 2316 } 2317 2318 /** 2319 * @tests java.net.Socket#setReuseAddress(boolean) 2320 */ 2321 @TestTargetNew( 2322 level = TestLevel.COMPLETE, 2323 notes = "", 2324 method = "setReuseAddress", 2325 args = {boolean.class} 2326 ) 2327 public void test_setReuseAddressZ() { 2328 2329 try { 2330 InetAddress allAddresses[] = InetAddress.getAllByName(InetAddress 2331 .getLocalHost().getHostName()); 2332 if (allAddresses.length > 1) { 2333 2334 InetSocketAddress theAddress = new InetSocketAddress( 2335 InetAddress.getLocalHost(), Support_PortManager 2336 .getNextPort()); 2337 ServerSocket serverSocket = new ServerSocket(); 2338 serverSocket.bind(theAddress); 2339 2340 // try to bind to port address that is already in use with 2341 // reuseAddress = false. 2342 // On windows platforms the bind is allowed even then 2343 // reUseAddress is false (ONLY IF BOTH SOCKETS 2344 // ARE IPV4 Sockets) so our test uses the platform to determine 2345 // what the expected result is. It seems that on linux 2346 // platforms we also don't get an exception. 2347 InetSocketAddress theLocalAddress = new InetSocketAddress( 2348 (InetAddress) allAddresses[1], Support_PortManager 2349 .getNextPort()); 2350 InetSocketAddress theOtherLocalAddress = new InetSocketAddress( 2351 (InetAddress) allAddresses[0], theLocalAddress 2352 .getPort()); 2353 Socket theSocket = new Socket(); 2354 theSocket.setReuseAddress(false); 2355 theSocket.bind(theLocalAddress); 2356 Socket theSocket2 = null; 2357 String platform = System.getProperty("os.name"); 2358 try { 2359 theSocket2 = new Socket(); 2360 theSocket2.setReuseAddress(false); 2361 theSocket2.bind(theOtherLocalAddress); 2362 2363 if ((!platform.startsWith("Linux")) 2364 && ((!platform.startsWith("Windows")) || 2365 // for windows we don't get an exception with 2366 // setreuse set to false unless one of the 2367 // addresses we bind to is an IPv6 address and we 2368 // are therefore using the IPv6 stack. 2369 !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 2370 fail("No exception when setReuseAddress is false and we bind:" 2371 + theLocalAddress.toString() 2372 + ":" 2373 + theOtherLocalAddress.toString()); 2374 } 2375 } catch (IOException ex) { 2376 if ((platform.startsWith("Linux")) 2377 || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 2378 fail("Got unexpected exception when binding with setReuseAddress false on windows platform:" 2379 + theAddress.toString() + ":" + ex.toString()); 2380 } 2381 } 2382 theSocket.close(); 2383 theSocket2.close(); 2384 2385 // try to bind to port that is allready in use with reuseAddress 2386 // = true 2387 theLocalAddress = new InetSocketAddress( 2388 (InetAddress) allAddresses[0], Support_PortManager 2389 .getNextPort()); 2390 theOtherLocalAddress = new InetSocketAddress( 2391 (InetAddress) allAddresses[1], theLocalAddress 2392 .getPort()); 2393 2394 theSocket = new Socket(); 2395 theSocket.setReuseAddress(true); 2396 theSocket.bind(theLocalAddress); 2397 try { 2398 theSocket2 = new Socket(); 2399 theSocket2.setReuseAddress(true); 2400 theSocket2.bind(theOtherLocalAddress); 2401 theSocket2.close(); 2402 } catch (IOException ex) { 2403 fail("IOException when setReuseAddress is true and we bind :" 2404 + ex.toString()); 2405 } 2406 theSocket.close(); 2407 serverSocket.close(); 2408 2409 // try with default behavior which should be the same on all 2410 // platforms 2411 theLocalAddress = new InetSocketAddress( 2412 (InetAddress) allAddresses[0], Support_PortManager 2413 .getNextPort()); 2414 theOtherLocalAddress = new InetSocketAddress( 2415 (InetAddress) allAddresses[1], theLocalAddress 2416 .getPort()); 2417 2418 theSocket = new Socket(); 2419 theSocket.bind(theLocalAddress); 2420 try { 2421 theSocket2 = new Socket(); 2422 theSocket2.bind(theOtherLocalAddress); 2423 theSocket2.close(); 2424 if ((!platform.startsWith("Linux")) 2425 && ((!platform.startsWith("Windows")) || !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 2426 fail("No exception when setReuseAddress is default and we bind:" 2427 + theLocalAddress.toString() 2428 + ":" 2429 + theOtherLocalAddress.toString()); 2430 } 2431 } catch (IOException ex) { 2432 if ((platform.startsWith("Linux")) 2433 || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 2434 fail("Got unexpected exception when binding with setReuseAddress default on windows platform:" 2435 + theAddress.toString() + ":" + ex.toString()); 2436 } 2437 } 2438 theSocket.close(); 2439 serverSocket.close(); 2440 2441 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 2442 } 2443 } catch (Exception e) { 2444 handleException(e, SO_REUSEADDR); 2445 } 2446 2447 try { 2448 Socket theSocket = new Socket(); 2449 theSocket.close(); 2450 theSocket.setReuseAddress(true); 2451 fail("SocketException was not thrown."); 2452 } catch(SocketException ioe) { 2453 //expected 2454 } catch(IOException ioe) { 2455 fail("IOException was thrown."); 2456 } 2457 } 2458 2459 /** 2460 * @tests java.net.Socket#getReuseAddress() 2461 */ 2462 @TestTargetNew( 2463 level = TestLevel.COMPLETE, 2464 notes = "", 2465 method = "getReuseAddress", 2466 args = {} 2467 ) 2468 public void test_getReuseAddress() { 2469 try { 2470 Socket theSocket = new Socket(); 2471 theSocket.setReuseAddress(true); 2472 assertTrue("getReuseAddress false when it should be true", 2473 theSocket.getReuseAddress()); 2474 theSocket.setReuseAddress(false); 2475 assertFalse("getReuseAddress true when it should be False", 2476 theSocket.getReuseAddress()); 2477 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 2478 } catch (Exception e) { 2479 handleException(e, SO_REUSEADDR); 2480 } 2481 2482 try { 2483 Socket newSocket = new Socket(); 2484 newSocket.close(); 2485 try { 2486 newSocket.getReuseAddress(); 2487 fail("SocketException was not thrown."); 2488 } catch(SocketException e) { 2489 //expected 2490 } 2491 } catch(Exception e) { 2492 fail("Unexpected exception."); 2493 } 2494 } 2495 2496 /** 2497 * @tests java.net.Socket#setOOBInline(boolean) 2498 */ 2499 @TestTargetNew( 2500 level = TestLevel.COMPLETE, 2501 notes = "", 2502 method = "setOOBInline", 2503 args = {boolean.class} 2504 ) 2505 public void test_setOOBInlineZ() { 2506 // mostly tested in getOOBInline. Just set to make sure call works ok 2507 try { 2508 new InetSocketAddress(InetAddress.getLocalHost(), 2509 Support_PortManager.getNextPort()); 2510 Socket theSocket = new Socket(); 2511 theSocket.setOOBInline(true); 2512 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 2513 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE); 2514 } catch (Exception e) { 2515 handleException(e, SO_OOBINLINE); 2516 } 2517 2518 try { 2519 Socket theSocket = new Socket(); 2520 theSocket.close(); 2521 theSocket.setOOBInline(true); 2522 fail("SocketException was not thrown."); 2523 } catch(SocketException ioe) { 2524 //expected 2525 } catch(IOException ioe) { 2526 fail("IOException was thrown."); 2527 } 2528 } 2529 2530 /** 2531 * @tests java.net.Socket#getOOBInline() 2532 */ 2533 @TestTargetNew( 2534 level = TestLevel.COMPLETE, 2535 notes = "", 2536 method = "getOOBInline", 2537 args = {} 2538 ) 2539 public void test_getOOBInline() { 2540 2541 try { 2542 new InetSocketAddress(InetAddress.getLocalHost(), 2543 Support_PortManager.getNextPort()); 2544 Socket theSocket = new Socket(); 2545 2546 // validate that value reflects what we set it to true after true, 2547 // false after false and false after false false 2548 theSocket.setOOBInline(true); 2549 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 2550 theSocket.setOOBInline(false); 2551 assertFalse("expected OOBIline to be true", theSocket 2552 .getOOBInline()); 2553 theSocket.setOOBInline(false); 2554 assertFalse("expected OOBIline to be true", theSocket 2555 .getOOBInline()); 2556 theSocket.close(); 2557 try { 2558 theSocket.getOOBInline(); 2559 fail("SocketException was not thrown."); 2560 } catch(SocketException se) { 2561 //expected 2562 } 2563 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE); 2564 2565 } catch (Exception e) { 2566 handleException(e, SO_OOBINLINE); 2567 } 2568 } 2569 2570 /** 2571 * @tests java.net.Socket#setTrafficClass(int) 2572 */ 2573 @TestTargetNew( 2574 level = TestLevel.COMPLETE, 2575 notes = "", 2576 method = "setTrafficClass", 2577 args = {int.class} 2578 ) 2579 public void test_setTrafficClassI() { 2580 try { 2581 int IPTOS_LOWCOST = 0x2; 2582 int IPTOS_RELIABILTY = 0x4; 2583 int IPTOS_THROUGHPUT = 0x8; 2584 int IPTOS_LOWDELAY = 0x10; 2585 2586 new InetSocketAddress(InetAddress.getLocalHost(), 2587 Support_PortManager.getNextPort()); 2588 Socket theSocket = new Socket(); 2589 2590 // validate that value set must be between 0 and 255 2591 try { 2592 theSocket.setTrafficClass(256); 2593 fail("No exception was thrown when traffic class set to 256"); 2594 } catch (IllegalArgumentException e) { 2595 } 2596 2597 try { 2598 theSocket.setTrafficClass(-1); 2599 fail("No exception was thrown when traffic class set to -1"); 2600 } catch (IllegalArgumentException e) { 2601 } 2602 2603 // now validate that we can set it to some good values 2604 theSocket.setTrafficClass(IPTOS_LOWCOST); 2605 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 2606 ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS); 2607 } catch (Exception e) { 2608 handleException(e, IP_TOS); 2609 } 2610 2611 try { 2612 Socket theSocket = new Socket(); 2613 theSocket.close(); 2614 theSocket.setTrafficClass(0); 2615 fail("SocketException was not thrown."); 2616 } catch(SocketException ioe) { 2617 //expected 2618 } catch(IOException ioe) { 2619 fail("IOException was thrown."); 2620 } 2621 } 2622 2623 /** 2624 * @tests java.net.Socket#getTrafficClass() 2625 */ 2626 @TestTargetNew( 2627 level = TestLevel.SUFFICIENT, 2628 notes = "SocketException checking missing.", 2629 method = "getTrafficClass", 2630 args = {} 2631 ) 2632 public void test_getTrafficClass() { 2633 try { 2634 int IPTOS_LOWCOST = 0x2; 2635 int IPTOS_RELIABILTY = 0x4; 2636 int IPTOS_THROUGHPUT = 0x8; 2637 int IPTOS_LOWDELAY = 0x10; 2638 2639 new InetSocketAddress(InetAddress.getLocalHost(), 2640 Support_PortManager.getNextPort()); 2641 Socket theSocket = new Socket(); 2642 2643 /* 2644 * we cannot actually check that the values are set as if a platform 2645 * does not support the option then it may come back unset even 2646 * though we set it so just get the value to make sure we can get it 2647 */ 2648 int trafficClass = theSocket.getTrafficClass(); 2649 ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS); 2650 } catch (Exception e) { 2651 handleException(e, IP_TOS); 2652 } 2653 } 2654 2655 /** 2656 * @tests java.net.Socket#getChannel() 2657 */ 2658 @TestTargetNew( 2659 level = TestLevel.COMPLETE, 2660 notes = "", 2661 method = "getChannel", 2662 args = {} 2663 ) 2664 public void test_getChannel() throws Exception { 2665 assertNull(new Socket().getChannel()); 2666 2667 SocketChannel channel = SocketChannel.open(); 2668 Socket socket = channel.socket(); 2669 assertEquals(channel, socket.getChannel()); 2670 socket.close(); 2671 channel.close(); 2672 } 2673 2674 /** 2675 * @tests java.net.Socket#sendUrgentData(int) 2676 */ 2677 @TestTargetNew( 2678 level = TestLevel.COMPLETE, 2679 notes = "", 2680 method = "sendUrgentData", 2681 args = {int.class} 2682 ) 2683 public void test_sendUrgentDataI() { 2684 2685 // Some platforms may not support urgent data in this case we will not 2686 // run these tests. For now run on all platforms until we find those 2687 // that do not support urgent data 2688 String platform = System.getProperty("os.name"); 2689 if (!platform.equals("Dummy")) { 2690 // validate that when OOBInline is false that any urgent data 2691 // is silently ignored 2692 String urgentData = "U"; 2693 try { 2694 InetSocketAddress theAddress = new InetSocketAddress( 2695 InetAddress.getLocalHost(), Support_PortManager 2696 .getNextPort()); 2697 Socket theSocket = new Socket(); 2698 ServerSocket serverSocket = new ServerSocket(); 2699 serverSocket.bind(theAddress); 2700 theSocket.connect(theAddress); 2701 Socket servSock = serverSocket.accept(); 2702 InputStream theInput = theSocket.getInputStream(); 2703 OutputStream theOutput = servSock.getOutputStream(); 2704 2705 // send the regular data 2706 String sendString = new String("Test"); 2707 theOutput.write(sendString.getBytes()); 2708 theOutput.flush(); 2709 2710 // send the urgent data which should not be received 2711 theSocket.setOOBInline(false); 2712 servSock.sendUrgentData(urgentData.getBytes()[0]); 2713 theOutput.write(sendString.getBytes()); 2714 theOutput.flush(); 2715 2716 // give things some time to settle 2717 Thread.sleep(1000); 2718 2719 int totalBytesRead = 0; 2720 byte[] myBytes = new byte[100]; 2721 while (theInput.available() > 0) { 2722 int bytesRead = theInput.read(myBytes, totalBytesRead, 2723 myBytes.length - totalBytesRead); 2724 totalBytesRead = totalBytesRead + bytesRead; 2725 } 2726 2727 String receivedString = new String(myBytes, 0, totalBytesRead); 2728 //assertTrue("Urgent Data seems to have been received:" 2729 // + receivedString + ":" + sendString, receivedString 2730 // .equals(sendString + sendString)); 2731 2732 theSocket.close(); 2733 serverSocket.close(); 2734 2735 // now validate that urgent data is received as expected. Expect 2736 // that it should be between the two writes. 2737 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2738 Support_PortManager.getNextPort()); 2739 theSocket = new Socket(); 2740 serverSocket = new ServerSocket(); 2741 serverSocket.bind(theAddress); 2742 theSocket.connect(theAddress); 2743 servSock = serverSocket.accept(); 2744 theInput = theSocket.getInputStream(); 2745 theOutput = servSock.getOutputStream(); 2746 2747 // send the regular data 2748 sendString = new String("Test - Urgent Data"); 2749 theOutput.write(sendString.getBytes()); 2750 theOutput.flush(); 2751 2752 // send the urgent data which should be received 2753 theSocket.setOOBInline(true); 2754 servSock.sendUrgentData(urgentData.getBytes()[0]); 2755 2756 theOutput.write(sendString.getBytes()); 2757 theOutput.flush(); 2758 2759 Thread.sleep(1000); 2760 2761 totalBytesRead = 0; 2762 myBytes = new byte[100]; 2763 while (theInput.available() > 0) { 2764 int bytesRead = theInput.read(myBytes, totalBytesRead, 2765 myBytes.length - totalBytesRead); 2766 totalBytesRead = totalBytesRead + bytesRead; 2767 } 2768 2769 receivedString = new String(myBytes, 0, totalBytesRead); 2770 assertTrue("Urgent Data was not received with one urgent byte:" 2771 + receivedString + ":" + sendString + urgentData 2772 + sendString, receivedString.equals(sendString 2773 + urgentData + sendString)); 2774 2775 theSocket.close(); 2776 serverSocket.close(); 2777 2778 // now test case where we try to send two urgent bytes. 2779 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2780 Support_PortManager.getNextPort()); 2781 theSocket = new Socket(); 2782 serverSocket = new ServerSocket(); 2783 serverSocket.bind(theAddress); 2784 theSocket.connect(theAddress); 2785 servSock = serverSocket.accept(); 2786 theInput = theSocket.getInputStream(); 2787 theOutput = servSock.getOutputStream(); 2788 2789 // send the regular data 2790 sendString = new String("Test - Urgent Data"); 2791 theOutput.write(sendString.getBytes()); 2792 theOutput.flush(); 2793 2794 // send the urgent data which should not be received 2795 theSocket.setOOBInline(true); 2796 servSock.sendUrgentData(urgentData.getBytes()[0]); 2797 servSock.sendUrgentData(urgentData.getBytes()[0]); 2798 2799 theOutput.write(sendString.getBytes()); 2800 theOutput.flush(); 2801 2802 Thread.sleep(1000); 2803 2804 totalBytesRead = 0; 2805 myBytes = new byte[100]; 2806 while (theInput.available() > 0) { 2807 int bytesRead = theInput.read(myBytes, totalBytesRead, 2808 myBytes.length - totalBytesRead); 2809 totalBytesRead = totalBytesRead + bytesRead; 2810 } 2811 2812 receivedString = new String(myBytes, 0, totalBytesRead); 2813 assertTrue( 2814 "Did not get right byte of urgent data when two sent:" 2815 + receivedString + ":" + sendString 2816 + urgentData + urgentData + sendString, 2817 receivedString.equals(sendString + urgentData 2818 + urgentData + sendString)); 2819 2820 theSocket.close(); 2821 serverSocket.close(); 2822 2823 /* 2824 * TODO : These do not currently pass on XP SP2 and Server 2003 2825 */ 2826 if (!platform.startsWith("Windows")) { 2827 // now test the case were we send turn the OOBInline on/off 2828 theAddress = new InetSocketAddress(InetAddress 2829 .getLocalHost(), Support_PortManager.getNextPort()); 2830 theSocket = new Socket(); 2831 serverSocket = new ServerSocket(); 2832 serverSocket.bind(theAddress); 2833 theSocket.connect(theAddress); 2834 servSock = serverSocket.accept(); 2835 theInput = theSocket.getInputStream(); 2836 theOutput = servSock.getOutputStream(); 2837 2838 // send the regular data 2839 sendString = new String("Test - Urgent Data"); 2840 theOutput.write(sendString.getBytes()); 2841 theOutput.flush(); 2842 2843 // send the urgent data which should be received 2844 theSocket.setOOBInline(true); 2845 servSock.sendUrgentData(urgentData.getBytes()[0]); 2846 2847 theOutput.write(sendString.getBytes()); 2848 theOutput.flush(); 2849 2850 Thread.sleep(1000); 2851 2852 totalBytesRead = 0; 2853 myBytes = new byte[100]; 2854 while (theInput.available() > 0) { 2855 int bytesRead = theInput.read(myBytes, totalBytesRead, 2856 myBytes.length - totalBytesRead); 2857 totalBytesRead = totalBytesRead + bytesRead; 2858 } 2859 2860 receivedString = new String(myBytes, 0, totalBytesRead); 2861 assertTrue( 2862 "Did not get urgent data when turning on/off(1):" 2863 + receivedString + ":" + sendString 2864 + urgentData + sendString, receivedString 2865 .equals(sendString + urgentData 2866 + sendString)); 2867 2868 // send the regular data 2869 sendString = new String("Test - Urgent Data"); 2870 theOutput.write(sendString.getBytes()); 2871 theOutput.flush(); 2872 2873 // send the urgent data which should not be received 2874 theSocket.setOOBInline(false); 2875 servSock.sendUrgentData(urgentData.getBytes()[0]); 2876 2877 // send trailing data 2878 theOutput.write(sendString.getBytes()); 2879 theOutput.flush(); 2880 2881 Thread.sleep(1000); 2882 2883 totalBytesRead = 0; 2884 myBytes = new byte[100]; 2885 while (theInput.available() > 0) { 2886 int bytesRead = theInput.read(myBytes, totalBytesRead, 2887 myBytes.length - totalBytesRead); 2888 totalBytesRead = totalBytesRead + bytesRead; 2889 } 2890 2891 receivedString = new String(myBytes, 0, totalBytesRead); 2892 //assertTrue( 2893 // "Got unexpected data data when turning on/off(2):" 2894 // + receivedString + ":" + sendString 2895 // + sendString, receivedString 2896 // .equals(sendString + sendString)); 2897 2898 // now turn back on and get data. Here we also 2899 // get the previously sent byte of urgent data as it is 2900 // still in the urgent buffer 2901 2902 // send the regular data 2903 sendString = new String("Test - Urgent Data"); 2904 theOutput.write(sendString.getBytes()); 2905 theOutput.flush(); 2906 2907 // send the urgent data which should be received again 2908 theSocket.setOOBInline(true); 2909 servSock.sendUrgentData(urgentData.getBytes()[0]); 2910 2911 theOutput.write(sendString.getBytes()); 2912 theOutput.flush(); 2913 2914 Thread.sleep(1000); 2915 2916 totalBytesRead = 0; 2917 myBytes = new byte[100]; 2918 while (theInput.available() > 0) { 2919 int bytesRead = theInput.read(myBytes, totalBytesRead, 2920 myBytes.length - totalBytesRead); 2921 totalBytesRead = totalBytesRead + bytesRead; 2922 } 2923 2924 receivedString = new String(myBytes, 0, totalBytesRead); 2925 // depending on the platform we may get the previously sent 2926 // urgent data or not (examples windows-yes, Linux-no). 2927 // So accept either so long as we get the urgent data from 2928 // when it was on. 2929 //assertTrue( 2930 // "Did not get urgent data when turning on/off(3) GOT:" 2931 // + receivedString + ":Expected" + urgentData 2932 // + sendString + urgentData + sendString 2933 // + ":OR:" + sendString + urgentData 2934 // + sendString, 2935 // (receivedString.equals(urgentData + sendString 2936 // + urgentData + sendString) || receivedString 2937 // .equals(sendString + urgentData 2938 // + sendString))); 2939 2940 theSocket.close(); 2941 serverSocket.close(); 2942 } 2943 2944 // now test the case where there is only urgent data 2945 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2946 Support_PortManager.getNextPort()); 2947 theSocket = new Socket(); 2948 serverSocket = new ServerSocket(); 2949 serverSocket.bind(theAddress); 2950 theSocket.connect(theAddress); 2951 servSock = serverSocket.accept(); 2952 theInput = theSocket.getInputStream(); 2953 theOutput = servSock.getOutputStream(); 2954 2955 // send the urgent data which should not be received. 2956 theSocket.setOOBInline(true); 2957 servSock.sendUrgentData(urgentData.getBytes()[0]); 2958 2959 Thread.sleep(1000); 2960 2961 totalBytesRead = 0; 2962 myBytes = new byte[100]; 2963 while (theInput.available() > 0) { 2964 int bytesRead = theInput.read(myBytes, totalBytesRead, 2965 myBytes.length - totalBytesRead); 2966 totalBytesRead = totalBytesRead + bytesRead; 2967 } 2968 2969 receivedString = new String(myBytes, 0, totalBytesRead); 2970 assertTrue("Did not get urgent data only urgent data sent:" 2971 + receivedString + ":" + urgentData, receivedString 2972 .equals(urgentData)); 2973 2974 } catch (Exception e) { 2975 // for platforms that do not support urgent data we expect an 2976 // exception. For the others report an error. 2977 // TODO : Need to introduce a better test for the exception 2978 // so that the failure only occurs on platforms that support 2979 // urgent data 2980 fail("Platform:" + platform 2981 + ": Got exception during sendUrgent data tests" 2982 + e.toString()); 2983 } 2984 } 2985 2986 try { 2987 Socket theSocket = new Socket(); 2988 theSocket.close(); 2989 theSocket.sendUrgentData(0); 2990 fail("IOException was not thrown."); 2991 } catch(IOException ioe) { 2992 //expected 2993 } 2994 } 2995 2996 /* 2997 * @tests java.net.Socket#setPerformancePreference() 2998 */ 2999 @TestTargetNew( 3000 level = TestLevel.COMPLETE, 3001 notes = "", 3002 method = "setPerformancePreferences", 3003 args = {int.class, int.class, int.class} 3004 ) 3005 public void test_setPerformancePreference_Int_Int_Int() throws Exception { 3006 Socket theSocket = new Socket(); 3007 theSocket.setPerformancePreferences(1, 1, 1); 3008 } 3009 3010 /** 3011 * @tests java.net.Socket#Socket(Proxy) 3012 */ 3013 @TestTargetNew( 3014 level = TestLevel.COMPLETE, 3015 notes = "", 3016 method = "Socket", 3017 args = {java.net.Proxy.class} 3018 ) 3019 public void test_ConstructorLjava_net_Proxy_Exception() { 3020 3021 SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1", 3022 80); 3023 SocketAddress addr2 = new InetSocketAddress("localhost", 80); 3024 3025 Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1); 3026 // IllegalArgumentException test 3027 try { 3028 new Socket(proxy1); 3029 fail("should throw IllegalArgumentException"); 3030 } catch (IllegalArgumentException e) { 3031 // expected 3032 } 3033 3034 Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1); 3035 // should not throw any exception 3036 new Socket(proxy2); 3037 new Socket(Proxy.NO_PROXY); 3038 3039 // SecurityException test 3040 SecurityManager originalSecurityManager = System.getSecurityManager(); 3041 try { 3042 System.setSecurityManager(new MockSecurityManager()); 3043 } catch (SecurityException e) { 3044 System.err 3045 .println("No permission to setSecurityManager, security related test in test_ConstructorLjava_net_Proxy_Security is ignored"); 3046 return; 3047 } 3048 3049 Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, addr1); 3050 Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, addr2); 3051 try { 3052 try { 3053 new Socket(proxy3); 3054 fail("should throw SecurityException"); 3055 } catch (SecurityException e) { 3056 // expected 3057 } 3058 try { 3059 new Socket(proxy4); 3060 fail("should throw SecurityException"); 3061 } catch (SecurityException e) { 3062 // expected 3063 } 3064 } finally { 3065 System.setSecurityManager(originalSecurityManager); 3066 } 3067 3068 SecurityManager oldSm = System.getSecurityManager(); 3069 System.setSecurityManager(sm); 3070 try { 3071 new Socket(InetAddress.getLocalHost(), 0, true); 3072 fail("SecurityException should be thrown."); 3073 } catch (SecurityException e) { 3074 // expected 3075 } catch (SocketException e) { 3076 fail("SocketException was thrown."); 3077 } catch (UnknownHostException e) { 3078 fail("UnknownHostException was thrown."); 3079 } catch (IOException e) { 3080 fail("IOException was thrown."); 3081 } finally { 3082 System.setSecurityManager(oldSm); 3083 } 3084 3085 try { 3086 new Socket((Proxy) null); 3087 fail("IllegalArgumentException was not thrown."); 3088 } catch(IllegalArgumentException iae) { 3089 //expected 3090 } 3091 } 3092 3093 @TestTargetNew( 3094 level = TestLevel.SUFFICIENT, 3095 notes = "SocketException depends on the underlying protocol.", 3096 method = "Socket", 3097 args = {SocketImpl.class} 3098 ) 3099 public void test_ConstructorLSocketImpl() { 3100 MockSocketImpl msi = new MockSocketImpl(); 3101 try { 3102 new TestSocket(msi); 3103 } catch (SocketException e) { 3104 fail("SocketException was thrown."); 3105 } 3106 } 3107 3108 /** 3109 * @tests Socket#connect(SocketAddress) try an unknownhost 3110 */ 3111 @TestTargetNew( 3112 level = TestLevel.PARTIAL, 3113 notes = "UnknownHostException checking only.", 3114 method = "connect", 3115 args = {java.net.SocketAddress.class, int.class} 3116 ) 3117 public void test_connect_unknownhost() throws Exception { 3118 Socket socket = new Socket(); 3119 try { 3120 socket.connect(new InetSocketAddress("unknownhost", 12345)); 3121 fail("Should throw UnknownHostException"); 3122 } catch (UnknownHostException e) { 3123 // expected 3124 } 3125 } 3126 3127 /** 3128 * @tests Socket#connect(SocketAddress) try an unknownhost created by 3129 * createUnresolved() 3130 */ 3131 @TestTargetNew( 3132 level = TestLevel.PARTIAL, 3133 notes = "UnknownHostException checking only.", 3134 method = "connect", 3135 args = {java.net.SocketAddress.class, int.class} 3136 ) 3137 public void test_connect_unresolved_unknown() throws Exception { 3138 Socket socket = new Socket(); 3139 try { 3140 socket.connect(InetSocketAddress.createUnresolved("unknownhost", 3141 12345)); 3142 fail("Should throw UnknownHostException"); 3143 } catch (UnknownHostException e) { 3144 // expected 3145 } 3146 } 3147 3148 /** 3149 * @tests Socket#connect(SocketAddress) try a known host created by 3150 * createUnresolved() 3151 */ 3152 @TestTargetNew( 3153 level = TestLevel.PARTIAL, 3154 notes = "UnknownHostException checking only.", 3155 method = "connect", 3156 args = {java.net.SocketAddress.class, int.class} 3157 ) 3158 public void test_connect_unresolved() throws Exception { 3159 Socket socket = new Socket(); 3160 try { 3161 socket.connect(InetSocketAddress.createUnresolved( 3162 Support_Configuration.SocksServerTestHost, 3163 Support_Configuration.SocksServerTestPort)); 3164 fail("Should throw UnknownHostException"); 3165 } catch (UnknownHostException e) { 3166 // expected 3167 } 3168 } 3169 3170 /** 3171 * @tests Socket#getOutputStream() 3172 */ 3173 @TestTargetNew( 3174 level = TestLevel.PARTIAL_COMPLETE, 3175 notes = "SocketException, IllegalBlockingModeException checking.", 3176 method = "getOutputStream", 3177 args = {} 3178 ) 3179 public void test_getOutputStream_shutdownOutput() throws Exception { 3180 // regression test for Harmony-873 3181 ServerSocket ss = new ServerSocket(0); 3182 Socket s = new Socket("127.0.0.1", ss.getLocalPort()); 3183 ss.accept(); 3184 s.shutdownOutput(); 3185 try { 3186 s.getOutputStream(); 3187 fail("should throw SocketException"); 3188 } catch (IOException e) { 3189 // expected 3190 } finally { 3191 s.close(); 3192 } 3193 3194 SocketChannel channel = SocketChannel.open( 3195 new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort())); 3196 channel.configureBlocking(false); 3197 ss.accept(); 3198 Socket socket = channel.socket(); 3199 3200 OutputStream out = null; 3201 3202 try { 3203 out = socket.getOutputStream(); 3204 out.write(1); 3205 fail("IllegalBlockingModeException was not thrown."); 3206 } catch(IllegalBlockingModeException ibme) { 3207 //expected 3208 } finally { 3209 if(out != null) out.close(); 3210 socket.close(); 3211 channel.close(); 3212 } 3213 } 3214 3215 /** 3216 * @tests Socket#shutdownInput() 3217 * @tests Socket#shutdownOutput() 3218 */ 3219 @TestTargetNew( 3220 level = TestLevel.ADDITIONAL, 3221 notes = "Regression test.", 3222 method = "shutdownInput", 3223 args = {} 3224 ) 3225 public void test_shutdownInputOutput_twice() throws Exception { 3226 // regression test for Harmony-2944 3227 Socket s = new Socket("0.0.0.0", 0, false); 3228 s.shutdownInput(); 3229 3230 try { 3231 s.shutdownInput(); 3232 fail("should throw SocketException"); 3233 } catch (SocketException se) { 3234 // expected 3235 } 3236 s.shutdownOutput(); 3237 3238 try { 3239 s.shutdownOutput(); 3240 fail("should throw SocketException"); 3241 } catch (SocketException se) { 3242 // expected 3243 } 3244 } 3245 3246 /** 3247 * Sets up the fixture, for example, open a network connection. This method 3248 * is called before a test is executed. 3249 * 3250 * @throws Exception 3251 */ 3252 protected void setUp() throws Exception { 3253 super.setUp(); 3254 } 3255 3256 /** 3257 * Tears down the fixture, for example, close a network connection. This 3258 * method is called after a test is executed. 3259 */ 3260 protected void tearDown() { 3261 try { 3262 if (s != null) 3263 s.close(); 3264 } catch (Exception e) { 3265 } 3266 try { 3267 if (ss != null) 3268 ss.close(); 3269 } catch (Exception e) { 3270 } 3271 try { 3272 if (t != null) 3273 t.interrupt(); 3274 } catch (Exception e) { 3275 } 3276 } 3277 3278 static class MockSecurityManager extends SecurityManager { 3279 3280 public void checkConnect(String host, int port) { 3281 if ("127.0.0.1".equals(host)) { 3282 throw new SecurityException("permission is not allowed"); 3283 } 3284 } 3285 3286 public void checkPermission(Permission permission) { 3287 return; 3288 } 3289 3290 } 3291 3292 /** 3293 * 3294 */ 3295 protected int startServer(String name) { 3296 int portNumber = Support_PortManager.getNextPort(); 3297 try { 3298 ss = new ServerSocket(portNumber, 5); 3299 } catch (IOException e) { 3300 fail(name + ": " + e); 3301 } 3302 return ss.getLocalPort(); 3303 } 3304 3305 class MockSocketImpl extends SocketImpl { 3306 3307 public MockSocketImpl() { 3308 super(); 3309 } 3310 3311 @Override 3312 protected void accept(SocketImpl arg0) throws IOException { 3313 // TODO Auto-generated method stub 3314 3315 } 3316 3317 @Override 3318 protected int available() throws IOException { 3319 // TODO Auto-generated method stub 3320 return 0; 3321 } 3322 3323 @Override 3324 protected void bind(InetAddress arg0, int arg1) throws IOException { 3325 // TODO Auto-generated method stub 3326 3327 } 3328 3329 @Override 3330 protected void close() throws IOException { 3331 // TODO Auto-generated method stub 3332 3333 } 3334 3335 @Override 3336 protected void connect(String arg0, int arg1) throws IOException { 3337 // TODO Auto-generated method stub 3338 3339 } 3340 3341 @Override 3342 protected void connect(InetAddress arg0, int arg1) throws IOException { 3343 // TODO Auto-generated method stub 3344 3345 } 3346 3347 @Override 3348 protected void connect(SocketAddress arg0, int arg1) throws IOException { 3349 // TODO Auto-generated method stub 3350 3351 } 3352 3353 @Override 3354 protected void create(boolean arg0) throws IOException { 3355 // TODO Auto-generated method stub 3356 3357 } 3358 3359 @Override 3360 protected InputStream getInputStream() throws IOException { 3361 // TODO Auto-generated method stub 3362 return null; 3363 } 3364 3365 @Override 3366 protected OutputStream getOutputStream() throws IOException { 3367 // TODO Auto-generated method stub 3368 return null; 3369 } 3370 3371 @Override 3372 protected void listen(int arg0) throws IOException { 3373 // TODO Auto-generated method stub 3374 3375 } 3376 3377 @Override 3378 protected void sendUrgentData(int arg0) throws IOException { 3379 // TODO Auto-generated method stub 3380 3381 } 3382 3383 public Object getOption(int arg0) throws SocketException { 3384 // TODO Auto-generated method stub 3385 return null; 3386 } 3387 3388 public void setOption(int arg0, Object arg1) throws SocketException { 3389 // TODO Auto-generated method stub 3390 3391 } 3392 3393 } 3394 3395} 3396