DatagramPacketTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 tests.api.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;
26import java.net.UnknownHostException;
27
28import tests.support.Support_Configuration;
29import tests.support.Support_PortManager;
30
31public class DatagramPacketTest extends junit.framework.TestCase {
32
33	DatagramPacket dp;
34
35	volatile boolean started = false;
36
37	/**
38	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int)
39	 */
40	public void test_Constructor$BI() {
41		// Test for method java.net.DatagramPacket(byte [], int)
42		try {
43			dp = new DatagramPacket("Hello".getBytes(), 5);
44			assertEquals("Created incorrect packet", "Hello", new String(dp.getData(), 0,
45					dp.getData().length));
46			assertEquals("Wrong length", 5, dp.getLength());
47		} catch (Exception e) {
48			fail("Exception during Constructor test: " + e.toString());
49		}
50        //regression for Harmony-890
51        dp = new DatagramPacket(new byte[942],4);
52        assertEquals(-1, dp.getPort());
53        try{
54            dp.getSocketAddress();
55            fail("Should throw IllegalArgumentException");
56        }catch(IllegalArgumentException e){
57            //expected
58        }
59	}
60
61	/**
62	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int)
63	 */
64	public void test_Constructor$BII() {
65		try {
66			dp = new DatagramPacket("Hello".getBytes(), 2, 3);
67			assertEquals("Created incorrect packet", "Hello", new String(dp.getData(), 0,
68					dp.getData().length));
69			assertEquals("Wrong length", 3, dp.getLength());
70			assertEquals("Wrong offset", 2, dp.getOffset());
71		} catch (Exception e) {
72			fail("Exception during Constructor test: " + e.toString());
73		}
74	}
75
76	/**
77	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int,
78	 *        java.net.InetAddress, int)
79	 */
80	public void test_Constructor$BIILjava_net_InetAddressI() {
81		try {
82			dp = new DatagramPacket("Hello".getBytes(), 2, 3, InetAddress
83					.getLocalHost(), 0);
84			assertTrue("Created incorrect packet", dp.getAddress().equals(
85					InetAddress.getLocalHost())
86					&& dp.getPort() == 0);
87			assertEquals("Wrong length", 3, dp.getLength());
88			assertEquals("Wrong offset", 2, dp.getOffset());
89		} catch (Exception e) {
90			fail("Exception during Constructor test: " + e.toString());
91		}
92	}
93
94	/**
95	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int,
96	 *        java.net.InetAddress, int)
97	 */
98	public void test_Constructor$BILjava_net_InetAddressI() {
99		// Test for method java.net.DatagramPacket(byte [], int,
100		// java.net.InetAddress, int)
101		try {
102			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
103					.getLocalHost(), 0);
104			assertTrue("Created incorrect packet", dp.getAddress().equals(
105					InetAddress.getLocalHost())
106					&& dp.getPort() == 0);
107			assertEquals("Wrong length", 5, dp.getLength());
108		} catch (Exception e) {
109			fail("Exception during Constructor test: " + e.toString());
110		}
111	}
112
113	/**
114	 * @tests java.net.DatagramPacket#getAddress()
115	 */
116	public void test_getAddress() {
117		// Test for method java.net.InetAddress
118		// java.net.DatagramPacket.getAddress()
119		try {
120			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
121					.getLocalHost(), 0);
122			assertTrue("Incorrect address returned", dp.getAddress().equals(
123					InetAddress.getLocalHost()));
124		} catch (Exception e) {
125			fail("Exception during getAddress test:" + e.toString());
126		}
127	}
128
129	/**
130	 * @tests java.net.DatagramPacket#getData()
131	 */
132	public void test_getData() {
133		// Test for method byte [] java.net.DatagramPacket.getData()
134
135		dp = new DatagramPacket("Hello".getBytes(), 5);
136		assertEquals("Incorrect length returned", "Hello", new String(dp.getData(), 0, dp
137				.getData().length));
138	}
139
140	/**
141	 * @tests java.net.DatagramPacket#getLength()
142	 */
143	public void test_getLength() {
144		// Test for method int java.net.DatagramPacket.getLength()
145
146		dp = new DatagramPacket("Hello".getBytes(), 5);
147		assertEquals("Incorrect length returned", 5, dp.getLength());
148	}
149
150	/**
151	 * @tests java.net.DatagramPacket#getOffset()
152	 */
153	public void test_getOffset() {
154		dp = new DatagramPacket("Hello".getBytes(), 3, 2);
155		assertEquals("Incorrect length returned", 3, dp.getOffset());
156	}
157
158	/**
159	 * @tests java.net.DatagramPacket#getPort()
160	 */
161	public void test_getPort() {
162		// Test for method int java.net.DatagramPacket.getPort()
163		try {
164			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
165					.getLocalHost(), 1000);
166			assertEquals("Incorrect port returned", 1000, dp.getPort());
167		} catch (Exception e) {
168			fail("Exception during getPort test : " + e.getMessage());
169		}
170
171		InetAddress localhost = null;
172		try {
173			localhost = InetAddress.getByName("localhost");
174		} catch (UnknownHostException e) {
175			fail("Unexpected UnknownHostException : " + e.getMessage());
176		}
177		int[] ports = Support_PortManager.getNextPortsForUDP(2);
178		final int port = ports[0];
179		final Object lock = new Object();
180
181		Thread thread = new Thread(new Runnable() {
182			public void run() {
183				DatagramSocket socket = null;
184				try {
185					socket = new DatagramSocket(port);
186					synchronized (lock) {
187						started = true;
188						lock.notifyAll();
189					}
190					socket.setSoTimeout(3000);
191					DatagramPacket packet = new DatagramPacket(new byte[256],
192							256);
193					socket.receive(packet);
194					socket.send(packet);
195					socket.close();
196				} catch (IOException e) {
197					System.out.println("thread exception: " + e);
198					if (socket != null)
199						socket.close();
200				}
201			}
202		});
203		thread.start();
204
205		DatagramSocket socket = null;
206		try {
207			socket = new DatagramSocket(ports[1]);
208			socket.setSoTimeout(3000);
209			DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4,
210					5, 6 }, 6, localhost, port);
211			synchronized (lock) {
212				try {
213					if (!started)
214						lock.wait();
215				} catch (InterruptedException e) {
216					fail(e.toString());
217				}
218			}
219			socket.send(packet);
220			socket.receive(packet);
221			socket.close();
222			assertTrue("datagram received wrong port: " + packet.getPort(),
223					packet.getPort() == port);
224		} catch (IOException e) {
225			if (socket != null)
226				socket.close();
227			System.err.println("port: " + port + " datagram server error: ");
228			e.printStackTrace();
229			fail("port : " + port + " datagram server error : "
230					+ e.getMessage());
231		}
232	}
233
234	/**
235	 * @tests java.net.DatagramPacket#setAddress(java.net.InetAddress)
236	 */
237	public void test_setAddressLjava_net_InetAddress() {
238		// Test for method void
239		// java.net.DatagramPacket.setAddress(java.net.InetAddress)
240		try {
241			InetAddress ia = InetAddress
242					.getByName(Support_Configuration.InetTestIP);
243			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
244					.getLocalHost(), 0);
245			dp.setAddress(ia);
246			assertTrue("Incorrect address returned", dp.getAddress().equals(ia));
247		} catch (Exception e) {
248			fail("Exception during getAddress test:" + e.toString());
249		}
250	}
251
252	/**
253	 * @tests java.net.DatagramPacket#setData(byte[], int, int)
254	 */
255	public void test_setData$BII() {
256		dp = new DatagramPacket("Hello".getBytes(), 5);
257		dp.setData("Wagga Wagga".getBytes(), 2, 3);
258		assertEquals("Incorrect data set", "Wagga Wagga", new String(dp.getData())
259				);
260	}
261
262	/**
263	 * @tests java.net.DatagramPacket#setData(byte[])
264	 */
265	public void test_setData$B() {
266		// Test for method void java.net.DatagramPacket.setData(byte [])
267		dp = new DatagramPacket("Hello".getBytes(), 5);
268		dp.setData("Ralph".getBytes());
269		assertEquals("Incorrect data set", "Ralph", new String(dp.getData(), 0, dp
270				.getData().length));
271	}
272
273	/**
274	 * @tests java.net.DatagramPacket#setLength(int)
275	 */
276	public void test_setLengthI() {
277		// Test for method void java.net.DatagramPacket.setLength(int)
278		dp = new DatagramPacket("Hello".getBytes(), 5);
279		dp.setLength(1);
280		assertEquals("Failed to set packet length", 1, dp.getLength());
281	}
282
283	/**
284	 * @tests java.net.DatagramPacket#setPort(int)
285	 */
286	public void test_setPortI() {
287		// Test for method void java.net.DatagramPacket.setPort(int)
288		try {
289			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
290					.getLocalHost(), 1000);
291			dp.setPort(2000);
292			assertEquals("Port not set", 2000, dp.getPort());
293		} catch (Exception e) {
294			fail("Exception during setPort test : " + e.getMessage());
295		}
296	}
297
298	/**
299	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int,
300	 *        java.net.SocketAddress)
301	 */
302	public void test_Constructor$BILjava_net_SocketAddress() {
303		class mySocketAddress extends SocketAddress {
304
305			public mySocketAddress() {
306			}
307		}
308
309		try {
310			// unsupported SocketAddress subclass
311			byte buf[] = new byte[1];
312			try {
313				DatagramPacket thePacket = new DatagramPacket(buf, 1,
314						new mySocketAddress());
315				fail("No exception when constructing using unsupported SocketAddress subclass");
316			} catch (IllegalArgumentException ex) {
317			}
318
319			// case were we try to pass in null
320			// unsupported SocketAddress subclass
321
322			try {
323				DatagramPacket thePacket = new DatagramPacket(buf, 1, null);
324				fail("No exception when constructing address using null");
325			} catch (IllegalArgumentException ex) {
326			}
327
328			// now validate we can construct
329			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
330					.getLocalHost(), Support_PortManager.getNextPortForUDP());
331			DatagramPacket thePacket = new DatagramPacket(buf, 1, theAddress);
332			assertTrue("Socket address not set correctly (1)", theAddress
333					.equals(thePacket.getSocketAddress()));
334			assertTrue("Socket address not set correctly (2)", theAddress
335					.equals(new InetSocketAddress(thePacket.getAddress(),
336							thePacket.getPort())));
337		} catch (Exception e) {
338			fail("Exception during constructor test(1):" + e.toString());
339		}
340	}
341
342	/**
343	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int,
344	 *        java.net.SocketAddress)
345	 */
346	public void test_Constructor$BIILjava_net_SocketAddress() {
347		class mySocketAddress extends SocketAddress {
348
349			public mySocketAddress() {
350			}
351		}
352
353		try {
354			// unsupported SocketAddress subclass
355			byte buf[] = new byte[2];
356			try {
357				DatagramPacket thePacket = new DatagramPacket(buf, 1, 1,
358						new mySocketAddress());
359				fail("No exception when constructing using unsupported SocketAddress subclass");
360			} catch (IllegalArgumentException ex) {
361			}
362
363			// case were we try to pass in null
364			// unsupported SocketAddress subclass
365
366			try {
367				DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, null);
368				fail("No exception when constructing address using null");
369			} catch (IllegalArgumentException ex) {
370			}
371
372			// now validate we can construct
373			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
374					.getLocalHost(), Support_PortManager.getNextPortForUDP());
375			DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, theAddress);
376			assertTrue("Socket address not set correctly (1)", theAddress
377					.equals(thePacket.getSocketAddress()));
378			assertTrue("Socket address not set correctly (2)", theAddress
379					.equals(new InetSocketAddress(thePacket.getAddress(),
380							thePacket.getPort())));
381			assertEquals("Offset not set correctly", 1, thePacket.getOffset());
382		} catch (Exception e) {
383			fail("Exception during constructor test(2):" + e.toString());
384		}
385	}
386
387	/**
388	 * @tests java.net.DatagramPacket#getSocketAddress()
389	 */
390	public void test_getSocketAddress() {
391		try {
392			byte buf[] = new byte[1];
393			DatagramPacket thePacket = new DatagramPacket(buf, 1);
394
395			// validate get returns the value we set
396			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
397					.getLocalHost(), Support_PortManager.getNextPortForUDP());
398			thePacket = new DatagramPacket(buf, 1);
399			thePacket.setSocketAddress(theAddress);
400			assertTrue("Socket address not set correctly (1)", theAddress
401					.equals(thePacket.getSocketAddress()));
402		} catch (Exception e) {
403			fail(
404					"Exception during getSocketAddress test:" + e.toString());
405		}
406	}
407
408	/**
409	 * @tests java.net.DatagramPacket#setSocketAddress(java.net.SocketAddress)
410	 */
411	public void test_setSocketAddressLjava_net_SocketAddress() {
412
413		class mySocketAddress extends SocketAddress {
414
415			public mySocketAddress() {
416			}
417		}
418
419		try {
420			// unsupported SocketAddress subclass
421			byte buf[] = new byte[1];
422			DatagramPacket thePacket = new DatagramPacket(buf, 1);
423			try {
424				thePacket.setSocketAddress(new mySocketAddress());
425				fail("No exception when setting address using unsupported SocketAddress subclass");
426			} catch (IllegalArgumentException ex) {
427			}
428
429			// case were we try to pass in null
430			// unsupported SocketAddress subclass
431			thePacket = new DatagramPacket(buf, 1);
432			try {
433				thePacket.setSocketAddress(null);
434				fail("No exception when setting address using null");
435			} catch (IllegalArgumentException ex) {
436			}
437
438			// now validate we can set it correctly
439			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
440					.getLocalHost(), Support_PortManager.getNextPortForUDP());
441			thePacket = new DatagramPacket(buf, 1);
442			thePacket.setSocketAddress(theAddress);
443			assertTrue("Socket address not set correctly (1)", theAddress
444					.equals(thePacket.getSocketAddress()));
445			assertTrue("Socket address not set correctly (2)", theAddress
446					.equals(new InetSocketAddress(thePacket.getAddress(),
447							thePacket.getPort())));
448		} catch (Exception e) {
449			fail(
450					"Exception during setSocketAddress test:" + e.toString());
451		}
452	}
453
454	/**
455	 * Sets up the fixture, for example, open a network connection. This method
456	 * is called before a test is executed.
457	 */
458	protected void setUp() {
459	}
460
461	/**
462	 * Tears down the fixture, for example, close a network connection. This
463	 * method is called after a test is executed.
464	 */
465	protected void tearDown() {
466	}
467
468	protected void doneSuite() {
469	}
470}
471