ServerSocketTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 org.apache.harmony.luni.tests.java.net; 19 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.InterruptedIOException; 23import java.io.OutputStream; 24import java.net.BindException; 25import java.net.ConnectException; 26import java.net.InetAddress; 27import java.net.InetSocketAddress; 28import java.net.ServerSocket; 29import java.net.Socket; 30import java.net.SocketAddress; 31import java.net.SocketException; 32import java.net.SocketImpl; 33import java.net.SocketImplFactory; 34import java.net.UnknownHostException; 35import java.util.Date; 36import java.util.Locale; 37import java.util.Properties; 38 39import org.apache.harmony.luni.net.PlainServerSocketImpl; 40 41import tests.support.Support_Configuration; 42import tests.support.Support_Exec; 43 44public class ServerSocketTest extends SocketTestCase { 45 46 boolean interrupted; 47 48 ServerSocket s; 49 50 Socket sconn; 51 52 Thread t; 53 54 static class SSClient implements Runnable { 55 Socket cs; 56 57 int port; 58 59 public SSClient(int prt) { 60 port = prt; 61 } 62 63 public void run() { 64 try { 65 // Go to sleep so the server can setup and wait for connection 66 Thread.sleep(1000); 67 cs = new Socket(InetAddress.getLocalHost().getHostName(), port); 68 // Sleep again to allow server side processing. Thread is 69 // stopped by server. 70 Thread.sleep(10000); 71 } catch (InterruptedException e) { 72 return; 73 } catch (Throwable e) { 74 System.out 75 .println("Error establishing client: " + e.toString()); 76 } finally { 77 try { 78 if (cs != null) 79 cs.close(); 80 } catch (Exception e) { 81 } 82 } 83 } 84 } 85 86 /** 87 * @tests java.net.ServerSocket#ServerSocket() 88 */ 89 public void test_Constructor() { 90 // Test for method java.net.ServerSocket(int) 91 assertTrue("Used during tests", true); 92 } 93 94 /** 95 * @tests java.net.ServerSocket#ServerSocket(int) 96 */ 97 public void test_ConstructorI() { 98 // Test for method java.net.ServerSocket(int) 99 assertTrue("Used during tests", true); 100 } 101 102 /** 103 * @tests java.net.ServerSocket#ServerSocket(int) 104 */ 105 public void test_ConstructorI_SocksSet() throws IOException { 106 // Harmony-623 regression test 107 ServerSocket ss = null; 108 Properties props = (Properties) System.getProperties().clone(); 109 try { 110 System.setProperty("socksProxyHost", "127.0.0.1"); 111 System.setProperty("socksProxyPort", "12345"); 112 ss = new ServerSocket(0); 113 } finally { 114 System.setProperties(props); 115 if (null != ss) { 116 ss.close(); 117 } 118 } 119 } 120 121 /** 122 * @tests java.net.ServerSocket#ServerSocket(int, int) 123 */ 124 public void test_ConstructorII() throws IOException { 125 try { 126 s = new ServerSocket(0, 10); 127 s.setSoTimeout(2000); 128 startClient(s.getLocalPort()); 129 sconn = s.accept(); 130 } catch (InterruptedIOException e) { 131 return; 132 } 133 134 ServerSocket s1 = new ServerSocket(0); 135 try { 136 try { 137 ServerSocket s2 = new ServerSocket(s1.getLocalPort()); 138 s2.close(); 139 fail("Was able to create two serversockets on same port"); 140 } catch (BindException e) { 141 // Expected 142 } 143 } finally { 144 s1.close(); 145 } 146 147 s1 = new ServerSocket(0); 148 int allocatedPort = s1.getLocalPort(); 149 s1.close(); 150 s1 = new ServerSocket(allocatedPort); 151 s1.close(); 152 } 153 154 /** 155 * @tests java.net.ServerSocket#ServerSocket(int, int, java.net.InetAddress) 156 */ 157 public void test_ConstructorIILjava_net_InetAddress() 158 throws UnknownHostException, IOException { 159 s = new ServerSocket(0, 10, InetAddress.getLocalHost()); 160 try { 161 s.setSoTimeout(5000); 162 startClient(s.getLocalPort()); 163 sconn = s.accept(); 164 assertNotNull("Was unable to accept connection", sconn); 165 sconn.close(); 166 } finally { 167 s.close(); 168 } 169 } 170 171 /** 172 * @tests java.net.ServerSocket#accept() 173 */ 174 public void test_accept() throws IOException { 175 s = new ServerSocket(0); 176 try { 177 s.setSoTimeout(5000); 178 startClient(s.getLocalPort()); 179 sconn = s.accept(); 180 int localPort1 = s.getLocalPort(); 181 int localPort2 = sconn.getLocalPort(); 182 sconn.close(); 183 assertEquals("Bad local port value", localPort1, localPort2); 184 } finally { 185 s.close(); 186 } 187 188 try { 189 interrupted = false; 190 final ServerSocket ss = new ServerSocket(0); 191 ss.setSoTimeout(12000); 192 Runnable runnable = new Runnable() { 193 public void run() { 194 try { 195 ss.accept(); 196 } catch (InterruptedIOException e) { 197 interrupted = true; 198 } catch (IOException e) { 199 } 200 } 201 }; 202 Thread thread = new Thread(runnable, "ServerSocket.accept"); 203 thread.start(); 204 try { 205 do { 206 Thread.sleep(500); 207 } while (!thread.isAlive()); 208 } catch (InterruptedException e) { 209 } 210 ss.close(); 211 int c = 0; 212 do { 213 try { 214 Thread.sleep(500); 215 } catch (InterruptedException e) { 216 } 217 if (interrupted) { 218 fail("accept interrupted"); 219 } 220 if (++c > 4) { 221 fail("accept call did not exit"); 222 } 223 } while (thread.isAlive()); 224 225 interrupted = false; 226 ServerSocket ss2 = new ServerSocket(0); 227 ss2.setSoTimeout(500); 228 Date start = new Date(); 229 try { 230 ss2.accept(); 231 } catch (InterruptedIOException e) { 232 interrupted = true; 233 } 234 assertTrue("accept not interrupted", interrupted); 235 Date finish = new Date(); 236 int delay = (int) (finish.getTime() - start.getTime()); 237 assertTrue("timeout too soon: " + delay + " " + start.getTime() 238 + " " + finish.getTime(), delay >= 490); 239 ss2.close(); 240 } catch (IOException e) { 241 fail("Unexpected IOException : " + e.getMessage()); 242 } 243 } 244 245 /** 246 * @tests java.net.ServerSocket#close() 247 */ 248 public void test_close() throws IOException { 249 try { 250 s = new ServerSocket(0); 251 try { 252 s.close(); 253 s.accept(); 254 fail("Close test failed"); 255 } catch (SocketException e) { 256 // expected; 257 } 258 } finally { 259 s.close(); 260 } 261 } 262 263 /** 264 * @tests java.net.ServerSocket#getInetAddress() 265 */ 266 public void test_getInetAddress() throws IOException { 267 InetAddress addr = InetAddress.getLocalHost(); 268 s = new ServerSocket(0, 10, addr); 269 try { 270 assertEquals("Returned incorrect InetAdrees", addr, s 271 .getInetAddress()); 272 } finally { 273 s.close(); 274 } 275 } 276 277 /** 278 * @tests java.net.ServerSocket#getLocalPort() 279 */ 280 public void test_getLocalPort() throws IOException { 281 // Try a specific port number, but don't complain if we don't get it 282 int portNumber = 63024; // I made this up 283 try { 284 try { 285 s = new ServerSocket(portNumber); 286 } catch (BindException e) { 287 // we could not get the port, give up 288 return; 289 } 290 assertEquals("Returned incorrect port", portNumber, s 291 .getLocalPort()); 292 } finally { 293 s.close(); 294 } 295 } 296 297 /** 298 * @tests java.net.ServerSocket#getSoTimeout() 299 */ 300 public void test_getSoTimeout() throws IOException { 301 s = new ServerSocket(0); 302 try { 303 s.setSoTimeout(100); 304 assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout()); 305 } finally { 306 s.close(); 307 } 308 } 309 310 /** 311 * @tests java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) 312 */ 313 public void test_setSocketFactoryLjava_net_SocketImplFactory() 314 throws IOException { 315 SocketImplFactory factory = new MockSocketImplFactory(); 316 // Should not throw SocketException when set DatagramSocketImplFactory 317 // for the first time. 318 ServerSocket.setSocketFactory(factory); 319 320 try { 321 ServerSocket.setSocketFactory(null); 322 fail("Should throw SocketException"); 323 } catch (SocketException e) { 324 // Expected 325 } 326 327 try { 328 ServerSocket.setSocketFactory(factory); 329 fail("Should throw SocketException"); 330 } catch (SocketException e) { 331 // Expected 332 } 333 } 334 335 private static class MockSocketImplFactory implements SocketImplFactory { 336 public SocketImpl createSocketImpl() { 337 return new PlainServerSocketImpl(); 338 } 339 } 340 341 /** 342 * @tests java.net.ServerSocket#setSoTimeout(int) 343 */ 344 public void test_setSoTimeoutI() throws IOException { 345 // Timeout should trigger and throw InterruptedIOException 346 try { 347 s = new ServerSocket(0); 348 s.setSoTimeout(100); 349 s.accept(); 350 } catch (InterruptedIOException e) { 351 assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout()); 352 return; 353 } 354 355 // Timeout should not trigger in this case 356 s = new ServerSocket(0); 357 startClient(s.getLocalPort()); 358 s.setSoTimeout(10000); 359 sconn = s.accept(); 360 } 361 362 /** 363 * @tests java.net.ServerSocket#toString() 364 */ 365 public void test_toString() throws Exception { 366 s = new ServerSocket(0); 367 try { 368 int portNumber = s.getLocalPort(); 369 assertEquals("ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=" 370 + portNumber + "]", s.toString()); 371 } finally { 372 s.close(); 373 } 374 } 375 376 /** 377 * @tests java.net.ServerSocket#bind(java.net.SocketAddress) 378 */ 379 public void test_bindLjava_net_SocketAddress() throws IOException { 380 class mySocketAddress extends SocketAddress { 381 public mySocketAddress() { 382 } 383 } 384 // create servers socket, bind it and then validate basic state 385 ServerSocket theSocket = new ServerSocket(); 386 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 387 .getLocalHost(), 0); 388 theSocket.bind(theAddress); 389 int portNumber = theSocket.getLocalPort(); 390 assertTrue( 391 "Returned incorrect InetSocketAddress(2):" 392 + theSocket.getLocalSocketAddress().toString() 393 + "Expected: " 394 + (new InetSocketAddress(InetAddress.getLocalHost(), 395 portNumber)).toString(), theSocket 396 .getLocalSocketAddress().equals( 397 new InetSocketAddress(InetAddress 398 .getLocalHost(), portNumber))); 399 assertTrue("Server socket not bound when it should be:", theSocket 400 .isBound()); 401 402 // now make sure that it is actually bound and listening on the 403 // address we provided 404 Socket clientSocket = new Socket(); 405 InetSocketAddress clAddress = new InetSocketAddress(InetAddress 406 .getLocalHost(), portNumber); 407 clientSocket.connect(clAddress); 408 Socket servSock = theSocket.accept(); 409 410 assertEquals(clAddress, clientSocket.getRemoteSocketAddress()); 411 theSocket.close(); 412 servSock.close(); 413 clientSocket.close(); 414 415 // validate we can specify null for the address in the bind and all 416 // goes ok 417 theSocket = new ServerSocket(); 418 theSocket.bind(null); 419 theSocket.close(); 420 421 // Address that we have already bound to 422 theSocket = new ServerSocket(); 423 ServerSocket theSocket2 = new ServerSocket(); 424 try { 425 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 426 theSocket.bind(theAddress); 427 SocketAddress localAddress = theSocket.getLocalSocketAddress(); 428 theSocket2.bind(localAddress); 429 fail("No exception binding to address that is not available"); 430 } catch (IOException ex) { 431 } 432 theSocket.close(); 433 theSocket2.close(); 434 435 // validate we get io address when we try to bind to address we 436 // cannot bind to 437 theSocket = new ServerSocket(); 438 try { 439 theSocket.bind(new InetSocketAddress(InetAddress 440 .getByAddress(Support_Configuration.nonLocalAddressBytes), 441 0)); 442 fail("No exception was thrown when binding to bad address"); 443 } catch (IOException ex) { 444 } 445 theSocket.close(); 446 447 // now validate case where we pass in an unsupported subclass of 448 // SocketAddress 449 theSocket = new ServerSocket(); 450 try { 451 theSocket.bind(new mySocketAddress()); 452 fail("No exception when binding using unsupported SocketAddress subclass"); 453 } catch (IllegalArgumentException ex) { 454 } 455 theSocket.close(); 456 } 457 458 /** 459 * @tests java.net.ServerSocket#bind(java.net.SocketAddress,int) 460 */ 461 public void test_bindLjava_net_SocketAddressI() throws IOException { 462 class mySocketAddress extends SocketAddress { 463 464 public mySocketAddress() { 465 } 466 } 467 468 // create servers socket, bind it and then validate basic state 469 ServerSocket theSocket = new ServerSocket(); 470 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 471 .getLocalHost(), 0); 472 theSocket.bind(theAddress, 5); 473 int portNumber = theSocket.getLocalPort(); 474 assertTrue( 475 "Returned incorrect InetSocketAddress(2):" 476 + theSocket.getLocalSocketAddress().toString() 477 + "Expected: " 478 + (new InetSocketAddress(InetAddress.getLocalHost(), 479 portNumber)).toString(), theSocket 480 .getLocalSocketAddress().equals( 481 new InetSocketAddress(InetAddress 482 .getLocalHost(), portNumber))); 483 assertTrue("Server socket not bound when it should be:", theSocket 484 .isBound()); 485 486 // now make sure that it is actually bound and listening on the 487 // address we provided 488 SocketAddress localAddress = theSocket.getLocalSocketAddress(); 489 Socket clientSocket = new Socket(); 490 clientSocket.connect(localAddress); 491 Socket servSock = theSocket.accept(); 492 493 assertTrue(clientSocket.getRemoteSocketAddress().equals(localAddress)); 494 theSocket.close(); 495 servSock.close(); 496 clientSocket.close(); 497 498 // validate we can specify null for the address in the bind and all 499 // goes ok 500 theSocket = new ServerSocket(); 501 theSocket.bind(null, 5); 502 theSocket.close(); 503 504 // Address that we have already bound to 505 theSocket = new ServerSocket(); 506 ServerSocket theSocket2 = new ServerSocket(); 507 try { 508 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 509 theSocket.bind(theAddress, 5); 510 SocketAddress inuseAddress = theSocket.getLocalSocketAddress(); 511 theSocket2.bind(inuseAddress, 5); 512 fail("No exception binding to address that is not available"); 513 } catch (IOException ex) { 514 // expected 515 } 516 theSocket.close(); 517 theSocket2.close(); 518 519 // validate we get ioException when we try to bind to address we 520 // cannot bind to 521 theSocket = new ServerSocket(); 522 try { 523 theSocket.bind(new InetSocketAddress(InetAddress 524 .getByAddress(Support_Configuration.nonLocalAddressBytes), 525 0), 5); 526 fail("No exception was thrown when binding to bad address"); 527 } catch (IOException ex) { 528 } 529 theSocket.close(); 530 531 // now validate case where we pass in an unsupported subclass of 532 // SocketAddress 533 theSocket = new ServerSocket(); 534 try { 535 theSocket.bind(new mySocketAddress(), 5); 536 fail("Binding using unsupported SocketAddress subclass should have thrown exception"); 537 } catch (IllegalArgumentException ex) { 538 } 539 theSocket.close(); 540 541 // now validate that backlog is respected. We have to do a test that 542 // checks if it is a least a certain number as some platforms make 543 // it higher than we request. Unfortunately non-server versions of 544 // windows artificially limit the backlog to 5 and 5 is the 545 // historical default so it is not a great test. 546 theSocket = new ServerSocket(); 547 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 548 theSocket.bind(theAddress, 4); 549 localAddress = theSocket.getLocalSocketAddress(); 550 Socket theSockets[] = new Socket[4]; 551 int i = 0; 552 try { 553 for (i = 0; i < 4; i++) { 554 theSockets[i] = new Socket(); 555 theSockets[i].connect(localAddress); 556 } 557 } catch (ConnectException ex) { 558 fail("Backlog does not seem to be respected in bind:" + i + ":" 559 + ex.toString()); 560 } 561 562 for (i = 0; i < 4; i++) { 563 theSockets[i].close(); 564 } 565 566 theSocket.close(); 567 servSock.close(); 568 } 569 570 /** 571 * @tests java.net.ServerSocket#getLocalSocketAddress() 572 */ 573 public void test_getLocalSocketAddress() throws Exception { 574 // set up server connect and then validate that we get the right 575 // response for the local address 576 ServerSocket theSocket = new ServerSocket(0, 5, InetAddress 577 .getLocalHost()); 578 int portNumber = theSocket.getLocalPort(); 579 assertTrue("Returned incorrect InetSocketAddress(1):" 580 + theSocket.getLocalSocketAddress().toString() 581 + "Expected: " 582 + (new InetSocketAddress(InetAddress.getLocalHost(), 583 portNumber)).toString(), theSocket 584 .getLocalSocketAddress().equals( 585 new InetSocketAddress(InetAddress.getLocalHost(), 586 portNumber))); 587 theSocket.close(); 588 589 // now create a socket that is not bound and validate we get the 590 // right answer 591 theSocket = new ServerSocket(); 592 assertNull( 593 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 594 theSocket.getLocalSocketAddress()); 595 596 // now bind the socket and make sure we get the right answer 597 theSocket 598 .bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 599 int localPort = theSocket.getLocalPort(); 600 assertEquals("Returned incorrect InetSocketAddress(2):", theSocket 601 .getLocalSocketAddress(), new InetSocketAddress(InetAddress 602 .getLocalHost(), localPort)); 603 theSocket.close(); 604 } 605 606 /** 607 * @tests java.net.ServerSocket#isBound() 608 */ 609 public void test_isBound() throws IOException { 610 InetAddress addr = InetAddress.getLocalHost(); 611 ServerSocket serverSocket = new ServerSocket(); 612 assertFalse("Socket indicated bound when it should be (1)", 613 serverSocket.isBound()); 614 615 // now bind and validate bound ok 616 serverSocket.bind(new InetSocketAddress(addr, 0)); 617 assertTrue("Socket indicated not bound when it should be (1)", 618 serverSocket.isBound()); 619 serverSocket.close(); 620 621 // now do with some of the other constructors 622 serverSocket = new ServerSocket(0); 623 assertTrue("Socket indicated not bound when it should be (2)", 624 serverSocket.isBound()); 625 serverSocket.close(); 626 627 serverSocket = new ServerSocket(0, 5, addr); 628 assertTrue("Socket indicated not bound when it should be (3)", 629 serverSocket.isBound()); 630 serverSocket.close(); 631 632 serverSocket = new ServerSocket(0, 5); 633 assertTrue("Socket indicated not bound when it should be (4)", 634 serverSocket.isBound()); 635 serverSocket.close(); 636 } 637 638 /** 639 * @tests java.net.ServerSocket#isClosed() 640 */ 641 public void test_isClosed() throws IOException { 642 InetAddress addr = InetAddress.getLocalHost(); 643 ServerSocket serverSocket = new ServerSocket(0, 5, addr); 644 645 // validate isClosed returns expected values 646 assertFalse("Socket should indicate it is not closed(1):", serverSocket 647 .isClosed()); 648 serverSocket.close(); 649 assertTrue("Socket should indicate it is closed(1):", serverSocket 650 .isClosed()); 651 652 // now do with some of the other constructors 653 serverSocket = new ServerSocket(0); 654 assertFalse("Socket should indicate it is not closed(1):", serverSocket 655 .isClosed()); 656 serverSocket.close(); 657 assertTrue("Socket should indicate it is closed(1):", serverSocket 658 .isClosed()); 659 660 serverSocket = new ServerSocket(0, 5, addr); 661 assertFalse("Socket should indicate it is not closed(1):", serverSocket 662 .isClosed()); 663 serverSocket.close(); 664 assertTrue("Socket should indicate it is closed(1):", serverSocket 665 .isClosed()); 666 667 serverSocket = new ServerSocket(0, 5); 668 assertFalse("Socket should indicate it is not closed(1):", serverSocket 669 .isClosed()); 670 serverSocket.close(); 671 assertTrue("Socket should indicate it is closed(1):", serverSocket 672 .isClosed()); 673 } 674 675 /* 676 * Regression HARMONY-6090 677 */ 678 public void test_defaultValueReuseAddress() throws Exception { 679 String platform = System.getProperty("os.name").toLowerCase(Locale.US); 680 if (!platform.startsWith("windows")) { 681 // on Unix 682 assertTrue(new ServerSocket().getReuseAddress()); 683 assertTrue(new ServerSocket(0).getReuseAddress()); 684 assertTrue(new ServerSocket(0, 50).getReuseAddress()); 685 assertTrue(new ServerSocket(0, 50, InetAddress.getLocalHost()).getReuseAddress()); 686 } else { 687 // on Windows 688 assertFalse(new ServerSocket().getReuseAddress()); 689 assertFalse(new ServerSocket(0).getReuseAddress()); 690 assertFalse(new ServerSocket(0, 50).getReuseAddress()); 691 assertFalse(new ServerSocket(0, 50, InetAddress.getLocalHost()).getReuseAddress()); 692 } 693 } 694 695 /** 696 * @tests java.net.ServerSocket#setReuseAddress(boolean) 697 */ 698 public void test_setReuseAddressZ() { 699 try { 700 // set up server and connect 701 InetSocketAddress anyAddress = new InetSocketAddress(InetAddress 702 .getLocalHost(), 0); 703 ServerSocket serverSocket = new ServerSocket(); 704 serverSocket.setReuseAddress(false); 705 serverSocket.bind(anyAddress); 706 SocketAddress theAddress = serverSocket.getLocalSocketAddress(); 707 708 // make a connection to the server, then close the server 709 Socket theSocket = new Socket(); 710 theSocket.connect(theAddress); 711 Socket stillActiveSocket = serverSocket.accept(); 712 serverSocket.close(); 713 714 // now try to rebind the server which should fail with 715 // setReuseAddress to false. On windows platforms the bind is 716 // allowed even then reUseAddress is false so our test uses 717 // the platform to determine what the expected result is. 718 String platform = System.getProperty("os.name"); 719 try { 720 serverSocket = new ServerSocket(); 721 serverSocket.setReuseAddress(false); 722 serverSocket.bind(theAddress); 723 if ((!platform.startsWith("Windows"))) { 724 fail("No exception when setReuseAddress is false and we bind:" 725 + theAddress.toString()); 726 } 727 } catch (IOException ex) { 728 if (platform.startsWith("Windows")) { 729 fail("Got unexpected exception when binding with setReuseAddress false on windows platform:" 730 + theAddress.toString() + ":" + ex.toString()); 731 } 732 } 733 stillActiveSocket.close(); 734 theSocket.close(); 735 736 // now test case were we set it to true 737 anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 738 serverSocket = new ServerSocket(); 739 serverSocket.setReuseAddress(true); 740 serverSocket.bind(anyAddress); 741 theAddress = serverSocket.getLocalSocketAddress(); 742 743 // make a connection to the server, then close the server 744 theSocket = new Socket(); 745 theSocket.connect(theAddress); 746 stillActiveSocket = serverSocket.accept(); 747 serverSocket.close(); 748 749 // now try to rebind the server which should pass with 750 // setReuseAddress to true 751 try { 752 serverSocket = new ServerSocket(); 753 serverSocket.setReuseAddress(true); 754 serverSocket.bind(theAddress); 755 } catch (IOException ex) { 756 fail("Unexpected exception when setReuseAddress is true and we bind:" 757 + theAddress.toString() + ":" + ex.toString()); 758 } 759 stillActiveSocket.close(); 760 theSocket.close(); 761 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 762 763 // now test default case were we expect this to work regardless of 764 // the value set 765 anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 766 serverSocket = new ServerSocket(); 767 serverSocket.bind(anyAddress); 768 theAddress = serverSocket.getLocalSocketAddress(); 769 770 // make a connection to the server, then close the server 771 theSocket = new Socket(); 772 theSocket.connect(theAddress); 773 stillActiveSocket = serverSocket.accept(); 774 serverSocket.close(); 775 776 // now try to rebind the server which should pass 777 try { 778 serverSocket = new ServerSocket(); 779 serverSocket.bind(theAddress); 780 } catch (IOException ex) { 781 fail("Unexpected exception when setReuseAddress is the default case and we bind:" 782 + theAddress.toString() + ":" + ex.toString()); 783 } 784 stillActiveSocket.close(); 785 theSocket.close(); 786 787 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 788 } catch (Exception e) { 789 handleException(e, SO_REUSEADDR); 790 } 791 } 792 793 /** 794 * @tests java.net.ServerSocket#getReuseAddress() 795 */ 796 public void test_getReuseAddress() { 797 try { 798 ServerSocket theSocket = new ServerSocket(); 799 theSocket.setReuseAddress(true); 800 assertTrue("getReuseAddress false when it should be true", 801 theSocket.getReuseAddress()); 802 theSocket.setReuseAddress(false); 803 assertFalse("getReuseAddress true when it should be False", 804 theSocket.getReuseAddress()); 805 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 806 } catch (Exception e) { 807 handleException(e, SO_REUSEADDR); 808 } 809 } 810 811 /** 812 * @tests java.net.ServerSocket#setReceiveBufferSize(int) 813 */ 814 public void test_setReceiveBufferSizeI() { 815 try { 816 // now validate case where we try to set to 0 817 ServerSocket theSocket = new ServerSocket(); 818 try { 819 theSocket.setReceiveBufferSize(0); 820 fail("No exception when receive buffer size set to 0"); 821 } catch (IllegalArgumentException ex) { 822 } 823 theSocket.close(); 824 825 // now validate case where we try to set to a negative value 826 theSocket = new ServerSocket(); 827 try { 828 theSocket.setReceiveBufferSize(-1000); 829 fail("No exception when receive buffer size set to -1000"); 830 } catch (IllegalArgumentException ex) { 831 } 832 theSocket.close(); 833 834 // now just try to set a good value to make sure it is set and there 835 // are not exceptions 836 theSocket = new ServerSocket(); 837 theSocket.setReceiveBufferSize(1000); 838 theSocket.close(); 839 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 840 } catch (Exception e) { 841 handleException(e, SO_RCVBUF); 842 } 843 844 } 845 846 /* 847 * @tests java.net.ServerSocket#getReceiveBufferSize() 848 */ 849 public void test_getReceiveBufferSize() { 850 try { 851 ServerSocket theSocket = new ServerSocket(); 852 853 // since the value returned is not necessary what we set we are 854 // limited in what we can test 855 // just validate that it is not 0 or negative 856 assertFalse("get Buffer size returns 0:", 0 == theSocket 857 .getReceiveBufferSize()); 858 assertFalse("get Buffer size returns a negative value:", 859 0 > theSocket.getReceiveBufferSize()); 860 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 861 } catch (Exception e) { 862 handleException(e, SO_RCVBUF); 863 } 864 } 865 866 /** 867 * @tests java.net.ServerSocket#getChannel() 868 */ 869 public void test_getChannel() throws Exception { 870 assertNull(new ServerSocket().getChannel()); 871 } 872 873 /* 874 * @tests java.net.ServerSocket#setPerformancePreference() 875 */ 876 public void test_setPerformancePreference_Int_Int_Int() throws Exception { 877 ServerSocket theSocket = new ServerSocket(); 878 theSocket.setPerformancePreferences(1, 1, 1); 879 } 880 881 /** 882 * Sets up the fixture, for example, open a network connection. This method 883 * is called before a test is executed. 884 */ 885 protected void setUp() { 886 } 887 888 /** 889 * Tears down the fixture, for example, close a network connection. This 890 * method is called after a test is executed. 891 */ 892 protected void tearDown() { 893 894 try { 895 if (s != null) 896 s.close(); 897 if (sconn != null) 898 sconn.close(); 899 if (t != null) 900 t.interrupt(); 901 } catch (Exception e) { 902 } 903 } 904 905 /** 906 * Sets up the fixture, for example, open a network connection. This method 907 * is called before a test is executed. 908 */ 909 protected void startClient(int port) { 910 t = new Thread(new SSClient(port), "SSClient"); 911 t.start(); 912 try { 913 Thread.sleep(1000); 914 } catch (InterruptedException e) { 915 System.out.println("Exception during startClinet()" + e.toString()); 916 } 917 } 918 919 /** 920 * @tests java.net.ServerSocket#implAccept 921 */ 922 public void test_implAcceptLjava_net_Socket() throws Exception { 923 // regression test for Harmony-1235 924 try { 925 new MockServerSocket().mockImplAccept(new MockSocket( 926 new MockSocketImpl())); 927 } catch (SocketException e) { 928 // expected 929 } 930 } 931 932 /** 933 * Regression for HARMONY-3265 934 * @throws Exception 935 */ 936 public void test_ServerSocket_init() throws Exception { 937 String[] args = new String[]{"org.apache.harmony.luni.tests.java.net.TestServerSocketInit"}; 938 Support_Exec.execJava(args, null, true); 939 } 940 941 static class MockSocketImpl extends SocketImpl { 942 protected void create(boolean arg0) throws IOException { 943 // empty 944 } 945 946 protected void connect(String arg0, int arg1) throws IOException { 947 // empty 948 } 949 950 protected void connect(InetAddress arg0, int arg1) throws IOException { 951 // empty 952 } 953 954 protected void connect(SocketAddress arg0, int arg1) throws IOException { 955 // empty 956 } 957 958 protected void bind(InetAddress arg0, int arg1) throws IOException { 959 // empty 960 } 961 962 protected void listen(int arg0) throws IOException { 963 // empty 964 } 965 966 protected void accept(SocketImpl arg0) throws IOException { 967 // empty 968 } 969 970 protected InputStream getInputStream() throws IOException { 971 return null; 972 } 973 974 protected OutputStream getOutputStream() throws IOException { 975 return null; 976 } 977 978 protected int available() throws IOException { 979 return 0; 980 } 981 982 protected void close() throws IOException { 983 // empty 984 } 985 986 protected void sendUrgentData(int arg0) throws IOException { 987 // empty 988 } 989 990 public void setOption(int arg0, Object arg1) throws SocketException { 991 // empty 992 } 993 994 public Object getOption(int arg0) throws SocketException { 995 return null; 996 } 997 } 998 999 static class MockSocket extends Socket { 1000 public MockSocket(SocketImpl impl) throws SocketException { 1001 super(impl); 1002 } 1003 } 1004 1005 static class MockServerSocket extends ServerSocket { 1006 public MockServerSocket() throws Exception { 1007 super(); 1008 } 1009 1010 public void mockImplAccept(Socket s) throws Exception { 1011 super.implAccept(s); 1012 } 1013 } 1014} 1015