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