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