OldDatagramSocketTest.java revision 04e25f30524dc7b51f280912d6ca97d991ae84b0
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package libcore.java.net; 19 20import java.io.IOException; 21import java.io.InterruptedIOException; 22import java.net.BindException; 23import java.net.DatagramPacket; 24import java.net.DatagramSocket; 25import java.net.DatagramSocketImpl; 26import java.net.DatagramSocketImplFactory; 27import java.net.Inet4Address; 28import java.net.Inet6Address; 29import java.net.InetAddress; 30import java.net.InetSocketAddress; 31import java.net.NetworkInterface; 32import java.net.PortUnreachableException; 33import java.net.SocketAddress; 34import java.net.SocketException; 35import java.net.SocketTimeoutException; 36import java.net.UnknownHostException; 37import java.nio.channels.DatagramChannel; 38import java.nio.channels.IllegalBlockingModeException; 39import java.util.Date; 40import java.util.Vector; 41import tests.support.Support_Configuration; 42import tests.support.Support_PortManager; 43 44public class OldDatagramSocketTest extends junit.framework./*Socket*/TestCase { 45 46 java.net.DatagramSocket ds; 47 48 java.net.DatagramPacket dp; 49 50 DatagramSocket sds = null; 51 52 String retval; 53 54 String testString = "Test String"; 55 56 boolean interrupted; 57 58 class DatagramServer extends Thread { 59 60 public DatagramSocket ms; 61 62 boolean running = true; 63 64 public volatile byte[] rbuf = new byte[512]; 65 66 volatile DatagramPacket rdp = null; 67 68 public void run() { 69 try { 70 while (running) { 71 try { 72 ms.receive(rdp); 73 // echo the packet back 74 ms.send(rdp); 75 } catch (java.io.InterruptedIOException e) { 76 Thread.yield(); 77 } 78 ; 79 } 80 ; 81 } catch (java.io.IOException e) { 82 System.out.println("DatagramServer server failed: " + e); 83 } finally { 84 ms.close(); 85 } 86 } 87 88 public void stopServer() { 89 running = false; 90 } 91 92 public DatagramServer(int aPort, InetAddress address) 93 throws java.io.IOException { 94 rbuf = new byte[512]; 95 rbuf[0] = -1; 96 rdp = new DatagramPacket(rbuf, rbuf.length); 97 ms = new DatagramSocket(aPort, address); 98 ms.setSoTimeout(2000); 99 } 100 } 101 102 public void test_Constructor() { 103 // Test for method java.net.DatagramSocket() 104 try { 105 ds = new java.net.DatagramSocket(); 106 } catch (Exception e) { 107 fail("Could not create DatagramSocket : " + e.getMessage()); 108 } 109 110 /* 111 SecurityManager sm = new SecurityManager() { 112 113 public void checkPermission(Permission perm) { 114 } 115 116 public void checkListen(int port) { 117 throw new SecurityException(); 118 } 119 }; 120 121 SecurityManager oldSm = System.getSecurityManager(); 122 System.setSecurityManager(sm); 123 try { 124 new DatagramSocket(); 125 fail("SecurityException should be thrown."); 126 } catch (SecurityException e) { 127 // expected 128 } catch (SocketException e) { 129 fail("SocketException was thrown."); 130 } finally { 131 System.setSecurityManager(oldSm); 132 } 133 */ 134 } 135 136 public void test_ConstructorI() { 137 // Test for method java.net.DatagramSocket(int) 138 try { 139 int portNumber = Support_PortManager.getNextPortForUDP(); 140 ds = new java.net.DatagramSocket(portNumber); 141 assertTrue("Created socket with incorrect port", 142 ds.getLocalPort() == portNumber); 143 } catch (Exception e) { 144 fail("Could not create DatagramSocket : " + e.getMessage()); 145 } 146 147 /* 148 SecurityManager sm = new SecurityManager() { 149 150 public void checkPermission(Permission perm) { 151 } 152 153 public void checkListen(int port) { 154 throw new SecurityException(); 155 } 156 }; 157 158 SecurityManager oldSm = System.getSecurityManager(); 159 System.setSecurityManager(sm); 160 try { 161 new DatagramSocket(8080); 162 fail("SecurityException should be thrown."); 163 } catch (SecurityException e) { 164 // expected 165 } catch (SocketException e) { 166 fail("SocketException was thrown."); 167 } finally { 168 System.setSecurityManager(oldSm); 169 } 170 */ 171 172 try { 173 DatagramSocket ds = new java.net.DatagramSocket(1); 174 if (!("root".equals(System.getProperty("user.name")))) { 175 fail("SocketException was not thrown."); 176 } 177 } catch (SocketException e) { 178 //expected 179 } 180 181 } 182 183 public void test_ConstructorILjava_net_InetAddress() { 184 // Test for method java.net.DatagramSocket(int, java.net.InetAddress) 185 try { 186 int portNumber = Support_PortManager.getNextPortForUDP(); 187 ds = new java.net.DatagramSocket(portNumber, InetAddress 188 .getLocalHost()); 189 assertTrue("Created socket with incorrect port", 190 ds.getLocalPort() == portNumber); 191 assertTrue("Created socket with incorrect address", ds 192 .getLocalAddress().equals(InetAddress.getLocalHost())); 193 } catch (Exception e) { 194 fail("Could not create DatagramSocket : " + e.getMessage()); 195 } 196 197 /* 198 SecurityManager sm = new SecurityManager() { 199 200 public void checkPermission(Permission perm) { 201 } 202 203 public void checkListen(int port) { 204 throw new SecurityException(); 205 } 206 }; 207 208 SecurityManager oldSm = System.getSecurityManager(); 209 System.setSecurityManager(sm); 210 try { 211 new java.net.DatagramSocket(8080, InetAddress 212 .getLocalHost()); 213 fail("SecurityException should be thrown."); 214 } catch (SecurityException e) { 215 // expected 216 } catch (SocketException e) { 217 fail("SocketException was thrown."); 218 } catch (UnknownHostException e) { 219 fail("UnknownHostException was thrown."); 220 } finally { 221 System.setSecurityManager(oldSm); 222 } 223 */ 224 225 try { 226 new java.net.DatagramSocket(1, InetAddress 227 .getLocalHost()); 228 fail("SocketException was not thrown."); 229 } catch(SocketException se) { 230 //expected 231 } catch (UnknownHostException e) { 232 fail("UnknownHostException was thrown."); 233 } 234 } 235 236 public void test_close() { 237 // Test for method void java.net.DatagramSocket.close() 238 try { 239 int portNumber = Support_PortManager.getNextPortForUDP(); 240 ds = new java.net.DatagramSocket(portNumber); 241 dp = new DatagramPacket("Test String".getBytes(), 11, InetAddress 242 .getLocalHost(), 0); 243 ds.close(); 244 try { 245 ds.send(dp); 246 fail("IOException was not thrown."); 247 } catch(IOException ioe) { 248 //expected 249 } 250 } catch (Exception e) { 251 fail("Unexpected exception: " + e.getMessage()); 252 } 253 } 254 255 public void test_connectLjava_net_InetAddressI() throws 256 UnknownHostException, SocketException { 257 try { 258 ds = new java.net.DatagramSocket(); 259 InetAddress inetAddress = InetAddress.getLocalHost(); 260 int portNumber = Support_PortManager.getNextPortForUDP(); 261 ds.connect(inetAddress, portNumber); 262 assertTrue("Incorrect InetAddress", ds.getInetAddress().equals( 263 inetAddress)); 264 assertTrue("Incorrect Port", ds.getPort() == portNumber); 265 ds.disconnect(); 266 } catch (Exception e) { 267 fail("Exception during test : " + e.getMessage()); 268 } 269 270 System.out 271 .println("Running test_connectLjava_net_InetAddressI" + 272 "(DatagramSocketTest) with IPv6GlobalAddressJcl4: " 273 + Support_Configuration.IPv6GlobalAddressJcl4); 274 try { 275 ds = new java.net.DatagramSocket(); 276 InetAddress inetAddress = InetAddress 277 .getByName(Support_Configuration.IPv6GlobalAddressJcl4); 278 int portNumber = Support_PortManager.getNextPortForUDP(); 279 ds.connect(inetAddress, portNumber); 280 assertTrue("Incorrect InetAddress", ds.getInetAddress().equals( 281 inetAddress)); 282 assertTrue("Incorrect Port", ds.getPort() == portNumber); 283 ds.disconnect(); 284 } catch (Exception e) { 285 fail("Exception during test : " + e.getMessage()); 286 } 287 288 try { 289 // Create a connected datagram socket to test 290 // PlainDatagramSocketImpl.peek() 291 InetAddress localHost = InetAddress.getLocalHost(); 292 DatagramSocket ds = new DatagramSocket(); 293 int port = ds.getLocalPort(); 294 ds.connect(localHost, port); 295 DatagramPacket send = new DatagramPacket(new byte[10], 10, 296 localHost, port); 297 ds.send(send); 298 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 299 ds.setSoTimeout(2000); 300 ds.receive(receive); 301 ds.close(); 302 assertTrue("Wrong size: " + receive.getLength(), receive 303 .getLength() == 10); 304 assertTrue("Wrong receiver", receive.getAddress().equals(localHost)); 305 } catch (IOException e) { 306 fail("Unexpected IOException : " + e.getMessage()); 307 } 308 309 class DatagramServer extends Thread { 310 311 public DatagramSocket ms; 312 313 boolean running = true; 314 315 public byte[] rbuf = new byte[512]; 316 317 DatagramPacket rdp = null; 318 319 public void run() { 320 try { 321 while (running) { 322 try { 323 ms.receive(rdp); 324 // echo the packet back 325 ms.send(rdp); 326 } catch (java.io.InterruptedIOException e) { 327 Thread.yield(); 328 } 329 330 } 331 332 } catch (java.io.IOException e) { 333 System.out.println("Multicast server failed: " + e); 334 } finally { 335 ms.close(); 336 } 337 } 338 339 public void stopServer() { 340 running = false; 341 } 342 343 public DatagramServer(int aPort, InetAddress address) 344 throws java.io.IOException { 345 rbuf = new byte[512]; 346 rbuf[0] = -1; 347 rdp = new DatagramPacket(rbuf, rbuf.length); 348 ms = new DatagramSocket(aPort, address); 349 ms.setSoTimeout(2000); 350 } 351 } 352 353 // validate that we get the PortUnreachable exception if we try to 354 // send a dgram to a server that is not running and then do a recv 355 try { 356 ds = new java.net.DatagramSocket(); 357 InetAddress inetAddress = InetAddress.getLocalHost(); 358 int portNumber = Support_PortManager.getNextPortForUDP(); 359 ds.connect(inetAddress, portNumber); 360 DatagramPacket send = new DatagramPacket(new byte[10], 10); 361 ds.send(send); 362 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 363 ds.setSoTimeout(10000); 364 ds.receive(receive); 365 ds.close(); 366 fail( 367 "No PortUnreachableException when connected at native level on recv "); 368 } catch (Exception e) { 369 assertTrue( 370 "Wrong exception when trying to connect at native level on recv: " 371 + e.toString(), 372 (e instanceof PortUnreachableException)); 373 } 374 375 // validate that we can send/receive with datagram sockets connected at 376 // the native level 377 DatagramServer server = null; 378 int[] ports = Support_PortManager.getNextPortsForUDP(3); 379 int serverPortNumber = ports[0]; 380 try { 381 InetAddress localHost = InetAddress.getLocalHost(); 382 DatagramSocket ds = new DatagramSocket(ports[1]); 383 DatagramSocket ds2 = new DatagramSocket(ports[2]); 384 385 try { 386 server = new DatagramServer(serverPortNumber, localHost); 387 server.start(); 388 Thread.sleep(1000); 389 } catch (Exception e) { 390 fail( 391 "Failed to set up datagram server for native connected Dgram socket test "); 392 } 393 394 int port = ds.getLocalPort(); 395 ds.connect(localHost, serverPortNumber); 396 397 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 398 DatagramPacket send = new DatagramPacket(sendBytes, 399 sendBytes.length); 400 ds.send(send); 401 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 402 ds.setSoTimeout(2000); 403 ds.receive(receive); 404 ds.close(); 405 assertTrue("Wrong size data received: " + receive.getLength(), 406 receive.getLength() == sendBytes.length); 407 assertTrue("Wrong data received" 408 + new String(receive.getData(), 0, receive.getLength()) 409 + ":" + new String(sendBytes), new String( 410 receive.getData(), 0, receive.getLength()) 411 .equals(new String(sendBytes))); 412 assertTrue("Wrong receiver:" + receive.getAddress() + ":" 413 + localHost, receive.getAddress().equals(localHost)); 414 } catch (Exception e) { 415 fail( 416 "Unexpected exception when sending data on dgram connected at native level:" 417 + e.toString()); 418 } 419 420 if (server != null) { 421 server.stopServer(); 422 } 423 424 // validate that we can disconnect 425 try { 426 ds = new java.net.DatagramSocket(); 427 InetAddress inetAddress = InetAddress.getLocalHost(); 428 int portNumber = Support_PortManager.getNextPortForUDP(); 429 ds.connect(inetAddress, portNumber); 430 ds.disconnect(); 431 ds.close(); 432 } catch (Exception e) { 433 assertTrue("Unexpected exception when trying to connect at native" 434 + e.toString(), (e instanceof PortUnreachableException)); 435 } 436 437 // validate that once connected we cannot send to another address 438 try { 439 ds = new java.net.DatagramSocket(); 440 InetAddress inetAddress = InetAddress.getLocalHost(); 441 int portNumber = Support_PortManager.getNextPortForUDP(); 442 ds.connect(inetAddress, portNumber); 443 DatagramPacket send = new DatagramPacket(new byte[10], 10, 444 inetAddress, portNumber + 1); 445 ds.send(send); 446 ds.close(); 447 fail( 448 "No Exception when trying to send to a different address on a connected socket "); 449 } catch (Exception e) { 450 assertTrue( 451 "Wrong exception when trying to send to a different address on a connected socket: " 452 + e.toString(), 453 (e instanceof IllegalArgumentException)); 454 } 455 456 // validate that we can connect, then disconnect, then connect then 457 // send/recv 458 server = null; 459 ports = Support_PortManager.getNextPortsForUDP(3); 460 serverPortNumber = ports[0]; 461 try { 462 InetAddress localHost = InetAddress.getLocalHost(); 463 DatagramSocket ds = new DatagramSocket(ports[1]); 464 DatagramSocket ds2 = new DatagramSocket(ports[2]); 465 466 try { 467 server = new DatagramServer(serverPortNumber, localHost); 468 server.start(); 469 Thread.sleep(1000); 470 } catch (Exception e) { 471 fail( 472 "Failed to set up datagram server for native connected Dgram socket test "); 473 } 474 475 int port = ds.getLocalPort(); 476 ds.connect(localHost, serverPortNumber + 1); 477 ds.disconnect(); 478 ds.connect(localHost, serverPortNumber); 479 480 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 481 DatagramPacket send = new DatagramPacket(sendBytes, 482 sendBytes.length); 483 ds.send(send); 484 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 485 ds.setSoTimeout(2000); 486 ds.receive(receive); 487 ds.close(); 488 assertTrue( 489 "connect/disconnect/connect - Wrong size data received: " 490 + receive.getLength(), 491 receive.getLength() == sendBytes.length); 492 assertTrue("connect/disconnect/connect - Wrong data received" 493 + new String(receive.getData(), 0, receive.getLength()) 494 + ":" + new String(sendBytes), new String( 495 receive.getData(), 0, receive.getLength()) 496 .equals(new String(sendBytes))); 497 assertTrue("connect/disconnect/connect - Wrong receiver:" 498 + receive.getAddress() + ":" + localHost, receive 499 .getAddress().equals(localHost)); 500 } catch (Exception e) { 501 fail( 502 "Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:" 503 + e.toString()); 504 } 505 506 if (server != null) { 507 server.stopServer(); 508 } 509 510 // validate that we can connect/disconnect then send/recv to any address 511 server = null; 512 ports = Support_PortManager.getNextPortsForUDP(3); 513 serverPortNumber = ports[0]; 514 try { 515 InetAddress localHost = InetAddress.getLocalHost(); 516 DatagramSocket ds = new DatagramSocket(ports[1]); 517 DatagramSocket ds2 = new DatagramSocket(ports[2]); 518 519 try { 520 server = new DatagramServer(serverPortNumber, localHost); 521 server.start(); 522 Thread.sleep(1000); 523 } catch (Exception e) { 524 fail( 525 "Failed to set up datagram server for native connected Dgram socket test "); 526 } 527 528 int port = ds.getLocalPort(); 529 ds.connect(localHost, serverPortNumber + 1); 530 ds.disconnect(); 531 532 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 533 DatagramPacket send = new DatagramPacket(sendBytes, 534 sendBytes.length, localHost, serverPortNumber); 535 ds.send(send); 536 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 537 ds.setSoTimeout(2000); 538 ds.receive(receive); 539 ds.close(); 540 assertTrue("connect/disconnect - Wrong size data received: " 541 + receive.getLength(), 542 receive.getLength() == sendBytes.length); 543 assertTrue("connect/disconnect - Wrong data received" 544 + new String(receive.getData(), 0, receive.getLength()) 545 + ":" + new String(sendBytes), new String( 546 receive.getData(), 0, receive.getLength()) 547 .equals(new String(sendBytes))); 548 assertTrue("connect/disconnect - Wrong receiver:" 549 + receive.getAddress() + ":" + localHost, receive 550 .getAddress().equals(localHost)); 551 } catch (Exception e) { 552 fail( 553 "Unexpected exception when sending data on dgram connected at native level after connect/disconnect:" 554 + e.toString()); 555 } 556 557 if (server != null) { 558 server.stopServer(); 559 } 560 561 // validate that we can connect on an allready connected socket and then 562 // send/recv 563 server = null; 564 ports = Support_PortManager.getNextPortsForUDP(3); 565 serverPortNumber = ports[0]; 566 try { 567 InetAddress localHost = InetAddress.getLocalHost(); 568 DatagramSocket ds = new DatagramSocket(ports[1]); 569 DatagramSocket ds2 = new DatagramSocket(ports[2]); 570 571 try { 572 server = new DatagramServer(serverPortNumber, localHost); 573 server.start(); 574 Thread.sleep(1000); 575 } catch (Exception e) { 576 fail( 577 "Failed to set up datagram server for native connected Dgram socket test "); 578 } 579 580 int port = ds.getLocalPort(); 581 ds.connect(localHost, serverPortNumber + 1); 582 ds.connect(localHost, serverPortNumber); 583 584 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 585 DatagramPacket send = new DatagramPacket(sendBytes, 586 sendBytes.length); 587 ds.send(send); 588 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 589 ds.setSoTimeout(2000); 590 ds.receive(receive); 591 ds.close(); 592 assertTrue("connect/connect - Wrong size data received: " 593 + receive.getLength(), 594 receive.getLength() == sendBytes.length); 595 assertTrue("connect/connect - Wrong data received" 596 + new String(receive.getData(), 0, receive.getLength()) 597 + ":" + new String(sendBytes), new String( 598 receive.getData(), 0, receive.getLength()) 599 .equals(new String(sendBytes))); 600 assertTrue("connect/connect - Wrong receiver:" 601 + receive.getAddress() + ":" + localHost, receive 602 .getAddress().equals(localHost)); 603 } catch (Exception e) { 604 fail( 605 "Unexpected exception when sending data on dgram connected at native level after connect/connect: " 606 + e.toString()); 607 } 608 609 if (server != null) { 610 server.stopServer(); 611 } 612 613 // test for when we fail to connect at the native level. Even though we 614 // fail at the native level there is no way to return an exception so 615 // there should be no exception 616 try { 617 ds = new java.net.DatagramSocket(); 618 byte[] addressBytes = { 0, 0, 0, 0 }; 619 InetAddress inetAddress = InetAddress.getByAddress(addressBytes); 620 int portNumber = Support_PortManager.getNextPortForUDP(); 621 ds.connect(inetAddress, portNumber); 622 } catch (Exception e) { 623 fail( 624 "Unexcpected exception when trying to connect at native level with bad address for signature with no exception to be returned: " 625 + e.toString()); 626 } 627 628 System.out 629 .println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6 address"); 630 try { 631 ds = new java.net.DatagramSocket(); 632 byte[] addressBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 633 0, 0, 0 }; 634 InetAddress inetAddress = InetAddress 635 .getByAddress(addressBytes); 636 int portNumber = Support_PortManager.getNextPortForUDP(); 637 ds.connect(inetAddress, portNumber); 638 } catch (Exception e) { 639 fail( 640 "Unexcpected exception when trying to connect at native level with bad IPv6 address for signature with no exception to be returned: " 641 + e.toString()); 642 } 643 } 644 645 public void test_disconnect() { 646 try { 647 ds = new java.net.DatagramSocket(); 648 InetAddress inetAddress = InetAddress.getLocalHost(); 649 int portNumber = Support_PortManager.getNextPortForUDP(); 650 ds.connect(inetAddress, portNumber); 651 ds.disconnect(); 652 assertNull("Incorrect InetAddress", ds.getInetAddress()); 653 assertEquals("Incorrect Port", -1, ds.getPort()); 654 } catch (Exception e) { 655 fail("Exception during test : " + e.getMessage()); 656 } 657 658 System.out 659 .println("Running test_disconnect(DatagramSocketTest) with IPv6GlobalAddressJcl4: " 660 + Support_Configuration.IPv6GlobalAddressJcl4); 661 try { 662 ds = new java.net.DatagramSocket(); 663 InetAddress inetAddress = InetAddress 664 .getByName(Support_Configuration.IPv6GlobalAddressJcl4); 665 int portNumber = Support_PortManager.getNextPortForUDP(); 666 ds.connect(inetAddress, portNumber); 667 ds.disconnect(); 668 assertNull("Incorrect InetAddress", ds.getInetAddress()); 669 assertEquals("Incorrect Port", -1, ds.getPort()); 670 } catch (Exception e) { 671 fail("Exception during test : " + e.getMessage()); 672 } 673 } 674 675 public void test_getInetAddress() { 676 Vector<InetAddress> ias = new Vector<InetAddress>(); 677 try { 678 ias.add(InetAddress.getLocalHost()); 679 ias.add(InetAddress 680 .getByName(Support_Configuration.IPv6GlobalAddressJcl4)); 681 ias.add(InetAddress 682 .getByName(Support_Configuration.InetTestAddress2)); 683 ias.add(InetAddress 684 .getByName(Support_Configuration.InetTestAddress2)); 685 ias.add(InetAddress 686 .getByName(Support_Configuration.InetTestIP)); 687 } catch(Exception e) { 688 fail("Unexpected exception was thrown: " + e.toString()); 689 } 690 691 for(InetAddress ia:ias) { 692 int portNumber = Support_PortManager.getNextPortForUDP(); 693 DatagramSocket ds = null; 694 try { 695 ds = new DatagramSocket(); 696 ds.connect(ia, portNumber); 697 assertEquals(ia, ds.getInetAddress()); 698 assertEquals("" + ia, ia, ds.getInetAddress()); 699 } catch (SocketException e) { 700 fail("SocketException was thrown."); 701 } finally { 702 ds.disconnect(); 703 ds.close(); 704 } 705 } 706 707 try { 708 assertNull(new DatagramSocket().getInetAddress()); 709 } catch (SocketException e) { 710 fail("SocketException was thrown."); 711 } 712 713 } 714 715 public void test_getLocalPort() { 716 // Test for method int java.net.DatagramSocket.getLocalPort() 717 try { 718 int portNumber = Support_PortManager.getNextPortForUDP(); 719 ds = new java.net.DatagramSocket(portNumber); 720 assertTrue("Returned incorrect port", 721 ds.getLocalPort() == portNumber); 722 } catch (Exception e) { 723 fail("Exception during getLocalAddress : " + e.getMessage()); 724 } 725 } 726 727 public void test_getPort() { 728 try { 729 int portNumber = Support_PortManager.getNextPortForUDP(); 730 DatagramSocket theSocket = new DatagramSocket(portNumber); 731 assertEquals("Expected -1 for remote port as not connected", 732 -1, theSocket.getPort()); 733 734 // now connect the socket and validate that we get the right port 735 theSocket.connect(InetAddress.getLocalHost(), portNumber); 736 assertTrue("getPort returned wrong value:" + theSocket.getPort() 737 + ":Expected:" + portNumber, 738 theSocket.getPort() == portNumber); 739 } catch (Exception e) { 740 fail("unexpected exception during getPort test : " + e.getMessage()); 741 } 742 } 743 744 public void test_getReceiveBufferSize() throws Exception { 745 int portNumber = Support_PortManager.getNextPortForUDP(); 746 ds = new java.net.DatagramSocket(portNumber); 747 ds.setReceiveBufferSize(130); 748 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 749 ds.close(); 750 try { 751 ds.getReceiveBufferSize(); 752 fail("SocketException was not thrown."); 753 } catch(SocketException se) { 754 //expected 755 } 756 } 757 758 public void test_getSendBufferSize() throws Exception { 759 int portNumber = Support_PortManager.getNextPortForUDP(); 760 ds = new java.net.DatagramSocket(portNumber); 761 ds.setSendBufferSize(134); 762 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 763 ds.close(); 764 try { 765 ds.getSendBufferSize(); 766 fail("SocketException was not thrown."); 767 } catch(SocketException se) { 768 //expected 769 } 770 } 771 772 public void test_getSoTimeout_setSoTimeout() throws Exception { 773 // TODO: a useful test would check that setSoTimeout actually causes timeouts! 774 DatagramSocket s = new DatagramSocket(); 775 s.setSoTimeout(1500); 776 int ms = s.getSoTimeout(); 777 if (ms < 1500-10 || ms > 1500+10) { 778 fail("suspicious timeout: " + ms); 779 } 780 s.close(); 781 try { 782 s.getSoTimeout(); 783 fail("SocketException was not thrown."); 784 } catch (SocketException expected) { 785 } 786 try { 787 s.setSoTimeout(1000); 788 fail("SocketException was not thrown."); 789 } catch (SocketException expected) { 790 } 791 } 792 793 public void test_receiveLjava_net_DatagramPacket() throws Exception { 794 // Test for method void 795 // java.net.DatagramSocket.receive(java.net.DatagramPacket) 796 797 receive_oversize_java_net_DatagramPacket(); 798 final int[] ports = Support_PortManager.getNextPortsForUDP(2); 799 final int portNumber = ports[0]; 800 801 class TestDGRcv implements Runnable { 802 public void run() { 803 try { 804 InetAddress localHost = InetAddress.getLocalHost(); 805 Thread.sleep(1000); 806 DatagramSocket sds = new DatagramSocket(ports[1]); 807 DatagramPacket rdp = new DatagramPacket("Test String" 808 .getBytes(), 11, localHost, portNumber); 809 sds.send(rdp); 810 sds.close(); 811 } catch (Exception e) { 812 throw new RuntimeException(e); 813 } 814 } 815 } 816 817 try { 818 new Thread(new TestDGRcv(), "DGSender").start(); 819 ds = new java.net.DatagramSocket(portNumber); 820 ds.setSoTimeout(6000); 821 byte rbuf[] = new byte[1000]; 822 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 823 ds.receive(rdp); 824 ds.close(); 825 assertEquals("Send/Receive failed to return correct data: " 826 + new String(rbuf, 0, 11), "Test String", new String(rbuf, 0, 11)); 827 } finally { 828 ds.close(); 829 } 830 DatagramSocket socket = null; 831 try { 832 byte rbuf[] = new byte[1000]; 833 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 834 DatagramChannel channel = DatagramChannel.open(); 835 channel.configureBlocking(false); 836 socket = channel.socket(); 837 socket.receive(rdp); 838 fail("IllegalBlockingModeException was not thrown."); 839 } catch(IllegalBlockingModeException expected) { 840 } finally { 841 socket.close(); 842 } 843 844 try { 845 ds = new java.net.DatagramSocket(portNumber); 846 ds.setSoTimeout(1000); 847 byte rbuf[] = new byte[1000]; 848 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 849 ds.receive(rdp); 850 fail("SocketTimeoutException was not thrown."); 851 } catch(SocketTimeoutException expected) { 852 } finally { 853 ds.close(); 854 } 855 856 interrupted = false; 857 final DatagramSocket ds = new DatagramSocket(); 858 ds.setSoTimeout(12000); 859 Runnable runnable = new Runnable() { 860 public void run() { 861 try { 862 ds.receive(new DatagramPacket(new byte[1], 1)); 863 } catch (InterruptedIOException e) { 864 interrupted = true; 865 } catch (IOException ignored) { 866 } 867 } 868 }; 869 Thread thread = new Thread(runnable, "DatagramSocket.receive1"); 870 thread.start(); 871 do { 872 Thread.sleep(500); 873 } while (!thread.isAlive()); 874 ds.close(); 875 int c = 0; 876 do { 877 Thread.sleep(500); 878 if (interrupted) { 879 fail("received interrupt"); 880 } 881 if (++c > 4) { 882 fail("read call did not exit"); 883 } 884 } while (thread.isAlive()); 885 886 interrupted = false; 887 final int portNum = ports[0]; 888 final DatagramSocket ds2 = new DatagramSocket(ports[1]); 889 ds2.setSoTimeout(12000); 890 Runnable runnable2 = new Runnable() { 891 public void run() { 892 try { 893 ds2.receive(new DatagramPacket(new byte[1], 1, 894 InetAddress.getLocalHost(), portNum)); 895 } catch (InterruptedIOException e) { 896 interrupted = true; 897 } catch (IOException ignored) { 898 } 899 } 900 }; 901 Thread thread2 = new Thread(runnable2, "DatagramSocket.receive2"); 902 thread2.start(); 903 try { 904 do { 905 Thread.sleep(500); 906 } while (!thread2.isAlive()); 907 } catch (InterruptedException ignored) { 908 } 909 ds2.close(); 910 int c2 = 0; 911 do { 912 Thread.sleep(500); 913 if (interrupted) { 914 fail("receive2 was interrupted"); 915 } 916 if (++c2 > 4) { 917 fail("read2 call did not exit"); 918 } 919 } while (thread2.isAlive()); 920 921 interrupted = false; 922 DatagramSocket ds3 = new DatagramSocket(); 923 ds3.setSoTimeout(500); 924 Date start = new Date(); 925 try { 926 ds3.receive(new DatagramPacket(new byte[1], 1)); 927 } catch (InterruptedIOException e) { 928 interrupted = true; 929 } 930 ds3.close(); 931 assertTrue("receive not interrupted", interrupted); 932 int delay = (int) (new Date().getTime() - start.getTime()); 933 assertTrue("timeout too soon: " + delay, delay >= 490); 934 } 935 936 public void test_sendLjava_net_DatagramPacket() throws Exception { 937 // Test for method void 938 // java.net.DatagramSocket.send(java.net.DatagramPacket) 939 int[] ports = Support_PortManager.getNextPortsForUDP(2); 940 final int portNumber = ports[0]; 941 942 class TestDGSend implements Runnable { 943 Thread pThread; 944 945 public TestDGSend(Thread t) { 946 pThread = t; 947 } 948 949 public void run() { 950 try { 951 byte[] rbuf = new byte[1000]; 952 953 sds = new DatagramSocket(portNumber); 954 DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length); 955 sds.setSoTimeout(6000); 956 sds.receive(sdp); 957 retval = new String(rbuf, 0, testString.length()); 958 pThread.interrupt(); 959 } catch (java.io.InterruptedIOException e) { 960 System.out.println("Recv operation timed out"); 961 pThread.interrupt(); 962 ds.close(); 963 } catch (Exception e) { 964 System.out.println("Failed to establish Dgram server: " + e); 965 } 966 } 967 } 968 try { 969 new Thread(new TestDGSend(Thread.currentThread()), "DGServer") 970 .start(); 971 ds = new java.net.DatagramSocket(ports[1]); 972 dp = new DatagramPacket(testString.getBytes(), testString.length(), 973 InetAddress.getLocalHost(), portNumber); 974 // Wait to allow send to occur 975 try { 976 Thread.sleep(500); 977 ds.send(dp); 978 Thread.sleep(5000); 979 } catch (InterruptedException e) { 980 ds.close(); 981 assertTrue("Incorrect data sent: " + retval, retval 982 .equals(testString)); 983 } 984 } catch (Exception e) { 985 fail("Exception during send test : " + e.getMessage()); 986 } finally { 987 ds.close(); 988 } 989 990 /* 991 SecurityManager sm = new SecurityManager() { 992 993 public void checkPermission(Permission perm) { 994 } 995 996 public void checkMulticast(InetAddress maddr) { 997 throw new SecurityException(); 998 } 999 1000 public void checkConnect(String host, 1001 int port) { 1002 throw new SecurityException(); 1003 } 1004 }; 1005 try { 1006 1007 ds = new java.net.DatagramSocket(ports[1]); 1008 dp = new DatagramPacket(testString.getBytes(), testString.length(), 1009 InetAddress.getLocalHost(), portNumber); 1010 1011 SecurityManager oldSm = System.getSecurityManager(); 1012 System.setSecurityManager(sm); 1013 try { 1014 ds.send(dp); 1015 fail("SecurityException should be thrown."); 1016 } catch (SecurityException e) { 1017 // expected 1018 } catch (SocketException e) { 1019 fail("SocketException was thrown."); 1020 } finally { 1021 System.setSecurityManager(oldSm); 1022 } 1023 } catch(Exception e) { 1024 fail("Unexpected exception was thrown: " + e.getMessage()); 1025 } 1026 */ 1027 1028 DatagramSocket socket = null; 1029 try { 1030 byte rbuf[] = new byte[1000]; 1031 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 1032 SocketAddress address = new InetSocketAddress(portNumber); 1033 DatagramChannel channel = DatagramChannel.open(); 1034 channel.configureBlocking(false); 1035 socket = channel.socket(); 1036 socket.send(rdp); 1037 fail("IllegalBlockingModeException was not thrown."); 1038 } catch(IllegalBlockingModeException ibme) { 1039 //expected 1040 } catch(IOException ioe) { 1041 fail("IOException was thrown: " + ioe.getMessage()); 1042 } finally { 1043 socket.close(); 1044 } 1045 1046 //Regression for HARMONY-1118 1047 class testDatagramSocket extends DatagramSocket { 1048 public testDatagramSocket(DatagramSocketImpl impl){ 1049 super(impl); 1050 } 1051 } 1052 class testDatagramSocketImpl extends DatagramSocketImpl { 1053 protected void create() throws SocketException {} 1054 protected void bind(int arg0, InetAddress arg1) throws SocketException {} 1055 protected void send(DatagramPacket arg0) throws IOException {} 1056 protected int peek(InetAddress arg0) throws IOException { 1057 return 0; 1058 } 1059 protected int peekData(DatagramPacket arg0) throws IOException { 1060 return 0; 1061 } 1062 protected void receive(DatagramPacket arg0) throws IOException {} 1063 protected void setTTL(byte arg0) throws IOException {} 1064 protected byte getTTL() throws IOException { 1065 return 0; 1066 } 1067 protected void setTimeToLive(int arg0) throws IOException {} 1068 protected int getTimeToLive() throws IOException { 1069 return 0; 1070 } 1071 protected void join(InetAddress arg0) throws IOException {} 1072 protected void leave(InetAddress arg0) throws IOException {} 1073 protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {} 1074 protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {} 1075 protected void close() {} 1076 public void setOption(int arg0, Object arg1) throws SocketException {} 1077 public Object getOption(int arg0) throws SocketException { 1078 return null; 1079 } 1080 } 1081 InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0); 1082 //no exception expected for next line 1083 new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa)); 1084 1085 // Regression test for Harmony-2938 1086 InetAddress i = InetAddress.getByName("127.0.0.1"); 1087 DatagramSocket d = new DatagramSocket(0, i); 1088 try { 1089 d.send(new DatagramPacket(new byte[] { 1 }, 1)); 1090 fail("should throw NPE."); 1091 } catch (NullPointerException e) { 1092 // expected; 1093 } finally { 1094 d.close(); 1095 } 1096 } 1097 1098 public void test_setSendBufferSizeI() throws Exception { 1099 int portNumber = Support_PortManager.getNextPortForUDP(); 1100 ds = new java.net.DatagramSocket(portNumber); 1101 ds.setSendBufferSize(134); 1102 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 1103 ds.close(); 1104 try { 1105 ds.setSendBufferSize(1); 1106 fail("SocketException was not thrown."); 1107 } catch(SocketException se) { 1108 //expected 1109 } 1110 } 1111 1112 public void test_setReceiveBufferSizeI() throws Exception { 1113 int portNumber = Support_PortManager.getNextPortForUDP(); 1114 ds = new java.net.DatagramSocket(portNumber); 1115 ds.setReceiveBufferSize(130); 1116 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 1117 1118 try { 1119 ds.setReceiveBufferSize(0); 1120 fail("IllegalArgumentException was not thrown."); 1121 } catch(IllegalArgumentException iae) { 1122 //expected 1123 } 1124 1125 try { 1126 ds.setReceiveBufferSize(-1); 1127 fail("IllegalArgumentException was not thrown."); 1128 } catch(IllegalArgumentException iae) { 1129 //expected 1130 } 1131 1132 ds.close(); 1133 1134 try { 1135 ds.setReceiveBufferSize(1); 1136 fail("SocketException was not thrown."); 1137 } catch (SocketException e) { 1138 //expected 1139 } 1140 } 1141 1142 public void test_ConstructorLjava_net_DatagramSocketImpl() { 1143 class testDatagramSocket extends DatagramSocket { 1144 public testDatagramSocket(DatagramSocketImpl impl){ 1145 super(impl); 1146 } 1147 } 1148 1149 try { 1150 new testDatagramSocket((DatagramSocketImpl) null); 1151 fail("exception expected"); 1152 } catch (NullPointerException ex) { 1153 //expected 1154 } 1155 } 1156 1157 public void test_ConstructorLjava_net_SocketAddress() { 1158 class mySocketAddress extends SocketAddress { 1159 1160 public mySocketAddress() { 1161 } 1162 } 1163 1164 try { 1165 try { 1166 int portNumber = Support_PortManager.getNextPortForUDP(); 1167 ds = new java.net.DatagramSocket(new InetSocketAddress( 1168 InetAddress.getLocalHost(), portNumber)); 1169 assertTrue(ds.getBroadcast()); 1170 assertTrue("Created socket with incorrect port", ds 1171 .getLocalPort() == portNumber); 1172 assertTrue("Created socket with incorrect address", ds 1173 .getLocalAddress().equals(InetAddress.getLocalHost())); 1174 } catch (Exception e) { 1175 fail("Could not create DatagramSocket : " + e.getMessage()); 1176 } 1177 1178 try { 1179 int portNumber = Support_PortManager.getNextPortForUDP(); 1180 ds = new java.net.DatagramSocket(new mySocketAddress()); 1181 fail( 1182 "No exception when constucting datagramSocket with unsupported SocketAddress type"); 1183 } catch (IllegalArgumentException e) { 1184 1185 } 1186 //regression for Harmony-894 1187 ds = new DatagramSocket((SocketAddress)null); 1188 assertTrue(ds.getBroadcast()); 1189 } catch (Exception ex) { 1190 fail( 1191 "unexpected exception when datagramSocket SocketAddress constructor test"); 1192 } 1193 1194 /* 1195 SecurityManager sm = new SecurityManager() { 1196 1197 public void checkPermission(Permission perm) { 1198 } 1199 1200 public void checkListen(int port) { 1201 throw new SecurityException(); 1202 } 1203 }; 1204 1205 SecurityManager oldSm = System.getSecurityManager(); 1206 System.setSecurityManager(sm); 1207 try { 1208 new DatagramSocket(new InetSocketAddress( 1209 InetAddress.getLocalHost(), 1)); 1210 fail("SecurityException should be thrown."); 1211 } catch (SecurityException e) { 1212 // expected 1213 } catch (SocketException e) { 1214 fail("SocketException was thrown."); 1215 } catch (UnknownHostException e) { 1216 fail("UnknownHostException was thrown."); 1217 } finally { 1218 System.setSecurityManager(oldSm); 1219 } 1220 */ 1221 1222 InetSocketAddress isa = null; 1223 try { 1224 isa = new InetSocketAddress( 1225 InetAddress.getLocalHost(), 1); 1226 } catch (UnknownHostException e) { 1227 fail("UnknownHostException was thrown."); 1228 } 1229 } 1230 1231 public void test_bindLjava_net_SocketAddress() throws Exception { 1232 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1233 int serverPortNumber = ports[1]; 1234 1235 // now create a socket that is not bound and then bind it 1236 InetAddress localHost = InetAddress.getLocalHost(); 1237 InetSocketAddress localAddress1 = new InetSocketAddress(localHost, ports[0]); 1238 DatagramSocket theSocket = new DatagramSocket(localAddress1); 1239 1240 // validate that the localSocketAddress reflects the address we bound to 1241 assertEquals(localAddress1, theSocket.getLocalSocketAddress()); 1242 1243 // now make sure that datagrams sent from this socket appear to come 1244 // from the address we bound to 1245 InetSocketAddress localAddress2 = new InetSocketAddress(localHost, ports[2]); 1246 DatagramSocket ds = new DatagramSocket((SocketAddress) null); 1247 ds.bind(localAddress2); 1248 1249 DatagramServer server = new DatagramServer(serverPortNumber, localHost); 1250 server.start(); 1251 Thread.sleep(1000); 1252 1253 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1254 1255 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1256 DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length); 1257 ds.send(send); 1258 Thread.sleep(1000); 1259 ds.close(); 1260 // Check that the address in the packet matches the bound address. 1261 assertEquals(localAddress2, server.rdp.getSocketAddress()); 1262 1263 if (server != null) { 1264 server.stopServer(); 1265 } 1266 } 1267 1268 public void test_bindLjava_net_SocketAddress_null() throws Exception { 1269 // validate if we pass in null that it picks an address for us. 1270 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1271 theSocket.bind(null); 1272 assertNotNull(theSocket.getLocalSocketAddress()); 1273 theSocket.close(); 1274 } 1275 1276 public void test_bindLjava_net_SocketAddress_bad_address() throws Exception { 1277 // Address we cannot bind to 1278 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1279 try { 1280 InetAddress badAddress = InetAddress.getByAddress(Support_Configuration.nonLocalAddressBytes); 1281 theSocket.bind(new InetSocketAddress(badAddress, Support_PortManager.getNextPortForUDP())); 1282 fail("No exception when binding to bad address"); 1283 } catch (SocketException expected) { 1284 } 1285 theSocket.close(); 1286 } 1287 1288 public void test_bindLjava_net_SocketAddress_address_in_use() throws Exception { 1289 // Address that we have already bound to 1290 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1291 DatagramSocket theSocket1 = new DatagramSocket((SocketAddress) null); 1292 DatagramSocket theSocket2 = new DatagramSocket(ports[0]); 1293 try { 1294 InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), ports[1]); 1295 theSocket1.bind(theAddress); 1296 theSocket2.bind(theAddress); 1297 fail("No exception binding to address that is not available"); 1298 } catch (SocketException expected) { 1299 } 1300 theSocket1.close(); 1301 theSocket2.close(); 1302 } 1303 1304 public void test_bindLjava_net_SocketAddress_unsupported_address_type() throws Exception { 1305 class mySocketAddress extends SocketAddress { 1306 public mySocketAddress() { 1307 } 1308 } 1309 1310 // unsupported SocketAddress subclass 1311 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1312 try { 1313 theSocket.bind(new mySocketAddress()); 1314 fail("No exception when binding using unsupported SocketAddress subclass"); 1315 } catch (IllegalArgumentException expected) { 1316 } 1317 theSocket.close(); 1318 } 1319 1320 public void test_connectLjava_net_SocketAddress() { 1321 1322 // validate that we get the PortUnreachable exception if we try to 1323 // send a dgram to a server that is not running and then do a recv 1324 try { 1325 ds = new java.net.DatagramSocket(); 1326 InetAddress inetAddress = InetAddress.getLocalHost(); 1327 int portNumber = Support_PortManager.getNextPortForUDP(); 1328 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1329 DatagramPacket send = new DatagramPacket(new byte[10], 10); 1330 ds.send(send); 1331 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1332 ds.setSoTimeout(10000); 1333 ds.receive(receive); 1334 ds.close(); 1335 fail( 1336 "No PortUnreachableException when connected at native level on recv "); 1337 } catch (Exception e) { 1338 assertTrue( 1339 "Wrong exception when trying to connect at native level on recv: " 1340 + e.toString(), 1341 (e instanceof PortUnreachableException)); 1342 } 1343 1344 try { 1345 ds = new java.net.DatagramSocket(); 1346 InetAddress inetAddress = InetAddress.getLocalHost(); 1347 int portNumber = Support_PortManager.getNextPortForUDP(); 1348 1349 ds.connect(new InetSocketAddress("asdfasdf", 1)); 1350 ds.close(); 1351 fail("SocketException was not thrown."); 1352 } catch (Exception e) { 1353 assertTrue( 1354 "Wrong exception when trying to connect to unknown host: " 1355 + e.toString(), 1356 (e instanceof SocketException)); 1357 } 1358 1359 // validate that we can send/receive with datagram sockets connected at 1360 // the native level 1361 DatagramServer server = null; 1362 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1363 int serverPortNumber = ports[0]; 1364 try { 1365 InetAddress localHost = InetAddress.getLocalHost(); 1366 DatagramSocket ds = new DatagramSocket(ports[1]); 1367 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1368 1369 try { 1370 server = new DatagramServer(serverPortNumber, localHost); 1371 server.start(); 1372 Thread.sleep(1000); 1373 } catch (Exception e) { 1374 fail( 1375 "Failed to set up datagram server for native connected Dgram socket test "); 1376 } 1377 1378 int port = ds.getLocalPort(); 1379 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1380 1381 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1382 DatagramPacket send = new DatagramPacket(sendBytes, 1383 sendBytes.length); 1384 ds.send(send); 1385 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1386 ds.setSoTimeout(2000); 1387 ds.receive(receive); 1388 ds.close(); 1389 assertTrue("Wrong size data received: " + receive.getLength(), 1390 receive.getLength() == sendBytes.length); 1391 assertTrue("Wrong data received" 1392 + new String(receive.getData(), 0, receive.getLength()) 1393 + ":" + new String(sendBytes), new String( 1394 receive.getData(), 0, receive.getLength()) 1395 .equals(new String(sendBytes))); 1396 assertTrue("Wrong receiver:" + receive.getAddress() + ":" 1397 + localHost, receive.getAddress().equals(localHost)); 1398 } catch (Exception e) { 1399 fail( 1400 "Unexpected exception when sending data on dgram connected at native level:" 1401 + e.toString()); 1402 } 1403 1404 if (server != null) { 1405 server.stopServer(); 1406 } 1407 1408 // validate that we can disconnect 1409 try { 1410 ds = new java.net.DatagramSocket(); 1411 InetAddress inetAddress = InetAddress.getLocalHost(); 1412 int portNumber = Support_PortManager.getNextPortForUDP(); 1413 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1414 ds.disconnect(); 1415 ds.close(); 1416 } catch (Exception e) { 1417 assertTrue("Unexpected exception when trying to connect at native" 1418 + e.toString(), (e instanceof PortUnreachableException)); 1419 } 1420 1421 // validate that once connected we cannot send to another address 1422 try { 1423 ds = new java.net.DatagramSocket(); 1424 InetAddress inetAddress = InetAddress.getLocalHost(); 1425 int portNumber = Support_PortManager.getNextPortForUDP(); 1426 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1427 DatagramPacket send = new DatagramPacket(new byte[10], 10, 1428 inetAddress, portNumber + 1); 1429 ds.send(send); 1430 ds.close(); 1431 fail( 1432 "No Exception when trying to send to a different address on a connected socket "); 1433 } catch (Exception e) { 1434 assertTrue( 1435 "Wrong exception when trying to send to a different address on a connected socket: " 1436 + e.toString(), 1437 (e instanceof IllegalArgumentException)); 1438 } 1439 1440 // validate that we can connect, then disconnect, then connect then 1441 // send/recv 1442 server = null; 1443 ports = Support_PortManager.getNextPortsForUDP(3); 1444 serverPortNumber = ports[0]; 1445 try { 1446 InetAddress localHost = InetAddress.getLocalHost(); 1447 DatagramSocket ds = new DatagramSocket(ports[1]); 1448 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1449 1450 try { 1451 server = new DatagramServer(serverPortNumber, localHost); 1452 server.start(); 1453 Thread.sleep(1000); 1454 } catch (Exception e) { 1455 fail( 1456 "Failed to set up datagram server for native connected Dgram socket test "); 1457 } 1458 1459 int port = ds.getLocalPort(); 1460 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1461 ds.disconnect(); 1462 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1463 1464 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1465 DatagramPacket send = new DatagramPacket(sendBytes, 1466 sendBytes.length); 1467 ds.send(send); 1468 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1469 ds.setSoTimeout(2000); 1470 ds.receive(receive); 1471 ds.close(); 1472 assertTrue( 1473 "connect/disconnect/connect - Wrong size data received: " 1474 + receive.getLength(), 1475 receive.getLength() == sendBytes.length); 1476 assertTrue("connect/disconnect/connect - Wrong data received" 1477 + new String(receive.getData(), 0, receive.getLength()) 1478 + ":" + new String(sendBytes), new String( 1479 receive.getData(), 0, receive.getLength()) 1480 .equals(new String(sendBytes))); 1481 assertTrue("connect/disconnect/connect - Wrong receiver:" 1482 + receive.getAddress() + ":" + localHost, receive 1483 .getAddress().equals(localHost)); 1484 } catch (Exception e) { 1485 fail( 1486 "Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:" 1487 + e.toString()); 1488 } 1489 1490 if (server != null) { 1491 server.stopServer(); 1492 } 1493 1494 // validate that we can connect/disconnect then send/recv to any address 1495 server = null; 1496 ports = Support_PortManager.getNextPortsForUDP(3); 1497 serverPortNumber = ports[0]; 1498 try { 1499 InetAddress localHost = InetAddress.getLocalHost(); 1500 DatagramSocket ds = new DatagramSocket(ports[1]); 1501 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1502 1503 try { 1504 server = new DatagramServer(serverPortNumber, localHost); 1505 server.start(); 1506 Thread.sleep(1000); 1507 } catch (Exception e) { 1508 fail( 1509 "Failed to set up datagram server for native connected Dgram socket test "); 1510 } 1511 1512 int port = ds.getLocalPort(); 1513 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1514 ds.disconnect(); 1515 1516 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1517 DatagramPacket send = new DatagramPacket(sendBytes, 1518 sendBytes.length, localHost, serverPortNumber); 1519 ds.send(send); 1520 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1521 ds.setSoTimeout(2000); 1522 ds.receive(receive); 1523 ds.close(); 1524 assertTrue("connect/disconnect - Wrong size data received: " 1525 + receive.getLength(), 1526 receive.getLength() == sendBytes.length); 1527 assertTrue("connect/disconnect - Wrong data received" 1528 + new String(receive.getData(), 0, receive.getLength()) 1529 + ":" + new String(sendBytes), new String( 1530 receive.getData(), 0, receive.getLength()) 1531 .equals(new String(sendBytes))); 1532 assertTrue("connect/disconnect - Wrong receiver:" 1533 + receive.getAddress() + ":" + localHost, receive 1534 .getAddress().equals(localHost)); 1535 } catch (Exception e) { 1536 fail( 1537 "Unexpected exception when sending data on dgram connected at native level after connect/disconnect:" 1538 + e.toString()); 1539 } 1540 1541 if (server != null) { 1542 server.stopServer(); 1543 } 1544 1545 // validate that we can connect on an allready connected socket and then 1546 // send/recv 1547 server = null; 1548 ports = Support_PortManager.getNextPortsForUDP(3); 1549 serverPortNumber = ports[0]; 1550 try { 1551 InetAddress localHost = InetAddress.getLocalHost(); 1552 DatagramSocket ds = new DatagramSocket(ports[1]); 1553 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1554 1555 try { 1556 server = new DatagramServer(serverPortNumber, localHost); 1557 server.start(); 1558 Thread.sleep(1000); 1559 } catch (Exception e) { 1560 fail( 1561 "Failed to set up datagram server for native connected Dgram socket test "); 1562 } 1563 1564 int port = ds.getLocalPort(); 1565 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1566 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1567 1568 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1569 DatagramPacket send = new DatagramPacket(sendBytes, 1570 sendBytes.length); 1571 ds.send(send); 1572 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1573 ds.setSoTimeout(2000); 1574 ds.receive(receive); 1575 ds.close(); 1576 assertTrue("connect/connect - Wrong size data received: " 1577 + receive.getLength(), 1578 receive.getLength() == sendBytes.length); 1579 assertTrue("connect/connect - Wrong data received" 1580 + new String(receive.getData(), 0, receive.getLength()) 1581 + ":" + new String(sendBytes), new String( 1582 receive.getData(), 0, receive.getLength()) 1583 .equals(new String(sendBytes))); 1584 assertTrue("connect/connect - Wrong receiver:" 1585 + receive.getAddress() + ":" + localHost, receive 1586 .getAddress().equals(localHost)); 1587 } catch (Exception e) { 1588 fail( 1589 "Unexpected exception when sending data on dgram connected at native level after connect/connect: " 1590 + e.toString()); 1591 } 1592 1593 if (server != null) { 1594 server.stopServer(); 1595 } 1596 1597 // test for when we fail to connect at the native level. It seems to 1598 // fail for the any address so we use this. Now to be compatible we 1599 // don't throw the exception but eat it and then act as if we were 1600 // connected at the Java level. 1601 try { 1602 ds = new java.net.DatagramSocket(); 1603 byte[] addressBytes = { 0, 0, 0, 0 }; 1604 InetAddress inetAddress = InetAddress.getByAddress(addressBytes); 1605 int portNumber = Support_PortManager.getNextPortForUDP(); 1606 InetAddress localHost = InetAddress.getLocalHost(); 1607 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1608 assertTrue("Is not connected after connect to inaddr any", ds 1609 .isConnected()); 1610 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1611 DatagramPacket send = new DatagramPacket(sendBytes, 1612 sendBytes.length, localHost, portNumber); 1613 ds.send(send); 1614 fail( 1615 "No exception when trying to connect at native level with bad address (exception from send) "); 1616 } catch (Exception e) { 1617 assertTrue( 1618 "Wrong exception when trying to connect at native level with bad address (exception from send): " 1619 + e.toString(), 1620 (e instanceof IllegalArgumentException)); 1621 } 1622 } 1623 1624 public void test_isBound() { 1625 try { 1626 InetAddress addr = InetAddress.getLocalHost(); 1627 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1628 int port = ports[0]; 1629 1630 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1631 assertTrue("Socket indicated not bound when it should be (1)", 1632 theSocket.isBound()); 1633 theSocket.close(); 1634 1635 theSocket = new DatagramSocket(new InetSocketAddress(addr, port)); 1636 assertTrue("Socket indicated not bound when it should be (2)", 1637 theSocket.isBound()); 1638 theSocket.close(); 1639 1640 theSocket = new DatagramSocket((SocketAddress) null); 1641 assertFalse("Socket indicated bound when it should not be (1)", 1642 theSocket.isBound()); 1643 theSocket.close(); 1644 1645 // connect causes implicit bind 1646 theSocket = new DatagramSocket((SocketAddress) null); 1647 theSocket.connect(new InetSocketAddress(addr, port)); 1648 assertTrue("Socket indicated not bound when it should be (3)", 1649 theSocket.isBound()); 1650 theSocket.close(); 1651 1652 // now test when we bind explicitely 1653 InetSocketAddress theLocalAddress = new InetSocketAddress( 1654 InetAddress.getLocalHost(), ports[2]); 1655 theSocket = new DatagramSocket((SocketAddress) null); 1656 assertFalse("Socket indicated bound when it should not be (2)", 1657 theSocket.isBound()); 1658 theSocket.bind(theLocalAddress); 1659 assertTrue("Socket indicated not bound when it should be (4)", 1660 theSocket.isBound()); 1661 theSocket.close(); 1662 assertTrue("Socket indicated not bound when it should be (5)", 1663 theSocket.isBound()); 1664 } catch (Exception e) { 1665 fail("Got exception during isBound tests" + e.toString()); 1666 } 1667 } 1668 1669 public void test_isConnected() { 1670 try { 1671 InetAddress addr = InetAddress.getLocalHost(); 1672 int[] ports = Support_PortManager.getNextPortsForUDP(4); 1673 int port = ports[0]; 1674 1675 // base test 1676 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1677 assertFalse("Socket indicated connected when it should not be", 1678 theSocket.isConnected()); 1679 theSocket.connect(new InetSocketAddress(addr, port)); 1680 assertTrue("Socket indicated not connected when it should be", 1681 theSocket.isConnected()); 1682 1683 // reconnect the socket and make sure we get the right answer 1684 theSocket.connect(new InetSocketAddress(addr, ports[2])); 1685 assertTrue("Socket indicated not connected when it should be", 1686 theSocket.isConnected()); 1687 1688 // now disconnect the socket and make sure we get the right answer 1689 theSocket.disconnect(); 1690 assertFalse("Socket indicated connected when it should not be", 1691 theSocket.isConnected()); 1692 theSocket.close(); 1693 1694 // now check behavior when socket is closed when connected 1695 theSocket = new DatagramSocket(ports[3]); 1696 theSocket.connect(new InetSocketAddress(addr, port)); 1697 theSocket.close(); 1698 assertTrue("Socket indicated not connected when it should be", 1699 theSocket.isConnected()); 1700 } catch (Exception e) { 1701 fail("Got exception during isConnected tests" + e.toString()); 1702 } 1703 } 1704 1705 public void test_getRemoteSocketAddress() { 1706 try { 1707 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1708 int sport = ports[0]; 1709 int portNumber = ports[1]; 1710 DatagramSocket s = new DatagramSocket(new InetSocketAddress( 1711 InetAddress.getLocalHost(), portNumber)); 1712 s.connect(new InetSocketAddress(InetAddress.getLocalHost(), sport)); 1713 assertTrue("Returned incorrect InetSocketAddress(1):" 1714 + s.getLocalSocketAddress().toString(), s 1715 .getRemoteSocketAddress().equals( 1716 new InetSocketAddress(InetAddress.getLocalHost(), 1717 sport))); 1718 s.close(); 1719 1720 // now create one that is not connected and validate that we get the 1721 // right answer 1722 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1723 portNumber = ports[2]; 1724 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1725 portNumber)); 1726 assertNull( 1727 "Returned incorrect InetSocketAddress -unconnected socket:" 1728 + "Expected: NULL", theSocket 1729 .getRemoteSocketAddress()); 1730 1731 // now connect and validate we get the right answer 1732 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1733 sport)); 1734 assertTrue("Returned incorrect InetSocketAddress(2):" 1735 + theSocket.getRemoteSocketAddress().toString(), theSocket 1736 .getRemoteSocketAddress().equals( 1737 new InetSocketAddress(InetAddress.getLocalHost(), 1738 sport))); 1739 theSocket.close(); 1740 1741 } catch (Exception e) { 1742 fail("Exception during getRemoteSocketAddress test: " + e); 1743 } 1744 } 1745 1746 public void test_setReuseAddressZ() throws Exception { 1747 // test case were we set it to false 1748 DatagramSocket theSocket1 = null; 1749 DatagramSocket theSocket2 = null; 1750 try { 1751 InetSocketAddress theAddress = new InetSocketAddress( 1752 InetAddress.getLocalHost(), Support_PortManager 1753 .getNextPortForUDP()); 1754 theSocket1 = new DatagramSocket((SocketAddress) null); 1755 theSocket2 = new DatagramSocket((SocketAddress) null); 1756 theSocket1.setReuseAddress(false); 1757 theSocket2.setReuseAddress(false); 1758 theSocket1.bind(theAddress); 1759 theSocket2.bind(theAddress); 1760 fail( 1761 "No exception when trying to connect to do duplicate socket bind with re-useaddr set to false"); 1762 } catch (BindException e) { 1763 1764 } 1765 if (theSocket1 != null) 1766 theSocket1.close(); 1767 if (theSocket2 != null) 1768 theSocket2.close(); 1769 1770 // test case were we set it to true 1771 try { 1772 InetSocketAddress theAddress = new InetSocketAddress( 1773 InetAddress.getLocalHost(), Support_PortManager 1774 .getNextPortForUDP()); 1775 theSocket1 = new DatagramSocket((SocketAddress) null); 1776 theSocket2 = new DatagramSocket((SocketAddress) null); 1777 theSocket1.setReuseAddress(true); 1778 theSocket2.setReuseAddress(true); 1779 theSocket1.bind(theAddress); 1780 theSocket2.bind(theAddress); 1781 } catch (Exception e) { 1782 fail( 1783 "unexpected exception when trying to connect to do duplicate socket bind with re-useaddr set to true"); 1784 } 1785 if (theSocket1 != null) 1786 theSocket1.close(); 1787 if (theSocket2 != null) 1788 theSocket2.close(); 1789 1790 // test the default case which we expect to be the same on all 1791 // platforms 1792 try { 1793 InetSocketAddress theAddress = new InetSocketAddress( 1794 InetAddress.getLocalHost(), Support_PortManager 1795 .getNextPortForUDP()); 1796 theSocket1 = new DatagramSocket((SocketAddress) null); 1797 theSocket2 = new DatagramSocket((SocketAddress) null); 1798 theSocket1.bind(theAddress); 1799 theSocket2.bind(theAddress); 1800 fail( 1801 "No exception when trying to connect to do duplicate socket bind with re-useaddr left as default"); 1802 } catch (BindException e) { 1803 1804 } 1805 if (theSocket1 != null) 1806 theSocket1.close(); 1807 if (theSocket2 != null) 1808 theSocket2.close(); 1809 1810 try { 1811 theSocket1.setReuseAddress(true); 1812 fail("SocketException was not thrown."); 1813 } catch(SocketException se) { 1814 //expected 1815 } 1816 } 1817 1818 public void test_getReuseAddress() throws Exception { 1819 DatagramSocket theSocket = new DatagramSocket(); 1820 theSocket.setReuseAddress(true); 1821 assertTrue("getReuseAddress false when it should be true", 1822 theSocket.getReuseAddress()); 1823 theSocket.setReuseAddress(false); 1824 assertFalse("getReuseAddress true when it should be false", 1825 theSocket.getReuseAddress()); 1826 theSocket.close(); 1827 try { 1828 theSocket.getReuseAddress(); 1829 fail("SocketException was not thrown."); 1830 } catch(SocketException se) { 1831 //expected 1832 } 1833 } 1834 1835 public void test_setBroadcastZ() throws Exception { 1836 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1837 DatagramSocket theSocket = new DatagramSocket(ports[0]); 1838 theSocket.setBroadcast(false); 1839 byte theBytes[] = { -1, -1, -1, -1 }; 1840 1841 // validate we cannot connect to the broadcast address when 1842 // setBroadcast is false 1843 try { 1844 theSocket.connect(new InetSocketAddress(InetAddress 1845 .getByAddress(theBytes), ports[1])); 1846 assertFalse( 1847 "No exception when connecting to broadcast address with setBroadcast(false)", 1848 theSocket.getBroadcast()); 1849 } catch (SocketException ex) { 1850 //expected 1851 } 1852 1853 // now validate that we can connect to the broadcast address when 1854 // setBroadcast is true 1855 theSocket.setBroadcast(true); 1856 theSocket.connect(new InetSocketAddress(InetAddress 1857 .getByAddress(theBytes), ports[2])); 1858 1859 theSocket.close(); 1860 try { 1861 theSocket.setBroadcast(false); 1862 fail("SocketException was not thrown."); 1863 } catch(SocketException se) { 1864 //expected 1865 } 1866 } 1867 1868 public void test_getBroadcast() throws Exception { 1869 DatagramSocket theSocket = new DatagramSocket(); 1870 theSocket.setBroadcast(true); 1871 assertTrue("getBroadcast false when it should be true", theSocket 1872 .getBroadcast()); 1873 theSocket.setBroadcast(false); 1874 assertFalse("getBroadcast true when it should be False", theSocket 1875 .getBroadcast()); 1876 theSocket.close(); 1877 try { 1878 theSocket.getBroadcast(); 1879 fail("SocketException was not thrown."); 1880 } catch(SocketException se) { 1881 //expected 1882 } 1883 } 1884 1885 public void test_setTrafficClassI() throws Exception { 1886 int IPTOS_LOWCOST = 0x2; 1887 int IPTOS_RELIABILTY = 0x4; 1888 int IPTOS_THROUGHPUT = 0x8; 1889 int IPTOS_LOWDELAY = 0x10; 1890 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1891 1892 new InetSocketAddress(InetAddress.getLocalHost(), 1893 ports[0]); 1894 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1895 1896 // validate that value set must be between 0 and 255 1897 try { 1898 theSocket.setTrafficClass(256); 1899 fail("No exception when traffic class set to 256"); 1900 } catch (IllegalArgumentException e) { 1901 } 1902 1903 try { 1904 theSocket.setTrafficClass(-1); 1905 fail("No exception when traffic class set to -1"); 1906 } catch (IllegalArgumentException e) { 1907 } 1908 1909 // now validate that we can set it to some good values 1910 theSocket.setTrafficClass(IPTOS_LOWCOST); 1911 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 1912 1913 theSocket.close(); 1914 try { 1915 theSocket.setTrafficClass(1); 1916 fail("SocketException was not thrown."); 1917 } catch(SocketException se) { 1918 //expected 1919 } 1920 } 1921 1922 public void test_getTrafficClass() throws Exception { 1923 int IPTOS_LOWCOST = 0x2; 1924 int IPTOS_RELIABILTY = 0x4; 1925 int IPTOS_THROUGHPUT = 0x8; 1926 int IPTOS_LOWDELAY = 0x10; 1927 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1928 1929 new InetSocketAddress(InetAddress.getLocalHost(), 1930 ports[0]); 1931 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1932 1933 /* 1934 * we cannot actually check that the values are set as if a platform 1935 * does not support the option then it may come back unset even 1936 * though we set it so just get the value to make sure we can get it 1937 */ 1938 int trafficClass = theSocket.getTrafficClass(); 1939 1940 theSocket.close(); 1941 try { 1942 theSocket.getTrafficClass(); 1943 fail("SocketException was not thrown."); 1944 } catch(SocketException se) { 1945 //expected 1946 } 1947 } 1948 1949 public void test_isClosed() { 1950 try { 1951 DatagramSocket theSocket = new DatagramSocket(); 1952 1953 // validate isClosed returns expected values 1954 assertFalse("Socket should indicate it is not closed(1):", 1955 theSocket.isClosed()); 1956 theSocket.close(); 1957 assertTrue("Socket should indicate it is not closed(1):", theSocket 1958 .isClosed()); 1959 1960 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1961 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 1962 theSocket = new DatagramSocket(theAddress); 1963 assertFalse("Socket should indicate it is not closed(2):", 1964 theSocket.isClosed()); 1965 theSocket.close(); 1966 assertTrue("Socket should indicate it is not closed(2):", theSocket 1967 .isClosed()); 1968 } catch (Exception e) { 1969 fail("Got exception during isClosed tests" + e.toString()); 1970 } 1971 } 1972 1973 public void test_getChannel() throws Exception { 1974 assertNull(new DatagramSocket().getChannel()); 1975 1976 int portNumber = Support_PortManager.getNextPortForUDP(); 1977 DatagramSocket ds = null; 1978 try { 1979 InetAddress ia = InetAddress 1980 .getByName(Support_Configuration.IPv6GlobalAddressJcl4); 1981 ds = new DatagramSocket(); 1982 assertNull(ds.getChannel()); 1983 ds.connect(ia, portNumber); 1984 assertNull(ds.getChannel()); 1985 } catch (SocketException e) { 1986 fail("SocketException was thrown."); 1987 } finally { 1988 ds.disconnect(); 1989 ds.close(); 1990 } 1991 portNumber = Support_PortManager.getNextPortForUDP(); 1992 SocketAddress address = new InetSocketAddress(portNumber); 1993 DatagramChannel channel = DatagramChannel.open(); 1994 DatagramSocket socket = channel.socket(); 1995 assertEquals(channel, socket.getChannel()); 1996 socket.close(); 1997 } 1998 1999 class TestDatagramSocketImplFactory implements DatagramSocketImplFactory { 2000 public DatagramSocketImpl createDatagramSocketImpl() { 2001 return new TestDatagramSocketImpl(); 2002 } 2003 } 2004 2005 class TestDatagramSocketImpl extends DatagramSocketImpl { 2006 2007 @Override 2008 protected void bind(int arg0, InetAddress arg1) throws SocketException { 2009 // TODO Auto-generated method stub 2010 2011 } 2012 2013 @Override 2014 protected void close() { 2015 // TODO Auto-generated method stub 2016 2017 } 2018 2019 @Override 2020 protected void create() throws SocketException { 2021 // TODO Auto-generated method stub 2022 2023 } 2024 2025 @Override 2026 protected byte getTTL() throws IOException { 2027 // TODO Auto-generated method stub 2028 return 0; 2029 } 2030 2031 @Override 2032 protected int getTimeToLive() throws IOException { 2033 // TODO Auto-generated method stub 2034 return 0; 2035 } 2036 2037 @Override 2038 protected void join(InetAddress arg0) throws IOException { 2039 // TODO Auto-generated method stub 2040 2041 } 2042 2043 @Override 2044 protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException { 2045 // TODO Auto-generated method stub 2046 2047 } 2048 2049 @Override 2050 protected void leave(InetAddress arg0) throws IOException { 2051 // TODO Auto-generated method stub 2052 2053 } 2054 2055 @Override 2056 protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException { 2057 // TODO Auto-generated method stub 2058 2059 } 2060 2061 @Override 2062 protected int peek(InetAddress arg0) throws IOException { 2063 // TODO Auto-generated method stub 2064 return 0; 2065 } 2066 2067 @Override 2068 protected int peekData(DatagramPacket arg0) throws IOException { 2069 // TODO Auto-generated method stub 2070 return 0; 2071 } 2072 2073 @Override 2074 protected void receive(DatagramPacket arg0) throws IOException { 2075 // TODO Auto-generated method stub 2076 2077 } 2078 2079 @Override 2080 protected void send(DatagramPacket arg0) throws IOException { 2081 // TODO Auto-generated method stub 2082 2083 } 2084 2085 @Override 2086 protected void setTTL(byte arg0) throws IOException { 2087 // TODO Auto-generated method stub 2088 2089 } 2090 2091 @Override 2092 protected void setTimeToLive(int arg0) throws IOException { 2093 // TODO Auto-generated method stub 2094 2095 } 2096 2097 public Object getOption(int arg0) throws SocketException { 2098 // TODO Auto-generated method stub 2099 return null; 2100 } 2101 2102 public void setOption(int arg0, Object arg1) throws SocketException { 2103 // TODO Auto-generated method stub 2104 2105 } 2106 2107 } 2108 2109 /** 2110 * Sets up the fixture, for example, open a network connection. This method 2111 * is called before a test is executed. 2112 */ 2113 protected void setUp() { 2114 retval = "Bogus retval"; 2115 } 2116 2117 /** 2118 * Tears down the fixture, for example, close a network connection. This 2119 * method is called after a test is executed. 2120 */ 2121 protected void tearDown() { 2122 try { 2123 ds.close(); 2124 sds.close(); 2125 } catch (Exception e) { 2126 } 2127 } 2128 2129 protected void receive_oversize_java_net_DatagramPacket() throws Exception { 2130 final int[] ports = Support_PortManager.getNextPortsForUDP(2); 2131 final int portNumber = ports[0]; 2132 2133 class TestDGRcvOver implements Runnable { 2134 public void run() { 2135 InetAddress localHost = null; 2136 try { 2137 localHost = InetAddress.getLocalHost(); 2138 Thread.sleep(1000); 2139 DatagramSocket sds = new DatagramSocket(ports[1]); 2140 DatagramPacket rdp = new DatagramPacket("0123456789" 2141 .getBytes(), 10, localHost, portNumber); 2142 sds.send(rdp); 2143 sds.close(); 2144 } catch (Exception e) { 2145 throw new RuntimeException(e); 2146 } 2147 } 2148 } 2149 2150 try { 2151 new Thread(new TestDGRcvOver(), "DGSenderOver").start(); 2152 ds = new java.net.DatagramSocket(portNumber); 2153 ds.setSoTimeout(6000); 2154 byte rbuf[] = new byte[5]; 2155 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 2156 ; 2157 ds.receive(rdp); 2158 ds.close(); 2159 assertTrue("Send/Receive oversize failed to return correct data: " 2160 + new String(rbuf, 0, 5), new String(rbuf, 0, 5) 2161 .equals("01234")); 2162 } finally { 2163 ds.close(); 2164 } 2165 } 2166} 2167