1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.jetspeed.security.impl.AuthenticationProviderImpl;
27 import org.apache.jetspeed.security.impl.AuthenticationProviderProxyImpl;
28 import org.apache.jetspeed.security.impl.GroupManagerImpl;
29 import org.apache.jetspeed.security.impl.LoginModuleProxyImpl;
30 import org.apache.jetspeed.security.impl.RoleManagerImpl;
31 import org.apache.jetspeed.security.impl.SecurityProviderImpl;
32 import org.apache.jetspeed.security.impl.UserManagerImpl;
33 import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
34
35 /***
36 * <p>
37 * Unit testing for {@link TestAuthenticationProviderProxy}.
38 * </p>
39 *
40 * TODO Needs an LDAP server configured for most of those tests to be valid.
41 * Commented until embedded ldap is supported.
42 *
43 * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
44 */
45 public class TestAuthenticationProviderProxy extends AbstractSecurityTestcase
46 {
47 int userCount = 0;
48 int usersAdded = 0;
49
50 /***
51 * @see junit.framework.TestCase#setUp()
52 */
53 protected void setUp() throws Exception
54 {
55 super.setUp();
56 destroyTestData();
57
58
59
60
61
62
63
64
65 AuthenticationProvider defaultAtnProvider = new AuthenticationProviderImpl("DefaultAuthenticator",
66 "The default authenticator", "login.conf", ch, ush);
67
68
69
70
71 List atnProviders = new ArrayList();
72 atnProviders.add(defaultAtnProvider);
73
74 AuthenticationProviderProxy atnProviderProxy = new AuthenticationProviderProxyImpl(atnProviders,
75 "DefaultAuthenticator");
76
77
78 securityProvider = new SecurityProviderImpl(atnProviderProxy, rsh, gsh, smh);
79 ums = new UserManagerImpl(securityProvider);
80 gms = new GroupManagerImpl(securityProvider);
81 rms = new RoleManagerImpl(securityProvider);
82
83
84 new LoginModuleProxyImpl(ums);
85 }
86
87 /***
88 * @see junit.framework.TestCase#tearDown()
89 */
90 public void tearDown() throws Exception
91 {
92 destroyTestData();
93 super.tearDown();
94 }
95
96 public static Test suite()
97 {
98 return new TestSuite(TestAuthenticationProviderProxy.class);
99 }
100
101 /***
102 * <p>
103 * Test user manager.
104 * </p>
105 */
106 public void testUserManager()
107 {
108 initTestData();
109
110 try
111 {
112
113
114
115
116
117
118
119
120
121
122 User user = ums.getUser("anonuser1");
123 assertNotNull(user);
124 assertEquals("anonuser1", SecurityHelper.getPrincipal(user.getSubject(), UserPrincipal.class).getName());
125
126
127
128
129
130
131
132 assertTrue(ums.authenticate("anonuser2", "password"));
133 assertFalse(ums.authenticate("anonuser3", "pword"));
134
135
136 Iterator users = ums.getUsers("");
137 int count = 0;
138 while (users.hasNext())
139 {
140 users.next();
141 count++;
142 }
143
144
145
146 assertEquals(userCount + usersAdded, count);
147 }
148 catch (SecurityException sex)
149 {
150 assertTrue("security exception caught: " + sex, false);
151 }
152
153 destroyTestData();
154 }
155
156 /***
157 * <p>
158 * Test role manager.
159 * </p>
160 */
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 /***
205 * <p>
206 * Test group manager.
207 * </p>
208 */
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 /***
253 * <p>
254 * Init test data.
255 * </p>
256 */
257 private void initTestData()
258 {
259 final String[] users = new String[] { "anonuser1", "anonuser2", "anonuser3", "anonuser4", "anonuser5", };
260 final String[] roles = new String[] { "testrole1", "testrole1.subrole1", "testrole1.subrole1.subrole2",
261 "testrole2", "testrole2.subrole1" };
262 final String[] groups = new String[] { "testgroup1", "testgroup1.subgroup1", "testgroup1.subgroup1.subgroup2",
263 "testgroup2", "testgroup2.subgroup1" };
264
265
266
267 try
268 {
269 Iterator it = ums.getUsers("");
270 userCount = 0;
271 while (it.hasNext())
272 {
273 it.next();
274 userCount++;
275 }
276 }
277 catch (Exception e)
278 {
279
280 }
281
282 usersAdded = 0;
283 for (int i = 0; i < users.length; i++)
284 {
285 try
286 {
287 ums.addUser(users[i], "password");
288 usersAdded++;
289 }
290 catch (SecurityException e)
291 {
292 System.err.println(e.toString());
293 }
294 }
295
296 for (int i = 0; i < roles.length; i++)
297 {
298 try
299 {
300 rms.addRole(roles[i]);
301 }
302 catch (SecurityException e)
303 {
304 System.err.println(e.toString());
305 }
306 }
307
308 for (int i = 0; i < groups.length; i++)
309 {
310 try
311 {
312 gms.addGroup(groups[i]);
313 }
314 catch (SecurityException e)
315 {
316 System.err.println(e.toString());
317 }
318 }
319 }
320
321 /***
322 * <p>
323 * Destroy test data.
324 * </p>
325 */
326 private void destroyTestData()
327 {
328 try
329 {
330 Iterator userIter = ums.getUsers("");
331 User user;
332 String userName;
333 while (userIter.hasNext())
334 {
335 user = (User) userIter.next();
336 userName = SecurityHelper.getPrincipal(user.getSubject(), UserPrincipal.class).getName();
337 if (!userName.equals(ums.getAnonymousUser()))
338 {
339 ums.removeUser(userName);
340 }
341 }
342 }
343 catch (SecurityException e)
344 {
345 System.err.println(e.toString());
346 }
347
348 final String[] roles = new String[] { "testrole1", "testrole1.subrole1", "testrole1.subrole1.subrole2",
349 "testrole2", "testrole2.subrole1" };
350 final String[] groups = new String[] { "testgroup1", "testgroup1.subgroup1", "testgroup1.subgroup1.subgroup2",
351 "testgroup2", "testgroup2.subgroup1" };
352
353 for (int i = 0; i < roles.length; i++)
354 {
355 try
356 {
357 rms.removeRole(roles[i]);
358 }
359 catch (SecurityException e)
360 {
361 System.err.println(e.toString());
362 }
363 }
364
365 for (int i = 0; i < groups.length; i++)
366 {
367 try
368 {
369 gms.removeGroup(groups[i]);
370 }
371 catch (SecurityException e)
372 {
373 System.err.println(e.toString());
374 }
375 }
376 }
377 }