1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.net; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestLevel; 23 24import java.io.IOException; 25import java.io.InputStream; 26import java.io.InterruptedIOException; 27import java.io.OutputStream; 28import java.net.BindException; 29import java.net.ConnectException; 30import java.net.InetAddress; 31import java.net.InetSocketAddress; 32import java.net.ServerSocket; 33import java.net.Socket; 34import java.net.SocketAddress; 35import java.net.SocketException; 36import java.net.SocketImpl; 37import java.net.SocketImplFactory; 38import java.net.SocketTimeoutException; 39import java.net.UnknownHostException; 40import java.nio.channels.IllegalBlockingModeException; 41import java.nio.channels.ServerSocketChannel; 42import java.security.Permission; 43import java.util.Date; 44import java.util.Properties; 45 46import tests.support.Support_Configuration; 47import tests.support.Support_PortManager; 48import tests.util.TestEnvironment; 49 50@TestTargetClass(value = ServerSocket.class) 51public class ServerSocketTest extends SocketTestCase { 52 53 boolean interrupted; 54 boolean isCreateCalled = false; 55 56 ServerSocket s; 57 58 Socket sconn; 59 60 Thread t; 61 62 static class SSClient implements Runnable { 63 Socket cs; 64 65 int port; 66 67 public SSClient(int prt) { 68 port = prt; 69 } 70 71 public void run() { 72 try { 73 // Go to sleep so the server can setup and wait for connection 74 Thread.sleep(1000); 75 cs = new Socket(InetAddress.getLocalHost().getHostName(), port); 76 // Sleep again to allow server side processing. Thread is 77 // stopped by server. 78 Thread.sleep(10000); 79 } catch (InterruptedException e) { 80 return; 81 } catch (Throwable e) { 82 System.out 83 .println("Error establishing client: " + e.toString()); 84 } finally { 85 try { 86 if (cs != null) 87 cs.close(); 88 } catch (Exception e) { 89 } 90 } 91 } 92 } 93 94 SecurityManager sm = new SecurityManager() { 95 96 public void checkPermission(Permission perm) { 97 } 98 99 public void checkListen(int port) { 100 throw new SecurityException(); 101 } 102 }; 103 104 /** 105 * @tests java.net.ServerSocket#ServerSocket() 106 */ 107 @TestTargetNew( 108 level = TestLevel.COMPLETE, 109 notes = "", 110 method = "ServerSocket", 111 args = {} 112 ) 113 public void test_Constructor() { 114 ServerSocket ss = null; 115 try { 116 ss = new ServerSocket(); 117 assertEquals(-1, ss.getLocalPort()); 118 } catch (IOException e) { 119 fail("IOException was thrown."); 120 } finally { 121 try { 122 ss.close(); 123 } catch(IOException ioe) {} 124 } 125 } 126 127 /** 128 * @tests java.net.ServerSocket#ServerSocket(int) 129 */ 130 @TestTargetNew( 131 level = TestLevel.COMPLETE, 132 notes = "", 133 method = "ServerSocket", 134 args = {int.class} 135 ) 136 public void test_ConstructorI() throws Exception { 137 int portNumber = Support_PortManager.getNextPort(); 138 s = new ServerSocket(portNumber); 139 try { 140 new ServerSocket(portNumber); 141 fail("IOException was not thrown."); 142 } catch(IOException ioe) { 143 //expected 144 } 145 try { 146 startClient(s.getLocalPort()); 147 sconn = s.accept(); 148 assertNotNull("Was unable to accept connection", sconn); 149 sconn.close(); 150 } finally { 151 s.close(); 152 } 153 154 s = new ServerSocket(0); 155 try { 156 startClient(s.getLocalPort()); 157 sconn = s.accept(); 158 assertNotNull("Was unable to accept connection", sconn); 159 sconn.close(); 160 } finally { 161 s.close(); 162 } 163 164 SecurityManager oldSm = System.getSecurityManager(); 165 System.setSecurityManager(sm); 166 try { 167 new ServerSocket(0); 168 fail("SecurityException should be thrown."); 169 } catch (SecurityException e) { 170 // expected 171 } catch (SocketException e) { 172 fail("SocketException was thrown."); 173 } finally { 174 System.setSecurityManager(oldSm); 175 } 176 } 177 178 /** 179 * @tests java.net.ServerSocket#ServerSocket(int) 180 */ 181 @TestTargetNew( 182 level = TestLevel.SUFFICIENT, 183 notes = "Regression test.", 184 method = "ServerSocket", 185 args = {int.class} 186 ) 187 public void test_ConstructorI_SocksSet() throws IOException { 188 // Harmony-623 regression test 189 ServerSocket ss = null; 190 Properties props = (Properties) System.getProperties().clone(); 191 try { 192 System.setProperty("socksProxyHost", "127.0.0.1"); 193 System.setProperty("socksProxyPort", "12345"); 194 ss = new ServerSocket(0); 195 } finally { 196 System.setProperties(props); 197 if (null != ss) { 198 ss.close(); 199 } 200 } 201 } 202 203 /** 204 * @tests java.net.ServerSocket#ServerSocket(int, int) 205 */ 206 @TestTargetNew( 207 level = TestLevel.SUFFICIENT, 208 notes = "Doesn't check backlog.", 209 method = "ServerSocket", 210 args = {int.class, int.class} 211 ) 212 public void test_ConstructorII() throws IOException { 213 int freePortNumber = Support_PortManager.getNextPort(); 214 try { 215 s = new ServerSocket(freePortNumber, 1); 216 s.setSoTimeout(2000); 217 startClient(freePortNumber); 218 sconn = s.accept(); 219 220 } catch (InterruptedIOException e) { 221 fail("InterruptedIOException was thrown."); 222 } finally { 223 try { 224 sconn.close(); 225 s.close(); 226 } catch(IOException ioe) {} 227 } 228 229 SecurityManager oldSm = System.getSecurityManager(); 230 System.setSecurityManager(sm); 231 try { 232 new ServerSocket(0, 0); 233 fail("SecurityException should be thrown."); 234 } catch (SecurityException e) { 235 // expected 236 } catch (SocketException e) { 237 fail("SocketException was thrown."); 238 } finally { 239 System.setSecurityManager(oldSm); 240 } 241 242 int portNumber = Support_PortManager.getNextPort(); 243 new ServerSocket(portNumber, 0); 244 try { 245 new ServerSocket(portNumber, 0); 246 fail("IOExcepion was not thrown."); 247 } catch(IOException ioe) { 248 //expected 249 } 250 } 251 252 /** 253 * @tests java.net.ServerSocket#ServerSocket(int, int, java.net.InetAddress) 254 */ 255 @TestTargetNew( 256 level = TestLevel.SUFFICIENT, 257 notes = "Doesn't check backlog value.", 258 method = "ServerSocket", 259 args = {int.class, int.class, java.net.InetAddress.class} 260 ) 261 public void test_ConstructorIILjava_net_InetAddress() 262 throws UnknownHostException, IOException { 263 s = new ServerSocket(0, 10, InetAddress.getLocalHost()); 264 try { 265 s.setSoTimeout(5000); 266 startClient(s.getLocalPort()); 267 sconn = s.accept(); 268 assertNotNull("Was unable to accept connection", sconn); 269 sconn.close(); 270 } finally { 271 s.close(); 272 } 273 274 int freePortNumber = Support_PortManager.getNextPort(); 275 ServerSocket ss = new ServerSocket(freePortNumber, 10, 276 InetAddress.getLocalHost()); 277 278 try { 279 new ServerSocket(freePortNumber, 10, 280 InetAddress.getLocalHost()); 281 fail("IOException was not thrown."); 282 } catch(IOException ioe) { 283 //expected 284 } 285 286 try { 287 new ServerSocket(65536, 10, 288 InetAddress.getLocalHost()); 289 fail("IllegalArgumentException was not thrown."); 290 } catch(IllegalArgumentException iae) { 291 //expected 292 } 293 294 SecurityManager oldSm = System.getSecurityManager(); 295 System.setSecurityManager(sm); 296 try { 297 new ServerSocket(0, 10, InetAddress.getLocalHost()); 298 fail("SecurityException should be thrown."); 299 } catch (SecurityException e) { 300 // expected 301 } catch (SocketException e) { 302 fail("SocketException was thrown."); 303 } finally { 304 System.setSecurityManager(oldSm); 305 } 306 307 int portNumber = Support_PortManager.getNextPort(); 308 new ServerSocket(portNumber, 0); 309 try { 310 new ServerSocket(portNumber, 0); 311 fail("IOExcepion was not thrown."); 312 } catch(IOException ioe) { 313 //expected 314 } 315 } 316 317 /** 318 * @tests java.net.ServerSocket#accept() 319 */ 320 @TestTargetNew( 321 level = TestLevel.SUFFICIENT, 322 notes = "IOException is not checked.", 323 method = "accept", 324 args = {} 325 ) 326 public void test_accept() throws IOException { 327 s = new ServerSocket(0); 328 try { 329 s.setSoTimeout(5000); 330 startClient(s.getLocalPort()); 331 sconn = s.accept(); 332 int localPort1 = s.getLocalPort(); 333 int localPort2 = sconn.getLocalPort(); 334 sconn.close(); 335 assertEquals("Bad local port value", localPort1, localPort2); 336 } finally { 337 s.close(); 338 } 339 340 try { 341 interrupted = false; 342 final ServerSocket ss = new ServerSocket(0); 343 ss.setSoTimeout(12000); 344 Runnable runnable = new Runnable() { 345 public void run() { 346 try { 347 ss.accept(); 348 } catch (InterruptedIOException e) { 349 interrupted = true; 350 } catch (IOException e) { 351 } 352 } 353 }; 354 Thread thread = new Thread(runnable, "ServerSocket.accept"); 355 thread.start(); 356 try { 357 do { 358 Thread.sleep(500); 359 } while (!thread.isAlive()); 360 } catch (InterruptedException e) { 361 } 362 ss.close(); 363 int c = 0; 364 do { 365 try { 366 Thread.sleep(500); 367 } catch (InterruptedException e) { 368 } 369 if (interrupted) { 370 fail("accept interrupted"); 371 } 372 if (++c > 4) { 373 fail("accept call did not exit"); 374 } 375 } while (thread.isAlive()); 376 377 interrupted = false; 378 ServerSocket ss2 = new ServerSocket(0); 379 ss2.setSoTimeout(500); 380 Date start = new Date(); 381 try { 382 ss2.accept(); 383 } catch (InterruptedIOException e) { 384 interrupted = true; 385 } 386 assertTrue("accept not interrupted", interrupted); 387 Date finish = new Date(); 388 int delay = (int) (finish.getTime() - start.getTime()); 389 assertTrue("timeout too soon: " + delay + " " + start.getTime() 390 + " " + finish.getTime(), delay >= 490); 391 ss2.close(); 392 } catch (IOException e) { 393 fail("Unexpected IOException : " + e.getMessage()); 394 } 395 396 int portNumber = Support_PortManager.getNextPort(); 397 ServerSocket serSocket = new ServerSocket(portNumber); 398 startClient(portNumber); 399 400 SecurityManager sm = new SecurityManager() { 401 402 public void checkPermission(Permission perm) { 403 } 404 405 public void checkAccept(String host, 406 int port) { 407 throw new SecurityException(); 408 } 409 }; 410 411 SecurityManager oldSm = System.getSecurityManager(); 412 System.setSecurityManager(sm); 413 try { 414 serSocket.accept(); 415 fail("SecurityException should be thrown."); 416 } catch (SecurityException e) { 417 // expected 418 } catch (SocketException e) { 419 fail("SocketException was thrown."); 420 } finally { 421 System.setSecurityManager(oldSm); 422 serSocket.close(); 423 } 424 425 ServerSocket newSocket = new ServerSocket(portNumber); 426 newSocket.setSoTimeout(500); 427 428 try { 429 newSocket.accept(); 430 fail("SocketTimeoutException was not thrown."); 431 } catch(SocketTimeoutException ste) { 432 //expected 433 } finally { 434 newSocket.close(); 435 } 436 437 ServerSocketChannel ssc = ServerSocketChannel.open(); 438 ServerSocket ss = ssc.socket(); 439 440 try { 441 ss.accept(); 442 fail("IllegalBlockingModeException was not thrown."); 443 } catch(IllegalBlockingModeException ibme) { 444 //expected 445 } finally { 446 ss.close(); 447 ssc.close(); 448 } 449 } 450 451 /** 452 * @tests java.net.ServerSocket#close() 453 */ 454 @TestTargetNew( 455 level = TestLevel.SUFFICIENT, 456 notes = "IOException checking missed.", 457 method = "close", 458 args = {} 459 ) 460 public void test_close() throws IOException { 461 try { 462 s = new ServerSocket(0); 463 try { 464 s.close(); 465 s.accept(); 466 fail("Close test failed"); 467 } catch (SocketException e) { 468 // expected; 469 } 470 } finally { 471 s.close(); 472 } 473 } 474 475 /** 476 * @tests java.net.ServerSocket#getInetAddress() 477 */ 478 @TestTargetNew( 479 level = TestLevel.COMPLETE, 480 notes = "", 481 method = "getInetAddress", 482 args = {} 483 ) 484 public void test_getInetAddress() throws IOException { 485 InetAddress addr = InetAddress.getLocalHost(); 486 s = new ServerSocket(0, 10, addr); 487 try { 488 assertEquals("Returned incorrect InetAdrees", addr, s 489 .getInetAddress()); 490 } finally { 491 s.close(); 492 } 493 } 494 495 /** 496 * @tests java.net.ServerSocket#getLocalPort() 497 */ 498 @TestTargetNew( 499 level = TestLevel.PARTIAL_COMPLETE, 500 notes = "", 501 method = "getLocalPort", 502 args = {} 503 ) 504 public void test_getLocalPort() throws IOException { 505 // Try a specific port number, but don't complain if we don't get it 506 int portNumber = 63024; // I made this up 507 try { 508 try { 509 s = new ServerSocket(portNumber); 510 } catch (BindException e) { 511 // we could not get the port, give up 512 return; 513 } 514 assertEquals("Returned incorrect port", portNumber, s 515 .getLocalPort()); 516 } finally { 517 s.close(); 518 } 519 } 520 521 /** 522 * @tests java.net.ServerSocket#getSoTimeout() 523 */ 524 @TestTargetNew( 525 level = TestLevel.COMPLETE, 526 notes = "", 527 method = "getSoTimeout", 528 args = {} 529 ) 530 public void test_getSoTimeout() throws IOException { 531 s = new ServerSocket(0); 532 try { 533 s.setSoTimeout(100); 534 assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout()); 535 } finally { 536 s.close(); 537 } 538 try { 539 ServerSocket newSocket = new ServerSocket(); 540 newSocket.close(); 541 try { 542 newSocket.setSoTimeout(100); 543 fail("SocketException was not thrown."); 544 } catch(SocketException e) { 545 //expected 546 } 547 } catch(Exception e) { 548 fail("Unexpected exception."); 549 } 550 } 551 552 /** 553 * @tests java.net.ServerSocket#setSoTimeout(int) 554 */ 555 @TestTargetNew( 556 level = TestLevel.COMPLETE, 557 notes = "", 558 method = "setSoTimeout", 559 args = {int.class} 560 ) 561 public void test_setSoTimeoutI() throws IOException { 562 // Timeout should trigger and throw InterruptedIOException 563 try { 564 s = new ServerSocket(0); 565 s.setSoTimeout(100); 566 s.accept(); 567 } catch (InterruptedIOException e) { 568 try { 569 assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout()); 570 return; 571 } catch (Exception x) { 572 fail("Exception during setSOTimeout: " + e.toString()); 573 } 574 } catch (IOException iox) { 575 fail("IOException during setSotimeout: " + iox.toString()); 576 } 577 578 // Timeout should not trigger in this case 579 s = new ServerSocket(0); 580 startClient(s.getLocalPort()); 581 s.setSoTimeout(10000); 582 sconn = s.accept(); 583 584 ServerSocket newSocket = new ServerSocket(); 585 newSocket.close(); 586 try { 587 newSocket.setSoTimeout(100); 588 fail("SocketException was not thrown."); 589 } catch(SocketException se) { 590 //expected 591 } 592 } 593 594 /** 595 * @tests java.net.ServerSocket#toString() 596 */ 597 @TestTargetNew( 598 level = TestLevel.COMPLETE, 599 notes = "", 600 method = "toString", 601 args = {} 602 ) 603 public void test_toString() throws Exception { 604 s = new ServerSocket(0); 605 try { 606 int portNumber = s.getLocalPort(); 607 assertTrue(s.toString().contains("" + portNumber)); 608 } finally { 609 try { 610 s.close(); 611 } catch(Exception e) { 612 613 } 614 } 615 } 616 617 /** 618 * @tests java.net.ServerSocket#bind(java.net.SocketAddress) 619 */ 620 621 @TestTargetNew( 622 level = TestLevel.COMPLETE, 623 notes = "", 624 method = "bind", 625 args = {java.net.SocketAddress.class} 626 ) 627 public void test_bindLjava_net_SocketAddress() throws IOException { 628 class mySocketAddress extends SocketAddress { 629 public mySocketAddress() { 630 } 631 } 632 // create servers socket, bind it and then validate basic state 633 ServerSocket theSocket = new ServerSocket(); 634 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 635 .getLocalHost(), 0); 636 theSocket.bind(theAddress); 637 int portNumber = theSocket.getLocalPort(); 638 assertTrue( 639 "Returned incorrect InetSocketAddress(2):" 640 + theSocket.getLocalSocketAddress().toString() 641 + "Expected: " 642 + (new InetSocketAddress(InetAddress.getLocalHost(), 643 portNumber)).toString(), theSocket 644 .getLocalSocketAddress().equals( 645 new InetSocketAddress(InetAddress 646 .getLocalHost(), portNumber))); 647 assertTrue("Server socket not bound when it should be:", theSocket 648 .isBound()); 649 650 // now make sure that it is actually bound and listening on the 651 // address we provided 652 Socket clientSocket = new Socket(); 653 InetSocketAddress clAddress = new InetSocketAddress(InetAddress 654 .getLocalHost(), portNumber); 655 clientSocket.connect(clAddress); 656 Socket servSock = theSocket.accept(); 657 658 assertEquals(clAddress, clientSocket.getRemoteSocketAddress()); 659 theSocket.close(); 660 servSock.close(); 661 clientSocket.close(); 662 663 // validate we can specify null for the address in the bind and all 664 // goes ok 665 theSocket = new ServerSocket(); 666 theSocket.bind(null); 667 theSocket.close(); 668 669 // Address that we have already bound to 670 theSocket = new ServerSocket(); 671 ServerSocket theSocket2 = new ServerSocket(); 672 try { 673 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 674 theSocket.bind(theAddress); 675 SocketAddress localAddress = theSocket.getLocalSocketAddress(); 676 theSocket2.bind(localAddress); 677 fail("No exception binding to address that is not available"); 678 } catch (IOException ex) { 679 } 680 theSocket.close(); 681 theSocket2.close(); 682 683 // validate we get io address when we try to bind to address we 684 // cannot bind to 685 theSocket = new ServerSocket(); 686 try { 687 theSocket.bind(new InetSocketAddress(InetAddress 688 .getByAddress(Support_Configuration.nonLocalAddressBytes), 689 0)); 690 fail("No exception was thrown when binding to bad address"); 691 } catch (IOException ex) { 692 } 693 theSocket.close(); 694 695 // now validate case where we pass in an unsupported subclass of 696 // SocketAddress 697 theSocket = new ServerSocket(); 698 try { 699 theSocket.bind(new mySocketAddress()); 700 fail("No exception when binding using unsupported SocketAddress subclass"); 701 } catch (IllegalArgumentException ex) { 702 } 703 theSocket.close(); 704 705 706 ServerSocket serSocket = new ServerSocket(); 707 708 SecurityManager oldSm = System.getSecurityManager(); 709 System.setSecurityManager(sm); 710 try { 711 serSocket.bind(theAddress); 712 fail("SecurityException should be thrown."); 713 } catch (SecurityException e) { 714 // expected 715 } catch (SocketException e) { 716 fail("SocketException was thrown."); 717 } finally { 718 System.setSecurityManager(oldSm); 719 serSocket.close(); 720 } 721 } 722 723 /** 724 * @tests java.net.ServerSocket#bind(java.net.SocketAddress,int) 725 */ 726 @TestTargetNew( 727 level = TestLevel.COMPLETE, 728 notes = "", 729 method = "bind", 730 args = {java.net.SocketAddress.class, int.class} 731 ) 732 public void test_bindLjava_net_SocketAddressI() throws IOException { 733 class mySocketAddress extends SocketAddress { 734 735 public mySocketAddress() { 736 } 737 } 738 739 // create servers socket, bind it and then validate basic state 740 ServerSocket theSocket = new ServerSocket(); 741 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 742 .getLocalHost(), 0); 743 theSocket.bind(theAddress, 5); 744 int portNumber = theSocket.getLocalPort(); 745 assertTrue( 746 "Returned incorrect InetSocketAddress(2):" 747 + theSocket.getLocalSocketAddress().toString() 748 + "Expected: " 749 + (new InetSocketAddress(InetAddress.getLocalHost(), 750 portNumber)).toString(), theSocket 751 .getLocalSocketAddress().equals( 752 new InetSocketAddress(InetAddress 753 .getLocalHost(), portNumber))); 754 assertTrue("Server socket not bound when it should be:", theSocket 755 .isBound()); 756 757 // now make sure that it is actually bound and listening on the 758 // address we provided 759 SocketAddress localAddress = theSocket.getLocalSocketAddress(); 760 Socket clientSocket = new Socket(); 761 clientSocket.connect(localAddress); 762 Socket servSock = theSocket.accept(); 763 764 assertTrue(clientSocket.getRemoteSocketAddress().equals(localAddress)); 765 theSocket.close(); 766 servSock.close(); 767 clientSocket.close(); 768 769 // validate we can specify null for the address in the bind and all 770 // goes ok 771 theSocket = new ServerSocket(); 772 theSocket.bind(null, 5); 773 theSocket.close(); 774 775 // Address that we have already bound to 776 theSocket = new ServerSocket(); 777 ServerSocket theSocket2 = new ServerSocket(); 778 try { 779 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 780 theSocket.bind(theAddress, 5); 781 SocketAddress inuseAddress = theSocket.getLocalSocketAddress(); 782 theSocket2.bind(inuseAddress, 5); 783 fail("No exception binding to address that is not available"); 784 } catch (IOException ex) { 785 // expected 786 } 787 theSocket.close(); 788 theSocket2.close(); 789 790 // validate we get ioException when we try to bind to address we 791 // cannot bind to 792 theSocket = new ServerSocket(); 793 try { 794 theSocket.bind(new InetSocketAddress(InetAddress 795 .getByAddress(Support_Configuration.nonLocalAddressBytes), 796 0), 5); 797 fail("No exception was thrown when binding to bad address"); 798 } catch (IOException ex) { 799 } 800 theSocket.close(); 801 802 // now validate case where we pass in an unsupported subclass of 803 // SocketAddress 804 theSocket = new ServerSocket(); 805 try { 806 theSocket.bind(new mySocketAddress(), 5); 807 fail("Binding using unsupported SocketAddress subclass should have thrown exception"); 808 } catch (IllegalArgumentException ex) { 809 } 810 theSocket.close(); 811 812 // now validate that backlog is respected. We have to do a test that 813 // checks if it is a least a certain number as some platforms make 814 // it higher than we request. Unfortunately non-server versions of 815 // windows artificially limit the backlog to 5 and 5 is the 816 // historical default so it it not a great test. 817 theSocket = new ServerSocket(); 818 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 819 theSocket.bind(theAddress, 4); 820 localAddress = theSocket.getLocalSocketAddress(); 821 Socket theSockets[] = new Socket[4]; 822 int i = 0; 823 try { 824 for (i = 0; i < 4; i++) { 825 theSockets[i] = new Socket(); 826 theSockets[i].connect(localAddress); 827 } 828 } catch (ConnectException ex) { 829 fail("Backlog does not seem to be respected in bind:" + i + ":" 830 + ex.toString()); 831 } 832 833 for (i = 0; i < 4; i++) { 834 theSockets[i].close(); 835 } 836 837 theSocket.close(); 838 servSock.close(); 839 840 ServerSocket serSocket = new ServerSocket(); 841 842 SecurityManager oldSm = System.getSecurityManager(); 843 System.setSecurityManager(sm); 844 try { 845 serSocket.bind(theAddress, 5); 846 fail("SecurityException should be thrown."); 847 } catch (SecurityException e) { 848 // expected 849 } catch (SocketException e) { 850 fail("SocketException was thrown."); 851 } finally { 852 System.setSecurityManager(oldSm); 853 serSocket.close(); 854 } 855 } 856 857 /** 858 * @tests java.net.ServerSocket#getLocalSocketAddress() 859 */ 860 @TestTargetNew( 861 level = TestLevel.COMPLETE, 862 notes = "", 863 method = "getLocalSocketAddress", 864 args = {} 865 ) 866 public void test_getLocalSocketAddress() { 867 // set up server connect and then validate that we get the right 868 // response for the local address 869 try { 870 ServerSocket theSocket = new ServerSocket(0, 5, InetAddress 871 .getLocalHost()); 872 int portNumber = theSocket.getLocalPort(); 873 assertTrue("Returned incorrect InetSocketAddress(1):" 874 + theSocket.getLocalSocketAddress().toString() 875 + "Expected: " 876 + (new InetSocketAddress(InetAddress.getLocalHost(), 877 portNumber)).toString(), theSocket 878 .getLocalSocketAddress().equals( 879 new InetSocketAddress(InetAddress.getLocalHost(), 880 portNumber))); 881 theSocket.close(); 882 883 // now create a socket that is not bound and validate we get the 884 // right answer 885 theSocket = new ServerSocket(); 886 assertNull( 887 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 888 theSocket.getLocalSocketAddress()); 889 890 // now bind the socket and make sure we get the right answer 891 theSocket 892 .bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 893 int localPort = theSocket.getLocalPort(); 894 assertEquals("Returned incorrect InetSocketAddress(2):", theSocket 895 .getLocalSocketAddress(), new InetSocketAddress(InetAddress 896 .getLocalHost(), localPort)); 897 theSocket.close(); 898 } catch (Exception e) { 899 fail("Exception during getLocalSocketAddress test: " + e); 900 } 901 } 902 903 /** 904 * @tests java.net.ServerSocket#isBound() 905 */ 906 @TestTargetNew( 907 level = TestLevel.COMPLETE, 908 notes = "", 909 method = "isBound", 910 args = {} 911 ) 912 public void test_isBound() throws IOException { 913 InetAddress addr = InetAddress.getLocalHost(); 914 ServerSocket serverSocket = new ServerSocket(); 915 assertFalse("Socket indicated bound when it should be (1)", 916 serverSocket.isBound()); 917 918 // now bind and validate bound ok 919 serverSocket.bind(new InetSocketAddress(addr, 0)); 920 assertTrue("Socket indicated not bound when it should be (1)", 921 serverSocket.isBound()); 922 serverSocket.close(); 923 924 // now do with some of the other constructors 925 serverSocket = new ServerSocket(0); 926 assertTrue("Socket indicated not bound when it should be (2)", 927 serverSocket.isBound()); 928 serverSocket.close(); 929 930 serverSocket = new ServerSocket(0, 5, addr); 931 assertTrue("Socket indicated not bound when it should be (3)", 932 serverSocket.isBound()); 933 serverSocket.close(); 934 935 serverSocket = new ServerSocket(0, 5); 936 assertTrue("Socket indicated not bound when it should be (4)", 937 serverSocket.isBound()); 938 serverSocket.close(); 939 } 940 941 /** 942 * @tests java.net.ServerSocket#isClosed() 943 */ 944 @TestTargetNew( 945 level = TestLevel.COMPLETE, 946 notes = "", 947 method = "isClosed", 948 args = {} 949 ) 950 public void test_isClosed() throws IOException { 951 InetAddress addr = InetAddress.getLocalHost(); 952 ServerSocket serverSocket = new ServerSocket(0, 5, addr); 953 954 // validate isClosed returns expected values 955 assertFalse("Socket should indicate it is not closed(1):", serverSocket 956 .isClosed()); 957 serverSocket.close(); 958 assertTrue("Socket should indicate it is closed(1):", serverSocket 959 .isClosed()); 960 961 // now do with some of the other constructors 962 serverSocket = new ServerSocket(0); 963 assertFalse("Socket should indicate it is not closed(1):", serverSocket 964 .isClosed()); 965 serverSocket.close(); 966 assertTrue("Socket should indicate it is closed(1):", serverSocket 967 .isClosed()); 968 969 serverSocket = new ServerSocket(0, 5, addr); 970 assertFalse("Socket should indicate it is not closed(1):", serverSocket 971 .isClosed()); 972 serverSocket.close(); 973 assertTrue("Socket should indicate it is closed(1):", serverSocket 974 .isClosed()); 975 976 serverSocket = new ServerSocket(0, 5); 977 assertFalse("Socket should indicate it is not closed(1):", serverSocket 978 .isClosed()); 979 serverSocket.close(); 980 assertTrue("Socket should indicate it is closed(1):", serverSocket 981 .isClosed()); 982 } 983 984 /** 985 * @tests java.net.ServerSocket#setReuseAddress(boolean) 986 */ 987 @TestTargetNew( 988 level = TestLevel.COMPLETE, 989 notes = "", 990 method = "setReuseAddress", 991 args = {boolean.class} 992 ) 993 public void test_setReuseAddressZ() { 994 try { 995 // set up server and connect 996 InetSocketAddress anyAddress = new InetSocketAddress(InetAddress 997 .getLocalHost(), 0); 998 ServerSocket serverSocket = new ServerSocket(); 999 serverSocket.setReuseAddress(false); 1000 serverSocket.bind(anyAddress); 1001 SocketAddress theAddress = serverSocket.getLocalSocketAddress(); 1002 1003 // make a connection to the server, then close the server 1004 Socket theSocket = new Socket(); 1005 theSocket.connect(theAddress); 1006 Socket stillActiveSocket = serverSocket.accept(); 1007 serverSocket.close(); 1008 1009 // now try to rebind the server which should fail with 1010 // setReuseAddress to false. On windows platforms the bind is 1011 // allowed even then reUseAddress is false so our test uses 1012 // the platform to determine what the expected result is. 1013 String platform = System.getProperty("os.name"); 1014 try { 1015 serverSocket = new ServerSocket(); 1016 serverSocket.setReuseAddress(false); 1017 serverSocket.bind(theAddress); 1018 if ((!platform.startsWith("Windows"))) { 1019 fail("No exception when setReuseAddress is false and we bind:" 1020 + theAddress.toString()); 1021 } 1022 } catch (IOException ex) { 1023 if (platform.startsWith("Windows")) { 1024 fail("Got unexpected exception when binding with setReuseAddress false on windows platform:" 1025 + theAddress.toString() + ":" + ex.toString()); 1026 } 1027 } 1028 stillActiveSocket.close(); 1029 theSocket.close(); 1030 1031 // now test case were we set it to true 1032 anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 1033 serverSocket = new ServerSocket(); 1034 serverSocket.setReuseAddress(true); 1035 serverSocket.bind(anyAddress); 1036 theAddress = serverSocket.getLocalSocketAddress(); 1037 1038 // make a connection to the server, then close the server 1039 theSocket = new Socket(); 1040 theSocket.connect(theAddress); 1041 stillActiveSocket = serverSocket.accept(); 1042 serverSocket.close(); 1043 1044 // now try to rebind the server which should pass with 1045 // setReuseAddress to true 1046 try { 1047 serverSocket = new ServerSocket(); 1048 serverSocket.setReuseAddress(true); 1049 serverSocket.bind(theAddress); 1050 } catch (IOException ex) { 1051 fail("Unexpected exception when setReuseAddress is true and we bind:" 1052 + theAddress.toString() + ":" + ex.toString()); 1053 } 1054 stillActiveSocket.close(); 1055 theSocket.close(); 1056 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 1057 1058 // now test default case were we expect this to work regardless of 1059 // the value set 1060 anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 1061 serverSocket = new ServerSocket(); 1062 serverSocket.bind(anyAddress); 1063 theAddress = serverSocket.getLocalSocketAddress(); 1064 1065 // make a connection to the server, then close the server 1066 theSocket = new Socket(); 1067 theSocket.connect(theAddress); 1068 stillActiveSocket = serverSocket.accept(); 1069 serverSocket.close(); 1070 1071 // now try to rebind the server which should pass 1072 try { 1073 serverSocket = new ServerSocket(); 1074 serverSocket.bind(theAddress); 1075 } catch (IOException ex) { 1076 fail("Unexpected exception when setReuseAddress is the default case and we bind:" 1077 + theAddress.toString() + ":" + ex.toString()); 1078 } 1079 stillActiveSocket.close(); 1080 theSocket.close(); 1081 try { 1082 theSocket.setReuseAddress(true); 1083 fail("SocketException was not thrown."); 1084 } catch(SocketException se) { 1085 //expected 1086 } 1087 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 1088 } catch (Exception e) { 1089 handleException(e, SO_REUSEADDR); 1090 } 1091 } 1092 1093 /** 1094 * @tests java.net.ServerSocket#getReuseAddress() 1095 */ 1096 @TestTargetNew( 1097 level = TestLevel.COMPLETE, 1098 notes = "", 1099 method = "getReuseAddress", 1100 args = {} 1101 ) 1102 public void test_getReuseAddress() { 1103 try { 1104 ServerSocket theSocket = new ServerSocket(); 1105 theSocket.setReuseAddress(true); 1106 assertTrue("getReuseAddress false when it should be true", 1107 theSocket.getReuseAddress()); 1108 theSocket.setReuseAddress(false); 1109 assertFalse("getReuseAddress true when it should be False", 1110 theSocket.getReuseAddress()); 1111 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 1112 } catch (Exception e) { 1113 handleException(e, SO_REUSEADDR); 1114 } 1115 1116 try { 1117 ServerSocket newSocket = new ServerSocket(); 1118 newSocket.close(); 1119 try { 1120 newSocket.getReuseAddress(); 1121 fail("SocketException was not thrown."); 1122 } catch(SocketException e) { 1123 //expected 1124 } 1125 } catch(Exception e) { 1126 fail("Unexpected exception."); 1127 } 1128 } 1129 1130 /** 1131 * @tests java.net.ServerSocket#setReceiveBufferSize(int) 1132 */ 1133 @TestTargetNew( 1134 level = TestLevel.COMPLETE, 1135 notes = "", 1136 method = "setReceiveBufferSize", 1137 args = {int.class} 1138 ) 1139 public void test_setReceiveBufferSizeI() { 1140 try { 1141 // now validate case where we try to set to 0 1142 ServerSocket theSocket = new ServerSocket(); 1143 try { 1144 theSocket.setReceiveBufferSize(0); 1145 fail("No exception when receive buffer size set to 0"); 1146 } catch (IllegalArgumentException ex) { 1147 } 1148 theSocket.close(); 1149 1150 // now validate case where we try to set to a negative value 1151 theSocket = new ServerSocket(); 1152 try { 1153 theSocket.setReceiveBufferSize(-1000); 1154 fail("No exception when receive buffer size set to -1000"); 1155 } catch (IllegalArgumentException ex) { 1156 } 1157 theSocket.close(); 1158 1159 // now just try to set a good value to make sure it is set and there 1160 // are not exceptions 1161 theSocket = new ServerSocket(); 1162 theSocket.setReceiveBufferSize(1000); 1163 theSocket.close(); 1164 try { 1165 theSocket.setReceiveBufferSize(10); 1166 fail("SocketException was not thrown."); 1167 } catch(SocketException se) { 1168 //expected 1169 } 1170 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 1171 } catch (Exception e) { 1172 handleException(e, SO_RCVBUF); 1173 } 1174 1175 } 1176 1177 /* 1178 * @tests java.net.ServerSocket#getReceiveBufferSize() 1179 */ 1180 @TestTargetNew( 1181 level = TestLevel.COMPLETE, 1182 notes = "", 1183 method = "getReceiveBufferSize", 1184 args = {} 1185 ) 1186 public void test_getReceiveBufferSize() { 1187 try { 1188 ServerSocket theSocket = new ServerSocket(); 1189 1190 // since the value returned is not necessary what we set we are 1191 // limited in what we can test 1192 // just validate that it is not 0 or negative 1193 assertFalse("get Buffer size returns 0:", 0 == theSocket 1194 .getReceiveBufferSize()); 1195 assertFalse("get Buffer size returns a negative value:", 1196 0 > theSocket.getReceiveBufferSize()); 1197 1198 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 1199 } catch (Exception e) { 1200 handleException(e, SO_RCVBUF); 1201 } 1202 try { 1203 ServerSocket newSocket = new ServerSocket(); 1204 newSocket.close(); 1205 try { 1206 newSocket.getReceiveBufferSize(); 1207 fail("SocketException was not thrown."); 1208 } catch(SocketException e) { 1209 //expected 1210 } 1211 } catch(Exception e) { 1212 fail("Unexpected exception."); 1213 } 1214 } 1215 1216 /** 1217 * @tests java.net.ServerSocket#getChannel() 1218 */ 1219 @TestTargetNew( 1220 level = TestLevel.COMPLETE, 1221 notes = "", 1222 method = "getChannel", 1223 args = {} 1224 ) 1225 public void test_getChannel() throws Exception { 1226 assertNull(new ServerSocket().getChannel()); 1227 } 1228 1229 /* 1230 * @tests java.net.ServerSocket#setPerformancePreference() 1231 */ 1232 @TestTargetNew( 1233 level = TestLevel.COMPLETE, 1234 notes = "", 1235 method = "setPerformancePreferences", 1236 args = {int.class, int.class, int.class} 1237 ) 1238 public void test_setPerformancePreference_Int_Int_Int() throws Exception { 1239 performancePreferenceTest(1, 0, 0); 1240 performancePreferenceTest(1, 1, 1); 1241 performancePreferenceTest(0, 1, 2); 1242 performancePreferenceTest(Integer.MAX_VALUE, Integer.MAX_VALUE, 1243 Integer.MAX_VALUE); 1244 } 1245 1246 void performancePreferenceTest(int connectionTime, int latency, 1247 int bandwidth) throws Exception { 1248 ServerSocket theSocket = new ServerSocket(); 1249 theSocket.setPerformancePreferences(connectionTime, latency, bandwidth); 1250 1251 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1252 .getLocalHost(), 0); 1253 theSocket.bind(theAddress); 1254 int portNumber = theSocket.getLocalPort(); 1255 assertTrue( 1256 "Returned incorrect InetSocketAddress(2):" 1257 + theSocket.getLocalSocketAddress().toString() 1258 + "Expected: " 1259 + (new InetSocketAddress(InetAddress.getLocalHost(), 1260 portNumber)).toString(), theSocket 1261 .getLocalSocketAddress().equals( 1262 new InetSocketAddress(InetAddress 1263 .getLocalHost(), portNumber))); 1264 assertTrue("Server socket not bound when it should be:", theSocket 1265 .isBound()); 1266 1267 // now make sure that it is actually bound and listening on the 1268 // address we provided 1269 Socket clientSocket = new Socket(); 1270 InetSocketAddress clAddress = new InetSocketAddress(InetAddress 1271 .getLocalHost(), portNumber); 1272 clientSocket.connect(clAddress); 1273 Socket servSock = theSocket.accept(); 1274 1275 assertEquals(clAddress, clientSocket.getRemoteSocketAddress()); 1276 theSocket.close(); 1277 servSock.close(); 1278 clientSocket.close(); 1279 } 1280 1281 /** 1282 * Sets up the fixture, for example, open a network connection. This method 1283 * is called before a test is executed. 1284 */ 1285 protected void setUp() { 1286 } 1287 1288 /** 1289 * Tears down the fixture, for example, close a network connection. This 1290 * method is called after a test is executed. 1291 */ 1292 protected void tearDown() { 1293 TestEnvironment.reset(); 1294 try { 1295 if (s != null) 1296 s.close(); 1297 if (sconn != null) 1298 sconn.close(); 1299 if (t != null) 1300 t.interrupt(); 1301 } catch (Exception e) { 1302 } 1303 } 1304 1305 /** 1306 * Sets up the fixture, for example, open a network connection. This method 1307 * is called before a test is executed. 1308 */ 1309 protected void startClient(int port) { 1310 t = new Thread(new SSClient(port), "SSClient"); 1311 t.start(); 1312 try { 1313 Thread.sleep(1000); 1314 } catch (InterruptedException e) { 1315 System.out.println("Exception during startClinet()" + e.toString()); 1316 } 1317 } 1318 1319 /** 1320 * @tests java.net.ServerSocket#implAccept 1321 */ 1322 @TestTargetNew( 1323 level = TestLevel.COMPLETE, 1324 notes = "Regression test.", 1325 method = "implAccept", 1326 args = {java.net.Socket.class} 1327 ) 1328 public void test_implAcceptLjava_net_Socket() throws Exception { 1329 // regression test for Harmony-1235 1330 try { 1331 new MockServerSocket().mockImplAccept(new MockSocket( 1332 new MockSocketImpl())); 1333 } catch (SocketException e) { 1334 // expected 1335 } 1336 } 1337 1338 class MockSocketImpl extends SocketImpl { 1339 public MockSocketImpl() { 1340 isCreateCalled = true; 1341 } 1342 1343 protected void create(boolean arg0) throws IOException { 1344 //empty 1345 } 1346 1347 protected void connect(String arg0, int arg1) throws IOException { 1348 // empty 1349 } 1350 1351 protected void connect(InetAddress arg0, int arg1) throws IOException { 1352 // empty 1353 } 1354 1355 protected void connect(SocketAddress arg0, int arg1) throws IOException { 1356 // empty 1357 } 1358 1359 protected void bind(InetAddress arg0, int arg1) throws IOException { 1360 // empty 1361 } 1362 1363 protected void listen(int arg0) throws IOException { 1364 // empty 1365 } 1366 1367 protected void accept(SocketImpl arg0) throws IOException { 1368 // empty 1369 } 1370 1371 protected InputStream getInputStream() throws IOException { 1372 return null; 1373 } 1374 1375 protected OutputStream getOutputStream() throws IOException { 1376 return null; 1377 } 1378 1379 protected int available() throws IOException { 1380 return 0; 1381 } 1382 1383 protected void close() throws IOException { 1384 // empty 1385 } 1386 1387 protected void sendUrgentData(int arg0) throws IOException { 1388 // empty 1389 } 1390 1391 public void setOption(int arg0, Object arg1) throws SocketException { 1392 // empty 1393 } 1394 1395 public Object getOption(int arg0) throws SocketException { 1396 return null; 1397 } 1398 } 1399 1400 static class MockSocket extends Socket { 1401 public MockSocket(SocketImpl impl) throws SocketException { 1402 super(impl); 1403 } 1404 } 1405 1406 static class MockServerSocket extends ServerSocket { 1407 public MockServerSocket() throws Exception { 1408 super(); 1409 } 1410 1411 public void mockImplAccept(Socket s) throws Exception { 1412 super.implAccept(s); 1413 } 1414 } 1415 1416 @TestTargetNew( 1417 level = TestLevel.PARTIAL_COMPLETE, 1418 notes = "", 1419 method = "getLocalPort", 1420 args = {} 1421 ) 1422 public void test_LocalPort() throws IOException { 1423 ServerSocket ss1 = new ServerSocket(4242); 1424 assertEquals(ss1.getLocalPort(), 4242); 1425 ss1.close(); 1426 1427 ServerSocket ss2 = new ServerSocket(); 1428 ss2.bind(new InetSocketAddress("127.0.0.1", 4343)); 1429 assertEquals(ss2.getLocalPort(), 4343); 1430 ss2.close(); 1431 1432 ServerSocket ss3 = new ServerSocket(0); 1433 assertTrue(ss3.getLocalPort() != 0); 1434 ss3.close(); 1435 } 1436 1437 /** 1438 * @tests java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) 1439 */ 1440 @TestTargetNew( 1441 level = TestLevel.SUFFICIENT, 1442 notes = "", 1443 method = "setSocketFactory", 1444 args = {java.net.SocketImplFactory.class} 1445 ) 1446 public void test_setSocketFactoryLjava_net_SocketImplFactory() { 1447 1448 SecurityManager sm = new SecurityManager() { 1449 1450 public void checkPermission(Permission perm) { 1451 } 1452 1453 public void checkSetFactory() { 1454 throw new SecurityException(); 1455 } 1456 }; 1457 1458 MockSocketFactory sf = new MockSocketFactory(); 1459 SecurityManager oldSm = System.getSecurityManager(); 1460 System.setSecurityManager(sm); 1461 try { 1462 ServerSocket.setSocketFactory(sf); 1463 fail("SecurityException should be thrown."); 1464 } catch (SecurityException e) { 1465 // expected 1466 } catch (IOException e) { 1467 fail("IOException was thrown."); 1468 } finally { 1469 System.setSecurityManager(oldSm); 1470 } 1471/* 1472* try { 1473* ServerSocket.setSocketFactory(sf); 1474* ServerSocket ss1 = new ServerSocket(); 1475* assertTrue(isCreateCalled); 1476* isCreateCalled = false; 1477* ServerSocket ss2 = new ServerSocket(0); 1478* assertTrue(isCreateCalled); 1479* } catch(IOException ioe) { 1480* fail("IOException was thrown: " + ioe.toString()); 1481* } 1482 1483* try { 1484* ServerSocket.setSocketFactory(null); 1485* fail("IOException was not thrown."); 1486* } catch(IOException ioe) { 1487* //expected 1488* } 1489*/ 1490 } 1491 1492 class MockSocketFactory implements SocketImplFactory { 1493 public SocketImpl createSocketImpl() { 1494 return new MockSocketImpl(); 1495 } 1496 } 1497} 1498