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.net.DatagramPacket; 22 import java.net.DatagramSocket; 23 import java.net.InetAddress; 24 import java.net.InetSocketAddress; 25 import java.net.SocketAddress; 26 import java.net.SocketException; 27 import java.nio.ByteBuffer; 28 import java.nio.channels.AsynchronousCloseException; 29 import java.nio.channels.ClosedChannelException; 30 import java.nio.channels.DatagramChannel; 31 import java.nio.channels.IllegalBlockingModeException; 32 import java.nio.channels.NotYetConnectedException; 33 import java.nio.channels.UnresolvedAddressException; 34 import java.nio.channels.UnsupportedAddressTypeException; 35 import java.nio.channels.spi.SelectorProvider; 36 import java.security.Permission; 37 38 import junit.framework.TestCase; 39 import tests.support.Support_PortManager; 40 41 /** 42 * Test for DatagramChannel 43 * 44 */ 45 public class DatagramChannelTest extends TestCase { 46 47 private static final int CAPACITY_NORMAL = 200; 48 49 private static final int CAPACITY_1KB = 1024; 50 51 private static final int CAPACITY_64KB = 65536; 52 53 private static final int CAPACITY_ZERO = 0; 54 55 private static final int CAPACITY_ONE = 1; 56 57 private static final int TIME_UNIT = 500; 58 59 private InetSocketAddress localAddr1; 60 61 private InetSocketAddress localAddr2; 62 63 private DatagramChannel channel1; 64 65 private DatagramChannel channel2; 66 67 private DatagramSocket datagramSocket1; 68 69 private DatagramSocket datagramSocket2; 70 71 // The port to be used in test cases. 72 private int testPort; 73 74 protected void setUp() throws Exception { 75 super.setUp(); 76 this.channel1 = DatagramChannel.open(); 77 this.channel2 = DatagramChannel.open(); 78 int[] ports = Support_PortManager.getNextPortsForUDP(5); 79 this.localAddr1 = new InetSocketAddress("127.0.0.1", ports[0]); 80 this.localAddr2 = new InetSocketAddress("127.0.0.1", ports[1]); 81 this.datagramSocket1 = new DatagramSocket(ports[2]); 82 this.datagramSocket2 = new DatagramSocket(ports[3]); 83 testPort = ports[4]; 84 } 85 86 protected void tearDown() throws Exception { 87 if (null != this.channel1) { 88 try { 89 this.channel1.close(); 90 } catch (Exception e) { 91 //ignore 92 } 93 } 94 if (null != this.channel2) { 95 try { 96 this.channel2.close(); 97 } catch (Exception e) { 98 //ignore 99 } 100 } 101 if (null != this.datagramSocket1) { 102 try { 103 this.datagramSocket1.close(); 104 } catch (Exception e) { 105 //ignore 106 } 107 } 108 if (null != this.datagramSocket2) { 109 try { 110 this.datagramSocket2.close(); 111 } catch (Exception e) { 112 //ignore 113 } 114 } 115 localAddr1 = null; 116 localAddr2 = null; 117 super.tearDown(); 118 } 119 120 // ------------------------------------------------------------------- 121 // Test for methods in abstract class. 122 // ------------------------------------------------------------------- 123 /* 124 * Test method for 'java.nio.channels.DatagramChannel.validOps()' 125 */ 126 public void testValidOps() { 127 MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 128 .provider()); 129 MockDatagramChannel testMocknull = new MockDatagramChannel(null); 130 int val = this.channel1.validOps(); 131 assertEquals(5, val); 132 assertEquals(val, testMock.validOps()); 133 assertEquals(val, testMocknull.validOps()); 134 } 135 136 /* 137 * Test method for 'java.nio.channels.DatagramChannel.open()' 138 */ 139 public void testOpen() { 140 MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 141 .provider()); 142 MockDatagramChannel testMocknull = new MockDatagramChannel(null); 143 assertNull(testMocknull.provider()); 144 assertNotNull(testMock.provider()); 145 assertEquals(this.channel1.provider(), testMock.provider()); 146 assertEquals(5, testMock.validOps()); 147 } 148 149 /* 150 * Test method for 'java.nio.channels.DatagramChannel.read(ByteBuffer)' 151 */ 152 public void testReadByteBufferArray() throws IOException { 153 final int testNum = 0; 154 long readres = testNum; 155 MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 156 .provider()); 157 MockDatagramChannel testMocknull = new MockDatagramChannel(null); 158 int bufSize = 10; 159 ByteBuffer[] readBuf = null; 160 try { 161 this.channel1.read(readBuf); 162 fail("Should throw NPE"); 163 } catch (NullPointerException e) { 164 // correct 165 } 166 try { 167 readres = testMock.read(readBuf); 168 fail("Should throw NPE"); 169 } catch (NullPointerException e) { 170 // correct 171 } 172 readBuf = new ByteBuffer[bufSize]; 173 try { 174 readres = this.channel1.read(readBuf); 175 fail("Should throw NotYetConnectedException"); 176 } catch (NotYetConnectedException e) { 177 // correct 178 } 179 readres = testMock.read(readBuf); 180 assertEquals(testNum, readres); 181 readres = testMocknull.read(readBuf); 182 assertEquals(testNum, readres); 183 } 184 185 /* 186 * Test method for 'java.nio.channels.DatagramChannel.read(ByteBuffer)' 187 */ 188 public void testReadByteBufferArray_BufNull() throws IOException { 189 MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 190 .provider()); 191 MockDatagramChannel testMocknull = new MockDatagramChannel(null); 192 193 ByteBuffer[] readBuf = null; 194 try { 195 this.channel1.read(readBuf); 196 fail("Should throw NPE"); 197 } catch (NullPointerException e) { 198 // correct 199 } 200 try { 201 testMock.read(readBuf); 202 fail("Should throw NPE"); 203 } catch (NullPointerException e) { 204 // correct 205 } 206 try { 207 testMocknull.read(readBuf); 208 fail("Should throw NPE"); 209 } catch (NullPointerException e) { 210 // correct 211 } 212 } 213 214 /* 215 * Test method for 'java.nio.channels.DatagramChannel.write(ByteBuffer)' 216 */ 217 public void testWriteByteBuffer() throws IOException { 218 MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 219 .provider()); 220 MockDatagramChannel testMocknull = new MockDatagramChannel(null); 221 int bufSize = 10; 222 ByteBuffer[] readBuf = null; 223 try { 224 this.channel1.write(readBuf); 225 fail("Should throw NPE"); 226 } catch (NullPointerException e) { 227 // correct 228 } 229 try { 230 testMock.write(readBuf); 231 fail("Should throw NPE"); 232 } catch (NullPointerException e) { 233 // correct 234 } 235 readBuf = new ByteBuffer[bufSize]; 236 try { 237 this.channel1.write(readBuf); 238 fail("Should throw NotYetConnectedException"); 239 } catch (NotYetConnectedException e) { 240 // correct 241 } 242 long writeres = 0; 243 writeres = testMock.write(readBuf); 244 245 assertEquals(0, writeres); 246 writeres = testMocknull.write(readBuf); 247 assertEquals(0, writeres); 248 } 249 250 /* 251 * Test method for 'java.nio.channels.DatagramChannel.write(ByteBuffer)' 252 */ 253 public void testWriteByteBuffer_Bufnull() throws IOException { 254 MockDatagramChannel testMock = new MockDatagramChannel(SelectorProvider 255 .provider()); 256 MockDatagramChannel testMocknull = new MockDatagramChannel(null); 257 ByteBuffer[] readBuf = null; 258 try { 259 this.channel1.write(readBuf); 260 fail("Should throw NPE"); 261 } catch (NullPointerException e) { 262 // correct 263 } 264 try { 265 testMock.write(readBuf); 266 fail("Should throw NPE"); 267 } catch (NullPointerException e) { 268 // correct 269 } 270 try { 271 testMocknull.write(readBuf); 272 fail("Should throw NPE"); 273 } catch (NullPointerException e) { 274 // correct 275 } 276 } 277 278 // ------------------------------------------------------------------- 279 // Test for socket() 280 // ------------------------------------------------------------------- 281 282 /** 283 * Test method for 'DatagramChannelImpl.socket()' 284 * 285 * @throws SocketException 286 */ 287 public void testSocket_BasicStatusBeforeConnect() throws SocketException { 288 assertFalse(this.channel1.isConnected());// not connected 289 DatagramSocket s1 = this.channel1.socket(); 290 assertSocketBeforeConnect(s1); 291 DatagramSocket s2 = this.channel1.socket(); 292 // same 293 assertSame(s1, s2); 294 } 295 296 /** 297 * Test method for 'DatagramChannelImpl.socket()' 298 * 299 * @throws IOException 300 */ 301 public void testSocket_Block_BasicStatusAfterConnect() throws IOException { 302 this.channel1.connect(localAddr1); 303 DatagramSocket s1 = this.channel1.socket(); 304 assertSocketAfterConnect(s1); 305 DatagramSocket s2 = this.channel1.socket(); 306 // same 307 assertSame(s1, s2); 308 } 309 310 public void testSocket_NonBlock_BasicStatusAfterConnect() 311 throws IOException { 312 this.channel1.connect(localAddr1); 313 this.channel1.configureBlocking(false); 314 DatagramSocket s1 = this.channel1.socket(); 315 assertSocketAfterConnect(s1); 316 DatagramSocket s2 = this.channel1.socket(); 317 // same 318 assertSame(s1, s2); 319 } 320 321 /** 322 * Test method for 'DatagramChannelImpl.socket()' 323 * 324 * @throws IOException 325 */ 326 public void testSocket_ActionsBeforeConnect() throws IOException { 327 assertFalse(this.channel1.isConnected());// not connected 328 DatagramSocket s = this.channel1.socket(); 329 assertSocketActionBeforeConnect(s); 330 } 331 332 /** 333 * Test method for 'DatagramChannelImpl.socket()' 334 * 335 * @throws IOException 336 */ 337 public void testSocket_Block_ActionsAfterConnect() throws IOException { 338 assertFalse(this.channel1.isConnected());// not connected 339 this.channel1.connect(localAddr1); 340 DatagramSocket s = this.channel1.socket(); 341 assertSocketActionAfterConnect(s); 342 } 343 344 public void testSocket_NonBlock_ActionsAfterConnect() throws IOException { 345 this.channel1.connect(localAddr1); 346 this.channel1.configureBlocking(false); 347 DatagramSocket s = this.channel1.socket(); 348 assertSocketActionAfterConnect(s); 349 } 350 351 private void assertSocketBeforeConnect(DatagramSocket s) 352 throws SocketException { 353 assertFalse(s.isBound()); 354 assertFalse(s.isClosed()); 355 assertFalse(s.isConnected()); 356 assertFalse(s.getBroadcast()); 357 assertFalse(s.getReuseAddress()); 358 assertNull(s.getInetAddress()); 359 assertTrue(s.getLocalAddress().isAnyLocalAddress()); 360 assertEquals(s.getLocalPort(), 0); 361 assertNull(s.getLocalSocketAddress()); 362 assertEquals(s.getPort(), -1); 363 assertTrue(s.getReceiveBufferSize() >= 8192); 364 assertNull(s.getRemoteSocketAddress()); 365 assertFalse(s.getReuseAddress()); 366 assertTrue(s.getSendBufferSize() >= 8192); 367 assertEquals(s.getSoTimeout(), 0); 368 assertEquals(s.getTrafficClass(), 0); 369 } 370 371 private void assertSocketAfterConnect(DatagramSocket s) 372 throws SocketException { 373 assertTrue(s.isBound()); 374 assertFalse(s.isClosed()); 375 assertTrue(s.isConnected()); 376 assertFalse(s.getBroadcast()); 377 assertFalse(s.getReuseAddress()); 378 assertSame(s.getInetAddress(), localAddr1.getAddress()); 379 assertEquals(s.getLocalAddress(), localAddr1.getAddress()); 380 assertNotNull(s.getLocalSocketAddress()); 381 assertEquals(s.getPort(), localAddr1.getPort()); 382 assertTrue(s.getReceiveBufferSize() >= 8192); 383 // not same , but equals 384 assertNotSame(s.getRemoteSocketAddress(), (SocketAddress) localAddr1); 385 assertEquals(s.getRemoteSocketAddress(), (SocketAddress) localAddr1); 386 assertFalse(s.getReuseAddress()); 387 assertTrue(s.getSendBufferSize() >= 8192); 388 assertEquals(s.getSoTimeout(), 0); 389 assertEquals(s.getTrafficClass(), 0); 390 } 391 392 private void assertSocketActionBeforeConnect(DatagramSocket s) 393 throws IOException { 394 s.connect(localAddr2); 395 assertFalse(this.channel1.isConnected()); 396 assertFalse(s.isConnected()); 397 398 s.disconnect(); 399 assertFalse(this.channel1.isConnected()); 400 assertFalse(s.isConnected()); 401 402 s.close(); 403 assertTrue(s.isClosed()); 404 assertFalse(this.channel1.isOpen()); 405 } 406 407 private void assertSocketActionAfterConnect(DatagramSocket s) 408 throws IOException { 409 assertEquals(s.getPort(), localAddr1.getPort()); 410 s.connect(localAddr2); 411 assertTrue(this.channel1.isConnected()); 412 assertTrue(s.isConnected()); 413 // not changed 414 assertEquals(s.getPort(), localAddr1.getPort()); 415 416 s.disconnect(); 417 assertFalse(this.channel1.isConnected()); 418 assertFalse(s.isConnected()); 419 420 s.close(); 421 assertTrue(s.isClosed()); 422 assertFalse(this.channel1.isOpen()); 423 } 424 425 // ------------------------------------------------------------------- 426 // Test for configureBlocking() 427 // ------------------------------------------------------------------- 428 429 public void testConfigureBlocking_Read() throws Exception { 430 assertTrue(this.channel1.isBlocking()); 431 ByteBuffer buf = ByteBuffer.allocate(CAPACITY_1KB); 432 new Thread() { 433 public void run() { 434 try { 435 sleep(TIME_UNIT * 5); 436 channel1.configureBlocking(false); 437 assertFalse(channel1.isBlocking()); 438 datagramSocket1.close(); 439 } catch (Exception e) { 440 // do nothing 441 } 442 } 443 }.start(); 444 SocketAddress addr = channel1.receive(buf); 445 assertNull(addr); 446 } 447 448 // ------------------------------------------------------------------- 449 // Test for isConnected() 450 // ------------------------------------------------------------------- 451 452 /** 453 * Test method for 'DatagramChannelImpl.isConnected()' 454 * 455 * @throws IOException 456 */ 457 public void testIsConnected_WithServer() throws IOException { 458 connectLocalServer(); 459 disconnectAfterConnected(); 460 this.datagramSocket1.close(); 461 this.channel1.close(); 462 assertFalse(this.channel1.isConnected()); 463 } 464 465 // ------------------------------------------------------------------- 466 // Test for connect() 467 // ------------------------------------------------------------------- 468 469 /** 470 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 471 */ 472 public void testConnect_BlockWithServer() throws IOException { 473 // blocking mode 474 assertTrue(this.channel1.isBlocking()); 475 connectLocalServer(); 476 datagramSocket1.close(); 477 disconnectAfterConnected(); 478 } 479 480 /** 481 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 482 */ 483 public void testConnect_BlockNoServer() throws IOException { 484 connectWithoutServer(); 485 disconnectAfterConnected(); 486 } 487 488 /** 489 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 490 * 491 * @throws IOException 492 */ 493 public void testConnect_NonBlockWithServer() throws IOException { 494 // Non blocking mode 495 this.channel1.configureBlocking(false); 496 connectLocalServer(); 497 datagramSocket1.close(); 498 disconnectAfterConnected(); 499 } 500 501 /** 502 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 503 * 504 * @throws IOException 505 */ 506 public void testConnect_Null() throws IOException { 507 assertFalse(this.channel1.isConnected()); 508 try { 509 this.channel1.connect(null); 510 fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 511 } catch (IllegalArgumentException e) { 512 // OK. 513 } 514 } 515 516 /** 517 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 518 * 519 * @throws IOException 520 */ 521 public void testConnect_UnsupportedType() throws IOException { 522 assertFalse(this.channel1.isConnected()); 523 class SubSocketAddress extends SocketAddress { 524 private static final long serialVersionUID = 1L; 525 526 public SubSocketAddress() { 527 super(); 528 } 529 } 530 SocketAddress newTypeAddress = new SubSocketAddress(); 531 try { 532 this.channel1.connect(newTypeAddress); 533 fail("Should throw an UnsupportedAddressTypeException here."); 534 } catch (UnsupportedAddressTypeException e) { 535 // OK. 536 } 537 } 538 539 /** 540 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 541 * 542 * @throws IOException 543 */ 544 public void testConnect_Unresolved() throws IOException { 545 assertFalse(this.channel1.isConnected()); 546 InetSocketAddress unresolved = new InetSocketAddress( 547 "unresolved address", 1080); 548 try { 549 this.channel1.connect(unresolved); 550 fail("Should throw an UnresolvedAddressException here."); //$NON-NLS-1$ 551 } catch (UnresolvedAddressException e) { 552 // OK. 553 } 554 } 555 556 public void testConnect_EmptyHost() throws Exception { 557 assertFalse(this.channel1.isConnected()); 558 559 assertEquals(this.channel1, this.channel1 560 .connect(new InetSocketAddress("", 1081))); //$NON-NLS-1$ 561 562 } 563 564 /** 565 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 566 * 567 * @throws IOException 568 * 569 */ 570 public void testConnect_ClosedChannelException() throws IOException { 571 assertFalse(this.channel1.isConnected()); 572 this.channel1.close(); 573 assertFalse(this.channel1.isOpen()); 574 try { 575 this.channel1.connect(localAddr1); 576 fail("Should throw ClosedChannelException."); //$NON-NLS-1$ 577 } catch (ClosedChannelException e) { 578 // OK. 579 } 580 } 581 582 /** 583 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 584 * 585 * @throws IOException 586 * 587 */ 588 public void testConnect_IllegalStateException() throws IOException { 589 assertFalse(this.channel1.isConnected()); 590 this.channel1.connect(localAddr1); 591 assertTrue(this.channel1.isConnected()); 592 // connect after connected. 593 try { 594 this.channel1.connect(localAddr1); 595 fail("Should throw IllegalStateException."); //$NON-NLS-1$ 596 } catch (IllegalStateException e) { 597 // OK. 598 } 599 } 600 601 /** 602 * Test method for 'DatagramChannelImpl.connect(SocketAddress)' 603 * 604 * @throws IOException 605 * 606 */ 607 public void testConnect_CheckOpenBeforeStatus() throws IOException { 608 assertFalse(this.channel1.isConnected()); 609 this.channel1.connect(localAddr1); 610 assertTrue(this.channel1.isConnected()); 611 // connect after connected. 612 this.channel1.close(); 613 assertFalse(this.channel1.isOpen()); 614 // checking open is before checking status. 615 try { 616 this.channel1.connect(localAddr1); 617 fail("Should throw ClosedChannelException."); //$NON-NLS-1$ 618 } catch (ClosedChannelException e) { 619 // OK. 620 } 621 } 622 623 private void disconnectAfterConnected() throws IOException { 624 assertTrue(this.channel1.isConnected()); 625 this.channel1.disconnect(); 626 assertFalse(this.channel1.isConnected()); 627 } 628 629 private void disconnectAfterClosed() throws IOException { 630 assertFalse(this.channel1.isOpen()); 631 assertFalse(this.channel1.isConnected()); 632 this.channel1.disconnect(); 633 assertFalse(this.channel1.isConnected()); 634 } 635 636 private void connectLocalServer() throws IOException { 637 assertFalse(this.channel1.isConnected()); 638 assertTrue(this.datagramSocket1.isBound()); 639 assertSame(this.channel1, this.channel1.connect(localAddr1)); 640 assertTrue(this.channel1.isConnected()); 641 } 642 643 private void connectWithoutServer() throws IOException { 644 assertFalse(this.channel1.isConnected()); 645 this.datagramSocket1.close(); 646 assertTrue(this.datagramSocket1.isClosed()); 647 assertSame(this.channel1, this.channel1.connect(localAddr1)); 648 assertTrue(this.channel1.isConnected()); 649 } 650 651 // ------------------------------------------------------------------- 652 // Test for disconnect() 653 // ------------------------------------------------------------------- 654 655 /** 656 * Test method for 'DatagramChannelImpl.disconnect()' 657 * 658 * @throws IOException 659 */ 660 public void testDisconnect_BeforeConnect() throws IOException { 661 assertFalse(this.channel1.isConnected()); 662 assertEquals(this.channel1, this.channel1.disconnect()); 663 assertFalse(this.channel1.isConnected()); 664 } 665 666 /** 667 * Test method for 'DatagramChannelImpl.disconnect()' 668 * 669 * @throws IOException 670 */ 671 public void testDisconnect_UnconnectedClosed() throws IOException { 672 assertFalse(this.channel1.isConnected()); 673 this.channel1.close(); 674 assertFalse(this.channel1.isOpen()); 675 assertEquals(this.channel1, this.channel1.disconnect()); 676 assertFalse(this.channel1.isConnected()); 677 } 678 679 /** 680 * Test method for 'DatagramChannelImpl.disconnect()' 681 * 682 * @throws IOException 683 */ 684 public void testDisconnect_BlockWithServerChannelClosed() 685 throws IOException { 686 assertTrue(this.channel1.isBlocking()); 687 connectLocalServer(); 688 // disconnect after channel close 689 this.channel1.close(); 690 disconnectAfterClosed(); 691 } 692 693 /** 694 * Test method for 'DatagramChannelImpl.disconnect()' 695 * 696 * @throws IOException 697 */ 698 public void testDisconnect_NonBlockWithServerChannelClosed() 699 throws IOException { 700 this.channel1.configureBlocking(false); 701 connectLocalServer(); 702 // disconnect after channel close 703 this.channel1.close(); 704 disconnectAfterClosed(); 705 } 706 707 /** 708 * Test method for 'DatagramChannelImpl.disconnect()' 709 * 710 * @throws IOException 711 */ 712 public void testDisconnect_BlockWithServerServerClosed() throws IOException { 713 assertTrue(this.channel1.isBlocking()); 714 connectLocalServer(); 715 // disconnect after server close 716 this.datagramSocket1.close(); 717 assertTrue(this.channel1.isOpen()); 718 assertTrue(this.channel1.isConnected()); 719 disconnectAfterConnected(); 720 } 721 722 /** 723 * Test method for 'DatagramChannelImpl.disconnect()' 724 * 725 * @throws IOException 726 */ 727 public void testDisconnect_NonBlockWithServerServerClosed() 728 throws IOException { 729 this.channel1.configureBlocking(false); 730 assertFalse(this.channel1.isBlocking()); 731 connectLocalServer(); 732 // disconnect after server close 733 this.datagramSocket1.close(); 734 assertTrue(this.channel1.isOpen()); 735 assertTrue(this.channel1.isConnected()); 736 disconnectAfterConnected(); 737 } 738 739 // ------------------------------------------------------------------- 740 // Test for receive(): Behavior Without Server. 741 // ------------------------------------------------------------------- 742 743 /** 744 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 745 * 746 * @throws Exception 747 */ 748 public void testReceive_UnconnectedNull() throws Exception { 749 assertFalse(this.channel1.isConnected()); 750 try { 751 this.channel1.receive(null); 752 fail("Should throw a NPE here."); //$NON-NLS-1$ 753 } catch (NullPointerException e) { 754 // OK. 755 } 756 } 757 758 /** 759 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 760 * 761 * @throws Exception 762 */ 763 public void testReceive_UnconnectedReadonly() throws Exception { 764 assertFalse(this.channel1.isConnected()); 765 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 766 .asReadOnlyBuffer(); 767 assertTrue(dst.isReadOnly()); 768 try { 769 this.channel1.receive(dst); 770 fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 771 } catch (IllegalArgumentException e) { 772 // OK. 773 } 774 } 775 776 /** 777 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 778 * 779 * @throws Exception 780 */ 781 public void testReceive_UnconnectedBufEmpty() throws Exception { 782 this.channel1.configureBlocking(false); 783 assertFalse(this.channel1.isConnected()); 784 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 785 assertNull(this.channel1.receive(dst)); 786 } 787 788 /** 789 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 790 * 791 * @throws Exception 792 */ 793 public void testReceive_UnconnectedBufZero() throws Exception { 794 assertFalse(this.channel1.isConnected()); 795 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ZERO); 796 assertNull(this.channel1.receive(dst)); 797 } 798 799 /** 800 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 801 * 802 * @throws Exception 803 */ 804 public void testReceive_UnconnectedBufNotEmpty() throws Exception { 805 assertFalse(this.channel1.isConnected()); 806 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 807 // buf is not empty 808 dst.put((byte) 88); 809 assertEquals(dst.position() + CAPACITY_NORMAL - 1, dst.limit()); 810 assertNull(this.channel1.receive(dst)); 811 } 812 813 /** 814 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 815 * 816 * @throws Exception 817 */ 818 public void testReceive_UnconnectedBufFull() throws Exception { 819 assertFalse(this.channel1.isConnected()); 820 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE); 821 // buf is full 822 dst.put((byte) 88); 823 assertEquals(dst.position(), dst.limit()); 824 assertNull(this.channel1.receive(dst)); 825 } 826 827 /** 828 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 829 * 830 * @throws Exception 831 */ 832 public void testReceive_UnconnectedClose() throws Exception { 833 assertFalse(this.channel1.isConnected()); 834 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 835 this.channel1.close(); 836 assertFalse(this.channel1.isOpen()); 837 try { 838 assertNull(this.channel1.receive(dst)); 839 fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$ 840 } catch (ClosedChannelException e) { 841 // OK. 842 } 843 } 844 845 /** 846 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 847 * 848 * @throws Exception 849 */ 850 public void testReceive_UnconnectedCloseNull() throws Exception { 851 assertFalse(this.channel1.isConnected()); 852 this.channel1.close(); 853 assertFalse(this.channel1.isOpen()); 854 // checking buffer before checking open 855 try { 856 this.channel1.receive(null); 857 fail("Should throw a NPE here."); //$NON-NLS-1$ 858 } catch (NullPointerException e) { 859 // OK. 860 } 861 } 862 863 /** 864 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 865 * 866 * @throws Exception 867 */ 868 public void testReceive_UnconnectedCloseReadonly() throws Exception { 869 assertFalse(this.channel1.isConnected()); 870 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 871 .asReadOnlyBuffer(); 872 assertTrue(dst.isReadOnly()); 873 this.channel1.close(); 874 assertFalse(this.channel1.isOpen()); 875 try { 876 this.channel1.receive(dst); 877 fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 878 } catch (IllegalArgumentException e) { 879 // OK. 880 } 881 } 882 883 /** 884 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 885 * 886 * @throws Exception 887 */ 888 public void testReceive_NonBlockNoServerBufEmpty() throws Exception { 889 this.channel1.configureBlocking(false); 890 receiveNonBlockNoServer(CAPACITY_NORMAL); 891 } 892 893 /** 894 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 895 * 896 * @throws Exception 897 */ 898 public void testReceive_BlockNoServerNull() throws Exception { 899 assertTrue(this.channel1.isBlocking()); 900 receiveNoServerNull(); 901 } 902 903 /** 904 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 905 * 906 * @throws Exception 907 */ 908 public void testReceive_NonBlockNoServerNull() throws Exception { 909 this.channel1.configureBlocking(false); 910 receiveNoServerNull(); 911 } 912 913 /** 914 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 915 * 916 * @throws Exception 917 */ 918 public void testReceive_BlockNoServerReadonly() throws Exception { 919 assertTrue(this.channel1.isBlocking()); 920 receiveNoServerReadonly(); 921 } 922 923 /** 924 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 925 * 926 * @throws Exception 927 */ 928 public void testReceive_NonBlockNoServerReadonly() throws Exception { 929 this.channel1.configureBlocking(false); 930 receiveNoServerReadonly(); 931 } 932 933 /** 934 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 935 * 936 * @throws Exception 937 */ 938 public void testReceive_NonBlockNoServerBufZero() throws Exception { 939 this.channel1.configureBlocking(false); 940 receiveNonBlockNoServer(CAPACITY_ZERO); 941 } 942 943 /** 944 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 945 * 946 * @throws Exception 947 */ 948 public void testReceive_NonBlockNoServerBufNotEmpty() throws Exception { 949 this.channel1.configureBlocking(false); 950 connectWithoutServer(); 951 ByteBuffer dst = allocateNonEmptyBuf(); 952 assertNull(this.channel1.receive(dst)); 953 } 954 955 956 /** 957 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 958 * 959 * @throws Exception 960 */ 961 public void testReceive_NonBlockNoServerBufFull() throws Exception { 962 this.channel1.configureBlocking(false); 963 connectWithoutServer(); 964 ByteBuffer dst = allocateFullBuf(); 965 assertNull(this.channel1.receive(dst)); 966 } 967 968 /** 969 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 970 * 971 * @throws Exception 972 */ 973 public void testReceive_BlockNoServerChannelClose() throws Exception { 974 assertTrue(this.channel1.isBlocking()); 975 receiveNoServerChannelClose(); 976 } 977 978 /** 979 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 980 * 981 * @throws Exception 982 */ 983 public void testReceive_NonBlockNoServerChannelClose() throws Exception { 984 this.channel1.configureBlocking(false); 985 receiveNoServerChannelClose(); 986 } 987 988 /** 989 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 990 * 991 * @throws Exception 992 */ 993 public void testReceive_BlockNoServerCloseNull() throws Exception { 994 assertTrue(this.channel1.isBlocking()); 995 receiveNoServerChannelCloseNull(); 996 } 997 998 /** 999 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 1000 * 1001 * @throws Exception 1002 */ 1003 public void testReceive_NonBlockNoServerCloseNull() throws Exception { 1004 this.channel1.configureBlocking(false); 1005 receiveNoServerChannelCloseNull(); 1006 } 1007 1008 /** 1009 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 1010 * 1011 * @throws Exception 1012 */ 1013 public void testReceive_NonBlockNoServerCloseReadonly() throws Exception { 1014 this.channel1.configureBlocking(false); 1015 receiveNoServerChannelCloseReadonly(); 1016 } 1017 1018 /** 1019 * Test method for 'DatagramChannelImpl.receive(ByteBuffer)' 1020 * 1021 * @throws Exception 1022 */ 1023 public void testReceive_BlockNoServerCloseReadonly() throws Exception { 1024 assertTrue(this.channel1.isBlocking()); 1025 receiveNoServerChannelCloseReadonly(); 1026 } 1027 1028 private void receiveNoServerNull() throws IOException { 1029 connectWithoutServer(); 1030 try { 1031 this.channel1.receive(null); 1032 fail("Should throw a NPE here."); //$NON-NLS-1$ 1033 } catch (NullPointerException e) { 1034 // OK. 1035 } 1036 } 1037 1038 private void receiveNoServerReadonly() throws IOException { 1039 connectWithoutServer(); 1040 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 1041 .asReadOnlyBuffer(); 1042 assertTrue(dst.isReadOnly()); 1043 try { 1044 this.channel1.receive(dst); 1045 fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 1046 } catch (IllegalArgumentException e) { 1047 // OK. 1048 } 1049 } 1050 1051 private void receiveNonBlockNoServer(int size) throws IOException { 1052 connectWithoutServer(); 1053 ByteBuffer dst = ByteBuffer.allocateDirect(size); 1054 assertNull(this.channel1.receive(dst)); 1055 } 1056 1057 private void receiveNoServerChannelClose() throws IOException { 1058 connectWithoutServer(); 1059 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1060 this.channel1.close(); 1061 assertFalse(this.channel1.isOpen()); 1062 try { 1063 assertNull(this.channel1.receive(dst)); 1064 fail("Should throw a ClosedChannelException here."); //$NON-NLS-1$ 1065 } catch (ClosedChannelException e) { 1066 // OK. 1067 } 1068 } 1069 1070 private void receiveNoServerChannelCloseNull() throws IOException { 1071 connectWithoutServer(); 1072 this.channel1.close(); 1073 assertFalse(this.channel1.isOpen()); 1074 try { 1075 this.channel1.receive(null); 1076 fail("Should throw a NPE here."); //$NON-NLS-1$ 1077 } catch (NullPointerException e) { 1078 // OK. 1079 } 1080 } 1081 1082 private void receiveNoServerChannelCloseReadonly() throws IOException { 1083 connectWithoutServer(); 1084 this.channel1.close(); 1085 assertFalse(this.channel1.isOpen()); 1086 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL) 1087 .asReadOnlyBuffer(); 1088 assertTrue(dst.isReadOnly()); 1089 try { 1090 this.channel1.receive(dst); 1091 fail("Should throw an IllegalArgumentException here."); //$NON-NLS-1$ 1092 } catch (IllegalArgumentException e) { 1093 // OK. 1094 } 1095 } 1096 1097 private ByteBuffer allocateFullBuf() { 1098 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_ONE); 1099 // buf is full 1100 dst.put((byte) 88); 1101 assertEquals(dst.position(), dst.limit()); 1102 return dst; 1103 } 1104 1105 private ByteBuffer allocateNonEmptyBuf() { 1106 ByteBuffer dst = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1107 // buf is not empty 1108 dst.put((byte) 88); 1109 dst.put((byte) 99); 1110 assertEquals(dst.position() + CAPACITY_NORMAL - 2, dst.limit()); 1111 return dst; 1112 } 1113 1114 // ------------------------------------------------------------------- 1115 // Test for send(): Behavior without server. 1116 // ------------------------------------------------------------------- 1117 1118 private void sendDataBlocking(InetSocketAddress addr, ByteBuffer writeBuf) 1119 throws IOException { 1120 InetSocketAddress ipAddr = addr; 1121 assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr)); 1122 assertTrue(this.channel1.isOpen()); 1123 assertTrue(this.channel1.isBlocking()); 1124 this.channel1.connect(ipAddr); 1125 assertTrue(this.channel1.isConnected()); 1126 } 1127 1128 private void sendDataNonBlocking(InetSocketAddress addr, ByteBuffer writeBuf) 1129 throws IOException { 1130 InetSocketAddress ipAddr = addr; 1131 this.channel1.configureBlocking(false); 1132 assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr)); 1133 assertTrue(this.channel1.isOpen()); 1134 assertFalse(this.channel1.isBlocking()); 1135 this.channel1.connect(ipAddr); 1136 assertTrue(this.channel1.isConnected()); 1137 } 1138 1139 /* 1140 * Test method for 'DatagramChannelImpl.send(ByteBuffer, SocketAddress)' 1141 */ 1142 public void testSend_NoServerBlockingCommon() throws IOException { 1143 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1144 sendDataBlocking(localAddr1, writeBuf); 1145 } 1146 1147 public void testSend_NoServerNonblockingCommon() throws IOException { 1148 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1149 sendDataNonBlocking(localAddr1, writeBuf); 1150 } 1151 1152 public void testSend_NoServerTwice() throws IOException { 1153 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1154 sendDataBlocking(localAddr1, writeBuf); 1155 // can not buffer twice! 1156 assertEquals(0, this.channel1.send(writeBuf, localAddr1)); 1157 try { 1158 channel1.send(writeBuf, localAddr2); 1159 fail("Should throw IllegalArgumentException"); 1160 } catch (IllegalArgumentException e) { 1161 // correct 1162 } 1163 } 1164 1165 public void testSend_NoServerNonBlockingTwice() throws IOException { 1166 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1167 sendDataNonBlocking(localAddr1, writeBuf); 1168 // can not buffer twice! 1169 assertEquals(0, this.channel1.send(writeBuf, localAddr1)); 1170 try { 1171 channel1.send(writeBuf, localAddr2); 1172 fail("Should throw IllegalArgumentException"); 1173 } catch (IllegalArgumentException e) { 1174 // correct 1175 } 1176 } 1177 1178 public void testSend_NoServerBufNull() throws IOException { 1179 try { 1180 sendDataBlocking(localAddr1, null); 1181 fail("Should throw a NPE here."); 1182 } catch (NullPointerException e) { 1183 // correct 1184 } 1185 } 1186 1187 public void testSend_NoServerBufNullTwice() throws IOException { 1188 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1189 try { 1190 sendDataBlocking(localAddr1, null); 1191 fail("Should throw a NPE here."); 1192 } catch (NullPointerException e) { 1193 // correct 1194 } 1195 sendDataBlocking(localAddr1, writeBuf); 1196 try { 1197 channel1.send(null, localAddr2); 1198 fail("Should throw NPE"); 1199 } catch (NullPointerException e) { 1200 // correct 1201 } 1202 } 1203 1204 public void testSend_NoServerAddrNull() throws IOException { 1205 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1206 try { 1207 sendDataBlocking(null, writeBuf); 1208 fail("Should throw a NPE here."); 1209 } catch (NullPointerException e) { 1210 // correct 1211 } 1212 } 1213 1214 public void testSend_NoServerAddrNullTwice() throws IOException { 1215 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1216 try { 1217 sendDataBlocking(null, writeBuf); 1218 fail("Should throw a NPE here."); 1219 } catch (NullPointerException e) { 1220 // correct 1221 } 1222 sendDataBlocking(localAddr1, writeBuf); 1223 try { 1224 channel1.send(writeBuf, null); 1225 fail("Should throw NPE"); 1226 } catch (NullPointerException e) { 1227 // correct 1228 } 1229 } 1230 1231 // ------------------------------------------------------------------- 1232 // Test for receive()and send(): Send and Receive with Real Data 1233 // ------------------------------------------------------------------- 1234 1235 public void testReceiveSend_Block_Normal() throws Exception { 1236 this.channel1.socket().bind(localAddr2); 1237 sendByChannel("some normal string in testReceiveSend_Normal", 1238 localAddr2); 1239 receiveByChannel(CAPACITY_NORMAL, localAddr2, 1240 "some normal string in testReceiveSend_Normal"); 1241 } 1242 1243 public void testReceiveSend_Block_NotBound() throws Exception { 1244 // not bound 1245 sendByChannel("some normal string in testReceiveSend_Normal", 1246 localAddr2); 1247 ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 1248 assertNull(channel1.receive(buf)); 1249 assertFalse(channel1.socket().isBound()); 1250 } 1251 1252 public void testReceiveSend_NonBlock_NotBound() throws Exception { 1253 // not bound 1254 this.channel1.configureBlocking(false); 1255 this.channel2.configureBlocking(false); 1256 sendByChannel("some normal string in testReceiveSend_Normal", 1257 localAddr2); 1258 ByteBuffer buf = ByteBuffer.wrap(new byte[CAPACITY_NORMAL]); 1259 assertNull((InetSocketAddress) this.channel1.receive(buf)); 1260 } 1261 1262 public void testReceiveSend_Block_Normal_S2C() throws Exception { 1263 this.channel1.socket().bind(localAddr2); 1264 sendByDatagramSocket( 1265 "some normal string in testReceiveSend_Normal_S2C", localAddr2); 1266 receiveByChannel(CAPACITY_NORMAL, localAddr2, 1267 "some normal string in testReceiveSend_Normal_S2C"); 1268 } 1269 1270 public void testReceiveSend_Block_Normal_C2S() throws Exception { 1271 this.datagramSocket1 = new DatagramSocket(localAddr2.getPort()); 1272 String str1 = "some normal string in testReceiveSend_Normal_C2S"; 1273 sendByChannel(str1, localAddr2); 1274 receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, str1); 1275 } 1276 1277 public void testReceiveSend_NonBlock_Normal_C2S() throws Exception { 1278 this.channel1.configureBlocking(false); 1279 this.channel2.configureBlocking(false); 1280 this.datagramSocket1 = new DatagramSocket(localAddr2.getPort()); 1281 String str1 = "some normal string in testReceiveSend_Normal_C2S"; 1282 sendByChannel(str1, localAddr2); 1283 receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, str1); 1284 } 1285 1286 public void testReceiveSend_Normal_S2S() throws Exception { 1287 String msg = "normal string in testReceiveSend_Normal_S2S"; 1288 this.datagramSocket1 = new DatagramSocket(testPort); 1289 DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(), 1290 localAddr2); 1291 datagramSocket2 = new DatagramSocket(localAddr2.getPort()); 1292 this.datagramSocket1.send(rdp); 1293 byte[] buf = new byte[CAPACITY_NORMAL]; 1294 this.datagramSocket2.setSoTimeout(TIME_UNIT); 1295 rdp = new DatagramPacket(buf, buf.length); 1296 this.datagramSocket2.receive(rdp); 1297 assertEquals(new String(buf, 0, CAPACITY_NORMAL).trim(), msg); 1298 } 1299 1300 public void testReceiveSend_Block_Empty() throws Exception { 1301 this.channel1.socket().bind(localAddr2); 1302 sendByChannel("", localAddr2); 1303 receiveByChannel(CAPACITY_NORMAL, localAddr2, ""); 1304 } 1305 1306 public void testReceiveSend_NonBlock_Empty() throws Exception { 1307 this.channel1.configureBlocking(false); 1308 this.channel2.configureBlocking(false); 1309 this.channel1.socket().bind(localAddr2); 1310 sendByChannel("", localAddr2); 1311 receiveByChannel(CAPACITY_NORMAL, localAddr2, ""); 1312 } 1313 1314 public void testReceiveSend_Block_Empty_S2C() throws Exception { 1315 this.channel1.socket().bind(localAddr2); 1316 sendByDatagramSocket("", localAddr2); 1317 receiveByChannel(CAPACITY_NORMAL, localAddr2, ""); 1318 } 1319 1320 public void testReceiveSend_NonBlock_Empty_S2C() throws Exception { 1321 this.channel1.configureBlocking(false); 1322 this.channel2.configureBlocking(false); 1323 this.channel1.socket().bind(localAddr2); 1324 sendByDatagramSocket("", localAddr2); 1325 receiveByChannel(CAPACITY_NORMAL, localAddr2, ""); 1326 } 1327 1328 public void testReceiveSend_Block_Empty_C2S() throws Exception { 1329 this.datagramSocket1 = new DatagramSocket(localAddr2.getPort()); 1330 sendByChannel("", localAddr2); 1331 receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, ""); 1332 } 1333 1334 public void testReceiveSend_NonBlock_Empty_C2S() throws Exception { 1335 this.channel1.configureBlocking(false); 1336 this.channel2.configureBlocking(false); 1337 this.datagramSocket1 = new DatagramSocket(localAddr2.getPort()); 1338 sendByChannel("", localAddr2); 1339 receiveByDatagramSocket(CAPACITY_NORMAL, localAddr2, ""); 1340 } 1341 1342 public void testReceiveSend_Empty_S2S() throws Exception { 1343 String msg = ""; 1344 this.datagramSocket1 = new DatagramSocket(testPort); 1345 DatagramPacket rdp = new DatagramPacket(msg.getBytes(), msg.length(), 1346 localAddr2); 1347 datagramSocket2 = new DatagramSocket(localAddr2.getPort()); 1348 this.datagramSocket1.send(rdp); 1349 byte[] buf = new byte[CAPACITY_NORMAL]; 1350 this.datagramSocket2.setSoTimeout(TIME_UNIT); 1351 rdp = new DatagramPacket(buf, buf.length); 1352 this.datagramSocket2.receive(rdp); 1353 assertEquals(new String(buf, 0, CAPACITY_NORMAL).trim(), msg); 1354 } 1355 1356 public void testReceiveSend_Block_Oversize() throws Exception { 1357 this.channel1.socket().bind(localAddr2); 1358 sendByChannel("0123456789", localAddr2); 1359 receiveByChannel(5, localAddr2, "01234"); 1360 } 1361 1362 public void testReceiveSend_Block_Oversize_C2S() throws Exception { 1363 this.datagramSocket1 = new DatagramSocket(localAddr2.getPort()); 1364 sendByChannel("0123456789", localAddr2); 1365 receiveByDatagramSocket(5, localAddr2, "01234"); 1366 } 1367 1368 public void testReceiveSend_NonBlock_Oversize_C2S() throws Exception { 1369 this.channel1.configureBlocking(false); 1370 this.channel2.configureBlocking(false); 1371 this.datagramSocket1 = new DatagramSocket(localAddr2.getPort()); 1372 sendByChannel("0123456789", localAddr2); 1373 receiveByDatagramSocket(5, localAddr2, "01234"); 1374 } 1375 1376 public void testReceiveSend_Block_Oversize_S2C() throws Exception { 1377 this.channel1.socket().bind(localAddr2); 1378 sendByDatagramSocket("0123456789", localAddr2); 1379 receiveByChannel(5, localAddr2, "01234"); 1380 } 1381 1382 public void testReceiveSend_8K() throws Exception { 1383 StringBuffer str8k = new StringBuffer(); 1384 for (int i = 0; i < 8 * CAPACITY_1KB; i++) { 1385 str8k.append('a'); 1386 } 1387 String str = str8k.toString(); 1388 this.channel1.socket().bind(localAddr2); 1389 sendByChannel(str, localAddr2); 1390 receiveByChannel(8 * CAPACITY_1KB, localAddr2, str); 1391 } 1392 1393 public void testReceiveSend_64K() throws Exception { 1394 StringBuffer str64k = new StringBuffer(); 1395 for (int i = 0; i < CAPACITY_64KB; i++) { 1396 str64k.append('a'); 1397 } 1398 String str = str64k.toString(); 1399 try { 1400 Thread.sleep(TIME_UNIT); 1401 channel2.send(ByteBuffer.wrap(str.getBytes()), localAddr1); 1402 fail("Should throw SocketException!"); 1403 } catch (SocketException e) { 1404 //expected 1405 } 1406 } 1407 1408 private void sendByChannel(String data, InetSocketAddress address) 1409 throws Exception { 1410 try { 1411 assertEquals(data.length(), this.channel2.send(ByteBuffer.wrap(data 1412 .getBytes()), address)); 1413 } finally { 1414 this.channel2.close(); 1415 } 1416 } 1417 1418 private void sendByDatagramSocket(String data, InetSocketAddress address) 1419 throws Exception { 1420 this.datagramSocket1 = new DatagramSocket(testPort); 1421 DatagramPacket rdp = new DatagramPacket(data.getBytes(), data.length(), 1422 address); 1423 this.datagramSocket1.send(rdp); 1424 } 1425 1426 private void receiveByChannel(int bufSize, InetSocketAddress address, 1427 String expectedString) throws IOException { 1428 try { 1429 ByteBuffer buf = ByteBuffer.wrap(new byte[bufSize]); 1430 InetSocketAddress returnAddr = null; 1431 long startTime = System.currentTimeMillis(); 1432 do { 1433 returnAddr = (InetSocketAddress) this.channel1.receive(buf); 1434 // continue loop when channel1 is non-blocking and no data was 1435 // received. 1436 if (channel1.isBlocking() || null != returnAddr) { 1437 break; 1438 } 1439 // avoid dead loop 1440 assertTimeout(startTime, 10000); 1441 } while (true); 1442 int length = returnAddr.getAddress().getAddress().length; 1443 for (int i = 0; i < length; i++) { 1444 assertEquals(returnAddr.getAddress().getAddress()[i], 1445 InetAddress.getByName("127.0.0.1").getAddress()[i]); 1446 } 1447 // port is NOT equal 1448 assertFalse(returnAddr.getPort() == address.getPort()); 1449 assertEquals(new String(buf.array(), 0, bufSize).trim(), 1450 expectedString); 1451 } finally { 1452 this.channel1.close(); 1453 } 1454 } 1455 1456 /* 1457 * Fails if the difference between current time and start time is greater 1458 * than timeout. 1459 */ 1460 private void assertTimeout(long startTime, long timeout) { 1461 long currentTime = System.currentTimeMillis(); 1462 if ((currentTime - startTime) > timeout) { 1463 fail("Timeout"); 1464 } 1465 } 1466 1467 private void receiveByDatagramSocket(int bufSize, 1468 InetSocketAddress address, String expectedString) 1469 throws IOException { 1470 byte[] buf = new byte[bufSize]; 1471 this.datagramSocket1.setSoTimeout(6000); 1472 DatagramPacket rdp = new DatagramPacket(buf, buf.length); 1473 this.datagramSocket1.receive(rdp); 1474 assertEquals(new String(buf, 0, bufSize).trim(), expectedString); 1475 } 1476 1477 public void testRead_NoSecurity() throws Exception { 1478 ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 1479 String strHello = "hello"; 1480 localAddr1 = new InetSocketAddress("127.0.0.1", testPort); 1481 this.channel1.socket().bind(localAddr1); 1482 this.channel2.socket().bind(localAddr2); 1483 this.channel1.connect(localAddr2); 1484 this.channel2.send(ByteBuffer.wrap(strHello.getBytes()), localAddr1); 1485 assertEquals(strHello.length(), this.channel1.read(buf)); 1486 assertAscii(buf, strHello); 1487 } 1488 1489 public void testReceive_Peek_NoSecurity_Nonblocking() throws Exception { 1490 String strHello = "hello"; 1491 localAddr1 = new InetSocketAddress("127.0.0.1", testPort); 1492 this.channel1.socket().bind(localAddr1); 1493 sendByChannel(strHello, localAddr1); 1494 this.channel1.configureBlocking(false); 1495 // for accepted addr, no problem. 1496 ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 1497 InetSocketAddress source = (InetSocketAddress) this.channel1.receive(buf); 1498 assertEquals(localAddr1.getAddress(), source.getAddress()); 1499 assertAscii(buf, strHello); 1500 } 1501 1502 private static void assertAscii(ByteBuffer b, String s) { 1503 assertEquals(s.length(), b.position()); 1504 for (int i = 0; i < s.length(); ++i) { 1505 assertEquals(s.charAt(i), b.get(i)); 1506 } 1507 } 1508 1509 // ------------------------------------------------------------------- 1510 // Test for write() 1511 // ------------------------------------------------------------------- 1512 1513 private void connectWriteBuf(InetSocketAddress ipAddr, ByteBuffer buf) 1514 throws IOException { 1515 this.channel1.connect(ipAddr); 1516 assertTrue(this.channel1.isConnected()); 1517 assertEquals(CAPACITY_NORMAL, this.channel1.write(buf)); 1518 assertEquals(0, this.channel1.write(buf)); 1519 } 1520 1521 private void noconnectWrite(ByteBuffer buf) throws IOException { 1522 try { 1523 this.channel1.write(buf); 1524 fail("should throw NotYetConnectedException"); 1525 } catch (NotYetConnectedException e) { 1526 // correct 1527 } 1528 } 1529 1530 /* 1531 * Test method for 'DatagramChannelImpl.write(ByteBuffer)' 1532 */ 1533 public void testWriteByteBuffer_Block() throws IOException { 1534 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1535 connectWriteBuf(localAddr1, writeBuf); 1536 } 1537 1538 public void testWriteByteBuffer_NonBlock() throws IOException { 1539 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1540 this.channel1.configureBlocking(false); 1541 connectWriteBuf(localAddr1, writeBuf); 1542 } 1543 1544 public void testWriteByteBuffer_Block_closed() throws IOException { 1545 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1546 InetSocketAddress ipAddr = localAddr1; 1547 noconnectWrite(writeBuf); 1548 this.channel1.connect(ipAddr); 1549 assertTrue(this.channel1.isConnected()); 1550 this.channel1.close(); 1551 try { 1552 channel1.write(writeBuf); 1553 fail("should throw ClosedChannelException"); 1554 } catch (ClosedChannelException e) { 1555 // correct 1556 } 1557 } 1558 1559 public void testWriteByteBuffer_NonBlock_closed() throws IOException { 1560 ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1561 InetSocketAddress ipAddr = localAddr1; 1562 // non block mode 1563 this.channel1.configureBlocking(false); 1564 noconnectWrite(writeBuf); 1565 this.channel1.connect(ipAddr); 1566 assertTrue(this.channel1.isConnected()); 1567 this.channel1.close(); 1568 try { 1569 channel1.write(writeBuf); 1570 fail("should throw ClosedChannelException"); 1571 } catch (ClosedChannelException e) { 1572 // correct 1573 } 1574 } 1575 1576 public void testWriteByteBuffer_Block_BufNull() throws IOException { 1577 ByteBuffer writeBuf = ByteBuffer.allocateDirect(0); 1578 InetSocketAddress ipAddr = localAddr1; 1579 try { 1580 this.channel1.write((ByteBuffer) null); 1581 fail("Should throw NPE."); 1582 } catch (NullPointerException e) { 1583 // correct 1584 } 1585 this.channel1.connect(ipAddr); 1586 assertTrue(this.channel1.isConnected()); 1587 try { 1588 this.channel1.write((ByteBuffer) null); 1589 fail("Should throw NPE."); 1590 } catch (NullPointerException e) { 1591 // correct 1592 } 1593 assertEquals(0, this.channel1.write(writeBuf)); 1594 datagramSocket1.close(); 1595 try { 1596 this.channel1.write((ByteBuffer) null); 1597 fail("Should throw NPE."); 1598 } catch (NullPointerException e) { 1599 // correct 1600 } 1601 } 1602 1603 public void testWriteByteBuffer_NonBlock_BufNull() throws IOException { 1604 ByteBuffer writeBuf = ByteBuffer.allocateDirect(0); 1605 InetSocketAddress ipAddr = localAddr1; 1606 1607 // non block mode 1608 this.channel1.configureBlocking(false); 1609 1610 try { 1611 this.channel1.write((ByteBuffer) null); 1612 fail("Should throw NPE."); 1613 } catch (NullPointerException e) { 1614 // correct 1615 } 1616 this.channel1.connect(ipAddr); 1617 assertTrue(this.channel1.isConnected()); 1618 try { 1619 this.channel1.write((ByteBuffer) null); 1620 fail("Should throw NPE."); 1621 } catch (NullPointerException e) { 1622 // correct 1623 } 1624 assertEquals(0, this.channel1.write(writeBuf)); 1625 datagramSocket1.close(); 1626 try { 1627 this.channel1.write((ByteBuffer) null); 1628 fail("Should throw NPE."); 1629 } catch (NullPointerException e) { 1630 // correct 1631 } 1632 } 1633 1634 /* 1635 * Test method for 'DatagramChannelImpl.write(ByteBuffer[], int, int)' 1636 */ 1637 public void testWriteByteBufferArrayIntInt_Block() throws IOException { 1638 ByteBuffer[] writeBuf = new ByteBuffer[2]; 1639 writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1640 writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1641 InetSocketAddress ipAddr = localAddr1; 1642 try { 1643 this.channel1.write(writeBuf, 0, 2); 1644 fail("Should throw NotYetConnectedException."); 1645 } catch (NotYetConnectedException e) { 1646 // correct 1647 } 1648 this.channel1.connect(ipAddr); 1649 assertTrue(this.channel1.isConnected()); 1650 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 1651 // cannot be buffered again! 1652 assertEquals(0, this.channel1.write(writeBuf, 0, 1)); 1653 1654 } 1655 1656 public void testWriteByteBufferArrayIntInt_NonBlock() throws IOException { 1657 ByteBuffer[] writeBuf = new ByteBuffer[2]; 1658 writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1659 writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1660 InetSocketAddress ipAddr = localAddr1; 1661 // non-block mode 1662 this.channel1.configureBlocking(false); 1663 try { 1664 this.channel1.write(writeBuf, 0, 2); 1665 fail("Should throw NotYetConnectedException."); 1666 } catch (NotYetConnectedException e) { 1667 // correct 1668 } 1669 this.channel1.connect(ipAddr); 1670 assertTrue(this.channel1.isConnected()); 1671 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 1672 // cannot be buffered again! 1673 assertEquals(0, this.channel1.write(writeBuf, 0, 1)); 1674 1675 } 1676 1677 public void testWriteByteBufferArrayIntInt_NoConnectIndexBad() 1678 throws IOException { 1679 ByteBuffer[] writeBuf = new ByteBuffer[2]; 1680 writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1681 writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1682 InetSocketAddress ipAddr = localAddr1; 1683 try { 1684 this.channel1.write(writeBuf, -1, 2); 1685 fail("should throw IndexOutOfBoundsException"); 1686 } catch (IndexOutOfBoundsException e) { 1687 // correct 1688 } 1689 try { 1690 this.channel1.write(writeBuf, 0, -1); 1691 fail("should throw IndexOutOfBoundsException"); 1692 } catch (IndexOutOfBoundsException e) { 1693 // correct 1694 } 1695 this.channel1.connect(ipAddr); 1696 assertTrue(this.channel1.isConnected()); 1697 assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); 1698 // cannot be buffered again! 1699 assertEquals(0, this.channel1.write(writeBuf, 0, 1)); 1700 } 1701 1702 public void testWriteByteBufferArrayIntInt_ConnectedIndexBad() 1703 throws IOException { 1704 ByteBuffer[] writeBuf = new ByteBuffer[2]; 1705 writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1706 writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1707 InetSocketAddress ipAddr = localAddr1; 1708 this.channel1.connect(ipAddr); 1709 assertTrue(this.channel1.isConnected()); 1710 try { 1711 this.channel1.write(writeBuf, -1, 2); 1712 fail("should throw IndexOutOfBoundsException"); 1713 } catch (IndexOutOfBoundsException e) { 1714 // correct 1715 } 1716 try { 1717 this.channel1.write(writeBuf, 0, -1); 1718 fail("should throw IndexOutOfBoundsException"); 1719 } catch (IndexOutOfBoundsException e) { 1720 // correct 1721 } 1722 } 1723 1724 public void testWriteByteBufferArrayIntInt_BufNullNoConnect() 1725 throws IOException { 1726 ByteBuffer[] writeBuf = new ByteBuffer[2]; 1727 writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1728 try { 1729 this.channel1.write(null, 0, 2); 1730 fail(); 1731 } catch (NullPointerException expected) { 1732 } 1733 try { 1734 this.channel1.write(writeBuf, -1, 2); 1735 fail(); 1736 } catch (IndexOutOfBoundsException expected) { 1737 } 1738 try { 1739 this.channel1.write(writeBuf, 0, 3); 1740 fail(); 1741 } catch (IndexOutOfBoundsException expected) { 1742 } 1743 } 1744 1745 public void testWriteByteBufferArrayIntInt_BufNullConnect() 1746 throws IOException { 1747 ByteBuffer[] writeBuf = new ByteBuffer[2]; 1748 writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1749 InetSocketAddress ipAddr = localAddr1; 1750 this.channel1.connect(ipAddr); 1751 assertTrue(this.channel1.isConnected()); 1752 try { 1753 this.channel1.write(null, 0, 2); 1754 fail("should throw NPE"); 1755 } catch (NullPointerException e) { 1756 // correct 1757 } 1758 try { 1759 this.channel1.write(writeBuf, 0, 3); 1760 fail("should throw IndexOutOfBoundsException"); 1761 } catch (IndexOutOfBoundsException e) { 1762 // correct 1763 } 1764 datagramSocket1.close(); 1765 try { 1766 this.channel1.write(null, 0, 2); 1767 fail("should throw NPE"); 1768 } catch (NullPointerException e) { 1769 // correct 1770 } 1771 } 1772 1773 // ------------------------------------------------------------------- 1774 // Test for read() 1775 // ------------------------------------------------------------------- 1776 1777 /* 1778 * Test method for 'DatagramChannelImpl.read(ByteBuffer)' 1779 */ 1780 public void testReadByteBuffer() throws IOException { 1781 ByteBuffer readBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1782 try { 1783 this.channel1.read(readBuf); 1784 fail("should throw NotYetConnectedException"); 1785 } catch (NotYetConnectedException e) { 1786 // correct 1787 } 1788 this.channel1.connect(localAddr1); 1789 assertTrue(this.channel1.isConnected()); 1790 this.channel1.configureBlocking(false); 1791 // note : blocking-mode will make the read process endless! 1792 assertEquals(0, this.channel1.read(readBuf)); 1793 this.channel1.close(); 1794 try { 1795 this.channel1.read(readBuf); 1796 fail("Should throw ClosedChannelException"); 1797 } catch (ClosedChannelException e) { 1798 // OK. 1799 } 1800 } 1801 1802 public void testReadByteBuffer_bufNull() throws IOException { 1803 ByteBuffer readBuf = ByteBuffer.allocateDirect(0); 1804 InetSocketAddress ipAddr = localAddr1; 1805 try { 1806 this.channel1.read(readBuf); 1807 fail("should throw NotYetConnectedException"); 1808 } catch (NotYetConnectedException e) { 1809 // correct 1810 } 1811 this.channel1.connect(ipAddr); 1812 assertTrue(this.channel1.isConnected()); 1813 try { 1814 channel1.read((ByteBuffer) null); 1815 fail("should throw NPE"); 1816 } catch (NullPointerException e) { 1817 // correct 1818 } 1819 this.channel1.configureBlocking(false); 1820 // note : blocking-mode will make the read process endless! 1821 assertEquals(0, this.channel1.read(readBuf)); 1822 datagramSocket1.close(); 1823 } 1824 1825 /* 1826 * Test method for 'DatagramChannelImpl.read(ByteBuffer[], int, int)' 1827 */ 1828 public void testReadByteBufferArrayIntInt() throws IOException { 1829 ByteBuffer[] readBuf = new ByteBuffer[2]; 1830 readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1831 readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1832 InetSocketAddress ipAddr = localAddr1; 1833 try { 1834 this.channel1.read(readBuf, 0, 2); 1835 fail("should throw NotYetConnectedException"); 1836 } catch (NotYetConnectedException e) { 1837 // correct 1838 } 1839 this.channel1.connect(ipAddr); 1840 assertTrue(this.channel1.isConnected()); 1841 this.channel1.configureBlocking(false); 1842 // note : blocking-mode will make the read process endless! 1843 assertEquals(0, this.channel1.read(readBuf, 0, 1)); 1844 assertEquals(0, this.channel1.read(readBuf, 0, 2)); 1845 datagramSocket1.close(); 1846 } 1847 1848 public void testReadByteBufferArrayIntInt_exceptions() throws IOException { 1849 //regression test for HARMONY-932 1850 try { 1851 DatagramChannel.open().read(new ByteBuffer[] {}, 2, Integer.MAX_VALUE); 1852 fail(); 1853 } catch (IndexOutOfBoundsException expected) { 1854 } 1855 try { 1856 DatagramChannel.open().read(new ByteBuffer[] {}, -1, 0); 1857 fail(); 1858 } catch (IndexOutOfBoundsException expected) { 1859 } 1860 try { 1861 DatagramChannel.open().read((ByteBuffer[]) null, 0, 0); 1862 fail(); 1863 } catch (NullPointerException expected) { 1864 } 1865 } 1866 1867 public void testReadByteBufferArrayIntInt_BufNull() throws IOException { 1868 ByteBuffer[] readBuf = new ByteBuffer[2]; 1869 readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 1870 InetSocketAddress ipAddr = localAddr1; 1871 try { 1872 this.channel1.read(null, 0, 0); 1873 fail("should throw NPE"); 1874 } catch (NullPointerException e) { 1875 // correct 1876 } 1877 this.channel1.connect(ipAddr); 1878 assertTrue(this.channel1.isConnected()); 1879 this.channel1.configureBlocking(false); 1880 // note : blocking-mode will make the read process endless! 1881 try { 1882 this.channel1.read(null, 0, 0); 1883 fail("should throw NPE"); 1884 } catch (NullPointerException e) { 1885 // correct 1886 } 1887 assertEquals(0, this.channel1.read(readBuf, 0, 1)); 1888 try { 1889 this.channel1.read(readBuf, 0, 2); 1890 fail("should throw NPE"); 1891 } catch (NullPointerException e) { 1892 // correct 1893 } 1894 try { 1895 this.channel1.read(readBuf, 0, 3); 1896 fail("should throw IndexOutOfBoundsException"); 1897 } catch (IndexOutOfBoundsException e) { 1898 // correct 1899 } 1900 datagramSocket1.close(); 1901 } 1902 1903 // ------------------------------------------------------------------- 1904 // test read and write 1905 // ------------------------------------------------------------------- 1906 1907 public void testReadWrite_configureBlock() throws Exception { 1908 byte[] targetArray = new byte[2]; 1909 // bind and connect 1910 this.channel1.socket().bind(localAddr2); 1911 this.channel1.connect(localAddr1); 1912 this.channel2.socket().bind(localAddr1); 1913 this.channel2.connect(localAddr2); 1914 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 1915 1916 new Thread() { 1917 public void run() { 1918 try { 1919 Thread.sleep(TIME_UNIT); 1920 channel1.configureBlocking(false); 1921 channel1.close(); 1922 } catch (Exception e) { 1923 //ignore 1924 } 1925 } 1926 }.start(); 1927 try { 1928 this.channel1.read(targetBuf); 1929 fail("should throw AsynchronousCloseException"); 1930 } catch (AsynchronousCloseException e) { 1931 // ok 1932 } 1933 } 1934 1935 public void testReadWrite_Block_Zero() throws Exception { 1936 byte[] sourceArray = new byte[0]; 1937 byte[] targetArray = new byte[0]; 1938 // bind and connect 1939 this.channel1.socket().bind(localAddr2); 1940 this.channel1.connect(localAddr1); 1941 this.channel2.socket().bind(localAddr1); 1942 this.channel2.connect(localAddr2); 1943 1944 // write 1945 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 1946 assertEquals(0, this.channel1.write(sourceBuf)); 1947 1948 // read 1949 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 1950 int readCount = this.channel2.read(targetBuf); 1951 1952 assertEquals(0, readCount); 1953 } 1954 1955 public void testReadWrite_Block_Normal() throws Exception { 1956 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 1957 byte[] targetArray = new byte[CAPACITY_NORMAL]; 1958 for (int i = 0; i < sourceArray.length; i++) { 1959 sourceArray[i] = (byte) i; 1960 } 1961 1962 // bind and connect 1963 this.channel1.socket().bind(localAddr2); 1964 this.channel1.connect(localAddr1); 1965 this.channel2.socket().bind(localAddr1); 1966 this.channel2.connect(localAddr2); 1967 1968 readWriteReadData(this.channel1, sourceArray, this.channel2, 1969 targetArray, CAPACITY_NORMAL, "testReadWrite_Block_Normal"); 1970 } 1971 1972 public void testReadWrite_Block_Empty() throws Exception { 1973 // empty buf 1974 byte[] sourceArray = "".getBytes(); 1975 byte[] targetArray = new byte[CAPACITY_NORMAL]; 1976 1977 // bind and connect 1978 1979 this.channel1.socket().bind(localAddr2); 1980 this.channel1.connect(localAddr1); 1981 this.channel2.socket().bind(localAddr1); 1982 this.channel2.connect(localAddr2); 1983 1984 // write 1985 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 1986 assertEquals(0, this.channel1.write(sourceBuf)); 1987 1988 // read 1989 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 1990 // empty message let the reader blocked 1991 closeBlockedReaderChannel2(targetBuf); 1992 } 1993 1994 public void testReadWrite_changeBlock_Empty() throws Exception { 1995 // empty buf 1996 byte[] sourceArray = "".getBytes(); 1997 byte[] targetArray = new byte[CAPACITY_NORMAL]; 1998 1999 // bind and connect 2000 2001 this.channel1.socket().bind(localAddr2); 2002 this.channel1.connect(localAddr1); 2003 this.channel2.socket().bind(localAddr1); 2004 this.channel2.connect(localAddr2); 2005 2006 // write 2007 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2008 assertEquals(0, this.channel1.write(sourceBuf)); 2009 2010 // read 2011 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2012 // empty message let the reader blocked 2013 new Thread() { 2014 public void run() { 2015 try { 2016 Thread.sleep(TIME_UNIT); 2017 channel2.configureBlocking(false); 2018 Thread.sleep(TIME_UNIT * 5); 2019 channel2.close(); 2020 } catch (Exception e) { 2021 // do nothing 2022 } 2023 } 2024 }.start(); 2025 try { 2026 assertTrue(this.channel2.isBlocking()); 2027 this.channel2.read(targetBuf); 2028 fail("Should throw AsynchronousCloseException"); 2029 } catch (AsynchronousCloseException e) { 2030 assertFalse(this.channel2.isBlocking()); 2031 // OK. 2032 } 2033 } 2034 2035 public void testReadWrite_Block_8KB() throws Exception { 2036 byte[] sourceArray = new byte[CAPACITY_1KB * 8]; 2037 byte[] targetArray = new byte[CAPACITY_1KB * 8]; 2038 for (int i = 0; i < sourceArray.length; i++) { 2039 sourceArray[i] = (byte) i; 2040 } 2041 2042 // bind and connect 2043 this.channel1.socket().bind(localAddr2); 2044 this.channel1.connect(localAddr1); 2045 this.channel2.socket().bind(localAddr1); 2046 this.channel2.connect(localAddr2); 2047 2048 readWriteReadData(this.channel1, sourceArray, this.channel2, 2049 targetArray, 8 * CAPACITY_1KB, "testReadWrite_Block_8KB"); 2050 } 2051 2052 /* 2053 * sender write the sourceArray whose size is dataSize, and receiver read 2054 * the data into targetArray 2055 */ 2056 private void readWriteReadData(DatagramChannel sender, byte[] sourceArray, 2057 DatagramChannel receiver, byte[] targetArray, int dataSize, 2058 String methodName) throws IOException { 2059 // write 2060 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2061 assertEquals(dataSize, sender.write(sourceBuf)); 2062 2063 // read 2064 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2065 2066 int count = 0; 2067 int total = 0; 2068 long beginTime = System.currentTimeMillis(); 2069 while (total < dataSize && (count = receiver.read(targetBuf)) != -1) { 2070 total = total + count; 2071 // 3s timeout to avoid dead loop 2072 if (System.currentTimeMillis() - beginTime > 3000){ 2073 break; 2074 } 2075 } 2076 2077 assertEquals(dataSize, total); 2078 assertEquals(targetBuf.position(), total); 2079 targetBuf.flip(); 2080 targetArray = targetBuf.array(); 2081 for (int i = 0; i < targetArray.length; i++) { 2082 assertEquals(targetArray[i], (byte) i); 2083 } 2084 } 2085 2086 public void testReadWrite_Block_64K() throws Exception { 2087 byte[] sourceArray = new byte[CAPACITY_64KB]; 2088 for (int i = 0; i < sourceArray.length; i++) { 2089 sourceArray[i] = (byte) i; 2090 } 2091 2092 // bind and connect 2093 this.channel1.socket().bind(localAddr2); 2094 this.channel1.connect(localAddr1); 2095 this.channel2.socket().bind(localAddr1); 2096 this.channel2.connect(localAddr2); 2097 2098 // write 2099 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2100 try { 2101 channel1.write(sourceBuf); 2102 fail("Should throw IOException"); 2103 } catch (IOException e) { 2104 // too big 2105 } 2106 } 2107 2108 public void testReadWrite_Block_DifferentAddr() throws Exception { 2109 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2110 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2111 for (int i = 0; i < sourceArray.length; i++) { 2112 sourceArray[i] = (byte) i; 2113 } 2114 2115 // bind and connect 2116 this.channel1.socket().bind(localAddr2); 2117 this.channel1.connect(localAddr1); 2118 this.channel2.socket().bind(localAddr1); 2119 this.channel2.connect(localAddr1);// the different addr 2120 2121 // write 2122 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2123 assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 2124 2125 // read 2126 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2127 // the wrong connected addr will make the read blocked. 2128 // we close the blocked channel 2129 closeBlockedReaderChannel2(targetBuf); 2130 } 2131 2132 public void testReadWrite_Block_WriterNotBind() throws Exception { 2133 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2134 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2135 for (int i = 0; i < sourceArray.length; i++) { 2136 sourceArray[i] = (byte) i; 2137 } 2138 2139 // bind and connect 2140 this.channel1.connect(localAddr1); 2141 this.channel2.socket().bind(localAddr1); 2142 this.channel2.connect(localAddr2); 2143 2144 // write 2145 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2146 assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 2147 2148 // read 2149 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2150 closeBlockedReaderChannel2(targetBuf); 2151 } 2152 2153 public void testReadWrite_Block_WriterBindLater() throws Exception { 2154 2155 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2156 2157 // bind and connect 2158 // writer channel1 is bound later 2159 this.channel2.socket().bind(localAddr1); 2160 this.channel2.connect(localAddr2); 2161 2162 // read 2163 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2164 new Thread() { 2165 public void run() { 2166 try { 2167 Thread.sleep(TIME_UNIT); 2168 // bind later 2169 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2170 for (int i = 0; i < sourceArray.length; i++) { 2171 sourceArray[i] = (byte) i; 2172 } 2173 channel1.socket().bind(localAddr2); 2174 channel1.connect(localAddr1); 2175 // write later 2176 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2177 assertEquals(CAPACITY_NORMAL, channel1.write(sourceBuf)); 2178 } catch (Exception e) { 2179 // do nothing 2180 } 2181 } 2182 }.start(); 2183 2184 int count = 0; 2185 int total = 0; 2186 long beginTime = System.currentTimeMillis(); 2187 while (total < CAPACITY_NORMAL && (count = channel2.read(targetBuf)) != -1) { 2188 total = total + count; 2189 // 3s timeout to avoid dead loop 2190 if (System.currentTimeMillis() - beginTime > 3000){ 2191 break; 2192 } 2193 } 2194 2195 assertEquals(CAPACITY_NORMAL, total); 2196 assertEquals(targetBuf.position(), total); 2197 targetBuf.flip(); 2198 targetArray = targetBuf.array(); 2199 for (int i = 0; i < targetArray.length; i++) { 2200 assertEquals(targetArray[i], (byte) i); 2201 } 2202 2203 } 2204 2205 public void testReadWrite_Block_ReaderNotBind() throws Exception { 2206 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2207 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2208 for (int i = 0; i < sourceArray.length; i++) { 2209 sourceArray[i] = (byte) i; 2210 } 2211 2212 // bind and connect 2213 this.channel1.socket().bind(localAddr2); 2214 this.channel1.connect(localAddr1); 2215 // reader channel2 is not bound 2216 this.channel2.connect(localAddr2); 2217 2218 // write 2219 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2220 assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 2221 2222 // read 2223 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2224 closeBlockedReaderChannel2(targetBuf); 2225 2226 } 2227 2228 private void closeBlockedReaderChannel2(ByteBuffer targetBuf) 2229 throws IOException { 2230 new Thread() { 2231 public void run() { 2232 try { 2233 Thread.sleep(TIME_UNIT); 2234 channel2.close(); 2235 } catch (Exception e) { 2236 // do nothing 2237 } 2238 } 2239 }.start(); 2240 try { 2241 assertTrue(this.channel2.isBlocking()); 2242 this.channel2.read(targetBuf); 2243 fail("Should throw AsynchronousCloseException"); 2244 } catch (AsynchronousCloseException e) { 2245 // OK. 2246 } 2247 } 2248 2249 // ------------------------------------------------------------------- 2250 // Test read and write in non-block mode. 2251 // ------------------------------------------------------------------- 2252 public void testReadWrite_NonBlock_Normal() throws Exception { 2253 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2254 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2255 for (int i = 0; i < sourceArray.length; i++) { 2256 sourceArray[i] = (byte) i; 2257 } 2258 2259 this.channel1.configureBlocking(false); 2260 this.channel2.configureBlocking(false); 2261 2262 // bind and connect 2263 this.channel1.socket().bind(localAddr2); 2264 this.channel1.connect(localAddr1); 2265 this.channel2.socket().bind(localAddr1); 2266 this.channel2.connect(localAddr2); 2267 2268 readWriteReadData(this.channel1, sourceArray, this.channel2, 2269 targetArray, CAPACITY_NORMAL, "testReadWrite_NonBlock_Normal"); 2270 } 2271 2272 public void testReadWrite_NonBlock_8KB() throws Exception { 2273 byte[] sourceArray = new byte[CAPACITY_1KB * 8]; 2274 byte[] targetArray = new byte[CAPACITY_1KB * 8]; 2275 for (int i = 0; i < sourceArray.length; i++) { 2276 sourceArray[i] = (byte) i; 2277 } 2278 2279 this.channel1.configureBlocking(false); 2280 this.channel2.configureBlocking(false); 2281 2282 // bind and connect 2283 this.channel1.socket().bind(localAddr2); 2284 this.channel1.connect(localAddr1); 2285 this.channel2.socket().bind(localAddr1); 2286 this.channel2.connect(localAddr2); 2287 2288 readWriteReadData(this.channel1, sourceArray, this.channel2, 2289 targetArray, 8 * CAPACITY_1KB, "testReadWrite_NonBlock_8KB"); 2290 } 2291 2292 public void testReadWrite_NonBlock_DifferentAddr() throws Exception { 2293 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2294 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2295 for (int i = 0; i < sourceArray.length; i++) { 2296 sourceArray[i] = (byte) i; 2297 } 2298 2299 this.channel1.configureBlocking(false); 2300 this.channel2.configureBlocking(false); 2301 2302 // bind and connect 2303 this.channel1.socket().bind(localAddr2); 2304 this.channel1.connect(localAddr1); 2305 this.channel2.socket().bind(localAddr1); 2306 this.channel2.connect(localAddr1);// the different addr 2307 2308 // write 2309 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2310 assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 2311 2312 // read 2313 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2314 assertEquals(0, this.channel2.read(targetBuf)); 2315 } 2316 2317 public void testReadWrite_NonBlock_WriterNotBind() throws Exception { 2318 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2319 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2320 for (int i = 0; i < sourceArray.length; i++) { 2321 sourceArray[i] = (byte) i; 2322 } 2323 2324 this.channel1.configureBlocking(false); 2325 this.channel2.configureBlocking(false); 2326 2327 // bind and connect 2328 this.channel1.connect(localAddr1); 2329 this.channel2.socket().bind(localAddr1); 2330 this.channel2.connect(localAddr2); 2331 2332 // write 2333 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2334 assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 2335 2336 // read 2337 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2338 assertEquals(0, this.channel2.read(targetBuf)); 2339 } 2340 2341 public void testReadWrite_NonBlock_ReaderNotBind() throws Exception { 2342 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2343 byte[] targetArray = new byte[CAPACITY_NORMAL]; 2344 for (int i = 0; i < sourceArray.length; i++) { 2345 sourceArray[i] = (byte) i; 2346 } 2347 2348 this.channel1.configureBlocking(false); 2349 this.channel2.configureBlocking(false); 2350 2351 // bind and connect 2352 this.channel1.socket().bind(localAddr2); 2353 this.channel1.connect(localAddr1); 2354 this.channel2.connect(localAddr2); 2355 2356 // write 2357 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2358 assertEquals(CAPACITY_NORMAL, this.channel1.write(sourceBuf)); 2359 2360 // read 2361 ByteBuffer targetBuf = ByteBuffer.wrap(targetArray); 2362 assertEquals(0, this.channel2.read(targetBuf)); 2363 } 2364 2365 public void test_write_LBuffer_positioned() throws Exception { 2366 // Regression test for Harmony-683 2367 int position = 16; 2368 DatagramChannel dc = DatagramChannel.open(); 2369 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2370 dc.connect(localAddr1); 2371 // write 2372 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2373 sourceBuf.position(position); 2374 assertEquals(CAPACITY_NORMAL - position, dc.write(sourceBuf)); 2375 } 2376 2377 public void test_send_LBuffer_LSocketAddress_PositionNotZero() 2378 throws Exception { 2379 // regression test for Harmony-701 2380 int CAPACITY_NORMAL = 256; 2381 int position = 16; 2382 DatagramChannel dc = DatagramChannel.open(); 2383 byte[] sourceArray = new byte[CAPACITY_NORMAL]; 2384 // send ByteBuffer whose position is not zero 2385 ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray); 2386 sourceBuf.position(position); 2387 int ret = dc.send(sourceBuf, localAddr1); 2388 // assert send (256 - 16) bytes 2389 assertEquals(CAPACITY_NORMAL - position, ret); 2390 // assert the position of ByteBuffer has been set 2391 assertEquals(CAPACITY_NORMAL, sourceBuf.position()); 2392 } 2393 2394 /** 2395 * @tests DatagramChannel#read(ByteBuffer[]) 2396 */ 2397 public void test_read_$LByteBuffer() throws Exception { 2398 // regression test for Harmony-754 2399 channel2.socket().bind(localAddr1); 2400 channel1.socket().bind(localAddr2); 2401 channel1.connect(localAddr1); 2402 channel2.connect(localAddr2); 2403 channel2.write(ByteBuffer.allocate(CAPACITY_NORMAL)); 2404 2405 ByteBuffer[] readBuf = new ByteBuffer[2]; 2406 readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2407 readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2408 2409 channel1.configureBlocking(true); 2410 assertEquals(CAPACITY_NORMAL, channel1.read(readBuf)); 2411 } 2412 2413 /** 2414 * @tests DatagramChannel#read(ByteBuffer[],int,int) 2415 */ 2416 public void test_read_$LByteBufferII() throws Exception { 2417 // regression test for Harmony-754 2418 channel2.socket().bind(localAddr1); 2419 channel1.socket().bind(localAddr2); 2420 channel1.connect(localAddr1); 2421 channel2.connect(localAddr2); 2422 channel2.write(ByteBuffer.allocate(CAPACITY_NORMAL)); 2423 2424 ByteBuffer[] readBuf = new ByteBuffer[2]; 2425 readBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2426 readBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); 2427 2428 channel1.configureBlocking(true); 2429 assertEquals(CAPACITY_NORMAL, channel1.read(readBuf,0,2)); 2430 } 2431 2432 /** 2433 * @tests DatagramChannel#read(ByteBuffer) 2434 */ 2435 public void test_read_LByteBuffer_closed_nullBuf() throws Exception { 2436 // regression test for Harmony-754 2437 ByteBuffer c = null; 2438 DatagramChannel channel = DatagramChannel.open(); 2439 channel.close(); 2440 try{ 2441 channel.read(c); 2442 fail("Should throw NullPointerException"); 2443 } catch (NullPointerException e){ 2444 // expected 2445 } 2446 } 2447 2448 /** 2449 * @tests DatagramChannel#read(ByteBuffer) 2450 */ 2451 public void test_read_LByteBuffer_NotConnected_nullBuf() throws Exception { 2452 // regression test for Harmony-754 2453 ByteBuffer c = null; 2454 DatagramChannel channel = DatagramChannel.open(); 2455 try{ 2456 channel.read(c); 2457 fail("Should throw NullPointerException"); 2458 } catch (NullPointerException e){ 2459 // expected 2460 } 2461 } 2462 2463 /** 2464 * @tests DatagramChannel#read(ByteBuffer) 2465 */ 2466 public void test_read_LByteBuffer_readOnlyBuf() throws Exception { 2467 // regression test for Harmony-754 2468 ByteBuffer c = ByteBuffer.allocate(1); 2469 DatagramChannel channel = DatagramChannel.open(); 2470 try{ 2471 channel.read(c.asReadOnlyBuffer()); 2472 fail("Should throw NotYetConnectedException"); 2473 } catch (NotYetConnectedException e){ 2474 } catch (IllegalArgumentException e){ 2475 // expected 2476 } 2477 channel.connect(localAddr1); 2478 try{ 2479 channel.read(c.asReadOnlyBuffer()); 2480 fail("Should throw IllegalArgumentException"); 2481 } catch (IllegalArgumentException e){ 2482 // expected 2483 } 2484 } 2485 2486 /** 2487 * @tests DatagramChannel#send(ByteBuffer, SocketAddress) 2488 */ 2489 public void test_send_LByteBuffer_LSocketAddress_closed() throws IOException{ 2490 // regression test for Harmony-913 2491 channel1.close(); 2492 ByteBuffer buf = ByteBuffer.allocate(CAPACITY_NORMAL); 2493 try { 2494 channel1.send(buf, localAddr1); 2495 fail("Should throw ClosedChannelException"); 2496 } catch (ClosedChannelException e) { 2497 //pass 2498 } 2499 try { 2500 channel1.send(null,localAddr1); 2501 fail("Should throw NullPointerException"); 2502 } catch (NullPointerException e) { 2503 //pass 2504 } 2505 try { 2506 channel1.send(buf, null); 2507 fail("Should throw ClosedChannelException"); 2508 } catch (ClosedChannelException e) { 2509 //pass 2510 } 2511 try { 2512 channel1.send(null, null); 2513 fail("Should throw NullPointerException"); 2514 } catch (NullPointerException e) { 2515 //pass 2516 } 2517 } 2518 2519 /** 2520 * @tests DatagramChannel#socket() 2521 */ 2522 public void test_socket_IllegalBlockingModeException() throws Exception { 2523 // regression test for Harmony-1036 2524 DatagramChannel channel = DatagramChannel.open(); 2525 channel.configureBlocking(false); 2526 DatagramSocket socket = channel.socket(); 2527 try { 2528 socket.send(null); 2529 fail("should throw IllegalBlockingModeException"); 2530 } catch (IllegalBlockingModeException e) { 2531 // expected 2532 } 2533 try { 2534 socket.receive(null); 2535 fail("should throw IllegalBlockingModeException"); 2536 } catch (IllegalBlockingModeException e) { 2537 // expected 2538 } 2539 } 2540 2541 public void test_bounded_harmony6493() throws IOException { 2542 DatagramChannel server = DatagramChannel.open(); 2543 InetSocketAddress addr = new InetSocketAddress("localhost", 0); 2544 server.socket().bind(addr); 2545 SocketAddress boundedAddress = server.socket().getLocalSocketAddress(); 2546 2547 DatagramChannel client = DatagramChannel.open(); 2548 ByteBuffer sent = ByteBuffer.allocate(1024); 2549 sent.put("test".getBytes()); 2550 sent.flip(); 2551 client.send(sent, boundedAddress); 2552 assertTrue(client.socket().isBound()); 2553 2554 server.close(); 2555 client.close(); 2556 } 2557 } 2558