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