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 java.io.IOException; 21import java.io.InputStream; 22import java.io.OutputStream; 23import java.net.ConnectException; 24import java.net.Inet4Address; 25import java.net.Inet6Address; 26import java.net.InetAddress; 27import java.net.InetSocketAddress; 28import java.net.Proxy; 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.SocketTimeoutException; 36import java.net.UnknownHostException; 37import java.security.Permission; 38import java.util.Arrays; 39import java.util.Locale; 40 41import tests.support.Support_Configuration; 42 43public class SocketTest extends junit.framework.TestCase { 44 private class ClientThread implements Runnable { 45 46 public void run() { 47 try { 48 Socket socket = new Socket(); 49 InetSocketAddress addr = new InetSocketAddress(host, port); 50 socket.connect(addr); 51 52 socket.close(); 53 } catch (IOException e) { 54 throw new RuntimeException(e); 55 } 56 } 57 } 58 59 private class ServerThread implements Runnable { 60 private static final int FIRST_TIME = 1; 61 62 private static final int SECOND_TIME = 2; 63 64 private int backlog = 10; 65 66 public boolean ready = false; 67 68 private int serverSocketConstructor = 0; 69 70 public void run() { 71 try { 72 73 ServerSocket socket; 74 switch (serverSocketConstructor) { 75 case FIRST_TIME: 76 socket = new ServerSocket(port, backlog, 77 new InetSocketAddress(host, port).getAddress()); 78 port = socket.getLocalPort(); 79 break; 80 case SECOND_TIME: 81 socket = new ServerSocket(port, backlog); 82 host = socket.getInetAddress().getHostName(); 83 port = socket.getLocalPort(); 84 break; 85 default: 86 socket = new ServerSocket(); 87 break; 88 } 89 90 synchronized (this) { 91 ready = true; 92 this.notifyAll(); 93 } 94 95 socket.setSoTimeout(5000); 96 Socket client = socket.accept(); 97 client.close(); 98 socket.close(); 99 } catch (IOException e) { 100 e.printStackTrace(); 101 } catch (Throwable e) { 102 e.printStackTrace(); 103 } 104 } 105 106 public synchronized void waitCreated() throws Exception { 107 while (!ready) { 108 this.wait(); 109 } 110 } 111 } 112 113 boolean interrupted; 114 115 String host = "localhost"; 116 int port; 117 118 Thread t; 119 120 private void connectTestImpl(int ssConsType) throws Exception { 121 ServerThread server = new ServerThread(); 122 server.serverSocketConstructor = ssConsType; 123 Thread serverThread = new Thread(server); 124 serverThread.start(); 125 server.waitCreated(); 126 127 ClientThread client = new ClientThread(); 128 Thread clientThread = new Thread(client); 129 clientThread.start(); 130 try { 131 serverThread.join(); 132 clientThread.join(); 133 } catch (InterruptedException e) { 134 e.printStackTrace(); 135 } 136 } 137 138 protected void tearDown() { 139 try { 140 if (t != null) { 141 t.interrupt(); 142 } 143 } catch (Exception e) { 144 } 145 this.t = null; 146 this.interrupted = false; 147 } 148 149 /** 150 * java.net.Socket#bind(java.net.SocketAddress) 151 */ 152 public void test_bindLjava_net_SocketAddress() throws IOException { 153 154 @SuppressWarnings("serial") 155 class UnsupportedSocketAddress extends SocketAddress { 156 public UnsupportedSocketAddress() { 157 } 158 } 159 160 // Address we cannot bind to 161 Socket theSocket = new Socket(); 162 InetSocketAddress bogusAddress = new InetSocketAddress(InetAddress 163 .getByAddress(Support_Configuration.nonLocalAddressBytes), 42); 164 try { 165 theSocket.bind(bogusAddress); 166 fail("No exception when binding to bad address"); 167 } catch (IOException ex) { 168 // Expected 169 } 170 theSocket.close(); 171 172 // Now create a socket that is not bound and then bind it 173 theSocket = new Socket(); 174 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 175 int portNumber = theSocket.getLocalPort(); 176 177 // Validate that the localSocketAddress reflects the address we 178 // bound to 179 assertEquals("Local address not correct after bind", 180 new InetSocketAddress(InetAddress.getLocalHost(), portNumber), 181 theSocket.getLocalSocketAddress()); 182 183 // Make sure we can now connect and that connections appear to come 184 // from the address we bound to. 185 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 186 .getLocalHost(), 0); 187 ServerSocket server = new ServerSocket(); 188 server.bind(theAddress); 189 int sport = server.getLocalPort(); 190 InetSocketAddress boundAddress = new InetSocketAddress(InetAddress 191 .getLocalHost(), sport); 192 193 theSocket.connect(boundAddress); 194 Socket worker = server.accept(); 195 assertEquals( 196 "Returned Remote address from server connected to does not match expected local address", 197 new InetSocketAddress(InetAddress.getLocalHost(), portNumber), 198 worker.getRemoteSocketAddress()); 199 theSocket.close(); 200 worker.close(); 201 server.close(); 202 203 // Validate if we pass in null that it picks an address for us and 204 // all is ok 205 theSocket = new Socket(); 206 theSocket.bind(null); 207 assertNotNull("Bind with null did not work", theSocket 208 .getLocalSocketAddress()); 209 theSocket.close(); 210 211 // now check the error conditions 212 213 // Address that we have already bound to 214 theSocket = new Socket(); 215 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 216 theSocket.bind(theAddress); 217 218 Socket theSocket2 = new Socket(); 219 try { 220 theSocket2.bind(theSocket.getLocalSocketAddress()); 221 fail("No exception binding to address that is not available"); 222 } catch (IOException ex) { 223 // Expected 224 } 225 theSocket.close(); 226 theSocket2.close(); 227 228 // Unsupported SocketAddress subclass 229 theSocket = new Socket(); 230 try { 231 theSocket.bind(new UnsupportedSocketAddress()); 232 fail("No exception when binding using unsupported SocketAddress subclass"); 233 } catch (IllegalArgumentException ex) { 234 // Expected 235 } 236 theSocket.close(); 237 } 238 239 /** 240 * java.net.Socket#bind(java.net.SocketAddress) 241 */ 242 public void test_bindLjava_net_SocketAddress_Proxy() throws IOException { 243 // The Proxy will not impact on the bind operation. It can be assigned 244 // with any address. 245 Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress( 246 "127.0.0.1", 0)); 247 Socket socket = new Socket(proxy); 248 249 InetAddress address = InetAddress.getByName("localhost"); 250 socket.bind(new InetSocketAddress(address, 0)); 251 252 assertEquals(address, socket.getLocalAddress()); 253 assertTrue(0 != socket.getLocalPort()); 254 255 socket.close(); 256 } 257 258 public void test_close() throws IOException { 259 ServerSocket server = new ServerSocket(0); 260 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 261 262 client.setSoLinger(false, 100); 263 264 client.close(); 265 try { 266 client.getOutputStream(); 267 fail("Failed to close socket"); 268 } catch (IOException expected) { 269 } 270 271 server.close(); 272 } 273 274 public void test_connect_unknownhost() throws Exception { 275 Socket socket = new Socket(); 276 try { 277 socket.connect(new InetSocketAddress("1.2.3.4hello", 12345)); 278 fail(); 279 } catch (UnknownHostException expected) { 280 } 281 } 282 283 public void test_connect_unresolved() throws IOException { 284 Socket socket = new Socket(); 285 InetSocketAddress unresolved = InetSocketAddress.createUnresolved("www.apache.org", 80); 286 try { 287 socket.connect(unresolved); 288 fail(); 289 } catch (UnknownHostException expected) { 290 } 291 try { 292 socket.connect(unresolved, 123); 293 fail(); 294 } catch (UnknownHostException expected) { 295 } 296 } 297 298 public void test_connectLjava_net_SocketAddress() throws Exception { 299 300 @SuppressWarnings("serial") 301 class UnsupportedSocketAddress extends SocketAddress { 302 public UnsupportedSocketAddress() { 303 } 304 } 305 306 Socket theSocket = new Socket(); 307 try { 308 theSocket.connect(null); 309 fail("No exception for null arg"); 310 } catch (IllegalArgumentException e) { 311 // Expected 312 } 313 314 try { 315 theSocket.connect(new UnsupportedSocketAddress()); 316 fail("No exception for invalid socket address"); 317 } catch (IllegalArgumentException e) { 318 // Expected 319 } 320 321 try { 322 theSocket.connect(new InetSocketAddress(InetAddress 323 .getByAddress(new byte[] { 0, 0, 0, 0 }), 42)); 324 fail("No exception with non-connectable address"); 325 } catch (ConnectException e) { 326 // Expected 327 } 328 329 // now validate that we get a connect exception if we try to connect to 330 // an address on which nobody is listening 331 theSocket = new Socket(); 332 try { 333 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 334 0)); 335 fail("No exception when connecting to address nobody listening on"); 336 } catch (ConnectException e) { 337 // Expected 338 } 339 340 // Now validate that we can actually connect when somebody is listening 341 ServerSocket server = new ServerSocket(0); 342 InetSocketAddress boundAddress = new InetSocketAddress(InetAddress 343 .getLocalHost(), server.getLocalPort()); 344 Socket client = new Socket(); 345 client.connect(boundAddress); 346 347 // validate that when a socket is connected that it answers 348 // correctly to related queries 349 assertTrue("Wrong connected status", client.isConnected()); 350 assertFalse("Wrong closed status", client.isClosed()); 351 assertTrue("Wrong bound status", client.isBound()); 352 assertFalse("Wrong input shutdown status", client.isInputShutdown()); 353 assertFalse("Wrong output shutdown status", client.isOutputShutdown()); 354 assertTrue("Local port was 0", client.getLocalPort() != 0); 355 356 client.close(); 357 server.close(); 358 359 // Now validate that we get the right exception if we connect when we 360 // are already connected 361 server = new ServerSocket(0); 362 boundAddress = new InetSocketAddress(InetAddress.getLocalHost(), server 363 .getLocalPort()); 364 client = new Socket(); 365 client.connect(boundAddress); 366 367 try { 368 client.connect(boundAddress); 369 fail("No exception when we try to connect on a connected socket: "); 370 } catch (SocketException e) { 371 // Expected 372 } 373 client.close(); 374 server.close(); 375 } 376 377 /** 378 * Regression for Harmony-2503 379 */ 380 public void test_connectLjava_net_SocketAddress_AnyAddress() 381 throws Exception { 382 connectTestImpl(ServerThread.FIRST_TIME); 383 connectTestImpl(ServerThread.SECOND_TIME); 384 } 385 386 /** 387 * java.net.Socket#connect(java.net.SocketAddress, int) 388 */ 389 public void test_connectLjava_net_SocketAddressI() throws Exception { 390 391 @SuppressWarnings("serial") 392 class UnsupportedSocketAddress extends SocketAddress { 393 public UnsupportedSocketAddress() { 394 } 395 } 396 397 // Start by validating the error checks 398 Socket theSocket = new Socket(); 399 try { 400 theSocket.connect(new InetSocketAddress(0), -100); 401 fail("No exception for negative timeout"); 402 } catch (IllegalArgumentException e) { 403 // Expected 404 } 405 406 try { 407 theSocket.connect(null, 0); 408 fail("No exception for null address"); 409 } catch (IllegalArgumentException e) { 410 // Expected 411 } 412 413 try { 414 theSocket.connect(new UnsupportedSocketAddress(), 1000); 415 fail("No exception for invalid socket address type"); 416 } catch (IllegalArgumentException e) { 417 // Expected 418 } 419 420 SocketAddress nonConnectableAddress = new InetSocketAddress(InetAddress 421 .getByAddress(new byte[] { 0, 0, 0, 0 }), 0); 422 try { 423 theSocket.connect(nonConnectableAddress, 1000); 424 fail("No exception when non Connectable Address passed in: "); 425 } catch (SocketException e) { 426 // Expected 427 } 428 429 // Now validate that we get a connect exception if we try to connect to 430 // an address on which nobody is listening 431 theSocket = new Socket(); 432 try { 433 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 434 0), 0); 435 fail("No exception when connecting to address nobody listening on"); 436 } catch (ConnectException e) { 437 // Expected 438 } 439 theSocket.close(); 440 441 // Now validate that we can actually connect when somebody is listening 442 ServerSocket server = new ServerSocket(0); 443 InetSocketAddress boundAddress = new InetSocketAddress(InetAddress 444 .getLocalHost(), server.getLocalPort()); 445 Socket client = new Socket(); 446 client.connect(boundAddress, 0); 447 448 // Validate that when a socket is connected that it answers 449 // correctly to related queries 450 assertTrue("Wrong connected status", client.isConnected()); 451 assertFalse("Wrong closed status", client.isClosed()); 452 assertTrue("Wrong bound status", client.isBound()); 453 assertFalse("Wrong input shutdown status", client.isInputShutdown()); 454 assertFalse("Wrong output shutdown status", client.isOutputShutdown()); 455 assertTrue("Local port was 0", client.getLocalPort() != 0); 456 457 client.close(); 458 server.close(); 459 460 // Now validate that we get a connect exception if we try to connect to 461 // an address on which nobody is listening 462 theSocket = new Socket(); 463 SocketAddress nonListeningAddress = new InetSocketAddress(InetAddress 464 .getLocalHost(), 42); 465 try { 466 theSocket.connect(nonListeningAddress, 1000); 467 fail("No exception when connecting to address nobody listening on"); 468 } catch (ConnectException e) { 469 // Expected 470 } catch (SocketTimeoutException e) { 471 // The other possibility is that the system timed us out. 472 } 473 theSocket.close(); 474 475 // Now validate that we get the right exception if we connect when we 476 // are already connected 477 server = new ServerSocket(0); 478 boundAddress = new InetSocketAddress(InetAddress.getLocalHost(), server 479 .getLocalPort()); 480 client = new Socket(); 481 client.connect(boundAddress, 10000); 482 483 try { 484 client.connect(boundAddress, 10000); 485 fail("No exception when we try to connect on a connected socket: "); 486 } catch (SocketException e) { 487 // Expected 488 } 489 client.close(); 490 server.close(); 491 } 492 493 /** 494 * java.net.Socket#Socket() 495 */ 496 public void test_Constructor() { 497 // create the socket and then validate some basic state 498 Socket s = new Socket(); 499 assertFalse("new socket should not be connected", s.isConnected()); 500 assertFalse("new socket should not be bound", s.isBound()); 501 assertFalse("new socket should not be closed", s.isClosed()); 502 assertFalse("new socket should not be in InputShutdown", s 503 .isInputShutdown()); 504 assertFalse("new socket should not be in OutputShutdown", s 505 .isOutputShutdown()); 506 } 507 508 /** 509 * java.net.Socket#Socket(java.lang.String, int) 510 */ 511 public void test_ConstructorLjava_lang_StringI() throws IOException { 512 ServerSocket server = new ServerSocket(0); 513 Socket client = new Socket(InetAddress.getLocalHost(), server 514 .getLocalPort()); 515 516 assertEquals("Failed to create socket", server.getLocalPort(), client 517 .getPort()); 518 519 // Regression for HARMONY-946 520 ServerSocket ss = new ServerSocket(0); 521 Socket s = new Socket("0.0.0.0", ss.getLocalPort()); 522 ss.close(); 523 s.close(); 524 } 525 526 /** 527 * java.net.Socket#Socket(java.lang.String, int, 528 *java.net.InetAddress, int) 529 */ 530 public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI() 531 throws IOException { 532 533 ServerSocket server = new ServerSocket(0); 534 int serverPort = server.getLocalPort(); 535 Socket client = new Socket(InetAddress.getLocalHost().getHostName(), 536 serverPort, InetAddress.getLocalHost(), 0); 537 assertTrue("Failed to create socket", client.getPort() == serverPort); 538 client.close(); 539 540 Socket theSocket; 541 try { 542 theSocket = new Socket("127.0.0.1", serverPort, InetAddress 543 .getLocalHost(), 0); 544 } catch (IOException e) { 545 // check here if InetAddress.getLocalHost() is returning the 546 // loopback address, if so that is likely the cause of the failure 547 assertFalse( 548 "Misconfiguration - local host is the loopback address", 549 InetAddress.getLocalHost().isLoopbackAddress()); 550 throw e; 551 } 552 553 assertTrue(theSocket.isConnected()); 554 555 try { 556 new Socket("127.0.0.1", serverPort, theSocket.getLocalAddress(), 557 theSocket.getLocalPort()); 558 fail("Was able to create two sockets on same port"); 559 } catch (IOException e) { 560 // Expected 561 } 562 563 theSocket.close(); 564 server.close(); 565 } 566 567 @SuppressWarnings("deprecation") 568 public void test_ConstructorLjava_lang_StringIZ() throws IOException { 569 ServerSocket server = new ServerSocket(0); 570 int serverPort = server.getLocalPort(); 571 Socket client = new Socket(InetAddress.getLocalHost().getHostAddress(), 572 serverPort, true); 573 574 assertEquals("Failed to create socket", serverPort, client.getPort()); 575 client.close(); 576 577 client = new Socket(InetAddress.getLocalHost().getHostName(), 578 serverPort, false); 579 client.close(); 580 server.close(); 581 } 582 583 /** 584 * java.net.Socket#Socket(java.net.InetAddress, int) 585 */ 586 public void test_ConstructorLjava_net_InetAddressI() throws IOException { 587 ServerSocket server = new ServerSocket(0); 588 Socket client = new Socket(InetAddress.getLocalHost(), server 589 .getLocalPort()); 590 591 assertEquals("Failed to create socket", server.getLocalPort(), client 592 .getPort()); 593 594 client.close(); 595 server.close(); 596 } 597 598 /** 599 * java.net.Socket#Socket(java.net.InetAddress, int, 600 *java.net.InetAddress, int) 601 */ 602 public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI() 603 throws IOException { 604 ServerSocket server = new ServerSocket(0); 605 Socket client = new Socket(InetAddress.getLocalHost(), server 606 .getLocalPort(), InetAddress.getLocalHost(), 0); 607 assertNotSame("Failed to create socket", 0, client.getLocalPort()); 608 } 609 610 /** 611 * java.net.Socket#Socket(java.net.InetAddress, int, boolean) 612 */ 613 @SuppressWarnings("deprecation") 614 public void test_ConstructorLjava_net_InetAddressIZ() throws IOException { 615 ServerSocket server = new ServerSocket(0); 616 int serverPort = server.getLocalPort(); 617 618 Socket client = new Socket(InetAddress.getLocalHost(), serverPort, true); 619 assertEquals("Failed to create socket", serverPort, client.getPort()); 620 621 client = new Socket(InetAddress.getLocalHost(), serverPort, false); 622 client.close(); 623 } 624 625 /** 626 * java.net.Socket#Socket(Proxy) 627 */ 628 public void test_ConstructorLjava_net_Proxy_Exception() { 629 630 SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1", 80); 631 632 Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1); 633 // IllegalArgumentException test 634 try { 635 new Socket(proxy1); 636 fail("should throw IllegalArgumentException"); 637 } catch (IllegalArgumentException e) { 638 // expected 639 } 640 641 Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1); 642 // should not throw any exception 643 new Socket(proxy2); 644 new Socket(Proxy.NO_PROXY); 645 } 646 647 /** 648 * java.net.Socket#getChannel() 649 */ 650 public void test_getChannel() { 651 assertNull(new Socket().getChannel()); 652 } 653 654 /** 655 * java.net.Socket#getInetAddress() 656 */ 657 public void test_getInetAddress() throws IOException { 658 ServerSocket server = new ServerSocket(0); 659 Socket client = new Socket(InetAddress.getLocalHost(), server 660 .getLocalPort()); 661 662 assertTrue("Returned incorrect InetAdrees", client.getInetAddress() 663 .equals(InetAddress.getLocalHost())); 664 665 client.close(); 666 server.close(); 667 } 668 669 /** 670 * java.net.Socket#getInputStream() 671 */ 672 public void test_getInputStream() throws IOException { 673 // Simple fetch test 674 ServerSocket server = new ServerSocket(0); 675 Socket client = new Socket(InetAddress.getLocalHost(), server 676 .getLocalPort()); 677 InputStream is = client.getInputStream(); 678 assertNotNull("Failed to get stream", is); 679 is.close(); 680 client.close(); 681 server.close(); 682 } 683 684 private boolean isUnix() { 685 String osName = System.getProperty("os.name"); 686 687 // only comparing ASCII, so assume english locale 688 osName = (osName == null ? null : osName.toLowerCase(Locale.ENGLISH)); 689 690 if (osName != null && osName.startsWith("windows")) { //$NON-NLS-1$ 691 return false; 692 } 693 return true; 694 } 695 696 public void test_getKeepAlive() throws Exception { 697 ServerSocket server = new ServerSocket(0); 698 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort(), null, 0); 699 700 client.setKeepAlive(true); 701 assertTrue("getKeepAlive false when it should be true", client.getKeepAlive()); 702 703 client.setKeepAlive(false); 704 assertFalse("getKeepAlive true when it should be False", client.getKeepAlive()); 705 } 706 707 public void test_getLocalAddress() throws IOException { 708 ServerSocket server = new ServerSocket(0); 709 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 710 711 assertTrue("Returned incorrect InetAddress", client.getLocalAddress() 712 .equals(InetAddress.getLocalHost())); 713 714 client = new Socket(); 715 client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 716 assertTrue(client.getLocalAddress().isAnyLocalAddress()); 717 718 client.close(); 719 server.close(); 720 } 721 722 /** 723 * java.net.Socket#getLocalPort() 724 */ 725 public void test_getLocalPort() throws IOException { 726 ServerSocket server = new ServerSocket(0); 727 Socket client = new Socket(InetAddress.getLocalHost(), server 728 .getLocalPort()); 729 730 assertNotSame("Returned incorrect port", 0, client.getLocalPort()); 731 732 client.close(); 733 server.close(); 734 } 735 736 public void test_getLocalSocketAddress() throws IOException { 737 // set up server connect and then validate that we get the right 738 // response for the local address 739 ServerSocket server = new ServerSocket(0); 740 Socket client = new Socket(InetAddress.getLocalHost(), server 741 .getLocalPort()); 742 int clientPort = client.getLocalPort(); 743 744 assertEquals("Returned incorrect InetSocketAddress(1):", 745 new InetSocketAddress(InetAddress.getLocalHost(), clientPort), 746 client.getLocalSocketAddress()); 747 client.close(); 748 server.close(); 749 750 // now create a socket that is not bound and validate we get the 751 // right answer 752 client = new Socket(); 753 assertNull( 754 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 755 client.getLocalSocketAddress()); 756 757 // now bind the socket and make sure we get the right answer 758 client.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 759 clientPort = client.getLocalPort(); 760 assertEquals("Returned incorrect InetSocketAddress(2):", 761 new InetSocketAddress(InetAddress.getLocalHost(), clientPort), 762 client.getLocalSocketAddress()); 763 client.close(); 764 765 // now validate the behaviour when the any address is returned 766 client = new Socket(); 767 client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 768 assertTrue(((InetSocketAddress) client.getLocalSocketAddress()).getAddress().isAnyLocalAddress()); 769 client.close(); 770 771 // now validate the same for getLocalAddress 772 client = new Socket(); 773 client.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 774 assertTrue(client.getLocalAddress().isAnyLocalAddress()); 775 client.close(); 776 } 777 778 public void test_getOOBInline() throws Exception { 779 Socket theSocket = new Socket(); 780 781 theSocket.setOOBInline(true); 782 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 783 784 theSocket.setOOBInline(false); 785 assertFalse("expected OOBIline to be false", theSocket.getOOBInline()); 786 787 theSocket.setOOBInline(false); 788 assertFalse("expected OOBIline to be false", theSocket.getOOBInline()); 789 } 790 791 /** 792 * java.net.Socket#getOutputStream() 793 */ 794 @SuppressWarnings("deprecation") 795 public void test_getOutputStream() throws IOException { 796 // Simple fetch test 797 ServerSocket server = new ServerSocket(0); 798 Socket client = new Socket(InetAddress.getLocalHost(), server 799 .getLocalPort()); 800 OutputStream os = client.getOutputStream(); 801 assertNotNull("Failed to get stream", os); 802 os.close(); 803 client.close(); 804 server.close(); 805 806 // Simple read/write test over the IO streams 807 final ServerSocket sinkServer = new ServerSocket(0); 808 Runnable runnable = new Runnable() { 809 public void run() { 810 try { 811 Socket worker = sinkServer.accept(); 812 sinkServer.close(); 813 InputStream in = worker.getInputStream(); 814 in.read(); 815 in.close(); 816 worker.close(); 817 } catch (IOException e) { 818 fail(); 819 } 820 } 821 }; 822 Thread thread = new Thread(runnable, "Socket.getOutputStream"); 823 thread.start(); 824 825 Socket pingClient = new Socket(InetAddress.getLocalHost(), sinkServer 826 .getLocalPort()); 827 828 // Busy wait until the client is connected. 829 int c = 0; 830 while (!pingClient.isConnected()) { 831 try { 832 Thread.sleep(200); 833 } catch (InterruptedException e) { 834 } 835 if (++c > 4) { 836 fail("thread is not alive"); 837 } 838 } 839 840 // Write some data to the server 841 OutputStream out = pingClient.getOutputStream(); 842 out.write(new byte[256]); 843 844 // Wait for the server to finish 845 Thread.yield(); 846 c = 0; 847 while (thread.isAlive()) { 848 try { 849 Thread.sleep(200); 850 } catch (InterruptedException e) { 851 } 852 if (++c > 4) { 853 fail("read call did not exit"); 854 } 855 } 856 857 // Subsequent writes should throw an exception 858 try { 859 // The output buffer may remain valid until the close completes 860 for (int i = 0; i < 400; i++) { 861 out.write(new byte[256]); 862 } 863 fail("write to closed socket did not cause exception"); 864 } catch (IOException e) { 865 // Expected 866 } 867 868 out.close(); 869 pingClient.close(); 870 sinkServer.close(); 871 872 // Regression test for HARMONY-873 873 ServerSocket ss2 = new ServerSocket(0); 874 Socket s = new Socket("127.0.0.1", ss2.getLocalPort()); 875 ss2.accept(); 876 s.shutdownOutput(); 877 try { 878 s.getOutputStream(); 879 fail("should throw SocketException"); 880 } catch (SocketException e) { 881 // expected 882 } 883 } 884 885 public void test_getPort() throws IOException { 886 ServerSocket server = new ServerSocket(0); 887 int serverPort = server.getLocalPort(); 888 Socket client = new Socket(InetAddress.getLocalHost(), serverPort); 889 890 assertEquals("Returned incorrect port", serverPort, client.getPort()); 891 892 client.close(); 893 server.close(); 894 } 895 896 public void test_getReceiveBufferSize() throws Exception { 897 ServerSocket server = new ServerSocket(0); 898 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 899 client.setReceiveBufferSize(130); 900 901 assertTrue("Incorrect buffer size", client.getReceiveBufferSize() >= 130); 902 903 client.close(); 904 server.close(); 905 } 906 907 /** 908 * java.net.Socket#getRemoteSocketAddress() 909 */ 910 public void test_getRemoteSocketAddress() throws IOException { 911 // set up server connect and then validate that we get the right 912 // response for the remote address 913 ServerSocket server = new ServerSocket(0); 914 int serverPort = server.getLocalPort(); 915 Socket client = new Socket(InetAddress.getLocalHost(), serverPort); 916 917 assertEquals("Returned incorrect InetSocketAddress(1):", 918 new InetSocketAddress(InetAddress.getLocalHost(), serverPort), 919 client.getRemoteSocketAddress()); 920 client.close(); 921 922 // now create one that is not connected and validate that we get the 923 // right answer 924 Socket theSocket = new Socket(); 925 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 926 assertNull("Returned incorrect InetSocketAddress -unconnected socket:", 927 theSocket.getRemoteSocketAddress()); 928 929 // now connect and validate we get the right answer 930 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 931 serverPort)); 932 assertEquals("Returned incorrect InetSocketAddress(2):", 933 new InetSocketAddress(InetAddress.getLocalHost(), serverPort), 934 theSocket.getRemoteSocketAddress()); 935 theSocket.close(); 936 937 server.close(); 938 } 939 940 public void test_getReuseAddress() throws Exception { 941 Socket theSocket = new Socket(); 942 theSocket.setReuseAddress(true); 943 assertTrue("getReuseAddress false when it should be true", theSocket.getReuseAddress()); 944 theSocket.setReuseAddress(false); 945 assertFalse("getReuseAddress true when it should be False", theSocket.getReuseAddress()); 946 } 947 948 public void test_getSendBufferSize() throws Exception { 949 ServerSocket server = new ServerSocket(0); 950 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 951 client.setSendBufferSize(134); 952 assertTrue("Incorrect buffer size", client.getSendBufferSize() >= 134); 953 client.close(); 954 server.close(); 955 } 956 957 public void test_getSoLinger() throws Exception { 958 ServerSocket server = new ServerSocket(0); 959 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 960 client.setSoLinger(true, 200); 961 assertEquals("Returned incorrect linger", 200, client.getSoLinger()); 962 client.setSoLinger(false, 0); 963 client.close(); 964 server.close(); 965 } 966 967 public void test_getSoTimeout() throws Exception { 968 ServerSocket server = new ServerSocket(0); 969 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 970 client.setSoTimeout(100); 971 assertEquals("Returned incorrect sotimeout", 100, client.getSoTimeout()); 972 client.close(); 973 server.close(); 974 } 975 976 public void test_getTcpNoDelay() throws Exception { 977 ServerSocket server = new ServerSocket(0); 978 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 979 980 boolean bool = !client.getTcpNoDelay(); 981 client.setTcpNoDelay(bool); 982 assertTrue("Failed to get no delay setting: " + client.getTcpNoDelay(), client.getTcpNoDelay() == bool); 983 984 client.close(); 985 server.close(); 986 } 987 988 public void test_getTrafficClass() throws Exception { 989 /* 990 * We cannot actually check that the values are set as if a platform 991 * does not support the option then it may come back unset even 992 * though we set it so just get the value to make sure we can get it 993 */ 994 int trafficClass = new Socket().getTrafficClass(); 995 assertTrue(0 <= trafficClass); 996 assertTrue(trafficClass <= 255); 997 } 998 999 /** 1000 * java.net.Socket#isBound() 1001 */ 1002 public void test_isBound() throws IOException { 1003 ServerSocket server = new ServerSocket(0); 1004 Socket client = new Socket(InetAddress.getLocalHost(), server 1005 .getLocalPort()); 1006 Socket worker = server.accept(); 1007 1008 assertTrue("Socket indicated not bound when it should be (1)", client 1009 .isBound()); 1010 worker.close(); 1011 client.close(); 1012 server.close(); 1013 1014 client = new Socket(); 1015 assertFalse("Socket indicated bound when it was not (2)", client 1016 .isBound()); 1017 1018 server = new ServerSocket(); 1019 server.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 1020 InetSocketAddress boundAddress = new InetSocketAddress(server 1021 .getInetAddress(), server.getLocalPort()); 1022 client.connect(boundAddress); 1023 worker = server.accept(); 1024 assertTrue("Socket indicated not bound when it should be (2)", client 1025 .isBound()); 1026 worker.close(); 1027 client.close(); 1028 server.close(); 1029 1030 // now test when we bind explicitly 1031 InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress 1032 .getLocalHost(), 0); 1033 client = new Socket(); 1034 assertFalse("Socket indicated bound when it was not (3)", client 1035 .isBound()); 1036 client.bind(theLocalAddress); 1037 assertTrue("Socket indicated not bound when it should be (3a)", client 1038 .isBound()); 1039 client.close(); 1040 assertTrue("Socket indicated not bound when it should be (3b)", client 1041 .isBound()); 1042 } 1043 1044 /** 1045 * java.net.Socket#isClosed() 1046 */ 1047 public void test_isClosed() throws IOException { 1048 ServerSocket server = new ServerSocket(0); 1049 Socket client = new Socket(InetAddress.getLocalHost(), server 1050 .getLocalPort()); 1051 Socket worker = server.accept(); 1052 1053 // validate isClosed returns expected values 1054 assertFalse("Socket should indicate it is not closed(1):", client 1055 .isClosed()); 1056 client.close(); 1057 assertTrue("Socket should indicate it is closed(1):", client.isClosed()); 1058 1059 // validate that isClosed works ok for sockets returned from 1060 // ServerSocket.accept() 1061 assertFalse("Accepted Socket should indicate it is not closed:", worker 1062 .isClosed()); 1063 worker.close(); 1064 assertTrue("Accepted Socket should indicate it is closed:", worker 1065 .isClosed()); 1066 1067 // and finally for the server socket 1068 assertFalse("Server Socket should indicate it is not closed:", server 1069 .isClosed()); 1070 server.close(); 1071 assertTrue("Server Socket should indicate it is closed:", server 1072 .isClosed()); 1073 } 1074 1075 /** 1076 * java.net.Socket#isConnected() 1077 */ 1078 public void test_isConnected() throws IOException { 1079 ServerSocket server = new ServerSocket(0); 1080 Socket client = new Socket(InetAddress.getLocalHost(), server 1081 .getLocalPort()); 1082 Socket worker = server.accept(); 1083 1084 assertTrue("Socket indicated not connected when it should be", client 1085 .isConnected()); 1086 client.close(); 1087 worker.close(); 1088 server.close(); 1089 1090 // now do it with the new constructors and revalidate 1091 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1092 .getLocalHost(), 0); 1093 client = new Socket(); 1094 assertFalse("Socket indicated connected when it was not", client 1095 .isConnected()); 1096 1097 server = new ServerSocket(); 1098 server.bind(theAddress); 1099 InetSocketAddress boundAddress = new InetSocketAddress(server 1100 .getInetAddress(), server.getLocalPort()); 1101 client.connect(boundAddress); 1102 worker = server.accept(); 1103 assertTrue("Socket indicated not connected when it should be", client 1104 .isConnected()); 1105 client.close(); 1106 worker.close(); 1107 server.close(); 1108 } 1109 1110 /** 1111 * java.net.Socket#isInputShutdown() 1112 */ 1113 public void test_isInputShutdown() throws IOException { 1114 ServerSocket server = new ServerSocket(0); 1115 Socket client = new Socket(InetAddress.getLocalHost(), server 1116 .getLocalPort()); 1117 1118 Socket worker = server.accept(); 1119 InputStream theInput = client.getInputStream(); 1120 OutputStream theOutput = worker.getOutputStream(); 1121 1122 // make sure we get the right answer with newly connected socket 1123 assertFalse("Socket indicated input shutdown when it should not have", 1124 client.isInputShutdown()); 1125 1126 // shutdown the output 1127 client.shutdownInput(); 1128 1129 // make sure we get the right answer once it is shut down 1130 assertTrue( 1131 "Socket indicated input was NOT shutdown when it should have been", 1132 client.isInputShutdown()); 1133 1134 client.close(); 1135 worker.close(); 1136 server.close(); 1137 1138 // make sure we get the right answer for closed sockets 1139 assertFalse( 1140 "Socket indicated input was shutdown when socket was closed", 1141 worker.isInputShutdown()); 1142 1143 theInput.close(); 1144 theOutput.close(); 1145 } 1146 1147 /** 1148 * java.net.Socket#isOutputShutdown() 1149 */ 1150 public void test_isOutputShutdown() throws IOException { 1151 ServerSocket server = new ServerSocket(0); 1152 Socket client = new Socket(InetAddress.getLocalHost(), server 1153 .getLocalPort()); 1154 1155 Socket worker = server.accept(); 1156 InputStream theInput = client.getInputStream(); 1157 OutputStream theOutput = worker.getOutputStream(); 1158 1159 // make sure we get the right answer with newly connected socket 1160 assertFalse("Socket indicated output shutdown when it should not have", 1161 worker.isOutputShutdown()); 1162 1163 // shutdown the output 1164 worker.shutdownOutput(); 1165 1166 // make sure we get the right answer once it is shut down 1167 assertTrue( 1168 "Socket indicated output was NOT shutdown when it should have been", 1169 worker.isOutputShutdown()); 1170 1171 client.close(); 1172 worker.close(); 1173 server.close(); 1174 1175 // make sure we get the right answer for closed sockets 1176 assertFalse( 1177 "Socket indicated output was output shutdown when the socket was closed", 1178 client.isOutputShutdown()); 1179 1180 theInput.close(); 1181 theOutput.close(); 1182 } 1183 1184 /** 1185 * java.net.Socket#sendUrgentData(int) 1186 */ 1187 public void test_sendUrgentDataI() throws Exception { 1188 /* 1189 * Some platforms may not support urgent data in this case we will not 1190 * run these tests. For now run on all platforms until we find those 1191 * that do not support urgent data 1192 */ 1193 String platform = System.getProperty("os.name"); 1194 if (platform.equals("Dummy")) { 1195 return; 1196 } 1197 1198 /* 1199 * Test 1: Validate that when OOBInline is false that any urgent data is 1200 * silently ignored 1201 */ 1202 InetAddress localHost = InetAddress.getLocalHost(); 1203 ServerSocket server = new ServerSocket(0, 5, localHost); 1204 SocketAddress serverAddress = new InetSocketAddress(localHost, server 1205 .getLocalPort()); 1206 1207 Socket client = new Socket(); 1208 client.setOOBInline(false); 1209 1210 client.connect(serverAddress); 1211 Socket worker = server.accept(); 1212 worker.setTcpNoDelay(true); 1213 OutputStream theOutput = worker.getOutputStream(); 1214 1215 // Send the regular data 1216 byte[] sendBytes = "Test".getBytes(); 1217 theOutput.write(sendBytes); 1218 theOutput.flush(); 1219 1220 // Send the urgent data byte which should not be received 1221 worker.sendUrgentData("UrgentData".getBytes()[0]); 1222 theOutput.write(sendBytes); 1223 worker.shutdownOutput(); 1224 worker.close(); 1225 1226 // Try to read the bytes back 1227 int totalBytesRead = 0; 1228 byte[] myBytes = new byte[100]; 1229 InputStream theInput = client.getInputStream(); 1230 while (true) { 1231 int bytesRead = theInput.read(myBytes, totalBytesRead, 1232 myBytes.length - totalBytesRead); 1233 if (bytesRead == -1) { 1234 break; 1235 } 1236 totalBytesRead = totalBytesRead + bytesRead; 1237 } 1238 1239 client.close(); 1240 server.close(); 1241 1242 byte[] expectBytes = new byte[2 * sendBytes.length]; 1243 System.arraycopy(sendBytes, 0, expectBytes, 0, sendBytes.length); 1244 System.arraycopy(sendBytes, 0, expectBytes, sendBytes.length, 1245 sendBytes.length); 1246 1247 byte[] resultBytes = new byte[totalBytesRead]; 1248 System.arraycopy(myBytes, 0, resultBytes, 0, totalBytesRead); 1249 1250 assertTrue("Urgent data was received", Arrays.equals(expectBytes, 1251 resultBytes)); 1252 1253 /* 1254 * Test 2: Now validate that urgent data is received as expected. Expect 1255 * that it should be between the two writes. 1256 */ 1257 server = new ServerSocket(0, 5, localHost); 1258 serverAddress = new InetSocketAddress(localHost, server.getLocalPort()); 1259 1260 client = new Socket(); 1261 client.setOOBInline(true); 1262 1263 client.connect(serverAddress); 1264 worker = server.accept(); 1265 worker.setTcpNoDelay(true); 1266 theOutput = worker.getOutputStream(); 1267 1268 // Send the regular data 1269 sendBytes = "Test - Urgent Data".getBytes(); 1270 theOutput.write(sendBytes); 1271 1272 // Send the urgent data (one byte) which should be received 1273 client.setOOBInline(true); 1274 byte urgentByte = "UrgentData".getBytes()[0]; 1275 worker.sendUrgentData(urgentByte); 1276 1277 // Send more data, the urgent byte must stay in position 1278 theOutput.write(sendBytes); 1279 worker.shutdownOutput(); 1280 worker.close(); 1281 1282 // Try to read the bytes back 1283 totalBytesRead = 0; 1284 myBytes = new byte[100]; 1285 theInput = client.getInputStream(); 1286 while (true) { 1287 int bytesRead = theInput.read(myBytes, totalBytesRead, 1288 myBytes.length - totalBytesRead); 1289 if (bytesRead == -1) { 1290 break; 1291 } 1292 totalBytesRead = totalBytesRead + bytesRead; 1293 } 1294 1295 client.close(); 1296 server.close(); 1297 1298 expectBytes = new byte[2 * sendBytes.length + 1]; 1299 System.arraycopy(sendBytes, 0, expectBytes, 0, sendBytes.length); 1300 expectBytes[sendBytes.length] = urgentByte; 1301 System.arraycopy(sendBytes, 0, expectBytes, sendBytes.length + 1, 1302 sendBytes.length); 1303 1304 resultBytes = new byte[totalBytesRead]; 1305 System.arraycopy(myBytes, 0, resultBytes, 0, totalBytesRead); 1306 1307 assertTrue("Urgent data was not received with one urgent byte", Arrays 1308 .equals(expectBytes, resultBytes)); 1309 1310 /* 1311 * Test 3: Now validate that urgent data is received as expected. Expect 1312 * that it should be between the two writes. 1313 */ 1314 server = new ServerSocket(0, 5, localHost); 1315 serverAddress = new InetSocketAddress(localHost, server.getLocalPort()); 1316 1317 client = new Socket(); 1318 client.setOOBInline(true); 1319 1320 client.connect(serverAddress); 1321 worker = server.accept(); 1322 worker.setTcpNoDelay(true); 1323 theOutput = worker.getOutputStream(); 1324 1325 // Send the regular data 1326 sendBytes = "Test - Urgent Data".getBytes(); 1327 theOutput.write(sendBytes); 1328 1329 // Send the urgent data (one byte) which should be received 1330 client.setOOBInline(true); 1331 byte urgentByte1 = "UrgentData".getBytes()[0]; 1332 byte urgentByte2 = "UrgentData".getBytes()[1]; 1333 worker.sendUrgentData(urgentByte1); 1334 worker.sendUrgentData(urgentByte2); 1335 1336 // Send more data, the urgent byte must stay in position 1337 theOutput.write(sendBytes); 1338 worker.shutdownOutput(); 1339 worker.close(); 1340 1341 // Try to read the bytes back 1342 totalBytesRead = 0; 1343 myBytes = new byte[100]; 1344 theInput = client.getInputStream(); 1345 while (true) { 1346 int bytesRead = theInput.read(myBytes, totalBytesRead, 1347 myBytes.length - totalBytesRead); 1348 if (bytesRead == -1) { 1349 break; 1350 } 1351 totalBytesRead = totalBytesRead + bytesRead; 1352 } 1353 1354 client.close(); 1355 server.close(); 1356 1357 expectBytes = new byte[2 * sendBytes.length + 2]; 1358 System.arraycopy(sendBytes, 0, expectBytes, 0, sendBytes.length); 1359 expectBytes[sendBytes.length] = urgentByte1; 1360 expectBytes[sendBytes.length + 1] = urgentByte2; 1361 System.arraycopy(sendBytes, 0, expectBytes, sendBytes.length + 2, 1362 sendBytes.length); 1363 1364 resultBytes = new byte[totalBytesRead]; 1365 System.arraycopy(myBytes, 0, resultBytes, 0, totalBytesRead); 1366 1367 assertTrue("Urgent data was not received with two urgent bytes", Arrays 1368 .equals(expectBytes, resultBytes)); 1369 1370 /* 1371 * Test 4: Now test the case where there is only urgent data. 1372 */ 1373 server = new ServerSocket(0, 5, localHost); 1374 serverAddress = new InetSocketAddress(localHost, server.getLocalPort()); 1375 1376 client = new Socket(); 1377 client.setOOBInline(true); 1378 1379 client.connect(serverAddress); 1380 worker = server.accept(); 1381 worker.setTcpNoDelay(true); 1382 1383 // Send the urgent data (one byte) which should be received 1384 client.setOOBInline(true); 1385 urgentByte = "UrgentData".getBytes()[0]; 1386 worker.sendUrgentData(urgentByte); 1387 worker.close(); 1388 1389 // Try to read the bytes back 1390 theInput = client.getInputStream(); 1391 int byteRead = theInput.read(); 1392 1393 client.close(); 1394 server.close(); 1395 1396 assertEquals("Sole urgent data was not received", 1397 (int) (urgentByte & 0xff), byteRead); 1398 } 1399 1400 /** 1401 * java.net.Socket#setKeepAlive(boolean) 1402 */ 1403 public void test_setKeepAliveZ() throws IOException { 1404 1405 class TestSocket extends Socket { 1406 public TestSocket(SocketImpl impl) throws SocketException { 1407 super(impl); 1408 } 1409 } 1410 1411 // There is not really a good test for this as it is there to detect 1412 // crashed machines. Just make sure we can set it 1413 ServerSocket server = new ServerSocket(0); 1414 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1415 1416 client.setKeepAlive(true); 1417 client.setKeepAlive(false); 1418 client.close(); 1419 server.close(); 1420 1421 // Regression test for HARMONY-1136 1422 new TestSocket(null).setKeepAlive(true); 1423 } 1424 1425 public void test_setOOBInlineZ() throws Exception { 1426 Socket theSocket = new Socket(); 1427 theSocket.setOOBInline(true); 1428 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 1429 } 1430 1431 public void test_setPerformancePreference_Int_Int_Int() throws IOException { 1432 Socket theSocket = new Socket(); 1433 theSocket.setPerformancePreferences(1, 1, 1); 1434 } 1435 1436 public void test_setReceiveBufferSizeI() throws Exception { 1437 ServerSocket server = new ServerSocket(0); 1438 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1439 1440 client.setReceiveBufferSize(130); 1441 assertTrue("Incorrect buffer size", client.getReceiveBufferSize() >= 130); 1442 1443 client.close(); 1444 server.close(); 1445 } 1446 1447 public void test_setReuseAddressZ() throws Exception { 1448 Socket theSocket = new Socket(); 1449 theSocket.setReuseAddress(false); 1450 // Bind to any available port on the given address 1451 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 1452 InetSocketAddress localAddress1 = new InetSocketAddress(theSocket.getLocalAddress(), theSocket.getLocalPort()); 1453 1454 Socket theSocket2 = new Socket(); 1455 theSocket2.setReuseAddress(false); 1456 1457 /* 1458 * Try to invoke a bind while the port is busy (TIME_WAIT). Note 1459 * that we may not succeed, which will cause the test to pass 1460 * without testing the reuseaddr behavior. 1461 */ 1462 theSocket.close(); 1463 theSocket2.bind(localAddress1); 1464 1465 theSocket2.close(); 1466 } 1467 1468 public void test_setSendBufferSizeI() throws Exception { 1469 ServerSocket server = new ServerSocket(0); 1470 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1471 client.setSendBufferSize(134); 1472 assertTrue("Incorrect buffer size", client.getSendBufferSize() >= 134); 1473 client.close(); 1474 server.close(); 1475 } 1476 1477 public void test_setSocketImplFactoryLjava_net_SocketImplFactory() { 1478 // Cannot test as setting will cause the factory to be changed for 1479 // all subsequent sockets 1480 } 1481 1482 public void test_setSoLingerZI() throws IOException { 1483 ServerSocket server = new ServerSocket(0); 1484 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1485 client.setSoLinger(true, 500); 1486 assertEquals("Set incorrect linger", 500, client.getSoLinger()); 1487 client.setSoLinger(false, 0); 1488 client.close(); 1489 server.close(); 1490 } 1491 1492 public void test_setSoTimeoutI() throws Exception { 1493 ServerSocket server = new ServerSocket(0); 1494 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1495 client.setSoTimeout(100); 1496 assertEquals("Set incorrect sotimeout", 100, client.getSoTimeout()); 1497 client.close(); 1498 server.close(); 1499 } 1500 1501 public void test_setTcpNoDelayZ() throws Exception { 1502 ServerSocket server = new ServerSocket(0); 1503 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1504 1505 boolean bool; 1506 client.setTcpNoDelay(bool = !client.getTcpNoDelay()); 1507 assertTrue("Failed to set no delay setting: " + client.getTcpNoDelay(), client.getTcpNoDelay() == bool); 1508 1509 client.close(); 1510 server.close(); 1511 } 1512 1513 public void test_setTrafficClassI() throws Exception { 1514 int IPTOS_LOWCOST = 0x2; 1515 int IPTOS_RELIABILTY = 0x4; 1516 int IPTOS_THROUGHPUT = 0x8; 1517 int IPTOS_LOWDELAY = 0x10; 1518 1519 Socket theSocket = new Socket(); 1520 1521 // validate that value set must be between 0 and 255 1522 try { 1523 theSocket.setTrafficClass(256); 1524 fail("No exception was thrown when traffic class set to 256"); 1525 } catch (IllegalArgumentException expected) { 1526 } 1527 1528 try { 1529 theSocket.setTrafficClass(-1); 1530 fail("No exception was thrown when traffic class set to -1"); 1531 } catch (IllegalArgumentException expected) { 1532 } 1533 1534 // now validate that we can set it to some good values 1535 theSocket.setTrafficClass(IPTOS_LOWCOST); 1536 theSocket.setTrafficClass(IPTOS_RELIABILTY); 1537 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 1538 theSocket.setTrafficClass(IPTOS_LOWDELAY); 1539 } 1540 1541 @SuppressWarnings("deprecation") 1542 public void test_shutdownInput() throws IOException { 1543 ServerSocket server = new ServerSocket(0); 1544 int port = server.getLocalPort(); 1545 Socket client = new Socket(InetAddress.getLocalHost(), port); 1546 1547 Socket worker = server.accept(); 1548 worker.setTcpNoDelay(true); 1549 1550 InputStream theInput = client.getInputStream(); 1551 OutputStream theOutput = worker.getOutputStream(); 1552 1553 // shutdown the input 1554 client.shutdownInput(); 1555 1556 // send the regular data 1557 String sendString = "Test"; 1558 theOutput.write(sendString.getBytes()); 1559 theOutput.flush(); 1560 1561 // RI fails here. It is a RI bug not to return 0 to indicate EOF 1562 assertEquals(0, theInput.available()); 1563 1564 client.close(); 1565 server.close(); 1566 1567 // Regression test for HARMONY-2944 1568 // Port 0 is not allowed to be used in connect() on some platforms, 1569 // Since server has been closed here, so the port is free now 1570 Socket s = new Socket("0.0.0.0", port, false); 1571 s.shutdownInput(); 1572 try { 1573 s.shutdownInput(); 1574 fail("should throw SocketException"); 1575 } catch (SocketException se) { 1576 // Expected 1577 } 1578 s.close(); 1579 } 1580 1581 /** 1582 * java.net.Socket#shutdownOutput() 1583 */ 1584 @SuppressWarnings("deprecation") 1585 public void test_shutdownOutput() throws IOException { 1586 ServerSocket server = new ServerSocket(0); 1587 int port = server.getLocalPort(); 1588 Socket client = new Socket(InetAddress.getLocalHost(), port); 1589 1590 Socket worker = server.accept(); 1591 OutputStream theOutput = worker.getOutputStream(); 1592 1593 // shutdown the output 1594 worker.shutdownOutput(); 1595 1596 // send the regular data 1597 String sendString = "Test"; 1598 try { 1599 theOutput.write(sendString.getBytes()); 1600 theOutput.flush(); 1601 fail("No exception when writing on socket with output shutdown"); 1602 } catch (IOException e) { 1603 // Expected 1604 } 1605 1606 client.close(); 1607 server.close(); 1608 1609 // Regression test for HARMONY-2944 1610 // Port 0 is not allowed to be used in connect() on some platforms, 1611 // Since server has been closed here, so the port is free now 1612 Socket s = new Socket("0.0.0.0", port, false); 1613 s.shutdownOutput(); 1614 try { 1615 s.shutdownOutput(); 1616 fail("should throw SocketException"); 1617 } catch (SocketException se) { 1618 // Expected 1619 } 1620 s.close(); 1621 } 1622 1623 public void test_toString() throws IOException { 1624 ServerSocket server = new ServerSocket(0); 1625 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1626 // RI has "addr" and "localport" instead of "address" and "localPort". 1627 String expected = "Socket[address=" + InetAddress.getLocalHost() 1628 + ",port=" + client.getPort() + ",localPort=" 1629 + client.getLocalPort() + "]"; 1630 assertEquals(expected, client.toString()); 1631 client.close(); 1632 server.close(); 1633 } 1634} 1635