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 final int timeoutSet = 100; 971 client.setSoTimeout(timeoutSet); 972 int actualTimeout = client.getSoTimeout(); 973 // The kernel can round the requested value based on the HZ setting. We allow up to 10ms. 974 assertTrue("Returned incorrect sotimeout", Math.abs(timeoutSet - actualTimeout) <= 10); 975 client.close(); 976 server.close(); 977 } 978 979 public void test_getTcpNoDelay() throws Exception { 980 ServerSocket server = new ServerSocket(0); 981 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 982 983 boolean bool = !client.getTcpNoDelay(); 984 client.setTcpNoDelay(bool); 985 assertTrue("Failed to get no delay setting: " + client.getTcpNoDelay(), client.getTcpNoDelay() == bool); 986 987 client.close(); 988 server.close(); 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 int trafficClass = new Socket().getTrafficClass(); 998 assertTrue(0 <= trafficClass); 999 assertTrue(trafficClass <= 255); 1000 } 1001 1002 /** 1003 * java.net.Socket#isBound() 1004 */ 1005 public void test_isBound() throws IOException { 1006 ServerSocket server = new ServerSocket(0); 1007 Socket client = new Socket(InetAddress.getLocalHost(), server 1008 .getLocalPort()); 1009 Socket worker = server.accept(); 1010 1011 assertTrue("Socket indicated not bound when it should be (1)", client 1012 .isBound()); 1013 worker.close(); 1014 client.close(); 1015 server.close(); 1016 1017 client = new Socket(); 1018 assertFalse("Socket indicated bound when it was not (2)", client 1019 .isBound()); 1020 1021 server = new ServerSocket(); 1022 server.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 1023 InetSocketAddress boundAddress = new InetSocketAddress(server 1024 .getInetAddress(), server.getLocalPort()); 1025 client.connect(boundAddress); 1026 worker = server.accept(); 1027 assertTrue("Socket indicated not bound when it should be (2)", client 1028 .isBound()); 1029 worker.close(); 1030 client.close(); 1031 server.close(); 1032 1033 // now test when we bind explicitly 1034 InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress 1035 .getLocalHost(), 0); 1036 client = new Socket(); 1037 assertFalse("Socket indicated bound when it was not (3)", client 1038 .isBound()); 1039 client.bind(theLocalAddress); 1040 assertTrue("Socket indicated not bound when it should be (3a)", client 1041 .isBound()); 1042 client.close(); 1043 assertTrue("Socket indicated not bound when it should be (3b)", client 1044 .isBound()); 1045 } 1046 1047 /** 1048 * java.net.Socket#isClosed() 1049 */ 1050 public void test_isClosed() throws IOException { 1051 ServerSocket server = new ServerSocket(0); 1052 Socket client = new Socket(InetAddress.getLocalHost(), server 1053 .getLocalPort()); 1054 Socket worker = server.accept(); 1055 1056 // validate isClosed returns expected values 1057 assertFalse("Socket should indicate it is not closed(1):", client 1058 .isClosed()); 1059 client.close(); 1060 assertTrue("Socket should indicate it is closed(1):", client.isClosed()); 1061 1062 // validate that isClosed works ok for sockets returned from 1063 // ServerSocket.accept() 1064 assertFalse("Accepted Socket should indicate it is not closed:", worker 1065 .isClosed()); 1066 worker.close(); 1067 assertTrue("Accepted Socket should indicate it is closed:", worker 1068 .isClosed()); 1069 1070 // and finally for the server socket 1071 assertFalse("Server Socket should indicate it is not closed:", server 1072 .isClosed()); 1073 server.close(); 1074 assertTrue("Server Socket should indicate it is closed:", server 1075 .isClosed()); 1076 } 1077 1078 /** 1079 * java.net.Socket#isConnected() 1080 */ 1081 public void test_isConnected() throws IOException { 1082 ServerSocket server = new ServerSocket(0); 1083 Socket client = new Socket(InetAddress.getLocalHost(), server 1084 .getLocalPort()); 1085 Socket worker = server.accept(); 1086 1087 assertTrue("Socket indicated not connected when it should be", client 1088 .isConnected()); 1089 client.close(); 1090 worker.close(); 1091 server.close(); 1092 1093 // now do it with the new constructors and revalidate 1094 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1095 .getLocalHost(), 0); 1096 client = new Socket(); 1097 assertFalse("Socket indicated connected when it was not", client 1098 .isConnected()); 1099 1100 server = new ServerSocket(); 1101 server.bind(theAddress); 1102 InetSocketAddress boundAddress = new InetSocketAddress(server 1103 .getInetAddress(), server.getLocalPort()); 1104 client.connect(boundAddress); 1105 worker = server.accept(); 1106 assertTrue("Socket indicated not connected when it should be", client 1107 .isConnected()); 1108 client.close(); 1109 worker.close(); 1110 server.close(); 1111 } 1112 1113 /** 1114 * java.net.Socket#isInputShutdown() 1115 */ 1116 public void test_isInputShutdown() throws IOException { 1117 ServerSocket server = new ServerSocket(0); 1118 Socket client = new Socket(InetAddress.getLocalHost(), server 1119 .getLocalPort()); 1120 1121 Socket worker = server.accept(); 1122 InputStream theInput = client.getInputStream(); 1123 OutputStream theOutput = worker.getOutputStream(); 1124 1125 // make sure we get the right answer with newly connected socket 1126 assertFalse("Socket indicated input shutdown when it should not have", 1127 client.isInputShutdown()); 1128 1129 // shutdown the output 1130 client.shutdownInput(); 1131 1132 // make sure we get the right answer once it is shut down 1133 assertTrue( 1134 "Socket indicated input was NOT shutdown when it should have been", 1135 client.isInputShutdown()); 1136 1137 client.close(); 1138 worker.close(); 1139 server.close(); 1140 1141 // make sure we get the right answer for closed sockets 1142 assertFalse( 1143 "Socket indicated input was shutdown when socket was closed", 1144 worker.isInputShutdown()); 1145 1146 theInput.close(); 1147 theOutput.close(); 1148 } 1149 1150 /** 1151 * java.net.Socket#isOutputShutdown() 1152 */ 1153 public void test_isOutputShutdown() throws IOException { 1154 ServerSocket server = new ServerSocket(0); 1155 Socket client = new Socket(InetAddress.getLocalHost(), server 1156 .getLocalPort()); 1157 1158 Socket worker = server.accept(); 1159 InputStream theInput = client.getInputStream(); 1160 OutputStream theOutput = worker.getOutputStream(); 1161 1162 // make sure we get the right answer with newly connected socket 1163 assertFalse("Socket indicated output shutdown when it should not have", 1164 worker.isOutputShutdown()); 1165 1166 // shutdown the output 1167 worker.shutdownOutput(); 1168 1169 // make sure we get the right answer once it is shut down 1170 assertTrue( 1171 "Socket indicated output was NOT shutdown when it should have been", 1172 worker.isOutputShutdown()); 1173 1174 client.close(); 1175 worker.close(); 1176 server.close(); 1177 1178 // make sure we get the right answer for closed sockets 1179 assertFalse( 1180 "Socket indicated output was output shutdown when the socket was closed", 1181 client.isOutputShutdown()); 1182 1183 theInput.close(); 1184 theOutput.close(); 1185 } 1186 1187 /** 1188 * java.net.Socket#sendUrgentData(int) 1189 */ 1190 public void test_sendUrgentDataI() throws Exception { 1191 /* 1192 * Some platforms may not support urgent data in this case we will not 1193 * run these tests. For now run on all platforms until we find those 1194 * that do not support urgent data 1195 */ 1196 String platform = System.getProperty("os.name"); 1197 if (platform.equals("Dummy")) { 1198 return; 1199 } 1200 1201 /* 1202 * Test 1: Validate that when OOBInline is false that any urgent data is 1203 * silently ignored 1204 */ 1205 InetAddress localHost = InetAddress.getLocalHost(); 1206 ServerSocket server = new ServerSocket(0, 5, localHost); 1207 SocketAddress serverAddress = new InetSocketAddress(localHost, server 1208 .getLocalPort()); 1209 1210 Socket client = new Socket(); 1211 client.setOOBInline(false); 1212 1213 client.connect(serverAddress); 1214 Socket worker = server.accept(); 1215 worker.setTcpNoDelay(true); 1216 OutputStream theOutput = worker.getOutputStream(); 1217 1218 // Send the regular data 1219 byte[] sendBytes = "Test".getBytes(); 1220 theOutput.write(sendBytes); 1221 theOutput.flush(); 1222 1223 // Send the urgent data byte which should not be received 1224 worker.sendUrgentData("UrgentData".getBytes()[0]); 1225 theOutput.write(sendBytes); 1226 worker.shutdownOutput(); 1227 worker.close(); 1228 1229 // Try to read the bytes back 1230 int totalBytesRead = 0; 1231 byte[] myBytes = new byte[100]; 1232 InputStream theInput = client.getInputStream(); 1233 while (true) { 1234 int bytesRead = theInput.read(myBytes, totalBytesRead, 1235 myBytes.length - totalBytesRead); 1236 if (bytesRead == -1) { 1237 break; 1238 } 1239 totalBytesRead = totalBytesRead + bytesRead; 1240 } 1241 1242 client.close(); 1243 server.close(); 1244 1245 byte[] expectBytes = new byte[2 * sendBytes.length]; 1246 System.arraycopy(sendBytes, 0, expectBytes, 0, sendBytes.length); 1247 System.arraycopy(sendBytes, 0, expectBytes, sendBytes.length, 1248 sendBytes.length); 1249 1250 byte[] resultBytes = new byte[totalBytesRead]; 1251 System.arraycopy(myBytes, 0, resultBytes, 0, totalBytesRead); 1252 1253 assertTrue("Urgent data was received", Arrays.equals(expectBytes, 1254 resultBytes)); 1255 1256 /* 1257 * Test 2: Now validate that urgent data is received as expected. Expect 1258 * that it should be between the two writes. 1259 */ 1260 server = new ServerSocket(0, 5, localHost); 1261 serverAddress = new InetSocketAddress(localHost, server.getLocalPort()); 1262 1263 client = new Socket(); 1264 client.setOOBInline(true); 1265 1266 client.connect(serverAddress); 1267 worker = server.accept(); 1268 worker.setTcpNoDelay(true); 1269 theOutput = worker.getOutputStream(); 1270 1271 // Send the regular data 1272 sendBytes = "Test - Urgent Data".getBytes(); 1273 theOutput.write(sendBytes); 1274 1275 // Send the urgent data (one byte) which should be received 1276 client.setOOBInline(true); 1277 byte urgentByte = "UrgentData".getBytes()[0]; 1278 worker.sendUrgentData(urgentByte); 1279 1280 // Send more data, the urgent byte must stay in position 1281 theOutput.write(sendBytes); 1282 worker.shutdownOutput(); 1283 worker.close(); 1284 1285 // Try to read the bytes back 1286 totalBytesRead = 0; 1287 myBytes = new byte[100]; 1288 theInput = client.getInputStream(); 1289 while (true) { 1290 int bytesRead = theInput.read(myBytes, totalBytesRead, 1291 myBytes.length - totalBytesRead); 1292 if (bytesRead == -1) { 1293 break; 1294 } 1295 totalBytesRead = totalBytesRead + bytesRead; 1296 } 1297 1298 client.close(); 1299 server.close(); 1300 1301 expectBytes = new byte[2 * sendBytes.length + 1]; 1302 System.arraycopy(sendBytes, 0, expectBytes, 0, sendBytes.length); 1303 expectBytes[sendBytes.length] = urgentByte; 1304 System.arraycopy(sendBytes, 0, expectBytes, sendBytes.length + 1, 1305 sendBytes.length); 1306 1307 resultBytes = new byte[totalBytesRead]; 1308 System.arraycopy(myBytes, 0, resultBytes, 0, totalBytesRead); 1309 1310 assertTrue("Urgent data was not received with one urgent byte", Arrays 1311 .equals(expectBytes, resultBytes)); 1312 1313 /* 1314 * Test 3: Now validate that urgent data is received as expected. Expect 1315 * that it should be between the two writes. 1316 */ 1317 server = new ServerSocket(0, 5, localHost); 1318 serverAddress = new InetSocketAddress(localHost, server.getLocalPort()); 1319 1320 client = new Socket(); 1321 client.setOOBInline(true); 1322 1323 client.connect(serverAddress); 1324 worker = server.accept(); 1325 worker.setTcpNoDelay(true); 1326 theOutput = worker.getOutputStream(); 1327 1328 // Send the regular data 1329 sendBytes = "Test - Urgent Data".getBytes(); 1330 theOutput.write(sendBytes); 1331 1332 // Send the urgent data (one byte) which should be received 1333 client.setOOBInline(true); 1334 byte urgentByte1 = "UrgentData".getBytes()[0]; 1335 byte urgentByte2 = "UrgentData".getBytes()[1]; 1336 worker.sendUrgentData(urgentByte1); 1337 worker.sendUrgentData(urgentByte2); 1338 1339 // Send more data, the urgent byte must stay in position 1340 theOutput.write(sendBytes); 1341 worker.shutdownOutput(); 1342 worker.close(); 1343 1344 // Try to read the bytes back 1345 totalBytesRead = 0; 1346 myBytes = new byte[100]; 1347 theInput = client.getInputStream(); 1348 while (true) { 1349 int bytesRead = theInput.read(myBytes, totalBytesRead, 1350 myBytes.length - totalBytesRead); 1351 if (bytesRead == -1) { 1352 break; 1353 } 1354 totalBytesRead = totalBytesRead + bytesRead; 1355 } 1356 1357 client.close(); 1358 server.close(); 1359 1360 expectBytes = new byte[2 * sendBytes.length + 2]; 1361 System.arraycopy(sendBytes, 0, expectBytes, 0, sendBytes.length); 1362 expectBytes[sendBytes.length] = urgentByte1; 1363 expectBytes[sendBytes.length + 1] = urgentByte2; 1364 System.arraycopy(sendBytes, 0, expectBytes, sendBytes.length + 2, 1365 sendBytes.length); 1366 1367 resultBytes = new byte[totalBytesRead]; 1368 System.arraycopy(myBytes, 0, resultBytes, 0, totalBytesRead); 1369 1370 assertTrue("Urgent data was not received with two urgent bytes", Arrays 1371 .equals(expectBytes, resultBytes)); 1372 1373 /* 1374 * Test 4: Now test the case where there is only urgent data. 1375 */ 1376 server = new ServerSocket(0, 5, localHost); 1377 serverAddress = new InetSocketAddress(localHost, server.getLocalPort()); 1378 1379 client = new Socket(); 1380 client.setOOBInline(true); 1381 1382 client.connect(serverAddress); 1383 worker = server.accept(); 1384 worker.setTcpNoDelay(true); 1385 1386 // Send the urgent data (one byte) which should be received 1387 client.setOOBInline(true); 1388 urgentByte = "UrgentData".getBytes()[0]; 1389 worker.sendUrgentData(urgentByte); 1390 worker.close(); 1391 1392 // Try to read the bytes back 1393 theInput = client.getInputStream(); 1394 int byteRead = theInput.read(); 1395 1396 client.close(); 1397 server.close(); 1398 1399 assertEquals("Sole urgent data was not received", 1400 (int) (urgentByte & 0xff), byteRead); 1401 } 1402 1403 /** 1404 * java.net.Socket#setKeepAlive(boolean) 1405 */ 1406 public void test_setKeepAliveZ() throws IOException { 1407 1408 class TestSocket extends Socket { 1409 public TestSocket(SocketImpl impl) throws SocketException { 1410 super(impl); 1411 } 1412 } 1413 1414 // There is not really a good test for this as it is there to detect 1415 // crashed machines. Just make sure we can set it 1416 ServerSocket server = new ServerSocket(0); 1417 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1418 1419 client.setKeepAlive(true); 1420 client.setKeepAlive(false); 1421 client.close(); 1422 server.close(); 1423 1424 // Regression test for HARMONY-1136 1425 new TestSocket(null).setKeepAlive(true); 1426 } 1427 1428 public void test_setOOBInlineZ() throws Exception { 1429 Socket theSocket = new Socket(); 1430 theSocket.setOOBInline(true); 1431 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 1432 } 1433 1434 public void test_setPerformancePreference_Int_Int_Int() throws IOException { 1435 Socket theSocket = new Socket(); 1436 theSocket.setPerformancePreferences(1, 1, 1); 1437 } 1438 1439 public void test_setReceiveBufferSizeI() throws Exception { 1440 ServerSocket server = new ServerSocket(0); 1441 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1442 1443 client.setReceiveBufferSize(130); 1444 assertTrue("Incorrect buffer size", client.getReceiveBufferSize() >= 130); 1445 1446 client.close(); 1447 server.close(); 1448 } 1449 1450 public void test_setReuseAddressZ() throws Exception { 1451 Socket theSocket = new Socket(); 1452 theSocket.setReuseAddress(false); 1453 // Bind to any available port on the given address 1454 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 1455 InetSocketAddress localAddress1 = new InetSocketAddress(theSocket.getLocalAddress(), theSocket.getLocalPort()); 1456 1457 Socket theSocket2 = new Socket(); 1458 theSocket2.setReuseAddress(false); 1459 1460 /* 1461 * Try to invoke a bind while the port is busy (TIME_WAIT). Note 1462 * that we may not succeed, which will cause the test to pass 1463 * without testing the reuseaddr behavior. 1464 */ 1465 theSocket.close(); 1466 theSocket2.bind(localAddress1); 1467 1468 theSocket2.close(); 1469 } 1470 1471 public void test_setSendBufferSizeI() throws Exception { 1472 ServerSocket server = new ServerSocket(0); 1473 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1474 client.setSendBufferSize(134); 1475 assertTrue("Incorrect buffer size", client.getSendBufferSize() >= 134); 1476 client.close(); 1477 server.close(); 1478 } 1479 1480 public void test_setSocketImplFactoryLjava_net_SocketImplFactory() { 1481 // Cannot test as setting will cause the factory to be changed for 1482 // all subsequent sockets 1483 } 1484 1485 public void test_setSoLingerZI() throws IOException { 1486 ServerSocket server = new ServerSocket(0); 1487 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1488 client.setSoLinger(true, 500); 1489 assertEquals("Set incorrect linger", 500, client.getSoLinger()); 1490 client.setSoLinger(false, 0); 1491 client.close(); 1492 server.close(); 1493 } 1494 1495 public void test_setSoTimeoutI() throws Exception { 1496 ServerSocket server = new ServerSocket(0); 1497 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1498 final int timeoutSet = 100; 1499 client.setSoTimeout(timeoutSet); 1500 int actualTimeout = client.getSoTimeout(); 1501 // The kernel can round the requested value based on the HZ setting. We allow up to 10ms. 1502 assertTrue("Set incorrect sotimeout", Math.abs(timeoutSet - actualTimeout) <= 10); 1503 client.close(); 1504 server.close(); 1505 } 1506 1507 public void test_setTcpNoDelayZ() throws Exception { 1508 ServerSocket server = new ServerSocket(0); 1509 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1510 1511 boolean bool; 1512 client.setTcpNoDelay(bool = !client.getTcpNoDelay()); 1513 assertTrue("Failed to set no delay setting: " + client.getTcpNoDelay(), client.getTcpNoDelay() == bool); 1514 1515 client.close(); 1516 server.close(); 1517 } 1518 1519 public void test_setTrafficClassI() throws Exception { 1520 int IPTOS_LOWCOST = 0x2; 1521 int IPTOS_RELIABILTY = 0x4; 1522 int IPTOS_THROUGHPUT = 0x8; 1523 int IPTOS_LOWDELAY = 0x10; 1524 1525 Socket theSocket = new Socket(); 1526 1527 // validate that value set must be between 0 and 255 1528 try { 1529 theSocket.setTrafficClass(256); 1530 fail("No exception was thrown when traffic class set to 256"); 1531 } catch (IllegalArgumentException expected) { 1532 } 1533 1534 try { 1535 theSocket.setTrafficClass(-1); 1536 fail("No exception was thrown when traffic class set to -1"); 1537 } catch (IllegalArgumentException expected) { 1538 } 1539 1540 // now validate that we can set it to some good values 1541 theSocket.setTrafficClass(IPTOS_LOWCOST); 1542 theSocket.setTrafficClass(IPTOS_RELIABILTY); 1543 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 1544 theSocket.setTrafficClass(IPTOS_LOWDELAY); 1545 } 1546 1547 @SuppressWarnings("deprecation") 1548 public void test_shutdownInput() throws IOException { 1549 ServerSocket server = new ServerSocket(0); 1550 int port = server.getLocalPort(); 1551 Socket client = new Socket(InetAddress.getLocalHost(), port); 1552 1553 Socket worker = server.accept(); 1554 worker.setTcpNoDelay(true); 1555 1556 InputStream theInput = client.getInputStream(); 1557 OutputStream theOutput = worker.getOutputStream(); 1558 1559 // shutdown the input 1560 client.shutdownInput(); 1561 1562 // send the regular data 1563 String sendString = "Test"; 1564 theOutput.write(sendString.getBytes()); 1565 theOutput.flush(); 1566 1567 // RI fails here. It is a RI bug not to return 0 to indicate EOF 1568 assertEquals(0, theInput.available()); 1569 1570 client.close(); 1571 server.close(); 1572 1573 // Regression test for HARMONY-2944 1574 // Port 0 is not allowed to be used in connect() on some platforms, 1575 // Since server has been closed here, so the port is free now 1576 Socket s = new Socket("0.0.0.0", port, false); 1577 s.shutdownInput(); 1578 try { 1579 s.shutdownInput(); 1580 fail("should throw SocketException"); 1581 } catch (SocketException se) { 1582 // Expected 1583 } 1584 s.close(); 1585 } 1586 1587 /** 1588 * java.net.Socket#shutdownOutput() 1589 */ 1590 @SuppressWarnings("deprecation") 1591 public void test_shutdownOutput() throws IOException { 1592 ServerSocket server = new ServerSocket(0); 1593 int port = server.getLocalPort(); 1594 Socket client = new Socket(InetAddress.getLocalHost(), port); 1595 1596 Socket worker = server.accept(); 1597 OutputStream theOutput = worker.getOutputStream(); 1598 1599 // shutdown the output 1600 worker.shutdownOutput(); 1601 1602 // send the regular data 1603 String sendString = "Test"; 1604 try { 1605 theOutput.write(sendString.getBytes()); 1606 theOutput.flush(); 1607 fail("No exception when writing on socket with output shutdown"); 1608 } catch (IOException e) { 1609 // Expected 1610 } 1611 1612 client.close(); 1613 server.close(); 1614 1615 // Regression test for HARMONY-2944 1616 // Port 0 is not allowed to be used in connect() on some platforms, 1617 // Since server has been closed here, so the port is free now 1618 Socket s = new Socket("0.0.0.0", port, false); 1619 s.shutdownOutput(); 1620 try { 1621 s.shutdownOutput(); 1622 fail("should throw SocketException"); 1623 } catch (SocketException se) { 1624 // Expected 1625 } 1626 s.close(); 1627 } 1628 1629 public void test_toString() throws IOException { 1630 ServerSocket server = new ServerSocket(0); 1631 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 1632 // RI has "addr" and "localport" instead of "address" and "localPort". 1633 String expected = "Socket[address=" + InetAddress.getLocalHost() 1634 + ",port=" + client.getPort() + ",localPort=" 1635 + client.getLocalPort() + "]"; 1636 assertEquals(expected, client.toString()); 1637 client.close(); 1638 server.close(); 1639 } 1640} 1641