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 18 package org.apache.harmony.tests.java.net; 19 20 import tests.support.Support_Configuration; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.InterruptedIOException; 24 import java.io.OutputStream; 25 import java.net.BindException; 26 import java.net.ConnectException; 27 import java.net.InetAddress; 28 import java.net.InetSocketAddress; 29 import java.net.PlainServerSocketImpl; 30 import java.net.ServerSocket; 31 import java.net.Socket; 32 import java.net.SocketAddress; 33 import java.net.SocketException; 34 import java.net.SocketImpl; 35 import java.net.SocketImplFactory; 36 import java.net.UnknownHostException; 37 import java.util.Date; 38 import java.util.Locale; 39 import java.util.Properties; 40 41 public 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 try { 300 s.setSoTimeout(100); 301 assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout()); 302 } finally { 303 s.close(); 304 } 305 } 306 307 /** 308 * java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) 309 */ 310 public void test_setSocketFactoryLjava_net_SocketImplFactory() 311 throws IOException { 312 SocketImplFactory factory = new MockSocketImplFactory(); 313 // Should not throw SocketException when set DatagramSocketImplFactory 314 // for the first time. 315 ServerSocket.setSocketFactory(factory); 316 317 try { 318 ServerSocket.setSocketFactory(null); 319 fail("Should throw SocketException"); 320 } catch (SocketException e) { 321 // Expected 322 } 323 324 try { 325 ServerSocket.setSocketFactory(factory); 326 fail("Should throw SocketException"); 327 } catch (SocketException e) { 328 // Expected 329 } 330 } 331 332 private static class MockSocketImplFactory implements SocketImplFactory { 333 public SocketImpl createSocketImpl() { 334 return new PlainServerSocketImpl(); 335 } 336 } 337 338 /** 339 * java.net.ServerSocket#setSoTimeout(int) 340 */ 341 public void test_setSoTimeoutI() throws IOException { 342 // Timeout should trigger and throw InterruptedIOException 343 try { 344 s = new ServerSocket(0); 345 s.setSoTimeout(100); 346 s.accept(); 347 } catch (InterruptedIOException e) { 348 assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout()); 349 return; 350 } 351 352 // Timeout should not trigger in this case 353 s = new ServerSocket(0); 354 startClient(s.getLocalPort()); 355 s.setSoTimeout(10000); 356 sconn = s.accept(); 357 } 358 359 /** 360 * java.net.ServerSocket#toString() 361 */ 362 public void test_toString() throws Exception { 363 s = new ServerSocket(0); 364 try { 365 int portNumber = s.getLocalPort(); 366 // In IPv6, the all-zeros-address is written as "::" 367 assertEquals("ServerSocket[addr=::/::,port=0,localport=" 368 + portNumber + "]", s.toString()); 369 } finally { 370 s.close(); 371 } 372 } 373 374 /** 375 * java.net.ServerSocket#bind(java.net.SocketAddress) 376 */ 377 public void test_bindLjava_net_SocketAddress() throws IOException { 378 class mySocketAddress extends SocketAddress { 379 public mySocketAddress() { 380 } 381 } 382 // create servers socket, bind it and then validate basic state 383 ServerSocket theSocket = new ServerSocket(); 384 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 385 .getLocalHost(), 0); 386 theSocket.bind(theAddress); 387 int portNumber = theSocket.getLocalPort(); 388 assertTrue( 389 "Returned incorrect InetSocketAddress(2):" 390 + theSocket.getLocalSocketAddress().toString() 391 + "Expected: " 392 + (new InetSocketAddress(InetAddress.getLocalHost(), 393 portNumber)).toString(), theSocket 394 .getLocalSocketAddress().equals( 395 new InetSocketAddress(InetAddress 396 .getLocalHost(), portNumber))); 397 assertTrue("Server socket not bound when it should be:", theSocket 398 .isBound()); 399 400 // now make sure that it is actually bound and listening on the 401 // address we provided 402 Socket clientSocket = new Socket(); 403 InetSocketAddress clAddress = new InetSocketAddress(InetAddress 404 .getLocalHost(), portNumber); 405 clientSocket.connect(clAddress); 406 Socket servSock = theSocket.accept(); 407 408 assertEquals(clAddress, clientSocket.getRemoteSocketAddress()); 409 theSocket.close(); 410 servSock.close(); 411 clientSocket.close(); 412 413 // validate we can specify null for the address in the bind and all 414 // goes ok 415 theSocket = new ServerSocket(); 416 theSocket.bind(null); 417 theSocket.close(); 418 419 // Address that we have already bound to 420 theSocket = new ServerSocket(); 421 ServerSocket theSocket2 = new ServerSocket(); 422 try { 423 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 424 theSocket.bind(theAddress); 425 SocketAddress localAddress = theSocket.getLocalSocketAddress(); 426 theSocket2.bind(localAddress); 427 fail("No exception binding to address that is not available"); 428 } catch (IOException ex) { 429 } 430 theSocket.close(); 431 theSocket2.close(); 432 433 // validate we get io address when we try to bind to address we 434 // cannot bind to 435 theSocket = new ServerSocket(); 436 try { 437 theSocket.bind(new InetSocketAddress(InetAddress 438 .getByAddress(Support_Configuration.nonLocalAddressBytes), 439 0)); 440 fail("No exception was thrown when binding to bad address"); 441 } catch (IOException ex) { 442 } 443 theSocket.close(); 444 445 // now validate case where we pass in an unsupported subclass of 446 // SocketAddress 447 theSocket = new ServerSocket(); 448 try { 449 theSocket.bind(new mySocketAddress()); 450 fail("No exception when binding using unsupported SocketAddress subclass"); 451 } catch (IllegalArgumentException ex) { 452 } 453 theSocket.close(); 454 } 455 456 /** 457 * java.net.ServerSocket#bind(java.net.SocketAddress, int) 458 */ 459 public void test_bindLjava_net_SocketAddressI() throws IOException { 460 class mySocketAddress extends SocketAddress { 461 462 public mySocketAddress() { 463 } 464 } 465 466 // create servers socket, bind it and then validate basic state 467 ServerSocket theSocket = new ServerSocket(); 468 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 469 .getLocalHost(), 0); 470 theSocket.bind(theAddress, 5); 471 int portNumber = theSocket.getLocalPort(); 472 assertTrue( 473 "Returned incorrect InetSocketAddress(2):" 474 + theSocket.getLocalSocketAddress().toString() 475 + "Expected: " 476 + (new InetSocketAddress(InetAddress.getLocalHost(), 477 portNumber)).toString(), theSocket 478 .getLocalSocketAddress().equals( 479 new InetSocketAddress(InetAddress 480 .getLocalHost(), portNumber))); 481 assertTrue("Server socket not bound when it should be:", theSocket 482 .isBound()); 483 484 // now make sure that it is actually bound and listening on the 485 // address we provided 486 SocketAddress localAddress = theSocket.getLocalSocketAddress(); 487 Socket clientSocket = new Socket(); 488 clientSocket.connect(localAddress); 489 Socket servSock = theSocket.accept(); 490 491 assertTrue(clientSocket.getRemoteSocketAddress().equals(localAddress)); 492 theSocket.close(); 493 servSock.close(); 494 clientSocket.close(); 495 496 // validate we can specify null for the address in the bind and all 497 // goes ok 498 theSocket = new ServerSocket(); 499 theSocket.bind(null, 5); 500 theSocket.close(); 501 502 // Address that we have already bound to 503 theSocket = new ServerSocket(); 504 ServerSocket theSocket2 = new ServerSocket(); 505 try { 506 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 507 theSocket.bind(theAddress, 5); 508 SocketAddress inuseAddress = theSocket.getLocalSocketAddress(); 509 theSocket2.bind(inuseAddress, 5); 510 fail("No exception binding to address that is not available"); 511 } catch (IOException ex) { 512 // expected 513 } 514 theSocket.close(); 515 theSocket2.close(); 516 517 // validate we get ioException when we try to bind to address we 518 // cannot bind to 519 theSocket = new ServerSocket(); 520 try { 521 theSocket.bind(new InetSocketAddress(InetAddress 522 .getByAddress(Support_Configuration.nonLocalAddressBytes), 523 0), 5); 524 fail("No exception was thrown when binding to bad address"); 525 } catch (IOException ex) { 526 } 527 theSocket.close(); 528 529 // now validate case where we pass in an unsupported subclass of 530 // SocketAddress 531 theSocket = new ServerSocket(); 532 try { 533 theSocket.bind(new mySocketAddress(), 5); 534 fail("Binding using unsupported SocketAddress subclass should have thrown exception"); 535 } catch (IllegalArgumentException ex) { 536 } 537 theSocket.close(); 538 539 // now validate that backlog is respected. We have to do a test that 540 // checks if it is a least a certain number as some platforms make 541 // it higher than we request. Unfortunately non-server versions of 542 // windows artificially limit the backlog to 5 and 5 is the 543 // historical default so it is not a great test. 544 theSocket = new ServerSocket(); 545 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 546 theSocket.bind(theAddress, 4); 547 localAddress = theSocket.getLocalSocketAddress(); 548 Socket theSockets[] = new Socket[4]; 549 int i = 0; 550 try { 551 for (i = 0; i < 4; i++) { 552 theSockets[i] = new Socket(); 553 theSockets[i].connect(localAddress); 554 } 555 } catch (ConnectException ex) { 556 fail("Backlog does not seem to be respected in bind:" + i + ":" 557 + ex.toString()); 558 } 559 560 for (i = 0; i < 4; i++) { 561 theSockets[i].close(); 562 } 563 564 theSocket.close(); 565 servSock.close(); 566 } 567 568 /** 569 * java.net.ServerSocket#getLocalSocketAddress() 570 */ 571 public void test_getLocalSocketAddress() throws Exception { 572 // set up server connect and then validate that we get the right 573 // response for the local address 574 ServerSocket theSocket = new ServerSocket(0, 5, InetAddress 575 .getLocalHost()); 576 int portNumber = theSocket.getLocalPort(); 577 assertTrue("Returned incorrect InetSocketAddress(1):" 578 + theSocket.getLocalSocketAddress().toString() 579 + "Expected: " 580 + (new InetSocketAddress(InetAddress.getLocalHost(), 581 portNumber)).toString(), theSocket 582 .getLocalSocketAddress().equals( 583 new InetSocketAddress(InetAddress.getLocalHost(), 584 portNumber))); 585 theSocket.close(); 586 587 // now create a socket that is not bound and validate we get the 588 // right answer 589 theSocket = new ServerSocket(); 590 assertNull( 591 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 592 theSocket.getLocalSocketAddress()); 593 594 // now bind the socket and make sure we get the right answer 595 theSocket 596 .bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 597 int localPort = theSocket.getLocalPort(); 598 assertEquals("Returned incorrect InetSocketAddress(2):", theSocket 599 .getLocalSocketAddress(), new InetSocketAddress(InetAddress 600 .getLocalHost(), localPort)); 601 theSocket.close(); 602 } 603 604 /** 605 * java.net.ServerSocket#isBound() 606 */ 607 public void test_isBound() throws IOException { 608 InetAddress addr = InetAddress.getLocalHost(); 609 ServerSocket serverSocket = new ServerSocket(); 610 assertFalse("Socket indicated bound when it should be (1)", 611 serverSocket.isBound()); 612 613 // now bind and validate bound ok 614 serverSocket.bind(new InetSocketAddress(addr, 0)); 615 assertTrue("Socket indicated not bound when it should be (1)", 616 serverSocket.isBound()); 617 serverSocket.close(); 618 619 // now do with some of the other constructors 620 serverSocket = new ServerSocket(0); 621 assertTrue("Socket indicated not bound when it should be (2)", 622 serverSocket.isBound()); 623 serverSocket.close(); 624 625 serverSocket = new ServerSocket(0, 5, addr); 626 assertTrue("Socket indicated not bound when it should be (3)", 627 serverSocket.isBound()); 628 serverSocket.close(); 629 630 serverSocket = new ServerSocket(0, 5); 631 assertTrue("Socket indicated not bound when it should be (4)", 632 serverSocket.isBound()); 633 serverSocket.close(); 634 } 635 636 /** 637 * java.net.ServerSocket#isClosed() 638 */ 639 public void test_isClosed() throws IOException { 640 InetAddress addr = InetAddress.getLocalHost(); 641 ServerSocket serverSocket = new ServerSocket(0, 5, addr); 642 643 // validate isClosed returns expected values 644 assertFalse("Socket should indicate it is not closed(1):", serverSocket 645 .isClosed()); 646 serverSocket.close(); 647 assertTrue("Socket should indicate it is closed(1):", serverSocket 648 .isClosed()); 649 650 // now do with some of the other constructors 651 serverSocket = new ServerSocket(0); 652 assertFalse("Socket should indicate it is not closed(1):", serverSocket 653 .isClosed()); 654 serverSocket.close(); 655 assertTrue("Socket should indicate it is closed(1):", serverSocket 656 .isClosed()); 657 658 serverSocket = new ServerSocket(0, 5, addr); 659 assertFalse("Socket should indicate it is not closed(1):", serverSocket 660 .isClosed()); 661 serverSocket.close(); 662 assertTrue("Socket should indicate it is closed(1):", serverSocket 663 .isClosed()); 664 665 serverSocket = new ServerSocket(0, 5); 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 673 /* 674 * Regression HARMONY-6090 675 */ 676 public void test_defaultValueReuseAddress() throws Exception { 677 String platform = System.getProperty("os.name").toLowerCase(Locale.US); 678 if (!platform.startsWith("windows")) { 679 // on Unix 680 assertTrue(new ServerSocket().getReuseAddress()); 681 assertTrue(new ServerSocket(0).getReuseAddress()); 682 assertTrue(new ServerSocket(0, 50).getReuseAddress()); 683 assertTrue(new ServerSocket(0, 50, InetAddress.getLocalHost()).getReuseAddress()); 684 } else { 685 // on Windows 686 assertFalse(new ServerSocket().getReuseAddress()); 687 assertFalse(new ServerSocket(0).getReuseAddress()); 688 assertFalse(new ServerSocket(0, 50).getReuseAddress()); 689 assertFalse(new ServerSocket(0, 50, InetAddress.getLocalHost()).getReuseAddress()); 690 } 691 } 692 693 public void test_setReuseAddressZ() throws Exception { 694 // set up server and connect 695 InetSocketAddress anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 696 ServerSocket serverSocket = new ServerSocket(); 697 serverSocket.setReuseAddress(false); 698 serverSocket.bind(anyAddress); 699 SocketAddress theAddress = serverSocket.getLocalSocketAddress(); 700 701 // make a connection to the server, then close the server 702 Socket theSocket = new Socket(); 703 theSocket.connect(theAddress); 704 Socket stillActiveSocket = serverSocket.accept(); 705 serverSocket.close(); 706 707 // now try to rebind the server which should fail with 708 // setReuseAddress to false. On windows platforms the bind is 709 // allowed even then reUseAddress is false so our test uses 710 // the platform to determine what the expected result is. 711 String platform = System.getProperty("os.name"); 712 try { 713 serverSocket = new ServerSocket(); 714 serverSocket.setReuseAddress(false); 715 serverSocket.bind(theAddress); 716 fail("No exception when setReuseAddress is false and we bind:" + theAddress.toString()); 717 } catch (IOException expected) { 718 } 719 stillActiveSocket.close(); 720 theSocket.close(); 721 722 // now test case were we set it to true 723 anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 724 serverSocket = new ServerSocket(); 725 serverSocket.setReuseAddress(true); 726 serverSocket.bind(anyAddress); 727 theAddress = serverSocket.getLocalSocketAddress(); 728 729 // make a connection to the server, then close the server 730 theSocket = new Socket(); 731 theSocket.connect(theAddress); 732 stillActiveSocket = serverSocket.accept(); 733 serverSocket.close(); 734 735 // now try to rebind the server which should pass with 736 // setReuseAddress to true 737 try { 738 serverSocket = new ServerSocket(); 739 serverSocket.setReuseAddress(true); 740 serverSocket.bind(theAddress); 741 } catch (IOException ex) { 742 fail("Unexpected exception when setReuseAddress is true and we bind:" 743 + theAddress.toString() + ":" + ex.toString()); 744 } 745 stillActiveSocket.close(); 746 theSocket.close(); 747 748 // now test default case were we expect this to work regardless of 749 // the value set 750 anyAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 751 serverSocket = new ServerSocket(); 752 serverSocket.bind(anyAddress); 753 theAddress = serverSocket.getLocalSocketAddress(); 754 755 // make a connection to the server, then close the server 756 theSocket = new Socket(); 757 theSocket.connect(theAddress); 758 stillActiveSocket = serverSocket.accept(); 759 serverSocket.close(); 760 761 // now try to rebind the server which should pass 762 try { 763 serverSocket = new ServerSocket(); 764 serverSocket.bind(theAddress); 765 } catch (IOException ex) { 766 fail("Unexpected exception when setReuseAddress is the default case and we bind:" 767 + theAddress.toString() + ":" + ex.toString()); 768 } 769 stillActiveSocket.close(); 770 theSocket.close(); 771 } 772 773 public void test_getReuseAddress() throws Exception { 774 ServerSocket theSocket = new ServerSocket(); 775 theSocket.setReuseAddress(true); 776 assertTrue("getReuseAddress false when it should be true", theSocket.getReuseAddress()); 777 theSocket.setReuseAddress(false); 778 assertFalse("getReuseAddress true when it should be False", theSocket.getReuseAddress()); 779 } 780 781 public void test_setReceiveBufferSizeI() throws Exception { 782 // now validate case where we try to set to 0 783 ServerSocket theSocket = new ServerSocket(); 784 try { 785 theSocket.setReceiveBufferSize(0); 786 fail("No exception when receive buffer size set to 0"); 787 } catch (IllegalArgumentException ex) { 788 } 789 theSocket.close(); 790 791 // now validate case where we try to set to a negative value 792 theSocket = new ServerSocket(); 793 try { 794 theSocket.setReceiveBufferSize(-1000); 795 fail("No exception when receive buffer size set to -1000"); 796 } catch (IllegalArgumentException ex) { 797 } 798 theSocket.close(); 799 800 // now just try to set a good value to make sure it is set and there 801 // are not exceptions 802 theSocket = new ServerSocket(); 803 theSocket.setReceiveBufferSize(1000); 804 theSocket.close(); 805 } 806 807 public void test_getReceiveBufferSize() throws Exception { 808 ServerSocket theSocket = new ServerSocket(); 809 810 // since the value returned is not necessary what we set we are 811 // limited in what we can test 812 // just validate that it is not 0 or negative 813 assertFalse("get Buffer size returns 0:", 0 == theSocket.getReceiveBufferSize()); 814 assertFalse("get Buffer size returns a negative value:", 0 > theSocket.getReceiveBufferSize()); 815 } 816 817 public void test_getChannel() throws Exception { 818 assertNull(new ServerSocket().getChannel()); 819 } 820 821 public void test_setPerformancePreference_Int_Int_Int() throws Exception { 822 ServerSocket theSocket = new ServerSocket(); 823 theSocket.setPerformancePreferences(1, 1, 1); 824 } 825 826 /** 827 * Sets up the fixture, for example, open a network connection. This method 828 * is called before a test is executed. 829 */ 830 protected void setUp() { 831 } 832 833 /** 834 * Tears down the fixture, for example, close a network connection. This 835 * method is called after a test is executed. 836 */ 837 protected void tearDown() { 838 839 try { 840 if (s != null) 841 s.close(); 842 if (sconn != null) 843 sconn.close(); 844 if (t != null) 845 t.interrupt(); 846 } catch (Exception e) { 847 } 848 } 849 850 /** 851 * Sets up the fixture, for example, open a network connection. This method 852 * is called before a test is executed. 853 */ 854 protected void startClient(int port) { 855 t = new Thread(new SSClient(port), "SSClient"); 856 t.start(); 857 try { 858 Thread.sleep(1000); 859 } catch (InterruptedException e) { 860 System.out.println("Exception during startClinet()" + e.toString()); 861 } 862 } 863 864 /** 865 * java.net.ServerSocket#implAccept 866 */ 867 public void test_implAcceptLjava_net_Socket() throws Exception { 868 // regression test for Harmony-1235 869 try { 870 new MockServerSocket().mockImplAccept(new MockSocket( 871 new MockSocketImpl())); 872 } catch (SocketException e) { 873 // expected 874 } 875 } 876 877 static class MockSocketImpl extends SocketImpl { 878 protected void create(boolean arg0) throws IOException { 879 // empty 880 } 881 882 protected void connect(String arg0, int arg1) throws IOException { 883 // empty 884 } 885 886 protected void connect(InetAddress arg0, int arg1) throws IOException { 887 // empty 888 } 889 890 protected void connect(SocketAddress arg0, int arg1) throws IOException { 891 // empty 892 } 893 894 protected void bind(InetAddress arg0, int arg1) throws IOException { 895 // empty 896 } 897 898 protected void listen(int arg0) throws IOException { 899 // empty 900 } 901 902 protected void accept(SocketImpl arg0) throws IOException { 903 // empty 904 } 905 906 protected InputStream getInputStream() throws IOException { 907 return null; 908 } 909 910 protected OutputStream getOutputStream() throws IOException { 911 return null; 912 } 913 914 protected int available() throws IOException { 915 return 0; 916 } 917 918 protected void close() throws IOException { 919 // empty 920 } 921 922 protected void sendUrgentData(int arg0) throws IOException { 923 // empty 924 } 925 926 public void setOption(int arg0, Object arg1) throws SocketException { 927 // empty 928 } 929 930 public Object getOption(int arg0) throws SocketException { 931 return null; 932 } 933 } 934 935 static class MockSocket extends Socket { 936 public MockSocket(SocketImpl impl) throws SocketException { 937 super(impl); 938 } 939 } 940 941 static class MockServerSocket extends ServerSocket { 942 public MockServerSocket() throws Exception { 943 super(); 944 } 945 946 public void mockImplAccept(Socket s) throws Exception { 947 super.implAccept(s); 948 } 949 } 950 } 951