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