1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.net;
19
20import java.io.IOException;
21import java.net.DatagramPacket;
22import java.net.DatagramSocket;
23import java.net.InetAddress;
24import java.net.InetSocketAddress;
25import java.net.SocketAddress;
26
27import tests.support.Support_Configuration;
28
29public class DatagramPacketTest extends junit.framework.TestCase {
30
31    volatile boolean started = false;
32
33    /**
34     * java.net.DatagramPacket#DatagramPacket(byte[], int)
35     */
36    public void test_Constructor$BI() {
37        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
38        assertEquals("Created incorrect packet", "Hello", new String(dp
39                .getData(), 0, dp.getData().length));
40        assertEquals("Wrong length", 5, dp.getLength());
41
42        // Regression for HARMONY-890
43        dp = new DatagramPacket(new byte[942], 4);
44        assertEquals(-1, dp.getPort());
45        try {
46            dp.getSocketAddress();
47            fail("Should throw IllegalArgumentException");
48        } catch (IllegalArgumentException e) {
49            // expected
50        }
51    }
52
53    /**
54     * java.net.DatagramPacket#DatagramPacket(byte[], int, int)
55     */
56    public void test_Constructor$BII() {
57        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 2, 3);
58        assertEquals("Created incorrect packet", "Hello", new String(dp
59                .getData(), 0, dp.getData().length));
60        assertEquals("Wrong length", 3, dp.getLength());
61        assertEquals("Wrong offset", 2, dp.getOffset());
62    }
63
64    /**
65     * java.net.DatagramPacket#DatagramPacket(byte[], int, int,
66     *java.net.InetAddress, int)
67     */
68    public void test_Constructor$BIILjava_net_InetAddressI() throws IOException {
69        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 2, 3,
70                InetAddress.getLocalHost(), 0);
71        assertEquals("Wrong host", InetAddress.getLocalHost(), dp.getAddress());
72        assertEquals("Wrong port", 0, dp.getPort());
73        assertEquals("Wrong length", 3, dp.getLength());
74        assertEquals("Wrong offset", 2, dp.getOffset());
75    }
76
77    /**
78     * java.net.DatagramPacket#DatagramPacket(byte[], int,
79     *java.net.InetAddress, int)
80     */
81    public void test_Constructor$BILjava_net_InetAddressI() throws IOException {
82        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
83                InetAddress.getLocalHost(), 0);
84        assertEquals("Wrong address", InetAddress.getLocalHost(), dp
85                .getAddress());
86        assertEquals("Wrong port", 0, dp.getPort());
87        assertEquals("Wrong length", 5, dp.getLength());
88    }
89
90    /**
91     * java.net.DatagramPacket#getAddress()
92     */
93    public void test_getAddress() throws IOException {
94        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
95                InetAddress.getLocalHost(), 0);
96        assertEquals("Incorrect address returned", InetAddress.getLocalHost(),
97                dp.getAddress());
98    }
99
100    /**
101     * java.net.DatagramPacket#getData()
102     */
103    public void test_getData() {
104        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
105        assertEquals("Incorrect length returned", "Hello", new String(dp
106                .getData(), 0, dp.getData().length));
107    }
108
109    /**
110     * java.net.DatagramPacket#getLength()
111     */
112    public void test_getLength() {
113        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
114        assertEquals("Incorrect length returned", 5, dp.getLength());
115    }
116
117    /**
118     * java.net.DatagramPacket#getOffset()
119     */
120    public void test_getOffset() {
121        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 3, 2);
122        assertEquals("Incorrect length returned", 3, dp.getOffset());
123    }
124
125    /**
126     * java.net.DatagramPacket#getPort()
127     */
128    public void test_getPort() throws IOException {
129        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
130                InetAddress.getLocalHost(), 1000);
131        assertEquals("Incorrect port returned", 1000, dp.getPort());
132
133        final InetAddress localhost = InetAddress.getLocalHost();
134        DatagramSocket socket = new DatagramSocket(0, localhost);
135        final int port = socket.getLocalPort();
136
137        socket.setSoTimeout(3000);
138        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5,
139                6 }, 6, localhost, port);
140        socket.send(packet);
141        socket.receive(packet);
142        socket.close();
143        assertTrue("datagram received wrong port: " + packet.getPort(), packet
144                .getPort() == port);
145    }
146
147    /**
148     * java.net.DatagramPacket#setAddress(java.net.InetAddress)
149     */
150    public void test_setAddressLjava_net_InetAddress() throws IOException {
151        InetAddress ia = InetAddress.getByName("127.0.0.1");
152        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
153                InetAddress.getLocalHost(), 0);
154        dp.setAddress(ia);
155        assertEquals("Incorrect address returned", ia, dp.getAddress());
156    }
157
158    /**
159     * java.net.DatagramPacket#setData(byte[], int, int)
160     */
161    public void test_setData$BII() {
162        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
163        dp.setData("Wagga Wagga".getBytes(), 2, 3);
164        assertEquals("Incorrect data set", "Wagga Wagga", new String(dp
165                .getData()));
166    }
167
168    /**
169     * java.net.DatagramPacket#setData(byte[])
170     */
171    public void test_setData$B() {
172        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
173        dp.setData("Ralph".getBytes());
174        assertEquals("Incorrect data set", "Ralph", new String(dp.getData(), 0,
175                dp.getData().length));
176    }
177
178    /**
179     * java.net.DatagramPacket#setLength(int)
180     */
181    public void test_setLengthI() {
182        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5);
183        dp.setLength(1);
184        assertEquals("Failed to set packet length", 1, dp.getLength());
185    }
186
187    /**
188     * java.net.DatagramPacket#setPort(int)
189     */
190    public void test_setPortI() throws Exception {
191        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
192                InetAddress.getLocalHost(), 1000);
193        dp.setPort(2000);
194        assertEquals("Port not set", 2000, dp.getPort());
195    }
196
197    /**
198     * java.net.DatagramPacket#DatagramPacket(byte[], int,
199     *java.net.SocketAddress)
200     */
201    public void test_Constructor$BILjava_net_SocketAddress() throws IOException {
202        @SuppressWarnings("serial")
203        class UnsupportedSocketAddress extends SocketAddress {
204
205            public UnsupportedSocketAddress() {
206            }
207        }
208
209        // Unsupported SocketAddress subclass
210        byte buf[] = new byte[1];
211        try {
212            new DatagramPacket(buf, 1, new UnsupportedSocketAddress());
213            fail("No exception when constructing using unsupported SocketAddress subclass");
214        } catch (IllegalArgumentException ex) {
215            // Expected
216        }
217
218        // Case were we try to pass in null
219        try {
220            new DatagramPacket(buf, 1, null);
221            fail("No exception when constructing address using null");
222        } catch (IllegalArgumentException ex) {
223            // Expected
224        }
225
226        // Now validate we can construct
227        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
228                .getLocalHost(), 2067);
229        DatagramPacket thePacket = new DatagramPacket(buf, 1, theAddress);
230        assertEquals("Socket address not set correctly (1)", theAddress,
231                thePacket.getSocketAddress());
232        assertEquals("Socket address not set correctly (2)", theAddress,
233                new InetSocketAddress(thePacket.getAddress(), thePacket
234                        .getPort()));
235    }
236
237    /**
238     * java.net.DatagramPacket#DatagramPacket(byte[], int, int,
239     *java.net.SocketAddress)
240     */
241    public void test_Constructor$BIILjava_net_SocketAddress()
242            throws IOException {
243        @SuppressWarnings("serial")
244        class UnsupportedSocketAddress extends SocketAddress {
245
246            public UnsupportedSocketAddress() {
247            }
248        }
249
250        // Unsupported SocketAddress subclass
251        byte buf[] = new byte[2];
252        try {
253            new DatagramPacket(buf, 1, 1, new UnsupportedSocketAddress());
254            fail("No exception when constructing using unsupported SocketAddress subclass");
255        } catch (IllegalArgumentException ex) {
256            // Expected
257        }
258
259        // Case were we try to pass in null
260        try {
261            new DatagramPacket(buf, 1, 1, null);
262            fail("No exception when constructing address using null");
263        } catch (IllegalArgumentException ex) {
264            // Expected
265        }
266
267        // now validate we can construct
268        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
269                .getLocalHost(), 2067);
270        DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, theAddress);
271        assertEquals("Socket address not set correctly (1)", theAddress,
272                thePacket.getSocketAddress());
273        assertEquals("Socket address not set correctly (2)", theAddress,
274                new InetSocketAddress(thePacket.getAddress(), thePacket
275                        .getPort()));
276        assertEquals("Offset not set correctly", 1, thePacket.getOffset());
277    }
278
279    /**
280     * java.net.DatagramPacket#getSocketAddress()
281     */
282    public void test_getSocketAddress() throws IOException {
283        byte buf[] = new byte[1];
284        DatagramPacket thePacket = new DatagramPacket(buf, 1);
285
286        // Validate get returns the value we set
287        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
288                .getLocalHost(), 0);
289        thePacket = new DatagramPacket(buf, 1);
290        thePacket.setSocketAddress(theAddress);
291        assertEquals("Socket address not set correctly (1)", theAddress,
292                thePacket.getSocketAddress());
293    }
294
295    /**
296     * java.net.DatagramPacket#setSocketAddress(java.net.SocketAddress)
297     */
298    public void test_setSocketAddressLjava_net_SocketAddress()
299            throws IOException {
300
301        @SuppressWarnings("serial")
302        class UnsupportedSocketAddress extends SocketAddress {
303
304            public UnsupportedSocketAddress() {
305            }
306        }
307
308        // Unsupported SocketAddress subclass
309        byte buf[] = new byte[1];
310        DatagramPacket thePacket = new DatagramPacket(buf, 1);
311        try {
312            thePacket.setSocketAddress(new UnsupportedSocketAddress());
313            fail("No exception when setting address using unsupported SocketAddress subclass");
314        } catch (IllegalArgumentException ex) {
315            // Expected
316        }
317
318        // Case were we try to pass in null
319        thePacket = new DatagramPacket(buf, 1);
320        try {
321            thePacket.setSocketAddress(null);
322            fail("No exception when setting address using null");
323        } catch (IllegalArgumentException ex) {
324            // Expected
325        }
326
327        // Now validate we can set it correctly
328        InetSocketAddress theAddress = new InetSocketAddress(InetAddress
329                .getLocalHost(), 2049);
330        thePacket = new DatagramPacket(buf, 1);
331        thePacket.setSocketAddress(theAddress);
332        assertEquals("Socket address not set correctly (1)", theAddress,
333                thePacket.getSocketAddress());
334        assertEquals("Socket address not set correctly (2)", theAddress,
335                new InetSocketAddress(thePacket.getAddress(), thePacket
336                        .getPort()));
337    }
338}
339