1 // Tags: JDK1.1 |
|
2 // Uses: MulticastServer MulticastClient |
|
3 |
|
4 /* |
|
5 Copyright (C) 1999 Hewlett-Packard Company |
|
6 |
|
7 This file is part of Mauve. |
|
8 |
|
9 Mauve is free software; you can redistribute it and/or modify |
|
10 it under the terms of the GNU General Public License as published by |
|
11 the Free Software Foundation; either version 2, or (at your option) |
|
12 any later version. |
|
13 |
|
14 Mauve is distributed in the hope that it will be useful, |
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 GNU General Public License for more details. |
|
18 |
|
19 You should have received a copy of the GNU General Public License |
|
20 along with Mauve; see the file COPYING. If not, write to |
|
21 the Free Software Foundation, 59 Temple Place - Suite 330, |
|
22 Boston, MA 02111-1307, USA. |
|
23 */ |
|
24 |
|
25 package gnu.testlet.java.net.MulticastSocket; |
|
26 import gnu.testlet.Testlet; |
|
27 import gnu.testlet.TestHarness; |
|
28 import java.net.*; |
|
29 import java.io.*; |
|
30 |
|
31 |
|
32 public class MulticastSocketTest implements Testlet |
|
33 { |
|
34 protected static TestHarness harness; |
|
35 public void test_Basics() |
|
36 { |
|
37 MulticastSocket socket; |
|
38 int nPort = 0; |
|
39 |
|
40 // Test for incorrect ipaddress, port and a port in use. |
|
41 |
|
42 try { |
|
43 socket = new MulticastSocket(4441); |
|
44 InetAddress address = InetAddress.getByName("15.0.0.1"); |
|
45 socket.joinGroup(address); |
|
46 harness.fail("Wrong ipaddress arg. - 1"); |
|
47 } catch (IOException e) { |
|
48 harness.check(true); |
|
49 } |
|
50 |
|
51 |
|
52 try { |
|
53 socket = new MulticastSocket(-1); |
|
54 harness.fail("Wrong port arg. - 2"); |
|
55 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
56 socket.joinGroup(address); |
|
57 |
|
58 } catch (Exception e) { |
|
59 harness.check(true); |
|
60 } |
|
61 |
|
62 |
|
63 try { |
|
64 socket = new MulticastSocket(0); |
|
65 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
66 socket.joinGroup(address); |
|
67 socket.leaveGroup(address); |
|
68 socket.close(); |
|
69 harness.check(true); |
|
70 } catch (Exception e) { |
|
71 harness.fail("Correct args. - 3"); |
|
72 harness.debug(e); |
|
73 } |
|
74 |
|
75 |
|
76 try { |
|
77 socket = new MulticastSocket(); |
|
78 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
79 nPort = socket.getLocalPort(); |
|
80 socket.joinGroup(address); |
|
81 socket.leaveGroup(address); |
|
82 socket.close(); |
|
83 harness.check(true); |
|
84 } catch (Exception e) { |
|
85 harness.fail("Correct args. different constructor. - 4"); |
|
86 harness.debug(e); |
|
87 } |
|
88 |
|
89 |
|
90 try { |
|
91 socket = new MulticastSocket(nPort); |
|
92 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
93 socket.joinGroup(address); |
|
94 socket.joinGroup(address); |
|
95 harness.fail("joinGroup() twice."); |
|
96 } catch (Exception e) { |
|
97 harness.check(true); |
|
98 } |
|
99 |
|
100 try { |
|
101 socket = new MulticastSocket(++nPort); |
|
102 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
103 socket.joinGroup(null); |
|
104 harness.fail("joinGroup() with incorrect params. - 5"); |
|
105 } catch (NullPointerException e) { |
|
106 harness.check(true); |
|
107 } |
|
108 catch(Exception e) { |
|
109 harness.fail("joinGroup() with incorrect params. should have " + |
|
110 "thrown a NullPointerException - 5a"); |
|
111 harness.debug(e); |
|
112 } |
|
113 |
|
114 |
|
115 try { |
|
116 socket = new MulticastSocket(++nPort); |
|
117 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
118 socket.joinGroup(address); |
|
119 socket.leaveGroup(address); |
|
120 harness.check(true); |
|
121 socket.leaveGroup(address); |
|
122 harness.fail("leaveGroup() twice. - 6"); |
|
123 socket.close(); |
|
124 } catch (Exception e) { |
|
125 harness.check(true); |
|
126 } |
|
127 |
|
128 |
|
129 try { |
|
130 socket = new MulticastSocket(++nPort); |
|
131 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
132 socket.joinGroup(address); |
|
133 socket.leaveGroup(null); |
|
134 harness.fail("leaveGroup() with incorrect params - 7"); |
|
135 socket.close(); |
|
136 } catch (NullPointerException e) { |
|
137 harness.check(true); |
|
138 } catch (Exception e) { |
|
139 harness.fail("leaveGroup() with incorrect params. should have " + |
|
140 "thrown a NullPointerException - 7a"); |
|
141 harness.debug(e); |
|
142 } |
|
143 |
|
144 |
|
145 try { |
|
146 socket = new MulticastSocket(++nPort); |
|
147 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
148 socket.joinGroup(address); |
|
149 socket.leaveGroup(address); |
|
150 socket.close(); |
|
151 harness.check(true); |
|
152 } catch (Exception e) { |
|
153 harness.fail("Correct args. - 8"); |
|
154 harness.debug(e); |
|
155 } |
|
156 |
|
157 |
|
158 try { |
|
159 // System.out.println("getTTL() and setTTL()."); |
|
160 socket = new MulticastSocket(++nPort); |
|
161 InetAddress address = InetAddress.getByName("230.0.0.1"); |
|
162 socket.joinGroup(address); |
|
163 byte bTTL = socket.getTTL(); |
|
164 harness.check(bTTL != 0, "getTTL() should never return zero - 9"); |
|
165 |
|
166 //System.out.println("Default TTL = " + bTTL); |
|
167 byte newbTTL = (byte)127; |
|
168 socket.setTTL(newbTTL); |
|
169 bTTL = socket.getTTL(); |
|
170 //System.out.println("New TTL = " + bTTL); |
|
171 harness.check(bTTL, newbTTL, |
|
172 "getTTL() should return same value (127) used for setTTL() - 10"); |
|
173 |
|
174 bTTL = (byte)-56; |
|
175 socket.setTTL(bTTL); |
|
176 bTTL = socket.getTTL(); |
|
177 //System.out.println("Newer TTL = " + bTTL); |
|
178 // FIXME: if unsigned byte is used -56 will roll to a +ve value. |
|
179 // Developer should verify if this is a failure case or not. |
|
180 //if(bTTL == -56) |
|
181 //System.out.println("FAIL : TTL cannot be negative"); |
|
182 |
|
183 socket.setTTL((byte)1); |
|
184 socket.leaveGroup(address); |
|
185 socket.close(); |
|
186 harness.check(true); |
|
187 } catch (Exception e) { |
|
188 harness.fail("Should not have thrown any exception - 11"); |
|
189 harness.debug(e); |
|
190 } |
|
191 |
|
192 |
|
193 } |
|
194 |
|
195 public void test_MultipleBind() { |
|
196 |
|
197 final int sharedMcastPort = 1234; |
|
198 |
|
199 // First Socket |
|
200 |
|
201 MulticastSocket firstMcastSock; |
|
202 try { |
|
203 firstMcastSock = new MulticastSocket(sharedMcastPort); |
|
204 harness.check(true); |
|
205 } |
|
206 catch (Exception e) { |
|
207 harness.fail("could not create FIRST multicast socket on shared port " + sharedMcastPort); |
|
208 harness.debug(e); |
|
209 } |
|
210 |
|
211 // Second Socket |
|
212 |
|
213 MulticastSocket secondMcastSock; |
|
214 try { |
|
215 secondMcastSock = new MulticastSocket(sharedMcastPort); |
|
216 harness.check(true); |
|
217 } |
|
218 catch (Exception e) { |
|
219 harness.fail("could not create SECOND multicast socket on shared port " + sharedMcastPort); |
|
220 harness.debug(e); |
|
221 } |
|
222 } |
|
223 |
|
224 |
|
225 public void test_Comm(){ |
|
226 try { |
|
227 MulticastClient client = new MulticastClient(); |
|
228 client.start(); |
|
229 MulticastServer server = new MulticastServer(4446); |
|
230 server.start(); |
|
231 harness.check(true); |
|
232 }catch(Exception e){ |
|
233 harness.fail("test_Comm failed"); |
|
234 harness.debug(e); |
|
235 } |
|
236 } |
|
237 |
|
238 public void testall() |
|
239 { |
|
240 test_Basics(); |
|
241 test_MultipleBind(); |
|
242 test_Comm(); |
|
243 } |
|
244 |
|
245 public void test (TestHarness the_harness) |
|
246 { |
|
247 harness = the_harness; |
|
248 testall (); |
|
249 } |
|
250 } |
|