OldDatagramSocketTest.java revision 23ec09188303a874b3b391f96ae0a29af002bff9
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_getLocalAddress() { 716 // Test for method java.net.InetAddress 717 // java.net.DatagramSocket.getLocalAddress() 718 InetAddress local = null; 719 try { 720 int portNumber = Support_PortManager.getNextPortForUDP(); 721 local = InetAddress.getLocalHost(); 722 ds = new java.net.DatagramSocket(portNumber, local); 723 assertTrue("Returned incorrect address. Got:" 724 + ds.getLocalAddress() 725 + " wanted: " 726 + InetAddress.getByName(InetAddress.getLocalHost() 727 .getHostName()), InetAddress.getByName( 728 InetAddress.getLocalHost().getHostName()).equals( 729 ds.getLocalAddress())); 730 731 // now check behavior when the ANY address is returned 732 DatagramSocket s = new DatagramSocket(0); 733 assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), 734 s.getLocalAddress() instanceof Inet6Address); 735 s.close(); 736 } catch (Exception e) { 737 fail( 738 "Exception during getLocalAddress: " + local + " - " + e); 739 } 740 } 741 742 public void test_getLocalPort() { 743 // Test for method int java.net.DatagramSocket.getLocalPort() 744 try { 745 int portNumber = Support_PortManager.getNextPortForUDP(); 746 ds = new java.net.DatagramSocket(portNumber); 747 assertTrue("Returned incorrect port", 748 ds.getLocalPort() == portNumber); 749 } catch (Exception e) { 750 fail("Exception during getLocalAddress : " + e.getMessage()); 751 } 752 } 753 754 public void test_getPort() { 755 try { 756 int portNumber = Support_PortManager.getNextPortForUDP(); 757 DatagramSocket theSocket = new DatagramSocket(portNumber); 758 assertEquals("Expected -1 for remote port as not connected", 759 -1, theSocket.getPort()); 760 761 // now connect the socket and validate that we get the right port 762 theSocket.connect(InetAddress.getLocalHost(), portNumber); 763 assertTrue("getPort returned wrong value:" + theSocket.getPort() 764 + ":Expected:" + portNumber, 765 theSocket.getPort() == portNumber); 766 } catch (Exception e) { 767 fail("unexpected exception during getPort test : " + e.getMessage()); 768 } 769 } 770 771 public void test_getReceiveBufferSize() throws Exception { 772 int portNumber = Support_PortManager.getNextPortForUDP(); 773 ds = new java.net.DatagramSocket(portNumber); 774 ds.setReceiveBufferSize(130); 775 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 776 ds.close(); 777 try { 778 ds.getReceiveBufferSize(); 779 fail("SocketException was not thrown."); 780 } catch(SocketException se) { 781 //expected 782 } 783 } 784 785 public void test_getSendBufferSize() throws Exception { 786 int portNumber = Support_PortManager.getNextPortForUDP(); 787 ds = new java.net.DatagramSocket(portNumber); 788 ds.setSendBufferSize(134); 789 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 790 ds.close(); 791 try { 792 ds.getSendBufferSize(); 793 fail("SocketException was not thrown."); 794 } catch(SocketException se) { 795 //expected 796 } 797 } 798 799 public void test_getSoTimeout() throws Exception { 800 // Test for method int java.net.DatagramSocket.getSoTimeout() 801 int portNumber = Support_PortManager.getNextPortForUDP(); 802 ds = new java.net.DatagramSocket(portNumber); 803 ds.setSoTimeout(100); 804 assertEquals("Returned incorrect timeout", 100, ds.getSoTimeout()); 805 ds.close(); 806 try { 807 ds.getSoTimeout(); 808 fail("SocketException was not thrown."); 809 } catch(SocketException se) { 810 //expected 811 } 812 } 813 814 public void test_receiveLjava_net_DatagramPacket() throws Exception { 815 // Test for method void 816 // java.net.DatagramSocket.receive(java.net.DatagramPacket) 817 818 receive_oversize_java_net_DatagramPacket(); 819 final int[] ports = Support_PortManager.getNextPortsForUDP(2); 820 final int portNumber = ports[0]; 821 822 class TestDGRcv implements Runnable { 823 public void run() { 824 InetAddress localHost = null; 825 try { 826 localHost = InetAddress.getLocalHost(); 827 Thread.sleep(1000); 828 DatagramSocket sds = new DatagramSocket(ports[1]); 829 DatagramPacket rdp = new DatagramPacket("Test String" 830 .getBytes(), 11, localHost, portNumber); 831 sds.send(rdp); 832 sds.close(); 833 } catch (Exception e) { 834 throw new RuntimeException(e); 835 } 836 } 837 } 838 839 try { 840 new Thread(new TestDGRcv(), "DGSender").start(); 841 ds = new java.net.DatagramSocket(portNumber); 842 ds.setSoTimeout(6000); 843 byte rbuf[] = new byte[1000]; 844 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 845 ds.receive(rdp); 846 ds.close(); 847 assertTrue("Send/Receive failed to return correct data: " 848 + new String(rbuf, 0, 11), new String(rbuf, 0, 11) 849 .equals("Test String")); 850 } finally { 851 ds.close(); 852 } 853 DatagramSocket socket = null; 854 try { 855 byte rbuf[] = new byte[1000]; 856 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 857 SocketAddress address = new InetSocketAddress(portNumber); 858 DatagramChannel channel = DatagramChannel.open(); 859 channel.configureBlocking(false); 860 socket = channel.socket(); 861 socket.receive(rdp); 862 fail("IllegalBlockingModeException was not thrown."); 863 } catch(IllegalBlockingModeException ibme) { 864 //expected 865 } finally { 866 socket.close(); 867 } 868 869 try { 870 ds = new java.net.DatagramSocket(portNumber); 871 ds.setSoTimeout(1000); 872 byte rbuf[] = new byte[1000]; 873 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 874 ds.receive(rdp); 875 fail("SocketTimeoutException was not thrown."); 876 } catch(SocketTimeoutException te) { 877 //expected 878 } finally { 879 ds.close(); 880 } 881 882 883 884 try { 885 interrupted = false; 886 final DatagramSocket ds = new DatagramSocket(); 887 ds.setSoTimeout(12000); 888 Runnable runnable = new Runnable() { 889 public void run() { 890 try { 891 ds.receive(new DatagramPacket(new byte[1], 1)); 892 } catch (InterruptedIOException e) { 893 interrupted = true; 894 } catch (IOException e) { 895 throw new RuntimeException(e); 896 } 897 } 898 }; 899 Thread thread = new Thread(runnable, "DatagramSocket.receive1"); 900 thread.start(); 901 try { 902 do { 903 Thread.sleep(500); 904 } while (!thread.isAlive()); 905 } catch (InterruptedException e) { 906 } 907 ds.close(); 908 int c = 0; 909 do { 910 try { 911 Thread.sleep(500); 912 } catch (InterruptedException e) { 913 } 914 if (interrupted) { 915 fail("received interrupt"); 916 } 917 if (++c > 4) { 918 fail("read call did not exit"); 919 } 920 } while (thread.isAlive()); 921 922 interrupted = false; 923 int[] ports1 = Support_PortManager.getNextPortsForUDP(2); 924 final int portNum = ports[0]; 925 final DatagramSocket ds2 = new DatagramSocket(ports[1]); 926 ds2.setSoTimeout(12000); 927 Runnable runnable2 = new Runnable() { 928 public void run() { 929 try { 930 ds2.receive(new DatagramPacket(new byte[1], 1, 931 InetAddress.getLocalHost(), portNum)); 932 } catch (InterruptedIOException e) { 933 interrupted = true; 934 } catch (IOException e) { 935 } 936 } 937 }; 938 Thread thread2 = new Thread(runnable2, "DatagramSocket.receive2"); 939 thread2.start(); 940 try { 941 do { 942 Thread.sleep(500); 943 } while (!thread2.isAlive()); 944 } catch (InterruptedException e) { 945 } 946 ds2.close(); 947 int c2 = 0; 948 do { 949 try { 950 Thread.sleep(500); 951 } catch (InterruptedException e) { 952 } 953 if (interrupted) { 954 fail("receive2 was interrupted"); 955 } 956 if (++c2 > 4) { 957 fail("read2 call did not exit"); 958 } 959 } while (thread2.isAlive()); 960 961 interrupted = false; 962 DatagramSocket ds3 = new DatagramSocket(); 963 ds3.setSoTimeout(500); 964 Date start = new Date(); 965 try { 966 ds3.receive(new DatagramPacket(new byte[1], 1)); 967 } catch (InterruptedIOException e) { 968 interrupted = true; 969 } 970 ds3.close(); 971 assertTrue("receive not interrupted", interrupted); 972 int delay = (int) (new Date().getTime() - start.getTime()); 973 assertTrue("timeout too soon: " + delay, delay >= 490); 974 } catch (IOException e) { 975 fail("Unexpected IOException : " + e.getMessage()); 976 } 977 978 979 } 980 981 public void test_sendLjava_net_DatagramPacket() throws Exception { 982 // Test for method void 983 // java.net.DatagramSocket.send(java.net.DatagramPacket) 984 int[] ports = Support_PortManager.getNextPortsForUDP(2); 985 final int portNumber = ports[0]; 986 987 class TestDGSend implements Runnable { 988 Thread pThread; 989 990 public TestDGSend(Thread t) { 991 pThread = t; 992 } 993 994 public void run() { 995 try { 996 byte[] rbuf = new byte[1000]; 997 998 sds = new DatagramSocket(portNumber); 999 DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length); 1000 sds.setSoTimeout(6000); 1001 sds.receive(sdp); 1002 retval = new String(rbuf, 0, testString.length()); 1003 pThread.interrupt(); 1004 } catch (java.io.InterruptedIOException e) { 1005 System.out.println("Recv operation timed out"); 1006 pThread.interrupt(); 1007 ds.close(); 1008 return; 1009 } catch (Exception e) { 1010 System.out 1011 .println("Failed to establish Dgram server: " + e); 1012 } 1013 } 1014 } 1015 try { 1016 new Thread(new TestDGSend(Thread.currentThread()), "DGServer") 1017 .start(); 1018 ds = new java.net.DatagramSocket(ports[1]); 1019 dp = new DatagramPacket(testString.getBytes(), testString.length(), 1020 InetAddress.getLocalHost(), portNumber); 1021 // Wait to allow send to occur 1022 try { 1023 Thread.sleep(500); 1024 ds.send(dp); 1025 Thread.sleep(5000); 1026 } catch (InterruptedException e) { 1027 ds.close(); 1028 assertTrue("Incorrect data sent: " + retval, retval 1029 .equals(testString)); 1030 } 1031 } catch (Exception e) { 1032 fail("Exception during send test : " + e.getMessage()); 1033 } finally { 1034 ds.close(); 1035 } 1036 1037 /* 1038 SecurityManager sm = new SecurityManager() { 1039 1040 public void checkPermission(Permission perm) { 1041 } 1042 1043 public void checkMulticast(InetAddress maddr) { 1044 throw new SecurityException(); 1045 } 1046 1047 public void checkConnect(String host, 1048 int port) { 1049 throw new SecurityException(); 1050 } 1051 }; 1052 try { 1053 1054 ds = new java.net.DatagramSocket(ports[1]); 1055 dp = new DatagramPacket(testString.getBytes(), testString.length(), 1056 InetAddress.getLocalHost(), portNumber); 1057 1058 SecurityManager oldSm = System.getSecurityManager(); 1059 System.setSecurityManager(sm); 1060 try { 1061 ds.send(dp); 1062 fail("SecurityException should be thrown."); 1063 } catch (SecurityException e) { 1064 // expected 1065 } catch (SocketException e) { 1066 fail("SocketException was thrown."); 1067 } finally { 1068 System.setSecurityManager(oldSm); 1069 } 1070 } catch(Exception e) { 1071 fail("Unexpected exception was thrown: " + e.getMessage()); 1072 } 1073 */ 1074 1075 DatagramSocket socket = null; 1076 try { 1077 byte rbuf[] = new byte[1000]; 1078 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 1079 SocketAddress address = new InetSocketAddress(portNumber); 1080 DatagramChannel channel = DatagramChannel.open(); 1081 channel.configureBlocking(false); 1082 socket = channel.socket(); 1083 socket.send(rdp); 1084 fail("IllegalBlockingModeException was not thrown."); 1085 } catch(IllegalBlockingModeException ibme) { 1086 //expected 1087 } catch(IOException ioe) { 1088 fail("IOException was thrown: " + ioe.getMessage()); 1089 } finally { 1090 socket.close(); 1091 } 1092 1093 //Regression for HARMONY-1118 1094 class testDatagramSocket extends DatagramSocket { 1095 public testDatagramSocket(DatagramSocketImpl impl){ 1096 super(impl); 1097 } 1098 } 1099 class testDatagramSocketImpl extends DatagramSocketImpl { 1100 protected void create() throws SocketException {} 1101 protected void bind(int arg0, InetAddress arg1) throws SocketException {} 1102 protected void send(DatagramPacket arg0) throws IOException {} 1103 protected int peek(InetAddress arg0) throws IOException { 1104 return 0; 1105 } 1106 protected int peekData(DatagramPacket arg0) throws IOException { 1107 return 0; 1108 } 1109 protected void receive(DatagramPacket arg0) throws IOException {} 1110 protected void setTTL(byte arg0) throws IOException {} 1111 protected byte getTTL() throws IOException { 1112 return 0; 1113 } 1114 protected void setTimeToLive(int arg0) throws IOException {} 1115 protected int getTimeToLive() throws IOException { 1116 return 0; 1117 } 1118 protected void join(InetAddress arg0) throws IOException {} 1119 protected void leave(InetAddress arg0) throws IOException {} 1120 protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {} 1121 protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {} 1122 protected void close() {} 1123 public void setOption(int arg0, Object arg1) throws SocketException {} 1124 public Object getOption(int arg0) throws SocketException { 1125 return null; 1126 } 1127 } 1128 InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0); 1129 //no exception expected for next line 1130 new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa)); 1131 1132 // Regression test for Harmony-2938 1133 InetAddress i = InetAddress.getByName("127.0.0.1"); 1134 DatagramSocket d = new DatagramSocket(0, i); 1135 try { 1136 d.send(new DatagramPacket(new byte[] { 1 }, 1)); 1137 fail("should throw NPE."); 1138 } catch (NullPointerException e) { 1139 // expected; 1140 } finally { 1141 d.close(); 1142 } 1143 } 1144 1145 public void test_setSendBufferSizeI() throws Exception { 1146 int portNumber = Support_PortManager.getNextPortForUDP(); 1147 ds = new java.net.DatagramSocket(portNumber); 1148 ds.setSendBufferSize(134); 1149 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 1150 ds.close(); 1151 try { 1152 ds.setSendBufferSize(1); 1153 fail("SocketException was not thrown."); 1154 } catch(SocketException se) { 1155 //expected 1156 } 1157 } 1158 1159 public void test_setReceiveBufferSizeI() throws Exception { 1160 int portNumber = Support_PortManager.getNextPortForUDP(); 1161 ds = new java.net.DatagramSocket(portNumber); 1162 ds.setReceiveBufferSize(130); 1163 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 1164 1165 try { 1166 ds.setReceiveBufferSize(0); 1167 fail("IllegalArgumentException was not thrown."); 1168 } catch(IllegalArgumentException iae) { 1169 //expected 1170 } 1171 1172 try { 1173 ds.setReceiveBufferSize(-1); 1174 fail("IllegalArgumentException was not thrown."); 1175 } catch(IllegalArgumentException iae) { 1176 //expected 1177 } 1178 1179 ds.close(); 1180 1181 try { 1182 ds.setReceiveBufferSize(1); 1183 fail("SocketException was not thrown."); 1184 } catch (SocketException e) { 1185 //expected 1186 } 1187 } 1188 1189 public void test_setSoTimeoutI() throws Exception { 1190 // Test for method void java.net.DatagramSocket.setSoTimeout(int) 1191 int portNumber = Support_PortManager.getNextPortForUDP(); 1192 ds = new java.net.DatagramSocket(portNumber); 1193 ds.setSoTimeout(5000); 1194 assertTrue("Set incorrect timeout", ds.getSoTimeout() >= 5000); 1195 ds.close(); 1196 try { 1197 ds.setSoTimeout(100); 1198 fail("SocketException was not thrown."); 1199 } catch(SocketException se) { 1200 //expected 1201 } 1202 } 1203 1204 public void test_ConstructorLjava_net_DatagramSocketImpl() { 1205 class testDatagramSocket extends DatagramSocket { 1206 public testDatagramSocket(DatagramSocketImpl impl){ 1207 super(impl); 1208 } 1209 } 1210 1211 try { 1212 new testDatagramSocket((DatagramSocketImpl) null); 1213 fail("exception expected"); 1214 } catch (NullPointerException ex) { 1215 //expected 1216 } 1217 } 1218 1219 public void test_ConstructorLjava_net_SocketAddress() { 1220 class mySocketAddress extends SocketAddress { 1221 1222 public mySocketAddress() { 1223 } 1224 } 1225 1226 try { 1227 try { 1228 int portNumber = Support_PortManager.getNextPortForUDP(); 1229 ds = new java.net.DatagramSocket(new InetSocketAddress( 1230 InetAddress.getLocalHost(), portNumber)); 1231 assertTrue(ds.getBroadcast()); 1232 assertTrue("Created socket with incorrect port", ds 1233 .getLocalPort() == portNumber); 1234 assertTrue("Created socket with incorrect address", ds 1235 .getLocalAddress().equals(InetAddress.getLocalHost())); 1236 } catch (Exception e) { 1237 fail("Could not create DatagramSocket : " + e.getMessage()); 1238 } 1239 1240 try { 1241 int portNumber = Support_PortManager.getNextPortForUDP(); 1242 ds = new java.net.DatagramSocket(new mySocketAddress()); 1243 fail( 1244 "No exception when constucting datagramSocket with unsupported SocketAddress type"); 1245 } catch (IllegalArgumentException e) { 1246 1247 } 1248 //regression for Harmony-894 1249 ds = new DatagramSocket((SocketAddress)null); 1250 assertTrue(ds.getBroadcast()); 1251 } catch (Exception ex) { 1252 fail( 1253 "unexpected exception when datagramSocket SocketAddress constructor test"); 1254 } 1255 1256 /* 1257 SecurityManager sm = new SecurityManager() { 1258 1259 public void checkPermission(Permission perm) { 1260 } 1261 1262 public void checkListen(int port) { 1263 throw new SecurityException(); 1264 } 1265 }; 1266 1267 SecurityManager oldSm = System.getSecurityManager(); 1268 System.setSecurityManager(sm); 1269 try { 1270 new DatagramSocket(new InetSocketAddress( 1271 InetAddress.getLocalHost(), 1)); 1272 fail("SecurityException should be thrown."); 1273 } catch (SecurityException e) { 1274 // expected 1275 } catch (SocketException e) { 1276 fail("SocketException was thrown."); 1277 } catch (UnknownHostException e) { 1278 fail("UnknownHostException was thrown."); 1279 } finally { 1280 System.setSecurityManager(oldSm); 1281 } 1282 */ 1283 1284 InetSocketAddress isa = null; 1285 try { 1286 isa = new InetSocketAddress( 1287 InetAddress.getLocalHost(), 1); 1288 } catch (UnknownHostException e) { 1289 fail("UnknownHostException was thrown."); 1290 } 1291 } 1292 1293 public void test_bindLjava_net_SocketAddress() throws Exception { 1294 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1295 int serverPortNumber = ports[1]; 1296 1297 // now create a socket that is not bound and then bind it 1298 InetAddress localHost = InetAddress.getLocalHost(); 1299 InetSocketAddress localAddress1 = new InetSocketAddress(localHost, ports[0]); 1300 DatagramSocket theSocket = new DatagramSocket(localAddress1); 1301 1302 // validate that the localSocketAddress reflects the address we bound to 1303 assertEquals(localAddress1, theSocket.getLocalSocketAddress()); 1304 1305 // now make sure that datagrams sent from this socket appear to come 1306 // from the address we bound to 1307 InetSocketAddress localAddress2 = new InetSocketAddress(localHost, ports[2]); 1308 DatagramSocket ds = new DatagramSocket((SocketAddress) null); 1309 ds.bind(localAddress2); 1310 1311 DatagramServer server = new DatagramServer(serverPortNumber, localHost); 1312 server.start(); 1313 Thread.sleep(1000); 1314 1315 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1316 1317 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1318 DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length); 1319 ds.send(send); 1320 Thread.sleep(1000); 1321 ds.close(); 1322 // Check that the address in the packet matches the bound address. 1323 assertEquals(localAddress2, server.rdp.getSocketAddress()); 1324 1325 if (server != null) { 1326 server.stopServer(); 1327 } 1328 } 1329 1330 public void test_bindLjava_net_SocketAddress_null() throws Exception { 1331 // validate if we pass in null that it picks an address for us. 1332 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1333 theSocket.bind(null); 1334 assertNotNull(theSocket.getLocalSocketAddress()); 1335 theSocket.close(); 1336 } 1337 1338 public void test_bindLjava_net_SocketAddress_bad_address() throws Exception { 1339 // Address we cannot bind to 1340 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1341 try { 1342 InetAddress badAddress = InetAddress.getByAddress(Support_Configuration.nonLocalAddressBytes); 1343 theSocket.bind(new InetSocketAddress(badAddress, Support_PortManager.getNextPortForUDP())); 1344 fail("No exception when binding to bad address"); 1345 } catch (SocketException expected) { 1346 } 1347 theSocket.close(); 1348 } 1349 1350 public void test_bindLjava_net_SocketAddress_address_in_use() throws Exception { 1351 // Address that we have already bound to 1352 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1353 DatagramSocket theSocket1 = new DatagramSocket((SocketAddress) null); 1354 DatagramSocket theSocket2 = new DatagramSocket(ports[0]); 1355 try { 1356 InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), ports[1]); 1357 theSocket1.bind(theAddress); 1358 theSocket2.bind(theAddress); 1359 fail("No exception binding to address that is not available"); 1360 } catch (SocketException expected) { 1361 } 1362 theSocket1.close(); 1363 theSocket2.close(); 1364 } 1365 1366 public void test_bindLjava_net_SocketAddress_unsupported_address_type() throws Exception { 1367 class mySocketAddress extends SocketAddress { 1368 public mySocketAddress() { 1369 } 1370 } 1371 1372 // unsupported SocketAddress subclass 1373 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1374 try { 1375 theSocket.bind(new mySocketAddress()); 1376 fail("No exception when binding using unsupported SocketAddress subclass"); 1377 } catch (IllegalArgumentException expected) { 1378 } 1379 theSocket.close(); 1380 } 1381 1382 public void test_connectLjava_net_SocketAddress() { 1383 1384 // validate that we get the PortUnreachable exception if we try to 1385 // send a dgram to a server that is not running and then do a recv 1386 try { 1387 ds = new java.net.DatagramSocket(); 1388 InetAddress inetAddress = InetAddress.getLocalHost(); 1389 int portNumber = Support_PortManager.getNextPortForUDP(); 1390 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1391 DatagramPacket send = new DatagramPacket(new byte[10], 10); 1392 ds.send(send); 1393 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1394 ds.setSoTimeout(10000); 1395 ds.receive(receive); 1396 ds.close(); 1397 fail( 1398 "No PortUnreachableException when connected at native level on recv "); 1399 } catch (Exception e) { 1400 assertTrue( 1401 "Wrong exception when trying to connect at native level on recv: " 1402 + e.toString(), 1403 (e instanceof PortUnreachableException)); 1404 } 1405 1406 try { 1407 ds = new java.net.DatagramSocket(); 1408 InetAddress inetAddress = InetAddress.getLocalHost(); 1409 int portNumber = Support_PortManager.getNextPortForUDP(); 1410 1411 ds.connect(new InetSocketAddress("asdfasdf", 1)); 1412 ds.close(); 1413 fail("SocketException was not thrown."); 1414 } catch (Exception e) { 1415 assertTrue( 1416 "Wrong exception when trying to connect to unknown host: " 1417 + e.toString(), 1418 (e instanceof SocketException)); 1419 } 1420 1421 // validate that we can send/receive with datagram sockets connected at 1422 // the native level 1423 DatagramServer server = null; 1424 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1425 int serverPortNumber = ports[0]; 1426 try { 1427 InetAddress localHost = InetAddress.getLocalHost(); 1428 DatagramSocket ds = new DatagramSocket(ports[1]); 1429 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1430 1431 try { 1432 server = new DatagramServer(serverPortNumber, localHost); 1433 server.start(); 1434 Thread.sleep(1000); 1435 } catch (Exception e) { 1436 fail( 1437 "Failed to set up datagram server for native connected Dgram socket test "); 1438 } 1439 1440 int port = ds.getLocalPort(); 1441 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1442 1443 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1444 DatagramPacket send = new DatagramPacket(sendBytes, 1445 sendBytes.length); 1446 ds.send(send); 1447 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1448 ds.setSoTimeout(2000); 1449 ds.receive(receive); 1450 ds.close(); 1451 assertTrue("Wrong size data received: " + receive.getLength(), 1452 receive.getLength() == sendBytes.length); 1453 assertTrue("Wrong data received" 1454 + new String(receive.getData(), 0, receive.getLength()) 1455 + ":" + new String(sendBytes), new String( 1456 receive.getData(), 0, receive.getLength()) 1457 .equals(new String(sendBytes))); 1458 assertTrue("Wrong receiver:" + receive.getAddress() + ":" 1459 + localHost, receive.getAddress().equals(localHost)); 1460 } catch (Exception e) { 1461 fail( 1462 "Unexpected exception when sending data on dgram connected at native level:" 1463 + e.toString()); 1464 } 1465 1466 if (server != null) { 1467 server.stopServer(); 1468 } 1469 1470 // validate that we can disconnect 1471 try { 1472 ds = new java.net.DatagramSocket(); 1473 InetAddress inetAddress = InetAddress.getLocalHost(); 1474 int portNumber = Support_PortManager.getNextPortForUDP(); 1475 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1476 ds.disconnect(); 1477 ds.close(); 1478 } catch (Exception e) { 1479 assertTrue("Unexpected exception when trying to connect at native" 1480 + e.toString(), (e instanceof PortUnreachableException)); 1481 } 1482 1483 // validate that once connected we cannot send to another address 1484 try { 1485 ds = new java.net.DatagramSocket(); 1486 InetAddress inetAddress = InetAddress.getLocalHost(); 1487 int portNumber = Support_PortManager.getNextPortForUDP(); 1488 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1489 DatagramPacket send = new DatagramPacket(new byte[10], 10, 1490 inetAddress, portNumber + 1); 1491 ds.send(send); 1492 ds.close(); 1493 fail( 1494 "No Exception when trying to send to a different address on a connected socket "); 1495 } catch (Exception e) { 1496 assertTrue( 1497 "Wrong exception when trying to send to a different address on a connected socket: " 1498 + e.toString(), 1499 (e instanceof IllegalArgumentException)); 1500 } 1501 1502 // validate that we can connect, then disconnect, then connect then 1503 // send/recv 1504 server = null; 1505 ports = Support_PortManager.getNextPortsForUDP(3); 1506 serverPortNumber = ports[0]; 1507 try { 1508 InetAddress localHost = InetAddress.getLocalHost(); 1509 DatagramSocket ds = new DatagramSocket(ports[1]); 1510 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1511 1512 try { 1513 server = new DatagramServer(serverPortNumber, localHost); 1514 server.start(); 1515 Thread.sleep(1000); 1516 } catch (Exception e) { 1517 fail( 1518 "Failed to set up datagram server for native connected Dgram socket test "); 1519 } 1520 1521 int port = ds.getLocalPort(); 1522 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1523 ds.disconnect(); 1524 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1525 1526 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1527 DatagramPacket send = new DatagramPacket(sendBytes, 1528 sendBytes.length); 1529 ds.send(send); 1530 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1531 ds.setSoTimeout(2000); 1532 ds.receive(receive); 1533 ds.close(); 1534 assertTrue( 1535 "connect/disconnect/connect - Wrong size data received: " 1536 + receive.getLength(), 1537 receive.getLength() == sendBytes.length); 1538 assertTrue("connect/disconnect/connect - Wrong data received" 1539 + new String(receive.getData(), 0, receive.getLength()) 1540 + ":" + new String(sendBytes), new String( 1541 receive.getData(), 0, receive.getLength()) 1542 .equals(new String(sendBytes))); 1543 assertTrue("connect/disconnect/connect - Wrong receiver:" 1544 + receive.getAddress() + ":" + localHost, receive 1545 .getAddress().equals(localHost)); 1546 } catch (Exception e) { 1547 fail( 1548 "Unexpected exception when sending data on dgram connected at native level after connect/disconnect/connect:" 1549 + e.toString()); 1550 } 1551 1552 if (server != null) { 1553 server.stopServer(); 1554 } 1555 1556 // validate that we can connect/disconnect then send/recv to any address 1557 server = null; 1558 ports = Support_PortManager.getNextPortsForUDP(3); 1559 serverPortNumber = ports[0]; 1560 try { 1561 InetAddress localHost = InetAddress.getLocalHost(); 1562 DatagramSocket ds = new DatagramSocket(ports[1]); 1563 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1564 1565 try { 1566 server = new DatagramServer(serverPortNumber, localHost); 1567 server.start(); 1568 Thread.sleep(1000); 1569 } catch (Exception e) { 1570 fail( 1571 "Failed to set up datagram server for native connected Dgram socket test "); 1572 } 1573 1574 int port = ds.getLocalPort(); 1575 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1576 ds.disconnect(); 1577 1578 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1579 DatagramPacket send = new DatagramPacket(sendBytes, 1580 sendBytes.length, localHost, serverPortNumber); 1581 ds.send(send); 1582 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1583 ds.setSoTimeout(2000); 1584 ds.receive(receive); 1585 ds.close(); 1586 assertTrue("connect/disconnect - Wrong size data received: " 1587 + receive.getLength(), 1588 receive.getLength() == sendBytes.length); 1589 assertTrue("connect/disconnect - Wrong data received" 1590 + new String(receive.getData(), 0, receive.getLength()) 1591 + ":" + new String(sendBytes), new String( 1592 receive.getData(), 0, receive.getLength()) 1593 .equals(new String(sendBytes))); 1594 assertTrue("connect/disconnect - Wrong receiver:" 1595 + receive.getAddress() + ":" + localHost, receive 1596 .getAddress().equals(localHost)); 1597 } catch (Exception e) { 1598 fail( 1599 "Unexpected exception when sending data on dgram connected at native level after connect/disconnect:" 1600 + e.toString()); 1601 } 1602 1603 if (server != null) { 1604 server.stopServer(); 1605 } 1606 1607 // validate that we can connect on an allready connected socket and then 1608 // send/recv 1609 server = null; 1610 ports = Support_PortManager.getNextPortsForUDP(3); 1611 serverPortNumber = ports[0]; 1612 try { 1613 InetAddress localHost = InetAddress.getLocalHost(); 1614 DatagramSocket ds = new DatagramSocket(ports[1]); 1615 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1616 1617 try { 1618 server = new DatagramServer(serverPortNumber, localHost); 1619 server.start(); 1620 Thread.sleep(1000); 1621 } catch (Exception e) { 1622 fail( 1623 "Failed to set up datagram server for native connected Dgram socket test "); 1624 } 1625 1626 int port = ds.getLocalPort(); 1627 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1628 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1629 1630 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1631 DatagramPacket send = new DatagramPacket(sendBytes, 1632 sendBytes.length); 1633 ds.send(send); 1634 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1635 ds.setSoTimeout(2000); 1636 ds.receive(receive); 1637 ds.close(); 1638 assertTrue("connect/connect - Wrong size data received: " 1639 + receive.getLength(), 1640 receive.getLength() == sendBytes.length); 1641 assertTrue("connect/connect - Wrong data received" 1642 + new String(receive.getData(), 0, receive.getLength()) 1643 + ":" + new String(sendBytes), new String( 1644 receive.getData(), 0, receive.getLength()) 1645 .equals(new String(sendBytes))); 1646 assertTrue("connect/connect - Wrong receiver:" 1647 + receive.getAddress() + ":" + localHost, receive 1648 .getAddress().equals(localHost)); 1649 } catch (Exception e) { 1650 fail( 1651 "Unexpected exception when sending data on dgram connected at native level after connect/connect: " 1652 + e.toString()); 1653 } 1654 1655 if (server != null) { 1656 server.stopServer(); 1657 } 1658 1659 // test for when we fail to connect at the native level. It seems to 1660 // fail for the any address so we use this. Now to be compatible we 1661 // don't throw the exception but eat it and then act as if we were 1662 // connected at the Java level. 1663 try { 1664 ds = new java.net.DatagramSocket(); 1665 byte[] addressBytes = { 0, 0, 0, 0 }; 1666 InetAddress inetAddress = InetAddress.getByAddress(addressBytes); 1667 int portNumber = Support_PortManager.getNextPortForUDP(); 1668 InetAddress localHost = InetAddress.getLocalHost(); 1669 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1670 assertTrue("Is not connected after connect to inaddr any", ds 1671 .isConnected()); 1672 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1673 DatagramPacket send = new DatagramPacket(sendBytes, 1674 sendBytes.length, localHost, portNumber); 1675 ds.send(send); 1676 fail( 1677 "No exception when trying to connect at native level with bad address (exception from send) "); 1678 } catch (Exception e) { 1679 assertTrue( 1680 "Wrong exception when trying to connect at native level with bad address (exception from send): " 1681 + e.toString(), 1682 (e instanceof IllegalArgumentException)); 1683 } 1684 } 1685 1686 public void test_isBound() { 1687 try { 1688 InetAddress addr = InetAddress.getLocalHost(); 1689 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1690 int port = ports[0]; 1691 1692 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1693 assertTrue("Socket indicated not bound when it should be (1)", 1694 theSocket.isBound()); 1695 theSocket.close(); 1696 1697 theSocket = new DatagramSocket(new InetSocketAddress(addr, port)); 1698 assertTrue("Socket indicated not bound when it should be (2)", 1699 theSocket.isBound()); 1700 theSocket.close(); 1701 1702 theSocket = new DatagramSocket((SocketAddress) null); 1703 assertFalse("Socket indicated bound when it should not be (1)", 1704 theSocket.isBound()); 1705 theSocket.close(); 1706 1707 // connect causes implicit bind 1708 theSocket = new DatagramSocket((SocketAddress) null); 1709 theSocket.connect(new InetSocketAddress(addr, port)); 1710 assertTrue("Socket indicated not bound when it should be (3)", 1711 theSocket.isBound()); 1712 theSocket.close(); 1713 1714 // now test when we bind explicitely 1715 InetSocketAddress theLocalAddress = new InetSocketAddress( 1716 InetAddress.getLocalHost(), ports[2]); 1717 theSocket = new DatagramSocket((SocketAddress) null); 1718 assertFalse("Socket indicated bound when it should not be (2)", 1719 theSocket.isBound()); 1720 theSocket.bind(theLocalAddress); 1721 assertTrue("Socket indicated not bound when it should be (4)", 1722 theSocket.isBound()); 1723 theSocket.close(); 1724 assertTrue("Socket indicated not bound when it should be (5)", 1725 theSocket.isBound()); 1726 } catch (Exception e) { 1727 fail("Got exception during isBound tests" + e.toString()); 1728 } 1729 } 1730 1731 public void test_isConnected() { 1732 try { 1733 InetAddress addr = InetAddress.getLocalHost(); 1734 int[] ports = Support_PortManager.getNextPortsForUDP(4); 1735 int port = ports[0]; 1736 1737 // base test 1738 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1739 assertFalse("Socket indicated connected when it should not be", 1740 theSocket.isConnected()); 1741 theSocket.connect(new InetSocketAddress(addr, port)); 1742 assertTrue("Socket indicated not connected when it should be", 1743 theSocket.isConnected()); 1744 1745 // reconnect the socket and make sure we get the right answer 1746 theSocket.connect(new InetSocketAddress(addr, ports[2])); 1747 assertTrue("Socket indicated not connected when it should be", 1748 theSocket.isConnected()); 1749 1750 // now disconnect the socket and make sure we get the right answer 1751 theSocket.disconnect(); 1752 assertFalse("Socket indicated connected when it should not be", 1753 theSocket.isConnected()); 1754 theSocket.close(); 1755 1756 // now check behavior when socket is closed when connected 1757 theSocket = new DatagramSocket(ports[3]); 1758 theSocket.connect(new InetSocketAddress(addr, port)); 1759 theSocket.close(); 1760 assertTrue("Socket indicated not connected when it should be", 1761 theSocket.isConnected()); 1762 } catch (Exception e) { 1763 fail("Got exception during isConnected tests" + e.toString()); 1764 } 1765 } 1766 1767 public void test_getRemoteSocketAddress() { 1768 try { 1769 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1770 int sport = ports[0]; 1771 int portNumber = ports[1]; 1772 DatagramSocket s = new DatagramSocket(new InetSocketAddress( 1773 InetAddress.getLocalHost(), portNumber)); 1774 s.connect(new InetSocketAddress(InetAddress.getLocalHost(), sport)); 1775 assertTrue("Returned incorrect InetSocketAddress(1):" 1776 + s.getLocalSocketAddress().toString(), s 1777 .getRemoteSocketAddress().equals( 1778 new InetSocketAddress(InetAddress.getLocalHost(), 1779 sport))); 1780 s.close(); 1781 1782 // now create one that is not connected and validate that we get the 1783 // right answer 1784 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1785 portNumber = ports[2]; 1786 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1787 portNumber)); 1788 assertNull( 1789 "Returned incorrect InetSocketAddress -unconnected socket:" 1790 + "Expected: NULL", theSocket 1791 .getRemoteSocketAddress()); 1792 1793 // now connect and validate we get the right answer 1794 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1795 sport)); 1796 assertTrue("Returned incorrect InetSocketAddress(2):" 1797 + theSocket.getRemoteSocketAddress().toString(), theSocket 1798 .getRemoteSocketAddress().equals( 1799 new InetSocketAddress(InetAddress.getLocalHost(), 1800 sport))); 1801 theSocket.close(); 1802 1803 } catch (Exception e) { 1804 fail("Exception during getRemoteSocketAddress test: " + e); 1805 } 1806 } 1807 1808 public void test_getLocalSocketAddress() throws Exception { 1809 int portNumber = Support_PortManager.getNextPortForUDP(); 1810 DatagramSocket s = new DatagramSocket(new InetSocketAddress( 1811 InetAddress.getLocalHost(), portNumber)); 1812 assertTrue("Returned incorrect InetSocketAddress(1):" 1813 + s.getLocalSocketAddress().toString() 1814 + "Expected: " 1815 + (new InetSocketAddress(InetAddress.getLocalHost(), 1816 portNumber)).toString(), s.getLocalSocketAddress() 1817 .equals( 1818 new InetSocketAddress(InetAddress.getLocalHost(), 1819 portNumber))); 1820 s.close(); 1821 1822 InetSocketAddress remoteAddress = (InetSocketAddress) s 1823 .getRemoteSocketAddress(); 1824 1825 // now create a socket that is not bound and validate we get the 1826 // right answer 1827 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1828 assertNull( 1829 "Returned incorrect InetSocketAddress -unbound socket- Expected null", 1830 theSocket.getLocalSocketAddress()); 1831 1832 // now bind the socket and make sure we get the right answer 1833 portNumber = Support_PortManager.getNextPortForUDP(); 1834 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1835 portNumber)); 1836 assertTrue("Returned incorrect InetSocketAddress(2):" 1837 + theSocket.getLocalSocketAddress().toString() 1838 + "Expected: " 1839 + (new InetSocketAddress(InetAddress.getLocalHost(), 1840 portNumber)).toString(), theSocket 1841 .getLocalSocketAddress().equals( 1842 new InetSocketAddress(InetAddress.getLocalHost(), 1843 portNumber))); 1844 theSocket.close(); 1845 1846 // now check behavior when the ANY address is returned 1847 s = new DatagramSocket(0); 1848 assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), 1849 ((InetSocketAddress) s.getLocalSocketAddress()).getAddress() instanceof Inet6Address); 1850 s.close(); 1851 } 1852 1853 public void test_setReuseAddressZ() throws Exception { 1854 // test case were we set it to false 1855 DatagramSocket theSocket1 = null; 1856 DatagramSocket theSocket2 = null; 1857 try { 1858 InetSocketAddress theAddress = new InetSocketAddress( 1859 InetAddress.getLocalHost(), Support_PortManager 1860 .getNextPortForUDP()); 1861 theSocket1 = new DatagramSocket((SocketAddress) null); 1862 theSocket2 = new DatagramSocket((SocketAddress) null); 1863 theSocket1.setReuseAddress(false); 1864 theSocket2.setReuseAddress(false); 1865 theSocket1.bind(theAddress); 1866 theSocket2.bind(theAddress); 1867 fail( 1868 "No exception when trying to connect to do duplicate socket bind with re-useaddr set to false"); 1869 } catch (BindException e) { 1870 1871 } 1872 if (theSocket1 != null) 1873 theSocket1.close(); 1874 if (theSocket2 != null) 1875 theSocket2.close(); 1876 1877 // test case were we set it to true 1878 try { 1879 InetSocketAddress theAddress = new InetSocketAddress( 1880 InetAddress.getLocalHost(), Support_PortManager 1881 .getNextPortForUDP()); 1882 theSocket1 = new DatagramSocket((SocketAddress) null); 1883 theSocket2 = new DatagramSocket((SocketAddress) null); 1884 theSocket1.setReuseAddress(true); 1885 theSocket2.setReuseAddress(true); 1886 theSocket1.bind(theAddress); 1887 theSocket2.bind(theAddress); 1888 } catch (Exception e) { 1889 fail( 1890 "unexpected exception when trying to connect to do duplicate socket bind with re-useaddr set to true"); 1891 } 1892 if (theSocket1 != null) 1893 theSocket1.close(); 1894 if (theSocket2 != null) 1895 theSocket2.close(); 1896 1897 // test the default case which we expect to be the same on all 1898 // platforms 1899 try { 1900 InetSocketAddress theAddress = new InetSocketAddress( 1901 InetAddress.getLocalHost(), Support_PortManager 1902 .getNextPortForUDP()); 1903 theSocket1 = new DatagramSocket((SocketAddress) null); 1904 theSocket2 = new DatagramSocket((SocketAddress) null); 1905 theSocket1.bind(theAddress); 1906 theSocket2.bind(theAddress); 1907 fail( 1908 "No exception when trying to connect to do duplicate socket bind with re-useaddr left as default"); 1909 } catch (BindException e) { 1910 1911 } 1912 if (theSocket1 != null) 1913 theSocket1.close(); 1914 if (theSocket2 != null) 1915 theSocket2.close(); 1916 1917 try { 1918 theSocket1.setReuseAddress(true); 1919 fail("SocketException was not thrown."); 1920 } catch(SocketException se) { 1921 //expected 1922 } 1923 } 1924 1925 public void test_getReuseAddress() throws Exception { 1926 DatagramSocket theSocket = new DatagramSocket(); 1927 theSocket.setReuseAddress(true); 1928 assertTrue("getReuseAddress false when it should be true", 1929 theSocket.getReuseAddress()); 1930 theSocket.setReuseAddress(false); 1931 assertFalse("getReuseAddress true when it should be false", 1932 theSocket.getReuseAddress()); 1933 theSocket.close(); 1934 try { 1935 theSocket.getReuseAddress(); 1936 fail("SocketException was not thrown."); 1937 } catch(SocketException se) { 1938 //expected 1939 } 1940 } 1941 1942 public void test_setBroadcastZ() throws Exception { 1943 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1944 DatagramSocket theSocket = new DatagramSocket(ports[0]); 1945 theSocket.setBroadcast(false); 1946 byte theBytes[] = { -1, -1, -1, -1 }; 1947 1948 // validate we cannot connect to the broadcast address when 1949 // setBroadcast is false 1950 try { 1951 theSocket.connect(new InetSocketAddress(InetAddress 1952 .getByAddress(theBytes), ports[1])); 1953 assertFalse( 1954 "No exception when connecting to broadcast address with setBroadcast(false)", 1955 theSocket.getBroadcast()); 1956 } catch (SocketException ex) { 1957 //expected 1958 } 1959 1960 // now validate that we can connect to the broadcast address when 1961 // setBroadcast is true 1962 theSocket.setBroadcast(true); 1963 theSocket.connect(new InetSocketAddress(InetAddress 1964 .getByAddress(theBytes), ports[2])); 1965 1966 theSocket.close(); 1967 try { 1968 theSocket.setBroadcast(false); 1969 fail("SocketException was not thrown."); 1970 } catch(SocketException se) { 1971 //expected 1972 } 1973 } 1974 1975 public void test_getBroadcast() throws Exception { 1976 DatagramSocket theSocket = new DatagramSocket(); 1977 theSocket.setBroadcast(true); 1978 assertTrue("getBroadcast false when it should be true", theSocket 1979 .getBroadcast()); 1980 theSocket.setBroadcast(false); 1981 assertFalse("getBroadcast true when it should be False", theSocket 1982 .getBroadcast()); 1983 theSocket.close(); 1984 try { 1985 theSocket.getBroadcast(); 1986 fail("SocketException was not thrown."); 1987 } catch(SocketException se) { 1988 //expected 1989 } 1990 } 1991 1992 public void test_setTrafficClassI() throws Exception { 1993 int IPTOS_LOWCOST = 0x2; 1994 int IPTOS_RELIABILTY = 0x4; 1995 int IPTOS_THROUGHPUT = 0x8; 1996 int IPTOS_LOWDELAY = 0x10; 1997 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1998 1999 new InetSocketAddress(InetAddress.getLocalHost(), 2000 ports[0]); 2001 DatagramSocket theSocket = new DatagramSocket(ports[1]); 2002 2003 // validate that value set must be between 0 and 255 2004 try { 2005 theSocket.setTrafficClass(256); 2006 fail("No exception when traffic class set to 256"); 2007 } catch (IllegalArgumentException e) { 2008 } 2009 2010 try { 2011 theSocket.setTrafficClass(-1); 2012 fail("No exception when traffic class set to -1"); 2013 } catch (IllegalArgumentException e) { 2014 } 2015 2016 // now validate that we can set it to some good values 2017 theSocket.setTrafficClass(IPTOS_LOWCOST); 2018 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 2019 2020 theSocket.close(); 2021 try { 2022 theSocket.setTrafficClass(1); 2023 fail("SocketException was not thrown."); 2024 } catch(SocketException se) { 2025 //expected 2026 } 2027 } 2028 2029 public void test_getTrafficClass() throws Exception { 2030 int IPTOS_LOWCOST = 0x2; 2031 int IPTOS_RELIABILTY = 0x4; 2032 int IPTOS_THROUGHPUT = 0x8; 2033 int IPTOS_LOWDELAY = 0x10; 2034 int[] ports = Support_PortManager.getNextPortsForUDP(2); 2035 2036 new InetSocketAddress(InetAddress.getLocalHost(), 2037 ports[0]); 2038 DatagramSocket theSocket = new DatagramSocket(ports[1]); 2039 2040 /* 2041 * we cannot actually check that the values are set as if a platform 2042 * does not support the option then it may come back unset even 2043 * though we set it so just get the value to make sure we can get it 2044 */ 2045 int trafficClass = theSocket.getTrafficClass(); 2046 2047 theSocket.close(); 2048 try { 2049 theSocket.getTrafficClass(); 2050 fail("SocketException was not thrown."); 2051 } catch(SocketException se) { 2052 //expected 2053 } 2054 } 2055 2056 public void test_isClosed() { 2057 try { 2058 DatagramSocket theSocket = new DatagramSocket(); 2059 2060 // validate isClosed returns expected values 2061 assertFalse("Socket should indicate it is not closed(1):", 2062 theSocket.isClosed()); 2063 theSocket.close(); 2064 assertTrue("Socket should indicate it is not closed(1):", theSocket 2065 .isClosed()); 2066 2067 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 2068 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 2069 theSocket = new DatagramSocket(theAddress); 2070 assertFalse("Socket should indicate it is not closed(2):", 2071 theSocket.isClosed()); 2072 theSocket.close(); 2073 assertTrue("Socket should indicate it is not closed(2):", theSocket 2074 .isClosed()); 2075 } catch (Exception e) { 2076 fail("Got exception during isClosed tests" + e.toString()); 2077 } 2078 } 2079 2080 public void test_getChannel() throws Exception { 2081 assertNull(new DatagramSocket().getChannel()); 2082 2083 int portNumber = Support_PortManager.getNextPortForUDP(); 2084 DatagramSocket ds = null; 2085 try { 2086 InetAddress ia = InetAddress 2087 .getByName(Support_Configuration.IPv6GlobalAddressJcl4); 2088 ds = new DatagramSocket(); 2089 assertNull(ds.getChannel()); 2090 ds.connect(ia, portNumber); 2091 assertNull(ds.getChannel()); 2092 } catch (SocketException e) { 2093 fail("SocketException was thrown."); 2094 } finally { 2095 ds.disconnect(); 2096 ds.close(); 2097 } 2098 portNumber = Support_PortManager.getNextPortForUDP(); 2099 SocketAddress address = new InetSocketAddress(portNumber); 2100 DatagramChannel channel = DatagramChannel.open(); 2101 DatagramSocket socket = channel.socket(); 2102 assertEquals(channel, socket.getChannel()); 2103 socket.close(); 2104 } 2105 2106 class TestDatagramSocketImplFactory implements DatagramSocketImplFactory { 2107 public DatagramSocketImpl createDatagramSocketImpl() { 2108 return new TestDatagramSocketImpl(); 2109 } 2110 } 2111 2112 class TestDatagramSocketImpl extends DatagramSocketImpl { 2113 2114 @Override 2115 protected void bind(int arg0, InetAddress arg1) throws SocketException { 2116 // TODO Auto-generated method stub 2117 2118 } 2119 2120 @Override 2121 protected void close() { 2122 // TODO Auto-generated method stub 2123 2124 } 2125 2126 @Override 2127 protected void create() throws SocketException { 2128 // TODO Auto-generated method stub 2129 2130 } 2131 2132 @Override 2133 protected byte getTTL() throws IOException { 2134 // TODO Auto-generated method stub 2135 return 0; 2136 } 2137 2138 @Override 2139 protected int getTimeToLive() throws IOException { 2140 // TODO Auto-generated method stub 2141 return 0; 2142 } 2143 2144 @Override 2145 protected void join(InetAddress arg0) throws IOException { 2146 // TODO Auto-generated method stub 2147 2148 } 2149 2150 @Override 2151 protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException { 2152 // TODO Auto-generated method stub 2153 2154 } 2155 2156 @Override 2157 protected void leave(InetAddress arg0) throws IOException { 2158 // TODO Auto-generated method stub 2159 2160 } 2161 2162 @Override 2163 protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException { 2164 // TODO Auto-generated method stub 2165 2166 } 2167 2168 @Override 2169 protected int peek(InetAddress arg0) throws IOException { 2170 // TODO Auto-generated method stub 2171 return 0; 2172 } 2173 2174 @Override 2175 protected int peekData(DatagramPacket arg0) throws IOException { 2176 // TODO Auto-generated method stub 2177 return 0; 2178 } 2179 2180 @Override 2181 protected void receive(DatagramPacket arg0) throws IOException { 2182 // TODO Auto-generated method stub 2183 2184 } 2185 2186 @Override 2187 protected void send(DatagramPacket arg0) throws IOException { 2188 // TODO Auto-generated method stub 2189 2190 } 2191 2192 @Override 2193 protected void setTTL(byte arg0) throws IOException { 2194 // TODO Auto-generated method stub 2195 2196 } 2197 2198 @Override 2199 protected void setTimeToLive(int arg0) throws IOException { 2200 // TODO Auto-generated method stub 2201 2202 } 2203 2204 public Object getOption(int arg0) throws SocketException { 2205 // TODO Auto-generated method stub 2206 return null; 2207 } 2208 2209 public void setOption(int arg0, Object arg1) throws SocketException { 2210 // TODO Auto-generated method stub 2211 2212 } 2213 2214 } 2215 2216 /** 2217 * Sets up the fixture, for example, open a network connection. This method 2218 * is called before a test is executed. 2219 */ 2220 protected void setUp() { 2221 retval = "Bogus retval"; 2222 } 2223 2224 /** 2225 * Tears down the fixture, for example, close a network connection. This 2226 * method is called after a test is executed. 2227 */ 2228 protected void tearDown() { 2229 try { 2230 ds.close(); 2231 sds.close(); 2232 } catch (Exception e) { 2233 } 2234 } 2235 2236 protected void receive_oversize_java_net_DatagramPacket() throws Exception { 2237 final int[] ports = Support_PortManager.getNextPortsForUDP(2); 2238 final int portNumber = ports[0]; 2239 2240 class TestDGRcvOver implements Runnable { 2241 public void run() { 2242 InetAddress localHost = null; 2243 try { 2244 localHost = InetAddress.getLocalHost(); 2245 Thread.sleep(1000); 2246 DatagramSocket sds = new DatagramSocket(ports[1]); 2247 DatagramPacket rdp = new DatagramPacket("0123456789" 2248 .getBytes(), 10, localHost, portNumber); 2249 sds.send(rdp); 2250 sds.close(); 2251 } catch (Exception e) { 2252 throw new RuntimeException(e); 2253 } 2254 } 2255 } 2256 2257 try { 2258 new Thread(new TestDGRcvOver(), "DGSenderOver").start(); 2259 ds = new java.net.DatagramSocket(portNumber); 2260 ds.setSoTimeout(6000); 2261 byte rbuf[] = new byte[5]; 2262 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 2263 ; 2264 ds.receive(rdp); 2265 ds.close(); 2266 assertTrue("Send/Receive oversize failed to return correct data: " 2267 + new String(rbuf, 0, 5), new String(rbuf, 0, 5) 2268 .equals("01234")); 2269 } finally { 2270 ds.close(); 2271 } 2272 } 2273} 2274