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