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.tests.java.net; 19 20 import java.io.IOException; 21 import java.io.InterruptedIOException; 22 import java.net.BindException; 23 import java.net.DatagramPacket; 24 import java.net.DatagramSocket; 25 import java.net.DatagramSocketImpl; 26 import java.net.Inet6Address; 27 import java.net.InetAddress; 28 import java.net.InetSocketAddress; 29 import java.net.NetworkInterface; 30 import java.net.SocketAddress; 31 import java.net.SocketException; 32 import java.net.UnknownHostException; 33 import java.nio.channels.DatagramChannel; 34 35 public class DatagramSocketTest extends junit.framework.TestCase { 36 37 static final class DatagramServer extends Thread { 38 39 volatile boolean running = true; 40 41 private final boolean echo; 42 private final byte[] rbuf; 43 private final DatagramPacket rdp; 44 final DatagramSocket serverSocket; 45 46 public DatagramServer(InetAddress address, boolean echo) 47 throws IOException { 48 this.echo = echo; 49 rbuf = new byte[512]; 50 rbuf[0] = -1; 51 rdp = new DatagramPacket(rbuf, rbuf.length); 52 serverSocket = new DatagramSocket(0, address); 53 serverSocket.setSoTimeout(2000); 54 } 55 56 public DatagramServer(InetAddress address) throws IOException { 57 this(address, true /* echo */); 58 } 59 60 public void run() { 61 try { 62 while (running) { 63 try { 64 serverSocket.receive(rdp); 65 if (echo) { 66 serverSocket.send(rdp); 67 } 68 } catch (InterruptedIOException e) { 69 } 70 } 71 } catch (IOException e) { 72 fail(); 73 } finally { 74 serverSocket.close(); 75 } 76 } 77 78 public int getPort() { 79 return serverSocket.getLocalPort(); 80 } 81 82 public void stopServer() { 83 running = false; 84 } 85 } 86 87 /** 88 * java.net.DatagramSocket#DatagramSocket() 89 */ 90 public void test_Constructor() throws SocketException { 91 new DatagramSocket(); 92 } 93 94 /** 95 * java.net.DatagramSocket#DatagramSocket(int) 96 */ 97 public void test_ConstructorI() throws SocketException { 98 DatagramSocket ds = new DatagramSocket(0); 99 ds.close(); 100 } 101 102 /** 103 * java.net.DatagramSocket#DatagramSocket(int, java.net.InetAddress) 104 */ 105 public void test_ConstructorILjava_net_InetAddress() throws IOException { 106 DatagramSocket ds = new DatagramSocket(0, InetAddress.getLocalHost()); 107 assertTrue("Created socket with incorrect port", ds.getLocalPort() != 0); 108 assertEquals("Created socket with incorrect address", InetAddress 109 .getLocalHost(), ds.getLocalAddress()); 110 } 111 112 /** 113 * java.net.DatagramSocket#close() 114 */ 115 public void test_close() throws UnknownHostException, SocketException { 116 DatagramSocket ds = new DatagramSocket(0); 117 DatagramPacket dp = new DatagramPacket("Test String".getBytes(), 11, 118 InetAddress.getLocalHost(), 0); 119 ds.close(); 120 try { 121 ds.send(dp); 122 fail("Data sent after close"); 123 } catch (IOException e) { 124 // Expected 125 } 126 } 127 128 public void test_connectLjava_net_InetAddressI() throws Exception { 129 DatagramSocket ds = new DatagramSocket(); 130 InetAddress inetAddress = InetAddress.getLocalHost(); 131 ds.connect(inetAddress, 0); 132 assertEquals("Incorrect InetAddress", inetAddress, ds.getInetAddress()); 133 assertEquals("Incorrect Port", 0, ds.getPort()); 134 ds.disconnect(); 135 136 ds = new java.net.DatagramSocket(); 137 inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"); 138 ds.connect(inetAddress, 0); 139 assertEquals(inetAddress, ds.getInetAddress()); 140 ds.disconnect(); 141 } 142 143 public void testConnect_connectToSelf() throws Exception { 144 // Create a connected datagram socket to test 145 // PlainDatagramSocketImpl.peek() 146 InetAddress localHost = InetAddress.getLocalHost(); 147 final DatagramSocket ds = new DatagramSocket(0); 148 ds.connect(localHost, ds.getLocalPort()); 149 DatagramPacket send = new DatagramPacket(new byte[10], 10, localHost, 150 ds.getLocalPort()); 151 ds.send(send); 152 153 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 154 ds.setSoTimeout(2000); 155 ds.receive(receive); 156 ds.close(); 157 158 assertEquals(10, receive.getLength()); 159 assertEquals(localHost, receive.getAddress()); 160 } 161 162 private static void assertPacketDataEquals(DatagramPacket p1, DatagramPacket p2) 163 throws Exception { 164 assertEquals(p1.getLength(), p2.getLength()); 165 final byte[] p1Bytes = p1.getData(); 166 final byte[] p2Bytes = p2.getData(); 167 168 for (int i = 0; i < p1.getLength(); ++i) { 169 if (p1Bytes[p1.getOffset() + i] != p2Bytes[p2.getOffset() + i]) { 170 String expected = new String(p1Bytes, p1.getOffset(), p1.getLength(), 171 "UTF-8"); 172 String actual = new String(p2Bytes, p2.getOffset(), p2.getLength(), 173 "UTF-8"); 174 fail("expected: " + expected + ", actual: " + actual); 175 } 176 } 177 } 178 179 public void testConnect_echoServer() throws Exception { 180 final DatagramSocket ds = new DatagramSocket(0); 181 182 final DatagramServer server = new DatagramServer(Inet6Address.LOOPBACK); 183 server.start(); 184 185 ds.connect(Inet6Address.LOOPBACK, server.getPort()); 186 187 final byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 188 final DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length); 189 final DatagramPacket receive = new DatagramPacket(new byte[20], 20); 190 191 ds.send(send); 192 ds.setSoTimeout(2000); 193 ds.receive(receive); 194 ds.close(); 195 196 assertEquals(sendBytes.length, receive.getLength()); 197 assertPacketDataEquals(send, receive); 198 assertEquals(Inet6Address.LOOPBACK, receive.getAddress()); 199 200 server.stopServer(); 201 } 202 203 // Validate that once connected we cannot send to another address. 204 public void testConnect_throwsOnAddressMismatch() throws Exception { 205 final DatagramSocket ds = new DatagramSocket(0); 206 207 DatagramServer s1 = new DatagramServer(Inet6Address.LOOPBACK); 208 DatagramServer s2 = new DatagramServer(Inet6Address.LOOPBACK); 209 try { 210 ds.connect(Inet6Address.LOOPBACK, s1.getPort()); 211 ds.send(new DatagramPacket(new byte[10], 10, Inet6Address.LOOPBACK, s2.getPort())); 212 fail(); 213 } catch (IllegalArgumentException expected) { 214 } finally { 215 ds.close(); 216 s1.stopServer(); 217 s2.stopServer(); 218 } 219 } 220 221 // Validate that we can connect, then disconnect, then connect then 222 // send/recv. 223 public void testConnect_connectDisconnectConnectThenSendRecv() throws Exception { 224 final DatagramSocket ds = new DatagramSocket(0); 225 226 final DatagramServer server = new DatagramServer(Inet6Address.LOOPBACK); 227 final DatagramServer broken = new DatagramServer(Inet6Address.LOOPBACK, false); 228 server.start(); 229 broken.start(); 230 231 final int serverPortNumber = server.getPort(); 232 ds.connect(Inet6Address.LOOPBACK, broken.getPort()); 233 ds.disconnect(); 234 ds.connect(Inet6Address.LOOPBACK, serverPortNumber); 235 236 final byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 237 final DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length); 238 final DatagramPacket receive = new DatagramPacket(new byte[20], 20); 239 ds.send(send); 240 ds.setSoTimeout(2000); 241 ds.receive(receive); 242 ds.close(); 243 244 assertPacketDataEquals(send, receive); 245 assertEquals(Inet6Address.LOOPBACK, receive.getAddress()); 246 247 server.stopServer(); 248 broken.stopServer(); 249 } 250 251 // Validate that we can connect/disconnect then send/recv to any address 252 public void testConnect_connectDisconnectThenSendRecv() throws Exception { 253 final DatagramSocket ds = new DatagramSocket(0); 254 255 final DatagramServer server = new DatagramServer(Inet6Address.LOOPBACK); 256 server.start(); 257 258 final int serverPortNumber = server.getPort(); 259 ds.connect(Inet6Address.LOOPBACK, serverPortNumber); 260 ds.disconnect(); 261 262 final byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 263 final DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length, 264 Inet6Address.LOOPBACK, serverPortNumber); 265 final DatagramPacket receive = new DatagramPacket(new byte[20], 20); 266 ds.send(send); 267 ds.setSoTimeout(2000); 268 ds.receive(receive); 269 ds.close(); 270 271 assertPacketDataEquals(send, receive); 272 assertEquals(Inet6Address.LOOPBACK, receive.getAddress()); 273 274 server.stopServer(); 275 } 276 277 public void testConnect_connectTwice() throws Exception { 278 final DatagramSocket ds = new DatagramSocket(0); 279 280 final DatagramServer server = new DatagramServer(Inet6Address.LOOPBACK); 281 final DatagramServer broken = new DatagramServer(Inet6Address.LOOPBACK); 282 server.start(); 283 broken.start(); 284 285 final int serverPortNumber = server.getPort(); 286 ds.connect(Inet6Address.LOOPBACK, broken.getPort()); 287 ds.connect(Inet6Address.LOOPBACK, serverPortNumber); 288 ds.disconnect(); 289 290 final byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 291 final DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length, 292 Inet6Address.LOOPBACK, serverPortNumber); 293 final DatagramPacket receive = new DatagramPacket(new byte[20], 20); 294 ds.send(send); 295 ds.setSoTimeout(2000); 296 ds.receive(receive); 297 ds.close(); 298 299 assertPacketDataEquals(send, receive); 300 assertEquals(Inet6Address.LOOPBACK, receive.getAddress()); 301 302 server.stopServer(); 303 broken.stopServer(); 304 } 305 306 public void testConnect_zeroAddress() throws Exception { 307 DatagramSocket ds = new DatagramSocket(); 308 byte[] addressBytes = { 0, 0, 0, 0 }; 309 InetAddress inetAddress = InetAddress.getByAddress(addressBytes); 310 ds.connect(inetAddress, 0); 311 312 ds = new java.net.DatagramSocket(); 313 byte[] addressTestBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 314 0, 0, 0 }; 315 inetAddress = InetAddress.getByAddress(addressTestBytes); 316 ds.connect(inetAddress, 0); 317 } 318 319 public void test_disconnect() throws Exception { 320 DatagramSocket ds = new DatagramSocket(); 321 InetAddress inetAddress = InetAddress.getLocalHost(); 322 ds.connect(inetAddress, 0); 323 ds.disconnect(); 324 assertNull("Incorrect InetAddress", ds.getInetAddress()); 325 assertEquals("Incorrect Port", -1, ds.getPort()); 326 327 ds = new DatagramSocket(); 328 inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"); 329 ds.connect(inetAddress, 0); 330 ds.disconnect(); 331 assertNull("Incorrect InetAddress", ds.getInetAddress()); 332 assertEquals("Incorrect Port", -1, ds.getPort()); 333 } 334 335 public void test_getLocalAddress() throws Exception { 336 // Test for method java.net.InetAddress 337 // java.net.DatagramSocket.getLocalAddress() 338 InetAddress local = InetAddress.getLocalHost(); 339 DatagramSocket ds = new java.net.DatagramSocket(0, local); 340 assertEquals(InetAddress.getByName(InetAddress.getLocalHost().getHostName()), ds.getLocalAddress()); 341 342 // now check behavior when the ANY address is returned 343 DatagramSocket s = new DatagramSocket(0); 344 assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), s.getLocalAddress() instanceof Inet6Address); 345 s.close(); 346 } 347 348 public void test_getLocalPort() throws SocketException { 349 DatagramSocket ds = new DatagramSocket(); 350 assertTrue("Returned incorrect port", ds.getLocalPort() != 0); 351 } 352 353 public void test_getPort() throws IOException { 354 DatagramSocket theSocket = new DatagramSocket(); 355 assertEquals("Expected -1 for remote port as not connected", -1, 356 theSocket.getPort()); 357 358 // Now connect the socket and validate that we get the right port 359 int portNumber = 49152; // any valid port, even if it is unreachable 360 theSocket.connect(InetAddress.getLocalHost(), portNumber); 361 assertEquals("getPort returned wrong value", portNumber, theSocket 362 .getPort()); 363 } 364 365 public void test_getReceiveBufferSize() throws Exception { 366 DatagramSocket ds = new DatagramSocket(); 367 ds.setReceiveBufferSize(130); 368 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 369 ds.close(); 370 try { 371 ds.getReceiveBufferSize(); 372 fail("SocketException was not thrown."); 373 } catch(SocketException se) { 374 //expected 375 } 376 } 377 378 public void test_getSendBufferSize() throws Exception { 379 final DatagramSocket ds = new java.net.DatagramSocket(0); 380 ds.setSendBufferSize(134); 381 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 382 ds.close(); 383 try { 384 ds.getSendBufferSize(); 385 fail("SocketException was not thrown."); 386 } catch(SocketException se) { 387 //expected 388 } 389 } 390 391 public void test_getSoTimeout() throws Exception { 392 DatagramSocket ds = new DatagramSocket(); 393 ds.setSoTimeout(100); 394 assertEquals("Returned incorrect timeout", 100, ds.getSoTimeout()); 395 } 396 397 static final class TestDatagramSocketImpl extends DatagramSocketImpl { 398 // This field exists solely to force initialization of this class 399 // inside a test method. 400 public static final Object ACCESS = new Object(); 401 402 @Override 403 protected void create() throws SocketException { 404 } 405 406 @Override 407 protected void bind(int arg0, InetAddress arg1) 408 throws SocketException { 409 } 410 411 @Override 412 protected void send(DatagramPacket arg0) throws IOException { 413 } 414 415 @Override 416 protected int peek(InetAddress arg0) throws IOException { 417 return 0; 418 } 419 420 @Override 421 protected int peekData(DatagramPacket arg0) throws IOException { 422 return 0; 423 } 424 425 @Override 426 protected void receive(DatagramPacket arg0) throws IOException { 427 } 428 429 @Override 430 protected void setTTL(byte arg0) throws IOException { 431 } 432 433 @Override 434 protected byte getTTL() throws IOException { 435 return 0; 436 } 437 438 @Override 439 protected void setTimeToLive(int arg0) throws IOException { 440 } 441 442 @Override 443 protected int getTimeToLive() throws IOException { 444 return 0; 445 } 446 447 @Override 448 protected void join(InetAddress arg0) throws IOException { 449 } 450 451 @Override 452 protected void joinGroup(SocketAddress addr, NetworkInterface netInterface) throws IOException { 453 454 } 455 456 @Override 457 protected void leave(InetAddress arg0) throws IOException { 458 } 459 460 @Override 461 protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) 462 throws IOException { 463 } 464 465 @Override 466 protected void close() { 467 } 468 469 public void setOption(int arg0, Object arg1) throws SocketException { 470 } 471 472 public Object getOption(int arg0) throws SocketException { 473 return null; 474 } 475 } 476 477 static final class TestDatagramSocket extends DatagramSocket { 478 // This field exists solely to force initialization of this class 479 // inside a test method. 480 public static final Object ACCESS = new Object(); 481 482 public TestDatagramSocket(DatagramSocketImpl impl) { 483 super(impl); 484 } 485 } 486 487 488 public void testArchivedHarmonyRegressions() throws Exception { 489 // Regression for HARMONY-1118 490 assertNotNull(TestDatagramSocketImpl.ACCESS); 491 assertNotNull(TestDatagramSocket.ACCESS); 492 493 // Regression test for Harmony-2938 494 InetAddress i = InetAddress.getByName("127.0.0.1"); 495 DatagramSocket d = new DatagramSocket(0, i); 496 try { 497 d.send(new DatagramPacket(new byte[] { 1 }, 1)); 498 fail(); 499 } catch (NullPointerException expected) { 500 } finally { 501 d.close(); 502 } 503 504 // Regression test for Harmony-6413 505 InetSocketAddress addr = InetSocketAddress.createUnresolved( 506 "localhost", 0); 507 try { 508 new DatagramPacket(new byte[272], 3, addr); 509 fail(); 510 } catch (IllegalArgumentException expected) { 511 } 512 } 513 514 public void test_sendLjava_net_DatagramPacket_nullDestination() throws IOException { 515 DatagramSocket datagramSocket = new DatagramSocket(0); 516 byte[] data = { 65 }; 517 DatagramPacket sendPacket = new DatagramPacket(data, data.length, null, 25000); 518 try { 519 datagramSocket.send(sendPacket); 520 fail(); 521 } catch (NullPointerException expected) { 522 // Expected 523 } finally { 524 datagramSocket.close(); 525 } 526 527 } 528 529 public void test_setSendBufferSizeI() throws Exception { 530 final DatagramSocket ds = new DatagramSocket(0); 531 ds.setSendBufferSize(134); 532 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 533 ds.close(); 534 try { 535 ds.setSendBufferSize(1); 536 fail("SocketException was not thrown."); 537 } catch(SocketException se) { 538 //expected 539 } 540 } 541 542 public void test_setReceiveBufferSizeI() throws Exception { 543 final DatagramSocket ds = new DatagramSocket(0); 544 ds.setReceiveBufferSize(130); 545 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 546 547 try { 548 ds.setReceiveBufferSize(0); 549 fail("IllegalArgumentException was not thrown."); 550 } catch(IllegalArgumentException iae) { 551 //expected 552 } 553 554 try { 555 ds.setReceiveBufferSize(-1); 556 fail("IllegalArgumentException was not thrown."); 557 } catch(IllegalArgumentException iae) { 558 //expected 559 } 560 561 ds.close(); 562 563 try { 564 ds.setReceiveBufferSize(1); 565 fail("SocketException was not thrown."); 566 } catch (SocketException e) { 567 //expected 568 } 569 } 570 571 572 public void test_ConstructorLjava_net_DatagramSocketImpl() { 573 class SimpleTestDatagramSocket extends DatagramSocket { 574 public SimpleTestDatagramSocket(DatagramSocketImpl impl) { 575 super(impl); 576 } 577 } 578 579 try { 580 new SimpleTestDatagramSocket(null); 581 fail("exception expected"); 582 } catch (NullPointerException ex) { 583 // expected 584 } 585 } 586 587 public void test_ConstructorLjava_net_SocketAddress() throws Exception { 588 class UnsupportedSocketAddress extends SocketAddress { 589 public UnsupportedSocketAddress() { 590 } 591 } 592 593 DatagramSocket ds = new DatagramSocket(new InetSocketAddress( 594 InetAddress.getLocalHost(), 0)); 595 assertTrue(ds.getBroadcast()); 596 assertTrue("Created socket with incorrect port", ds.getLocalPort() != 0); 597 assertEquals("Created socket with incorrect address", InetAddress 598 .getLocalHost(), ds.getLocalAddress()); 599 600 try { 601 ds = new java.net.DatagramSocket(new UnsupportedSocketAddress()); 602 fail("No exception when constructing datagramSocket with unsupported SocketAddress type"); 603 } catch (IllegalArgumentException e) { 604 // Expected 605 } 606 607 // regression for HARMONY-894 608 ds = new DatagramSocket(null); 609 assertTrue(ds.getBroadcast()); 610 } 611 612 613 public void test_bindLjava_net_SocketAddress_null() throws Exception { 614 // validate if we pass in null that it picks an address for us. 615 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 616 theSocket.bind(null); 617 assertNotNull(theSocket.getLocalSocketAddress()); 618 theSocket.close(); 619 } 620 621 public void test_bindLjava_net_SocketAddress_address_in_use() throws Exception { 622 DatagramSocket socket1 = new DatagramSocket(0); 623 try { 624 new DatagramSocket(socket1.getLocalPort()); 625 fail(); 626 } catch (SocketException expected) { 627 } 628 socket1.close(); 629 } 630 631 public void test_bindLjava_net_SocketAddress_unsupported_address_type() throws Exception { 632 class mySocketAddress extends SocketAddress { 633 public mySocketAddress() { 634 } 635 } 636 637 // unsupported SocketAddress subclass 638 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 639 try { 640 theSocket.bind(new mySocketAddress()); 641 fail("No exception when binding using unsupported SocketAddress subclass"); 642 } catch (IllegalArgumentException expected) { 643 } 644 theSocket.close(); 645 } 646 647 public void test_isBound() throws Exception { 648 DatagramSocket theSocket = new DatagramSocket(0); 649 assertTrue(theSocket.isBound()); 650 theSocket.close(); 651 652 theSocket = new DatagramSocket(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); 653 assertTrue(theSocket.isBound()); 654 theSocket.close(); 655 656 theSocket = new DatagramSocket(null); 657 assertFalse(theSocket.isBound()); 658 theSocket.close(); 659 660 // connect causes implicit bind 661 theSocket = new DatagramSocket(null); 662 theSocket.connect(new InetSocketAddress(Inet6Address.LOOPBACK, 0)); 663 assertTrue(theSocket.isBound()); 664 theSocket.close(); 665 666 // now test when we bind explicitely 667 InetSocketAddress theLocalAddress = new InetSocketAddress(Inet6Address.LOOPBACK, 0); 668 theSocket = new DatagramSocket(null); 669 assertFalse(theSocket.isBound()); 670 theSocket.bind(theLocalAddress); 671 assertTrue(theSocket.isBound()); 672 theSocket.close(); 673 assertTrue(theSocket.isBound()); 674 } 675 676 public void test_isConnected() throws Exception { 677 DatagramServer ds = new DatagramServer(Inet6Address.LOOPBACK); 678 679 // base test 680 DatagramSocket theSocket = new DatagramSocket(0); 681 assertFalse(theSocket.isConnected()); 682 theSocket.connect(new InetSocketAddress(Inet6Address.LOOPBACK, ds.getPort())); 683 assertTrue(theSocket.isConnected()); 684 685 // reconnect the socket and make sure we get the right answer 686 theSocket.connect(new InetSocketAddress(Inet6Address.LOOPBACK, ds.getPort())); 687 assertTrue(theSocket.isConnected()); 688 689 // now disconnect the socket and make sure we get the right answer 690 theSocket.disconnect(); 691 assertFalse(theSocket.isConnected()); 692 theSocket.close(); 693 694 // now check behavior when socket is closed when connected 695 theSocket = new DatagramSocket(0); 696 theSocket.connect(new InetSocketAddress(Inet6Address.LOOPBACK, ds.getPort())); 697 theSocket.close(); 698 assertTrue(theSocket.isConnected()); 699 } 700 701 public void test_getRemoteSocketAddress() throws Exception { 702 DatagramServer server = new DatagramServer(Inet6Address.LOOPBACK); 703 DatagramSocket s = new DatagramSocket(0); 704 s.connect(new InetSocketAddress(Inet6Address.LOOPBACK, server.getPort())); 705 706 assertEquals(new InetSocketAddress(Inet6Address.LOOPBACK, server.getPort()), 707 s.getRemoteSocketAddress()); 708 s.close(); 709 710 // now create one that is not connected and validate that we get the 711 // right answer 712 DatagramSocket theSocket = new DatagramSocket(null); 713 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 714 assertNull(theSocket.getRemoteSocketAddress()); 715 716 // now connect and validate we get the right answer 717 theSocket.connect(new InetSocketAddress(Inet6Address.LOOPBACK, server.getPort())); 718 assertEquals(new InetSocketAddress(Inet6Address.LOOPBACK, server.getPort()), 719 theSocket.getRemoteSocketAddress()); 720 theSocket.close(); 721 } 722 723 public void test_getLocalSocketAddress_late_bind() throws Exception { 724 // An unbound socket should return null as its local address. 725 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 726 assertNull(theSocket.getLocalSocketAddress()); 727 728 // now bind the socket and make sure we get the right answer 729 InetSocketAddress localAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 730 theSocket.bind(localAddress); 731 assertEquals(localAddress.getAddress(), theSocket.getLocalAddress()); 732 assertTrue(theSocket.getLocalPort() > 0); 733 theSocket.close(); 734 } 735 736 public void test_getLocalSocketAddress_unbound() throws Exception { 737 InetSocketAddress localAddress1 = new InetSocketAddress(InetAddress.getLocalHost(), 0); 738 DatagramSocket s = new DatagramSocket(localAddress1); 739 assertEquals(localAddress1.getAddress(), s.getLocalAddress()); 740 s.close(); 741 742 InetSocketAddress remoteAddress = (InetSocketAddress) s.getRemoteSocketAddress(); 743 assertNull(remoteAddress); 744 } 745 746 public void test_getLocalSocketAddress_ANY() throws Exception { 747 DatagramSocket s = new DatagramSocket(0); 748 try { 749 assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), 750 ((InetSocketAddress) s.getLocalSocketAddress()).getAddress() instanceof Inet6Address); 751 } finally { 752 s.close(); 753 } 754 } 755 756 public void test_setReuseAddressZ() throws Exception { 757 // test case were we set it to false 758 DatagramSocket theSocket1 = null; 759 DatagramSocket theSocket2 = null; 760 try { 761 InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 762 theSocket1 = new DatagramSocket(null); 763 theSocket2 = new DatagramSocket(null); 764 theSocket1.setReuseAddress(false); 765 theSocket2.setReuseAddress(false); 766 theSocket1.bind(theAddress); 767 theSocket2.bind(new InetSocketAddress(InetAddress.getLocalHost(), theSocket1.getLocalPort())); 768 fail(); 769 } catch (BindException expected) { 770 } 771 if (theSocket1 != null) { 772 theSocket1.close(); 773 } 774 if (theSocket2 != null) { 775 theSocket2.close(); 776 } 777 778 // test case were we set it to true 779 InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 780 theSocket1 = new DatagramSocket(null); 781 theSocket2 = new DatagramSocket(null); 782 theSocket1.setReuseAddress(true); 783 theSocket2.setReuseAddress(true); 784 theSocket1.bind(theAddress); 785 theSocket2.bind(new InetSocketAddress(InetAddress.getLocalHost(), theSocket1.getLocalPort())); 786 787 if (theSocket1 != null) { 788 theSocket1.close(); 789 } 790 if (theSocket2 != null) { 791 theSocket2.close(); 792 } 793 794 // test the default case which we expect to be the same on all 795 // platforms 796 try { 797 theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0); 798 theSocket1 = new DatagramSocket(null); 799 theSocket2 = new DatagramSocket(null); 800 theSocket1.bind(theAddress); 801 theSocket2.bind(new InetSocketAddress(InetAddress.getLocalHost(), theSocket1.getLocalPort())); 802 fail("No exception when trying to connect to do duplicate socket bind with re-useaddr left as default"); 803 } catch (BindException expected) { 804 } 805 if (theSocket1 != null) { 806 theSocket1.close(); 807 } 808 if (theSocket2 != null) { 809 theSocket2.close(); 810 } 811 812 try { 813 theSocket1.setReuseAddress(true); 814 fail("SocketException was not thrown."); 815 } catch(SocketException se) { 816 //expected 817 } 818 } 819 820 public void test_getReuseAddress() throws Exception { 821 DatagramSocket theSocket = new DatagramSocket(); 822 theSocket.setReuseAddress(true); 823 assertTrue("getReuseAddress false when it should be true", theSocket.getReuseAddress()); 824 theSocket.setReuseAddress(false); 825 assertFalse("getReuseAddress true when it should be False", theSocket.getReuseAddress()); 826 theSocket.close(); 827 try { 828 theSocket.getReuseAddress(); 829 fail("SocketException was not thrown."); 830 } catch(SocketException se) { 831 //expected 832 } 833 } 834 835 public void test_setBroadcastZ() throws Exception { 836 DatagramSocket theSocket = new DatagramSocket(0); 837 theSocket.setBroadcast(false); 838 byte theBytes[] = { -1, -1, -1, -1 }; 839 840 // validate we cannot connect to the broadcast address when 841 // setBroadcast is false 842 try { 843 theSocket.connect(new InetSocketAddress(InetAddress.getByAddress(theBytes), 0)); 844 fail(); 845 } catch (Exception expected) { 846 } 847 848 // now validate that we can connect to the broadcast address when 849 // setBroadcast is true 850 theSocket.setBroadcast(true); 851 theSocket.connect(new InetSocketAddress(InetAddress.getByAddress(theBytes), 0)); 852 853 theSocket.close(); 854 try { 855 theSocket.setBroadcast(false); 856 fail(); 857 } catch(SocketException se) { 858 //expected 859 } 860 } 861 862 public void test_getBroadcast() throws Exception { 863 DatagramSocket theSocket = new DatagramSocket(); 864 theSocket.setBroadcast(true); 865 assertTrue("getBroadcast false when it should be true", theSocket.getBroadcast()); 866 theSocket.setBroadcast(false); 867 assertFalse("getBroadcast true when it should be False", theSocket.getBroadcast()); 868 } 869 870 public void test_setTrafficClassI() throws Exception { 871 int IPTOS_LOWCOST = 0x2; 872 int IPTOS_THROUGHPUT = 0x8; 873 DatagramSocket theSocket = new DatagramSocket(0); 874 875 // validate that value set must be between 0 and 255 876 try { 877 theSocket.setTrafficClass(256); 878 fail("No exception when traffic class set to 256"); 879 } catch (IllegalArgumentException e) { 880 } 881 882 try { 883 theSocket.setTrafficClass(-1); 884 fail("No exception when traffic class set to -1"); 885 } catch (IllegalArgumentException e) { 886 } 887 888 // now validate that we can set it to some good values 889 theSocket.setTrafficClass(IPTOS_LOWCOST); 890 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 891 } 892 893 894 public void test_isClosed() throws Exception { 895 DatagramSocket theSocket = new DatagramSocket(); 896 // validate isClosed returns expected values 897 assertFalse(theSocket.isClosed()); 898 theSocket.close(); 899 assertTrue(theSocket.isClosed()); 900 901 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 902 .getLocalHost(), 0); 903 theSocket = new DatagramSocket(theAddress); 904 assertFalse(theSocket.isClosed()); 905 theSocket.close(); 906 assertTrue(theSocket.isClosed()); 907 } 908 909 public void test_getChannel() throws Exception { 910 assertNull(new DatagramSocket().getChannel()); 911 912 DatagramServer server = new DatagramServer(Inet6Address.LOOPBACK); 913 DatagramSocket ds = new DatagramSocket(0); 914 assertNull(ds.getChannel()); 915 ds.disconnect(); 916 ds.close(); 917 server.stopServer(); 918 919 DatagramChannel channel = DatagramChannel.open(); 920 DatagramSocket socket = channel.socket(); 921 assertEquals(channel, socket.getChannel()); 922 socket.close(); 923 } 924 925 public void testReceiveOversizePacket() throws Exception { 926 DatagramSocket ds = new DatagramSocket(0); 927 DatagramSocket sds = new DatagramSocket(0); 928 929 DatagramPacket rdp = new DatagramPacket("0123456789".getBytes("UTF-8"), 930 5, Inet6Address.LOOPBACK, ds.getLocalPort()); 931 sds.send(rdp); 932 sds.close(); 933 934 byte[] recvBuffer = new byte[5]; 935 DatagramPacket receive = new DatagramPacket(recvBuffer, recvBuffer.length); 936 ds.receive(receive); 937 ds.close(); 938 assertEquals(new String("01234"), new String(recvBuffer, 0, recvBuffer.length, "UTF-8")); 939 } 940 } 941