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