/external/apache-harmony/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/DatagramSocketTest.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 1304 lines · 944 code · 166 blank · 194 comment · 44 complexity · 0a6303c0535c74a32b3b1666d22a0601 MD5 · raw file

  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. package org.apache.harmony.luni.tests.java.net;
  18. import java.io.IOException;
  19. import java.io.InterruptedIOException;
  20. import java.net.BindException;
  21. import java.net.DatagramPacket;
  22. import java.net.DatagramSocket;
  23. import java.net.DatagramSocketImpl;
  24. import java.net.DatagramSocketImplFactory;
  25. import java.net.Inet4Address;
  26. import java.net.Inet6Address;
  27. import java.net.InetAddress;
  28. import java.net.InetSocketAddress;
  29. import java.net.NetworkInterface;
  30. import java.net.PortUnreachableException;
  31. import java.net.SocketAddress;
  32. import java.net.SocketException;
  33. import java.net.UnknownHostException;
  34. import java.util.Date;
  35. import tests.support.Support_Configuration;
  36. import tests.support.Support_PortManager;
  37. public class DatagramSocketTest extends junit.framework.TestCase {
  38. java.net.DatagramSocket ds;
  39. java.net.DatagramPacket dp;
  40. DatagramSocket sds = null;
  41. String retval;
  42. String testString = "Test String";
  43. boolean interrupted;
  44. class DatagramServer extends Thread {
  45. public DatagramSocket ms;
  46. boolean running = true;
  47. public volatile byte[] rbuf = new byte[512];
  48. volatile DatagramPacket rdp = null;
  49. public void run() {
  50. try {
  51. while (running) {
  52. try {
  53. ms.receive(rdp);
  54. // echo the packet back
  55. ms.send(rdp);
  56. } catch (java.io.InterruptedIOException e) {
  57. Thread.yield();
  58. }
  59. ;
  60. }
  61. ;
  62. } catch (java.io.IOException e) {
  63. System.out.println("DatagramServer server failed: " + e);
  64. } finally {
  65. ms.close();
  66. }
  67. }
  68. public void stopServer() {
  69. running = false;
  70. }
  71. public DatagramServer(int aPort, InetAddress address)
  72. throws IOException {
  73. rbuf = new byte[512];
  74. rbuf[0] = -1;
  75. rdp = new DatagramPacket(rbuf, rbuf.length);
  76. ms = new DatagramSocket(aPort, address);
  77. ms.setSoTimeout(2000);
  78. }
  79. }
  80. /**
  81. * @tests java.net.DatagramSocket#DatagramSocket()
  82. */
  83. public void test_Constructor() throws SocketException {
  84. new DatagramSocket();
  85. }
  86. /**
  87. * @tests java.net.DatagramSocket#DatagramSocket(int)
  88. */
  89. public void test_ConstructorI() throws SocketException {
  90. DatagramSocket ds = new DatagramSocket(0);
  91. ds.close();
  92. }
  93. /**
  94. * @tests java.net.DatagramSocket#DatagramSocket(int, java.net.InetAddress)
  95. */
  96. public void test_ConstructorILjava_net_InetAddress() throws IOException {
  97. DatagramSocket ds = new DatagramSocket(0, InetAddress.getLocalHost());
  98. assertTrue("Created socket with incorrect port", ds.getLocalPort() != 0);
  99. assertEquals("Created socket with incorrect address", InetAddress
  100. .getLocalHost(), ds.getLocalAddress());
  101. }
  102. /**
  103. * @tests java.net.DatagramSocket#close()
  104. */
  105. public void test_close() throws UnknownHostException, SocketException {
  106. DatagramSocket ds = new DatagramSocket(0);
  107. DatagramPacket dp = new DatagramPacket("Test String".getBytes(), 11,
  108. InetAddress.getLocalHost(), 0);
  109. ds.close();
  110. try {
  111. ds.send(dp);
  112. fail("Data sent after close");
  113. } catch (IOException e) {
  114. // Expected
  115. }
  116. }
  117. public void test_connectLjava_net_InetAddressI() throws Exception {
  118. DatagramSocket ds = new DatagramSocket();
  119. InetAddress inetAddress = InetAddress.getLocalHost();
  120. ds.connect(inetAddress, 0);
  121. assertEquals("Incorrect InetAddress", inetAddress, ds.getInetAddress());
  122. assertEquals("Incorrect Port", 0, ds.getPort());
  123. ds.disconnect();
  124. ds = new java.net.DatagramSocket();
  125. inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4");
  126. int portNumber = Support_PortManager.getNextPortForUDP();
  127. ds.connect(inetAddress, portNumber);
  128. assertTrue("Incorrect InetAddress", ds.getInetAddress().equals(inetAddress));
  129. assertTrue("Incorrect Port", ds.getPort() == portNumber);
  130. ds.disconnect();
  131. // Create a connected datagram socket to test
  132. // PlainDatagramSocketImpl.peek()
  133. InetAddress localHost = InetAddress.getLocalHost();
  134. ds = new DatagramSocket();
  135. int port = ds.getLocalPort();
  136. ds.connect(localHost, port);
  137. DatagramPacket send = new DatagramPacket(new byte[10], 10, localHost,
  138. port);
  139. ds.send(send);
  140. DatagramPacket receive = new DatagramPacket(new byte[20], 20);
  141. ds.setSoTimeout(2000);
  142. ds.receive(receive);
  143. ds.close();
  144. assertTrue("Wrong size: " + receive.getLength(),
  145. receive.getLength() == 10);
  146. assertTrue("Wrong receiver", receive.getAddress().equals(localHost));
  147. class DatagramServer extends Thread {
  148. public DatagramSocket ms;
  149. boolean running = true;
  150. public byte[] rbuf = new byte[512];
  151. DatagramPacket rdp = null;
  152. public void run() {
  153. try {
  154. while (running) {
  155. try {
  156. ms.receive(rdp);
  157. // echo the packet back
  158. ms.send(rdp);
  159. } catch (java.io.InterruptedIOException e) {
  160. Thread.yield();
  161. }
  162. ;
  163. }
  164. ;
  165. } catch (java.io.IOException e) {
  166. System.out.println("Multicast server failed: " + e);
  167. } finally {
  168. ms.close();
  169. }
  170. }
  171. public void stopServer() {
  172. running = false;
  173. }
  174. public DatagramServer(int aPort, InetAddress address)
  175. throws java.io.IOException {
  176. rbuf = new byte[512];
  177. rbuf[0] = -1;
  178. rdp = new DatagramPacket(rbuf, rbuf.length);
  179. ms = new DatagramSocket(aPort, address);
  180. ms.setSoTimeout(2000);
  181. }
  182. }
  183. // validate that we get the PortUnreachable exception if we try to
  184. // send a dgram to a server that is not running and then do a recv
  185. try {
  186. ds = new java.net.DatagramSocket();
  187. inetAddress = InetAddress.getLocalHost();
  188. portNumber = Support_PortManager.getNextPortForUDP();
  189. ds.connect(inetAddress, portNumber);
  190. send = new DatagramPacket(new byte[10], 10);
  191. ds.send(send);
  192. receive = new DatagramPacket(new byte[20], 20);
  193. ds.setSoTimeout(10000);
  194. ds.receive(receive);
  195. ds.close();
  196. fail("No PortUnreachableException when connected at native level on recv ");
  197. } catch (PortUnreachableException e) {
  198. // Expected
  199. }
  200. // validate that we can send/receive with datagram sockets connected at
  201. // the native level
  202. DatagramServer server = null;
  203. int[] ports = Support_PortManager.getNextPortsForUDP(3);
  204. int serverPortNumber = ports[0];
  205. localHost = InetAddress.getLocalHost();
  206. ds = new DatagramSocket(ports[1]);
  207. DatagramSocket ds2 = new DatagramSocket(ports[2]);
  208. server = new DatagramServer(serverPortNumber, localHost);
  209. server.start();
  210. Thread.sleep(1000);
  211. port = ds.getLocalPort();
  212. ds.connect(localHost, serverPortNumber);
  213. final byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
  214. send = new DatagramPacket(sendBytes, sendBytes.length);
  215. ds.send(send);
  216. receive = new DatagramPacket(new byte[20], 20);
  217. ds.setSoTimeout(2000);
  218. ds.receive(receive);
  219. ds.close();
  220. assertTrue("Wrong size data received: " + receive.getLength(), receive
  221. .getLength() == sendBytes.length);
  222. assertTrue("Wrong data received"
  223. + new String(receive.getData(), 0, receive.getLength()) + ":"
  224. + new String(sendBytes), new String(receive.getData(), 0,
  225. receive.getLength()).equals(new String(sendBytes)));
  226. assertTrue("Wrong receiver:" + receive.getAddress() + ":" + localHost,
  227. receive.getAddress().equals(localHost));
  228. if (server != null) {
  229. server.stopServer();
  230. }
  231. // validate that we can disconnect
  232. try {
  233. ds = new java.net.DatagramSocket();
  234. inetAddress = InetAddress.getLocalHost();
  235. portNumber = Support_PortManager.getNextPortForUDP();
  236. ds.connect(inetAddress, portNumber);
  237. ds.disconnect();
  238. ds.close();
  239. } catch (PortUnreachableException e) {
  240. // Expected
  241. }
  242. // validate that once connected we cannot send to another address
  243. try {
  244. ds = new java.net.DatagramSocket();
  245. inetAddress = InetAddress.getLocalHost();
  246. portNumber = Support_PortManager.getNextPortForUDP();
  247. ds.connect(inetAddress, portNumber);
  248. send = new DatagramPacket(new byte[10], 10, inetAddress,
  249. portNumber + 1);
  250. ds.send(send);
  251. ds.close();
  252. fail("No Exception when trying to send to a different address on a connected socket ");
  253. } catch (IllegalArgumentException e) {
  254. // Expected
  255. }
  256. // validate that we can connect, then disconnect, then connect then
  257. // send/recv
  258. server = null;
  259. ports = Support_PortManager.getNextPortsForUDP(3);
  260. serverPortNumber = ports[0];
  261. localHost = InetAddress.getLocalHost();
  262. ds = new DatagramSocket(ports[1]);
  263. ds2 = new DatagramSocket(ports[2]);
  264. server = new DatagramServer(serverPortNumber, localHost);
  265. server.start();
  266. Thread.sleep(1000);
  267. port = ds.getLocalPort();
  268. ds.connect(localHost, serverPortNumber + 1);
  269. ds.disconnect();
  270. ds.connect(localHost, serverPortNumber);
  271. send = new DatagramPacket(sendBytes, sendBytes.length);
  272. ds.send(send);
  273. receive = new DatagramPacket(new byte[20], 20);
  274. ds.setSoTimeout(2000);
  275. ds.receive(receive);
  276. ds.close();
  277. assertTrue("connect/disconnect/connect - Wrong size data received: "
  278. + receive.getLength(), receive.getLength() == sendBytes.length);
  279. assertTrue("connect/disconnect/connect - Wrong data received"
  280. + new String(receive.getData(), 0, receive.getLength()) + ":"
  281. + new String(sendBytes), new String(receive.getData(), 0,
  282. receive.getLength()).equals(new String(sendBytes)));
  283. assertTrue("connect/disconnect/connect - Wrong receiver:"
  284. + receive.getAddress() + ":" + localHost, receive.getAddress()
  285. .equals(localHost));
  286. if (server != null) {
  287. server.stopServer();
  288. }
  289. // validate that we can connect/disconnect then send/recv to any address
  290. server = null;
  291. ports = Support_PortManager.getNextPortsForUDP(3);
  292. serverPortNumber = ports[0];
  293. localHost = InetAddress.getLocalHost();
  294. ds = new DatagramSocket(ports[1]);
  295. ds2 = new DatagramSocket(ports[2]);
  296. server = new DatagramServer(serverPortNumber, localHost);
  297. server.start();
  298. Thread.sleep(1000);
  299. port = ds.getLocalPort();
  300. ds.connect(localHost, serverPortNumber + 1);
  301. ds.disconnect();
  302. send = new DatagramPacket(sendBytes, sendBytes.length, localHost,
  303. serverPortNumber);
  304. ds.send(send);
  305. receive = new DatagramPacket(new byte[20], 20);
  306. ds.setSoTimeout(2000);
  307. ds.receive(receive);
  308. ds.close();
  309. assertTrue("connect/disconnect - Wrong size data received: "
  310. + receive.getLength(), receive.getLength() == sendBytes.length);
  311. assertTrue("connect/disconnect - Wrong data received"
  312. + new String(receive.getData(), 0, receive.getLength()) + ":"
  313. + new String(sendBytes), new String(receive.getData(), 0,
  314. receive.getLength()).equals(new String(sendBytes)));
  315. assertTrue("connect/disconnect - Wrong receiver:"
  316. + receive.getAddress() + ":" + localHost, receive.getAddress()
  317. .equals(localHost));
  318. if (server != null) {
  319. server.stopServer();
  320. }
  321. // validate that we can connect on an allready connected socket and then
  322. // send/recv
  323. server = null;
  324. ports = Support_PortManager.getNextPortsForUDP(3);
  325. serverPortNumber = ports[0];
  326. localHost = InetAddress.getLocalHost();
  327. ds = new DatagramSocket(ports[1]);
  328. ds2 = new DatagramSocket(ports[2]);
  329. server = new DatagramServer(serverPortNumber, localHost);
  330. server.start();
  331. Thread.sleep(1000);
  332. port = ds.getLocalPort();
  333. ds.connect(localHost, serverPortNumber + 1);
  334. ds.connect(localHost, serverPortNumber);
  335. send = new DatagramPacket(sendBytes, sendBytes.length);
  336. ds.send(send);
  337. receive = new DatagramPacket(new byte[20], 20);
  338. ds.setSoTimeout(2000);
  339. ds.receive(receive);
  340. ds.close();
  341. assertTrue("connect/connect - Wrong size data received: "
  342. + receive.getLength(), receive.getLength() == sendBytes.length);
  343. assertTrue("connect/connect - Wrong data received"
  344. + new String(receive.getData(), 0, receive.getLength()) + ":"
  345. + new String(sendBytes), new String(receive.getData(), 0,
  346. receive.getLength()).equals(new String(sendBytes)));
  347. assertTrue("connect/connect - Wrong receiver:" + receive.getAddress()
  348. + ":" + localHost, receive.getAddress().equals(localHost));
  349. if (server != null) {
  350. server.stopServer();
  351. }
  352. // test for when we fail to connect at the native level. Even though we
  353. // fail at the native level there is no way to return an exception so
  354. // there should be no exception
  355. ds = new java.net.DatagramSocket();
  356. byte[] addressBytes = { 0, 0, 0, 0 };
  357. inetAddress = InetAddress.getByAddress(addressBytes);
  358. portNumber = Support_PortManager.getNextPortForUDP();
  359. ds.connect(inetAddress, portNumber);
  360. if ("true".equals(System.getProperty("run.ipv6tests"))) {
  361. System.out
  362. .println("Running test_connectLjava_net_InetAddressI(DatagramSocketTest) with IPv6 address");
  363. ds = new java.net.DatagramSocket();
  364. byte[] addressTestBytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  365. 0, 0, 0 };
  366. inetAddress = InetAddress.getByAddress(addressTestBytes);
  367. portNumber = Support_PortManager.getNextPortForUDP();
  368. ds.connect(inetAddress, portNumber);
  369. }
  370. }
  371. public void test_disconnect() throws Exception {
  372. DatagramSocket ds = new DatagramSocket();
  373. InetAddress inetAddress = InetAddress.getLocalHost();
  374. ds.connect(inetAddress, 0);
  375. ds.disconnect();
  376. assertNull("Incorrect InetAddress", ds.getInetAddress());
  377. assertEquals("Incorrect Port", -1, ds.getPort());
  378. ds = new DatagramSocket();
  379. inetAddress = InetAddress.getByName("FE80:0000:0000:0000:020D:60FF:FE0F:A776%4");
  380. ds.connect(inetAddress, 0);
  381. ds.disconnect();
  382. assertNull("Incorrect InetAddress", ds.getInetAddress());
  383. assertEquals("Incorrect Port", -1, ds.getPort());
  384. }
  385. /**
  386. * @tests java.net.DatagramSocket#getInetAddress()
  387. */
  388. public void test_getInetAddress() {
  389. assertTrue("Used to test", true);
  390. }
  391. public void test_getLocalAddress() throws Exception {
  392. // Test for method java.net.InetAddress
  393. // java.net.DatagramSocket.getLocalAddress()
  394. int portNumber = Support_PortManager.getNextPortForUDP();
  395. InetAddress local = InetAddress.getLocalHost();
  396. ds = new java.net.DatagramSocket(portNumber, local);
  397. assertEquals(InetAddress.getByName(InetAddress.getLocalHost().getHostName()), ds.getLocalAddress());
  398. // now check behavior when the ANY address is returned
  399. DatagramSocket s = new DatagramSocket(0);
  400. assertTrue("ANY address not IPv6: " + s.getLocalSocketAddress(), s.getLocalAddress() instanceof Inet6Address);
  401. s.close();
  402. }
  403. /**
  404. * @tests java.net.DatagramSocket#getLocalPort()
  405. */
  406. public void test_getLocalPort() throws SocketException {
  407. DatagramSocket ds = new DatagramSocket();
  408. assertTrue("Returned incorrect port", ds.getLocalPort() != 0);
  409. }
  410. /**
  411. * @tests java.net.DatagramSocket#getPort()
  412. */
  413. public void test_getPort() throws IOException {
  414. DatagramSocket theSocket = new DatagramSocket();
  415. assertEquals("Expected -1 for remote port as not connected", -1,
  416. theSocket.getPort());
  417. // Now connect the socket and validate that we get the right port
  418. int portNumber = 49152; // any valid port, even if it is unreachable
  419. theSocket.connect(InetAddress.getLocalHost(), portNumber);
  420. assertEquals("getPort returned wrong value", portNumber, theSocket
  421. .getPort());
  422. }
  423. public void test_getReceiveBufferSize() throws Exception {
  424. DatagramSocket ds = new DatagramSocket();
  425. ds.setReceiveBufferSize(130);
  426. assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130);
  427. }
  428. public void test_getSendBufferSize() throws Exception {
  429. int portNumber = Support_PortManager.getNextPortForUDP();
  430. ds = new java.net.DatagramSocket(portNumber);
  431. ds.setSendBufferSize(134);
  432. assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134);
  433. }
  434. public void test_getSoTimeout() throws Exception {
  435. DatagramSocket ds = new DatagramSocket();
  436. ds.setSoTimeout(100);
  437. assertEquals("Returned incorrect timeout", 100, ds.getSoTimeout());
  438. }
  439. public void test_receiveLjava_net_DatagramPacket() throws IOException {
  440. // Test for method void
  441. // java.net.DatagramSocket.receive(java.net.DatagramPacket)
  442. receive_oversize_java_net_DatagramPacket();
  443. final int[] ports = Support_PortManager.getNextPortsForUDP(2);
  444. final int portNumber = ports[0];
  445. class TestDGRcv implements Runnable {
  446. public void run() {
  447. InetAddress localHost = null;
  448. try {
  449. localHost = InetAddress.getLocalHost();
  450. Thread.sleep(1000);
  451. DatagramSocket sds = new DatagramSocket(ports[1]);
  452. DatagramPacket rdp = new DatagramPacket("Test String"
  453. .getBytes(), 11, localHost, portNumber);
  454. sds.send(rdp);
  455. sds.close();
  456. } catch (Exception e) {
  457. System.err.println("host " + localHost + " port "
  458. + portNumber + " failed to send data: " + e);
  459. e.printStackTrace();
  460. }
  461. }
  462. }
  463. try {
  464. new Thread(new TestDGRcv(), "DGSender").start();
  465. ds = new java.net.DatagramSocket(portNumber);
  466. ds.setSoTimeout(6000);
  467. byte rbuf[] = new byte[1000];
  468. DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
  469. ds.receive(rdp);
  470. ds.close();
  471. assertTrue("Send/Receive failed to return correct data: "
  472. + new String(rbuf, 0, 11), new String(rbuf, 0, 11)
  473. .equals("Test String"));
  474. } finally {
  475. ds.close();
  476. }
  477. try {
  478. interrupted = false;
  479. final DatagramSocket ds = new DatagramSocket();
  480. ds.setSoTimeout(12000);
  481. Runnable runnable = new Runnable() {
  482. public void run() {
  483. try {
  484. ds.receive(new DatagramPacket(new byte[1], 1));
  485. } catch (InterruptedIOException e) {
  486. interrupted = true;
  487. } catch (IOException e) {
  488. }
  489. }
  490. };
  491. Thread thread = new Thread(runnable, "DatagramSocket.receive1");
  492. thread.start();
  493. try {
  494. do {
  495. Thread.sleep(500);
  496. } while (!thread.isAlive());
  497. } catch (InterruptedException e) {
  498. }
  499. ds.close();
  500. int c = 0;
  501. do {
  502. try {
  503. Thread.sleep(500);
  504. } catch (InterruptedException e) {
  505. }
  506. if (interrupted) {
  507. fail("received interrupt");
  508. }
  509. if (++c > 4) {
  510. fail("read call did not exit");
  511. }
  512. } while (thread.isAlive());
  513. interrupted = false;
  514. int[] ports1 = Support_PortManager.getNextPortsForUDP(2);
  515. final int portNum = ports[0];
  516. final DatagramSocket ds2 = new DatagramSocket(ports[1]);
  517. ds2.setSoTimeout(12000);
  518. Runnable runnable2 = new Runnable() {
  519. public void run() {
  520. try {
  521. ds2.receive(new DatagramPacket(new byte[1], 1,
  522. InetAddress.getLocalHost(), portNum));
  523. } catch (InterruptedIOException e) {
  524. interrupted = true;
  525. } catch (IOException e) {
  526. }
  527. }
  528. };
  529. Thread thread2 = new Thread(runnable2, "DatagramSocket.receive2");
  530. thread2.start();
  531. try {
  532. do {
  533. Thread.sleep(500);
  534. } while (!thread2.isAlive());
  535. } catch (InterruptedException e) {
  536. }
  537. ds2.close();
  538. int c2 = 0;
  539. do {
  540. try {
  541. Thread.sleep(500);
  542. } catch (InterruptedException e) {
  543. }
  544. if (interrupted) {
  545. fail("receive2 was interrupted");
  546. }
  547. if (++c2 > 4) {
  548. fail("read2 call did not exit");
  549. }
  550. } while (thread2.isAlive());
  551. interrupted = false;
  552. DatagramSocket ds3 = new DatagramSocket();
  553. ds3.setSoTimeout(500);
  554. Date start = new Date();
  555. try {
  556. ds3.receive(new DatagramPacket(new byte[1], 1));
  557. } catch (InterruptedIOException e) {
  558. interrupted = true;
  559. }
  560. ds3.close();
  561. assertTrue("receive not interrupted", interrupted);
  562. int delay = (int) (new Date().getTime() - start.getTime());
  563. assertTrue("timeout too soon: " + delay, delay >= 490);
  564. } catch (IOException e) {
  565. fail("Unexpected IOException : " + e.getMessage());
  566. }
  567. }
  568. /**
  569. * Tests receive() method in various combinations with
  570. * DatagramPacket#getLength() and DatagramPacket#getLength(). This is
  571. * regression test for HARMONY-2276.
  572. *
  573. * @throws IOException
  574. * if some I/O error occured
  575. */
  576. // public void test2276() throws IOException {
  577. // final String ADDRESS = "239.255.2.3";
  578. // final int PORT = Support_PortManager.getNextPortForUDP();
  579. // InetAddress group = InetAddress.getByName(ADDRESS);
  580. // MulticastSocket socket = new MulticastSocket(PORT);
  581. // byte[] recvData = new byte[100];
  582. // DatagramPacket recvDatagram = new DatagramPacket(recvData,
  583. // recvData.length);
  584. //
  585. // String message = "Hello, world!";
  586. // String longerMessage = message + " again.";
  587. // String veryLongMessage = longerMessage + " Forever!";
  588. //
  589. // socket.joinGroup(group);
  590. // socket.setSoTimeout(5000); // prevent eternal block in
  591. // // socket.receive()
  592. // // send & recieve packet
  593. // byte[] sendData = message.getBytes();
  594. // DatagramPacket sendDatagram = new DatagramPacket(sendData, 0,
  595. // sendData.length, group, PORT);
  596. // socket.send(sendDatagram);
  597. // socket.receive(recvDatagram);
  598. // String recvMessage = new String(recvData, 0, recvDatagram.getLength());
  599. // assertEquals(message, recvMessage);
  600. //
  601. // // send & receive longer packet
  602. // sendData = longerMessage.getBytes();
  603. // sendDatagram = new DatagramPacket(sendData, 0, sendData.length,
  604. // group, PORT);
  605. // socket.send(sendDatagram);
  606. // socket.receive(recvDatagram);
  607. // recvMessage = new String(recvData, 0, recvDatagram.getLength());
  608. // assertEquals(longerMessage, recvMessage);
  609. //
  610. // // tricky case, added to test compatibility with RI;
  611. // // depends on the previous test case
  612. // sendData = veryLongMessage.getBytes();
  613. // sendDatagram = new DatagramPacket(sendData, 0, sendData.length, group,
  614. // PORT);
  615. // socket.send(sendDatagram);
  616. // recvDatagram.setLength(recvDatagram.getLength()); // !!!
  617. // socket.receive(recvDatagram);
  618. // recvMessage = new String(recvData, 0, recvDatagram.getLength());
  619. // assertEquals(longerMessage, recvMessage);
  620. //
  621. // // tests if received packet is truncated after length was set to 1
  622. // sendData = message.getBytes();
  623. // sendDatagram = new DatagramPacket(sendData, 0, sendData.length,
  624. // group, PORT);
  625. // socket.send(sendDatagram);
  626. // recvDatagram.setLength(1);
  627. // socket.receive(recvDatagram);
  628. // assertEquals("Received message was not truncated", 1,
  629. // recvDatagram.getLength());
  630. // assertSame("Received message is invalid", sendData[0], recvData[0]);
  631. //
  632. // socket.leaveGroup(group);
  633. // socket.close();
  634. // }
  635. /**
  636. * @tests java.net.DatagramSocket#send(java.net.DatagramPacket)
  637. */
  638. public void test_sendLjava_net_DatagramPacket() throws Exception {
  639. // Test for method void
  640. // java.net.DatagramSocket.send(java.net.DatagramPacket)
  641. int[] ports = Support_PortManager.getNextPortsForUDP(2);
  642. final int portNumber = ports[0];
  643. class TestDGSend implements Runnable {
  644. Thread pThread;
  645. public TestDGSend(Thread t) {
  646. pThread = t;
  647. }
  648. public void run() {
  649. try {
  650. byte[] rbuf = new byte[1000];
  651. sds = new DatagramSocket(portNumber);
  652. DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
  653. sds.setSoTimeout(6000);
  654. sds.receive(sdp);
  655. retval = new String(rbuf, 0, testString.length());
  656. pThread.interrupt();
  657. } catch (java.io.InterruptedIOException e) {
  658. System.out.println("Recv operation timed out");
  659. pThread.interrupt();
  660. ds.close();
  661. return;
  662. } catch (Exception e) {
  663. System.out
  664. .println("Failed to establish Dgram server: " + e);
  665. }
  666. }
  667. }
  668. try {
  669. new Thread(new TestDGSend(Thread.currentThread()), "DGServer")
  670. .start();
  671. ds = new java.net.DatagramSocket(ports[1]);
  672. dp = new DatagramPacket(testString.getBytes(), testString.length(),
  673. InetAddress.getLocalHost(), portNumber);
  674. // Wait to allow send to occur
  675. try {
  676. Thread.sleep(500);
  677. ds.send(dp);
  678. Thread.sleep(5000);
  679. } catch (InterruptedException e) {
  680. ds.close();
  681. assertTrue("Incorrect data sent: " + retval, retval
  682. .equals(testString));
  683. }
  684. } finally {
  685. ds.close();
  686. }
  687. // Regression for HARMONY-1118
  688. class testDatagramSocket extends DatagramSocket {
  689. public testDatagramSocket(DatagramSocketImpl impl) {
  690. super(impl);
  691. }
  692. }
  693. class testDatagramSocketImpl extends DatagramSocketImpl {
  694. protected void create() throws SocketException {
  695. }
  696. protected void bind(int arg0, InetAddress arg1)
  697. throws SocketException {
  698. }
  699. protected void send(DatagramPacket arg0) throws IOException {
  700. }
  701. protected int peek(InetAddress arg0) throws IOException {
  702. return 0;
  703. }
  704. protected int peekData(DatagramPacket arg0) throws IOException {
  705. return 0;
  706. }
  707. protected void receive(DatagramPacket arg0) throws IOException {
  708. }
  709. protected void setTTL(byte arg0) throws IOException {
  710. }
  711. protected byte getTTL() throws IOException {
  712. return 0;
  713. }
  714. protected void setTimeToLive(int arg0) throws IOException {
  715. }
  716. protected int getTimeToLive() throws IOException {
  717. return 0;
  718. }
  719. protected void join(InetAddress arg0) throws IOException {
  720. }
  721. protected void leave(InetAddress arg0) throws IOException {
  722. }
  723. protected void joinGroup(SocketAddress arg0, NetworkInterface arg1)
  724. throws IOException {
  725. }
  726. protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1)
  727. throws IOException {
  728. }
  729. protected void close() {
  730. }
  731. public void setOption(int arg0, Object arg1) throws SocketException {
  732. }
  733. public Object getOption(int arg0) throws SocketException {
  734. return null;
  735. }
  736. }
  737. // Regression test for Harmony-2938
  738. InetAddress i = InetAddress.getByName("127.0.0.1");
  739. DatagramSocket d = new DatagramSocket(0, i);
  740. try {
  741. d.send(new DatagramPacket(new byte[] { 1 }, 1));
  742. fail("should throw NPE.");
  743. } catch (NullPointerException e) {
  744. // expected;
  745. } finally {
  746. d.close();
  747. }
  748. // Regression test for Harmony-6413
  749. InetSocketAddress addr = InetSocketAddress.createUnresolved(
  750. "localhost", 0);
  751. try {
  752. DatagramPacket dp = new DatagramPacket(new byte[272], 3, addr);
  753. fail("should throw IllegalArgumentException");
  754. } catch (IllegalArgumentException e) {
  755. // expected
  756. }
  757. }
  758. /**
  759. * If the InetAddress of DatagramPacket is null, DatagramSocket.send(DatagramPacket)
  760. * should throw NullPointer Exception.
  761. * @tests java.net.DatagramSocket#send(java.net.DatagramPacket)
  762. */
  763. public void test_sendLjava_net_DatagramPacket2() throws IOException {
  764. int udp_port = 20000;
  765. int send_port = 23000;
  766. DatagramSocket udpSocket = new DatagramSocket(udp_port);
  767. byte[] data = {65};
  768. DatagramPacket sendPacket = new DatagramPacket(data, data.length, null, send_port);
  769. try {
  770. udpSocket.send(sendPacket);
  771. fail("Should throw SocketException");
  772. } catch (NullPointerException e) {
  773. // Expected
  774. } finally {
  775. udpSocket.close();
  776. }
  777. }
  778. public void test_setSendBufferSizeI() throws Exception {
  779. int portNumber = Support_PortManager.getNextPortForUDP();
  780. ds = new java.net.DatagramSocket(portNumber);
  781. ds.setSendBufferSize(134);
  782. assertTrue("Incorrect buffer size", ds.getSendBufferSize() >= 134);
  783. }
  784. public void test_setReceiveBufferSizeI() throws Exception {
  785. int portNumber = Support_PortManager.getNextPortForUDP();
  786. ds = new java.net.DatagramSocket(portNumber);
  787. ds.setReceiveBufferSize(130);
  788. assertTrue("Incorrect buffer size", ds.getReceiveBufferSize() >= 130);
  789. }
  790. public void test_setSoTimeoutI() throws Exception {
  791. DatagramSocket ds = new DatagramSocket();
  792. ds.setSoTimeout(100);
  793. assertTrue("Set incorrect timeout", ds.getSoTimeout() >= 100);
  794. }
  795. public void test_ConstructorLjava_net_DatagramSocketImpl() {
  796. class SimpleTestDatagramSocket extends DatagramSocket {
  797. public SimpleTestDatagramSocket(DatagramSocketImpl impl) {
  798. super(impl);
  799. }
  800. }
  801. try {
  802. new SimpleTestDatagramSocket((DatagramSocketImpl) null);
  803. fail("exception expected");
  804. } catch (NullPointerException ex) {
  805. // expected
  806. }
  807. }
  808. /**
  809. * @tests java.net.DatagramSocket#DatagramSocket(java.net.SocketAddress)
  810. */
  811. public void test_ConstructorLjava_net_SocketAddress() throws Exception {
  812. class UnsupportedSocketAddress extends SocketAddress {
  813. public UnsupportedSocketAddress() {
  814. }
  815. }
  816. DatagramSocket ds = new DatagramSocket(new InetSocketAddress(
  817. InetAddress.getLocalHost(), 0));
  818. assertTrue(ds.getBroadcast());
  819. assertTrue("Created socket with incorrect port", ds.getLocalPort() != 0);
  820. assertEquals("Created socket with incorrect address", InetAddress
  821. .getLocalHost(), ds.getLocalAddress());
  822. try {
  823. ds = new java.net.DatagramSocket(new UnsupportedSocketAddress());
  824. fail("No exception when constructing datagramSocket with unsupported SocketAddress type");
  825. } catch (IllegalArgumentException e) {
  826. // Expected
  827. }
  828. // regression for HARMONY-894
  829. ds = new DatagramSocket((SocketAddress) null);
  830. assertTrue(ds.getBroadcast());
  831. }
  832. /**
  833. * @tests java.net.DatagramSocket#bind(java.net.SocketAddress)
  834. */
  835. public void test_bindLjava_net_SocketAddress() throws Exception {
  836. class mySocketAddress extends SocketAddress {
  837. public mySocketAddress() {
  838. }
  839. }
  840. DatagramServer server = null;
  841. // now create a socket that is not bound and then bind it
  842. int[] ports = Support_PortManager.getNextPortsForUDP(3);
  843. int portNumber = ports[0];
  844. int serverPortNumber = ports[1];
  845. DatagramSocket theSocket = new DatagramSocket(new InetSocketAddress(
  846. InetAddress.getLocalHost(), portNumber));
  847. // validate that the localSocketAddress reflects the address we
  848. // bound to
  849. assertTrue(
  850. "Local address not correct after bind:"
  851. + theSocket.getLocalSocketAddress().toString()
  852. + "Expected: "
  853. + (new InetSocketAddress(InetAddress.getLocalHost(),
  854. portNumber)).toString(), theSocket
  855. .getLocalSocketAddress().equals(
  856. new InetSocketAddress(InetAddress
  857. .getLocalHost(), portNumber)));
  858. // now make sure that datagrams sent from this socket appear to come
  859. // from the address we bound to
  860. InetAddress localHost = InetAddress.getLocalHost();
  861. portNumber = ports[2];
  862. DatagramSocket ds = new DatagramSocket(null);
  863. ds.bind(new InetSocketAddress(localHost, portNumber));
  864. server = new DatagramServer(serverPortNumber, localHost);
  865. server.start();
  866. Thread.sleep(1000);
  867. ds.connect(new InetSocketAddress(localHost, serverPortNumber));
  868. byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
  869. DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
  870. ds.send(send);
  871. Thread.sleep(1000);
  872. ds.close();
  873. assertTrue("Address in packet sent does not match address bound to:"
  874. + server.rdp.getAddress() + ":" + server.rdp.getPort() + ":"
  875. + localHost + ":" + portNumber, (server.rdp.getAddress()
  876. .equals(localHost))
  877. && (server.rdp.getPort() == portNumber));
  878. // validate if we pass in null that it picks an address for us and
  879. // all is ok
  880. theSocket = new DatagramSocket(null);
  881. theSocket.bind(null);
  882. assertNotNull("Bind with null did not work", theSocket
  883. .getLocalSocketAddress());
  884. theSocket.close();
  885. // now check the error conditions
  886. // Address we cannot bind to
  887. theSocket = new DatagramSocket(null);
  888. try {
  889. theSocket.bind(new InetSocketAddress(InetAddress
  890. .getByAddress(Support_Configuration.nonLocalAddressBytes),
  891. Support_PortManager.getNextPortForUDP()));
  892. fail("No exception when binding to bad address");
  893. } catch (SocketException ex) {
  894. }
  895. theSocket.close();
  896. // Address that we have allready bound to
  897. ports = Support_PortManager.getNextPortsForUDP(2);
  898. theSocket = new DatagramSocket(null);
  899. DatagramSocket theSocket2 = new DatagramSocket(ports[0]);
  900. try {
  901. InetSocketAddress theAddress = new InetSocketAddress(InetAddress
  902. .getLocalHost(), ports[1]);
  903. theSocket.bind(theAddress);
  904. theSocket2.bind(theAddress);
  905. fail("No exception binding to address that is not available");
  906. } catch (SocketException ex) {
  907. }
  908. theSocket.close();
  909. theSocket2.close();
  910. // unsupported SocketAddress subclass
  911. theSocket = new DatagramSocket(null);
  912. try {
  913. theSocket.bind(new mySocketAddress());
  914. fail("No exception when binding using unsupported SocketAddress subclass");
  915. } catch (IllegalArgumentException ex) {
  916. }
  917. theSocket.close();
  918. if (server != null) {
  919. server.stopServer();
  920. }
  921. }
  922. /**
  923. * @tests java.net.DatagramSocket#connect(java.net.SocketAddress)
  924. */
  925. public void test_connectLjava_net_SocketAddress() throws Exception {
  926. // validate that we get the PortUnreachable exception if we try to
  927. // send a dgram to a server that is not running and then do a recv
  928. try {
  929. ds = new java.net.DatagramSocket();
  930. InetAddress inetAddress = InetAddress.getLocalHost();
  931. int portNumber = Support_PortManager.getNextPortForUDP();
  932. ds.connect(new InetSocketAddress(inetAddress, portNumber));
  933. DatagramPacket send = new DatagramPacket(new byte[10], 10);
  934. ds.send(send);
  935. DatagramPacket receive = new DatagramPacket(new byte[20], 20);
  936. ds.setSoTimeout(10000);
  937. ds.receive(receive);
  938. ds.close();
  939. fail("No PortUnreachableException when connected at native level on recv ");
  940. } catch (PortUnreachableException e) {
  941. // Expected
  942. }
  943. // validate that we can send/receive with datagram sockets connected at
  944. // the native level
  945. DatagramServer server = null;
  946. int[] ports = Support_PortManager.getNextPortsForUDP(3);
  947. int serverPortNumber = ports[0];
  948. InetAddress localHost = InetAddress.getLocalHost();
  949. DatagramSocket ds = new DatagramSocket(ports[1]);
  950. DatagramSocket ds2 = new DatagramSocket(ports[2]);
  951. server = new DatagramServer(serverPortNumber, localHost);
  952. server.start();
  953. Thread.sleep(1000);
  954. int port = ds.getLocalPort();
  955. ds.connect(new InetSocketAddress(localHost, serverPortNumber));
  956. final byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
  957. DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
  958. ds.send(send);
  959. DatagramPacket receive = new DatagramPacket(new byte[20], 20);
  960. ds.setSoTimeout(2000);
  961. ds.receive(receive);
  962. ds.close();
  963. assertTrue("Wrong size data received: " + receive.getLength(), receive
  964. .getLength() == sendBytes.length);
  965. assertTrue("Wrong data received"
  966. + new String(receive.getData(), 0, receive.getLength()) + ":"
  967. + new String(sendBytes), new String(receive.getData(), 0,
  968. receive.getLength()).equals(new String(sendBytes)));
  969. assertTrue("Wrong receiver:" + receive.getAddress() + ":" + localHost,
  970. receive.getAddress().equals(localHost));
  971. if (server != null) {
  972. server.stopServer();
  973. }
  974. // validate that we can disconnect
  975. try {
  976. ds = new java.net.DatagramSocket();
  977. InetAddress inetAddress = InetAddress.getLocalHost();
  978. int portNumber = Support_PortManager.getNextPortForUDP();
  979. ds.connect(new InetSocketAddress(inetAddress, portNumber));
  980. ds.disconnect();
  981. ds.close();
  982. } catch (PortUnreachableException e) {
  983. // Expected
  984. }
  985. // validate that once connected we cannot send to another address
  986. try {
  987. ds = new java.net.DatagramSocket();
  988. InetAddress inetAddress = InetAddress.getLocalHost();
  989. int portNumber = Support_PortManager.getNextPortForUDP();
  990. ds.connect(new InetSocketAddress(inetAddress, portNumber));
  991. DatagramPacket senddp = new DatagramPacket(new byte[10], 10,
  992. inetAddress, portNumber + 1);
  993. ds.send(senddp);
  994. ds.close();
  995. fail("No Exception when trying to send to a different address on a connected socket ");
  996. } catch (IllegalArgumentException e) {
  997. // Expected
  998. }
  999. // validate that we can connect, then disconnect, then connect then
  1000. // send/recv
  1001. server = null;
  1002. ports = Support_PortManager.getNextPortsForUDP(3);
  1003. serverPortNumber = ports[0];
  1004. localHost = InetAddress.getLocalHost();
  1005. ds = new DatagramSocket(ports[1]);
  1006. ds2 = new DatagramSocket(ports[2]);
  1007. server = new DatagramServer(serverPortNumber, localHost);
  1008. server.start();
  1009. Thread.sleep(1000);
  1010. port = ds.getLocalPort();
  1011. ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
  1012. ds.disconnect();
  1013. ds.connect(new InetSocketAddress(localHost, serverPortNumber));
  1014. send = new DatagramPacket(sendBytes, sendBytes.length);
  1015. ds.send(send);
  1016. receive = new DatagramPacket(new byte[20], 20);
  1017. ds.setSoTimeout(2000);
  1018. ds.receive(receive);
  1019. ds.close();
  1020. assertTrue("connect/disconnect/connect - Wrong size data received: "
  1021. + receive.getLength(), receive.getLength() == sendBytes.length);
  1022. assertTrue("connect/disconnect/connect - Wrong data received"
  1023. + new String(receive.getData(), 0, receive.getLength()) + ":"
  1024. + new String(sendBytes), new String(receive.getData(), 0,
  1025. receive.getLength()).equals(new String(sendBytes)));
  1026. assertTrue("connect/disconnect/connect - Wrong receiver:"
  1027. + receive.getAddress() + ":" + localHost, receive.getAddress()
  1028. .equals(localHost));
  1029. if (server != null) {
  1030. server.stopServer();
  1031. }
  1032. // validate that we can connect/disconnect then send/recv to any address
  1033. server = null;
  1034. ports = Support_PortManager.getNextPortsForUDP(3);
  1035. serverPortNumber = ports[0];
  1036. localHost = InetAddress.getLocalHost();
  1037. ds = new DatagramSocket(ports[1]);
  1038. ds2 = new DatagramSocket(ports[2]);
  1039. server = new DatagramServer(serverPortNumber, localHost);
  1040. server.start();
  1041. Thread.sleep(1000);
  1042. port = ds.getLocalPort();
  1043. ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
  1044. ds.disconnect();
  1045. send = new DatagramPacket(sendBytes, sendBytes.length, localHost,
  1046. serverPortNumber);
  1047. ds.send(send);
  1048. receive = new DatagramPacket(new byte[20], 20);
  1049. ds.setSoTimeout(2000);
  1050. ds.receive(receive);
  1051. ds.close();
  1052. assertTrue("connect/disconnect - Wrong size data received: "
  1053. + receive.getLength(), receive.getLength() == sendBytes.length);
  1054. assertTrue("connect/disconnect - Wrong data received"
  1055. + new String(receive.getData(), 0, receive.getLength()) + ":"
  1056. + new String(sendBytes), new String(receive.getData(), 0,
  1057. receive.getLength()).equals(new String(sendBytes)));
  1058. assertTrue("connect/disconnect - Wrong receiver:"
  1059. + receive.getAddress() + ":" + localHost, receive.getAddress()
  1060. .equals(localHost));
  1061. if (server != null) {
  1062. server.stopServer();
  1063. }
  1064. // validate that we can connect on an allready connected socket and then
  1065. // send/recv
  1066. server = null;
  1067. ports = Support_PortManager.getNextPortsForUDP(3);
  1068. serverPortNumber = ports[0];
  1069. localHost = InetAddress.getLocalHost();
  1070. ds = new DatagramSocket(ports[1]);
  1071. ds2 = new DatagramSocket(ports[2]);
  1072. server = new DatagramServer(serverPortNumber, localHost);
  1073. server.start();
  1074. Thread.sleep(1000);
  1075. port = ds.getLocalPort();
  1076. ds.connect(new InetSocketAddress(localHost, serverPortNumber + 1));
  1077. ds.connect(new InetSocketAddress(localHost, serverPortNumber));
  1078. byte[] sendTestBytes = { 'T', 'e', 's', 't', 0 };
  1079. send = new DatagramPacket(sendTestBytes, sendTestBytes.length);
  1080. ds.send(send);
  1081. DatagramPacket receivedp = new DatagramPacket(new byte[20], 20);
  1082. ds.setSoTimeout(2000);
  1083. ds.receive(receivedp);
  1084. ds.close();
  1085. assertTrue("connect/connect - Wrong size data received: "
  1086. + receivedp.getLength(),
  1087. receivedp.getLength() == sendTestBytes.length);
  1088. assertTrue("connect/connect - Wrong data received"
  1089. + new String(receivedp.getData(), 0, receivedp.getLength())
  1090. + ":" + new String(sendTestBytes), new String(receivedp
  1091. .getData(), 0, receivedp.getLength()).equals(new String(
  1092. sendTestBytes)));
  1093. assertTrue("connect/connect - Wrong receiver:" + receivedp.getAddress()
  1094. + ":" + localHost, receivedp.getAddress().equals(localHost));
  1095. if (server != null) {
  1096. server.stopServer();
  1097. }
  1098. // test for when we fail to connect at the native level. It seems to
  1099. // fail for the any address so we use this. Now to be compatible we
  1100. // don't throw the exception but eat it and then act as if we were
  1101. // connected at the Java level.
  1102. try {
  1103. ds = new java.net.DatagramSocket();
  1104. byte[] addressBytes = { 0, 0, 0, 0 };
  1105. InetAddress inetAddress = InetAddress.getByAddress(addressBytes);
  1106. int portNumber = Support_PortManager.getNextPortForUDP();
  1107. InetAddress localHostIA = InetAddress.getLocalHost();
  1108. ds.connect(new InetSocketAddress(inetAddress, portNumber));
  1109. assertTrue("Is not connected after connect to inaddr any", ds
  1110. .isConnected());
  1111. byte[] sendBytesArray = { 'T', 'e', 's', 't', 0 };
  1112. DatagramPacket senddp = new DatagramPacket(sendBytesArray,
  1113. sendBytesArray.length, localHostIA, portNumber);
  1114. ds.send(senddp);
  1115. fail("No exception when trying to connect at native level with bad address (exception from send) ");
  1116. } catch (IllegalArgumentException e) {
  1117. // Expected
  1118. }
  1119. }
  1120. /**
  1121. * @tests java.net.DatagramSocket#isBound()
  1122. */
  1123. public void test_isBound() throws Exception {
  1124. InetAddress addr = InetAddress.getLocalHost();
  1125. int[] ports = Support_PortManager.getNextPortsForUDP(3);
  1126. int port = ports[0];
  1127. DatagramSocket theSocket = new DatagramSocket(ports[1]);
  1128. assertTrue("Socket indicated not bound when it should be (1)",
  1129. theSocket.isBound());
  1130. theSocket.close();
  1131. theSocket = new DatagramSocket(new InetSocketAddress(addr, port));
  1132. assertTrue("Socket indicated not bound when it should be (2)",
  1133. theSocket.isBound());
  1134. theSocket.close();
  1135. theSocket = new DatagramSocket(null);
  1136. assertFalse("Socket indicated bound when it should not be (1)",
  1137. theSocket.isBound());
  1138. theSocket.clos