1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.nio.channels; 19 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.OutputStream; 23import java.net.BindException; 24import java.net.ConnectException; 25import java.net.InetAddress; 26import java.net.InetSocketAddress; 27import java.net.ServerSocket; 28import java.net.Socket; 29import java.net.SocketAddress; 30import java.net.SocketOption; 31import java.net.SocketException; 32import java.nio.Buffer; 33import java.nio.ByteBuffer; 34import java.nio.channels.AlreadyBoundException; 35import java.nio.channels.AlreadyConnectedException; 36import java.nio.channels.ClosedChannelException; 37import java.nio.channels.ConnectionPendingException; 38import java.nio.channels.IllegalBlockingModeException; 39import java.nio.channels.NoConnectionPendingException; 40import java.nio.channels.NotYetConnectedException; 41import java.nio.channels.ServerSocketChannel; 42import java.nio.channels.SocketChannel; 43import java.nio.channels.UnresolvedAddressException; 44import java.nio.channels.UnsupportedAddressTypeException; 45import java.nio.channels.spi.SelectorProvider; 46import java.util.Set; 47import junit.framework.TestCase; 48 49/** 50 * Tests for SocketChannel and its default implementation. 51 */ 52public class SocketChannelTest extends TestCase { 53 54 private static final int CAPACITY_NORMAL = 200; 55 56 private InetSocketAddress localAddr1; 57 private InetSocketAddress localAddr2; 58 59 private SocketChannel channel1; 60 61 private SocketChannel channel2; 62 63 private ServerSocket server1; 64 65 private ServerSocket server2; 66 67 private final static int TIMEOUT = 60000; 68 69 private final static int EOF = -1; 70 71 protected void setUp() throws Exception { 72 super.setUp(); 73 this.channel1 = SocketChannel.open(); 74 this.channel2 = SocketChannel.open(); 75 this.server1 = new ServerSocket(0); 76 this.localAddr1 = new InetSocketAddress("127.0.0.1", server1.getLocalPort()); 77 78 this.server2 = new ServerSocket(0); 79 this.localAddr2 = new InetSocketAddress("127.0.0.1", server2.getLocalPort()); 80 } 81 82 protected void tearDown() throws Exception { 83 super.tearDown(); 84 if (null != this.channel1) { 85 try { 86 this.channel1.close(); 87 } catch (Exception e) { 88 //ignore 89 } 90 } 91 if (null != this.channel2) { 92 try { 93 this.channel2.close(); 94 } catch (Exception e) { 95 //ignore 96 } 97 } 98 if (null != this.server1) { 99 try { 100 this.server1.close(); 101 } catch (Exception e) { 102 //ignore 103 } 104 } 105 if (null != this.server2) { 106 try { 107 this.server2.close(); 108 } catch (Exception e) { 109 //ignore 110 } 111 } 112 } 113 114 // ------------------------------------------------------------------- 115 // Test for methods in abstract class. 116 // ------------------------------------------------------------------- 117 /* 118 * Test method for 'java.nio.channels.SocketChannel.validOps()' 119 */ 120 public void testValidOps() { 121 MockSocketChannel testMSChannel = new MockSocketChannel(null); 122 assertEquals(13, this.channel1.validOps()); 123 assertEquals(13, testMSChannel.validOps()); 124 } 125 126 /* 127 * Test method for 'java.nio.channels.SocketChannel.open()' 128 */ 129 public void testOpen() throws IOException { 130 java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1]; 131 buf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL); 132 MockSocketChannel testMSChannel = new MockSocketChannel(null); 133 MockSocketChannel testMSChannelnotnull = new MockSocketChannel( 134 SelectorProvider.provider()); 135 assertNull(testMSChannel.provider()); 136 assertNotNull(testMSChannelnotnull.provider()); 137 assertNotNull(this.channel1); 138 assertEquals(this.channel1.provider(), testMSChannelnotnull.provider()); 139 try { 140 this.channel1.write(buf); 141 fail("Should throw NotYetConnectedException"); 142 } catch (NotYetConnectedException e) { 143 // correct 144 } 145 } 146 147 /* 148 * Test method for 'java.nio.channels.SocketChannel.open(SocketAddress)' 149 */ 150 public void testOpenSocketAddress_Null() throws IOException { 151 try { 152 SocketChannel.open(null); 153 fail("Should throw an IllegalArgumentException"); 154 } catch (IllegalArgumentException e) { 155 // correct 156 } 157 } 158 159 public void testBind_Null() throws Exception { 160 assertNull(channel1.socket().getLocalSocketAddress()); 161 162 channel1.socket().bind(null); 163 164 InetSocketAddress localAddress = (InetSocketAddress) channel1.socket().getLocalSocketAddress(); 165 assertTrue(localAddress.getAddress().isAnyLocalAddress()); 166 assertTrue(localAddress.getPort() > 0); 167 } 168 169 public void testBind_Failure() throws Exception { 170 assertNull(channel1.socket().getLocalSocketAddress()); 171 172 try { 173 // Bind to a local address that is in use 174 channel1.socket().bind(localAddr1); 175 fail(); 176 } catch (IOException expected) { 177 } 178 } 179 180 public void testBind_Closed() throws Exception { 181 channel1.close(); 182 183 try { 184 channel1.socket().bind(null); 185 fail(); 186 } catch (IOException expected) { 187 } 188 } 189 190 public void testBind_explicitPort() throws Exception { 191 ServerSocketChannel portPickingChannel = ServerSocketChannel.open(); 192 // Have the OS find a free port. 193 portPickingChannel.socket().bind(null); 194 InetSocketAddress address = (InetSocketAddress) portPickingChannel.socket().getLocalSocketAddress(); 195 assertTrue(address.getPort() > 0); 196 portPickingChannel.close(); 197 198 // There is a risk of flakiness here if the port is allocated to something else between 199 // close() and bind(). 200 InetSocketAddress bindAddress = new InetSocketAddress("localhost", address.getPort()); 201 // Allow the socket to bind to a port we know is already in use. 202 channel1.socket().setReuseAddress(true); 203 channel1.socket().bind(bindAddress); 204 205 InetSocketAddress boundAddress = (InetSocketAddress) channel1.socket().getLocalSocketAddress(); 206 assertEquals(bindAddress.getHostName(), boundAddress.getHostName()); 207 assertEquals(bindAddress.getPort(), boundAddress.getPort()); 208 } 209 210 public void test_getLocalSocketAddress_afterClose() throws IOException { 211 SocketChannel sc = SocketChannel.open(); 212 assertNull(sc.socket().getLocalSocketAddress()); 213 214 InetSocketAddress bindAddr = new InetSocketAddress("localhost", 0); 215 sc.socket().bind(bindAddr); 216 217 assertNotNull(sc.socket().getLocalSocketAddress()); 218 219 sc.close(); 220 221 assertFalse(sc.isOpen()); 222 223 sc.socket().getLocalSocketAddress(); 224 } 225 226 /* 227 * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])' 228 */ 229 public void testReadByteBufferArray() throws IOException { 230 java.nio.ByteBuffer[] byteBuf = null; 231 MockSocketChannel testMSChannelnull = new MockSocketChannel(null); 232 MockSocketChannel testMSChannel = new MockSocketChannel( 233 SelectorProvider.provider()); 234 ServerSocket testServer = new ServerSocket(0); 235 try { 236 try { 237 this.channel1.read(byteBuf); 238 fail("Should throw NPE"); 239 } catch (NullPointerException e) { 240 // correct 241 } 242 byteBuf = new java.nio.ByteBuffer[CAPACITY_NORMAL]; 243 try { 244 this.channel1.read(byteBuf); 245 fail("Should throw NotYetConnectedException"); 246 } catch (NotYetConnectedException e) { 247 // correct 248 } 249 long readNum = testMSChannel.read(byteBuf); 250 assertEquals(0, readNum); 251 readNum = CAPACITY_NORMAL; 252 readNum = testMSChannelnull.read(byteBuf); 253 assertEquals(0, readNum); 254 } finally { 255 testServer.close(); 256 } 257 } 258 259 /* 260 * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])' 261 */ 262 public void testReadByteBufferArray_BufNull() throws IOException { 263 java.nio.ByteBuffer[] byteBuf = null; 264 MockSocketChannel testMSChannelnull = new MockSocketChannel(null); 265 MockSocketChannel testMSChannel = new MockSocketChannel( 266 SelectorProvider.provider()); 267 try { 268 this.channel1.read(byteBuf); 269 fail("Should throw NPE"); 270 } catch (NullPointerException e) { 271 // correct 272 } 273 try { 274 testMSChannel.read(byteBuf); 275 fail("Should throw NPE"); 276 } catch (NullPointerException e) { 277 // correct 278 } 279 try { 280 testMSChannelnull.read(byteBuf); 281 fail("Should throw NPE"); 282 } catch (NullPointerException e) { 283 // correct 284 } 285 } 286 287 /* 288 * Test method for 'java.nio.channels.SocketChannel.write(ByteBuffer[])' 289 */ 290 public void testWriteByteBufferArray() throws IOException { 291 java.nio.ByteBuffer[] byteBuf = null; 292 MockSocketChannel testMSChannelnull = new MockSocketChannel(null); 293 MockSocketChannel testMSChannel = new MockSocketChannel( 294 SelectorProvider.provider()); 295 try { 296 this.channel1.write(byteBuf); 297 fail("Should throw NPE"); 298 } catch (NullPointerException e) { 299 // correct 300 } 301 byteBuf = new java.nio.ByteBuffer[CAPACITY_NORMAL]; 302 try { 303 this.channel1.write(byteBuf); 304 fail("Should throw NotYetConnectedException"); 305 } catch (NotYetConnectedException e) { 306 // correct 307 } 308 testMSChannel.write(byteBuf); 309 testMSChannelnull.write(byteBuf); 310 } 311 312 /* 313 * Test method for 'java.nio.channels.SocketChannel.write(ByteBuffer[])' 314 */ 315 public void testWriteByteBufferArray_BufNull() throws IOException { 316 java.nio.ByteBuffer[] byteBuf = null; 317 MockSocketChannel testMSChannelnull = new MockSocketChannel(null); 318 MockSocketChannel testMSChannel = new MockSocketChannel( 319 SelectorProvider.provider()); 320 try { 321 this.channel1.write(byteBuf); 322 fail("Should throw NPE"); 323 } catch (NullPointerException e) { 324 // correct 325 } 326 try { 327 testMSChannel.write(byteBuf); 328 fail("Should throw NPE"); 329 } catch (NullPointerException e) { 330 // correct 331 } 332 try { 333 testMSChannelnull.write(byteBuf); 334 fail("Should throw NPE"); 335 } catch (NullPointerException e) { 336 // correct 337 } 338 } 339 340 public void testSocket_BasicStatusBeforeConnect() throws IOException { 341 assertFalse(this.channel1.isConnected());// not connected 342 Socket s1 = this.channel1.socket(); 343 assertSocketBeforeBind(s1); 344 assertSocketBeforeConnect(s1); 345 Socket s2 = this.channel1.socket(); 346 // same 347 assertSame(s1, s2); 348 } 349 350 public void testSocket_Block_BasicStatusAfterConnect() throws IOException { 351 assertFalse(this.channel1.isConnected());// not connected 352 assertTrue(this.channel1.connect(localAddr1)); 353 354 assertTrue(this.channel1.isConnected()); 355 Socket s1 = this.channel1.socket(); 356 357 assertSocketAfterConnect(s1, localAddr1); 358 Socket s2 = this.channel1.socket(); 359 // same 360 assertSame(s1, s2); 361 } 362 363 public void testSocket_NonBlock_BasicStatusAfterConnect() throws Exception { 364 assertFalse(this.channel1.isConnected());// not connected 365 this.channel1.configureBlocking(false); 366 boolean connected = channel1.connect(localAddr1); 367 Socket s1; 368 Socket s2; 369 if (!connected) { 370 assertFalse(this.channel1.isConnected()); 371 assertTrue(this.channel1.isConnectionPending()); 372 s1 = this.channel1.socket(); 373 // A connect() causes an implicit bind() 374 assertSocketAfterImplicitBind(s1); 375 376 // status of not connected 377 assertSocketBeforeConnect(s1); 378 s2 = this.channel1.socket(); 379 // same 380 assertSame(s1, s2); 381 } 382 383 if (tryFinish()) { 384 assertTrue(this.channel1.isConnected()); 385 s1 = this.channel1.socket(); 386 assertSocketAfterConnect(s1, localAddr1); 387 s2 = this.channel1.socket(); 388 // same 389 assertSame(s1, s2); 390 } 391 } 392 393 public void testSocket_Block_ActionsBeforeConnect() throws IOException { 394 assertFalse(this.channel1.isConnected());// not connected 395 Socket s = this.channel1.socket(); 396 assertSocketAction_Block_BeforeConnect(s); 397 } 398 399 public void testSocket_Block_ActionsAfterConnect() throws IOException { 400 assertFalse(this.channel1.isConnected());// not connected 401 assertTrue(this.channel1.connect(localAddr1)); 402 assertTrue(this.channel1.isConnected()); 403 Socket s = this.channel1.socket(); 404 assertSocketAction_Block_AfterConnect(s); 405 406 } 407 408 public void testSocket_NonBlock_ActionsAfterConnectBeforeFinish() 409 throws IOException { 410 assertFalse(this.channel1.isConnected());// not connected 411 this.channel1.configureBlocking(false); 412 boolean connected = channel1.connect(localAddr1); 413 if (!connected) { 414 assertFalse(this.channel1.isConnected()); 415 assertTrue(this.channel1.isConnectionPending()); 416 Socket s1 = this.channel1.socket(); 417 // Action of not connected 418 assertSocketAction_NonBlock_BeforeConnect(s1); 419 Socket s2 = this.channel1.socket(); 420 // same 421 assertSame(s1, s2); 422 } 423 } 424 425 public void testSocket_NonBlock_ActionsAfterConnectAfterFinish() 426 throws Exception { 427 assertFalse(this.channel1.isConnected());// not connected 428 this.channel1.configureBlocking(false); 429 channel1.connect(localAddr1); 430 if (tryFinish()) { 431 Socket s1 = this.channel1.socket(); 432 assertSocketAction_NonBlock_AfterConnect(s1); 433 Socket s2 = this.channel1.socket(); 434 // same 435 assertSame(s1, s2); 436 } 437 } 438 439 public void testSocket_getInetAddress() throws Exception { 440 Socket socket = channel1.socket(); 441 assertNull(socket.getInetAddress()); 442 443 channel1.connect(localAddr1); 444 445 assertNotNull(socket.getInetAddress()); 446 assertEquals(localAddr1.getAddress(), socket.getInetAddress()); 447 } 448 449 public void testSocket_getRemoteSocketAddress() throws Exception { 450 Socket socket = channel1.socket(); 451 assertNull(socket.getRemoteSocketAddress()); 452 453 channel1.connect(localAddr1); 454 455 assertNotNull(socket.getRemoteSocketAddress()); 456 assertEquals(localAddr1, socket.getRemoteSocketAddress()); 457 } 458 459 public void testSocket_getPort() throws Exception { 460 Socket socket = channel1.socket(); 461 assertEquals(0, socket.getPort()); 462 463 channel1.connect(localAddr1); 464 465 assertEquals(localAddr1.getPort(), socket.getPort()); 466 } 467 468 public void testSocket_getLocalAddress() throws Exception { 469 Socket socket = channel1.socket(); 470 471 channel1.connect(localAddr1); 472 473 assertNotNull(socket.getLocalSocketAddress()); 474 } 475 476 public void testSocket_getLocalSocketAddress() throws Exception { 477 Socket socket = channel1.socket(); 478 assertNull(socket.getLocalSocketAddress()); 479 480 channel1.connect(localAddr1); 481 482 assertNotNull(socket.getLocalSocketAddress()); 483 } 484 485 public void testSocket_getLocalPort() throws Exception { 486 Socket socket = channel1.socket(); 487 assertEquals(-1, socket.getLocalPort()); 488 489 channel1.connect(localAddr1); 490 491 assertTrue(-1 != socket.getLocalPort()); 492 assertTrue(0 != socket.getLocalPort()); 493 } 494 495 public void testSocket_bind() throws Exception { 496 Socket socket = channel1.socket(); 497 socket.bind(new InetSocketAddress("127.0.0.1", 0)); 498 assertEquals("127.0.0.1", socket.getLocalAddress().getHostAddress()); 499 assertTrue(socket.getLocalPort() != -1); 500 } 501 502 private void assertSocketBeforeBind(Socket s) { 503 assertFalse(s.isBound()); 504 assertTrue(s.getLocalAddress().isAnyLocalAddress()); 505 // RI fails here. RI returns 0 while spec says unbound socket should 506 // return -1. 507 assertEquals(-1, s.getLocalPort()); 508 assertNull(s.getLocalSocketAddress()); 509 } 510 511 private void assertSocketAfterImplicitBind(Socket s) throws IOException { 512 assertTrue(s.isBound()); 513 assertTrue(s.getLocalAddress().isLoopbackAddress()); 514 assertTrue(s.getLocalPort() > 0); 515 516 InetSocketAddress localSocketAddress = (InetSocketAddress) s.getLocalSocketAddress(); 517 assertTrue(localSocketAddress.getAddress().isLoopbackAddress()); 518 assertEquals(s.getLocalPort(), localSocketAddress.getPort()); 519 } 520 521 private void assertSocketBeforeConnect(Socket s) throws IOException { 522 assertFalse(s.isClosed()); 523 assertFalse(s.isConnected()); 524 assertFalse(s.getKeepAlive()); 525 try { 526 s.getInputStream(); 527 fail("Should throw SocketException."); 528 } catch (SocketException e) { 529 // OK. 530 } 531 assertFalse(s.getOOBInline()); 532 try { 533 s.getOutputStream(); 534 fail("Should throw SocketException."); 535 } catch (SocketException e) { 536 // OK. 537 } 538 assertEquals(-1, s.getSoLinger()); 539 assertFalse(s.getTcpNoDelay()); 540 541 assertFalse(s.isInputShutdown()); 542 assertFalse(s.isOutputShutdown()); 543 544 assertNull(s.getInetAddress()); 545 assertFalse(s.getReuseAddress()); 546 547 // not connected 548 assertEquals(0, s.getPort()); 549 assertTrue(s.getReceiveBufferSize() >= 8192); 550 assertNull(s.getRemoteSocketAddress()); 551 assertTrue(s.getSendBufferSize() >= 8192); 552 assertEquals(0, s.getSoTimeout()); 553 assertEquals(0, s.getTrafficClass()); 554 555 } 556 557 private void assertSocketAfterConnect(Socket s, InetSocketAddress address) 558 throws IOException { 559 assertTrue(s.isBound()); 560 assertFalse(s.isClosed()); 561 assertTrue(s.isConnected()); 562 assertFalse(s.getKeepAlive()); 563 564 assertNotNull(s.getInputStream()); 565 assertNotNull(s.getOutputStream()); 566 567 assertFalse(s.getOOBInline()); 568 assertEquals(-1, s.getSoLinger()); 569 assertFalse(s.getTcpNoDelay()); 570 571 assertFalse(s.isInputShutdown()); 572 assertFalse(s.isOutputShutdown()); 573 574 assertSame(s.getInetAddress(), address.getAddress()); 575 576 assertEquals(s.getLocalAddress(), this.localAddr1.getAddress()); 577 assertEquals(s.getPort(), address.getPort()); 578 assertNotNull(s.getLocalSocketAddress()); 579 assertTrue(s.getReceiveBufferSize() >= 8192); 580 assertEquals(s.getRemoteSocketAddress(), (SocketAddress) address); 581 // assertFalse(s.getReuseAddress()); 582 assertTrue(s.getSendBufferSize() >= 8192); 583 assertEquals(0, s.getSoTimeout()); 584 assertEquals(0, s.getTrafficClass()); 585 } 586 587 private void assertSocketAction_Block_BeforeConnect(Socket s) 588 throws IOException { 589 assertFalse(this.channel1.isConnected()); 590 s.connect(localAddr2); 591 assertTrue(this.channel1.isConnected()); 592 assertTrue(s.isConnected()); 593 594 assertSocketAfterConnect(s, localAddr2); 595 596 try { 597 s.bind(localAddr2); 598 fail("Should throw SocketException"); 599 } catch (SocketException e) { 600 // OK. 601 } 602 603 s.close(); 604 assertTrue(s.isClosed()); 605 assertFalse(this.channel1.isOpen()); 606 } 607 608 private void assertSocketAction_NonBlock_BeforeConnect(Socket s) 609 throws IOException { 610 assertFalse(this.channel1.isConnected()); 611 try { 612 s.connect(localAddr2); 613 fail("Should throw IllegalBlockingModeException"); 614 } catch (IllegalBlockingModeException e1) { 615 // OK. 616 } 617 618 if (this.channel1.isConnectionPending()) { 619 try { 620 s.bind(localAddr2); 621 fail("Should throw ConnectionPendingException"); 622 } catch (ConnectionPendingException e1) { 623 // OK. 624 } 625 } else { 626 try { 627 s.bind(localAddr2); 628 fail("Should throw BindException"); 629 } catch (BindException e1) { 630 // OK. 631 } 632 } 633 634 assertFalse(this.channel1.isConnected()); 635 assertFalse(s.isConnected()); 636 637 s.close(); 638 assertTrue(s.isClosed()); 639 assertFalse(this.channel1.isOpen()); 640 } 641 642 private void assertSocketAction_Block_AfterConnect(Socket s) 643 throws IOException { 644 assertEquals(s.getPort(), localAddr1.getPort()); 645 assertTrue(this.channel1.isConnected()); 646 assertTrue(s.isConnected()); 647 try { 648 s.connect(localAddr2); 649 fail("Should throw SocketException"); 650 } catch (SocketException e) { 651 // OK. 652 } 653 654 try { 655 s.bind(localAddr2); 656 fail("Should throw SocketException"); 657 } catch (SocketException e) { 658 // OK. 659 } 660 661 s.close(); 662 assertTrue(s.isClosed()); 663 assertFalse(this.channel1.isOpen()); 664 } 665 666 private void assertSocketAction_NonBlock_AfterConnect(Socket s) 667 throws IOException { 668 assertEquals(s.getPort(), localAddr1.getPort()); 669 assertTrue(this.channel1.isConnected()); 670 assertTrue(s.isConnected()); 671 672 if (this.channel1.isConnectionPending()) { 673 try { 674 s.connect(localAddr2); 675 fail("Should throw SocketException"); 676 } catch (SocketException e) { 677 // OK. 678 } 679 } else { 680 try { 681 s.connect(localAddr2); 682 fail("Should throw IllegalBlockingModeException"); 683 } catch (IllegalBlockingModeException e) { 684 // OK. 685 } 686 } 687 688 try { 689 s.bind(localAddr2); 690 fail("Should throw SocketException"); 691 } catch (SocketException e) { 692 // OK. 693 } 694 695 s.close(); 696 assertTrue(s.isClosed()); 697 assertFalse(this.channel1.isOpen()); 698 } 699 700 // ------------------------------------------------------------------- 701 // Tests for connect(), finishConnect(),isConnected(),isConnectionPending() 702 // These methods are very close, so we test them together, call them "CFII". 703 // ------------------------------------------------------------------- 704 /** 705 * connect-->finish-->close 706 */ 707 public void testCFII_Norml_NoServer_Block() throws Exception { 708 // ensure 709 ensureServerClosed(); 710 assertTrue(this.channel1.isBlocking()); 711 statusNotConnected_NotPending(); 712 // connect 713 try { 714 this.channel1.connect(localAddr1); 715 fail("Should throw a ConnectException here."); 716 } catch (ConnectException e) { 717 // OK. 718 } 719 statusChannelClosed(); 720 try { 721 this.channel1.finishConnect(); 722 fail("Should throw a ClosedChannelException here."); 723 } catch (ClosedChannelException e) { 724 // OK. 725 } 726 } 727 728 /** 729 * connect-->finish-->close 730 */ 731 public void testCFII_Norml_NoServer_NonBlock() throws Exception { 732 connectNoServerNonBlock(); 733 734 this.channel1.close(); 735 statusChannelClosed(); 736 } 737 738 /** 739 * connect-->finish-->close 740 */ 741 public void testCFII_Norml_Server_Block() throws Exception { 742 connectServerBlock(); 743 744 this.channel1.close(); 745 statusChannelClosed(); 746 747 } 748 749 /** 750 * connect-->finish-->close 751 */ 752 public void testCFII_Norml_Server_NonBlock() throws Exception { 753 connectServerNonBlock(); 754 755 this.channel1.close(); 756 statusChannelClosed(); 757 } 758 759 /** 760 * connect-->server closed-->finish-->close 761 */ 762 public void testCFII_ServerClosed_Block() throws Exception { 763 // ensure 764 ensureServerOpen(); 765 assertTrue(this.channel1.isBlocking()); 766 statusNotConnected_NotPending(); 767 // connect 768 assertTrue(this.channel1.connect(localAddr1)); 769 statusConnected_NotPending(); 770 771 ensureServerClosed(); 772 773 tryFinish(); 774 775 this.channel1.close(); 776 statusChannelClosed(); 777 778 } 779 780 /** 781 * connect-->server closed-->finish-->close 782 */ 783 public void testCFII_ServerClosed_NonBlock() throws Exception { 784 // ensure 785 ensureServerOpen(); 786 this.channel1.configureBlocking(false); 787 statusNotConnected_NotPending(); 788 // connect 789 boolean connected = channel1.connect(localAddr1); 790 if (!connected) { 791 statusNotConnected_Pending(); 792 } 793 ensureServerClosed(); 794 795 tryFinish(); 796 797 this.channel1.close(); 798 statusChannelClosed(); 799 } 800 801 /** 802 * connect-->finish-->server closed-->close 803 */ 804 public void testCFII_ServerClosedAfterFinish_Block() throws Exception { 805 connectServerBlock(); 806 807 ensureServerClosed(); 808 assertTrue(this.channel1.isOpen()); 809 this.channel1.close(); 810 statusChannelClosed(); 811 812 } 813 814 /** 815 * connect-->finish-->server closed-->close 816 */ 817 public void testCFII_ServerClosedAfterFinish_NonBlock() throws Exception { 818 connectServerNonBlock(); 819 820 ensureServerClosed(); 821 assertTrue(this.channel1.isOpen()); 822 this.channel1.close(); 823 statusChannelClosed(); 824 } 825 826 /** 827 * no server-->connect-->server open-->finish-->close 828 */ 829 public void testCFII_ServerStartLater_Block() throws Exception { 830 // ensure 831 ensureServerClosed(); 832 assertTrue(this.channel1.isBlocking()); 833 statusNotConnected_NotPending(); 834 // connect 835 try { 836 this.channel1.connect(localAddr1); 837 fail("Should throw a ConnectException here."); 838 } catch (ConnectException e) { 839 // OK. 840 } 841 statusChannelClosed(); 842 ensureServerOpen(); 843 try { 844 this.channel1.finishConnect(); 845 fail("Should throw a ClosedChannelException here."); 846 } catch (ClosedChannelException e) { 847 // OK. 848 } 849 } 850 851 /** 852 * no server-->connect-->server open-->finish-->close 853 */ 854 public void testCFII_ServerStartLater_NonBlock() throws Exception { 855 // ensure 856 ensureServerClosed(); 857 this.channel1.configureBlocking(false); 858 statusNotConnected_NotPending(); 859 // connect 860 assertFalse(this.channel1.connect(localAddr1)); 861 statusNotConnected_Pending(); 862 863 ensureServerOpen(); 864 865 try { 866 assertFalse(this.channel1.finishConnect()); 867 statusNotConnected_Pending(); 868 this.channel1.close(); 869 } catch (ConnectException e) { 870 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 871 } 872 } 873 874 /** 875 * connect-->finish-->finish-->close 876 */ 877 public void testCFII_FinishTwice_NoServer_NonBlock() throws Exception { 878 // ensure 879 ensureServerClosed(); 880 this.channel1.configureBlocking(false); 881 statusNotConnected_NotPending(); 882 // connect 883 assertFalse(this.channel1.connect(localAddr1)); 884 statusNotConnected_Pending(); 885 try { 886 assertFalse(this.channel1.finishConnect()); 887 statusNotConnected_Pending(); 888 assertFalse(this.channel1.finishConnect()); 889 statusNotConnected_Pending(); 890 this.channel1.close(); 891 } catch (ConnectException e) { 892 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 893 } 894 statusChannelClosed(); 895 } 896 897 /** 898 * connect-->finish-->finish-->close 899 */ 900 public void testCFII_FinishTwice_Server_Block() throws Exception { 901 connectServerBlock(); 902 tryFinish(); 903 this.channel1.close(); 904 statusChannelClosed(); 905 906 } 907 908 /** 909 * connect-->finish-->finish-->close 910 */ 911 public void testCFII_FinishTwice_Server_NonBlock() throws Exception { 912 connectServerNonBlock(); 913 tryFinish(); 914 this.channel1.close(); 915 statusChannelClosed(); 916 } 917 918 /** 919 * connect-->finish-->connect-->close 920 */ 921 public void testCFII_ConnectAfterFinish_NoServer_Block() throws Exception { 922 // ensure 923 ensureServerClosed(); 924 assertTrue(this.channel1.isBlocking()); 925 statusNotConnected_NotPending(); 926 // connect 927 try { 928 this.channel1.connect(localAddr1); 929 fail("Should throw a ConnectException here."); 930 } catch (ConnectException e) { 931 // OK. 932 } 933 statusChannelClosed(); 934 try { 935 this.channel1.finishConnect(); 936 fail("Should throw a ClosedChannelException here."); 937 } catch (ClosedChannelException e) { 938 // OK. 939 } 940 statusChannelClosed(); 941 try { 942 this.channel1.connect(localAddr1); 943 fail("Should throw a ClosedChannelException here."); 944 } catch (ClosedChannelException e) { 945 // OK. 946 } 947 statusChannelClosed(); 948 } 949 950 /** 951 * connect-->finish-->connect-->close 952 */ 953 public void testCFII_ConnectAfterFinish_NoServer_NonBlock() 954 throws Exception { 955 // ensure 956 ensureServerClosed(); 957 this.channel1.configureBlocking(false); 958 statusNotConnected_NotPending(); 959 // connect 960 assertFalse(this.channel1.connect(localAddr1)); 961 statusNotConnected_Pending(); 962 try { 963 assertFalse(this.channel1.finishConnect()); 964 statusNotConnected_Pending(); 965 } catch (ConnectException e) { 966 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 967 } 968 969 if (this.channel1.isOpen()) { 970 971 try { 972 this.channel1.connect(localAddr1); 973 fail("Should throw a ConnectionPendingException here."); 974 } catch (ConnectionPendingException e) { 975 // OK. 976 } 977 statusNotConnected_Pending(); 978 979 // connect another addr 980 try { 981 this.channel1.connect(localAddr2); 982 fail("Should throw a ConnectionPendingException here."); 983 } catch (ConnectionPendingException e) { 984 // OK. 985 } 986 statusNotConnected_Pending(); 987 988 // connect if server closed 989 ensureServerClosed(); 990 991 try { 992 this.channel1.connect(localAddr1); 993 fail("Should throw a ConnectionPendingException here."); 994 } catch (ConnectionPendingException e) { 995 // OK. 996 } 997 statusNotConnected_Pending(); 998 999 this.channel1.close(); 1000 } 1001 statusChannelClosed(); 1002 } 1003 1004 /** 1005 * connect-->finish-->connect-->close 1006 */ 1007 public void testCFII_ConnectAfterFinish_Server_Block() throws Exception { 1008 connectServerBlock(); 1009 1010 if (!this.channel1.isConnected()) { 1011 System.err 1012 .println("Connection fail, testCFII_ConnectAfterFinish_Server_Block is not finished."); 1013 return; 1014 } 1015 1016 try { 1017 this.channel1.connect(localAddr1); 1018 fail("Should throw an AlreadyConnectedException here."); 1019 } catch (AlreadyConnectedException e) { 1020 // OK. 1021 } 1022 statusConnected_NotPending(); 1023 1024 // connect another addr 1025 try { 1026 this.channel1.connect(localAddr2); 1027 fail("Should throw an AlreadyConnectedException here."); 1028 } catch (AlreadyConnectedException e) { 1029 // OK. 1030 } 1031 statusConnected_NotPending(); 1032 1033 // connect if server closed 1034 ensureServerClosed(); 1035 1036 try { 1037 this.channel1.connect(localAddr1); 1038 fail("Should throw an AlreadyConnectedException here."); 1039 } catch (AlreadyConnectedException e) { 1040 // OK. 1041 } 1042 statusConnected_NotPending(); 1043 1044 this.channel1.close(); 1045 statusChannelClosed(); 1046 1047 } 1048 1049 /** 1050 * connect-->finish-->connect-->close 1051 */ 1052 public void testCFII_ConnectAfterFinish_Server_NonBlock() throws Exception { 1053 connectServerNonBlock(); 1054 1055 if (!this.channel1.isConnected()) { 1056 System.err 1057 .println("Connection fail, testCFII_ConnectAfterFinish_Server_Block is not finished."); 1058 return; 1059 } 1060 try { 1061 this.channel1.connect(localAddr1); 1062 fail("Should throw an AlreadyConnectedException or a ConnectionPendingException here."); 1063 } catch (AlreadyConnectedException e) { 1064 // OK. 1065 } 1066 1067 statusConnected_NotPending(); 1068 1069 // connect another addr 1070 try { 1071 this.channel1.connect(localAddr2); 1072 fail("Should throw an AlreadyConnectedException here."); 1073 } catch (AlreadyConnectedException e) { 1074 // OK. 1075 } 1076 statusConnected_NotPending(); 1077 1078 // connect if server closed 1079 ensureServerClosed(); 1080 1081 try { 1082 this.channel1.connect(localAddr1); 1083 fail("Should throw an AlreadyConnectedException here."); 1084 } catch (AlreadyConnectedException e) { 1085 // OK. 1086 } 1087 statusConnected_NotPending(); 1088 1089 this.channel1.close(); 1090 statusChannelClosed(); 1091 } 1092 1093 /** 1094 * connect-->connect-->finish-->close 1095 */ 1096 public void testCFII_ConnectTwice_NoServer_NonBlock() throws Exception { 1097 // ensure 1098 ensureServerClosed(); 1099 this.channel1.configureBlocking(false); 1100 statusNotConnected_NotPending(); 1101 // connect 1102 assertFalse(this.channel1.connect(localAddr1)); 1103 statusNotConnected_Pending(); 1104 1105 try { 1106 this.channel1.connect(localAddr1); 1107 fail("Should throw a ConnectionPendingException here."); 1108 } catch (ConnectionPendingException e) { 1109 // OK. 1110 } 1111 statusNotConnected_Pending(); 1112 1113 // connect another addr 1114 try { 1115 this.channel1.connect(localAddr2); 1116 fail("Should throw a ConnectionPendingException here."); 1117 } catch (ConnectionPendingException e) { 1118 // OK. 1119 } 1120 statusNotConnected_Pending(); 1121 1122 // connect if server closed 1123 ensureServerClosed(); 1124 1125 try { 1126 this.channel1.connect(localAddr1); 1127 fail("Should throw a ConnectionPendingException here."); 1128 } catch (ConnectionPendingException e) { 1129 // OK. 1130 } 1131 statusNotConnected_Pending(); 1132 1133 try { 1134 assertFalse(this.channel1.finishConnect()); 1135 statusNotConnected_Pending(); 1136 this.channel1.close(); 1137 } catch (ConnectException e) { 1138 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 1139 } 1140 1141 statusChannelClosed(); 1142 } 1143 1144 /** 1145 * connect-->connect-->finish-->close 1146 */ 1147 public void testCFII_ConnectTwice_Server_Block() throws Exception { 1148 // ensure 1149 ensureServerOpen(); 1150 assertTrue(this.channel1.isBlocking()); 1151 statusNotConnected_NotPending(); 1152 // connect 1153 assertTrue(this.channel1.connect(localAddr1)); 1154 statusConnected_NotPending(); 1155 1156 try { 1157 this.channel1.connect(localAddr1); 1158 fail("Should throw an AlreadyConnectedException here."); 1159 } catch (AlreadyConnectedException e) { 1160 // OK. 1161 } 1162 statusConnected_NotPending(); 1163 1164 // connect another addr 1165 try { 1166 this.channel1.connect(localAddr2); 1167 fail("Should throw an AlreadyConnectedException here."); 1168 } catch (AlreadyConnectedException e) { 1169 // OK. 1170 } 1171 statusConnected_NotPending(); 1172 1173 // connect if server closed 1174 ensureServerClosed(); 1175 1176 try { 1177 this.channel1.connect(localAddr1); 1178 fail("Should throw an AlreadyConnectedException here."); 1179 } catch (AlreadyConnectedException e) { 1180 // OK. 1181 } 1182 statusConnected_NotPending(); 1183 1184 tryFinish(); 1185 1186 this.channel1.close(); 1187 statusChannelClosed(); 1188 1189 } 1190 1191 /** 1192 * connect-->connect-->finish-->close 1193 */ 1194 public void testCFII_ConnectTwice_Server_NonBlock() throws Exception { 1195 // ensure 1196 ensureServerOpen(); 1197 this.channel1.configureBlocking(false); 1198 statusNotConnected_NotPending(); 1199 // connect 1200 boolean connected = channel1.connect(localAddr1); 1201 if (!connected) { 1202 statusNotConnected_Pending(); 1203 1204 try { 1205 this.channel1.connect(localAddr1); 1206 fail("Should throw a ConnectionPendingException here."); 1207 } catch (ConnectionPendingException e) { 1208 // OK. 1209 } 1210 statusNotConnected_Pending(); 1211 1212 // connect another addr 1213 try { 1214 this.channel1.connect(localAddr2); 1215 fail("Should throw a ConnectionPendingException here."); 1216 } catch (ConnectionPendingException e) { 1217 // OK. 1218 } 1219 statusNotConnected_Pending(); 1220 1221 // connect if server closed 1222 ensureServerClosed(); 1223 1224 try { 1225 this.channel1.connect(localAddr1); 1226 fail("Should throw a ConnectionPendingException here."); 1227 } catch (ConnectionPendingException e) { 1228 // OK. 1229 } 1230 statusNotConnected_Pending(); 1231 } 1232 tryFinish(); 1233 1234 this.channel1.close(); 1235 statusChannelClosed(); 1236 } 1237 1238 /** 1239 * finish-->connect-->finish-->close 1240 */ 1241 public void testCFII_FinishFirst_NoServer_Block() throws Exception { 1242 // ensure 1243 ensureServerClosed(); 1244 assertTrue(this.channel1.isBlocking()); 1245 statusNotConnected_NotPending(); 1246 // finish 1247 try { 1248 this.channel1.finishConnect(); 1249 fail("Should throw NoConnectionPendingException"); 1250 } catch (NoConnectionPendingException e) { 1251 // OK. 1252 } 1253 statusNotConnected_NotPending(); 1254 // connect 1255 try { 1256 this.channel1.connect(localAddr1); 1257 fail("Should throw a ConnectException here."); 1258 } catch (ConnectException e) { 1259 // OK. 1260 } 1261 statusChannelClosed(); 1262 try { 1263 this.channel1.finishConnect(); 1264 fail("Should throw a ClosedChannelException here."); 1265 } catch (ClosedChannelException e) { 1266 // OK. 1267 } 1268 statusChannelClosed(); 1269 } 1270 1271 /** 1272 * finish-->connect-->finish-->close 1273 */ 1274 public void testCFII_FinishFirst_NoServer_NonBlock() throws Exception { 1275 // ensure 1276 ensureServerClosed(); 1277 this.channel1.configureBlocking(false); 1278 statusNotConnected_NotPending(); 1279 // finish 1280 try { 1281 this.channel1.finishConnect(); 1282 fail("Should throw NoConnectionPendingException"); 1283 } catch (NoConnectionPendingException e) { 1284 // OK. 1285 } 1286 statusNotConnected_NotPending(); 1287 // connect 1288 assertFalse(this.channel1.connect(localAddr1)); 1289 statusNotConnected_Pending(); 1290 1291 try { 1292 assertFalse(this.channel1.finishConnect()); 1293 statusNotConnected_Pending(); 1294 this.channel1.close(); 1295 } catch (ConnectException e) { 1296 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 1297 } 1298 1299 statusChannelClosed(); 1300 } 1301 1302 /** 1303 * finish-->connect-->finish-->close 1304 */ 1305 public void testCFII_FinishFirst_Server_Block() throws Exception { 1306 // ensure 1307 ensureServerOpen(); 1308 assertTrue(this.channel1.isBlocking()); 1309 statusNotConnected_NotPending(); 1310 // finish 1311 try { 1312 this.channel1.finishConnect(); 1313 fail("Should throw NoConnectionPendingException"); 1314 } catch (NoConnectionPendingException e) { 1315 // OK. 1316 } 1317 statusNotConnected_NotPending(); 1318 // connect 1319 assertTrue(this.channel1.connect(localAddr1)); 1320 statusConnected_NotPending(); 1321 1322 tryFinish(); 1323 1324 this.channel1.close(); 1325 statusChannelClosed(); 1326 1327 } 1328 1329 /** 1330 * finish-->connect-->finish-->close 1331 */ 1332 public void testCFII_FinishFirst_Server_NonBlock() throws Exception { 1333 // ensure 1334 ensureServerOpen(); 1335 this.channel1.configureBlocking(false); 1336 statusNotConnected_NotPending(); 1337 // finish 1338 try { 1339 this.channel1.finishConnect(); 1340 fail("Should throw NoConnectionPendingException"); 1341 } catch (NoConnectionPendingException e) { 1342 // OK. 1343 } 1344 statusNotConnected_NotPending(); 1345 // connect 1346 boolean connected = channel1.connect(localAddr1); 1347 if (!connected) { 1348 statusNotConnected_Pending(); 1349 } 1350 tryFinish(); 1351 1352 this.channel1.close(); 1353 statusChannelClosed(); 1354 } 1355 1356 public void testCFII_Null() throws Exception { 1357 statusNotConnected_NotPending(); 1358 try { 1359 this.channel1.connect(null); 1360 fail("Should throw an IllegalArgumentException here."); 1361 } catch (IllegalArgumentException e) { 1362 // OK. 1363 } 1364 } 1365 1366 public void testCFII_UnsupportedType() throws Exception { 1367 class SubSocketAddress extends SocketAddress { 1368 private static final long serialVersionUID = 1L; 1369 1370 //empty 1371 public SubSocketAddress() { 1372 super(); 1373 } 1374 } 1375 statusNotConnected_NotPending(); 1376 SocketAddress newTypeAddress = new SubSocketAddress(); 1377 try { 1378 this.channel1.connect(newTypeAddress); 1379 fail("Should throw an UnsupportedAddressTypeException here."); 1380 } catch (UnsupportedAddressTypeException e) { 1381 // OK. 1382 } 1383 } 1384 1385 public void testCFII_Unresolved() throws IOException { 1386 statusNotConnected_NotPending(); 1387 InetSocketAddress unresolved = new InetSocketAddress( 1388 "unresolved address", 1080); 1389 try { 1390 this.channel1.connect(unresolved); 1391 fail("Should throw an UnresolvedAddressException here."); 1392 } catch (UnresolvedAddressException e) { 1393 // OK. 1394 } 1395 } 1396 1397 public void testCFII_EmptyHost() throws Exception { 1398 statusNotConnected_NotPending(); 1399 ServerSocket server = new ServerSocket(0); 1400 int port = server.getLocalPort(); 1401 server.close(); 1402 try { 1403 this.channel1.connect(new InetSocketAddress("", port)); 1404 fail("Should throw ConnectException"); 1405 } catch (ConnectException e) { 1406 // correct 1407 } 1408 } 1409 1410 public void testCFII_CloseFirst() throws Exception { 1411 this.channel1.close(); 1412 statusChannelClosed(); 1413 ensureServerOpen(); 1414 try { 1415 this.channel1.connect(localAddr1); 1416 fail("Should throw ClosedChannelException."); 1417 } catch (ClosedChannelException e) { 1418 // OK. 1419 } 1420 statusChannelClosed(); 1421 try { 1422 this.channel1.finishConnect(); 1423 fail("Should throw ClosedChannelException."); 1424 } catch (ClosedChannelException e) { 1425 // OK. 1426 } 1427 statusChannelClosed(); 1428 try { 1429 this.channel1.configureBlocking(false); 1430 fail("Should throw ClosedChannelException."); 1431 } catch (ClosedChannelException e) { 1432 // OK. 1433 } 1434 statusChannelClosed(); 1435 } 1436 1437 public void testCFII_StatusAfterFinish() throws Exception { 1438 // 1. close server, finish must return false, check the status 1439 ensureServerClosed(); 1440 1441 // 1.1 block mode 1442 assertTrue(this.channel1.isBlocking()); 1443 try { 1444 channel1.connect(localAddr1); 1445 fail("Should throw ConnectException"); 1446 } catch (ConnectException e) { 1447 // OK. 1448 } 1449 assertFalse(this.channel1.isOpen()); 1450 1451 assertFalse(this.channel1.isOpen()); 1452 assertTrue(this.channel1.isBlocking()); 1453 assertFalse(this.channel1.isConnectionPending()); 1454 1455 // 1.2 non block mode 1456 this.channel1 = SocketChannel.open(); 1457 this.channel1.configureBlocking(false); 1458 assertFalse(this.channel1.connect(localAddr1)); 1459 try { 1460 assertFalse(this.channel1.finishConnect()); 1461 statusNotConnected_Pending(); 1462 this.channel1.close(); 1463 } catch (ConnectException e) { 1464 System.out.println(e.getMessage()); 1465 } 1466 1467 // 2. start server, finish usually return true, check the status 1468 ensureServerOpen(); 1469 1470 // 2.1 block mode 1471 this.channel1 = SocketChannel.open(); 1472 assertTrue(this.channel1.isBlocking()); 1473 assertTrue(this.channel1.connect(localAddr1)); 1474 assertTrue(this.channel1.finishConnect()); 1475 statusConnected_NotPending(); 1476 this.channel1.close(); 1477 1478 // 2.2 non block mode 1479 this.channel1 = SocketChannel.open(); 1480 this.channel1.configureBlocking(false); 1481 assertFalse(this.channel1.connect(localAddr1)); 1482 tryFinish(); 1483 this.channel1.close(); 1484 } 1485 1486 private void ensureServerClosed() throws IOException { 1487 if (null != this.server1) { 1488 this.server1.close(); 1489 assertTrue(this.server1.isClosed()); 1490 } 1491 if (null != this.server2) { 1492 this.server2.close(); 1493 assertTrue(this.server2.isClosed()); 1494 } 1495 } 1496 1497 private void ensureServerOpen() throws IOException { 1498 ensureServerClosed(); 1499 this.server1 = new ServerSocket(0); 1500 this.localAddr1 = new InetSocketAddress("127.0.0.1", server1.getLocalPort()); 1501 this.server2 = new ServerSocket(0); 1502 this.localAddr2 = new InetSocketAddress("127.0.0.1", server2.getLocalPort()); 1503 assertTrue(this.server1.isBound()); 1504 assertTrue(this.server2.isBound()); 1505 } 1506 1507 private void connectNoServerNonBlock() throws Exception { 1508 // ensure 1509 ensureServerClosed(); 1510 this.channel1.configureBlocking(false); 1511 statusNotConnected_NotPending(); 1512 // connect 1513 assertFalse(this.channel1.connect(localAddr1)); 1514 statusNotConnected_Pending(); 1515 try { 1516 assertFalse(this.channel1.finishConnect()); 1517 statusNotConnected_Pending(); 1518 } catch (ConnectException e) { 1519 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 1520 } 1521 } 1522 1523 private void connectServerNonBlock() throws Exception { 1524 // ensure 1525 ensureServerOpen(); 1526 this.channel1.configureBlocking(false); 1527 statusNotConnected_NotPending(); 1528 // connect 1529 boolean connected = channel1.connect(localAddr1); 1530 if (!connected) { 1531 statusNotConnected_Pending(); 1532 } 1533 tryFinish(); 1534 } 1535 1536 private void connectServerBlock() throws Exception { 1537 // ensure 1538 ensureServerOpen(); 1539 assertTrue(this.channel1.isBlocking()); 1540 statusNotConnected_NotPending(); 1541 // connect 1542 assertTrue(this.channel1.connect(localAddr1)); 1543 statusConnected_NotPending(); 1544 tryFinish(); 1545 } 1546 1547 private void statusChannelClosed() { 1548 assertFalse(this.channel1.isConnected()); 1549 assertFalse(this.channel1.isConnectionPending()); 1550 assertFalse(this.channel1.isOpen()); 1551 } 1552 1553 private void statusNotConnected_NotPending() { 1554 assertFalse(this.channel1.isConnected()); 1555 assertFalse(this.channel1.isConnectionPending()); 1556 assertTrue(this.channel1.isOpen()); 1557 } 1558 1559 private void statusNotConnected_Pending() { 1560 assertFalse(this.channel1.isConnected()); 1561 assertTrue(this.channel1.isConnectionPending()); 1562 assertTrue(this.channel1.isOpen()); 1563 } 1564 1565 private void statusConnected_NotPending() { 1566 assertTrue(this.channel1.isConnected()); 1567 assertFalse(this.channel1.isConnectionPending()); 1568 assertTrue(this.channel1.isOpen()); 1569 } 1570 1571 private boolean tryFinish() throws IOException { 1572 /* 1573 * the result of finish will be asserted in multi-thread tests. 1574 */ 1575 boolean connected = false; 1576 assertTrue(this.channel1.isOpen()); 1577 try { 1578 connected = this.channel1.finishConnect(); 1579 } catch (SocketException e) { 1580 // Finish connection failed, probably due to reset by peer error. 1581 } 1582 if (connected) { 1583 statusConnected_NotPending(); 1584 } 1585 return connected; 1586 } 1587 1588 // ------------------------------------------------------------------- 1589 // Original tests. Test method for CFII with real data. 1590 // ------------------------------------------------------------------- 1591 1592 /** 1593 * 1594 * 'SocketChannelImpl.connect(SocketAddress)' 1595 */ 1596 public void testCFII_Data_ConnectWithServer() throws Exception { 1597 ensureServerOpen(); 1598 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer 1599 .allocate(CAPACITY_NORMAL); 1600 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1]; 1601 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 1602 assertFalse(this.channel1.isRegistered()); 1603 assertTrue(this.channel1.isBlocking()); 1604 1605 this.channel1.connect(localAddr1); 1606 1607 assertTrue(this.channel1.isBlocking()); 1608 assertTrue(this.channel1.isConnected()); 1609 assertFalse(this.channel1.isConnectionPending()); 1610 assertTrue(this.channel1.isOpen()); 1611 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf)); 1612 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1)); 1613 1614 this.channel1.configureBlocking(false); 1615 try { 1616 this.channel1.connect(localAddr1); 1617 fail("Should throw AlreadyConnectedException"); 1618 } catch (AlreadyConnectedException e) { 1619 // correct 1620 } 1621 1622 assertFalse(this.channel1.isRegistered()); 1623 tryFinish(); 1624 } 1625 1626 /* 1627 * Test method for 'SocketChannelImpl.connect(SocketAddress)' 1628 */ 1629 public void testCFII_Data_ConnectWithServer_nonBlocking() throws Exception { 1630 ensureServerOpen(); 1631 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer 1632 .allocate(CAPACITY_NORMAL); 1633 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1]; 1634 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 1635 assertFalse(this.channel1.isRegistered()); 1636 assertTrue(this.channel1.isBlocking()); 1637 this.channel1.configureBlocking(false); 1638 this.channel1.connect(localAddr1); 1639 1640 assertFalse(this.channel1.isBlocking()); 1641 boolean connected = channel1.isConnected(); 1642 if (!connected) { 1643 assertTrue(this.channel1.isConnectionPending()); 1644 assertTrue(this.channel1.isOpen()); 1645 } 1646 if (tryFinish()) { 1647 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf)); 1648 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1)); 1649 1650 this.channel1.configureBlocking(false); 1651 try { 1652 this.channel1.connect(localAddr1); 1653 fail("Should throw AlreadyConnectedException"); 1654 } catch (AlreadyConnectedException e) { 1655 // correct 1656 } 1657 } 1658 1659 assertFalse(this.channel1.isRegistered()); 1660 tryFinish(); 1661 } 1662 1663 /* 1664 * Test method for 'SocketChannelImpl.finishConnect()' 1665 */ 1666 public void testCFII_Data_FinishConnect_nonBlocking() throws IOException { 1667 ensureServerOpen(); 1668 1669 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer 1670 .allocate(CAPACITY_NORMAL); 1671 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1]; 1672 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 1673 1674 this.channel1.configureBlocking(false); 1675 try { 1676 this.channel1.finishConnect(); 1677 fail("Should throw NoConnectionPendingException"); 1678 } catch (NoConnectionPendingException e) { 1679 // correct 1680 } 1681 boolean connected = channel1.connect(localAddr1); 1682 if (!connected) { 1683 assertFalse(this.channel1.isBlocking()); 1684 assertFalse(this.channel1.isConnected()); 1685 assertTrue(this.channel1.isConnectionPending()); 1686 assertTrue(this.channel1.isOpen()); 1687 } 1688 this.server1.accept(); 1689 if (tryFinish()) { 1690 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf)); 1691 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1)); 1692 try { 1693 this.channel1.connect(localAddr1); 1694 fail("Should throw AlreadyConnectedException"); 1695 } catch (AlreadyConnectedException e) { 1696 // correct 1697 } 1698 } 1699 assertFalse(this.channel1.isRegistered()); 1700 tryFinish(); 1701 } 1702 1703 public void testCFII_Data_FinishConnect_AddrSetServerStartLater() 1704 throws IOException, InterruptedException { 1705 ensureServerClosed(); 1706 this.channel1.configureBlocking(false); 1707 try { 1708 SocketChannel.open(localAddr1); 1709 fail("Should throw ConnectException"); 1710 } catch (ConnectException e) { 1711 // correct 1712 } 1713 assertTrue(this.channel1.isOpen()); 1714 assertFalse(this.channel1.isBlocking()); 1715 assertFalse(this.channel1.isConnectionPending()); 1716 this.channel1.configureBlocking(true); 1717 try { 1718 this.channel1.finishConnect(); 1719 fail("Should throw NoConnectionPendingException"); 1720 } catch (NoConnectionPendingException e) { 1721 // correct 1722 } 1723 try { 1724 this.channel1.connect(localAddr2); 1725 fail("Should throw ConnectException"); 1726 } catch (ConnectException e) { 1727 // correct 1728 } 1729 1730 assertTrue(this.channel1.isBlocking()); 1731 try { 1732 this.channel1.finishConnect(); 1733 fail("Should throw ClosedChannelException"); 1734 } catch (ClosedChannelException e) { 1735 // correct 1736 } 1737 assertFalse(this.channel1.isConnected()); 1738 // finish after finish OK 1739 assertFalse(this.channel1.isConnectionPending()); 1740 this.channel1 = SocketChannel.open(); 1741 this.channel1.configureBlocking(false); 1742 this.channel1.connect(localAddr1); 1743 assertFalse(this.channel1.isConnected()); 1744 ensureServerOpen(); 1745 // cannot connect? 1746 try { 1747 assertFalse(this.channel1.finishConnect()); 1748 assertFalse(this.channel1.isBlocking()); 1749 assertFalse(this.channel1.isConnected()); 1750 assertTrue(this.channel1.isConnectionPending()); 1751 assertTrue(this.channel1.isOpen()); 1752 try { 1753 this.channel1.connect(localAddr1); 1754 fail("Should throw ConnectionPendingException"); 1755 } catch (ConnectionPendingException e) { 1756 // correct 1757 } 1758 this.channel1.configureBlocking(true); 1759 try { 1760 this.channel1.connect(localAddr1); 1761 fail("Should throw ConnectionPendingException"); 1762 } catch (ConnectionPendingException e) { 1763 // correct 1764 } 1765 tryFinish(); 1766 } catch (ConnectException e) { 1767 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 1768 } 1769 } 1770 1771 public void testCFII_Data_FinishConnect_ServerStartLater() 1772 throws IOException { 1773 ensureServerClosed(); 1774 this.channel1.configureBlocking(true); 1775 try { 1776 this.channel1.finishConnect(); 1777 fail("Should throw NoConnectionPendingException"); 1778 } catch (NoConnectionPendingException e) { 1779 // correct 1780 } 1781 try { 1782 this.channel1.connect(localAddr1); 1783 fail("Should throw ConnectException"); 1784 } catch (ConnectException e) { 1785 // correct 1786 } 1787 1788 try { 1789 this.channel1.finishConnect(); 1790 fail("Should throw ClosedChannelException"); 1791 } catch (ClosedChannelException e) { 1792 // correct 1793 } 1794 assertFalse(this.channel1.isConnected()); 1795 // finish after finish OK 1796 assertFalse(this.channel1.isConnectionPending()); 1797 this.channel1 = SocketChannel.open(); 1798 this.channel1.configureBlocking(false); 1799 this.channel1.connect(localAddr1); 1800 assertFalse(this.channel1.isConnected()); 1801 ensureServerOpen(); 1802 // cannot connect? 1803 try { 1804 assertFalse(this.channel1.finishConnect()); 1805 assertFalse(this.channel1.isBlocking()); 1806 assertFalse(this.channel1.isConnected()); 1807 assertTrue(this.channel1.isConnectionPending()); 1808 assertTrue(this.channel1.isOpen()); 1809 try { 1810 this.channel1.connect(localAddr1); 1811 fail("Should throw ConnectionPendingException"); 1812 } catch (ConnectionPendingException e) { 1813 // correct 1814 } 1815 this.channel1.configureBlocking(true); 1816 try { 1817 this.channel1.connect(localAddr1); 1818 fail("Should throw ConnectionPendingException"); 1819 } catch (ConnectionPendingException e) { 1820 // correct 1821 } 1822 tryFinish(); 1823 } catch (ConnectException e) { 1824 // FIXME: assertEquals(e.getMessage(), "Connection refused"); 1825 } 1826 } 1827 1828 public void testCFII_Data_FinishConnect_Blocking() throws IOException { 1829 ensureServerOpen(); 1830 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer 1831 .allocate(CAPACITY_NORMAL); 1832 java.nio.ByteBuffer[] writeBufArr = new java.nio.ByteBuffer[1]; 1833 writeBufArr[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 1834 this.channel1.configureBlocking(true); 1835 try { 1836 this.channel1.finishConnect(); 1837 fail("Should throw NoConnectionPendingException"); 1838 } catch (NoConnectionPendingException e) { 1839 // correct 1840 } 1841 1842 this.channel1.connect(localAddr1); 1843 1844 assertTrue(this.channel1.isConnected()); 1845 assertFalse(this.channel1.isConnectionPending()); 1846 assertTrue(this.channel1.isOpen()); 1847 if (tryFinish()) { 1848 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf)); 1849 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBufArr, 0, 1)); 1850 1851 try { 1852 this.channel1.connect(localAddr1); 1853 fail("Should throw AlreadyConnectedException"); 1854 } catch (AlreadyConnectedException e) { 1855 // correct 1856 } 1857 } 1858 assertFalse(this.channel1.isRegistered()); 1859 tryFinish(); 1860 } 1861 1862 /** 1863 * Regression test for Harmony-1947. 1864 */ 1865 public void test_finishConnect() throws Exception { 1866 SocketAddress address = new InetSocketAddress("localhost", 0); 1867 1868 ServerSocketChannel theServerChannel = ServerSocketChannel.open(); 1869 ServerSocket serversocket = theServerChannel.socket(); 1870 serversocket.setReuseAddress(true); 1871 // Bind the socket 1872 theServerChannel.socket().bind(address); 1873 1874 boolean doneNonBlockingConnect = false; 1875 // Loop so that we make sure we're definitely testing finishConnect() 1876 while (!doneNonBlockingConnect) { 1877 channel1 = SocketChannel.open(); 1878 1879 // Set the SocketChannel to non-blocking so that connect(..) does 1880 // not block 1881 channel1.configureBlocking(false); 1882 boolean connected = channel1.connect(new InetSocketAddress("localhost",serversocket.getLocalPort())); 1883 if (!connected) { 1884 // Now set the SocketChannel back to blocking so that 1885 // finishConnect() blocks. 1886 channel1.configureBlocking(true); 1887 doneNonBlockingConnect = channel1.finishConnect(); 1888 } 1889 if (doneNonBlockingConnect) { 1890 tryFinish(); 1891 } 1892 channel1.close(); 1893 } 1894 if (!serversocket.isClosed()) { 1895 serversocket.close(); 1896 } 1897 } 1898 1899 // ------------------------------------------------------------------- 1900 // End of original tests. Test method for CFII with real data. 1901 // ------------------------------------------------------------------- 1902 1903 /** 1904 * @tests java.nio.channels.SocketChannel#read(ByteBuffer) 1905 */ 1906 public void test_readLjava_nio_ByteBuffer_Blocking() throws IOException { 1907 // initialize write content 1908 byte[] writeContent = new byte[CAPACITY_NORMAL]; 1909 for (int i = 0; i < writeContent.length; i++) { 1910 writeContent[i] = (byte) i; 1911 } 1912 // establish connection 1913 channel1.connect(localAddr1); 1914 Socket acceptedSocket = server1.accept(); 1915 1916 // use OutputStream.write to send CAPACITY_NORMAL bytes data 1917 OutputStream out = acceptedSocket.getOutputStream(); 1918 out.write(writeContent); 1919 // use close to guarantee all data is sent 1920 acceptedSocket.close(); 1921 1922 ByteBuffer readContent = ByteBuffer.allocate(CAPACITY_NORMAL + 1); 1923 int totalCount = 0; 1924 int count; 1925 long startTime = System.currentTimeMillis(); 1926 // use SocketChannel.read to read data 1927 while (totalCount <= CAPACITY_NORMAL) { 1928 count = channel1.read(readContent); 1929 if (EOF == count) { 1930 break; 1931 } 1932 totalCount += count; 1933 // if the channel could not finish reading in TIMEOUT ms, the 1934 // test fails. It is used to guarantee the test never hangs even 1935 // if there are bugs of SocketChannel implementation. For 1936 // blocking read, it possibly returns 0 in some cases. 1937 assertTimeout(startTime, TIMEOUT); 1938 } 1939 assertEquals(CAPACITY_NORMAL, totalCount); 1940 readContent.flip(); 1941 for (int i = 0; i < CAPACITY_NORMAL; i++) { 1942 assertEquals(writeContent[i], readContent.get()); 1943 } 1944 } 1945 1946 /** 1947 * @tests java.nio.channels.SocketChannel#read(ByteBuffer) 1948 */ 1949 public void test_readLjava_nio_ByteBuffer_Nonblocking() throws IOException { 1950 // initialize write content 1951 byte[] writeContent = new byte[CAPACITY_NORMAL]; 1952 for (int i = 0; i < writeContent.length; i++) { 1953 writeContent[i] = (byte) i; 1954 } 1955 1956 // establish connection 1957 channel1.connect(localAddr1); 1958 Socket acceptedSocket = server1.accept(); 1959 // use OutputStream.write to write CAPACITY_NORMAL bytes data. 1960 OutputStream out = acceptedSocket.getOutputStream(); 1961 out.write(writeContent); 1962 // use close to guarantee all data is sent 1963 acceptedSocket.close(); 1964 1965 channel1.configureBlocking(false); 1966 ByteBuffer readContent = ByteBuffer.allocate(CAPACITY_NORMAL + 1); 1967 int totalCount = 0; 1968 int count; 1969 long startTime = System.currentTimeMillis(); 1970 // use SocketChannel.read to read data 1971 while (totalCount <= CAPACITY_NORMAL) { 1972 count = channel1.read(readContent); 1973 if (EOF == count) { 1974 break; 1975 } 1976 totalCount += count; 1977 // if the channel could not finish reading in TIMEOUT ms, the 1978 // test fails. It is used to guarantee the test never hangs even 1979 // if there are bugs of SocketChannel implementation. 1980 assertTimeout(startTime, TIMEOUT); 1981 } 1982 1983 // assert read content 1984 assertEquals(CAPACITY_NORMAL, totalCount); 1985 assertEquals(CAPACITY_NORMAL, readContent.position()); 1986 readContent.flip(); 1987 for (int i = 0; i < CAPACITY_NORMAL; i++) { 1988 assertEquals(writeContent[i], readContent.get()); 1989 } 1990 } 1991 1992 /** 1993 * @tests java.nio.channels.SocketChannel#write(ByteBuffer) 1994 */ 1995 public void test_writeLjava_nio_ByteBuffer_Blocking() throws IOException { 1996 // initialize write content 1997 ByteBuffer writeContent = ByteBuffer.allocate(CAPACITY_NORMAL); 1998 for (int i = 0; i < CAPACITY_NORMAL; i++) { 1999 writeContent.put((byte) i); 2000 } 2001 writeContent.flip(); 2002 // establish connection 2003 channel1.connect(localAddr1); 2004 Socket acceptedSocket = server1.accept(); 2005 2006 // use SocketChannel.write(ByteBuffer) to write CAPACITY_NORMAL bytes 2007 // data 2008 int writtenCount = channel1.write(writeContent); 2009 // assert written count and ByteBuffer position 2010 assertEquals(CAPACITY_NORMAL, writtenCount); 2011 assertEquals(CAPACITY_NORMAL, writeContent.position()); 2012 // use close to guarantee all data is sent 2013 channel1.close(); 2014 2015 InputStream in = acceptedSocket.getInputStream(); 2016 int totalCount = 0; 2017 int count = 0; 2018 byte[] readContent = new byte[CAPACITY_NORMAL + 1]; 2019 // if the channel could not finish reading in TIMEOUT ms, the test 2020 // fails. It is used to guarantee the test never hangs even if there 2021 // are bugs of SocketChannel implementation. 2022 acceptedSocket.setSoTimeout(TIMEOUT); 2023 2024 // use InputStream.read to read data. 2025 while (totalCount <= CAPACITY_NORMAL) { 2026 count = in.read(readContent, totalCount, readContent.length 2027 - totalCount); 2028 if (EOF == count) { 2029 break; 2030 } 2031 totalCount += count; 2032 } 2033 2034 // assert read content 2035 assertEquals(CAPACITY_NORMAL, totalCount); 2036 writeContent.flip(); 2037 for (int i = 0; i < CAPACITY_NORMAL; i++) { 2038 assertEquals(writeContent.get(), readContent[i]); 2039 } 2040 } 2041 2042 /** 2043 * @tests java.nio.channels.SocketChannel#write(ByteBuffer) 2044 */ 2045 public void test_writeLjava_nio_ByteBuffer_NonBlocking() throws Exception { 2046 // initialize write content 2047 ByteBuffer writeContent = ByteBuffer.allocate(CAPACITY_NORMAL); 2048 for (int i = 0; i < CAPACITY_NORMAL; i++) { 2049 writeContent.put((byte) i); 2050 } 2051 writeContent.flip(); 2052 2053 // establish connection 2054 channel1.connect(localAddr1); 2055 Socket acceptedSocket = server1.accept(); 2056 2057 channel1.configureBlocking(false); 2058 int writtenTotalCount = 0; 2059 int writtenCount = 0; 2060 long startTime = System.currentTimeMillis(); 2061 // use SocketChannel.write(ByteBuffer) to write CAPACITY_NORMAL bytes 2062 while (writtenTotalCount < CAPACITY_NORMAL) { 2063 writtenCount = channel1.write(writeContent); 2064 writtenTotalCount += writtenCount; 2065 // if the channel could not finish writing in TIMEOUT ms, the 2066 // test fails. It is used to guarantee the test never hangs even 2067 // if there are bugs of SocketChannel implementation. 2068 assertTimeout(startTime, TIMEOUT); 2069 } 2070 // assert written count and ByteBuffer position 2071 assertEquals(CAPACITY_NORMAL, writtenTotalCount); 2072 assertEquals(CAPACITY_NORMAL, writeContent.position()); 2073 // use close to guarantee all data is sent 2074 channel1.close(); 2075 2076 InputStream in = acceptedSocket.getInputStream(); 2077 byte[] readContent = new byte[CAPACITY_NORMAL + 1]; 2078 int totalCount = 0; 2079 int count = 0; 2080 // if the channel could not finish reading in TIMEOUT ms, the test 2081 // fails. It is used to guarantee the test never hangs even if there 2082 // are bugs of SocketChannel implementation. 2083 acceptedSocket.setSoTimeout(TIMEOUT); 2084 // use InputStream.read to read data. 2085 while (totalCount <= CAPACITY_NORMAL) { 2086 count = in.read(readContent, totalCount, readContent.length 2087 - totalCount); 2088 if (EOF == count) { 2089 break; 2090 } 2091 totalCount += count; 2092 } 2093 // assert read content 2094 assertEquals(CAPACITY_NORMAL, totalCount); 2095 writeContent.flip(); 2096 for (int i = 0; i < CAPACITY_NORMAL; i++) { 2097 assertEquals(writeContent.get(), readContent[i]); 2098 } 2099 } 2100 2101 /* 2102 * Fails if the difference between current time and start time is greater 2103 * than timeout. 2104 */ 2105 private void assertTimeout(long startTime, long timeout) { 2106 long currentTime = System.currentTimeMillis(); 2107 if ((currentTime - startTime) > timeout) { 2108 fail("Timeout"); 2109 } 2110 } 2111 2112 // ------------------------------------------------- 2113 // Test for read/write but no real data expressed 2114 // ------------------------------------------------- 2115 2116 public void testReadByteBuffer() throws Exception { 2117 assertTrue(this.server1.isBound()); 2118 java.nio.ByteBuffer readBuf = java.nio.ByteBuffer 2119 .allocate(CAPACITY_NORMAL); 2120 assertFalse(this.channel1.isRegistered()); 2121 assertTrue(this.channel1.isBlocking()); 2122 assertFalse(this.channel1.isConnected()); 2123 assertFalse(this.channel1.isConnectionPending()); 2124 assertTrue(this.channel1.isOpen()); 2125 // note: blocking-mode will make the read process endless! 2126 this.channel1.configureBlocking(false); 2127 try { 2128 channel1.read(readBuf); 2129 fail("Should throw NotYetConnectedException"); 2130 } catch (NotYetConnectedException e) { 2131 // correct 2132 } 2133 boolean connected = this.channel1.connect(localAddr1); 2134 if (!connected) { 2135 assertFalse(this.channel1.isBlocking()); 2136 assertTrue(this.channel1.isConnectionPending()); 2137 assertFalse(this.channel1.isConnected()); 2138 } 2139 if (tryFinish()) { 2140 assertEquals(0, this.channel1.read(readBuf)); 2141 } 2142 2143 this.channel1.close(); 2144 try { 2145 channel1.read(readBuf); 2146 fail("Should throw ClosedChannelException"); 2147 } catch (ClosedChannelException e) { 2148 // correct 2149 } 2150 } 2151 2152 public void testReadByteBuffer_Direct() throws Exception { 2153 assertTrue(this.server1.isBound()); 2154 java.nio.ByteBuffer readBuf = java.nio.ByteBuffer 2155 .allocateDirect(CAPACITY_NORMAL); 2156 assertFalse(this.channel1.isRegistered()); 2157 assertTrue(this.channel1.isBlocking()); 2158 assertFalse(this.channel1.isConnected()); 2159 assertFalse(this.channel1.isConnectionPending()); 2160 assertTrue(this.channel1.isOpen()); 2161 // note: blocking-mode will make the read process endless! 2162 this.channel1.configureBlocking(false); 2163 try { 2164 channel1.read(readBuf); 2165 fail("Should throw NotYetConnectedException"); 2166 } catch (NotYetConnectedException e) { 2167 // correct 2168 } 2169 boolean connected = this.channel1.connect(localAddr1); 2170 if (!connected) { 2171 assertFalse(this.channel1.isBlocking()); 2172 assertTrue(this.channel1.isConnectionPending()); 2173 assertFalse(this.channel1.isConnected()); 2174 } 2175 if (tryFinish()) { 2176 assertEquals(0, this.channel1.read(readBuf)); 2177 } 2178 2179 this.channel1.close(); 2180 try { 2181 channel1.read(readBuf); 2182 fail("Should throw ClosedChannelException"); 2183 } catch (ClosedChannelException e) { 2184 // correct 2185 } 2186 } 2187 2188 public void testReadByteBuffer_Direct2() throws IOException { 2189 byte[] request = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 2190 ByteBuffer buffer = ByteBuffer.allocateDirect(128); 2191 2192 ServerSocketChannel server = ServerSocketChannel.open(); 2193 server.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 5); 2194 Socket client = new Socket(InetAddress.getLocalHost(), server.socket() 2195 .getLocalPort()); 2196 client.setTcpNoDelay(false); 2197 Socket worker = server.socket().accept(); 2198 SocketChannel workerChannel = worker.getChannel(); 2199 2200 OutputStream out = client.getOutputStream(); 2201 out.write(request); 2202 out.close(); 2203 2204 buffer.limit(5); 2205 int bytesRead = workerChannel.read(buffer); 2206 assertEquals(5, bytesRead); 2207 assertEquals(5, buffer.position()); 2208 2209 buffer.limit(request.length); 2210 bytesRead = workerChannel.read(buffer); 2211 assertEquals(6, bytesRead); 2212 2213 buffer.flip(); 2214 assertEquals(request.length, buffer.limit()); 2215 2216 assertEquals(ByteBuffer.wrap(request), buffer); 2217 2218 client.close(); 2219 worker.close(); 2220 server.close(); 2221 } 2222 2223 public void testReadByteBuffer_BufNull() throws Exception { 2224 assertTrue(this.server1.isBound()); 2225 java.nio.ByteBuffer readBuf = java.nio.ByteBuffer.allocate(0); 2226 // note: blocking-mode will make the read process endless! 2227 this.channel1.configureBlocking(false); 2228 try { 2229 channel1.read((java.nio.ByteBuffer) null); 2230 fail("Should throw NPE"); 2231 } catch (NullPointerException e) { 2232 // correct 2233 } 2234 this.channel1.connect(localAddr1); 2235 if (tryFinish()) { 2236 try { 2237 this.channel1.read((java.nio.ByteBuffer) null); 2238 fail("Should throw NPE"); 2239 } catch (NullPointerException e) { 2240 // correct 2241 } 2242 assertEquals(0, this.channel1.read(readBuf)); 2243 } 2244 this.server1.close(); 2245 try { 2246 channel1.read((java.nio.ByteBuffer) null); 2247 fail("Should throw NPE"); 2248 } catch (NullPointerException e) { 2249 // correct 2250 } 2251 } 2252 2253 /* 2254 * SocketChannelImpl.read(ByteBuffer[], int, int)' 2255 */ 2256 public void testReadByteBufferArrayIntInt() throws Exception { 2257 assertTrue(this.server1.isBound()); 2258 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[2]; 2259 readBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 2260 readBuf[1] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 2261 assertFalse(this.channel1.isRegistered()); 2262 assertTrue(this.channel1.isBlocking()); 2263 assertFalse(this.channel1.isConnected()); 2264 assertFalse(this.channel1.isConnectionPending()); 2265 assertTrue(this.channel1.isOpen()); 2266 // note: blocking-mode will make the read process endless! 2267 this.channel1.configureBlocking(false); 2268 try { 2269 channel1.read(readBuf, 0, 1); 2270 fail("Should throw NotYetConnectedException"); 2271 } catch (NotYetConnectedException e) { 2272 // correct 2273 } 2274 boolean connected = this.channel1.connect(localAddr1); 2275 if (!connected) { 2276 assertFalse(this.channel1.isBlocking()); 2277 assertTrue(this.channel1.isConnectionPending()); 2278 assertFalse(this.channel1.isConnected()); 2279 } 2280 if (tryFinish()) { 2281 assertEquals(0, this.channel1.read(readBuf, 0, 1)); 2282 assertEquals(0, this.channel1.read(readBuf, 0, 2)); 2283 } 2284 2285 this.channel1.close(); 2286 try { 2287 channel1.read(readBuf, 0, 1); 2288 fail("Should throw ClosedChannelException"); 2289 } catch (ClosedChannelException e) { 2290 // correct 2291 } 2292 } 2293 2294 /* 2295 * SocketChannelImpl.read(ByteBuffer[], int, int)' 2296 */ 2297 public void testReadByteBufferArrayIntInt_Direct() throws Exception { 2298 assertTrue(this.server1.isBound()); 2299 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[2]; 2300 readBuf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2301 readBuf[1] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2302 assertFalse(this.channel1.isRegistered()); 2303 assertTrue(this.channel1.isBlocking()); 2304 assertFalse(this.channel1.isConnected()); 2305 assertFalse(this.channel1.isConnectionPending()); 2306 assertTrue(this.channel1.isOpen()); 2307 // note: blocking-mode will make the read process endless! 2308 this.channel1.configureBlocking(false); 2309 try { 2310 channel1.read(readBuf, 0, 1); 2311 fail("Should throw NotYetConnectedException"); 2312 } catch (NotYetConnectedException e) { 2313 // correct 2314 } 2315 boolean connected = this.channel1.connect(localAddr1); 2316 if (!connected) { 2317 assertFalse(this.channel1.isBlocking()); 2318 assertTrue(this.channel1.isConnectionPending()); 2319 assertFalse(this.channel1.isConnected()); 2320 } 2321 if (tryFinish()) { 2322 assertEquals(0, this.channel1.read(readBuf, 0, 1)); 2323 assertEquals(0, this.channel1.read(readBuf, 0, 2)); 2324 } 2325 2326 this.channel1.close(); 2327 try { 2328 channel1.read(readBuf, 0, 1); 2329 fail("Should throw ClosedChannelException"); 2330 } catch (ClosedChannelException e) { 2331 // correct 2332 } 2333 } 2334 2335 public void testReadByteBufferArrayIntInt_BufNull() throws Exception { 2336 assertTrue(this.server1.isBound()); 2337 java.nio.ByteBuffer[] readBuf = new java.nio.ByteBuffer[2]; 2338 readBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 2339 assertFalse(this.channel1.isRegistered()); 2340 assertTrue(this.channel1.isBlocking()); 2341 assertFalse(this.channel1.isConnected()); 2342 assertFalse(this.channel1.isConnectionPending()); 2343 assertTrue(this.channel1.isOpen()); 2344 // note: blocking-mode will make the read process endless! 2345 this.channel1.configureBlocking(false); 2346 try { 2347 channel1.read(null, 0, 1); 2348 fail("Should throw NPE"); 2349 } catch (NullPointerException e) { 2350 // correct 2351 } 2352 this.channel1.connect(localAddr1); 2353 if (tryFinish()) { 2354 2355 try { 2356 channel1.read(null, 0, 1); 2357 fail("Should throw NPE"); 2358 } catch (NullPointerException e) { 2359 // correct 2360 } 2361 try { 2362 channel1.read(readBuf, 0, 2); 2363 fail("Should throw NPE"); 2364 } catch (NullPointerException e) { 2365 // correct 2366 } 2367 2368 assertEquals(0, this.channel1.read(readBuf, 0, 1)); 2369 } 2370 this.channel1.close(); 2371 try { 2372 channel1.read(null, 0, 1); 2373 fail("Should throw NPE"); 2374 } catch (NullPointerException e) { 2375 // correct 2376 } 2377 } 2378 2379 public void testWriteByteBuffer() throws IOException { 2380 assertTrue(this.server1.isBound()); 2381 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer 2382 .allocate(CAPACITY_NORMAL); 2383 assertFalse(this.channel1.isRegistered()); 2384 assertTrue(this.channel1.isBlocking()); 2385 assertFalse(this.channel1.isConnected()); 2386 assertFalse(this.channel1.isConnectionPending()); 2387 assertTrue(this.channel1.isOpen()); 2388 try { 2389 channel1.write(writeBuf); 2390 fail("Should throw NotYetConnectedException"); 2391 } catch (NotYetConnectedException e) { 2392 // correct 2393 } 2394 this.channel1.connect(localAddr1); 2395 assertTrue(this.channel1.isBlocking()); 2396 assertTrue(this.channel1.isConnected()); 2397 assertFalse(this.channel1.isConnectionPending()); 2398 assertTrue(this.channel1.isOpen()); 2399 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf)); 2400 2401 this.channel1.close(); 2402 try { 2403 channel1.write(writeBuf); 2404 fail("Should throw ClosedChannelException"); 2405 } catch (ClosedChannelException e) { 2406 // correct 2407 } 2408 } 2409 2410 public void testWriteByteBuffer_Direct() throws IOException { 2411 assertTrue(this.server1.isBound()); 2412 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer 2413 .allocateDirect(CAPACITY_NORMAL); 2414 assertFalse(this.channel1.isRegistered()); 2415 assertTrue(this.channel1.isBlocking()); 2416 assertFalse(this.channel1.isConnected()); 2417 assertFalse(this.channel1.isConnectionPending()); 2418 assertTrue(this.channel1.isOpen()); 2419 try { 2420 channel1.write(writeBuf); 2421 fail("Should throw NotYetConnectedException"); 2422 } catch (NotYetConnectedException e) { 2423 // correct 2424 } 2425 this.channel1.connect(localAddr1); 2426 assertTrue(this.channel1.isBlocking()); 2427 assertTrue(this.channel1.isConnected()); 2428 assertFalse(this.channel1.isConnectionPending()); 2429 assertTrue(this.channel1.isOpen()); 2430 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf)); 2431 2432 this.channel1.close(); 2433 try { 2434 channel1.write(writeBuf); 2435 fail("Should throw ClosedChannelException"); 2436 } catch (ClosedChannelException e) { 2437 // correct 2438 } 2439 } 2440 2441 public void testWriteByteBuffer_BufNull() throws IOException { 2442 assertTrue(this.server1.isBound()); 2443 java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer.allocate(0); 2444 this.channel1.connect(localAddr1); 2445 assertEquals(this.channel1.write(writeBuf), 0); 2446 try { 2447 this.channel1.write((java.nio.ByteBuffer) null); 2448 fail("Should throw NPE"); 2449 } catch (NullPointerException e) { 2450 // correct 2451 } 2452 } 2453 2454 /* 2455 * SocketChannelImpl.write(ByteBuffer[], int, int)' 2456 */ 2457 public void testWriteByteBufferArrayIntInt() throws IOException { 2458 java.nio.ByteBuffer[] writeBuf = new java.nio.ByteBuffer[2]; 2459 writeBuf[0] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 2460 writeBuf[1] = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL); 2461 assertFalse(this.channel1.isRegistered()); 2462 assertTrue(this.channel1.isBlocking()); 2463 assertFalse(this.channel1.isConnected()); 2464 assertFalse(this.channel1.isConnectionPending()); 2465 assertTrue(this.channel1.isOpen()); 2466 try { 2467 channel1.write(writeBuf, 0, 1); 2468 fail("Should throw NotYetConnectedException"); 2469 } catch (NotYetConnectedException e) { 2470 // correct 2471 } 2472 this.channel1.connect(localAddr1); 2473 assertTrue(this.channel1.isBlocking()); 2474 assertTrue(this.channel1.isConnected()); 2475 assertFalse(this.channel1.isConnectionPending()); 2476 assertTrue(this.channel1.isOpen()); 2477 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 1)); 2478 // still writes the same size as above 2479 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 2)); 2480 writeBuf[0].flip(); 2481 writeBuf[1].flip(); 2482 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 2483 this.channel1.close(); 2484 try { 2485 channel1.write(writeBuf); 2486 fail("Should throw ClosedChannelException"); 2487 } catch (ClosedChannelException e) { 2488 // correct 2489 } 2490 } 2491 2492 /* 2493 * SocketChannelImpl.write(ByteBuffer[], int, int)' 2494 */ 2495 public void testWriteByteBufferArrayIntInt_Direct() throws IOException { 2496 java.nio.ByteBuffer[] writeBuf = new java.nio.ByteBuffer[2]; 2497 writeBuf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2498 writeBuf[1] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2499 assertFalse(this.channel1.isRegistered()); 2500 assertTrue(this.channel1.isBlocking()); 2501 assertFalse(this.channel1.isConnected()); 2502 assertFalse(this.channel1.isConnectionPending()); 2503 assertTrue(this.channel1.isOpen()); 2504 try { 2505 channel1.write(writeBuf, 0, 1); 2506 fail("Should throw NotYetConnectedException"); 2507 } catch (NotYetConnectedException e) { 2508 // correct 2509 } 2510 this.channel1.connect(localAddr1); 2511 assertTrue(this.channel1.isBlocking()); 2512 assertTrue(this.channel1.isConnected()); 2513 assertFalse(this.channel1.isConnectionPending()); 2514 assertTrue(this.channel1.isOpen()); 2515 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 1)); 2516 // still writes the same size as above 2517 assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf, 0, 2)); 2518 writeBuf[0].flip(); 2519 writeBuf[1].flip(); 2520 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 2521 this.channel1.close(); 2522 try { 2523 channel1.write(writeBuf); 2524 fail("Should throw ClosedChannelException"); 2525 } catch (ClosedChannelException e) { 2526 // correct 2527 } 2528 } 2529 2530 public void testWriteByteBufferArrayIntInt_BufNull() throws IOException { 2531 java.nio.ByteBuffer[] writeBuf = new java.nio.ByteBuffer[0]; 2532 2533 this.channel1.connect(localAddr1); 2534 try { 2535 this.channel1.write(null, 0, 1); 2536 fail("Should throw NPE"); 2537 } catch (NullPointerException e) { 2538 // correct 2539 } 2540 assertEquals(0, this.channel1.write(writeBuf, 0, 0)); 2541 try { 2542 this.channel1.write(writeBuf, 0, 1); 2543 fail("Should throw IndexOutOfBoundsException"); 2544 } catch (IndexOutOfBoundsException e) { 2545 // correct 2546 } 2547 writeBuf = new java.nio.ByteBuffer[1]; 2548 try { 2549 this.channel1.write(writeBuf, 0, 1); 2550 fail("Should throw NPE"); 2551 } catch (NullPointerException e) { 2552 // correct 2553 } 2554 try { 2555 this.channel1.write(writeBuf, 0, 2); 2556 fail("Should throw IndexOutOfBoundsException"); 2557 } catch (IndexOutOfBoundsException e) { 2558 // correct 2559 } 2560 this.server1.close(); 2561 try { 2562 channel1.read(null, 0, 1); 2563 fail("Should throw NPE"); 2564 } catch (NullPointerException e) { 2565 // correct 2566 } 2567 } 2568 2569 public void testWriteByteBufferArrayIntInt_SizeError() throws IOException { 2570 java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1]; 2571 this.channel1.connect(localAddr1); 2572 assertEquals(0, this.channel1.write(buf, 0, 0)); 2573 try { 2574 this.channel1.write(buf, -1, 1); 2575 fail(); 2576 } catch (IndexOutOfBoundsException expected) { 2577 } 2578 try { 2579 this.channel1.write(buf, 0, -1); 2580 fail(); 2581 } catch (IndexOutOfBoundsException expected) { 2582 } 2583 try { 2584 this.channel1.write(buf, 0, 2); 2585 fail(); 2586 } catch (IndexOutOfBoundsException expected) { 2587 } 2588 try { 2589 this.channel1.write(buf, 2, 0); 2590 fail(); 2591 } catch (IndexOutOfBoundsException expected) { 2592 } 2593 try { 2594 this.channel1.write(null, 0, 0); 2595 fail(); 2596 } catch (NullPointerException expected) { 2597 } 2598 this.server1.close(); 2599 } 2600 2601 public void testReadByteBufferArrayIntInt_SizeError() throws IOException { 2602 java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1]; 2603 this.channel1.connect(localAddr1); 2604 assertEquals(0, this.channel1.read(buf, 0, 0)); 2605 try { 2606 this.channel1.read(buf, -1, 1); 2607 fail(); 2608 } catch (IndexOutOfBoundsException expected) { 2609 } 2610 try { 2611 this.channel1.read(buf, 0, -1); 2612 fail(); 2613 } catch (IndexOutOfBoundsException expected) { 2614 } 2615 try { 2616 this.channel1.read(buf, 0, 2); 2617 fail(); 2618 } catch (IndexOutOfBoundsException expected) { 2619 } 2620 try { 2621 this.channel1.read(buf, 2, 0); 2622 fail(); 2623 } catch (IndexOutOfBoundsException expected) { 2624 } 2625 try { 2626 this.channel1.read(null, 0, 0); 2627 fail(); 2628 } catch (NullPointerException expected) { 2629 } 2630 this.server1.close(); 2631 } 2632 2633 /* 2634 * ========================================================================== 2635 * Tests for read/write real data 2636 * ========================================================================== 2637 */ 2638 2639 2640 /** 2641 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[]) 2642 */ 2643 public void test_read$LByteBuffer() throws IOException { 2644 MockSocketChannel sc = new MockSocketChannel(null); 2645 ByteBuffer [] byteBufferArray = { ByteBuffer.allocate(1), ByteBuffer.allocate(1)}; 2646 // Verify that calling read(ByteBuffer[]) leads to the method 2647 // read(ByteBuffer[], int, int) being called with a 0 for the 2648 // second parameter and targets.length as the third parameter. 2649 sc.read(byteBufferArray); 2650 assertTrue(sc.isReadCalled); 2651 } 2652 /** 2653 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[],int,int) 2654 */ 2655 public void test_read$LByteBufferII_blocking() throws Exception { 2656 assert_read$LByteBuffer(true); 2657 } 2658 2659 /** 2660 * @tests java.nio.channels.SocketChannel#read(ByteBuffer[],int,int) 2661 */ 2662 public void test_read$LByteBufferII_nonblocking() throws Exception { 2663 assert_read$LByteBuffer(false); 2664 } 2665 2666 private void assert_read$LByteBuffer(boolean isBlocking) throws IOException { 2667 // initialize write content 2668 byte[] writeContent = new byte[CAPACITY_NORMAL * 2]; 2669 for (int i = 0; i < CAPACITY_NORMAL * 2; i++) { 2670 writeContent[i] = (byte) i; 2671 } 2672 ByteBuffer[] readContents = new ByteBuffer[2]; 2673 readContents[0] = ByteBuffer.allocate(CAPACITY_NORMAL); 2674 readContents[1] = ByteBuffer.allocate(CAPACITY_NORMAL + 1); 2675 // establish connection 2676 channel1.connect(localAddr1); 2677 Socket acceptedSocket = server1.accept(); 2678 // use OutputStream.write to send CAPACITY_NORMAL * 2 bytes data 2679 OutputStream out = acceptedSocket.getOutputStream(); 2680 out.write(writeContent); 2681 // use close to guarantee all data is sent 2682 acceptedSocket.close(); 2683 // configure block/nonblock mode 2684 channel1.configureBlocking(isBlocking); 2685 long startTime = System.currentTimeMillis(); 2686 long totalRead = 0; 2687 long countRead; 2688 2689 while (totalRead <= CAPACITY_NORMAL * 2) { 2690 countRead = channel1.read(readContents, 0, 2); 2691 if (0 == countRead && !readContents[1].hasRemaining()) { 2692 // read returns 0 because readContents is full 2693 break; 2694 } 2695 if (EOF == countRead) { 2696 break; 2697 } 2698 totalRead += countRead; 2699 // if the channel could not finish reading in TIMEOUT ms, the 2700 // test fails. It is used to guarantee the test never hangs even 2701 // if there are bugs of SocketChannel implementation. For 2702 // blocking read, it possibly returns 0 in some cases. 2703 assertTimeout(startTime, TIMEOUT); 2704 } 2705 2706 // assert total bytes read and the position of ByteBuffers 2707 assertEquals(CAPACITY_NORMAL * 2, totalRead); 2708 assertEquals(CAPACITY_NORMAL, readContents[0].position()); 2709 assertEquals(CAPACITY_NORMAL, readContents[1].position()); 2710 // assert read content 2711 readContents[0].flip(); 2712 readContents[1].flip(); 2713 for (int i = 0; i < CAPACITY_NORMAL; i++) { 2714 assertEquals(writeContent[i], readContents[0].get()); 2715 } 2716 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) { 2717 assertEquals(writeContent[i], readContents[1].get()); 2718 } 2719 } 2720 2721 /** 2722 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[],int,int) 2723 */ 2724 public void test_write$LByteBufferII_blocking() throws Exception { 2725 assert_write$LByteBuffer(true); 2726 } 2727 2728 /** 2729 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[],int,int) 2730 */ 2731 public void test_write$LByteBufferII_nonblocking() 2732 throws Exception { 2733 assert_write$LByteBuffer(false); 2734 } 2735 2736 private void assert_write$LByteBuffer(boolean isBlocking) 2737 throws IOException { 2738 // initialize write contents 2739 ByteBuffer writeContents[] = new ByteBuffer[2]; 2740 writeContents[0] = ByteBuffer.allocate(CAPACITY_NORMAL); 2741 writeContents[1] = ByteBuffer.allocate(CAPACITY_NORMAL); 2742 for (int i = 0; i < CAPACITY_NORMAL; i++) { 2743 writeContents[0].put((byte) i); 2744 } 2745 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) { 2746 writeContents[1].put((byte) i); 2747 } 2748 writeContents[0].flip(); 2749 writeContents[1].flip(); 2750 // establish connection 2751 channel1.connect(localAddr1); 2752 Socket acceptedSocket = server1.accept(); 2753 // set blocking/nonblocking mode 2754 channel1.configureBlocking(isBlocking); 2755 2756 assertEquals(CAPACITY_NORMAL, channel1.write(writeContents, 0, 1)); 2757 assertEquals(CAPACITY_NORMAL, channel1.write(writeContents, 1, 1)); 2758 2759 // assert written count and ByteBuffer position 2760 assertEquals(CAPACITY_NORMAL, writeContents[0].position()); 2761 assertEquals(CAPACITY_NORMAL, writeContents[1].position()); 2762 // use close to guarantee all data is sent 2763 channel1.close(); 2764 InputStream in = acceptedSocket.getInputStream(); 2765 byte[] readContent = new byte[CAPACITY_NORMAL * 2 + 1]; 2766 int totalCount = 0; 2767 int count; 2768 // if the channel could not finish reading in TIMEOUT ms, the test 2769 // fails. It is used to guarantee the test never hangs even if there 2770 // are bugs of SocketChannel implementation. 2771 acceptedSocket.setSoTimeout(TIMEOUT); 2772 // use InputStream.read to read data. 2773 while (totalCount <= CAPACITY_NORMAL) { 2774 count = in.read(readContent, totalCount, readContent.length 2775 - totalCount); 2776 if (EOF == count) { 2777 break; 2778 } 2779 totalCount += count; 2780 } 2781 // assert read content 2782 assertEquals(CAPACITY_NORMAL * 2, totalCount); 2783 writeContents[0].flip(); 2784 writeContents[1].flip(); 2785 for (int i = 0; i < CAPACITY_NORMAL; i++) { 2786 assertEquals(writeContents[0].get(), readContent[i]); 2787 } 2788 for (int i = CAPACITY_NORMAL; i < CAPACITY_NORMAL * 2; i++) { 2789 assertEquals(writeContents[1].get(), readContent[i]); 2790 } 2791 } 2792 2793 /** 2794 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2795 */ 2796 public void test_write$LByteBuffer() throws IOException { 2797 MockSocketChannel sc = new MockSocketChannel(null); 2798 ByteBuffer [] byteBufferArray = { ByteBuffer.allocate(1), ByteBuffer.allocate(1)}; 2799 // Verify that calling write(ByteBuffer[]) leads to the method 2800 // write(ByteBuffer[], int, int) being called with a 0 for the 2801 // second parameter and sources.length as the third parameter. 2802 sc.write(byteBufferArray); 2803 assertTrue(sc.isWriteCalled); 2804 } 2805 2806 /** 2807 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2808 */ 2809 public void test_writev() throws Exception { 2810 ServerSocketChannel ssc = ServerSocketChannel.open(); 2811 ssc.socket().bind(null); 2812 SocketChannel sc = SocketChannel.open(); 2813 sc.connect(ssc.socket().getLocalSocketAddress()); 2814 SocketChannel sock = ssc.accept(); 2815 ByteBuffer[] buf = { ByteBuffer.allocate(10), ByteBuffer.allocateDirect(20) }; 2816 2817 while (buf[0].remaining() != 0 && buf[1].remaining() !=0) { 2818 assertTrue(sc.write(buf, 0, 2) >= 0); 2819 } 2820 2821 ByteBuffer target = ByteBuffer.allocate(30); 2822 2823 while (target.remaining() != 0) { 2824 assertTrue(sock.read(target) >=0); 2825 } 2826 2827 ssc.close(); 2828 sc.close(); 2829 sock.close(); 2830 } 2831 2832 /** 2833 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2834 */ 2835 public void test_writev2() throws Exception { 2836 ServerSocketChannel ssc = ServerSocketChannel.open(); 2837 ssc.configureBlocking(false); 2838 ssc.socket().bind(null); 2839 SocketChannel sc = SocketChannel.open(); 2840 sc.configureBlocking(false); 2841 boolean connected = sc.connect(ssc.socket().getLocalSocketAddress()); 2842 SocketChannel sock = ssc.accept(); 2843 if (!connected) { 2844 sc.finishConnect(); 2845 } 2846 2847 ByteBuffer buf1 = ByteBuffer.allocate(10); 2848 sc.socket().setSendBufferSize(512); 2849 int bufSize = sc.socket().getSendBufferSize(); 2850 ByteBuffer buf2 = ByteBuffer.allocate(bufSize * 10); 2851 2852 ByteBuffer[] sent = new ByteBuffer[2]; 2853 sent[0] = buf1; 2854 sent[1] = buf2; 2855 2856 long whole = buf1.remaining() + buf2.remaining(); 2857 2858 long write = sc.write(sent); 2859 ssc.close(); 2860 sc.close(); 2861 sock.close(); 2862 2863 assertTrue(whole == (write + buf1.remaining() + buf2.remaining())); 2864 } 2865 2866 /** 2867 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2868 * 2869 * In non-blocking mode, the native system call will return EAGAIN/EWOULDBLOCK error 2870 * code on Linux/Unix and return WSATRY_AGAIN/WSAEWOULDBLOCK error code on Windows. 2871 * These error code means try again but not fatal error, so we should not throw exception. 2872 */ 2873 public void test_write$NonBlockingException() throws Exception { 2874 ServerSocketChannel ssc = ServerSocketChannel.open(); 2875 ssc.configureBlocking(false); 2876 ssc.socket().bind(null); 2877 SocketChannel sc = SocketChannel.open(); 2878 sc.configureBlocking(false); 2879 boolean connected = sc.connect(ssc.socket().getLocalSocketAddress()); 2880 SocketChannel sock = ssc.accept(); 2881 if (!connected) { 2882 sc.finishConnect(); 2883 } 2884 2885 try { 2886 for (int i = 0; i < 100; i++) { 2887 ByteBuffer buf1 = ByteBuffer.allocate(10); 2888 sc.socket().setSendBufferSize(512); 2889 int bufSize = sc.socket().getSendBufferSize(); 2890 ByteBuffer buf2 = ByteBuffer.allocate(bufSize * 10); 2891 2892 ByteBuffer[] sent = new ByteBuffer[2]; 2893 sent[0] = buf1; 2894 sent[1] = buf2; 2895 2896 sc.write(sent); 2897 } 2898 } finally { 2899 ssc.close(); 2900 sc.close(); 2901 sock.close(); 2902 } 2903 2904 } 2905 2906 /** 2907 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2908 */ 2909 public void test_write$LByteBuffer2() throws IOException { 2910 // Set-up 2911 ServerSocketChannel server = ServerSocketChannel.open(); 2912 server.socket().bind(null); 2913 SocketChannel client = SocketChannel.open(); 2914 client.connect(server.socket().getLocalSocketAddress()); 2915 SocketChannel worker = server.accept(); 2916 2917 // Test overlapping buffers 2918 byte[] data = "Hello world!".getBytes("UTF-8"); 2919 ByteBuffer[] buffers = new ByteBuffer[3]; 2920 buffers[0] = ByteBuffer.wrap(data, 0, 6); 2921 buffers[1] = ByteBuffer.wrap(data, 6, data.length - 6); 2922 buffers[2] = ByteBuffer.wrap(data); 2923 2924 // Write them out, read what we wrote and check it 2925 client.write(buffers); 2926 client.close(); 2927 ByteBuffer readBuffer = ByteBuffer.allocate(1024); 2928 while (EOF != worker.read(readBuffer)) {} 2929 readBuffer.flip(); 2930 Buffer expected = ByteBuffer.allocate(1024).put(data).put(data).flip(); 2931 assertEquals(expected, readBuffer); 2932 2933 // Tidy-up 2934 worker.close(); 2935 server.close(); 2936 } 2937 2938 /** 2939 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2940 */ 2941 public void test_write$LByteBuffer_buffers() throws IOException { 2942 // Set-up 2943 ServerSocketChannel server = ServerSocketChannel.open(); 2944 server.socket().bind(null); 2945 SocketChannel client = SocketChannel.open(); 2946 client.connect(server.socket().getLocalSocketAddress()); 2947 SocketChannel worker = server.accept(); 2948 2949 // A variety of buffer types to write 2950 byte[] data = "Hello world!".getBytes("UTF-8"); 2951 ByteBuffer[] buffers = new ByteBuffer[3]; 2952 buffers[0] = ByteBuffer.wrap(data, 0, 2); 2953 assertFalse(buffers[0].isDirect()); 2954 assertTrue(buffers[0].hasArray()); 2955 2956 buffers[1] = ByteBuffer.wrap(data, 2, 4).asReadOnlyBuffer(); 2957 assertFalse(buffers[1].isDirect()); 2958 assertFalse(buffers[1].hasArray()); 2959 2960 buffers[2] = ByteBuffer.allocateDirect(42); 2961 buffers[2].put(data, 6, data.length - 6); 2962 buffers[2].flip(); 2963 assertTrue(buffers[2].isDirect()); 2964 2965 // Write them out, read what we wrote and check it 2966 client.write(buffers); 2967 client.close(); 2968 ByteBuffer readBuffer = ByteBuffer.allocate(1024); 2969 while (EOF != worker.read(readBuffer)) {} 2970 readBuffer.flip(); 2971 assertEquals(ByteBuffer.wrap(data), readBuffer); 2972 2973 // Tidy-up 2974 worker.close(); 2975 server.close(); 2976 } 2977 2978 /** 2979 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 2980 */ 2981 public void test_write$LByteBuffer_writes() throws IOException { 2982 // Set-up 2983 ServerSocketChannel server = ServerSocketChannel.open(); 2984 server.socket().bind(null); 2985 SocketChannel client = SocketChannel.open(); 2986 client.connect(server.socket().getLocalSocketAddress()); 2987 SocketChannel worker = server.accept(); 2988 2989 // Data to write 2990 byte[] data = "Hello world!".getBytes("UTF-8"); 2991 ByteBuffer[] buffers = new ByteBuffer[3]; 2992 buffers[0] = ByteBuffer.wrap(data, 0, 6); 2993 buffers[1] = ByteBuffer.wrap("world!".getBytes("UTF-8")); 2994 buffers[2] = buffers[0]; 2995 assertTrue(buffers[0].hasArray()); 2996 2997 // Test a sequence of write calls 2998 client.write(buffers, 0, 0); // write nothing 2999 client.write(buffers, 1, 0); // write nothing 3000 client.write(buffers, 0, 1); // write "Hello " 3001 assertEquals("Failed to drain buffer 0", 0, buffers[0].remaining()); 3002 assertEquals("Shouldn't touch buffer 1", buffers[1].limit(), buffers[1] 3003 .remaining()); 3004 client.write(buffers, 0, 2); // writes "world!" 3005 assertEquals("Failed to drain buffer 1", 0, buffers[1].remaining()); 3006 client.write(buffers, 0, 3); // write nothing 3007 client.close(); 3008 3009 // Read what we wrote and check it 3010 ByteBuffer readBuffer = ByteBuffer.allocate(1024); 3011 while (EOF != worker.read(readBuffer)) {} 3012 readBuffer.flip(); 3013 assertEquals(ByteBuffer.wrap(data), readBuffer); 3014 3015 // Tidy-up 3016 worker.close(); 3017 server.close(); 3018 } 3019 3020 /** 3021 * @tests java.nio.channels.SocketChannel#write(ByteBuffer[]) 3022 */ 3023 public void test_write$LByteBuffer_invalid() throws IOException { 3024 // Set-up 3025 ServerSocketChannel server = ServerSocketChannel.open(); 3026 server.socket().bind(null); 3027 3028 SocketChannel client = SocketChannel.open(); 3029 client.connect(server.socket().getLocalSocketAddress()); 3030 3031 SocketChannel worker = server.accept(); 3032 3033 // Do some stuff 3034 try { 3035 client.write((ByteBuffer[]) null); 3036 fail("Should throw a NPE"); 3037 } catch (NullPointerException expected) { 3038 } 3039 try { 3040 client.write((ByteBuffer[]) null, 0, 0); 3041 fail("Should throw a NPE"); 3042 } catch (NullPointerException expected) { 3043 } 3044 try { 3045 client.write((ByteBuffer[]) null, 1, 0); 3046 fail("Should throw a NPE"); 3047 } catch (NullPointerException expected) { 3048 } 3049 try { 3050 client.write((ByteBuffer[]) null, 0, 1); 3051 fail("Should throw a NPE"); 3052 } catch (NullPointerException expected) { 3053 } 3054 try { 3055 client.write((ByteBuffer[]) null, 1, 1); 3056 fail("Should throw a NPE"); 3057 } catch (NullPointerException expected) { 3058 } 3059 3060 ByteBuffer[] buffers = new ByteBuffer[1]; 3061 buffers[0] = ByteBuffer.wrap("Hello ".getBytes("UTF-8")); 3062 3063 try { 3064 client.write(buffers, -1, 1); 3065 fail(); 3066 } catch (IndexOutOfBoundsException expected) { 3067 } 3068 try { 3069 client.write(buffers, 0, -1); 3070 fail(); 3071 } catch (IndexOutOfBoundsException expected) { 3072 } 3073 try { 3074 client.write(buffers, 0, 2); 3075 fail(); 3076 } catch (IndexOutOfBoundsException expected) { 3077 } 3078 try { 3079 client.write(buffers, 2, 0); 3080 fail(); 3081 } catch (IndexOutOfBoundsException expected) { 3082 } 3083 try { 3084 client.write(null, 0, 0); 3085 fail(); 3086 } catch (NullPointerException expected) { 3087 } 3088 3089 // Tidy-up 3090 worker.close(); 3091 client.close(); 3092 server.close(); 3093 } 3094 3095 public void testSocket_configureblocking() throws IOException { 3096 byte[] serverWBuf = new byte[CAPACITY_NORMAL]; 3097 for (int i = 0; i < serverWBuf.length; i++) { 3098 serverWBuf[i] = (byte) i; 3099 } 3100 java.nio.ByteBuffer buf = java.nio.ByteBuffer 3101 .allocate(CAPACITY_NORMAL + 1); 3102 channel1.connect(localAddr1); 3103 server1.accept(); 3104 Socket sock = this.channel1.socket(); 3105 channel1.configureBlocking(false); 3106 assertFalse(channel1.isBlocking()); 3107 OutputStream channelSocketOut = sock.getOutputStream(); 3108 try { 3109 // write operation is not allowed in non-blocking mode 3110 channelSocketOut.write(buf.array()); 3111 fail("Non-Blocking mode should cause IllegalBlockingModeException"); 3112 } catch (IllegalBlockingModeException e) { 3113 // correct 3114 } 3115 channel1.configureBlocking(true); 3116 assertTrue(channel1.isBlocking()); 3117 // write operation is allowed in blocking mode 3118 channelSocketOut.write(buf.array()); 3119 } 3120 3121 /** 3122 * @tests SocketChannel#read(ByteBuffer[], int, int) when remote server 3123 * closed 3124 */ 3125 public void test_socketChannel_read_ByteBufferII_remoteClosed() 3126 throws Exception { 3127 // regression 1 for HARMONY-549 3128 ServerSocketChannel ssc = ServerSocketChannel.open(); 3129 ssc.socket().bind(null); 3130 SocketChannel sc = SocketChannel.open(); 3131 sc.connect(ssc.socket().getLocalSocketAddress()); 3132 ssc.accept().close(); 3133 ByteBuffer[] buf = { ByteBuffer.allocate(10) }; 3134 assertEquals(-1, sc.read(buf, 0, 1)); 3135 ssc.close(); 3136 sc.close(); 3137 } 3138 3139 /** 3140 * @tests SocketChannel#write(ByteBuffer[], int, int) 3141 */ 3142 public void test_socketChannel_write_ByteBufferII() throws Exception { 3143 // regression 2 for HARMONY-549 3144 ServerSocketChannel ssc = ServerSocketChannel.open(); 3145 ssc.socket().bind(null); 3146 SocketChannel sc = SocketChannel.open(); 3147 sc.connect(ssc.socket().getLocalSocketAddress()); 3148 SocketChannel sock = ssc.accept(); 3149 ByteBuffer[] buf = { ByteBuffer.allocate(10), null }; 3150 try { 3151 sc.write(buf, 0, 2); 3152 fail("should throw NPE"); 3153 } catch (NullPointerException expected) { 3154 } 3155 ssc.close(); 3156 sc.close(); 3157 ByteBuffer target = ByteBuffer.allocate(10); 3158 assertEquals(-1, sock.read(target)); 3159 } 3160 3161 /** 3162 * @tests SocketChannel#read(ByteBuffer[], int, int) with a null ByteBuffer 3163 */ 3164 public void test_socketChannel_read_ByteBufferII_bufNULL() throws Exception { 3165 // regression 3 for HARMONY-549 3166 ServerSocketChannel ssc = ServerSocketChannel.open(); 3167 ssc.socket().bind(null); 3168 SocketChannel sc = SocketChannel.open(); 3169 sc.connect(ssc.socket().getLocalSocketAddress()); 3170 ssc.accept(); 3171 ByteBuffer[] buf = new ByteBuffer[2]; 3172 buf[0] = ByteBuffer.allocate(1); 3173 // let buf[1] be null 3174 try { 3175 sc.read(buf, 0, 2); 3176 fail("should throw NullPointerException"); 3177 } catch (NullPointerException expected) { 3178 } 3179 ssc.close(); 3180 sc.close(); 3181 } 3182 3183 /** 3184 * @tests SocketChannel#write(ByteBuffer) after close 3185 */ 3186 public void test_socketChannel_write_close() throws Exception { 3187 // regression 4 for HARMONY-549 3188 ServerSocketChannel ssc = ServerSocketChannel.open(); 3189 ssc.socket().bind(null); 3190 SocketChannel sc = SocketChannel.open(); 3191 sc.connect(ssc.socket().getLocalSocketAddress()); 3192 SocketChannel sock = ssc.accept(); 3193 ByteBuffer buf = null; 3194 ssc.close(); 3195 sc.close(); 3196 try { 3197 sc.write(buf); 3198 fail("should throw NPE"); 3199 } catch (NullPointerException expected) { 3200 } 3201 sock.close(); 3202 } 3203 3204 /** 3205 * @tests SocketChannel#write(ByteBuffer) if position is not zero 3206 */ 3207 public void test_socketChannel_write_ByteBuffer_posNotZero() 3208 throws Exception { 3209 // regression 5 for HARMONY-549 3210 final String testStr = "Hello World"; 3211 ByteBuffer readBuf = ByteBuffer.allocate(11); 3212 ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes()); 3213 ServerSocketChannel ssc = ServerSocketChannel.open(); 3214 ssc.socket().bind(null); 3215 SocketChannel sc = SocketChannel.open(); 3216 sc.connect(ssc.socket().getLocalSocketAddress()); 3217 buf.position(2); 3218 ssc.accept().write(buf); 3219 assertEquals(9, sc.read(readBuf)); 3220 buf.flip(); 3221 readBuf.flip(); 3222 byte[] read = new byte[9]; 3223 byte[] write = new byte[11]; 3224 buf.get(write); 3225 readBuf.get(read); 3226 for (int i = 0; i < 9; i++) { 3227 assertEquals(read[i], write[i + 2]); 3228 } 3229 } 3230 3231 /** 3232 * @tests SocketChannelImpl#read(ByteBuffer[]) 3233 */ 3234 public void test_read_$ByteBuffer_Blocking() throws IOException { 3235 // regression test for Harmony-728 3236 byte[] data = new byte[CAPACITY_NORMAL]; 3237 for (int i = 0; i < CAPACITY_NORMAL; i++) { 3238 data[i] = (byte) i; 3239 } 3240 ByteBuffer[] buf = new ByteBuffer[2]; 3241 buf[0] = ByteBuffer.allocate(CAPACITY_NORMAL); 3242 buf[1] = ByteBuffer.allocate(CAPACITY_NORMAL); 3243 channel1.connect(localAddr1); 3244 Socket socket = null; 3245 try { 3246 socket = server1.accept(); 3247 OutputStream out = socket.getOutputStream(); 3248 out.write(data); 3249 // should not block here 3250 channel1.read(buf); 3251 } finally { 3252 if (null != socket) { 3253 socket.close(); 3254 } 3255 } 3256 } 3257 3258 public void test_socket_getOutputStream_nonBlocking_read_Exception() throws IOException { 3259 byte[] buf = new byte[1]; 3260 channel1.connect(this.localAddr1); 3261 InputStream is = channel1.socket().getInputStream(); 3262 channel1.configureBlocking(false); 3263 try { 3264 is.read(); 3265 fail(); 3266 } catch (IllegalBlockingModeException expected) { 3267 } 3268 3269 try { 3270 is.read(null); 3271 fail(); 3272 } catch (NullPointerException expected) { 3273 // Any of these exceptions are possible. 3274 } catch (IllegalBlockingModeException expected) { 3275 // Any of these exceptions are possible. 3276 } 3277 3278 try { 3279 is.read(buf, -1, 1); 3280 fail(); 3281 } catch (IndexOutOfBoundsException expected) { 3282 // Any of these exceptions are possible. 3283 } catch (IllegalBlockingModeException expected) { 3284 // Any of these exceptions are possible. 3285 } 3286 3287 try { 3288 is.read(buf, 0, -1); 3289 fail(); 3290 } catch (IndexOutOfBoundsException expected) { 3291 // Any of these exceptions are possible. 3292 } catch (IllegalBlockingModeException expected) { 3293 // Any of these exceptions are possible. 3294 } 3295 3296 try { 3297 is.read(buf, 0, 2); 3298 fail(); 3299 } catch (IndexOutOfBoundsException expected) { 3300 // Any of these exceptions are possible. 3301 } catch (IllegalBlockingModeException expected) { 3302 // Any of these exceptions are possible. 3303 } 3304 3305 try { 3306 is.read(buf, 2, 0); 3307 fail(); 3308 } catch (IndexOutOfBoundsException expected) { 3309 // Any of these exceptions are possible. 3310 } catch (IllegalBlockingModeException expected) { 3311 // Any of these exceptions are possible. 3312 } 3313 3314 try { 3315 is.read(null, 0, 0); 3316 fail(); 3317 } catch (NullPointerException expected) { 3318 // Any of these exceptions are possible. 3319 } catch (IllegalBlockingModeException expected) { 3320 // Any of these exceptions are possible. 3321 } 3322 3323 is.close(); 3324 3325 try { 3326 is.read(); 3327 fail(); 3328 } catch (IllegalBlockingModeException expected) { 3329 // Any of these exceptions are possible. 3330 } catch (IOException expected) { 3331 // Any of these exceptions are possible. 3332 } 3333 3334 try { 3335 is.read(null); 3336 fail(); 3337 } catch (NullPointerException expected) { 3338 // Any of these exceptions are possible. 3339 } catch (IllegalBlockingModeException expected) { 3340 // Any of these exceptions are possible. 3341 } catch (IOException expected) { 3342 // Any of these exceptions are possible. 3343 } 3344 3345 try { 3346 is.read(buf, -1, 1); 3347 fail(); 3348 } catch (IndexOutOfBoundsException expected) { 3349 // Any of these exceptions are possible. 3350 } catch (IllegalBlockingModeException expected) { 3351 // Any of these exceptions are possible. 3352 } catch (IOException expected) { 3353 // Any of these exceptions are possible. 3354 } 3355 3356 try { 3357 is.read(buf, 0, -1); 3358 fail(); 3359 } catch (IndexOutOfBoundsException expected) { 3360 // Any of these exceptions are possible. 3361 } catch (IllegalBlockingModeException expected) { 3362 // Any of these exceptions are possible. 3363 } catch (IOException expected) { 3364 // Any of these exceptions are possible. 3365 } 3366 3367 try { 3368 is.read(buf, 0, 2); 3369 fail(); 3370 } catch (IndexOutOfBoundsException expected) { 3371 // Any of these exceptions are possible. 3372 } catch (IllegalBlockingModeException expected) { 3373 // Any of these exceptions are possible. 3374 } catch (IOException expected) { 3375 // Any of these exceptions are possible. 3376 } 3377 3378 try { 3379 is.read(buf, 2, 0); 3380 fail(); 3381 } catch (IndexOutOfBoundsException expected) { 3382 // Any of these exceptions are possible. 3383 } catch (IllegalBlockingModeException expected) { 3384 // Any of these exceptions are possible. 3385 } catch (IOException expected) { 3386 // Any of these exceptions are possible. 3387 } 3388 3389 try { 3390 is.read(null, 0, 0); 3391 fail(); 3392 } catch (NullPointerException expected) { 3393 // Any of these exceptions are possible. 3394 } catch (IllegalBlockingModeException expected) { 3395 // Any of these exceptions are possible. 3396 } catch (IOException expected) { 3397 // Any of these exceptions are possible. 3398 } 3399 } 3400 3401 public void test_socket_getOutputStream_blocking_read_Exception() throws IOException { 3402 byte[] buf = new byte[1]; 3403 channel1.connect(this.localAddr1); 3404 InputStream is = channel1.socket().getInputStream(); 3405 try { 3406 is.read(null); 3407 fail(); 3408 } catch (NullPointerException expected) { 3409 } 3410 try { 3411 is.read(buf, -1, 1); 3412 fail(); 3413 } catch (IndexOutOfBoundsException expected) { 3414 } 3415 try { 3416 is.read(buf, 0, -1); 3417 fail(); 3418 } catch (IndexOutOfBoundsException expected) { 3419 } 3420 try { 3421 is.read(buf, 0, 2); 3422 fail(); 3423 } catch (IndexOutOfBoundsException expected) { 3424 } 3425 try { 3426 is.read(buf, 2, 1); 3427 fail(); 3428 } catch (IndexOutOfBoundsException expected) { 3429 } 3430 try { 3431 is.read(null, 0, 1); 3432 fail(); 3433 } catch (NullPointerException expected) { 3434 } 3435 3436 is.close(); 3437 3438 try { 3439 is.read(null); 3440 fail(); 3441 } catch (NullPointerException expected) { 3442 } 3443 try { 3444 is.read(buf, -1, 1); 3445 fail(); 3446 } catch (IndexOutOfBoundsException expected) { 3447 } 3448 try { 3449 is.read(buf, 0, -1); 3450 fail(); 3451 } catch (IndexOutOfBoundsException expected) { 3452 } 3453 try { 3454 is.read(buf, 0, 2); 3455 fail(); 3456 } catch (IndexOutOfBoundsException expected) { 3457 } 3458 try { 3459 is.read(buf, 2, 1); 3460 fail(); 3461 } catch (IndexOutOfBoundsException expected) { 3462 } 3463 try { 3464 is.read(null, 0, 1); 3465 fail(); 3466 } catch (NullPointerException expected) { 3467 } 3468 } 3469 3470 public void test_socket_getOutputStream_nonBlocking_write_Exception() throws IOException { 3471 byte[] buf = new byte[1]; 3472 channel1.connect(this.localAddr1); 3473 OutputStream os = channel1.socket().getOutputStream(); 3474 channel1.configureBlocking(false); 3475 3476 try { 3477 os.write(1); 3478 fail(); 3479 } catch (IllegalBlockingModeException expected) { 3480 } 3481 try { 3482 os.write(null); 3483 fail(); 3484 } catch (IllegalBlockingModeException expected) { 3485 // Any of these exceptions are possible. 3486 } catch (NullPointerException expected) { 3487 // Any of these exceptions are possible. 3488 } 3489 3490 try { 3491 os.write(buf, -1, 1); 3492 fail(); 3493 } catch (IllegalBlockingModeException expected) { 3494 // Any of these exceptions are possible. 3495 } catch (IndexOutOfBoundsException expected) { 3496 // Any of these exceptions are possible. 3497 } 3498 3499 try { 3500 os.write(buf, 0, -1); 3501 fail(); 3502 } catch (IllegalBlockingModeException expected) { 3503 // Any of these exceptions are possible. 3504 } catch (IndexOutOfBoundsException expected) { 3505 // Any of these exceptions are possible. 3506 } 3507 3508 try { 3509 os.write(buf, 0, 2); 3510 fail(); 3511 } catch (IllegalBlockingModeException expected) { 3512 // Any of these exceptions are possible. 3513 } catch (IndexOutOfBoundsException expected) { 3514 // Any of these exceptions are possible. 3515 } 3516 3517 try { 3518 os.write(buf, 2, 1); 3519 fail(); 3520 } catch (IllegalBlockingModeException expected) { 3521 // Any of these exceptions are possible. 3522 } catch (IndexOutOfBoundsException expected) { 3523 // Any of these exceptions are possible. 3524 } 3525 3526 try { 3527 os.write(null, 0, 1); 3528 fail(); 3529 } catch (IllegalBlockingModeException expected) { 3530 // Any of these exceptions are possible. 3531 } catch (NullPointerException expected) { 3532 // Any of these exceptions are possible. 3533 } 3534 3535 os.close(); 3536 3537 try { 3538 os.write(1); 3539 fail(); 3540 } catch (IllegalBlockingModeException expected) { 3541 } 3542 3543 try { 3544 os.write(null); 3545 fail(); 3546 } catch (IllegalBlockingModeException expected) { 3547 // Any of these exceptions are possible. 3548 } catch (NullPointerException expected) { 3549 // Any of these exceptions are possible. 3550 } 3551 3552 try { 3553 os.write(buf, -1, 1); 3554 fail(); 3555 } catch (IllegalBlockingModeException expected) { 3556 // Any of these exceptions are possible. 3557 } catch (IndexOutOfBoundsException expected) { 3558 // Any of these exceptions are possible. 3559 } 3560 3561 try { 3562 os.write(buf, 0, -1); 3563 fail(); 3564 } catch (IllegalBlockingModeException expected) { 3565 // Any of these exceptions are possible. 3566 } catch (IndexOutOfBoundsException expected) { 3567 // Any of these exceptions are possible. 3568 } 3569 3570 try { 3571 os.write(buf, 0, 2); 3572 fail(); 3573 } catch (IllegalBlockingModeException expected) { 3574 // Any of these exceptions are possible. 3575 } catch (IndexOutOfBoundsException expected) { 3576 // Any of these exceptions are possible. 3577 } 3578 3579 try { 3580 os.write(buf, 2, 0); 3581 fail(); 3582 } catch (IllegalBlockingModeException expected) { 3583 // Any of these exceptions are possible. 3584 } catch (IndexOutOfBoundsException expected) { 3585 // Any of these exceptions are possible. 3586 } 3587 3588 try { 3589 os.write(null, 0, 0); 3590 fail(); 3591 } catch (IllegalBlockingModeException expected) { 3592 // Any of these exceptions are possible. 3593 } catch (NullPointerException expected) { 3594 // Any of these exceptions are possible. 3595 } 3596 } 3597 3598 public void test_socket_getOutputStream_blocking_write_Exception() throws IOException { 3599 byte[] buf = new byte[1]; 3600 channel1.connect(this.localAddr1); 3601 OutputStream os = channel1.socket().getOutputStream(); 3602 try { 3603 os.write(null); 3604 fail(); 3605 } catch (NullPointerException expected) { 3606 } 3607 try { 3608 os.write(buf, -1, 1); 3609 fail(); 3610 } catch (IndexOutOfBoundsException expected) { 3611 } 3612 try { 3613 os.write(buf, 0, -1); 3614 fail(); 3615 } catch (IndexOutOfBoundsException expected) { 3616 } 3617 try { 3618 os.write(buf, 0, 2); 3619 fail(); 3620 } catch (IndexOutOfBoundsException expected) { 3621 } 3622 try { 3623 os.write(buf, 2, 0); 3624 fail(); 3625 } catch (IndexOutOfBoundsException expected) { 3626 } 3627 try { 3628 os.write(null, 0, 0); 3629 fail(); 3630 } catch (NullPointerException expected) { 3631 } 3632 3633 os.close(); 3634 3635 try { 3636 os.write(null); 3637 fail(); 3638 } catch (NullPointerException expected) { 3639 } 3640 try { 3641 os.write(buf, -1, 1); 3642 fail(); 3643 } catch (IndexOutOfBoundsException expected) { 3644 } 3645 try { 3646 os.write(buf, 0, -1); 3647 fail(); 3648 } catch (IndexOutOfBoundsException expected) { 3649 } 3650 try { 3651 os.write(buf, 0, 2); 3652 fail(); 3653 } catch (IndexOutOfBoundsException expected) { 3654 } 3655 try { 3656 os.write(buf, 2, 0); 3657 fail(); 3658 } catch (IndexOutOfBoundsException expected) { 3659 } 3660 try { 3661 os.write(null, 0, 0); 3662 fail(); 3663 } catch (NullPointerException expected) { 3664 } 3665 } 3666 3667 /** 3668 * @tests SocketChannelImpl#socket().getOutputStream().write(int) 3669 */ 3670 public void test_socket_getOutputStream_write_oneByte() 3671 throws IOException { 3672 3673 // Regression test for Harmony-3475 3674 3675 int MAGIC = 123; 3676 3677 channel1.connect(this.localAddr1); 3678 3679 OutputStream os = channel1.socket().getOutputStream(); 3680 3681 Socket acceptedSocket = server1.accept(); 3682 3683 InputStream in = acceptedSocket.getInputStream(); 3684 3685 os.write(MAGIC); 3686 channel1.close(); 3687 3688 int lastByte = in.read(); 3689 if (lastByte == -1) { 3690 fail("Server received nothing. Expected 1 byte."); 3691 } else if (lastByte != MAGIC) { 3692 fail("Server received wrong single byte: " + lastByte + 3693 ", expected: " + MAGIC); 3694 } 3695 3696 lastByte = in.read(); 3697 if (lastByte != -1) { 3698 fail("Server received too long sequence. Expected 1 byte."); 3699 } 3700 } 3701 3702 public void testSocket_setOptions() throws IOException { 3703 channel1.connect(localAddr1); 3704 Socket socket = channel1.socket(); 3705 3706 ByteBuffer buffer = ByteBuffer.wrap(new byte[] {1, 2, 3}); 3707 socket.setKeepAlive(true); 3708 channel1.write(buffer); 3709 3710 socket.setOOBInline(true); 3711 channel1.write(buffer); 3712 3713 socket.setReceiveBufferSize(100); 3714 channel1.write(buffer); 3715 3716 socket.setReuseAddress(true); 3717 channel1.write(buffer); 3718 3719 socket.setSendBufferSize(100); 3720 channel1.write(buffer); 3721 3722 socket.setSoLinger(true, 100); 3723 channel1.write(buffer); 3724 3725 socket.setSoTimeout(1000); 3726 channel1.write(buffer); 3727 3728 socket.setTcpNoDelay(true); 3729 channel1.write(buffer); 3730 3731 socket.setTrafficClass(10); 3732 channel1.write(buffer); 3733 } 3734 3735 class MockSocketChannel extends SocketChannel { 3736 3737 private boolean isWriteCalled = false; 3738 3739 private boolean isReadCalled = false; 3740 3741 public MockSocketChannel(SelectorProvider provider) { 3742 super(provider); 3743 } 3744 3745 @Override 3746 public Socket socket() { 3747 return null; 3748 } 3749 3750 @Override 3751 public boolean isConnected() { 3752 return false; 3753 } 3754 3755 @Override 3756 public boolean isConnectionPending() { 3757 return false; 3758 } 3759 3760 @Override 3761 public boolean connect(SocketAddress address) throws IOException { 3762 return false; 3763 } 3764 3765 @Override 3766 public boolean finishConnect() throws IOException { 3767 return false; 3768 } 3769 3770 @Override 3771 public int read(ByteBuffer target) throws IOException { 3772 return 0; 3773 } 3774 3775 @Override 3776 public long read(ByteBuffer[] targets, int offset, int length) throws IOException { 3777 // Verify that calling read(ByteBuffer[]) leads to the method 3778 // read(ByteBuffer[], int, int) being called with a 0 for the 3779 // second parameter and targets.length as the third parameter. 3780 if(0 == offset && length == targets.length){ 3781 isReadCalled = true; 3782 } 3783 return 0; 3784 } 3785 3786 @Override 3787 public int write(ByteBuffer source) throws IOException { 3788 return 0; 3789 } 3790 3791 @Override 3792 public long write(ByteBuffer[] sources, int offset, int length) throws IOException { 3793 // Verify that calling write(ByteBuffer[]) leads to the method 3794 // write(ByteBuffer[], int, int) being called with a 0 for the 3795 // second parameter and sources.length as the third parameter. 3796 if(0 == offset && length == sources.length){ 3797 isWriteCalled = true; 3798 } 3799 return 0; 3800 } 3801 3802 @Override 3803 protected void implCloseSelectableChannel() throws IOException { 3804 // empty 3805 } 3806 3807 @Override 3808 protected void implConfigureBlocking(boolean blockingMode) throws IOException { 3809 // empty 3810 } 3811 3812 @Override 3813 public SocketAddress getRemoteAddress() throws IOException { 3814 return null; 3815 } 3816 3817 @Override 3818 public SocketChannel shutdownOutput() throws IOException { 3819 return null; 3820 } 3821 3822 @Override 3823 public SocketChannel shutdownInput() throws IOException { 3824 return null; 3825 } 3826 3827 @Override 3828 public <T> SocketChannel setOption(SocketOption<T> name, T value) 3829 throws IOException { 3830 return null; 3831 } 3832 3833 @Override 3834 public SocketChannel bind(SocketAddress local) throws IOException { 3835 return null; 3836 } 3837 3838 @Override 3839 public Set<SocketOption<?>> supportedOptions() { 3840 return null; 3841 } 3842 3843 @Override 3844 public <T> T getOption(SocketOption<T> name) throws IOException { 3845 return null; 3846 } 3847 3848 @Override 3849 public SocketAddress getLocalAddress() throws IOException { 3850 return null; 3851 } 3852 } 3853} 3854