/luni/src/test/java/libcore/java/net/OldSocketTest.java
Java | 2623 lines | 2020 code | 325 blank | 278 comment | 81 complexity | 3db3c7a930b50371665dfa2c4b464775 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
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 assertTrue("Returned incorrect string: " + s.toString() 751 + " localHost: " + InetAddress.getLocalHost(), s.toString() 752 .equals( 753 "Socket[addr=" + InetAddress.getLocalHost() + ",port=" 754 + s.getPort() + ",localport=" 755 + s.getLocalPort() + "]")); 756 } 757 758 // AndroidOnly: RI returns wrong value for EOF 759 public void test_shutdownInput() throws Exception { 760 InetAddress addr = InetAddress.getLocalHost(); 761 int port = Support_PortManager.getNextPort(); 762 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 763 Socket theSocket = new Socket(addr, port); 764 Socket servSock = serverSocket.accept(); 765 766 InputStream theInput = theSocket.getInputStream(); 767 OutputStream theOutput = servSock.getOutputStream(); 768 769 // shutdown the input 770 theSocket.shutdownInput(); 771 772 // send the regular data 773 String sendString = new String("Test"); 774 theOutput.write(sendString.getBytes()); 775 theOutput.flush(); 776 777 // give things some time to settle 778 Thread.sleep(1000); 779 780 // RI fails here. It is a RI bug not to return 0 to indicate EOF 781 assertEquals(0, theInput.available()); 782 783 theSocket.close(); 784 serverSocket.close(); 785 786 Socket socket = new Socket(); 787 socket.close(); 788 try { 789 socket.shutdownInput(); 790 fail("IOException was not thrown."); 791 } catch(IOException ioe) { 792 //expected 793 } 794 } 795 796 public void test_shutdownOutput() throws IOException { 797 InetAddress addr = InetAddress.getLocalHost(); 798 int port = Support_PortManager.getNextPort(); 799 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 800 Socket theSocket = new Socket(addr, port); 801 Socket servSock = serverSocket.accept(); 802 803 InputStream theInput = theSocket.getInputStream(); 804 OutputStream theOutput = servSock.getOutputStream(); 805 806 // shutdown the output 807 servSock.shutdownOutput(); 808 809 // send the regular data 810 String sendString = new String("Test"); 811 try { 812 theOutput.write(sendString.getBytes()); 813 theOutput.flush(); 814 fail("No exception when writing on socket with output shutdown"); 815 } catch (Exception e) { 816 } 817 818 theSocket.close(); 819 serverSocket.close(); 820 821 try { 822 theSocket.shutdownInput(); 823 fail("IOException was not thrown."); 824 } catch(IOException ioe) { 825 //expected 826 } 827 } 828 829 public void test_getLocalSocketAddress() throws IOException { 830 // set up server connect and then validate that we get the right 831 // response for the local address 832 int sport = startServer("SServer getLocSocketAddress"); 833 int portNumber = Support_PortManager.getNextPort(); 834 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 835 assertTrue( 836 "Returned incorrect InetSocketAddress(1):" 837 + s.getLocalSocketAddress().toString() 838 + "Expected: " 839 + (new InetSocketAddress(InetAddress.getLocalHost(), 840 portNumber)).toString(), s 841 .getLocalSocketAddress().equals( 842 new InetSocketAddress(InetAddress 843 .getLocalHost(), portNumber))); 844 s.close(); 845 846 // now create a socket that is not bound and validate we get the 847 // right answer 848 Socket theSocket = new Socket(); 849 assertNull( 850 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 851 theSocket.getLocalSocketAddress()); 852 853 // now bind the socket and make sure we get the right answer 854 portNumber = Support_PortManager.getNextPort(); 855 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 856 portNumber)); 857 assertTrue( 858 "Returned incorrect InetSocketAddress(2):" 859 + theSocket.getLocalSocketAddress().toString() 860 + "Expected: " 861 + (new InetSocketAddress(InetAddress.getLocalHost(), 862 portNumber)).toString(), theSocket 863 .getLocalSocketAddress().equals( 864 new InetSocketAddress(InetAddress 865 .getLocalHost(), portNumber))); 866 theSocket.close(); 867 868 // now validate that behaviour when the any address is returned 869 s = new Socket(); 870 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 871 872 String preferIPv4StackValue = System 873 .getProperty("java.net.preferIPv4Stack"); 874 String preferIPv6AddressesValue = System 875 .getProperty("java.net.preferIPv6Addresses"); 876 if (((preferIPv4StackValue == null) || preferIPv4StackValue 877 .equalsIgnoreCase("false")) 878 && (preferIPv6AddressesValue != null) 879 && (preferIPv6AddressesValue.equals("true"))) { 880 assertTrue( 881 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " 882 + s.getLocalSocketAddress(), 883 ((InetSocketAddress) s.getLocalSocketAddress()) 884 .getAddress() instanceof Inet6Address); 885 } else { 886 assertTrue( 887 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " 888 + s.getLocalSocketAddress(), 889 ((InetSocketAddress) s.getLocalSocketAddress()) 890 .getAddress() instanceof Inet4Address); 891 } 892 s.close(); 893 894 // now validate the same for getLocalAddress 895 s = new Socket(); 896 s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0)); 897 if (((preferIPv4StackValue == null) || preferIPv4StackValue 898 .equalsIgnoreCase("false")) 899 && (preferIPv6AddressesValue != null) 900 && (preferIPv6AddressesValue.equals("true"))) { 901 assertTrue( 902 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false " 903 + s.getLocalSocketAddress(), 904 ((InetSocketAddress) s.getLocalSocketAddress()) 905 .getAddress() instanceof Inet6Address); 906 } else { 907 assertTrue( 908 "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true " 909 + s.getLocalSocketAddress(), 910 ((InetSocketAddress) s.getLocalSocketAddress()) 911 .getAddress() instanceof Inet4Address); 912 } 913 s.close(); 914 } 915 916 public void test_getRemoteSocketAddress() throws IOException { 917 // set up server connect and then validate that we get the right 918 // response for the remote address 919 int sport = startServer("SServer getLocRemoteAddress"); 920 int portNumber = Support_PortManager.getNextPort(); 921 s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber); 922 assertTrue("Returned incorrect InetSocketAddress(1):" 923 + s.getLocalSocketAddress().toString(), 924 s.getRemoteSocketAddress() 925 .equals( 926 new InetSocketAddress(InetAddress 927 .getLocalHost(), sport))); 928 s.close(); 929 930 // now create one that is not connect and validate that we get the 931 // right answer 932 Socket theSocket = new Socket(); 933 portNumber = Support_PortManager.getNextPort(); 934 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 935 portNumber)); 936 937 assertNull("Returned incorrect InetSocketAddress -unconnected socket:" 938 + "Expected: NULL", theSocket.getRemoteSocketAddress()); 939 940 // now connect and validate we get the right answer 941 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 942 sport)); 943 assertTrue("Returned incorrect InetSocketAddress(2):" 944 + theSocket.getRemoteSocketAddress().toString(), 945 theSocket.getRemoteSocketAddress() 946 .equals( 947 new InetSocketAddress(InetAddress 948 .getLocalHost(), sport))); 949 theSocket.close(); 950 951 } 952 953 public void test_isBound() throws IOException { 954 InetAddress addr = InetAddress.getLocalHost(); 955 int port = Support_PortManager.getNextPort(); 956 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 957 Socket theSocket = new Socket(addr, port); 958 Socket servSock = serverSocket.accept(); 959 assertTrue("Socket indicated not bound when it should be (1)", 960 theSocket.isBound()); 961 theSocket.close(); 962 serverSocket.close(); 963 964 // now do it with the new constructors and revalidate. Connect causes 965 // the socket to be bound 966 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 967 .getLocalHost(), Support_PortManager.getNextPort()); 968 theSocket = new Socket(); 969 assertFalse("Socket indicated bound when it was not (2)", theSocket 970 .isBound()); 971 serverSocket = new ServerSocket(); 972 serverSocket.bind(theAddress); 973 theSocket.connect(theAddress); 974 servSock = serverSocket.accept(); 975 assertTrue("Socket indicated not bound when it should be (2)", 976 theSocket.isBound()); 977 theSocket.close(); 978 serverSocket.close(); 979 980 // now test when we bind explicitly 981 InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress 982 .getLocalHost(), Support_PortManager.getNextPort()); 983 theSocket = new Socket(); 984 assertFalse("Socket indicated bound when it was not (3)", theSocket 985 .isBound()); 986 theSocket.bind(theLocalAddress); 987 assertTrue("Socket indicated not bound when it should be (3a)", 988 theSocket.isBound()); 989 theSocket.close(); 990 assertTrue("Socket indicated not bound when it should be (3b)", 991 theSocket.isBound()); 992 } 993 994 public void test_isConnected() throws IOException { 995 InetAddress addr = InetAddress.getLocalHost(); 996 int port = Support_PortManager.getNextPort(); 997 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 998 Socket theSocket = new Socket(addr, port); 999 Socket servSock = serverSocket.accept(); 1000 assertTrue("Socket indicated not connected when it should be", 1001 theSocket.isConnected()); 1002 theSocket.close(); 1003 serverSocket.close(); 1004 1005 // now do it with the new constructors and revalidate 1006 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1007 .getLocalHost(), Support_PortManager.getNextPort()); 1008 theSocket = new Socket(); 1009 assertFalse("Socket indicated connected when it was not", theSocket 1010 .isConnected()); 1011 serverSocket = new ServerSocket(); 1012 serverSocket.bind(theAddress); 1013 theSocket.connect(theAddress); 1014 servSock = serverSocket.accept(); 1015 assertTrue("Socket indicated not connected when it should be", 1016 theSocket.isConnected()); 1017 theSocket.close(); 1018 serverSocket.close(); 1019 } 1020 1021 public void test_isClosed() throws IOException { 1022 InetAddress addr = InetAddress.getLocalHost(); 1023 int port = Support_PortManager.getNextPort(); 1024 ServerSocket serverSocket = new ServerSocket(port, 5, addr); 1025 Socket theSocket = new Socket(addr, port); 1026 Socket servSock = serverSocket.accept(); 1027 1028 // validate isClosed returns expected values 1029 assertFalse("Socket should indicate it is not closed(1):", theSocket 1030 .isClosed()); 1031 theSocket.close(); 1032 assertTrue("Socket should indicate it is closed(1):", theSocket 1033 .isClosed()); 1034 1035 theSocket = new Socket(addr, port); 1036 assertFalse("Socket should indicate it is not closed(2):", theSocket 1037 .isClosed()); 1038 theSocket.close(); 1039 assertTrue("Socket should indicate it is closed(2):", theSocket 1040 .isClosed()); 1041 1042 // validate that isClosed works ok for sockets returned from 1043 // ServerSocket.accept() 1044 assertFalse("Server Socket should indicate it is not closed:", servSock 1045 .isClosed()); 1046 servSock.close(); 1047 assertTrue("Server Socket should indicate it is closed:", servSock 1048 .isClosed()); 1049 } 1050 1051 public void test_bindLjava_net_SocketAddress() throws IOException { 1052 1053 class mySocketAddress extends SocketAddress { 1054 1055 public mySocketAddress() { 1056 } 1057 } 1058 1059 // Address we cannot bind to 1060 Socket theSocket = new Socket(); 1061 try { 1062 theSocket.bind(new InetSocketAddress(InetAddress 1063 .getByAddress(Support_Configuration.nonLocalAddressBytes), 1064 Support_PortManager.getNextPort())); 1065 fail("No exception when binding to bad address:" 1066 + theSocket.getLocalSocketAddress().toString()); 1067 } catch (IOException ex) { 1068 } 1069 theSocket.close(); 1070 1071 // now create a socket that is not bound and then bind it 1072 theSocket = new Socket(); 1073 int portNumber = Support_PortManager.getNextPort(); 1074 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1075 portNumber)); 1076 1077 // validate that the localSocketAddress reflects the address we 1078 // bound to 1079 assertTrue( 1080 "Local address not correct after bind:" 1081 + theSocket.getLocalSocketAddress().toString() 1082 + " Expected: " 1083 + (new InetSocketAddress(InetAddress.getLocalHost(), 1084 portNumber)).toString(), theSocket 1085 .getLocalSocketAddress().equals( 1086 new InetSocketAddress(InetAddress 1087 .getLocalHost(), portNumber))); 1088 1089 // make sure we can now connect and that connections appear to come 1090 // from the address we bound to. 1091 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1092 .getLocalHost(), Support_PortManager.getNextPort()); 1093 ServerSocket serverSocket = new ServerSocket(); 1094 serverSocket.bind(theAddress); 1095 theSocket.connect(theAddress); 1096 Socket servSock = serverSocket.accept(); 1097 assertTrue( 1098 "Returned Remote address from server connected to does not match expected local address:" 1099 + servSock.getRemoteSocketAddress().toString() 1100 + " Expected: " 1101 + (new InetSocketAddress(InetAddress.getLocalHost(), 1102 portNumber)).toString(), servSock 1103 .getRemoteSocketAddress().equals( 1104 new InetSocketAddress(InetAddress 1105 .getLocalHost(), portNumber))); 1106 theSocket.close(); 1107 servSock.close(); 1108 serverSocket.close(); 1109 1110 // validate if we pass in null that it picks an address for us and 1111 // all is ok 1112 theSocket = new Socket(); 1113 theSocket.bind(null); 1114 assertNotNull("Bind with null did not work", theSocket 1115 .getLocalSocketAddress()); 1116 theSocket.close(); 1117 1118 // now check the error conditions 1119 1120 // Address that we have already bound to 1121 theSocket = new Socket(); 1122 Socket theSocket2 = new Socket(); 1123 try { 1124 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1125 Support_PortManager.getNextPort()); 1126 theSocket.bind(theAddress); 1127 theSocket2.bind(theAddress); 1128 fail("No exception binding to address that is not available"); 1129 } catch (IOException ex) { 1130 } 1131 theSocket.close(); 1132 theSocket2.close(); 1133 1134 // unsupported SocketAddress subclass 1135 theSocket = new Socket(); 1136 try { 1137 theSocket.bind(new mySocketAddress()); 1138 fail("No exception when binding using unsupported SocketAddress subclass"); 1139 } catch (IllegalArgumentException ex) { 1140 } 1141 theSocket.close(); 1142 } 1143 1144 public void test_bindLjava_net_SocketAddress_Proxy() throws IOException { 1145 //The Proxy will not impact on the bind operation.It can be assigned with any address. 1146 Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 0)); 1147 Socket socket = new Socket(proxy); 1148 1149 try { 1150 InetAddress address = InetAddress.getByName("localhost"); 1151 int port = 0; 1152 socket.bind(new InetSocketAddress(address, port)); 1153 1154 assertEquals(address, socket.getLocalAddress()); 1155 assertTrue(port!=socket.getLocalPort()); 1156 1157 } finally { 1158 socket.close(); 1159 } 1160 } 1161 1162 public void test_connectLjava_net_SocketAddress() throws Exception { 1163 // needed for some tests 1164 class mySocketAddress extends SocketAddress { 1165 1166 public mySocketAddress() { 1167 } 1168 } 1169 1170 class SocketCloser extends Thread { 1171 1172 int timeout = 0; 1173 1174 Socket theSocket = null; 1175 1176 public void run() { 1177 try { 1178 Thread.sleep(timeout); 1179 theSocket.close(); 1180 } catch (Exception e) { 1181 } 1182 ; 1183 return; 1184 } 1185 1186 public SocketCloser(int timeout, Socket theSocket) { 1187 this.timeout = timeout; 1188 this.theSocket = theSocket; 1189 } 1190 } 1191 1192 // start by validating the error checks 1193 int portNumber = Support_PortManager.getNextPort(); 1194 Socket theSocket = null; 1195 ServerSocket serverSocket = null; 1196 SocketAddress theAddress = null; 1197 SocketAddress nonConnectableAddress = null; 1198 SocketAddress nonReachableAddress = null; 1199 SocketAddress invalidType = null; 1200 // byte[] theBytes = {-1,-1,-1,-1}; 1201 byte[] theBytes = { 0, 0, 0, 0 }; 1202 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 1203 portNumber); 1204 nonConnectableAddress = new InetSocketAddress(InetAddress 1205 .getByAddress(theBytes), portNumber); 1206 nonReachableAddress = new InetSocketAddress(InetAddress 1207 .getByName(Support_Configuration.ResolvedNotExistingHost), 1208 portNumber); 1209 1210 invalidType = new mySocketAddress(); 1211 1212 try { 1213 theSocket = new Socket(); 1214 theSocket.connect(null); 1215 fail("No exception after null address passed in"); 1216 } catch (Exception e) { 1217 assertTrue("Wrong exception null address passed in: " 1218 + e.toString(), (e instanceof IllegalArgumentException)); 1219 } 1220 1221 try { 1222 theSocket = new Socket(); 1223 theSocket.connect(invalidType); 1224 fail("No exception when invalid socket address type passed in: "); 1225 } catch (Exception e) { 1226 assertTrue( 1227 "Wrong exception when when invalid socket address type passed in: " 1228 + e.toString(), 1229 (e instanceof IllegalArgumentException)); 1230 } 1231 1232 try { 1233 theSocket = new Socket(); 1234 theSocket.connect(nonConnectableAddress); 1235 fail("No exception when non Connectable Address passed in: "); 1236 } catch (Exception e) { 1237 assertTrue( 1238 "Wrong exception when non Connectable Address passed in: " 1239 + e.toString(), (e instan…
Large files files are truncated, but you can click here to view the full file