1 // $Id: getProviders.java,v 1.2 2006/01/03 09:24:39 raif Exp $ |
|
2 // |
|
3 // Copyright (C) 2003 Free Software Foundation, Inc. |
|
4 // |
|
5 // This file is part of Mauve. |
|
6 // |
|
7 // Mauve is free software; you can redistribute it and/or modify |
|
8 // it under the terms of the GNU General Public License as published by |
|
9 // the Free Software Foundation; either version 2, or (at your option) |
|
10 // any later version. |
|
11 // |
|
12 // Mauve is distributed in the hope that it will be useful, |
|
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 // GNU General Public License for more details. |
|
16 // |
|
17 // You should have received a copy of the GNU General Public License |
|
18 // along with Mauve; see the file COPYING. If not, write to |
|
19 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
20 // Boston, MA 02111-1307, USA. |
|
21 |
|
22 // Tags: JDK1.3 |
|
23 |
|
24 package gnu.testlet.java.security.Security; |
|
25 |
|
26 import gnu.testlet.TestHarness; |
|
27 import gnu.testlet.Testlet; |
|
28 import java.security.Security; |
|
29 import java.security.Provider; |
|
30 import java.security.InvalidParameterException; |
|
31 |
|
32 /** |
|
33 * Test of <code>getProviders(String)</code> and <code>getProviders(Map) |
|
34 * methods in {@link java.security.Security}. |
|
35 * |
|
36 * @version $Revision: 1.2 $ |
|
37 * @see java.security.Security#getProviders(String) |
|
38 * @see java.security.Security#getProviders(java.util.Map) |
|
39 */ |
|
40 public class getProviders implements Testlet |
|
41 { |
|
42 Provider tom = new Tom(); |
|
43 Provider dick = new Dick(); |
|
44 Provider harry = new Harry(); |
|
45 |
|
46 public void test (TestHarness harness) |
|
47 { |
|
48 harness.checkPoint("getProviders(String)"); |
|
49 |
|
50 testNoProviders(harness); |
|
51 test1Provider(harness); |
|
52 test2Providers(harness); |
|
53 test3Providers(harness); |
|
54 } |
|
55 |
|
56 private void testNoProviders(TestHarness harness) |
|
57 { |
|
58 String filter = "NoService.NoAlgorithm"; |
|
59 // try with dummy filter and no providers installed |
|
60 try |
|
61 { |
|
62 harness.check(Security.getProviders(filter), null); |
|
63 } |
|
64 catch (Exception x) |
|
65 { |
|
66 harness.debug(x); |
|
67 harness.fail("testNoProviders.1: "+String.valueOf(x)); |
|
68 } |
|
69 } |
|
70 |
|
71 private void test1Provider(TestHarness harness) |
|
72 { |
|
73 Security.addProvider(tom); |
|
74 |
|
75 String signature = "Security.getProvider(\"tom\")"; |
|
76 try |
|
77 { |
|
78 Provider sameProvider = Security.getProvider(" Tom "); |
|
79 harness.check(sameProvider != null, signature); |
|
80 } |
|
81 catch (Throwable x) |
|
82 { |
|
83 harness.fail(signature); |
|
84 harness.debug(x); |
|
85 } |
|
86 |
|
87 String filter = "NoService.NoAlgorithm"; |
|
88 // try dummy filter with one known provider |
|
89 try |
|
90 { |
|
91 harness.check(Security.getProviders(filter), null); |
|
92 } |
|
93 catch (Exception x) |
|
94 { |
|
95 harness.debug(x); |
|
96 harness.fail("test1Provider.2: "+String.valueOf(x)); |
|
97 } |
|
98 |
|
99 Provider[] providers; |
|
100 // try real filter with one known provider |
|
101 filter = "CoffeeMaker.Foo"; |
|
102 try |
|
103 { |
|
104 providers = Security.getProviders(filter); |
|
105 if (providers == null || providers.length != 1) |
|
106 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
107 else |
|
108 harness.check(providers[0], tom); |
|
109 } |
|
110 catch (Exception x) |
|
111 { |
|
112 harness.debug(x); |
|
113 harness.fail("test1Provider.3: "+String.valueOf(x)); |
|
114 } |
|
115 |
|
116 // try real filter (different case) with one known provider |
|
117 filter = "CoffeeMaker.FOO"; |
|
118 try |
|
119 { |
|
120 providers = Security.getProviders(filter); |
|
121 if (providers == null || providers.length != 1) |
|
122 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
123 else |
|
124 harness.check(providers[0], tom); |
|
125 } |
|
126 catch (Exception x) |
|
127 { |
|
128 harness.debug(x); |
|
129 harness.fail("test1Provider.4: "+String.valueOf(x)); |
|
130 } |
|
131 |
|
132 // try incorrect (syntax) filter with one known provider |
|
133 filter = "CoffeeMakerFoo"; |
|
134 try |
|
135 { |
|
136 providers = Security.getProviders(filter); |
|
137 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
138 } |
|
139 catch (InvalidParameterException x) // expected |
|
140 { |
|
141 harness.check(true); |
|
142 } |
|
143 catch (Exception x) |
|
144 { |
|
145 harness.debug(x); |
|
146 harness.fail("test1Provider.5: "+String.valueOf(x)); |
|
147 } |
|
148 |
|
149 // try filter alias (1 indirection) with one known provider |
|
150 filter = "CoffeeMaker.Bar"; |
|
151 try |
|
152 { |
|
153 providers = Security.getProviders(filter); |
|
154 if (providers == null || providers.length != 1) |
|
155 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
156 else |
|
157 harness.check(providers[0], tom); |
|
158 } |
|
159 catch (Exception x) |
|
160 { |
|
161 harness.debug(x); |
|
162 harness.fail("test1Provider.6: "+String.valueOf(x)); |
|
163 } |
|
164 |
|
165 // try filter alias (2 indirections) with one known provider |
|
166 filter = "CoffeeMaker.WHAT"; |
|
167 try |
|
168 { |
|
169 providers = Security.getProviders(filter); |
|
170 if (providers == null || providers.length != 1) |
|
171 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
172 else |
|
173 harness.check(providers[0], tom); |
|
174 } |
|
175 catch (Exception x) |
|
176 { |
|
177 harness.debug(x); |
|
178 harness.fail("test1Provider.7: "+String.valueOf(x)); |
|
179 } |
|
180 |
|
181 // try real filter (incl. attr only) with one known provider |
|
182 filter = "CoffeeMaker.FOO ImplementedIn:vapourware"; |
|
183 try |
|
184 { |
|
185 providers = Security.getProviders(filter); |
|
186 if (providers == null || providers.length != 1) |
|
187 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
188 else |
|
189 harness.check(providers[0], tom); |
|
190 } |
|
191 catch (Exception x) |
|
192 { |
|
193 harness.debug(x); |
|
194 harness.fail("test1Provider.8: "+String.valueOf(x)); |
|
195 } |
|
196 |
|
197 // try real filter (incl. attr+val) with one known provider |
|
198 filter = "CoffeeMaker.FOO minCapacity:150"; |
|
199 try |
|
200 { |
|
201 providers = Security.getProviders(filter); |
|
202 if (providers == null || providers.length != 1) |
|
203 harness.fail("Tom : getProviders(\""+filter+"\")"); |
|
204 else |
|
205 harness.check(providers[0], tom); |
|
206 } |
|
207 catch (Exception x) |
|
208 { |
|
209 harness.debug(x); |
|
210 harness.fail("test1Provider.9: "+String.valueOf(x)); |
|
211 } |
|
212 } |
|
213 |
|
214 private void test2Providers(TestHarness harness) |
|
215 { |
|
216 Security.addProvider(dick); |
|
217 |
|
218 Provider[] providers; |
|
219 String filter = "NoService.NoAlgorithm"; |
|
220 |
|
221 // try real filter (incl. attr only) with two known providers |
|
222 filter = "CoffeeMaker.FOO ImplementedIn:vapourware"; |
|
223 try |
|
224 { |
|
225 providers = Security.getProviders(filter); |
|
226 if (providers == null || providers.length != 2 |
|
227 || (providers[0] != tom && providers[1] != dick)) |
|
228 harness.fail("Tom, Dick : getProviders(\""+filter+"\")"); |
|
229 else |
|
230 harness.check(true); |
|
231 } |
|
232 catch (Exception x) |
|
233 { |
|
234 harness.debug(x); |
|
235 harness.fail("test2Providers.1: "+String.valueOf(x)); |
|
236 } |
|
237 |
|
238 // try real filter (incl. attr+val) with two known providers |
|
239 filter = "CoffeeMaker.FOO minCapacity:150"; |
|
240 try |
|
241 { |
|
242 providers = Security.getProviders(filter); |
|
243 if (providers == null || providers.length != 2 |
|
244 || (providers[0] != tom && providers[1] != dick)) |
|
245 harness.fail("Tom, Dick : getProviders(\""+filter+"\")"); |
|
246 else |
|
247 harness.check(true); |
|
248 } |
|
249 catch (Exception x) |
|
250 { |
|
251 harness.debug(x); |
|
252 harness.fail("test2Providers.2: "+String.valueOf(x)); |
|
253 } |
|
254 } |
|
255 |
|
256 private void test3Providers(TestHarness harness) |
|
257 { |
|
258 Security.addProvider(harry); |
|
259 |
|
260 Provider[] p; |
|
261 String filter = "NoService.NoAlgorithm"; |
|
262 |
|
263 // try real filter (incl. attr only) with three known providers |
|
264 filter = "CoffeeMaker.FOO ImplementedIn:vapourware"; |
|
265 try |
|
266 { |
|
267 p = Security.getProviders(filter); |
|
268 if (p == null || p.length != 3 || |
|
269 (p[0] != tom && p[1] != dick && p[2] != harry)) |
|
270 harness.fail("Tom, Dick, Harry : getProviders(\""+filter+"\")"); |
|
271 else |
|
272 harness.check(true); |
|
273 } |
|
274 catch (Exception x) |
|
275 { |
|
276 harness.debug(x); |
|
277 harness.fail("test3Providers.1: "+String.valueOf(x)); |
|
278 } |
|
279 |
|
280 // try real filter (incl. attr+val) with three known providers |
|
281 filter = "CoffeeMaker.FOO minCapacity:150"; |
|
282 try |
|
283 { |
|
284 p = Security.getProviders(filter); |
|
285 if (p == null || p.length != 3 || |
|
286 (p[0] != tom && p[1] != dick && p[2] != harry)) |
|
287 harness.fail("Tom, Dick, Harry : getProviders(\""+filter+"\")"); |
|
288 else |
|
289 harness.check(true); |
|
290 } |
|
291 catch (Exception x) |
|
292 { |
|
293 harness.debug(x); |
|
294 harness.fail("test3Providers.2: "+String.valueOf(x)); |
|
295 } |
|
296 } |
|
297 |
|
298 // Inner class(es) |
|
299 // ========================================================================== |
|
300 |
|
301 class Tom extends Provider |
|
302 { |
|
303 Tom() |
|
304 { |
|
305 super("Tom", 1.0, ""); |
|
306 |
|
307 put("CoffeeMaker.Foo", "acme.crypto.FooSpi"); |
|
308 put("CoffeeMaker.Foo ImplementedIn", "Vapourware"); |
|
309 put("CoffeeMaker.Foo MinCapacity", "100"); |
|
310 put("Alg.Alias.CoffeeMaker.bar", "fOO"); |
|
311 put("Alg.Alias.CoffeeMaker.what", "bar"); |
|
312 } |
|
313 } |
|
314 |
|
315 class Dick extends Provider |
|
316 { |
|
317 Dick() |
|
318 { |
|
319 super("Dick", 2.0, ""); |
|
320 |
|
321 put("CoffeeMaker.Foo", "acme.crypto.FooSpi"); |
|
322 put("CoffeeMaker.Foo ImplementedIn", "Vapourware"); |
|
323 put("CoffeeMaker.Foo MinCapacity", "120"); |
|
324 put("Alg.Alias.CoffeeMaker.bar", "fOO"); |
|
325 put("Alg.Alias.CoffeeMaker.what", "bar"); |
|
326 } |
|
327 } |
|
328 |
|
329 class Harry extends Provider |
|
330 { |
|
331 Harry() |
|
332 { |
|
333 super("Harry", 3.0, ""); |
|
334 |
|
335 put("CoffeeMaker.Foo", "acme.crypto.FooSpi"); |
|
336 put("CoffeeMaker.Foo ImplementedIn", "Vapourware"); |
|
337 put("CoffeeMaker.Foo MinCapacity", "140"); |
|
338 put("Alg.Alias.CoffeeMaker.bar", "fOO"); |
|
339 put("Alg.Alias.CoffeeMaker.what", "bar"); |
|
340 } |
|
341 } |
|
342 } |
|