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