001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security;
018:
019: import java.security.Principal;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.prefs.Preferences;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.jetspeed.security.impl.GroupPrincipalImpl;
028: import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
029:
030: /**
031: * <p>
032: * Unit testing for {@link GroupManager}.
033: * </p>
034: *
035: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
036: * @version $Id: TestGroupManager.java 517719 2007-03-13 15:05:48Z ate $
037: */
038: public class TestGroupManager extends AbstractSecurityTestcase {
039:
040: /**
041: * @see junit.framework.TestCase#setUp()
042: */
043: protected void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: /**
048: * @see junit.framework.TestCase#tearDown()
049: */
050: public void tearDown() throws Exception {
051: destroyGroups();
052: super .tearDown();
053:
054: }
055:
056: public static Test suite() {
057: // All methods starting with "test" will be executed in the test suite.
058: return new TestSuite(TestGroupManager.class);
059: }
060:
061: /**
062: * <p>
063: * Test add group.
064: * </p>
065: */
066: public void testAddGroup() {
067: try {
068: gms.addGroup("testgroup");
069: } catch (SecurityException sex) {
070: assertTrue(
071: "group should not already exists. exception caught: "
072: + sex, false);
073: }
074: try {
075: gms.addGroup("testgroup.newgroup0");
076: } catch (SecurityException sex) {
077: assertTrue(
078: "group should not already exists. exception caught: "
079: + sex, false);
080: }
081: // Add existing group.
082: try {
083: gms.addGroup("testgroup.newgroup0");
084: assertTrue(
085: "group should already exists. exception not thrown.",
086: false);
087: } catch (SecurityException sex) {
088: }
089:
090: // Cleanup test.
091: try {
092: gms.removeGroup("testgroup");
093: } catch (SecurityException sex) {
094: assertTrue("could not remove group. exception caught: "
095: + sex, false);
096: }
097: }
098:
099: /**
100: * <p>
101: * Test add user to group.
102: * </p>
103: */
104: public void testAddUserToGroup() {
105: // Init test.
106: try {
107: ums.addUser("anonuser1", "password");
108: gms.addGroup("testusertogroup1");
109: gms.addGroup("testusertogroup1.group1");
110: gms.addGroup("testusertogroup1.group2");
111: } catch (SecurityException sex) {
112: assertTrue("failed to init testAddUserToGroup(), " + sex,
113: false);
114: }
115: // Add group with no prior groups.
116: try {
117: gms.addUserToGroup("anonuser1", "testusertogroup1.group1");
118: Collection principals = ums.getUser("anonuser1")
119: .getSubject().getPrincipals();
120: assertTrue(
121: "anonuser1 should contain testusertogroup1.group1",
122: principals.contains(new GroupPrincipalImpl(
123: "testusertogroup1.group1")));
124: } catch (SecurityException sex) {
125: assertTrue("should add user to group. exception caught: "
126: + sex, false);
127: }
128: // Add group with existing groups.
129: try {
130: gms.addUserToGroup("anonuser1", "testusertogroup1.group2");
131: Collection principals = ums.getUser("anonuser1")
132: .getSubject().getPrincipals();
133: assertTrue(
134: "anonuser1 should contain testusertogroup1.group2",
135: principals.contains(new GroupPrincipalImpl(
136: "testusertogroup1.group2")));
137: } catch (SecurityException sex) {
138: assertTrue("should add user to group. exception caught: "
139: + sex, false);
140: }
141: // Add group when user does not exist.
142: try {
143: gms
144: .addUserToGroup("anonuser123",
145: "testusertogroup1.group2");
146: assertTrue("should catch exception: user does not exist.",
147: false);
148: } catch (SecurityException sex) {
149: }
150: // Add group when group does not exist.
151: try {
152: gms
153: .addUserToGroup("anonuser1",
154: "testusertogroup1.group123");
155: assertTrue("should catch exception: group does not exist.",
156: false);
157: } catch (SecurityException sex) {
158: }
159:
160: // Cleanup test.
161: try {
162: ums.removeUser("anonuser1");
163: gms.removeGroup("testusertogroup1");
164: } catch (SecurityException sex) {
165: assertTrue(
166: "could not remove user and group. exception caught: "
167: + sex, false);
168: }
169: }
170:
171: /**
172: * <p>
173: * Test remove group.
174: * </p>
175: */
176: public void testRemoveGroup() {
177: // Init test.
178: try {
179: ums.addUser("anonuser2", "password");
180: gms.addGroup("testgroup1");
181: gms.addGroup("testgroup1.group1");
182: gms.addGroup("testgroup1.group2");
183: gms.addGroup("testgroup2");
184: gms.addGroup("testgroup2.group1");
185: gms.addUserToGroup("anonuser2", "testgroup1.group1");
186: gms.addUserToGroup("anonuser2", "testgroup1.group2");
187: gms.addUserToGroup("anonuser2", "testgroup2.group1");
188: } catch (SecurityException sex) {
189: assertTrue("failed to init testRemoveGroup(), " + sex,
190: false);
191: }
192:
193: try {
194: gms.removeGroup("testgroup1.group1");
195: Collection principals = ums.getUser("anonuser2")
196: .getSubject().getPrincipals();
197: // because of hierarchical groups with generalization strategy as default. Was 5 groups + 1 user, should now be 5
198: // (4 groups + 1 user).
199: assertEquals(
200: "principal size should be == 5 after removing testgroup1.group1, for principals: "
201: + principals.toString(), 5, principals
202: .size());
203: assertFalse(
204: "anonuser2 should not contain testgroup1.group1",
205: principals.contains(new GroupPrincipalImpl(
206: "testgroup1.group1")));
207: } catch (SecurityException sex) {
208: assertTrue("should remove group. exception caught: " + sex,
209: false);
210: }
211:
212: // Cleanup test.
213: try {
214: ums.removeUser("anonuser2");
215: gms.removeGroup("testgroup1");
216: gms.removeGroup("testgroup2");
217: } catch (SecurityException sex) {
218: assertTrue(
219: "could not remove user and group. exception caught: "
220: + sex, false);
221: }
222: }
223:
224: /**
225: * <p>
226: * Test get group.
227: * </p>
228: */
229: public void testGetGroup() {
230: // Test when the group does not exist.
231: try {
232: gms.getGroup("testgroupdoesnotexist");
233: assertTrue(
234: "group does not exist. should have thrown an exception.",
235: false);
236: } catch (SecurityException sex) {
237: }
238: // Test when the group exists.
239: Group group = null;
240: try {
241: gms.addGroup("testgetgroup");
242: group = gms.getGroup("testgetgroup");
243: } catch (SecurityException sex) {
244: assertTrue(
245: "group exists. should not have thrown an exception.",
246: false);
247: }
248: assertNotNull("group is null", group);
249: // Test the Principal.
250: Principal groupPrincipal = group.getPrincipal();
251: assertNotNull("group principal is null", groupPrincipal);
252: assertEquals(
253: "expected group principal full path == testgetgroup",
254: "testgetgroup", groupPrincipal.getName());
255:
256: // Test the Group Preferences.
257: Preferences preferences = group.getPreferences();
258: assertEquals("expected group node == /group/testgetgroup",
259: SecurityHelper.getPreferencesFullPath(groupPrincipal),
260: preferences.absolutePath());
261:
262: // Cleanup test.
263: try {
264: gms.removeGroup("testgetgroup");
265: } catch (SecurityException sex) {
266: assertTrue("could not remove group. exception caught: "
267: + sex, false);
268: }
269: }
270:
271: /**
272: * <p>
273: * Test get groups for user.
274: * </p>
275: */
276: public void testGetGroupsForUser() {
277: // Init test.
278: try {
279: ums.addUser("anonuser2", "password");
280: gms.addGroup("testgroup1");
281: gms.addGroup("testgroup1.group1");
282: gms.addGroup("testgroup1.group2");
283: gms.addGroup("testgroup2");
284: gms.addGroup("testgroup2.group1");
285: gms.addUserToGroup("anonuser2", "testgroup1.group1");
286: gms.addUserToGroup("anonuser2", "testgroup1.group2");
287: gms.addUserToGroup("anonuser2", "testgroup2.group1");
288: } catch (SecurityException sex) {
289: assertTrue("failed to init testGetGroupsForUser(), " + sex,
290: false);
291: }
292:
293: try {
294: Collection groups = gms.getGroupsForUser("anonuser2");
295: // Default hierarchy used in by generalization.
296: assertEquals("groups size should be == 5", 5, groups.size());
297: } catch (SecurityException sex) {
298: assertTrue(
299: "user exists. should not have thrown an exception: "
300: + sex, false);
301: }
302:
303: // Cleanup test.
304: try {
305: ums.removeUser("anonuser2");
306: gms.removeGroup("testgroup1");
307: gms.removeGroup("testgroup2");
308: } catch (SecurityException sex) {
309: assertTrue(
310: "could not remove user and group. exception caught: "
311: + sex, false);
312: }
313: }
314:
315: /**
316: * <p>
317: * Test get groups in role.
318: * </p>
319: */
320: public void testGetGroupsInRole() {
321: // Init test.
322: try {
323: rms.addRole("testuserrolemapping");
324: gms.addGroup("testrolegroupmapping");
325: gms.addGroup("testrolegroupmapping.group1");
326: gms.addGroup("testrolegroupmapping.group2");
327: rms.addRoleToGroup("testuserrolemapping",
328: "testrolegroupmapping");
329: rms.addRoleToGroup("testuserrolemapping",
330: "testrolegroupmapping.group1");
331: rms.addRoleToGroup("testuserrolemapping",
332: "testrolegroupmapping.group2");
333: } catch (SecurityException sex) {
334: assertTrue("failed to init testGetRolesForGroup(), " + sex,
335: false);
336: }
337:
338: try {
339: Collection groups = gms
340: .getGroupsInRole("testuserrolemapping");
341: assertEquals("groups size should be == 3", 3, groups.size());
342: } catch (SecurityException sex) {
343: assertTrue(
344: "role exists. should not have thrown an exception: "
345: + sex, false);
346: }
347:
348: // Cleanup test.
349: try {
350: rms.removeRole("testuserrolemapping");
351: gms.removeGroup("testrolegroupmapping");
352: } catch (SecurityException sex) {
353: assertTrue(
354: "could not remove role and group. exception caught: "
355: + sex, false);
356: }
357: }
358:
359: /**
360: * <p>
361: * Test remove user from group.
362: * </p>
363: */
364: public void testRemoveUserFromGroup() {
365: // Init test.
366: try {
367: ums.addUser("anonuser4", "password");
368: gms.addGroup("testgroup1");
369: gms.addGroup("testgroup1.group1");
370: gms.addUserToGroup("anonuser4", "testgroup1.group1");
371: } catch (SecurityException sex) {
372: assertTrue("failed to init testRemoveUserFromGroup(), "
373: + sex, false);
374: }
375:
376: try {
377: gms.removeUserFromGroup("anonuser4", "testgroup1.group1");
378: Collection groups = gms.getGroupsForUser("anonuser4");
379: assertEquals("groups size should be == 0", 0, groups.size());
380: } catch (SecurityException sex) {
381: assertTrue(
382: "user exists. should not have thrown an exception: "
383: + sex, false);
384: }
385:
386: // Cleanup test.
387: try {
388: ums.removeUser("anonuser4");
389: gms.removeGroup("testgroup1");
390: } catch (SecurityException sex) {
391: assertTrue(
392: "could not remove user and group. exception caught: "
393: + sex, false);
394: }
395: }
396:
397: /**
398: * <p>
399: * Test is user in role.
400: * </p>
401: */
402: public void testIsUserInGroup() {
403: // Init test.
404: try {
405: ums.addUser("anonuser3", "password");
406: gms.addGroup("testgroup1");
407: gms.addGroup("testgroup1.group1");
408: gms.addUserToGroup("anonuser3", "testgroup1.group1");
409: } catch (SecurityException sex) {
410: assertTrue("failed to init testRemoveUserFromGroup(), "
411: + sex, false);
412: }
413:
414: try {
415: boolean isUserInGroup = gms.isUserInGroup("anonuser3",
416: "testgroup1.group1");
417: assertTrue(
418: "anonuser3 should be in group testgroup1.group1",
419: isUserInGroup);
420: } catch (SecurityException sex) {
421: assertTrue(
422: "user and group exist. should not have thrown an exception: "
423: + sex, false);
424: }
425:
426: // Cleanup test.
427: try {
428: ums.removeUser("anonuser4");
429: gms.removeGroup("testgroup1");
430: } catch (SecurityException sex) {
431: assertTrue(
432: "could not remove user and group. exception caught: "
433: + sex, false);
434: }
435: }
436:
437: /**
438: * <p>
439: * Test get groups.
440: * </p>
441: *
442: * @throws Exception Throws an exception.
443: */
444: public void testGetGroups() throws Exception {
445: int groupCount = 0;
446: int groupAdded = 0;
447: Iterator it = gms.getGroups("");
448: while (it.hasNext()) {
449: Group group = (Group) it.next();
450: System.out.println("Group is " + group);
451: groupCount++;
452: }
453:
454: ums.addUser("notme", "one-pw");
455: gms.addGroup("g1");
456: gms.addGroup("g2");
457: gms.addGroup("g3");
458: groupAdded = 3;
459: int count = 0;
460: it = gms.getGroups("");
461: while (it.hasNext()) {
462: Group group = (Group) it.next();
463: System.out.println("Group is " + group);
464: count++;
465: }
466: ums.removeUser("notme");
467: gms.removeGroup("g1");
468: gms.removeGroup("g2");
469: gms.removeGroup("g3");
470: assertTrue(
471: "group count should be " + (groupAdded + groupCount),
472: count == (groupAdded + groupCount));
473:
474: }
475:
476: /**
477: * <p>
478: * Destroy group test objects.
479: * </p>
480: */
481: protected void destroyGroups() throws Exception {
482: ums.removeUser("anonuser1");
483: ums.removeUser("anonuser2");
484: ums.removeUser("anonuser3");
485: ums.removeUser("anonuser4");
486: gms.removeGroup("testgroup1");
487: gms.removeGroup("testgroup2");
488: gms.removeGroup("testusertogroup1");
489: gms.removeGroup("testgetgroup");
490: }
491:
492: }
|