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