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 org.apache.harmony.luni.tests.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.UnknownHostException; 36import java.util.Date; 37 38import tests.support.Support_Configuration; 39import tests.support.Support_PortManager; 40 41public class DatagramSocketTest extends junit.framework.TestCase { 42 43 java.net.DatagramSocket ds; 44 45 java.net.DatagramPacket dp; 46 47 DatagramSocket sds = null; 48 49 String retval; 50 51 String testString = "Test String"; 52 53 boolean interrupted; 54 55 class DatagramServer extends Thread { 56 57 public DatagramSocket ms; 58 59 boolean running = true; 60 61 public volatile byte[] rbuf = new byte[512]; 62 63 volatile DatagramPacket rdp = null; 64 65 public void run() { 66 try { 67 while (running) { 68 try { 69 ms.receive(rdp); 70 // echo the packet back 71 ms.send(rdp); 72 } catch (java.io.InterruptedIOException e) { 73 Thread.yield(); 74 } 75 ; 76 } 77 ; 78 } catch (java.io.IOException e) { 79 System.out.println("DatagramServer server failed: " + e); 80 } finally { 81 ms.close(); 82 } 83 } 84 85 public void stopServer() { 86 running = false; 87 } 88 89 public DatagramServer(int aPort, InetAddress address) 90 throws IOException { 91 rbuf = new byte[512]; 92 rbuf[0] = -1; 93 rdp = new DatagramPacket(rbuf, rbuf.length); 94 ms = new DatagramSocket(aPort, address); 95 ms.setSoTimeout(2000); 96 } 97 } 98 99 /** 100 * @tests java.net.DatagramSocket#DatagramSocket() 101 */ 102 public void test_Constructor() throws SocketException { 103 new DatagramSocket(); 104 } 105 106 /** 107 * @tests java.net.DatagramSocket#DatagramSocket(int) 108 */ 109 public void test_ConstructorI() throws SocketException { 110 DatagramSocket ds = new DatagramSocket(0); 111 ds.close(); 112 } 113 114 /** 115 * @tests java.net.DatagramSocket#DatagramSocket(int, java.net.InetAddress) 116 */ 117 public void test_ConstructorILjava_net_InetAddress() throws IOException { 118 DatagramSocket ds = new DatagramSocket(0, InetAddress.getLocalHost()); 119 assertTrue("Created socket with incorrect port", ds.getLocalPort() != 0); 120 assertEquals("Created socket with incorrect address", InetAddress 121 .getLocalHost(), ds.getLocalAddress()); 122 } 123 124 /** 125 * @tests java.net.DatagramSocket#close() 126 */ 127 public void test_close() throws UnknownHostException, SocketException { 128 DatagramSocket ds = new DatagramSocket(0); 129 DatagramPacket dp = new DatagramPacket("Test String".getBytes(), 11, 130 InetAddress.getLocalHost(), 0); 131 ds.close(); 132 try { 133 ds.send(dp); 134 fail("Data sent after close"); 135 } catch (IOException e) { 136 // Expected 137 } 138 } 139 140 public void test_connectLjava_net_InetAddressI() throws Exception { 141 DatagramSocket ds = new DatagramSocket(); 142 InetAddress inetAddress = InetAddress.getLocalHost(); 143 ds.connect(inetAddress, 0); 144 assertEquals("Incorrect InetAddress", inetAddress, ds.getInetAddress()); 145 assertEquals("Incorrect Port", 0, ds.getPort()); 146 ds.disconnect(); 147 148 ds = new java.net.DatagramSocket(); 149 inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"); 150 int portNumber = Support_PortManager.getNextPortForUDP(); 151 ds.connect(inetAddress, portNumber); 152 assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(inetAddress)); 153 assertTrue("Incorrect Port", ds.getPort() == portNumber); 154 ds.disconnect(); 155 156 // Create a connected datagram socket to test 157 // PlainDatagramSocketImpl.peek() 158 InetAddress localHost = InetAddress.getLocalHost(); 159 ds = new DatagramSocket(); 160 int port = ds.getLocalPort(); 161 ds.connect(localHost, port); 162 DatagramPacket send = new DatagramPacket(new byte[10], 10, localHost, 163 port); 164 ds.send(send); 165 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 166 ds.setSoTimeout(2000); 167 ds.receive(receive); 168 ds.close(); 169 assertTrue("Wrong size: " + receive.getLength(), 170 receive.getLength() == 10); 171 assertTrue("Wrong receiver", receive.getAddress().equals(localHost)); 172 173 class DatagramServer extends Thread { 174 175 public DatagramSocket ms; 176 177 boolean running = true; 178 179 public byte[] rbuf = new byte[512]; 180 181 DatagramPacket rdp = null; 182 183 public void run() { 184 try { 185 while (running) { 186 try { 187 ms.receive(rdp); 188 // echo the packet back 189 ms.send(rdp); 190 } catch (java.io.InterruptedIOException e) { 191 Thread.yield(); 192 } 193 ; 194 } 195 ; 196 } catch (java.io.IOException e) { 197 System.out.println("Multicast server failed: " + e); 198 } finally { 199 ms.close(); 200 } 201 } 202 203 public void stopServer() { 204 running = false; 205 } 206 207 public DatagramServer(int aPort, InetAddress address) 208 throws java.io.IOException { 209 rbuf = new byte[512]; 210 rbuf[0] = -1; 211 rdp = new DatagramPacket(rbuf, rbuf.length); 212 ms = new DatagramSocket(aPort, address); 213 ms.setSoTimeout(2000); 214 } 215 } 216 217 // validate that we get the PortUnreachable exception if we try to 218 // send a dgram to a server that is not running and then do a recv 219 try { 220 ds = new java.net.DatagramSocket(); 221 inetAddress = InetAddress.getLocalHost(); 222 portNumber = Support_PortManager.getNextPortForUDP(); 223 ds.connect(inetAddress, portNumber); 224 send = new DatagramPacket(new byte[10], 10); 225 ds.send(send); 226 receive = new DatagramPacket(new byte[20], 20); 227 ds.setSoTimeout(10000); 228 ds.receive(receive); 229 ds.close(); 230 fail("No PortUnreachableException when connected at native level on recv "); 231 } catch (PortUnreachableException e) { 232 // Expected 233 } 234 235 // validate that we can send/receive with datagram sockets connected at 236 // the native level 237 DatagramServer server = null; 238 int[] ports = Support_PortManager.getNextPortsForUDP(3); 239 int serverPortNumber = ports[0]; 240 241 localHost = InetAddress.getLocalHost(); 242 ds = new DatagramSocket(ports[1]); 243 DatagramSocket ds2 = new DatagramSocket(ports[2]); 244 245 server = new DatagramServer(serverPortNumber, localHost); 246 server.start(); 247 Thread.sleep(1000); 248 249 port = ds.getLocalPort(); 250 ds.connect(localHost, serverPortNumber); 251 252 final byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 253 send = new DatagramPacket(sendBytes, sendBytes.length); 254 ds.send(send); 255 receive = new DatagramPacket(new byte[20], 20); 256 ds.setSoTimeout(2000); 257 ds.receive(receive); 258 ds.close(); 259 assertTrue("Wrong size data received: " + receive.getLength(), receive 260 .getLength() == sendBytes.length); 261 assertTrue("Wrong data received" 262 + new String(receive.getData(), 0, receive.getLength()) + ":" 263 + new String(sendBytes), new String(receive.getData(), 0, 264 receive.getLength()).equals(new String(sendBytes))); 265 assertTrue("Wrong receiver:" + receive.getAddress() + ":" + localHost, 266 receive.getAddress().equals(localHost)); 267 268 if (server != null) { 269 server.stopServer(); 270 } 271 272 // validate that we can disconnect 273 try { 274 ds = new java.net.DatagramSocket(); 275 inetAddress = InetAddress.getLocalHost(); 276 portNumber = Support_PortManager.getNextPortForUDP(); 277 ds.connect(inetAddress, portNumber); 278 ds.disconnect(); 279 ds.close(); 280 } catch (PortUnreachableException e) { 281 // Expected 282 } 283 284 // validate that once connected we cannot send to another address 285 try { 286 ds = new java.net.DatagramSocket(); 287 inetAddress = InetAddress.getLocalHost(); 288 portNumber = Support_PortManager.getNextPortForUDP(); 289 ds.connect(inetAddress, portNumber); 290 send = new DatagramPacket(new byte[10], 10, inetAddress, 291 portNumber + 1); 292 ds.send(send); 293 ds.close(); 294 fail("No Exception when trying to send to a different address on a connected socket "); 295 } catch (IllegalArgumentException e) { 296 // Expected 297 } 298 299 // validate that we can connect, then disconnect, then connect then 300 // send/recv 301 server = null; 302 ports = Support_PortManager.getNextPortsForUDP(3); 303 serverPortNumber = ports[0]; 304 305 localHost = InetAddress.getLocalHost(); 306 ds = new DatagramSocket(ports[1]); 307 ds2 = new DatagramSocket(ports[2]); 308 309 server = new DatagramServer(serverPortNumber, localHost); 310 server.start(); 311 Thread.sleep(1000); 312 313 port = ds.getLocalPort(); 314 ds.connect(localHost, serverPortNumber + 1); 315 ds.disconnect(); 316 ds.connect(localHost, serverPortNumber); 317 318 send = new DatagramPacket(sendBytes, sendBytes.length); 319 ds.send(send); 320 receive = new DatagramPacket(new byte[20], 20); 321 ds.setSoTimeout(2000); 322 ds.receive(receive); 323 ds.close(); 324 assertTrue("connect/disconnect/connect - Wrong size data received: " 325 + receive.getLength(), receive.getLength() == sendBytes.length); 326 assertTrue("connect/disconnect/connect - Wrong data received" 327 + new String(receive.getData(), 0, receive.getLength()) + ":" 328 + new String(sendBytes), new String(receive.getData(), 0, 329 receive.getLength()).equals(new String(sendBytes))); 330 assertTrue("connect/disconnect/connect - Wrong receiver:" 331 + receive.getAddress() + ":" + localHost, receive.getAddress() 332 .equals(localHost)); 333 334 if (server != null) { 335 server.stopServer(); 336 } 337 338 // validate that we can connect/disconnect then send/recv to any address 339 server = null; 340 ports = Support_PortManager.getNextPortsForUDP(3); 341 serverPortNumber = ports[0]; 342 343 localHost = InetAddress.getLocalHost(); 344 ds = new DatagramSocket(ports[1]); 345 ds2 = new DatagramSocket(ports[2]); 346 347 server = new DatagramServer(serverPortNumber, localHost); 348 server.start(); 349 Thread.sleep(1000); 350 351 port = ds.getLocalPort(); 352 ds.connect(localHost, serverPortNumber + 1); 353 ds.disconnect(); 354 355 send = new DatagramPacket(sendBytes, sendBytes.length, localHost, 356 serverPortNumber); 357 ds.send(send); 358 receive = new DatagramPacket(new byte[20], 20); 359 ds.setSoTimeout(2000); 360 ds.receive(receive); 361 ds.close(); 362 assertTrue("connect/disconnect - Wrong size data received: " 363 + receive.getLength(), receive.getLength() == sendBytes.length); 364 assertTrue("connect/disconnect - Wrong data received" 365 + new String(receive.getData(), 0, receive.getLength()) + ":" 366 + new String(sendBytes), new String(receive.getData(), 0, 367 receive.getLength()).equals(new String(sendBytes))); 368 assertTrue("connect/disconnect - Wrong receiver:" 369 + receive.getAddress() + ":" + localHost, receive.getAddress() 370 .equals(localHost)); 371 372 if (server != null) { 373 server.stopServer(); 374 } 375 376 // validate that we can connect on an allready connected socket and then 377 // send/recv 378 server = null; 379 ports = Support_PortManager.getNextPortsForUDP(3); 380 serverPortNumber = ports[0]; 381 382 localHost = InetAddress.getLocalHost(); 383 ds = new DatagramSocket(ports[1]); 384 ds2 = new DatagramSocket(ports[2]); 385 386 server = new DatagramServer(serverPortNumber, localHost); 387 server.start(); 388 Thread.sleep(1000); 389 390 port = ds.getLocalPort(); 391 ds.connect(localHost, serverPortNumber + 1); 392 ds.connect(localHost, serverPortNumber); 393 394 send = new DatagramPacket(sendBytes, sendBytes.length); 395 ds.send(send); 396 receive = new DatagramPacket(new byte[20], 20); 397 ds.setSoTimeout(2000); 398 ds.receive(receive); 399 ds.close(); 400 assertTrue("connect/connect - Wrong size data received: " 401 + receive.getLength(), receive.getLength() == sendBytes.length); 402 assertTrue("connect/connect - Wrong data received" 403 + new String(receive.getData(), 0, receive.getLength()) + ":" 404 + new String(sendBytes), new String(receive.getData(), 0, 405 receive.getLength()).equals(new String(sendBytes))); 406 assertTrue("connect/connect - Wrong receiver:" + receive.getAddress() 407 + ":" + localHost, receive.getAddress().equals(localHost)); 408 409 if (server != null) { 410 server.stopServer(); 411 } 412 413 // test for when we fail to connect at the native level. Even though we 414 // fail at the native level there is no way to return an exception so 415 // there should be no exception 416 ds = new java.net.DatagramSocket(); 417 byte[] addressBytes = { 0, 0, 0, 0 }; 418 inetAddress = InetAddress.getByAddress(addressBytes); 419 portNumber = Support_PortManager.getNextPortForUDP(); 420 ds.connect(inetAddress, portNumber); 421 422 if ("true".equals(System.getProperty("run.ipv6tests"))) { 423 System.out 424 .println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6 address"); 425 426 ds = new java.net.DatagramSocket(); 427 byte[] addressTestBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 428 0, 0, 0 }; 429 inetAddress = InetAddress.getByAddress(addressTestBytes); 430 portNumber = Support_PortManager.getNextPortForUDP(); 431 ds.connect(inetAddress, portNumber); 432 } 433 } 434 435 public void test_disconnect() throws Exception { 436 DatagramSocket ds = new DatagramSocket(); 437 InetAddress inetAddress = InetAddress.getLocalHost(); 438 ds.connect(inetAddress, 0); 439 ds.disconnect(); 440 assertNull("Incorrect InetAddress", ds.getInetAddress()); 441 assertEquals("Incorrect Port", -1, ds.getPort()); 442 443 ds = new DatagramSocket(); 444 inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4"); 445 ds.connect(inetAddress, 0); 446 ds.disconnect(); 447 assertNull("Incorrect InetAddress", ds.getInetAddress()); 448 assertEquals("Incorrect Port", -1, ds.getPort()); 449 } 450 451 /** 452 * @tests java.net.DatagramSocket#getInetAddress() 453 */ 454 public void test_getInetAddress() { 455 assertTrue("Used to test", true); 456 } 457 458 public void test_getLocalAddress() throws Exception { 459 // Test for method java.net.InetAddress 460 // java.net.DatagramSocket.getLocalAddress() 461 int portNumber = Support_PortManager.getNextPortForUDP(); 462 InetAddress local = InetAddress.getLocalHost(); 463 ds = new java.net.DatagramSocket(portNumber, local); 464 assertEquals(InetAddress.getByName(InetAddress.getLocalHost().getHostName()), ds.getLocalAddress()); 465 466 // now check behavior when the ANY address is returned 467 DatagramSocket s = new DatagramSocket(0); 468 assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), s.getLocalAddress() instanceof Inet6Address); 469 s.close(); 470 } 471 472 /** 473 * @tests java.net.DatagramSocket#getLocalPort() 474 */ 475 public void test_getLocalPort() throws SocketException { 476 DatagramSocket ds = new DatagramSocket(); 477 assertTrue("Returned incorrect port", ds.getLocalPort() != 0); 478 } 479 480 /** 481 * @tests java.net.DatagramSocket#getPort() 482 */ 483 public void test_getPort() throws IOException { 484 DatagramSocket theSocket = new DatagramSocket(); 485 assertEquals("Expected -1 for remote port as not connected", -1, 486 theSocket.getPort()); 487 488 // Now connect the socket and validate that we get the right port 489 int portNumber = 49152; // any valid port, even if it is unreachable 490 theSocket.connect(InetAddress.getLocalHost(), portNumber); 491 assertEquals("getPort returned wrong value", portNumber, theSocket 492 .getPort()); 493 } 494 495 public void test_getReceiveBufferSize() throws Exception { 496 DatagramSocket ds = new DatagramSocket(); 497 ds.setReceiveBufferSize(130); 498 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 499 } 500 501 public void test_getSendBufferSize() throws Exception { 502 int portNumber = Support_PortManager.getNextPortForUDP(); 503 ds = new java.net.DatagramSocket(portNumber); 504 ds.setSendBufferSize(134); 505 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 506 } 507 508 public void test_getSoTimeout() throws Exception { 509 DatagramSocket ds = new DatagramSocket(); 510 ds.setSoTimeout(100); 511 assertEquals("Returned incorrect timeout", 100, ds.getSoTimeout()); 512 } 513 514 public void test_receiveLjava_net_DatagramPacket() throws IOException { 515 // Test for method void 516 // java.net.DatagramSocket.receive(java.net.DatagramPacket) 517 518 receive_oversize_java_net_DatagramPacket(); 519 final int[] ports = Support_PortManager.getNextPortsForUDP(2); 520 final int portNumber = ports[0]; 521 522 class TestDGRcv implements Runnable { 523 public void run() { 524 InetAddress localHost = null; 525 try { 526 localHost = InetAddress.getLocalHost(); 527 Thread.sleep(1000); 528 DatagramSocket sds = new DatagramSocket(ports[1]); 529 DatagramPacket rdp = new DatagramPacket("Test String" 530 .getBytes(), 11, localHost, portNumber); 531 sds.send(rdp); 532 sds.close(); 533 } catch (Exception e) { 534 System.err.println("host " + localHost + " port " 535 + portNumber + " failed to send data: " + e); 536 e.printStackTrace(); 537 } 538 } 539 } 540 541 try { 542 new Thread(new TestDGRcv(), "DGSender").start(); 543 ds = new java.net.DatagramSocket(portNumber); 544 ds.setSoTimeout(6000); 545 byte rbuf[] = new byte[1000]; 546 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 547 548 ds.receive(rdp); 549 ds.close(); 550 assertTrue("Send/Receive failed to return correct data: " 551 + new String(rbuf, 0, 11), new String(rbuf, 0, 11) 552 .equals("Test String")); 553 } finally { 554 ds.close(); 555 } 556 557 try { 558 interrupted = false; 559 final DatagramSocket ds = new DatagramSocket(); 560 ds.setSoTimeout(12000); 561 Runnable runnable = new Runnable() { 562 public void run() { 563 try { 564 ds.receive(new DatagramPacket(new byte[1], 1)); 565 } catch (InterruptedIOException e) { 566 interrupted = true; 567 } catch (IOException e) { 568 } 569 } 570 }; 571 Thread thread = new Thread(runnable, "DatagramSocket.receive1"); 572 thread.start(); 573 try { 574 do { 575 Thread.sleep(500); 576 } while (!thread.isAlive()); 577 } catch (InterruptedException e) { 578 } 579 ds.close(); 580 int c = 0; 581 do { 582 try { 583 Thread.sleep(500); 584 } catch (InterruptedException e) { 585 } 586 if (interrupted) { 587 fail("received interrupt"); 588 } 589 if (++c > 4) { 590 fail("read call did not exit"); 591 } 592 } while (thread.isAlive()); 593 594 interrupted = false; 595 int[] ports1 = Support_PortManager.getNextPortsForUDP(2); 596 final int portNum = ports[0]; 597 final DatagramSocket ds2 = new DatagramSocket(ports[1]); 598 ds2.setSoTimeout(12000); 599 Runnable runnable2 = new Runnable() { 600 public void run() { 601 try { 602 ds2.receive(new DatagramPacket(new byte[1], 1, 603 InetAddress.getLocalHost(), portNum)); 604 } catch (InterruptedIOException e) { 605 interrupted = true; 606 } catch (IOException e) { 607 } 608 } 609 }; 610 Thread thread2 = new Thread(runnable2, "DatagramSocket.receive2"); 611 thread2.start(); 612 try { 613 do { 614 Thread.sleep(500); 615 } while (!thread2.isAlive()); 616 } catch (InterruptedException e) { 617 } 618 ds2.close(); 619 int c2 = 0; 620 do { 621 try { 622 Thread.sleep(500); 623 } catch (InterruptedException e) { 624 } 625 if (interrupted) { 626 fail("receive2 was interrupted"); 627 } 628 if (++c2 > 4) { 629 fail("read2 call did not exit"); 630 } 631 } while (thread2.isAlive()); 632 633 interrupted = false; 634 DatagramSocket ds3 = new DatagramSocket(); 635 ds3.setSoTimeout(500); 636 Date start = new Date(); 637 try { 638 ds3.receive(new DatagramPacket(new byte[1], 1)); 639 } catch (InterruptedIOException e) { 640 interrupted = true; 641 } 642 ds3.close(); 643 assertTrue("receive not interrupted", interrupted); 644 int delay = (int) (new Date().getTime() - start.getTime()); 645 assertTrue("timeout too soon: " + delay, delay >= 490); 646 } catch (IOException e) { 647 fail("Unexpected IOException : " + e.getMessage()); 648 } 649 650 } 651 652 /** 653 * Tests receive() method in various combinations with 654 * DatagramPacket#getLength() and DatagramPacket#getLength(). This is 655 * regression test for HARMONY-2276. 656 * 657 * @throws IOException 658 * if some I/O error occured 659 */ 660 // public void test2276() throws IOException { 661 // final String ADDRESS = "239.255.2.3"; 662 // final int PORT = Support_PortManager.getNextPortForUDP(); 663 // InetAddress group = InetAddress.getByName(ADDRESS); 664 // MulticastSocket socket = new MulticastSocket(PORT); 665 // byte[] recvData = new byte[100]; 666 // DatagramPacket recvDatagram = new DatagramPacket(recvData, 667 // recvData.length); 668 // 669 // String message = "Hello, world!"; 670 // String longerMessage = message + " again."; 671 // String veryLongMessage = longerMessage + " Forever!"; 672 // 673 // socket.joinGroup(group); 674 // socket.setSoTimeout(5000); // prevent eternal block in 675 // // socket.receive() 676 // // send & recieve packet 677 // byte[] sendData = message.getBytes(); 678 // DatagramPacket sendDatagram = new DatagramPacket(sendData, 0, 679 // sendData.length, group, PORT); 680 // socket.send(sendDatagram); 681 // socket.receive(recvDatagram); 682 // String recvMessage = new String(recvData, 0, recvDatagram.getLength()); 683 // assertEquals(message, recvMessage); 684 // 685 // // send & receive longer packet 686 // sendData = longerMessage.getBytes(); 687 // sendDatagram = new DatagramPacket(sendData, 0, sendData.length, 688 // group, PORT); 689 // socket.send(sendDatagram); 690 // socket.receive(recvDatagram); 691 // recvMessage = new String(recvData, 0, recvDatagram.getLength()); 692 // assertEquals(longerMessage, recvMessage); 693 // 694 // // tricky case, added to test compatibility with RI; 695 // // depends on the previous test case 696 // sendData = veryLongMessage.getBytes(); 697 // sendDatagram = new DatagramPacket(sendData, 0, sendData.length, group, 698 // PORT); 699 // socket.send(sendDatagram); 700 // recvDatagram.setLength(recvDatagram.getLength()); // !!! 701 // socket.receive(recvDatagram); 702 // recvMessage = new String(recvData, 0, recvDatagram.getLength()); 703 // assertEquals(longerMessage, recvMessage); 704 // 705 // // tests if received packet is truncated after length was set to 1 706 // sendData = message.getBytes(); 707 // sendDatagram = new DatagramPacket(sendData, 0, sendData.length, 708 // group, PORT); 709 // socket.send(sendDatagram); 710 // recvDatagram.setLength(1); 711 // socket.receive(recvDatagram); 712 // assertEquals("Received message was not truncated", 1, 713 // recvDatagram.getLength()); 714 // assertSame("Received message is invalid", sendData[0], recvData[0]); 715 // 716 // socket.leaveGroup(group); 717 // socket.close(); 718 // } 719 /** 720 * @tests java.net.DatagramSocket#send(java.net.DatagramPacket) 721 */ 722 public void test_sendLjava_net_DatagramPacket() throws Exception { 723 // Test for method void 724 // java.net.DatagramSocket.send(java.net.DatagramPacket) 725 int[] ports = Support_PortManager.getNextPortsForUDP(2); 726 final int portNumber = ports[0]; 727 728 class TestDGSend implements Runnable { 729 Thread pThread; 730 731 public TestDGSend(Thread t) { 732 pThread = t; 733 } 734 735 public void run() { 736 try { 737 byte[] rbuf = new byte[1000]; 738 739 sds = new DatagramSocket(portNumber); 740 DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length); 741 sds.setSoTimeout(6000); 742 sds.receive(sdp); 743 retval = new String(rbuf, 0, testString.length()); 744 pThread.interrupt(); 745 } catch (java.io.InterruptedIOException e) { 746 System.out.println("Recv operation timed out"); 747 pThread.interrupt(); 748 ds.close(); 749 return; 750 } catch (Exception e) { 751 System.out 752 .println("Failed to establish Dgram server: " + e); 753 } 754 } 755 } 756 try { 757 new Thread(new TestDGSend(Thread.currentThread()), "DGServer") 758 .start(); 759 ds = new java.net.DatagramSocket(ports[1]); 760 dp = new DatagramPacket(testString.getBytes(), testString.length(), 761 InetAddress.getLocalHost(), portNumber); 762 // Wait to allow send to occur 763 try { 764 Thread.sleep(500); 765 ds.send(dp); 766 Thread.sleep(5000); 767 } catch (InterruptedException e) { 768 ds.close(); 769 assertTrue("Incorrect data sent: " + retval, retval 770 .equals(testString)); 771 } 772 } finally { 773 ds.close(); 774 } 775 // Regression for HARMONY-1118 776 class testDatagramSocket extends DatagramSocket { 777 public testDatagramSocket(DatagramSocketImpl impl) { 778 super(impl); 779 } 780 } 781 class testDatagramSocketImpl extends DatagramSocketImpl { 782 protected void create() throws SocketException { 783 } 784 785 protected void bind(int arg0, InetAddress arg1) 786 throws SocketException { 787 } 788 789 protected void send(DatagramPacket arg0) throws IOException { 790 } 791 792 protected int peek(InetAddress arg0) throws IOException { 793 return 0; 794 } 795 796 protected int peekData(DatagramPacket arg0) throws IOException { 797 return 0; 798 } 799 800 protected void receive(DatagramPacket arg0) throws IOException { 801 } 802 803 protected void setTTL(byte arg0) throws IOException { 804 } 805 806 protected byte getTTL() throws IOException { 807 return 0; 808 } 809 810 protected void setTimeToLive(int arg0) throws IOException { 811 } 812 813 protected int getTimeToLive() throws IOException { 814 return 0; 815 } 816 817 protected void join(InetAddress arg0) throws IOException { 818 } 819 820 protected void leave(InetAddress arg0) throws IOException { 821 } 822 823 protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) 824 throws IOException { 825 } 826 827 protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) 828 throws IOException { 829 } 830 831 protected void close() { 832 } 833 834 public void setOption(int arg0, Object arg1) throws SocketException { 835 } 836 837 public Object getOption(int arg0) throws SocketException { 838 return null; 839 } 840 } 841 842 // Regression test for Harmony-2938 843 InetAddress i = InetAddress.getByName("127.0.0.1"); 844 DatagramSocket d = new DatagramSocket(0, i); 845 try { 846 d.send(new DatagramPacket(new byte[] { 1 }, 1)); 847 fail("should throw NPE."); 848 } catch (NullPointerException e) { 849 // expected; 850 } finally { 851 d.close(); 852 } 853 854 // Regression test for Harmony-6413 855 InetSocketAddress addr = InetSocketAddress.createUnresolved( 856 "localhost", 0); 857 try { 858 DatagramPacket dp = new DatagramPacket(new byte[272], 3, addr); 859 fail("should throw IllegalArgumentException"); 860 } catch (IllegalArgumentException e) { 861 // expected 862 } 863 } 864 865 /** 866 * If the InetAddress of DatagramPacket is null, DatagramSocket.send(DatagramPacket) 867 * should throw NullPointer Exception. 868 * @tests java.net.DatagramSocket#send(java.net.DatagramPacket) 869 */ 870 public void test_sendLjava_net_DatagramPacket2() throws IOException { 871 int udp_port = 20000; 872 int send_port = 23000; 873 DatagramSocket udpSocket = new DatagramSocket(udp_port); 874 byte[] data = {65}; 875 DatagramPacket sendPacket = new DatagramPacket(data, data.length, null, send_port); 876 try { 877 udpSocket.send(sendPacket); 878 fail("Should throw SocketException"); 879 } catch (NullPointerException e) { 880 // Expected 881 } finally { 882 udpSocket.close(); 883 } 884 885 } 886 887 public void test_setSendBufferSizeI() throws Exception { 888 int portNumber = Support_PortManager.getNextPortForUDP(); 889 ds = new java.net.DatagramSocket(portNumber); 890 ds.setSendBufferSize(134); 891 assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134); 892 } 893 894 public void test_setReceiveBufferSizeI() throws Exception { 895 int portNumber = Support_PortManager.getNextPortForUDP(); 896 ds = new java.net.DatagramSocket(portNumber); 897 ds.setReceiveBufferSize(130); 898 assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130); 899 } 900 901 public void test_setSoTimeoutI() throws Exception { 902 DatagramSocket ds = new DatagramSocket(); 903 ds.setSoTimeout(100); 904 assertTrue("Set incorrect timeout", ds.getSoTimeout() >= 100); 905 } 906 907 public void test_ConstructorLjava_net_DatagramSocketImpl() { 908 class SimpleTestDatagramSocket extends DatagramSocket { 909 public SimpleTestDatagramSocket(DatagramSocketImpl impl) { 910 super(impl); 911 } 912 } 913 914 try { 915 new SimpleTestDatagramSocket((DatagramSocketImpl) null); 916 fail("exception expected"); 917 } catch (NullPointerException ex) { 918 // expected 919 } 920 } 921 922 /** 923 * @tests java.net.DatagramSocket#DatagramSocket(java.net.SocketAddress) 924 */ 925 public void test_ConstructorLjava_net_SocketAddress() throws Exception { 926 class UnsupportedSocketAddress extends SocketAddress { 927 928 public UnsupportedSocketAddress() { 929 } 930 } 931 932 DatagramSocket ds = new DatagramSocket(new InetSocketAddress( 933 InetAddress.getLocalHost(), 0)); 934 assertTrue(ds.getBroadcast()); 935 assertTrue("Created socket with incorrect port", ds.getLocalPort() != 0); 936 assertEquals("Created socket with incorrect address", InetAddress 937 .getLocalHost(), ds.getLocalAddress()); 938 939 try { 940 ds = new java.net.DatagramSocket(new UnsupportedSocketAddress()); 941 fail("No exception when constructing datagramSocket with unsupported SocketAddress type"); 942 } catch (IllegalArgumentException e) { 943 // Expected 944 } 945 946 // regression for HARMONY-894 947 ds = new DatagramSocket((SocketAddress) null); 948 assertTrue(ds.getBroadcast()); 949 } 950 951 /** 952 * @tests java.net.DatagramSocket#bind(java.net.SocketAddress) 953 */ 954 public void test_bindLjava_net_SocketAddress() throws Exception { 955 class mySocketAddress extends SocketAddress { 956 957 public mySocketAddress() { 958 } 959 } 960 961 DatagramServer server = null; 962 963 // now create a socket that is not bound and then bind it 964 int[] ports = Support_PortManager.getNextPortsForUDP(3); 965 int portNumber = ports[0]; 966 int serverPortNumber = ports[1]; 967 DatagramSocket theSocket = new DatagramSocket(new InetSocketAddress( 968 InetAddress.getLocalHost(), portNumber)); 969 970 // validate that the localSocketAddress reflects the address we 971 // bound to 972 assertTrue( 973 "Local address not correct after bind:" 974 + theSocket.getLocalSocketAddress().toString() 975 + "Expected: " 976 + (new InetSocketAddress(InetAddress.getLocalHost(), 977 portNumber)).toString(), theSocket 978 .getLocalSocketAddress().equals( 979 new InetSocketAddress(InetAddress 980 .getLocalHost(), portNumber))); 981 982 // now make sure that datagrams sent from this socket appear to come 983 // from the address we bound to 984 InetAddress localHost = InetAddress.getLocalHost(); 985 portNumber = ports[2]; 986 DatagramSocket ds = new DatagramSocket(null); 987 ds.bind(new InetSocketAddress(localHost, portNumber)); 988 989 server = new DatagramServer(serverPortNumber, localHost); 990 server.start(); 991 Thread.sleep(1000); 992 993 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 994 995 byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 996 DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length); 997 ds.send(send); 998 Thread.sleep(1000); 999 ds.close(); 1000 assertTrue("Address in packet sent does not match address bound to:" 1001 + server.rdp.getAddress() + ":" + server.rdp.getPort() + ":" 1002 + localHost + ":" + portNumber, (server.rdp.getAddress() 1003 .equals(localHost)) 1004 && (server.rdp.getPort() == portNumber)); 1005 1006 // validate if we pass in null that it picks an address for us and 1007 // all is ok 1008 theSocket = new DatagramSocket(null); 1009 theSocket.bind(null); 1010 assertNotNull("Bind with null did not work", theSocket 1011 .getLocalSocketAddress()); 1012 theSocket.close(); 1013 1014 // now check the error conditions 1015 1016 // Address we cannot bind to 1017 theSocket = new DatagramSocket(null); 1018 try { 1019 theSocket.bind(new InetSocketAddress(InetAddress 1020 .getByAddress(Support_Configuration.nonLocalAddressBytes), 1021 Support_PortManager.getNextPortForUDP())); 1022 fail("No exception when binding to bad address"); 1023 } catch (SocketException ex) { 1024 } 1025 theSocket.close(); 1026 1027 // Address that we have allready bound to 1028 ports = Support_PortManager.getNextPortsForUDP(2); 1029 theSocket = new DatagramSocket(null); 1030 DatagramSocket theSocket2 = new DatagramSocket(ports[0]); 1031 try { 1032 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1033 .getLocalHost(), ports[1]); 1034 theSocket.bind(theAddress); 1035 theSocket2.bind(theAddress); 1036 fail("No exception binding to address that is not available"); 1037 } catch (SocketException ex) { 1038 } 1039 theSocket.close(); 1040 theSocket2.close(); 1041 1042 // unsupported SocketAddress subclass 1043 theSocket = new DatagramSocket(null); 1044 try { 1045 theSocket.bind(new mySocketAddress()); 1046 fail("No exception when binding using unsupported SocketAddress subclass"); 1047 } catch (IllegalArgumentException ex) { 1048 } 1049 theSocket.close(); 1050 1051 if (server != null) { 1052 server.stopServer(); 1053 } 1054 } 1055 1056 /** 1057 * @tests java.net.DatagramSocket#connect(java.net.SocketAddress) 1058 */ 1059 public void test_connectLjava_net_SocketAddress() throws Exception { 1060 1061 // validate that we get the PortUnreachable exception if we try to 1062 // send a dgram to a server that is not running and then do a recv 1063 try { 1064 ds = new java.net.DatagramSocket(); 1065 InetAddress inetAddress = InetAddress.getLocalHost(); 1066 int portNumber = Support_PortManager.getNextPortForUDP(); 1067 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1068 DatagramPacket send = new DatagramPacket(new byte[10], 10); 1069 ds.send(send); 1070 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1071 ds.setSoTimeout(10000); 1072 ds.receive(receive); 1073 ds.close(); 1074 fail("No PortUnreachableException when connected at native level on recv "); 1075 } catch (PortUnreachableException e) { 1076 // Expected 1077 } 1078 1079 // validate that we can send/receive with datagram sockets connected at 1080 // the native level 1081 DatagramServer server = null; 1082 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1083 int serverPortNumber = ports[0]; 1084 1085 InetAddress localHost = InetAddress.getLocalHost(); 1086 DatagramSocket ds = new DatagramSocket(ports[1]); 1087 DatagramSocket ds2 = new DatagramSocket(ports[2]); 1088 1089 server = new DatagramServer(serverPortNumber, localHost); 1090 server.start(); 1091 Thread.sleep(1000); 1092 1093 int port = ds.getLocalPort(); 1094 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1095 1096 final byte[] sendBytes = { 'T', 'e', 's', 't', 0 }; 1097 DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length); 1098 ds.send(send); 1099 DatagramPacket receive = new DatagramPacket(new byte[20], 20); 1100 ds.setSoTimeout(2000); 1101 ds.receive(receive); 1102 ds.close(); 1103 assertTrue("Wrong size data received: " + receive.getLength(), receive 1104 .getLength() == sendBytes.length); 1105 assertTrue("Wrong data received" 1106 + new String(receive.getData(), 0, receive.getLength()) + ":" 1107 + new String(sendBytes), new String(receive.getData(), 0, 1108 receive.getLength()).equals(new String(sendBytes))); 1109 assertTrue("Wrong receiver:" + receive.getAddress() + ":" + localHost, 1110 receive.getAddress().equals(localHost)); 1111 1112 if (server != null) { 1113 server.stopServer(); 1114 } 1115 1116 // validate that we can disconnect 1117 try { 1118 ds = new java.net.DatagramSocket(); 1119 InetAddress inetAddress = InetAddress.getLocalHost(); 1120 int portNumber = Support_PortManager.getNextPortForUDP(); 1121 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1122 ds.disconnect(); 1123 ds.close(); 1124 } catch (PortUnreachableException e) { 1125 // Expected 1126 } 1127 1128 // validate that once connected we cannot send to another address 1129 try { 1130 ds = new java.net.DatagramSocket(); 1131 InetAddress inetAddress = InetAddress.getLocalHost(); 1132 int portNumber = Support_PortManager.getNextPortForUDP(); 1133 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1134 DatagramPacket senddp = new DatagramPacket(new byte[10], 10, 1135 inetAddress, portNumber + 1); 1136 ds.send(senddp); 1137 ds.close(); 1138 fail("No Exception when trying to send to a different address on a connected socket "); 1139 } catch (IllegalArgumentException e) { 1140 // Expected 1141 } 1142 1143 // validate that we can connect, then disconnect, then connect then 1144 // send/recv 1145 server = null; 1146 ports = Support_PortManager.getNextPortsForUDP(3); 1147 serverPortNumber = ports[0]; 1148 1149 localHost = InetAddress.getLocalHost(); 1150 ds = new DatagramSocket(ports[1]); 1151 ds2 = new DatagramSocket(ports[2]); 1152 1153 server = new DatagramServer(serverPortNumber, localHost); 1154 server.start(); 1155 Thread.sleep(1000); 1156 1157 port = ds.getLocalPort(); 1158 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1159 ds.disconnect(); 1160 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1161 1162 send = new DatagramPacket(sendBytes, sendBytes.length); 1163 ds.send(send); 1164 receive = new DatagramPacket(new byte[20], 20); 1165 ds.setSoTimeout(2000); 1166 ds.receive(receive); 1167 ds.close(); 1168 assertTrue("connect/disconnect/connect - Wrong size data received: " 1169 + receive.getLength(), receive.getLength() == sendBytes.length); 1170 assertTrue("connect/disconnect/connect - Wrong data received" 1171 + new String(receive.getData(), 0, receive.getLength()) + ":" 1172 + new String(sendBytes), new String(receive.getData(), 0, 1173 receive.getLength()).equals(new String(sendBytes))); 1174 assertTrue("connect/disconnect/connect - Wrong receiver:" 1175 + receive.getAddress() + ":" + localHost, receive.getAddress() 1176 .equals(localHost)); 1177 1178 if (server != null) { 1179 server.stopServer(); 1180 } 1181 1182 // validate that we can connect/disconnect then send/recv to any address 1183 server = null; 1184 ports = Support_PortManager.getNextPortsForUDP(3); 1185 serverPortNumber = ports[0]; 1186 1187 localHost = InetAddress.getLocalHost(); 1188 ds = new DatagramSocket(ports[1]); 1189 ds2 = new DatagramSocket(ports[2]); 1190 1191 server = new DatagramServer(serverPortNumber, localHost); 1192 server.start(); 1193 Thread.sleep(1000); 1194 1195 port = ds.getLocalPort(); 1196 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1197 ds.disconnect(); 1198 1199 send = new DatagramPacket(sendBytes, sendBytes.length, localHost, 1200 serverPortNumber); 1201 ds.send(send); 1202 receive = new DatagramPacket(new byte[20], 20); 1203 ds.setSoTimeout(2000); 1204 ds.receive(receive); 1205 ds.close(); 1206 assertTrue("connect/disconnect - Wrong size data received: " 1207 + receive.getLength(), receive.getLength() == sendBytes.length); 1208 assertTrue("connect/disconnect - Wrong data received" 1209 + new String(receive.getData(), 0, receive.getLength()) + ":" 1210 + new String(sendBytes), new String(receive.getData(), 0, 1211 receive.getLength()).equals(new String(sendBytes))); 1212 assertTrue("connect/disconnect - Wrong receiver:" 1213 + receive.getAddress() + ":" + localHost, receive.getAddress() 1214 .equals(localHost)); 1215 1216 if (server != null) { 1217 server.stopServer(); 1218 } 1219 1220 // validate that we can connect on an allready connected socket and then 1221 // send/recv 1222 server = null; 1223 ports = Support_PortManager.getNextPortsForUDP(3); 1224 serverPortNumber = ports[0]; 1225 1226 localHost = InetAddress.getLocalHost(); 1227 ds = new DatagramSocket(ports[1]); 1228 ds2 = new DatagramSocket(ports[2]); 1229 1230 server = new DatagramServer(serverPortNumber, localHost); 1231 server.start(); 1232 Thread.sleep(1000); 1233 1234 port = ds.getLocalPort(); 1235 ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1)); 1236 ds.connect(new InetSocketAddress(localHost, serverPortNumber)); 1237 1238 byte[] sendTestBytes = { 'T', 'e', 's', 't', 0 }; 1239 send = new DatagramPacket(sendTestBytes, sendTestBytes.length); 1240 ds.send(send); 1241 DatagramPacket receivedp = new DatagramPacket(new byte[20], 20); 1242 ds.setSoTimeout(2000); 1243 ds.receive(receivedp); 1244 ds.close(); 1245 assertTrue("connect/connect - Wrong size data received: " 1246 + receivedp.getLength(), 1247 receivedp.getLength() == sendTestBytes.length); 1248 assertTrue("connect/connect - Wrong data received" 1249 + new String(receivedp.getData(), 0, receivedp.getLength()) 1250 + ":" + new String(sendTestBytes), new String(receivedp 1251 .getData(), 0, receivedp.getLength()).equals(new String( 1252 sendTestBytes))); 1253 assertTrue("connect/connect - Wrong receiver:" + receivedp.getAddress() 1254 + ":" + localHost, receivedp.getAddress().equals(localHost)); 1255 1256 if (server != null) { 1257 server.stopServer(); 1258 } 1259 1260 // test for when we fail to connect at the native level. It seems to 1261 // fail for the any address so we use this. Now to be compatible we 1262 // don't throw the exception but eat it and then act as if we were 1263 // connected at the Java level. 1264 try { 1265 ds = new java.net.DatagramSocket(); 1266 byte[] addressBytes = { 0, 0, 0, 0 }; 1267 InetAddress inetAddress = InetAddress.getByAddress(addressBytes); 1268 int portNumber = Support_PortManager.getNextPortForUDP(); 1269 InetAddress localHostIA = InetAddress.getLocalHost(); 1270 ds.connect(new InetSocketAddress(inetAddress, portNumber)); 1271 assertTrue("Is not connected after connect to inaddr any", ds 1272 .isConnected()); 1273 byte[] sendBytesArray = { 'T', 'e', 's', 't', 0 }; 1274 DatagramPacket senddp = new DatagramPacket(sendBytesArray, 1275 sendBytesArray.length, localHostIA, portNumber); 1276 ds.send(senddp); 1277 fail("No exception when trying to connect at native level with bad address (exception from send) "); 1278 } catch (IllegalArgumentException e) { 1279 // Expected 1280 } 1281 } 1282 1283 /** 1284 * @tests java.net.DatagramSocket#isBound() 1285 */ 1286 public void test_isBound() throws Exception { 1287 InetAddress addr = InetAddress.getLocalHost(); 1288 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1289 int port = ports[0]; 1290 1291 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1292 assertTrue("Socket indicated not bound when it should be (1)", 1293 theSocket.isBound()); 1294 theSocket.close(); 1295 1296 theSocket = new DatagramSocket(new InetSocketAddress(addr, port)); 1297 assertTrue("Socket indicated not bound when it should be (2)", 1298 theSocket.isBound()); 1299 theSocket.close(); 1300 1301 theSocket = new DatagramSocket(null); 1302 assertFalse("Socket indicated bound when it should not be (1)", 1303 theSocket.isBound()); 1304 theSocket.close(); 1305 1306 // connect causes implicit bind 1307 theSocket = new DatagramSocket(null); 1308 theSocket.connect(new InetSocketAddress(addr, port)); 1309 assertTrue("Socket indicated not bound when it should be (3)", 1310 theSocket.isBound()); 1311 theSocket.close(); 1312 1313 // now test when we bind explicitely 1314 InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress 1315 .getLocalHost(), ports[2]); 1316 theSocket = new DatagramSocket(null); 1317 assertFalse("Socket indicated bound when it should not be (2)", 1318 theSocket.isBound()); 1319 theSocket.bind(theLocalAddress); 1320 assertTrue("Socket indicated not bound when it should be (4)", 1321 theSocket.isBound()); 1322 theSocket.close(); 1323 assertTrue("Socket indicated not bound when it should be (5)", 1324 theSocket.isBound()); 1325 } 1326 1327 /** 1328 * @tests java.net.DatagramSocket#isConnected() 1329 */ 1330 public void test_isConnected() throws Exception { 1331 InetAddress addr = InetAddress.getLocalHost(); 1332 int[] ports = Support_PortManager.getNextPortsForUDP(4); 1333 int port = ports[0]; 1334 1335 // base test 1336 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1337 assertFalse("Socket indicated connected when it should not be", 1338 theSocket.isConnected()); 1339 theSocket.connect(new InetSocketAddress(addr, port)); 1340 assertTrue("Socket indicated not connected when it should be", 1341 theSocket.isConnected()); 1342 1343 // reconnect the socket and make sure we get the right answer 1344 theSocket.connect(new InetSocketAddress(addr, ports[2])); 1345 assertTrue("Socket indicated not connected when it should be", 1346 theSocket.isConnected()); 1347 1348 // now disconnect the socket and make sure we get the right answer 1349 theSocket.disconnect(); 1350 assertFalse("Socket indicated connected when it should not be", 1351 theSocket.isConnected()); 1352 theSocket.close(); 1353 1354 // now check behavior when socket is closed when connected 1355 theSocket = new DatagramSocket(ports[3]); 1356 theSocket.connect(new InetSocketAddress(addr, port)); 1357 theSocket.close(); 1358 assertTrue("Socket indicated not connected when it should be", 1359 theSocket.isConnected()); 1360 } 1361 1362 /** 1363 * @tests java.net.DatagramSocket#getRemoteSocketAddress() 1364 */ 1365 public void test_getRemoteSocketAddress() throws Exception { 1366 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1367 int sport = ports[0]; 1368 int portNumber = ports[1]; 1369 DatagramSocket s = new DatagramSocket(new InetSocketAddress(InetAddress 1370 .getLocalHost(), portNumber)); 1371 s.connect(new InetSocketAddress(InetAddress.getLocalHost(), sport)); 1372 assertTrue("Returned incorrect InetSocketAddress(1):" 1373 + s.getLocalSocketAddress().toString(), 1374 s.getRemoteSocketAddress() 1375 .equals( 1376 new InetSocketAddress(InetAddress 1377 .getLocalHost(), sport))); 1378 s.close(); 1379 1380 // now create one that is not connected and validate that we get the 1381 // right answer 1382 DatagramSocket theSocket = new DatagramSocket(null); 1383 portNumber = ports[2]; 1384 theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 1385 portNumber)); 1386 assertNull("Returned incorrect InetSocketAddress -unconnected socket:" 1387 + "Expected: NULL", theSocket.getRemoteSocketAddress()); 1388 1389 // now connect and validate we get the right answer 1390 theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1391 sport)); 1392 assertTrue("Returned incorrect InetSocketAddress(2):" 1393 + theSocket.getRemoteSocketAddress().toString(), 1394 theSocket.getRemoteSocketAddress() 1395 .equals( 1396 new InetSocketAddress(InetAddress 1397 .getLocalHost(), sport))); 1398 theSocket.close(); 1399 } 1400 1401 public void test_getLocalSocketAddress_late_bind() throws Exception { 1402 // An unbound socket should return null as its local address. 1403 DatagramSocket theSocket = new DatagramSocket((SocketAddress) null); 1404 assertNull(theSocket.getLocalSocketAddress()); 1405 1406 // now bind the socket and make sure we get the right answer 1407 int portNumber = Support_PortManager.getNextPortForUDP(); 1408 InetSocketAddress localAddress = new InetSocketAddress(InetAddress.getLocalHost(), portNumber); 1409 theSocket.bind(localAddress); 1410 assertEquals(localAddress, theSocket.getLocalSocketAddress()); 1411 theSocket.close(); 1412 } 1413 1414 public void test_getLocalSocketAddress_unbound() throws Exception { 1415 int portNumber = Support_PortManager.getNextPortForUDP(); 1416 InetSocketAddress localAddress1 = new InetSocketAddress(InetAddress.getLocalHost(), portNumber); 1417 DatagramSocket s = new DatagramSocket(localAddress1); 1418 assertEquals(localAddress1, s.getLocalSocketAddress()); 1419 s.close(); 1420 1421 InetSocketAddress remoteAddress = (InetSocketAddress) s.getRemoteSocketAddress(); 1422 assertNull(remoteAddress); 1423 } 1424 1425 public void test_getLocalSocketAddress_ANY() throws Exception { 1426 DatagramSocket s = new DatagramSocket(0); 1427 try { 1428 assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), 1429 ((InetSocketAddress) s.getLocalSocketAddress()).getAddress() instanceof Inet6Address); 1430 } finally { 1431 s.close(); 1432 } 1433 } 1434 1435 public void test_setReuseAddressZ() throws Exception { 1436 // test case were we set it to false 1437 DatagramSocket theSocket1 = null; 1438 DatagramSocket theSocket2 = null; 1439 try { 1440 InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager.getNextPortForUDP()); 1441 theSocket1 = new DatagramSocket(null); 1442 theSocket2 = new DatagramSocket(null); 1443 theSocket1.setReuseAddress(false); 1444 theSocket2.setReuseAddress(false); 1445 theSocket1.bind(theAddress); 1446 theSocket2.bind(theAddress); 1447 fail("No exception when trying to connect to do duplicate socket bind with re-useaddr set to false"); 1448 } catch (BindException expected) { 1449 } 1450 if (theSocket1 != null) { 1451 theSocket1.close(); 1452 } 1453 if (theSocket2 != null) { 1454 theSocket2.close(); 1455 } 1456 1457 // test case were we set it to true 1458 InetSocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager.getNextPortForUDP()); 1459 theSocket1 = new DatagramSocket(null); 1460 theSocket2 = new DatagramSocket(null); 1461 theSocket1.setReuseAddress(true); 1462 theSocket2.setReuseAddress(true); 1463 theSocket1.bind(theAddress); 1464 theSocket2.bind(theAddress); 1465 1466 if (theSocket1 != null) { 1467 theSocket1.close(); 1468 } 1469 if (theSocket2 != null) { 1470 theSocket2.close(); 1471 } 1472 1473 // test the default case which we expect to be the same on all 1474 // platforms 1475 try { 1476 theAddress = new InetSocketAddress(InetAddress.getLocalHost(),Support_PortManager.getNextPortForUDP()); 1477 theSocket1 = new DatagramSocket(null); 1478 theSocket2 = new DatagramSocket(null); 1479 theSocket1.bind(theAddress); 1480 theSocket2.bind(theAddress); 1481 fail("No exception when trying to connect to do duplicate socket bind with re-useaddr left as default"); 1482 } catch (BindException expected) { 1483 } 1484 if (theSocket1 != null) { 1485 theSocket1.close(); 1486 } 1487 if (theSocket2 != null) { 1488 theSocket2.close(); 1489 } 1490 } 1491 1492 public void test_getReuseAddress() throws Exception { 1493 DatagramSocket theSocket = new DatagramSocket(); 1494 theSocket.setReuseAddress(true); 1495 assertTrue("getReuseAddress false when it should be true", theSocket.getReuseAddress()); 1496 theSocket.setReuseAddress(false); 1497 assertFalse("getReuseAddress true when it should be False", theSocket.getReuseAddress()); 1498 } 1499 1500 public void test_setBroadcastZ() throws Exception { 1501 int[] ports = Support_PortManager.getNextPortsForUDP(3); 1502 DatagramSocket theSocket = new DatagramSocket(ports[0]); 1503 theSocket.setBroadcast(false); 1504 byte theBytes[] = { -1, -1, -1, -1 }; 1505 1506 // validate we cannot connect to the broadcast address when 1507 // setBroadcast is false 1508 try { 1509 theSocket.connect(new InetSocketAddress(InetAddress.getByAddress(theBytes), ports[1])); 1510 assertFalse("No exception when connecting to broadcast address with setBroadcast(false)", theSocket.getBroadcast()); 1511 } catch (Exception ex) { 1512 } 1513 1514 // now validate that we can connect to the broadcast address when 1515 // setBroadcast is true 1516 theSocket.setBroadcast(true); 1517 theSocket.connect(new InetSocketAddress(InetAddress.getByAddress(theBytes), ports[2])); 1518 } 1519 1520 public void test_getBroadcast() throws Exception { 1521 DatagramSocket theSocket = new DatagramSocket(); 1522 theSocket.setBroadcast(true); 1523 assertTrue("getBroadcast false when it should be true", theSocket.getBroadcast()); 1524 theSocket.setBroadcast(false); 1525 assertFalse("getBroadcast true when it should be False", theSocket.getBroadcast()); 1526 } 1527 1528 public void test_setTrafficClassI() throws Exception { 1529 int IPTOS_LOWCOST = 0x2; 1530 int IPTOS_RELIABILTY = 0x4; 1531 int IPTOS_THROUGHPUT = 0x8; 1532 int IPTOS_LOWDELAY = 0x10; 1533 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1534 1535 new InetSocketAddress(InetAddress.getLocalHost(), ports[0]); 1536 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1537 1538 // validate that value set must be between 0 and 255 1539 try { 1540 theSocket.setTrafficClass(256); 1541 fail("No exception when traffic class set to 256"); 1542 } catch (IllegalArgumentException e) { 1543 } 1544 1545 try { 1546 theSocket.setTrafficClass(-1); 1547 fail("No exception when traffic class set to -1"); 1548 } catch (IllegalArgumentException e) { 1549 } 1550 1551 // now validate that we can set it to some good values 1552 theSocket.setTrafficClass(IPTOS_LOWCOST); 1553 theSocket.setTrafficClass(IPTOS_THROUGHPUT); 1554 } 1555 1556 public void test_getTrafficClass() throws Exception { 1557 int IPTOS_LOWCOST = 0x2; 1558 int IPTOS_RELIABILTY = 0x4; 1559 int IPTOS_THROUGHPUT = 0x8; 1560 int IPTOS_LOWDELAY = 0x10; 1561 int[] ports = Support_PortManager.getNextPortsForUDP(2); 1562 1563 new InetSocketAddress(InetAddress.getLocalHost(), ports[0]); 1564 DatagramSocket theSocket = new DatagramSocket(ports[1]); 1565 1566 /* 1567 * we cannot actually check that the values are set as if a platform 1568 * does not support the option then it may come back unset even 1569 * though we set it so just get the value to make sure we can get it 1570 */ 1571 int trafficClass = theSocket.getTrafficClass(); 1572 } 1573 1574 public void test_isClosed() throws Exception { 1575 DatagramSocket theSocket = new DatagramSocket(); 1576 1577 // validate isClosed returns expected values 1578 assertFalse("Socket should indicate it is not closed(1):", theSocket 1579 .isClosed()); 1580 theSocket.close(); 1581 assertTrue("Socket should indicate it is not closed(1):", theSocket 1582 .isClosed()); 1583 1584 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 1585 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 1586 theSocket = new DatagramSocket(theAddress); 1587 assertFalse("Socket should indicate it is not closed(2):", theSocket 1588 .isClosed()); 1589 theSocket.close(); 1590 assertTrue("Socket should indicate it is not closed(2):", theSocket 1591 .isClosed()); 1592 } 1593 1594 /** 1595 * @tests java.net.DatagramSocket#getChannel() 1596 */ 1597 public void test_getChannel() throws SocketException { 1598 assertNull(new DatagramSocket().getChannel()); 1599 } 1600 1601 /** 1602 * Sets up the fixture, for example, open a network connection. This method 1603 * is called before a test is executed. 1604 */ 1605 protected void setUp() { 1606 retval = "Bogus retval"; 1607 } 1608 1609 /** 1610 * Tears down the fixture, for example, close a network connection. This 1611 * method is called after a test is executed. 1612 */ 1613 protected void tearDown() { 1614 try { 1615 ds.close(); 1616 sds.close(); 1617 } catch (Exception e) { 1618 } 1619 } 1620 1621 protected void receive_oversize_java_net_DatagramPacket() { 1622 final int[] ports = Support_PortManager.getNextPortsForUDP(2); 1623 final int portNumber = ports[0]; 1624 1625 class TestDGRcvOver implements Runnable { 1626 public void run() { 1627 InetAddress localHost = null; 1628 try { 1629 localHost = InetAddress.getLocalHost(); 1630 Thread.sleep(1000); 1631 DatagramSocket sds = new DatagramSocket(ports[1]); 1632 DatagramPacket rdp = new DatagramPacket("0123456789" 1633 .getBytes(), 10, localHost, portNumber); 1634 sds.send(rdp); 1635 sds.close(); 1636 } catch (Exception e) { 1637 System.err.println("host " + localHost + " port " 1638 + portNumber + " failed to send oversize data: " 1639 + e); 1640 e.printStackTrace(); 1641 } 1642 } 1643 } 1644 1645 try { 1646 new Thread(new TestDGRcvOver(), "DGSenderOver").start(); 1647 ds = new java.net.DatagramSocket(portNumber); 1648 ds.setSoTimeout(6000); 1649 byte rbuf[] = new byte[5]; 1650 DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length); 1651 ; 1652 ds.receive(rdp); 1653 ds.close(); 1654 assertTrue("Send/Receive oversize failed to return correct data: " 1655 + new String(rbuf, 0, 5), new String(rbuf, 0, 5) 1656 .equals("01234")); 1657 } catch (Exception e) { 1658 System.err.println("Exception during send test: " + e); 1659 e.printStackTrace(); 1660 fail("port " + portNumber + " Exception: " + e 1661 + " during oversize send test"); 1662 } finally { 1663 ds.close(); 1664 } 1665 } 1666} 1667