OldSocketTest.java revision c08b5f81dbe5aba03208538570c5434a9dd0eb36
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 libcore.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.SocketTimeoutException; 35import java.net.UnknownHostException; 36import java.nio.channels.IllegalBlockingModeException; 37import java.nio.channels.SocketChannel; 38import java.security.Permission; 39import tests.support.Support_Configuration; 40import tests.support.Support_PortManager; 41 42public class OldSocketTest extends OldSocketTestCase { 43 44 ServerSocket ss; 45 46 Socket s; 47 48 Thread t; 49 50 SecurityManager sm = new SecurityManager() { 51 52 public void checkPermission(Permission perm) {} 53 54 public void checkConnect(String host, int port) { 55 throw new SecurityException(); 56 } 57 }; 58 59 public void test_Constructor() { 60 // create the socket and then validate some basic state 61 s = new Socket(); 62 assertFalse("new socket should not be connected", s.isConnected()); 63 assertFalse("new socket should not be bound", s.isBound()); 64 assertFalse("new socket should not be closed", s.isClosed()); 65 assertFalse("new socket should not be in InputShutdown", s 66 .isInputShutdown()); 67 assertFalse("new socket should not be in OutputShutdown", s 68 .isOutputShutdown()); 69 70 } 71 72 public void test_ConstructorLjava_lang_StringI() throws IOException { 73 // Test for method java.net.Socket(java.lang.String, int) 74 int sport = startServer("Cons String,I"); 75 s = new Socket(InetAddress.getLocalHost().getHostName(), sport); 76 assertTrue("Failed to create socket", s.getPort() == sport); 77 78 //regression for HARMONY-946 79 ServerSocket ss = null; 80 Socket s = null; 81 try { 82 ss = new ServerSocket(0); 83 s = new Socket("0.0.0.0", ss.getLocalPort()); 84 } finally { 85 try { 86 ss.close(); 87 } catch(Exception e) { 88 //ignore 89 } 90 try { 91 s.close(); 92 } catch(Exception e) { 93 //ignore 94 } 95 } 96 97 try { 98 new Socket("unknown.host", 0); 99 fail("UnknownHostException was not thrown."); 100 } catch(UnknownHostException uhe) { 101 //expected 102 } 103 Socket socket = null; 104 try { 105 socket = new Socket(InetAddress.getByName(null), sport); 106 InetAddress address = socket.getLocalAddress(); 107 if (Boolean.getBoolean("java.net.preferIPv6Addresses")) { 108 assertTrue( 109 address.equals(InetAddress.getByName("::1")) || 110 address.equals(InetAddress.getByName("0:0:0:0:0:0:0:1"))); 111 } else { 112 assertEquals(address, InetAddress.getByName("127.0.0.1")); 113 } 114 } finally { 115 try { 116 socket.close(); 117 } catch(Exception e) {} 118 } 119 } 120 121 public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI() 122 throws IOException { 123 // Test for method java.net.Socket(java.lang.String, int, 124 // java.net.InetAddress, int) 125 int sport = startServer("Cons String,I,InetAddress,I"); 126 int portNumber = Support_PortManager.getNextPort(); 127 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 128 InetAddress.getLocalHost(), portNumber); 129 assertTrue("Failed to create socket", s.getPort() == sport); 130 131 if (("true".equals(System.getProperty("java.net.preferIPv6Addresses"))) 132 && !("true".equals(System 133 .getProperty("java.net.preferIPv4Stack")))) { 134 135 // ALTERNATE IPv6 TEST 136 if ("true".equals(System.getProperty("run.ipv6tests"))) { 137 System.out 138 .println("Running testConstructorLjava_lang_StringILjava_net_InetAddressI(OldSocketTest) with IPv6GlobalAddressJcl4: " 139 + Support_Configuration.IPv6GlobalAddressJcl4); 140 int testPort = Support_PortManager.getNextPort(); 141 Socket s1 = null, s2 = null; 142 try { 143 s1 = new Socket( 144 Support_Configuration.IPv6GlobalAddressJcl4, 80, 145 InetAddress.getLocalHost(), testPort); 146 } catch (IOException e) { 147 // check here if InetAddress.getLocalHost() is returning the 148 // loopback address. 149 // if so that is likely the cause of the failure 150 String warning = ""; 151 try { 152 InetAddress returnedLocalHost = InetAddress 153 .getLocalHost(); 154 // don't use isLoopbackAddress for some configurations 155 // as they do not have it 156 if (returnedLocalHost.isLoopbackAddress()) { 157 warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE"; 158 159 } 160 } catch (Exception ex) { 161 warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex; 162 } 163 164 fail("Exception creating 1st socket" + warning + ": " + e); 165 } 166 boolean exception = false; 167 try { 168 s2 = new Socket( 169 Support_Configuration.IPv6GlobalAddressJcl4, 80, 170 InetAddress.getLocalHost(), testPort); 171 } catch (IOException e) { 172 exception = true; 173 } 174 try { 175 s1.close(); 176 if (!exception) 177 s2.close(); 178 } catch (IOException e) { 179 } 180 assertTrue("Was able to create two sockets on same port", 181 exception); 182 } 183 184 } else { 185 int testPort = Support_PortManager.getNextPort(); 186 Socket s1 = null, s2 = null; 187 int serverPort = ss.getLocalPort(); 188 try { 189 s1 = new Socket("127.0.0.1", serverPort, InetAddress 190 .getLocalHost(), testPort); 191 } catch (IOException e) { 192 e.printStackTrace(); 193 194 // check here if InetAddress.getLocalHost() is returning the 195 // loopback address. 196 // if so that is likely the cause of the failure 197 String warning = ""; 198 try { 199 InetAddress returnedLocalHost = InetAddress.getLocalHost(); 200 // don't use isLoopbackAddress for some configurations as 201 // they do not have it 202 if (returnedLocalHost.isLoopbackAddress()) { 203 warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE"; 204 205 } 206 } catch (Exception ex) { 207 warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex; 208 } 209 210 fail("Exception creating 1st socket" + warning + ": " + e); 211 } 212 boolean exception = false; 213 try { 214 s2 = new Socket("127.0.0.1", serverPort, InetAddress 215 .getLocalHost(), testPort); 216 } catch (IOException e) { 217 exception = true; 218 } 219 try { 220 s1.close(); 221 if (!exception) 222 s2.close(); 223 } catch (IOException e) { 224 } 225 assertTrue("Was able to create two sockets on same port", exception); 226 } 227 } 228 229 public void test_ConstructorLjava_lang_StringIZ() throws IOException { 230 // Test for method java.net.Socket(java.lang.String, int, boolean) 231 int sport = startServer("Cons String,I,Z"); 232 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, true); 233 assertTrue("Failed to create socket", s.getPort() == sport); 234 235 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, false); 236 } 237 238 public void test_ConstructorLjava_net_InetAddressI() throws IOException { 239 // Test for method java.net.Socket(java.net.InetAddress, int) 240 int sport = startServer("Cons InetAddress,I"); 241 s = new Socket(InetAddress.getLocalHost(), sport); 242 assertTrue("Failed to create socket", s.getPort() == sport); 243 } 244 245 public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI() 246 throws IOException { 247 // Test for method java.net.Socket(java.net.InetAddress, int, 248 // java.net.InetAddress, int) 249 int sport = startServer("Cons InetAddress,I,InetAddress,I"); 250 int portNumber = Support_PortManager.getNextPort(); 251 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 252 InetAddress.getLocalHost(), portNumber); 253 assertTrue("Failed to create socket", s.getLocalPort() == portNumber); 254 } 255 256 public void test_ConstructorLjava_net_InetAddressIZ() throws IOException { 257 // Test for method java.net.Socket(java.net.InetAddress, int, boolean) 258 int sport = startServer("Cons InetAddress,I,Z"); 259 s = new Socket(InetAddress.getLocalHost(), sport, true); 260 assertTrue("Failed to create socket", s.getPort() == sport); 261 262 s = new Socket(InetAddress.getLocalHost(), sport, false); 263 } 264 265 public void test_close() throws IOException { 266 // Test for method void java.net.Socket.close() 267 int sport = startServer("SServer close"); 268 int portNumber = Support_PortManager.getNextPort(); 269 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 270 try { 271 s.setSoLinger(false, 100); 272 } catch (IOException e) { 273 handleException(e, SO_LINGER); 274 } 275 s.close(); 276 try { 277 s.getOutputStream(); 278 fail("IOException was not thrown."); 279 } catch (java.io.IOException e) { 280 //expected 281 } 282 } 283 284 public void test_getInetAddress() throws IOException { 285 // Test for method java.net.InetAddress java.net.Socket.getInetAddress() 286 int sport = startServer("SServer getInetAddress"); 287 int portNumber = Support_PortManager.getNextPort(); 288 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 289 assertTrue("Returned incorrect InetAddress", s.getInetAddress().equals( 290 InetAddress.getLocalHost())); 291 292 } 293 294 public void test_getInputStream() throws IOException { 295 // Simple fetch test 296 ServerSocket server = new ServerSocket(0); 297 Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort()); 298 InputStream is = client.getInputStream(); 299 assertNotNull("Failed to get stream", is); 300 is.close(); 301 client.close(); 302 server.close(); 303 } 304 305 public void test_getKeepAlive() { 306 try { 307 int sport = startServer("SServer getKeepAlive"); 308 int portNumber = Support_PortManager.getNextPort(); 309 Socket theSocket = new Socket(InetAddress.getLocalHost(), sport, 310 null, portNumber); 311 theSocket.setKeepAlive(true); 312 assertTrue("getKeepAlive false when it should be true", theSocket 313 .getKeepAlive()); 314 theSocket.setKeepAlive(false); 315 assertFalse("getKeepAlive true when it should be False", theSocket 316 .getKeepAlive()); 317 theSocket.close(); 318 try { 319 theSocket.setKeepAlive(false); 320 fail("IOException was not thrown after calling setKeepAlive " + 321 "method."); 322 } catch(IOException ioe) { 323 //expected 324 } 325 try { 326 theSocket.getKeepAlive(); 327 fail("IOException was not thrown after calling getKeepAlive +" + 328 "method."); 329 } catch(IOException ioe) { 330 //expected 331 } 332 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE); 333 } catch (Exception e) { 334 handleException(e, SO_KEEPALIVE); 335 } 336 } 337 338 public void test_getLocalAddress() throws IOException { 339 // Test for method java.net.InetAddress 340 // java.net.Socket.getLocalAddress() 341 int sport = startServer("SServer getLocAddress"); 342 int portNumber = Support_PortManager.getNextPort(); 343 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 344 assertEquals("Returned incorrect InetAddress", 345 InetAddress.getLocalHost(), s.getLocalAddress()); 346 347 // now validate that behaviour when the any address is returned 348 String preferIPv4StackValue = System 349 .getProperty("java.net.preferIPv4Stack"); 350 String preferIPv6AddressesValue = System 351 .getProperty("java.net.preferIPv6Addresses"); 352 353 s = new Socket(); 354 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 355 356 if (((preferIPv4StackValue == null) || preferIPv4StackValue 357 .equalsIgnoreCase("false")) 358 && (preferIPv6AddressesValue != null) 359 && (preferIPv6AddressesValue.equals("true"))) { 360 assertTrue( 361 "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false " 362 + s.getLocalSocketAddress(), 363 s.getLocalAddress() instanceof Inet6Address); 364 } else { 365 assertTrue( 366 "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true " 367 + s.getLocalSocketAddress(), 368 s.getLocalAddress() instanceof Inet4Address); 369 } 370 s.close(); 371 372 } 373 374 public void test_getLocalPort() throws IOException { 375 // Test for method int java.net.Socket.getLocalPort() 376 int sport = startServer("SServer getLocalPort"); 377 int portNumber = Support_PortManager.getNextPort(); 378 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 379 InetAddress.getLocalHost(), portNumber); 380 assertTrue("Returned incorrect port", s.getLocalPort() == portNumber); 381 } 382 383 @SuppressWarnings("deprecation") 384 public void test_getOutputStream() throws IOException { 385 // Test for method java.io.OutputStream 386 // java.net.Socket.getOutputStream() 387 int sport = startServer("SServer getOutputStream"); 388 int portNumber = Support_PortManager.getNextPort(); 389 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 390 java.io.OutputStream os = s.getOutputStream(); 391 assertNotNull("Failed to get stream", os); 392 os.write(1); 393 s.close(); 394 // Regression test for harmony-2934 395 s = new Socket("127.0.0.1", Support_PortManager.getNextPort(), 396 false); 397 OutputStream o = s.getOutputStream(); 398 o.write(1); 399 try { 400 Thread.sleep(1000); 401 } catch (InterruptedException e) { 402 } 403 o.close(); 404 s.close(); 405 406 // Regression test for harmony-2942 407 s = new Socket("0.0.0.0", Support_PortManager.getNextPort(), 408 false); 409 o = s.getOutputStream(); 410 o.write(1); 411 try { 412 Thread.sleep(1000); 413 } catch (InterruptedException e) { 414 } 415 o.close(); 416 s.close(); 417 } 418 419 public void test_getPort() throws IOException { 420 // Test for method int java.net.Socket.getPort() 421 int sport = startServer("SServer getPort"); 422 int portNumber = Support_PortManager.getNextPort(); 423 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 424 assertTrue("Returned incorrect port" + s.getPort(), 425 s.getPort() == sport); 426 } 427 428 public void test_getSoLinger() { 429 // Test for method int java.net.Socket.getSoLinger() 430 int sport = startServer("SServer getSoLinger"); 431 try { 432 int portNumber = Support_PortManager.getNextPort(); 433 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 434 s.setSoLinger(true, 200); 435 assertEquals("Returned incorrect linger", 200, s.getSoLinger()); 436 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER); 437 s.setSoLinger(false, 0); 438 } catch (Exception e) { 439 handleException(e, SO_LINGER); 440 } 441 442 try { 443 int portNumber = Support_PortManager.getNextPort(); 444 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 445 s.close(); 446 try { 447 s.getSoLinger(); 448 fail("SocketException was not thrown."); 449 } catch(SocketException ioe) { 450 //expected 451 } 452 } catch(Exception e) { 453 fail("Unexpected exception was thrown: " + e.toString()); 454 } 455 } 456 457 public void test_getReceiveBufferSize() { 458 try { 459 int sport = startServer("SServer getReceiveBufferSize"); 460 int portNumber = Support_PortManager.getNextPort(); 461 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 462 null, portNumber); 463 s.setReceiveBufferSize(130); 464 assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130); 465 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 466 } catch (Exception e) { 467 handleException(e, SO_RCVBUF); 468 } 469 470 try { 471 Socket newSocket = new Socket(); 472 newSocket.close(); 473 try { 474 newSocket.getReceiveBufferSize(); 475 fail("SocketException was not thrown."); 476 } catch(SocketException e) { 477 //expected 478 } 479 } catch(Exception e) { 480 fail("Unexpected exception."); 481 } 482 } 483 484 public void test_getSendBufferSize() { 485 int sport = startServer("SServer setSendBufferSize"); 486 try { 487 int portNumber = Support_PortManager.getNextPort(); 488 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 489 null, portNumber); 490 s.setSendBufferSize(134); 491 assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134); 492 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF); 493 } catch (Exception e) { 494 handleException(e, SO_SNDBUF); 495 } 496 try { 497 int portNumber = Support_PortManager.getNextPort(); 498 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 499 s.close(); 500 try { 501 s.getSendBufferSize(); 502 fail("IOException was not thrown."); 503 } catch(IOException ioe) { 504 //expected 505 } 506 } catch(Exception e) { 507 fail("Unexpected exception was thrown: " + e.toString()); 508 } 509 } 510 511 public void test_getSoTimeout() { 512 // Test for method int java.net.Socket.getSoTimeout() 513 int sport = startServer("SServer getSoTimeout"); 514 try { 515 s = new Socket(InetAddress.getLocalHost(), sport); 516 s.setSoTimeout(100); 517 assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout()); 518 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT); 519 } catch (Exception e) { 520 handleException(e, SO_TIMEOUT); 521 } 522 523 try { 524 int portNumber = Support_PortManager.getNextPort(); 525 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 526 s.close(); 527 try { 528 s.getSoTimeout(); 529 fail("SocketException was not thrown."); 530 } catch(SocketException ioe) { 531 //expected 532 } 533 } catch(Exception e) { 534 fail("Unexpected exception was thrown: " + e.toString()); 535 } 536 } 537 538 public void test_getTcpNoDelay() { 539 // Test for method boolean java.net.Socket.getTcpNoDelay() 540 int sport = startServer("SServer getTcpNoDelay"); 541 try { 542 int portNumber = Support_PortManager.getNextPort(); 543 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 544 boolean bool = !s.getTcpNoDelay(); 545 s.setTcpNoDelay(bool); 546 assertTrue("Failed to get no delay setting: " + s.getTcpNoDelay(), 547 s.getTcpNoDelay() == bool); 548 ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY); 549 } catch (Exception e) { 550 handleException(e, TCP_NODELAY); 551 } 552 553 try { 554 int portNumber = Support_PortManager.getNextPort(); 555 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 556 s.close(); 557 try { 558 s.getTcpNoDelay(); 559 fail("SocketException was not thrown."); 560 } catch(SocketException ioe) { 561 //expected 562 } 563 } catch(Exception e) { 564 fail("Unexpected exception was thrown: " + e.toString()); 565 } 566 } 567 568 public void test_setKeepAliveZ() throws Exception { 569 // There is not really a good test for this as it is there to detect 570 // crashed machines. Just make sure we can set it 571 try { 572 int sport = startServer("SServer setKeepAlive"); 573 int portNumber = Support_PortManager.getNextPort(); 574 Socket theSocket = new Socket(InetAddress.getLocalHost(), sport, 575 null, portNumber); 576 theSocket.setKeepAlive(true); 577 theSocket.setKeepAlive(false); 578 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE); 579 } catch (Exception e) { 580 handleException(e, SO_KEEPALIVE); 581 } 582 // regression test for HARMONY-1136 583 new TestSocket((SocketImpl) null).setKeepAlive(true); 584 585 try { 586 Socket theSocket = new Socket(); 587 theSocket.close(); 588 theSocket.setKeepAlive(true); 589 fail("SocketException was not thrown."); 590 } catch(SocketException ioe) { 591 //expected 592 } 593 } 594 class TestSocket extends Socket { 595 public TestSocket(SocketImpl impl) throws SocketException { 596 super(impl); 597 } 598 } 599 600 public void test_setSocketImplFactoryLjava_net_SocketImplFactory() { 601 // Test for method void 602 // java.net.Socket.setSocketImplFactory(java.net.SocketImplFactory) 603 604 // Cannot test as setting will cause the factory to be changed for 605 // all subsequent sockets 606 607 SecurityManager sm = new SecurityManager() { 608 609 public void checkPermission(Permission perm) { 610 } 611 612 public void checkSetFactory() { 613 throw new SecurityException(); 614 } 615 }; 616 } 617 618 public void test_setSendBufferSizeI() { 619 try { 620 int sport = startServer("SServer setSendBufferSizeI"); 621 int portNumber = Support_PortManager.getNextPort(); 622 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 623 s.setSendBufferSize(134); 624 assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134); 625 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF); 626 } catch (Exception e) { 627 handleException(e, SO_SNDBUF); 628 } 629 630 try { 631 Socket theSocket = new Socket(); 632 theSocket.close(); 633 theSocket.setSendBufferSize(1); 634 fail("SocketException was not thrown."); 635 } catch(SocketException ioe) { 636 //expected 637 } catch(IOException ioe) { 638 fail("IOException was thrown."); 639 } 640 } 641 642 public void test_setReceiveBufferSizeI() { 643 try { 644 int sport = startServer("SServer setReceiveBufferSizeI"); 645 int portNumber = Support_PortManager.getNextPort(); 646 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 647 s.setReceiveBufferSize(130); 648 assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130); 649 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF); 650 } catch (Exception e) { 651 handleException(e, SO_RCVBUF); 652 } 653 654 try { 655 Socket theSocket = new Socket(); 656 theSocket.close(); 657 theSocket.setReceiveBufferSize(1); 658 fail("SocketException was not thrown."); 659 } catch(SocketException ioe) { 660 //expected 661 } catch(IOException ioe) { 662 fail("IOException was thrown."); 663 } 664 } 665 666 public void test_setSoLingerZI() { 667 // Test for method void java.net.Socket.setSoLinger(boolean, int) 668 try { 669 int sport = startServer("SServer setSoLingerZI"); 670 int portNumber = Support_PortManager.getNextPort(); 671 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 672 s.setSoLinger(true, 500); 673 assertEquals("Set incorrect linger", 500, s.getSoLinger()); 674 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER); 675 s.setSoLinger(false, 0); 676 } catch (Exception e) { 677 handleException(e, SO_LINGER); 678 } 679 680 try { 681 Socket theSocket = new Socket(); 682 theSocket.close(); 683 theSocket.setSoLinger(true, 1); 684 fail("SocketException was not thrown."); 685 } catch(SocketException ioe) { 686 //expected 687 } catch(IOException ioe) { 688 fail("IOException was thrown."); 689 } 690 } 691 692 public void test_setSoTimeoutI() { 693 // Test for method void java.net.Socket.setSoTimeout(int) 694 try { 695 int sport = startServer("SServer seSoTimeoutI"); 696 int portNumber = Support_PortManager.getNextPort(); 697 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 698 s.setSoTimeout(100); 699 assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout()); 700 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT); 701 } catch (Exception e) { 702 handleException(e, SO_TIMEOUT); 703 } 704 705 try { 706 Socket theSocket = new Socket(); 707 theSocket.close(); 708 theSocket.setSoTimeout(1); 709 fail("SocketException was not thrown."); 710 } catch(SocketException ioe) { 711 //expected 712 } catch(IOException ioe) { 713 fail("IOException was thrown."); 714 } 715 } 716 717 public void test_setTcpNoDelayZ() { 718 // Test for method void java.net.Socket.setTcpNoDelay(boolean) 719 try { 720 int sport = startServer("SServer setTcpNoDelayZ"); 721 int portNumber = Support_PortManager.getNextPort(); 722 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 723 boolean bool; 724 s.setTcpNoDelay(bool = !s.getTcpNoDelay()); 725 assertTrue("Failed to set no delay setting: " + s.getTcpNoDelay(), 726 s.getTcpNoDelay() == bool); 727 ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY); 728 } catch (Exception e) { 729 handleException(e, TCP_NODELAY); 730 } 731 732 try { 733 Socket theSocket = new Socket(); 734 theSocket.close(); 735 theSocket.setTcpNoDelay(true); 736 fail("SocketException was not thrown."); 737 } catch(SocketException ioe) { 738 //expected 739 } catch(IOException ioe) { 740 fail("IOException was thrown."); 741 } 742 } 743 744 public void test_toString() throws IOException { 745 // Test for method java.lang.String java.net.Socket.toString() 746 int sport = startServer("SServer toString"); 747 int portNumber = Support_PortManager.getNextPort(); 748 s = new Socket(InetAddress.getLocalHost().getHostName(), sport, 749 InetAddress.getLocalHost(), portNumber); 750 assertEquals("Socket[address=" + InetAddress.getLocalHost() + ",port=" + s.getPort() 751 + ",localPort=" + s.getLocalPort() + "]", s.toString()); 752 } 753 754 // AndroidOnly: RI returns wrong value for EOF 755 public void test_shutdownInput() throws Exception { 756 InetAddress addr = InetAddress.getLocalHost(); 757 int port = Support_PortManager.getNextPort(); 758 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 759 Socket theSocket = new Socket(addr, port); 760 Socket servSock = serverSocket.accept(); 761 762 InputStream theInput = theSocket.getInputStream(); 763 OutputStream theOutput = servSock.getOutputStream(); 764 765 // shutdown the input 766 theSocket.shutdownInput(); 767 768 // send the regular data 769 String sendString = new String("Test"); 770 theOutput.write(sendString.getBytes()); 771 theOutput.flush(); 772 773 // give things some time to settle 774 Thread.sleep(1000); 775 776 // RI fails here. It is a RI bug not to return 0 to indicate EOF 777 assertEquals(0, theInput.available()); 778 779 theSocket.close(); 780 serverSocket.close(); 781 782 Socket socket = new Socket(); 783 socket.close(); 784 try { 785 socket.shutdownInput(); 786 fail("IOException was not thrown."); 787 } catch(IOException ioe) { 788 //expected 789 } 790 } 791 792 public void test_shutdownOutput() throws IOException { 793 InetAddress addr = InetAddress.getLocalHost(); 794 int port = Support_PortManager.getNextPort(); 795 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 796 Socket theSocket = new Socket(addr, port); 797 Socket servSock = serverSocket.accept(); 798 799 InputStream theInput = theSocket.getInputStream(); 800 OutputStream theOutput = servSock.getOutputStream(); 801 802 // shutdown the output 803 servSock.shutdownOutput(); 804 805 // send the regular data 806 String sendString = new String("Test"); 807 try { 808 theOutput.write(sendString.getBytes()); 809 theOutput.flush(); 810 fail("No exception when writing on socket with output shutdown"); 811 } catch (Exception e) { 812 } 813 814 theSocket.close(); 815 serverSocket.close(); 816 817 try { 818 theSocket.shutdownInput(); 819 fail("IOException was not thrown."); 820 } catch(IOException ioe) { 821 //expected 822 } 823 } 824 825 public void test_getLocalSocketAddress() throws IOException { 826 // set up server connect and then validate that we get the right 827 // response for the local address 828 int sport = startServer("SServer getLocSocketAddress"); 829 int portNumber = Support_PortManager.getNextPort(); 830 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 831 assertTrue( 832 "Returned incorrect InetSocketAddress(1):" 833 + s.getLocalSocketAddress().toString() 834 + "Expected: " 835 + (new InetSocketAddress(InetAddress.getLocalHost(), 836 portNumber)).toString(), s 837 .getLocalSocketAddress().equals( 838 new InetSocketAddress(InetAddress 839 .getLocalHost(), portNumber))); 840 s.close(); 841 842 // now create a socket that is not bound and validate we get the 843 // right answer 844 Socket theSocket = new Socket(); 845 assertNull( 846 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 847 theSocket.getLocalSocketAddress()); 848 849 // now bind the socket and make sure we get the right answer 850 portNumber = Support_PortManager.getNextPort(); 851 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 852 portNumber)); 853 assertTrue( 854 "Returned incorrect InetSocketAddress(2):" 855 + theSocket.getLocalSocketAddress().toString() 856 + "Expected: " 857 + (new InetSocketAddress(InetAddress.getLocalHost(), 858 portNumber)).toString(), theSocket 859 .getLocalSocketAddress().equals( 860 new InetSocketAddress(InetAddress 861 .getLocalHost(), portNumber))); 862 theSocket.close(); 863 864 // now validate that behaviour when the any address is returned 865 s = new Socket(); 866 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 867 868 String preferIPv4StackValue = System 869 .getProperty("java.net.preferIPv4Stack"); 870 String preferIPv6AddressesValue = System 871 .getProperty("java.net.preferIPv6Addresses"); 872 if (((preferIPv4StackValue == null) || preferIPv4StackValue 873 .equalsIgnoreCase("false")) 874 && (preferIPv6AddressesValue != null) 875 && (preferIPv6AddressesValue.equals("true"))) { 876 assertTrue( 877 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " 878 + s.getLocalSocketAddress(), 879 ((InetSocketAddress) s.getLocalSocketAddress()) 880 .getAddress() instanceof Inet6Address); 881 } else { 882 assertTrue( 883 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " 884 + s.getLocalSocketAddress(), 885 ((InetSocketAddress) s.getLocalSocketAddress()) 886 .getAddress() instanceof Inet4Address); 887 } 888 s.close(); 889 890 // now validate the same for getLocalAddress 891 s = new Socket(); 892 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 893 if (((preferIPv4StackValue == null) || preferIPv4StackValue 894 .equalsIgnoreCase("false")) 895 && (preferIPv6AddressesValue != null) 896 && (preferIPv6AddressesValue.equals("true"))) { 897 assertTrue( 898 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " 899 + s.getLocalSocketAddress(), 900 ((InetSocketAddress) s.getLocalSocketAddress()) 901 .getAddress() instanceof Inet6Address); 902 } else { 903 assertTrue( 904 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " 905 + s.getLocalSocketAddress(), 906 ((InetSocketAddress) s.getLocalSocketAddress()) 907 .getAddress() instanceof Inet4Address); 908 } 909 s.close(); 910 } 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 int sport = startServer("SServer getLocRemoteAddress"); 916 int portNumber = Support_PortManager.getNextPort(); 917 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 918 assertTrue("Returned incorrect InetSocketAddress(1):" 919 + s.getLocalSocketAddress().toString(), 920 s.getRemoteSocketAddress() 921 .equals( 922 new InetSocketAddress(InetAddress 923 .getLocalHost(), sport))); 924 s.close(); 925 926 // now create one that is not connect and validate that we get the 927 // right answer 928 Socket theSocket = new Socket(); 929 portNumber = Support_PortManager.getNextPort(); 930 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 931 portNumber)); 932 933 assertNull("Returned incorrect InetSocketAddress -unconnected socket:" 934 + "Expected: NULL", theSocket.getRemoteSocketAddress()); 935 936 // now connect and validate we get the right answer 937 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 938 sport)); 939 assertTrue("Returned incorrect InetSocketAddress(2):" 940 + theSocket.getRemoteSocketAddress().toString(), 941 theSocket.getRemoteSocketAddress() 942 .equals( 943 new InetSocketAddress(InetAddress 944 .getLocalHost(), sport))); 945 theSocket.close(); 946 947 } 948 949 public void test_isBound() throws IOException { 950 InetAddress addr = InetAddress.getLocalHost(); 951 int port = Support_PortManager.getNextPort(); 952 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 953 Socket theSocket = new Socket(addr, port); 954 Socket servSock = serverSocket.accept(); 955 assertTrue("Socket indicated not bound when it should be (1)", 956 theSocket.isBound()); 957 theSocket.close(); 958 serverSocket.close(); 959 960 // now do it with the new constructors and revalidate. Connect causes 961 // the socket to be bound 962 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 963 .getLocalHost(), Support_PortManager.getNextPort()); 964 theSocket = new Socket(); 965 assertFalse("Socket indicated bound when it was not (2)", theSocket 966 .isBound()); 967 serverSocket = new ServerSocket(); 968 serverSocket.bind(theAddress); 969 theSocket.connect(theAddress); 970 servSock = serverSocket.accept(); 971 assertTrue("Socket indicated not bound when it should be (2)", 972 theSocket.isBound()); 973 theSocket.close(); 974 serverSocket.close(); 975 976 // now test when we bind explicitly 977 InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress 978 .getLocalHost(), Support_PortManager.getNextPort()); 979 theSocket = new Socket(); 980 assertFalse("Socket indicated bound when it was not (3)", theSocket 981 .isBound()); 982 theSocket.bind(theLocalAddress); 983 assertTrue("Socket indicated not bound when it should be (3a)", 984 theSocket.isBound()); 985 theSocket.close(); 986 assertTrue("Socket indicated not bound when it should be (3b)", 987 theSocket.isBound()); 988 } 989 990 public void test_isConnected() throws IOException { 991 InetAddress addr = InetAddress.getLocalHost(); 992 int port = Support_PortManager.getNextPort(); 993 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 994 Socket theSocket = new Socket(addr, port); 995 Socket servSock = serverSocket.accept(); 996 assertTrue("Socket indicated not connected when it should be", 997 theSocket.isConnected()); 998 theSocket.close(); 999 serverSocket.close(); 1000 1001 // now do it with the new constructors and revalidate 1002 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1003 .getLocalHost(), Support_PortManager.getNextPort()); 1004 theSocket = new Socket(); 1005 assertFalse("Socket indicated connected when it was not", theSocket 1006 .isConnected()); 1007 serverSocket = new ServerSocket(); 1008 serverSocket.bind(theAddress); 1009 theSocket.connect(theAddress); 1010 servSock = serverSocket.accept(); 1011 assertTrue("Socket indicated not connected when it should be", 1012 theSocket.isConnected()); 1013 theSocket.close(); 1014 serverSocket.close(); 1015 } 1016 1017 public void test_isClosed() throws IOException { 1018 InetAddress addr = InetAddress.getLocalHost(); 1019 int port = Support_PortManager.getNextPort(); 1020 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1021 Socket theSocket = new Socket(addr, port); 1022 Socket servSock = serverSocket.accept(); 1023 1024 // validate isClosed returns expected values 1025 assertFalse("Socket should indicate it is not closed(1):", theSocket 1026 .isClosed()); 1027 theSocket.close(); 1028 assertTrue("Socket should indicate it is closed(1):", theSocket 1029 .isClosed()); 1030 1031 theSocket = new Socket(addr, port); 1032 assertFalse("Socket should indicate it is not closed(2):", theSocket 1033 .isClosed()); 1034 theSocket.close(); 1035 assertTrue("Socket should indicate it is closed(2):", theSocket 1036 .isClosed()); 1037 1038 // validate that isClosed works ok for sockets returned from 1039 // ServerSocket.accept() 1040 assertFalse("Server Socket should indicate it is not closed:", servSock 1041 .isClosed()); 1042 servSock.close(); 1043 assertTrue("Server Socket should indicate it is closed:", servSock 1044 .isClosed()); 1045 } 1046 1047 public void test_bindLjava_net_SocketAddress() throws IOException { 1048 1049 class mySocketAddress extends SocketAddress { 1050 1051 public mySocketAddress() { 1052 } 1053 } 1054 1055 // Address we cannot bind to 1056 Socket theSocket = new Socket(); 1057 try { 1058 theSocket.bind(new InetSocketAddress(InetAddress 1059 .getByAddress(Support_Configuration.nonLocalAddressBytes), 1060 Support_PortManager.getNextPort())); 1061 fail("No exception when binding to bad address:" 1062 + theSocket.getLocalSocketAddress().toString()); 1063 } catch (IOException ex) { 1064 } 1065 theSocket.close(); 1066 1067 // now create a socket that is not bound and then bind it 1068 theSocket = new Socket(); 1069 int portNumber = Support_PortManager.getNextPort(); 1070 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1071 portNumber)); 1072 1073 // validate that the localSocketAddress reflects the address we 1074 // bound to 1075 assertTrue( 1076 "Local address not correct after bind:" 1077 + theSocket.getLocalSocketAddress().toString() 1078 + " Expected: " 1079 + (new InetSocketAddress(InetAddress.getLocalHost(), 1080 portNumber)).toString(), theSocket 1081 .getLocalSocketAddress().equals( 1082 new InetSocketAddress(InetAddress 1083 .getLocalHost(), portNumber))); 1084 1085 // make sure we can now connect and that connections appear to come 1086 // from the address we bound to. 1087 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1088 .getLocalHost(), Support_PortManager.getNextPort()); 1089 ServerSocket serverSocket = new ServerSocket(); 1090 serverSocket.bind(theAddress); 1091 theSocket.connect(theAddress); 1092 Socket servSock = serverSocket.accept(); 1093 assertTrue( 1094 "Returned Remote address from server connected to does not match expected local address:" 1095 + servSock.getRemoteSocketAddress().toString() 1096 + " Expected: " 1097 + (new InetSocketAddress(InetAddress.getLocalHost(), 1098 portNumber)).toString(), servSock 1099 .getRemoteSocketAddress().equals( 1100 new InetSocketAddress(InetAddress 1101 .getLocalHost(), portNumber))); 1102 theSocket.close(); 1103 servSock.close(); 1104 serverSocket.close(); 1105 1106 // validate if we pass in null that it picks an address for us and 1107 // all is ok 1108 theSocket = new Socket(); 1109 theSocket.bind(null); 1110 assertNotNull("Bind with null did not work", theSocket 1111 .getLocalSocketAddress()); 1112 theSocket.close(); 1113 1114 // now check the error conditions 1115 1116 // Address that we have already bound to 1117 theSocket = new Socket(); 1118 Socket theSocket2 = new Socket(); 1119 try { 1120 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1121 Support_PortManager.getNextPort()); 1122 theSocket.bind(theAddress); 1123 theSocket2.bind(theAddress); 1124 fail("No exception binding to address that is not available"); 1125 } catch (IOException ex) { 1126 } 1127 theSocket.close(); 1128 theSocket2.close(); 1129 1130 // unsupported SocketAddress subclass 1131 theSocket = new Socket(); 1132 try { 1133 theSocket.bind(new mySocketAddress()); 1134 fail("No exception when binding using unsupported SocketAddress subclass"); 1135 } catch (IllegalArgumentException ex) { 1136 } 1137 theSocket.close(); 1138 } 1139 1140 public void test_bindLjava_net_SocketAddress_Proxy() throws IOException { 1141 //The Proxy will not impact on the bind operation.It can be assigned with any address. 1142 Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 0)); 1143 Socket socket = new Socket(proxy); 1144 1145 try { 1146 InetAddress address = InetAddress.getByName("localhost"); 1147 int port = 0; 1148 socket.bind(new InetSocketAddress(address, port)); 1149 1150 assertEquals(address, socket.getLocalAddress()); 1151 assertTrue(port!=socket.getLocalPort()); 1152 1153 } finally { 1154 socket.close(); 1155 } 1156 } 1157 1158 public void test_connectLjava_net_SocketAddress() throws Exception { 1159 // needed for some tests 1160 class mySocketAddress extends SocketAddress { 1161 1162 public mySocketAddress() { 1163 } 1164 } 1165 1166 class SocketCloser extends Thread { 1167 1168 int timeout = 0; 1169 1170 Socket theSocket = null; 1171 1172 public void run() { 1173 try { 1174 Thread.sleep(timeout); 1175 theSocket.close(); 1176 } catch (Exception e) { 1177 } 1178 ; 1179 return; 1180 } 1181 1182 public SocketCloser(int timeout, Socket theSocket) { 1183 this.timeout = timeout; 1184 this.theSocket = theSocket; 1185 } 1186 } 1187 1188 // start by validating the error checks 1189 int portNumber = Support_PortManager.getNextPort(); 1190 Socket theSocket = null; 1191 ServerSocket serverSocket = null; 1192 SocketAddress theAddress = null; 1193 SocketAddress nonConnectableAddress = null; 1194 SocketAddress nonReachableAddress = null; 1195 SocketAddress invalidType = null; 1196 // byte[] theBytes = {-1,-1,-1,-1}; 1197 byte[] theBytes = { 0, 0, 0, 0 }; 1198 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1199 portNumber); 1200 nonConnectableAddress = new InetSocketAddress(InetAddress 1201 .getByAddress(theBytes), portNumber); 1202 nonReachableAddress = new InetSocketAddress(InetAddress 1203 .getByName(Support_Configuration.ResolvedNotExistingHost), 1204 portNumber); 1205 1206 invalidType = new mySocketAddress(); 1207 1208 try { 1209 theSocket = new Socket(); 1210 theSocket.connect(null); 1211 fail("No exception after null address passed in"); 1212 } catch (Exception e) { 1213 assertTrue("Wrong exception null address passed in: " 1214 + e.toString(), (e instanceof IllegalArgumentException)); 1215 } 1216 1217 try { 1218 theSocket = new Socket(); 1219 theSocket.connect(invalidType); 1220 fail("No exception when invalid socket address type passed in: "); 1221 } catch (Exception e) { 1222 assertTrue( 1223 "Wrong exception when when invalid socket address type passed in: " 1224 + e.toString(), 1225 (e instanceof IllegalArgumentException)); 1226 } 1227 1228 try { 1229 theSocket = new Socket(); 1230 theSocket.connect(nonConnectableAddress); 1231 fail("No exception when non Connectable Address passed in: "); 1232 } catch (Exception e) { 1233 assertTrue( 1234 "Wrong exception when non Connectable Address passed in: " 1235 + e.toString(), (e instanceof ConnectException)); 1236 } 1237 1238 // now validate that we get a connect exception if we try to connect to 1239 // an address on which nobody is listening 1240 try { 1241 theSocket = new Socket(); 1242 theSocket.connect(theAddress); 1243 theSocket.close(); 1244 fail("No exception when connecting to address nobody listening on: "); 1245 } catch (Exception e) { 1246 assertTrue( 1247 "Wrong exception when connecting to address nobody listening on: " 1248 + e.toString(), (e instanceof ConnectException)); 1249 } 1250 1251 // now validate that we can actually connect when somebody is listening 1252 theSocket = new Socket(); 1253 serverSocket = new ServerSocket(); 1254 serverSocket.bind(theAddress); 1255 theSocket.connect(theAddress); 1256 theSocket.close(); 1257 serverSocket.close(); 1258 1259 // now validate that we can actually connect when somebody is listening 1260 theSocket = new Socket(); 1261 serverSocket = new ServerSocket(); 1262 serverSocket.bind(theAddress); 1263 theSocket.connect(theAddress); 1264 1265 // validate that when a socket is connected that it answers 1266 // correctly to related queries 1267 assertTrue("Socket did not returned connected when it is: ", theSocket 1268 .isConnected()); 1269 assertFalse("Socket returned closed when it should be connected ", 1270 theSocket.isClosed()); 1271 assertTrue("Socket returned not bound when it should be: ", theSocket 1272 .isBound()); 1273 assertFalse( 1274 "Socket returned input Shutdown when it should be connected ", 1275 theSocket.isInputShutdown()); 1276 assertFalse( 1277 "Socket returned output Shutdown when it should be connected ", 1278 theSocket.isOutputShutdown()); 1279 assertTrue("Local port on connected socket was 0", theSocket 1280 .getLocalPort() != 0); 1281 theSocket.close(); 1282 serverSocket.close(); 1283 1284 // now validate that we get the right exception if we connect when we 1285 // are already connected 1286 try { 1287 theSocket = new Socket(); 1288 serverSocket = new ServerSocket(); 1289 serverSocket.bind(theAddress); 1290 theSocket.connect(theAddress); 1291 theSocket.connect(theAddress); 1292 theSocket.close(); 1293 serverSocket.close(); 1294 fail("No exception when we try to connect on a connected socket: "); 1295 1296 } catch (Exception e) { 1297 assertTrue( 1298 "Wrong exception when connecting on socket that is allready connected" 1299 + e.toString(), (e instanceof SocketException)); 1300 assertFalse( 1301 "Wrong exception when connecting on socket that is allready connected" 1302 + e.toString(), 1303 (e instanceof SocketTimeoutException)); 1304 try { 1305 theSocket.close(); 1306 serverSocket.close(); 1307 } catch (Exception e2) { 1308 } 1309 1310 } 1311 1312 // now validate that connected socket can be used to read/write 1313 theSocket = new Socket(); 1314 serverSocket = new ServerSocket(); 1315 serverSocket.bind(theAddress); 1316 theSocket.connect(theAddress); 1317 Socket servSock = serverSocket.accept(); 1318 InputStream theInput = theSocket.getInputStream(); 1319 OutputStream theOutput = servSock.getOutputStream(); 1320 InputStream theInput2 = servSock.getInputStream(); 1321 OutputStream theOutput2 = theSocket.getOutputStream(); 1322 1323 String sendString = new String("Test"); 1324 theOutput.write(sendString.getBytes()); 1325 theOutput.flush(); 1326 1327 Thread.sleep(1000); 1328 1329 int totalBytesRead = 0; 1330 byte[] myBytes = new byte[100]; 1331 while (theInput.available() > 0) { 1332 int bytesRead = theInput.read(myBytes, totalBytesRead, 1333 myBytes.length - totalBytesRead); 1334 totalBytesRead = totalBytesRead + bytesRead; 1335 } 1336 1337 String receivedString = new String(myBytes, 0, totalBytesRead); 1338 assertTrue("Could not recv on socket connected with timeout:" 1339 + receivedString + ":" + sendString, receivedString 1340 .equals(sendString)); 1341 1342 sendString = new String("SEND - Test"); 1343 theOutput2.write(sendString.getBytes()); 1344 theOutput2.flush(); 1345 Thread.sleep(1000); 1346 1347 totalBytesRead = 0; 1348 myBytes = new byte[100]; 1349 while (theInput2.available() > 0) { 1350 int bytesRead = theInput2.read(myBytes, totalBytesRead, 1351 myBytes.length - totalBytesRead); 1352 totalBytesRead = totalBytesRead + bytesRead; 1353 } 1354 1355 receivedString = new String(myBytes, 0, totalBytesRead); 1356 assertTrue("Could not send on socket connected with timeout:" 1357 + receivedString + ":" + sendString, receivedString 1358 .equals(sendString)); 1359 1360 theSocket.close(); 1361 serverSocket.close(); 1362 1363 SocketChannel channel = SocketChannel.open(); 1364 channel.configureBlocking(false); 1365 Socket socket = channel.socket(); 1366 int port = Support_PortManager.getNextPort(); 1367 try { 1368 socket.connect( new InetSocketAddress(InetAddress.getLocalHost(), 1369 Support_PortManager.getNextPort())); 1370 fail("IllegalBlockingModeException was not thrown."); 1371 } catch(IllegalBlockingModeException ibme) { 1372 //expected 1373 } 1374 } 1375 1376 public void test_connectLjava_net_SocketAddressI() throws Exception { 1377 1378 // needed for some tests 1379 class mySocketAddress extends SocketAddress { 1380 1381 public mySocketAddress() { 1382 } 1383 } 1384 1385 class SocketCloser extends Thread { 1386 1387 int timeout = 0; 1388 1389 Socket theSocket = null; 1390 1391 public void run() { 1392 try { 1393 Thread.sleep(timeout); 1394 theSocket.close(); 1395 } catch (Exception e) { 1396 } 1397 return; 1398 } 1399 1400 public SocketCloser(int timeout, Socket theSocket) { 1401 this.timeout = timeout; 1402 this.theSocket = theSocket; 1403 } 1404 } 1405 1406 class SocketConnector extends Thread { 1407 1408 int timeout = 0; 1409 1410 Socket theSocket = null; 1411 1412 SocketAddress address = null; 1413 1414 public void run() { 1415 try { 1416 theSocket.connect(address, timeout); 1417 } catch (Exception e) { 1418 } 1419 1420 return; 1421 } 1422 1423 public SocketConnector(int timeout, Socket theSocket, 1424 SocketAddress address) { 1425 this.timeout = timeout; 1426 this.theSocket = theSocket; 1427 this.address = address; 1428 } 1429 } 1430 1431 // start by validating the error checks 1432 int portNumber = Support_PortManager.getNextPort(); 1433 Socket theSocket = null; 1434 ServerSocket serverSocket = null; 1435 SocketAddress theAddress = null; 1436 SocketAddress nonConnectableAddress = null; 1437 SocketAddress nonReachableAddress = null; 1438 SocketAddress nonListeningAddress = null; 1439 SocketAddress invalidType = null; 1440 byte[] theBytes = { 0, 0, 0, 0 }; 1441 1442 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1443 portNumber); 1444 nonConnectableAddress = new InetSocketAddress(InetAddress 1445 .getByAddress(theBytes), portNumber); 1446 nonReachableAddress = new InetSocketAddress(InetAddress 1447 .getByName(Support_Configuration.ResolvedNotExistingHost), 1448 portNumber); 1449 // make sure we get another port 1450 Thread.sleep(7000); 1451 nonListeningAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1452 Support_PortManager.getNextPort()); 1453 invalidType = new mySocketAddress(); 1454 1455 try { 1456 theSocket = new Socket(); 1457 theSocket.connect(theAddress, -100); 1458 fail("No exception after negative timeout passed in"); 1459 } catch (Exception e) { 1460 assertTrue("Wrong exception when negative timeout passed in: " 1461 + e.toString(), (e instanceof IllegalArgumentException)); 1462 } 1463 1464 try { 1465 theSocket = new Socket(); 1466 theSocket.connect(null, 0); 1467 fail("No exception after null address passed in"); 1468 } catch (Exception e) { 1469 assertTrue("Wrong exception null address passed in: " 1470 + e.toString(), (e instanceof IllegalArgumentException)); 1471 } 1472 1473 try { 1474 theSocket = new Socket(); 1475 theSocket.connect(invalidType, 100000); 1476 fail("No exception when invalid socket address type passed in: "); 1477 } catch (Exception e) { 1478 assertTrue( 1479 "Wrong exception when when invalid socket address type passed in: " 1480 + e.toString(), 1481 (e instanceof IllegalArgumentException)); 1482 } 1483 1484 try { 1485 theSocket = new Socket(); 1486 theSocket.connect(nonConnectableAddress, 100000); 1487 fail("No exception when non Connectable Address passed in: "); 1488 } catch (Exception e) { 1489 assertTrue( 1490 "Wrong exception when non Connectable Address passed in: " 1491 + e.toString(), (e instanceof SocketException)); 1492 } 1493 1494 // now validate that we get a connect exception if we try to connect to 1495 // an address on which nobody is listening 1496 try { 1497 theSocket = new Socket(); 1498 theSocket.connect(theAddress, 0); 1499 theSocket.close(); 1500 fail("No timeout:No exception when connecting to address nobody listening on: "); 1501 } catch (Exception e) { 1502 assertTrue( 1503 "No timeout:Wrong exception when connecting to address nobody listening on: " 1504 + e.toString(), (e instanceof ConnectException)); 1505 } 1506 1507 // now validate that we can actually connect when somebody is listening 1508 theSocket = new Socket(); 1509 serverSocket = new ServerSocket(); 1510 serverSocket.bind(theAddress); 1511 theSocket.connect(theAddress, 0); 1512 theSocket.close(); 1513 serverSocket.close(); 1514 1515 // now validate that we get a connect exception if we try to connect to 1516 // an address on which nobody is listening 1517 try { 1518 theSocket = new Socket(); 1519 theSocket.connect(nonListeningAddress, 100000); 1520 theSocket.close(); 1521 fail("No exception when connecting to address nobody listening on: "); 1522 } catch (Exception e) { 1523 assertTrue( 1524 "Wrong exception when connecting to address nobody listening on: " 1525 + e.toString(), (e instanceof ConnectException)); 1526 } 1527 1528 // now validate that we get a interrupted exception if we try to connect 1529 // to an address on which nobody is accepting connections and the 1530 // timeout expired 1531 try { 1532 theSocket = new Socket(); 1533 theSocket.connect(nonReachableAddress, 200); 1534 theSocket.close(); 1535 fail("No interrupted exception when connecting to address nobody listening on with short timeout 200: "); 1536 } catch (Exception e) { 1537 assertTrue( 1538 "Wrong exception when connecting to address nobody listening on with short timeout 200: " 1539 + e.toString(), 1540 (e instanceof SocketTimeoutException)); 1541 } 1542 1543 // now validate that we get a interrupted exception if we try to connect 1544 // to an address on which nobody is accepting connections and the 1545 // timeout expired 1546 try { 1547 theSocket = new Socket(); 1548 theSocket.connect(nonReachableAddress, 40); 1549 theSocket.close(); 1550 fail("No interrupted exception when connecting to address nobody listening on with short timeout 40: "); 1551 } catch (Exception e) { 1552 assertTrue( 1553 "Wrong exception when connecting to address nobody listening on with short timeout 40: " 1554 + e.toString(), 1555 (e instanceof SocketTimeoutException)); 1556 } 1557 1558 // now validate that we can actually connect when somebody is listening 1559 new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager 1560 .getNextPort()); 1561 theSocket = new Socket(); 1562 serverSocket = new ServerSocket(); 1563 serverSocket.bind(theAddress); 1564 theSocket.connect(theAddress, 100000); 1565 1566 // validate that when a socket is connected that it answers 1567 // correctly to related queries 1568 assertTrue("Socket did not returned connected when it is: ", theSocket 1569 .isConnected()); 1570 assertFalse("Socket returned closed when it should be connected ", 1571 theSocket.isClosed()); 1572 assertTrue("Socket returned not bound when it should be: ", theSocket 1573 .isBound()); 1574 assertFalse( 1575 "Socket returned input Shutdown when it should be connected ", 1576 theSocket.isInputShutdown()); 1577 assertFalse( 1578 "Socket returned output Shutdown when it should be connected ", 1579 theSocket.isOutputShutdown()); 1580 assertTrue("Local port on connected socket was 0", theSocket 1581 .getLocalPort() != 0); 1582 theSocket.close(); 1583 serverSocket.close(); 1584 1585 // now validate that we get the right exception if we connect when we 1586 // are already connected 1587 try { 1588 new InetSocketAddress(InetAddress.getLocalHost(), 1589 Support_PortManager.getNextPort()); 1590 theSocket = new Socket(); 1591 serverSocket = new ServerSocket(); 1592 serverSocket.bind(theAddress); 1593 theSocket.connect(theAddress, 100000); 1594 theSocket.connect(theAddress, 100000); 1595 theSocket.close(); 1596 serverSocket.close(); 1597 fail("No exception when we try to connect on a connected socket: "); 1598 1599 } catch (Exception e) { 1600 assertTrue( 1601 "Wrong exception when connecting on socket that is already connected" 1602 + e.toString(), (e instanceof SocketException)); 1603 assertFalse( 1604 "Wrong exception when connecting on socket that is already connected" 1605 + e.toString(), 1606 (e instanceof SocketTimeoutException)); 1607 try { 1608 theSocket.close(); 1609 serverSocket.close(); 1610 } catch (Exception e2) { 1611 } 1612 1613 } 1614 1615 // now validate that connected socket can be used to read/write 1616 new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager 1617 .getNextPort()); 1618 theSocket = new Socket(); 1619 serverSocket = new ServerSocket(); 1620 serverSocket.bind(theAddress); 1621 theSocket.connect(theAddress, 100000); 1622 Socket servSock = serverSocket.accept(); 1623 InputStream theInput = theSocket.getInputStream(); 1624 OutputStream theOutput = servSock.getOutputStream(); 1625 InputStream theInput2 = servSock.getInputStream(); 1626 OutputStream theOutput2 = theSocket.getOutputStream(); 1627 1628 String sendString = new String("Test"); 1629 theOutput.write(sendString.getBytes()); 1630 theOutput.flush(); 1631 1632 Thread.sleep(1000); 1633 1634 int totalBytesRead = 0; 1635 byte[] myBytes = new byte[100]; 1636 while (theInput.available() > 0) { 1637 int bytesRead = theInput.read(myBytes, totalBytesRead, 1638 myBytes.length - totalBytesRead); 1639 totalBytesRead = totalBytesRead + bytesRead; 1640 } 1641 1642 String receivedString = new String(myBytes, 0, totalBytesRead); 1643 assertTrue("Could not recv on socket connected with timeout:" 1644 + receivedString + ":" + sendString, receivedString 1645 .equals(sendString)); 1646 1647 sendString = new String("SEND - Test"); 1648 theOutput2.write(sendString.getBytes()); 1649 theOutput2.flush(); 1650 1651 totalBytesRead = 0; 1652 myBytes = new byte[100]; 1653 Thread.sleep(1000); 1654 while (theInput2.available() > 0) { 1655 int bytesRead = theInput2.read(myBytes, totalBytesRead, 1656 myBytes.length - totalBytesRead); 1657 totalBytesRead = totalBytesRead + bytesRead; 1658 } 1659 1660 receivedString = new String(myBytes, 0, totalBytesRead); 1661 assertTrue("Could not send on socket connected with timeout:" 1662 + receivedString + ":" + sendString, receivedString 1663 .equals(sendString)); 1664 1665 theSocket.close(); 1666 serverSocket.close(); 1667 1668 // now try to set options while we are connecting 1669 theSocket = new Socket(); 1670 SocketConnector connector = new SocketConnector(5000, theSocket, 1671 nonReachableAddress); 1672 connector.start(); 1673 theSocket.setSoTimeout(100); 1674 Thread.sleep(10); 1675 assertEquals("Socket option not set during connect: 10 ", 100, 1676 theSocket.getSoTimeout()); 1677 Thread.sleep(50); 1678 theSocket.setSoTimeout(200); 1679 assertEquals("Socket option not set during connect: 50 ", 200, 1680 theSocket.getSoTimeout()); 1681 Thread.sleep(5000); 1682 theSocket.close(); 1683 1684 SocketChannel channel = SocketChannel.open(); 1685 channel.configureBlocking(false); 1686 Socket socket = channel.socket(); 1687 int port = Support_PortManager.getNextPort(); 1688 try { 1689 socket.connect( new InetSocketAddress(InetAddress.getLocalHost(), 1690 Support_PortManager.getNextPort()), port); 1691 fail("IllegalBlockingModeException was not thrown."); 1692 } catch(IllegalBlockingModeException ibme) { 1693 //expected 1694 } 1695 } 1696 1697 public void test_isInputShutdown() throws IOException { 1698 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1699 .getLocalHost(), Support_PortManager.getNextPort()); 1700 Socket theSocket = new Socket(); 1701 ServerSocket serverSocket = new ServerSocket(); 1702 serverSocket.bind(theAddress); 1703 theSocket.connect(theAddress); 1704 Socket servSock = serverSocket.accept(); 1705 InputStream theInput = theSocket.getInputStream(); 1706 OutputStream theOutput = servSock.getOutputStream(); 1707 1708 // make sure we get the right answer with newly connected socket 1709 assertFalse("Socket indicated input shutdown when it should not have", 1710 theSocket.isInputShutdown()); 1711 1712 // shutdown the output 1713 theSocket.shutdownInput(); 1714 1715 // make sure we get the right answer once it is shut down 1716 assertTrue( 1717 "Socket indicated input was NOT shutdown when it should have been", 1718 theSocket.isInputShutdown()); 1719 1720 theSocket.close(); 1721 serverSocket.close(); 1722 1723 // make sure we get the right answer for closed sockets 1724 assertFalse( 1725 "Socket indicated input was shutdown when socket was closed", 1726 servSock.isInputShutdown()); 1727 1728 } 1729 1730 public void test_isOutputShutdown() throws IOException { 1731 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1732 .getLocalHost(), Support_PortManager.getNextPort()); 1733 Socket theSocket = new Socket(); 1734 ServerSocket serverSocket = new ServerSocket(); 1735 serverSocket.bind(theAddress); 1736 theSocket.connect(theAddress); 1737 Socket servSock = serverSocket.accept(); 1738 InputStream theInput = theSocket.getInputStream(); 1739 OutputStream theOutput = servSock.getOutputStream(); 1740 1741 // make sure we get the right answer with newly connected socket 1742 assertFalse("Socket indicated output shutdown when it should not have", 1743 servSock.isOutputShutdown()); 1744 1745 // shutdown the output 1746 servSock.shutdownOutput(); 1747 1748 // make sure we get the right answer once it is shut down 1749 assertTrue( 1750 "Socket indicated output was NOT shutdown when it should have been", 1751 servSock.isOutputShutdown()); 1752 1753 theSocket.close(); 1754 serverSocket.close(); 1755 1756 // make sure we get the right answer for closed sockets 1757 assertFalse( 1758 "Socket indicated output was output shutdown when the socket was closed", 1759 theSocket.isOutputShutdown()); 1760 1761 } 1762 1763 public void test_setReuseAddressZ() { 1764 1765 try { 1766 InetAddress allAddresses[] = InetAddress.getAllByName(InetAddress 1767 .getLocalHost().getHostName()); 1768 if (allAddresses.length > 1) { 1769 1770 InetSocketAddress theAddress = new InetSocketAddress( 1771 InetAddress.getLocalHost(), Support_PortManager 1772 .getNextPort()); 1773 ServerSocket serverSocket = new ServerSocket(); 1774 serverSocket.bind(theAddress); 1775 1776 // try to bind to port address that is already in use with 1777 // reuseAddress = false. 1778 // On windows platforms the bind is allowed even then 1779 // reUseAddress is false (ONLY IF BOTH SOCKETS 1780 // ARE IPV4 Sockets) so our test uses the platform to determine 1781 // what the expected result is. It seems that on linux 1782 // platforms we also don't get an exception. 1783 InetSocketAddress theLocalAddress = new InetSocketAddress( 1784 (InetAddress) allAddresses[1], Support_PortManager 1785 .getNextPort()); 1786 InetSocketAddress theOtherLocalAddress = new InetSocketAddress( 1787 (InetAddress) allAddresses[0], theLocalAddress 1788 .getPort()); 1789 Socket theSocket = new Socket(); 1790 theSocket.setReuseAddress(false); 1791 theSocket.bind(theLocalAddress); 1792 Socket theSocket2 = null; 1793 String platform = System.getProperty("os.name"); 1794 try { 1795 theSocket2 = new Socket(); 1796 theSocket2.setReuseAddress(false); 1797 theSocket2.bind(theOtherLocalAddress); 1798 1799 if ((!platform.startsWith("Linux")) 1800 && ((!platform.startsWith("Windows")) || 1801 // for windows we don't get an exception with 1802 // setreuse set to false unless one of the 1803 // addresses we bind to is an IPv6 address and we 1804 // are therefore using the IPv6 stack. 1805 !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 1806 fail("No exception when setReuseAddress is false and we bind:" 1807 + theLocalAddress.toString() 1808 + ":" 1809 + theOtherLocalAddress.toString()); 1810 } 1811 } catch (IOException ex) { 1812 if ((platform.startsWith("Linux")) 1813 || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 1814 fail("Got unexpected exception when binding with setReuseAddress false on windows platform:" 1815 + theAddress.toString() + ":" + ex.toString()); 1816 } 1817 } 1818 theSocket.close(); 1819 theSocket2.close(); 1820 1821 // try to bind to port that is already in use with reuseAddress 1822 // = true 1823 theLocalAddress = new InetSocketAddress( 1824 (InetAddress) allAddresses[0], Support_PortManager 1825 .getNextPort()); 1826 theOtherLocalAddress = new InetSocketAddress( 1827 (InetAddress) allAddresses[1], theLocalAddress 1828 .getPort()); 1829 1830 theSocket = new Socket(); 1831 theSocket.setReuseAddress(true); 1832 theSocket.bind(theLocalAddress); 1833 try { 1834 theSocket2 = new Socket(); 1835 theSocket2.setReuseAddress(true); 1836 theSocket2.bind(theOtherLocalAddress); 1837 theSocket2.close(); 1838 } catch (IOException ex) { 1839 fail("IOException when setReuseAddress is true and we bind :" 1840 + ex.toString()); 1841 } 1842 theSocket.close(); 1843 serverSocket.close(); 1844 1845 // try with default behavior which should be the same on all 1846 // platforms 1847 theLocalAddress = new InetSocketAddress( 1848 (InetAddress) allAddresses[0], Support_PortManager 1849 .getNextPort()); 1850 theOtherLocalAddress = new InetSocketAddress( 1851 (InetAddress) allAddresses[1], theLocalAddress 1852 .getPort()); 1853 1854 theSocket = new Socket(); 1855 theSocket.bind(theLocalAddress); 1856 try { 1857 theSocket2 = new Socket(); 1858 theSocket2.bind(theOtherLocalAddress); 1859 theSocket2.close(); 1860 if ((!platform.startsWith("Linux")) 1861 && ((!platform.startsWith("Windows")) || !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 1862 fail("No exception when setReuseAddress is default and we bind:" 1863 + theLocalAddress.toString() 1864 + ":" 1865 + theOtherLocalAddress.toString()); 1866 } 1867 } catch (IOException ex) { 1868 if ((platform.startsWith("Linux")) 1869 || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) { 1870 fail("Got unexpected exception when binding with setReuseAddress default on windows platform:" 1871 + theAddress.toString() + ":" + ex.toString()); 1872 } 1873 } 1874 theSocket.close(); 1875 serverSocket.close(); 1876 1877 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 1878 } 1879 } catch (Exception e) { 1880 handleException(e, SO_REUSEADDR); 1881 } 1882 1883 try { 1884 Socket theSocket = new Socket(); 1885 theSocket.close(); 1886 theSocket.setReuseAddress(true); 1887 fail("SocketException was not thrown."); 1888 } catch(SocketException ioe) { 1889 //expected 1890 } catch(IOException ioe) { 1891 fail("IOException was thrown."); 1892 } 1893 } 1894 1895 public void test_getReuseAddress() { 1896 try { 1897 Socket theSocket = new Socket(); 1898 theSocket.setReuseAddress(true); 1899 assertTrue("getReuseAddress false when it should be true", 1900 theSocket.getReuseAddress()); 1901 theSocket.setReuseAddress(false); 1902 assertFalse("getReuseAddress true when it should be False", 1903 theSocket.getReuseAddress()); 1904 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR); 1905 } catch (Exception e) { 1906 handleException(e, SO_REUSEADDR); 1907 } 1908 1909 try { 1910 Socket newSocket = new Socket(); 1911 newSocket.close(); 1912 try { 1913 newSocket.getReuseAddress(); 1914 fail("SocketException was not thrown."); 1915 } catch(SocketException e) { 1916 //expected 1917 } 1918 } catch(Exception e) { 1919 fail("Unexpected exception."); 1920 } 1921 } 1922 1923 public void test_setOOBInlineZ() { 1924 // mostly tested in getOOBInline. Just set to make sure call works ok 1925 try { 1926 new InetSocketAddress(InetAddress.getLocalHost(), 1927 Support_PortManager.getNextPort()); 1928 Socket theSocket = new Socket(); 1929 theSocket.setOOBInline(true); 1930 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 1931 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE); 1932 } catch (Exception e) { 1933 handleException(e, SO_OOBINLINE); 1934 } 1935 1936 try { 1937 Socket theSocket = new Socket(); 1938 theSocket.close(); 1939 theSocket.setOOBInline(true); 1940 fail("SocketException was not thrown."); 1941 } catch(SocketException ioe) { 1942 //expected 1943 } catch(IOException ioe) { 1944 fail("IOException was thrown."); 1945 } 1946 } 1947 1948 public void test_getOOBInline() { 1949 1950 try { 1951 new InetSocketAddress(InetAddress.getLocalHost(), 1952 Support_PortManager.getNextPort()); 1953 Socket theSocket = new Socket(); 1954 1955 // validate that value reflects what we set it to true after true, 1956 // false after false and false after false false 1957 theSocket.setOOBInline(true); 1958 assertTrue("expected OOBIline to be true", theSocket.getOOBInline()); 1959 theSocket.setOOBInline(false); 1960 assertFalse("expected OOBIline to be true", theSocket 1961 .getOOBInline()); 1962 theSocket.setOOBInline(false); 1963 assertFalse("expected OOBIline to be true", theSocket 1964 .getOOBInline()); 1965 theSocket.close(); 1966 try { 1967 theSocket.getOOBInline(); 1968 fail("SocketException was not thrown."); 1969 } catch(SocketException se) { 1970 //expected 1971 } 1972 ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE); 1973 1974 } catch (Exception e) { 1975 handleException(e, SO_OOBINLINE); 1976 } 1977 } 1978 1979 public void test_setTrafficClassI() { 1980 try { 1981 int IPTOS_LOWCOST = 0x2; 1982 int IPTOS_THROUGHPUT = 0x8; 1983 1984 new InetSocketAddress(InetAddress.getLocalHost(), 1985 Support_PortManager.getNextPort()); 1986 Socket theSocket = new Socket(); 1987 1988 // validate that value set must be between 0 and 255 1989 try { 1990 theSocket.setTrafficClass(256); 1991 fail("No exception was thrown when traffic class set to 256"); 1992 } catch (IllegalArgumentException e) { 1993 } 1994 1995 try { 1996 theSocket.setTrafficClass(-1); 1997 fail("No exception was thrown when traffic class set to -1"); 1998 } catch (IllegalArgumentException e) { 1999 } 2000 2001 // now validate that we can set it to some good values 2002 theSocket.setTrafficClass(IPTOS_LOWCOST); 2003 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 2004 ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS); 2005 } catch (Exception e) { 2006 handleException(e, IP_TOS); 2007 } 2008 2009 try { 2010 Socket theSocket = new Socket(); 2011 theSocket.close(); 2012 theSocket.setTrafficClass(0); 2013 fail("SocketException was not thrown."); 2014 } catch(SocketException ioe) { 2015 //expected 2016 } catch(IOException ioe) { 2017 fail("IOException was thrown."); 2018 } 2019 } 2020 2021 public void test_getTrafficClass() { 2022 try { 2023 new InetSocketAddress(InetAddress.getLocalHost(), 2024 Support_PortManager.getNextPort()); 2025 Socket theSocket = new Socket(); 2026 2027 /* 2028 * we cannot actually check that the values are set as if a platform 2029 * does not support the option then it may come back unset even 2030 * though we set it so just get the value to make sure we can get it 2031 */ 2032 int trafficClass = theSocket.getTrafficClass(); 2033 ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS); 2034 } catch (Exception e) { 2035 handleException(e, IP_TOS); 2036 } 2037 } 2038 2039 public void test_getChannel() throws Exception { 2040 assertNull(new Socket().getChannel()); 2041 2042 SocketChannel channel = SocketChannel.open(); 2043 Socket socket = channel.socket(); 2044 assertEquals(channel, socket.getChannel()); 2045 socket.close(); 2046 channel.close(); 2047 } 2048 2049 public void test_sendUrgentDataI() { 2050 2051 // Some platforms may not support urgent data in this case we will not 2052 // run these tests. For now run on all platforms until we find those 2053 // that do not support urgent data 2054 String platform = System.getProperty("os.name"); 2055 if (!platform.equals("Dummy")) { 2056 // validate that when OOBInline is false that any urgent data 2057 // is silently ignored 2058 String urgentData = "U"; 2059 try { 2060 InetSocketAddress theAddress = new InetSocketAddress( 2061 InetAddress.getLocalHost(), Support_PortManager 2062 .getNextPort()); 2063 Socket theSocket = new Socket(); 2064 ServerSocket serverSocket = new ServerSocket(); 2065 serverSocket.bind(theAddress); 2066 theSocket.connect(theAddress); 2067 Socket servSock = serverSocket.accept(); 2068 InputStream theInput = theSocket.getInputStream(); 2069 OutputStream theOutput = servSock.getOutputStream(); 2070 2071 // send the regular data 2072 String sendString = new String("Test"); 2073 theOutput.write(sendString.getBytes()); 2074 theOutput.flush(); 2075 2076 // send the urgent data which should not be received 2077 theSocket.setOOBInline(false); 2078 servSock.sendUrgentData(urgentData.getBytes()[0]); 2079 theOutput.write(sendString.getBytes()); 2080 theOutput.flush(); 2081 2082 // give things some time to settle 2083 Thread.sleep(1000); 2084 2085 int totalBytesRead = 0; 2086 byte[] myBytes = new byte[100]; 2087 while (theInput.available() > 0) { 2088 int bytesRead = theInput.read(myBytes, totalBytesRead, 2089 myBytes.length - totalBytesRead); 2090 totalBytesRead = totalBytesRead + bytesRead; 2091 } 2092 2093 String receivedString = new String(myBytes, 0, totalBytesRead); 2094 //assertTrue("Urgent Data seems to have been received:" 2095 // + receivedString + ":" + sendString, receivedString 2096 // .equals(sendString + sendString)); 2097 2098 theSocket.close(); 2099 serverSocket.close(); 2100 2101 // now validate that urgent data is received as expected. Expect 2102 // that it should be between the two writes. 2103 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2104 Support_PortManager.getNextPort()); 2105 theSocket = new Socket(); 2106 serverSocket = new ServerSocket(); 2107 serverSocket.bind(theAddress); 2108 theSocket.connect(theAddress); 2109 servSock = serverSocket.accept(); 2110 theInput = theSocket.getInputStream(); 2111 theOutput = servSock.getOutputStream(); 2112 2113 // send the regular data 2114 sendString = new String("Test - Urgent Data"); 2115 theOutput.write(sendString.getBytes()); 2116 theOutput.flush(); 2117 2118 // send the urgent data which should be received 2119 theSocket.setOOBInline(true); 2120 servSock.sendUrgentData(urgentData.getBytes()[0]); 2121 2122 theOutput.write(sendString.getBytes()); 2123 theOutput.flush(); 2124 2125 Thread.sleep(1000); 2126 2127 totalBytesRead = 0; 2128 myBytes = new byte[100]; 2129 while (theInput.available() > 0) { 2130 int bytesRead = theInput.read(myBytes, totalBytesRead, 2131 myBytes.length - totalBytesRead); 2132 totalBytesRead = totalBytesRead + bytesRead; 2133 } 2134 2135 receivedString = new String(myBytes, 0, totalBytesRead); 2136 assertTrue("Urgent Data was not received with one urgent byte:" 2137 + receivedString + ":" + sendString + urgentData 2138 + sendString, receivedString.equals(sendString 2139 + urgentData + sendString)); 2140 2141 theSocket.close(); 2142 serverSocket.close(); 2143 2144 // now test case where we try to send two urgent bytes. 2145 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2146 Support_PortManager.getNextPort()); 2147 theSocket = new Socket(); 2148 serverSocket = new ServerSocket(); 2149 serverSocket.bind(theAddress); 2150 theSocket.connect(theAddress); 2151 servSock = serverSocket.accept(); 2152 theInput = theSocket.getInputStream(); 2153 theOutput = servSock.getOutputStream(); 2154 2155 // send the regular data 2156 sendString = new String("Test - Urgent Data"); 2157 theOutput.write(sendString.getBytes()); 2158 theOutput.flush(); 2159 2160 // send the urgent data which should not be received 2161 theSocket.setOOBInline(true); 2162 servSock.sendUrgentData(urgentData.getBytes()[0]); 2163 servSock.sendUrgentData(urgentData.getBytes()[0]); 2164 2165 theOutput.write(sendString.getBytes()); 2166 theOutput.flush(); 2167 2168 Thread.sleep(1000); 2169 2170 totalBytesRead = 0; 2171 myBytes = new byte[100]; 2172 while (theInput.available() > 0) { 2173 int bytesRead = theInput.read(myBytes, totalBytesRead, 2174 myBytes.length - totalBytesRead); 2175 totalBytesRead = totalBytesRead + bytesRead; 2176 } 2177 2178 receivedString = new String(myBytes, 0, totalBytesRead); 2179 assertTrue( 2180 "Did not get right byte of urgent data when two sent:" 2181 + receivedString + ":" + sendString 2182 + urgentData + urgentData + sendString, 2183 receivedString.equals(sendString + urgentData 2184 + urgentData + sendString)); 2185 2186 theSocket.close(); 2187 serverSocket.close(); 2188 2189 /* 2190 * TODO : These do not currently pass on XP SP2 and Server 2003 2191 */ 2192 if (!platform.startsWith("Windows")) { 2193 // now test the case were we send turn the OOBInline on/off 2194 theAddress = new InetSocketAddress(InetAddress 2195 .getLocalHost(), Support_PortManager.getNextPort()); 2196 theSocket = new Socket(); 2197 serverSocket = new ServerSocket(); 2198 serverSocket.bind(theAddress); 2199 theSocket.connect(theAddress); 2200 servSock = serverSocket.accept(); 2201 theInput = theSocket.getInputStream(); 2202 theOutput = servSock.getOutputStream(); 2203 2204 // send the regular data 2205 sendString = new String("Test - Urgent Data"); 2206 theOutput.write(sendString.getBytes()); 2207 theOutput.flush(); 2208 2209 // send the urgent data which should be received 2210 theSocket.setOOBInline(true); 2211 servSock.sendUrgentData(urgentData.getBytes()[0]); 2212 2213 theOutput.write(sendString.getBytes()); 2214 theOutput.flush(); 2215 2216 Thread.sleep(1000); 2217 2218 totalBytesRead = 0; 2219 myBytes = new byte[100]; 2220 while (theInput.available() > 0) { 2221 int bytesRead = theInput.read(myBytes, totalBytesRead, 2222 myBytes.length - totalBytesRead); 2223 totalBytesRead = totalBytesRead + bytesRead; 2224 } 2225 2226 receivedString = new String(myBytes, 0, totalBytesRead); 2227 assertTrue( 2228 "Did not get urgent data when turning on/off(1):" 2229 + receivedString + ":" + sendString 2230 + urgentData + sendString, receivedString 2231 .equals(sendString + urgentData 2232 + sendString)); 2233 2234 // send the regular data 2235 sendString = new String("Test - Urgent Data"); 2236 theOutput.write(sendString.getBytes()); 2237 theOutput.flush(); 2238 2239 // send the urgent data which should not be received 2240 theSocket.setOOBInline(false); 2241 servSock.sendUrgentData(urgentData.getBytes()[0]); 2242 2243 // send trailing data 2244 theOutput.write(sendString.getBytes()); 2245 theOutput.flush(); 2246 2247 Thread.sleep(1000); 2248 2249 totalBytesRead = 0; 2250 myBytes = new byte[100]; 2251 while (theInput.available() > 0) { 2252 int bytesRead = theInput.read(myBytes, totalBytesRead, 2253 myBytes.length - totalBytesRead); 2254 totalBytesRead = totalBytesRead + bytesRead; 2255 } 2256 2257 receivedString = new String(myBytes, 0, totalBytesRead); 2258 //assertTrue( 2259 // "Got unexpected data data when turning on/off(2):" 2260 // + receivedString + ":" + sendString 2261 // + sendString, receivedString 2262 // .equals(sendString + sendString)); 2263 2264 // now turn back on and get data. Here we also 2265 // get the previously sent byte of urgent data as it is 2266 // still in the urgent buffer 2267 2268 // send the regular data 2269 sendString = new String("Test - Urgent Data"); 2270 theOutput.write(sendString.getBytes()); 2271 theOutput.flush(); 2272 2273 // send the urgent data which should be received again 2274 theSocket.setOOBInline(true); 2275 servSock.sendUrgentData(urgentData.getBytes()[0]); 2276 2277 theOutput.write(sendString.getBytes()); 2278 theOutput.flush(); 2279 2280 Thread.sleep(1000); 2281 2282 totalBytesRead = 0; 2283 myBytes = new byte[100]; 2284 while (theInput.available() > 0) { 2285 int bytesRead = theInput.read(myBytes, totalBytesRead, 2286 myBytes.length - totalBytesRead); 2287 totalBytesRead = totalBytesRead + bytesRead; 2288 } 2289 2290 receivedString = new String(myBytes, 0, totalBytesRead); 2291 // depending on the platform we may get the previously sent 2292 // urgent data or not (examples windows-yes, Linux-no). 2293 // So accept either so long as we get the urgent data from 2294 // when it was on. 2295 //assertTrue( 2296 // "Did not get urgent data when turning on/off(3) GOT:" 2297 // + receivedString + ":Expected" + urgentData 2298 // + sendString + urgentData + sendString 2299 // + ":OR:" + sendString + urgentData 2300 // + sendString, 2301 // (receivedString.equals(urgentData + sendString 2302 // + urgentData + sendString) || receivedString 2303 // .equals(sendString + urgentData 2304 // + sendString))); 2305 2306 theSocket.close(); 2307 serverSocket.close(); 2308 } 2309 2310 // now test the case where there is only urgent data 2311 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 2312 Support_PortManager.getNextPort()); 2313 theSocket = new Socket(); 2314 serverSocket = new ServerSocket(); 2315 serverSocket.bind(theAddress); 2316 theSocket.connect(theAddress); 2317 servSock = serverSocket.accept(); 2318 theInput = theSocket.getInputStream(); 2319 theOutput = servSock.getOutputStream(); 2320 2321 // send the urgent data which should not be received. 2322 theSocket.setOOBInline(true); 2323 servSock.sendUrgentData(urgentData.getBytes()[0]); 2324 2325 Thread.sleep(1000); 2326 2327 totalBytesRead = 0; 2328 myBytes = new byte[100]; 2329 while (theInput.available() > 0) { 2330 int bytesRead = theInput.read(myBytes, totalBytesRead, 2331 myBytes.length - totalBytesRead); 2332 totalBytesRead = totalBytesRead + bytesRead; 2333 } 2334 2335 receivedString = new String(myBytes, 0, totalBytesRead); 2336 assertTrue("Did not get urgent data only urgent data sent:" 2337 + receivedString + ":" + urgentData, receivedString 2338 .equals(urgentData)); 2339 2340 } catch (Exception e) { 2341 // for platforms that do not support urgent data we expect an 2342 // exception. For the others report an error. 2343 // TODO : Need to introduce a better test for the exception 2344 // so that the failure only occurs on platforms that support 2345 // urgent data 2346 fail("Platform:" + platform 2347 + ": Got exception during sendUrgent data tests" 2348 + e.toString()); 2349 } 2350 } 2351 2352 try { 2353 Socket theSocket = new Socket(); 2354 theSocket.close(); 2355 theSocket.sendUrgentData(0); 2356 fail("IOException was not thrown."); 2357 } catch(IOException ioe) { 2358 //expected 2359 } 2360 } 2361 2362 public void test_setPerformancePreference_Int_Int_Int() throws Exception { 2363 Socket theSocket = new Socket(); 2364 theSocket.setPerformancePreferences(1, 1, 1); 2365 } 2366 2367 public void test_ConstructorLjava_net_Proxy_Exception() { 2368 2369 SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1", 2370 80); 2371 SocketAddress addr2 = new InetSocketAddress("localhost", 80); 2372 2373 Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1); 2374 // IllegalArgumentException test 2375 try { 2376 new Socket(proxy1); 2377 fail("should throw IllegalArgumentException"); 2378 } catch (IllegalArgumentException e) { 2379 // expected 2380 } 2381 2382 Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1); 2383 // should not throw any exception 2384 new Socket(proxy2); 2385 new Socket(Proxy.NO_PROXY); 2386 2387 try { 2388 new Socket((Proxy) null); 2389 fail("IllegalArgumentException was not thrown."); 2390 } catch(IllegalArgumentException iae) { 2391 //expected 2392 } 2393 } 2394 2395 public void test_ConstructorLSocketImpl() { 2396 MockSocketImpl msi = new MockSocketImpl(); 2397 try { 2398 new TestSocket(msi); 2399 } catch (SocketException e) { 2400 fail("SocketException was thrown."); 2401 } 2402 } 2403 2404 public void test_connect_unknownhost() throws Exception { 2405 Socket socket = new Socket(); 2406 InetSocketAddress socketAddress = new InetSocketAddress("unknownhost", 12345); 2407 try { 2408 socket.connect(socketAddress); 2409 fail("Should throw IOException"); 2410 } catch (IOException e) { 2411 // expected 2412 } 2413 } 2414 2415 public void test_connect_unresolved_unknown() throws Exception { 2416 Socket socket = new Socket(); 2417 InetSocketAddress unresolved = InetSocketAddress.createUnresolved("unknownhost", 12345); 2418 try { 2419 socket.connect(unresolved); 2420 fail("Should throw IOException"); 2421 } catch (IOException e) { 2422 // expected 2423 } 2424 } 2425 2426 public void test_connect_unresolved() throws Exception { 2427 Socket socket = new Socket(); 2428 InetSocketAddress unresolvedSocketAddress = InetSocketAddress.createUnresolved( 2429 Support_Configuration.SocksServerTestHost, 2430 Support_Configuration.SocksServerTestPort); 2431 try { 2432 socket.connect(unresolvedSocketAddress); 2433 fail("Should throw IOException"); 2434 } catch (IOException e) { 2435 // expected 2436 } 2437 } 2438 2439 public void test_getOutputStream_shutdownOutput() throws Exception { 2440 // regression test for Harmony-873 2441 ServerSocket ss = new ServerSocket(0); 2442 Socket s = new Socket("127.0.0.1", ss.getLocalPort()); 2443 ss.accept(); 2444 s.shutdownOutput(); 2445 try { 2446 s.getOutputStream(); 2447 fail("should throw SocketException"); 2448 } catch (IOException e) { 2449 // expected 2450 } finally { 2451 s.close(); 2452 } 2453 2454 SocketChannel channel = SocketChannel.open( 2455 new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort())); 2456 channel.configureBlocking(false); 2457 ss.accept(); 2458 Socket socket = channel.socket(); 2459 2460 OutputStream out = null; 2461 2462 try { 2463 out = socket.getOutputStream(); 2464 out.write(1); 2465 fail("IllegalBlockingModeException was not thrown."); 2466 } catch(IllegalBlockingModeException ibme) { 2467 //expected 2468 } finally { 2469 if(out != null) out.close(); 2470 socket.close(); 2471 channel.close(); 2472 } 2473 } 2474 2475 public void test_shutdownInputOutput_twice() throws Exception { 2476 // regression test for Harmony-2944 2477 Socket s = new Socket("0.0.0.0", 0, false); 2478 s.shutdownInput(); 2479 2480 try { 2481 s.shutdownInput(); 2482 fail("should throw SocketException"); 2483 } catch (SocketException se) { 2484 // expected 2485 } 2486 s.shutdownOutput(); 2487 2488 try { 2489 s.shutdownOutput(); 2490 fail("should throw SocketException"); 2491 } catch (SocketException se) { 2492 // expected 2493 } 2494 } 2495 2496 /** 2497 * Sets up the fixture, for example, open a network connection. This method 2498 * is called before a test is executed. 2499 * 2500 * @throws Exception 2501 */ 2502 protected void setUp() throws Exception { 2503 super.setUp(); 2504 } 2505 2506 /** 2507 * Tears down the fixture, for example, close a network connection. This 2508 * method is called after a test is executed. 2509 */ 2510 protected void tearDown() { 2511 try { 2512 if (s != null) 2513 s.close(); 2514 } catch (Exception e) { 2515 } 2516 try { 2517 if (ss != null) 2518 ss.close(); 2519 } catch (Exception e) { 2520 } 2521 try { 2522 if (t != null) 2523 t.interrupt(); 2524 } catch (Exception e) { 2525 } 2526 } 2527 2528 static class MockSecurityManager extends SecurityManager { 2529 2530 public void checkConnect(String host, int port) { 2531 if ("127.0.0.1".equals(host)) { 2532 throw new SecurityException("permission is not allowed"); 2533 } 2534 } 2535 2536 public void checkPermission(Permission permission) { 2537 return; 2538 } 2539 2540 } 2541 2542 /** 2543 * 2544 */ 2545 protected int startServer(String name) { 2546 int portNumber = Support_PortManager.getNextPort(); 2547 try { 2548 ss = new ServerSocket(portNumber, 5); 2549 } catch (IOException e) { 2550 fail(name + ": " + e); 2551 } 2552 return ss.getLocalPort(); 2553 } 2554 2555 class MockSocketImpl extends SocketImpl { 2556 2557 public MockSocketImpl() { 2558 super(); 2559 } 2560 2561 @Override 2562 protected void accept(SocketImpl arg0) throws IOException { 2563 } 2564 2565 @Override 2566 protected int available() throws IOException { 2567 return 0; 2568 } 2569 2570 @Override 2571 protected void bind(InetAddress arg0, int arg1) throws IOException { 2572 } 2573 2574 @Override 2575 protected void close() throws IOException { 2576 } 2577 2578 @Override 2579 protected void connect(String arg0, int arg1) throws IOException { 2580 } 2581 2582 @Override 2583 protected void connect(InetAddress arg0, int arg1) throws IOException { 2584 } 2585 2586 @Override 2587 protected void connect(SocketAddress arg0, int arg1) throws IOException { 2588 } 2589 2590 @Override 2591 protected void create(boolean arg0) throws IOException { 2592 } 2593 2594 @Override 2595 protected InputStream getInputStream() throws IOException { 2596 return null; 2597 } 2598 2599 @Override 2600 protected OutputStream getOutputStream() throws IOException { 2601 return null; 2602 } 2603 2604 @Override 2605 protected void listen(int arg0) throws IOException { 2606 } 2607 2608 @Override 2609 protected void sendUrgentData(int arg0) throws IOException { 2610 } 2611 2612 public Object getOption(int arg0) throws SocketException { 2613 return null; 2614 } 2615 2616 public void setOption(int arg0, Object arg1) throws SocketException { 2617 } 2618 } 2619} 2620