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