1 // Tags: JDK1.1 |
|
2 |
|
3 /* |
|
4 Copyright (C) 1999 Hewlett-Packard Company |
|
5 |
|
6 This file is part of Mauve. |
|
7 |
|
8 Mauve is free software; you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation; either version 2, or (at your option) |
|
11 any later version. |
|
12 |
|
13 Mauve is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Mauve; see the file COPYING. If not, write to |
|
20 the Free Software Foundation, 59 Temple Place - Suite 330, |
|
21 Boston, MA 02111-1307, USA. |
|
22 */ |
|
23 /********************************************** |
|
24 * File name: DatagramPacketTest2.java |
|
25 **********************************************/ |
|
26 |
|
27 package gnu.testlet.java.net.DatagramPacket; |
|
28 import gnu.testlet.Testlet; |
|
29 import gnu.testlet.TestHarness; |
|
30 import java.net.*; |
|
31 |
|
32 /************************************************************** |
|
33 * |
|
34 * What does the test do? |
|
35 * ---------------------- |
|
36 * |
|
37 * This test is for DatagramPacket class. |
|
38 * It tests for Exceptions, by passing invalid arguments to |
|
39 * the constructors and methods. |
|
40 * |
|
41 * How do I run the test? |
|
42 * ---------------------- |
|
43 * |
|
44 * Usage: java DatagramPacketTest2 |
|
45 * |
|
46 * What about the test result? |
|
47 * --------------------------- |
|
48 * |
|
49 * If an Exception is not thrown, when it should have been, |
|
50 * and vice-versa, then the error is displayed on stdout and |
|
51 * the test continues. |
|
52 * |
|
53 * Check the file Test.out for any errors. |
|
54 * |
|
55 **************************************************************/ |
|
56 |
|
57 public class DatagramPacketTest2 implements Testlet |
|
58 { |
|
59 final static int INVALID_PORT = -1; |
|
60 final static int PORT = 7; |
|
61 final static int MAX_PORT = 65535; |
|
62 |
|
63 protected static TestHarness harness; |
|
64 public InetAddress ia; |
|
65 public byte [] buf; |
|
66 |
|
67 public DatagramPacketTest2() throws Exception |
|
68 { |
|
69 buf = new byte[10]; |
|
70 ia = InetAddress.getLocalHost(); |
|
71 } |
|
72 |
|
73 private void errormsg(String m, int num, boolean flag, String e) |
|
74 { |
|
75 if (e != null) |
|
76 { |
|
77 if (flag) |
|
78 harness.fail(m + ": " + "test " + num + |
|
79 " - Should throw " + e); |
|
80 else |
|
81 harness.fail(m + ": " + "test " + num + |
|
82 " - Should NOT throw " + e); |
|
83 } |
|
84 else |
|
85 harness.fail(m + ": " + "test " + num + |
|
86 " - Should NOT throw any Exception"); |
|
87 } |
|
88 |
|
89 // check for invalid data buffer |
|
90 public void invalid_buf() |
|
91 { |
|
92 try |
|
93 { |
|
94 DatagramPacket request = new DatagramPacket(null, 10, ia, PORT); |
|
95 errormsg("invalid_buf", 1, true, "NullPointerException"); |
|
96 } |
|
97 catch (NullPointerException e) |
|
98 { |
|
99 harness.check(true); |
|
100 } |
|
101 |
|
102 try |
|
103 { |
|
104 DatagramPacket request = new DatagramPacket(buf, buf.length, ia, |
|
105 PORT); |
|
106 request.setData(null); |
|
107 errormsg("invalid_buf", 2, true, "NullPointerException"); |
|
108 } |
|
109 catch (NullPointerException e) |
|
110 { |
|
111 harness.check(true); |
|
112 } |
|
113 |
|
114 try |
|
115 { |
|
116 DatagramPacket reply = new DatagramPacket(null, 10); |
|
117 errormsg("invalid_buf", 3, true, "NullPointerException"); |
|
118 } |
|
119 catch (NullPointerException e) |
|
120 { |
|
121 harness.check(true); |
|
122 } |
|
123 |
|
124 try |
|
125 { |
|
126 DatagramPacket reply = new DatagramPacket(buf, buf.length); |
|
127 reply.setData(null); |
|
128 errormsg("invalid_buf", 4, true, "NullPointerException"); |
|
129 } |
|
130 catch (NullPointerException e) |
|
131 { |
|
132 harness.check(true); |
|
133 } |
|
134 } |
|
135 |
|
136 // check for invalid data buffer length |
|
137 public void invalid_buflen() |
|
138 { |
|
139 try |
|
140 { |
|
141 DatagramPacket request = new DatagramPacket(buf, -1, ia, PORT); |
|
142 errormsg("invalid_buflen", 1, false, "IllegalArgumentException"); |
|
143 } |
|
144 catch (IllegalArgumentException e) |
|
145 { |
|
146 harness.check(true); |
|
147 } |
|
148 |
|
149 try |
|
150 { |
|
151 DatagramPacket request = new DatagramPacket(buf, 0, ia, PORT); |
|
152 harness.check(true); |
|
153 } |
|
154 catch (Exception e) |
|
155 { |
|
156 errormsg("invalid_buflen", 2, true, null); |
|
157 e.printStackTrace(); |
|
158 } |
|
159 |
|
160 try |
|
161 { |
|
162 |
|
163 DatagramPacket reply = new DatagramPacket(buf, -1); |
|
164 errormsg("invalid_buflen", 3, true, "IllegalArgumentException"); |
|
165 } |
|
166 catch (IllegalArgumentException e) |
|
167 { |
|
168 harness.check(true); |
|
169 } |
|
170 |
|
171 try |
|
172 { |
|
173 DatagramPacket reply = new DatagramPacket(buf, 0); |
|
174 harness.check(true); |
|
175 } |
|
176 catch (Exception e) |
|
177 { |
|
178 errormsg("invalid_buflen", 4, true, null); |
|
179 e.printStackTrace(); |
|
180 } |
|
181 |
|
182 try |
|
183 { |
|
184 DatagramPacket request = new DatagramPacket(buf, buf.length + 1, |
|
185 ia, PORT); |
|
186 errormsg("invalid_buflen", 5, true, "IllegalArgumentException"); |
|
187 } |
|
188 catch (IllegalArgumentException e) |
|
189 { |
|
190 harness.check(true); |
|
191 } |
|
192 |
|
193 try |
|
194 { |
|
195 DatagramPacket reply = new DatagramPacket(buf, buf.length + 1); |
|
196 errormsg("invalid_buflen", 6, true, "IllegalArgumentException"); |
|
197 } |
|
198 catch (IllegalArgumentException e) |
|
199 { |
|
200 harness.check(true); |
|
201 } |
|
202 |
|
203 try |
|
204 { |
|
205 DatagramPacket reply = new DatagramPacket(buf, buf.length); |
|
206 reply.setLength(buf.length + 1); |
|
207 errormsg("invalid_buflen", 7, true, "IllegalArgumentException"); |
|
208 } |
|
209 catch (IllegalArgumentException e) |
|
210 { |
|
211 harness.check(true); |
|
212 } |
|
213 } |
|
214 |
|
215 // check for invalid port number |
|
216 public void invalid_port() |
|
217 { |
|
218 try |
|
219 { |
|
220 DatagramPacket request = new DatagramPacket(buf, 10, ia, |
|
221 INVALID_PORT); |
|
222 errormsg("invalid_port", 1, true, "IllegalArgumentException"); |
|
223 } |
|
224 catch (IllegalArgumentException e) |
|
225 { |
|
226 harness.check(true); |
|
227 } |
|
228 |
|
229 try |
|
230 { |
|
231 DatagramPacket request = new DatagramPacket(buf, 10, ia, PORT); |
|
232 request.setPort(INVALID_PORT); |
|
233 errormsg("invalid_port", 2, true, "IllegalArgumentException"); |
|
234 } |
|
235 catch (IllegalArgumentException e) |
|
236 { |
|
237 harness.check(true); |
|
238 } |
|
239 |
|
240 try |
|
241 { |
|
242 DatagramPacket request2 = new DatagramPacket(buf, 10, ia, |
|
243 MAX_PORT + 1); |
|
244 errormsg("invalid_port", 3, true, "IllegalArgumentException"); |
|
245 } |
|
246 catch (IllegalArgumentException e) |
|
247 { |
|
248 harness.check(true); |
|
249 } |
|
250 |
|
251 try |
|
252 { |
|
253 DatagramPacket reply = new DatagramPacket(buf, 10); |
|
254 reply.setPort(INVALID_PORT); |
|
255 errormsg("invalid_port", 4, true, "IllegalArgumentException"); |
|
256 } |
|
257 catch (IllegalArgumentException e) |
|
258 { |
|
259 harness.check(true); |
|
260 } |
|
261 |
|
262 try |
|
263 { |
|
264 DatagramPacket reply = new DatagramPacket(buf, 10); |
|
265 reply.setPort(MAX_PORT + 1); |
|
266 errormsg("invalid_port", 5, true, "IllegalArgumentException"); |
|
267 } |
|
268 catch (IllegalArgumentException e) |
|
269 { |
|
270 harness.check(true); |
|
271 } |
|
272 } |
|
273 |
|
274 public void test (TestHarness the_harness) |
|
275 { |
|
276 harness = the_harness; |
|
277 testall (); |
|
278 } |
|
279 |
|
280 |
|
281 public void testall() |
|
282 { |
|
283 DatagramPacketTest2 m = null; |
|
284 try |
|
285 { |
|
286 m = new DatagramPacketTest2(); |
|
287 harness.check(true); |
|
288 } |
|
289 catch (Exception e) |
|
290 { |
|
291 harness.fail("DatagramPacketTest2 constructor"); |
|
292 e.printStackTrace(); |
|
293 System.exit(1); |
|
294 } |
|
295 |
|
296 m.invalid_buf(); |
|
297 m.invalid_buflen(); |
|
298 m.invalid_port(); |
|
299 } |
|
300 } |
|