001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.security.management.rdbms.test;
019:
020: import java.security.Principal;
021: import java.security.acl.Group;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.sape.carbon.core.component.Lookup;
028: import org.sape.carbon.core.exception.ExceptionUtility;
029: import org.sape.carbon.services.security.management.DefaultGroupImpl;
030: import org.sape.carbon.services.security.management.DefaultUserImpl;
031: import org.sape.carbon.services.security.management.DuplicatePrincipalException;
032: import org.sape.carbon.services.security.management.SecurityManagementException;
033: import org.sape.carbon.services.security.management.UnknownGroupException;
034: import org.sape.carbon.services.security.management.UnknownPrincipalException;
035: import org.sape.carbon.services.security.management.UserManager;
036: import org.sape.carbon.services.security.management.rdbms.RdbmsUserManagerConfiguration;
037:
038: import junit.extensions.ActiveTestSuite;
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: /**
044: * Test harness for the RdbmsUserManager implementation of the UserManager
045: * service
046: *
047: * @author Jordan Reed, September 2002
048: * @version $Revision: 1.7 $($Author: dvoet $ / $Date: 2003/10/28 19:02:01 $)
049: *
050: * @since carbon 1.2
051: */
052: public class RdbmsUserManagerServiceTest extends TestCase {
053: private static final String STANDARD_RDBMS_USERMANAGER = "/security/management/test/RdbmsOracleUserManager";
054: private static final String TEST_USERNAME = "test-user";
055: private static final Map TEST_CREDENTIAL = new HashMap();
056: private static final String TEST_STANDALONE_USERNAME = "test-standalone-user";
057: private static final Map TEST_STANDALONE_CREDENTIAL = new HashMap();
058: private static final String TEST_NONEXIST_USERNAME = "non-existent-user";
059: private static final String TEST_NONEXIST_GROUPNAME = "non-existent-group";
060: private static final String TEST_CHILD_GROUPNAME = "test-group";
061: private static final String TEST_PARENT_GROUPNAME = "test-group-2";
062: private static final String TEST_CYCLE_GROUPNAME = "test-group-cycle";
063:
064: static {
065: TEST_STANDALONE_CREDENTIAL
066: .put(
067: RdbmsUserManagerConfiguration.CreateUserBindParameters[0],
068: TEST_STANDALONE_USERNAME);
069: TEST_CREDENTIAL
070: .put(
071: RdbmsUserManagerConfiguration.CreateUserBindParameters[0],
072: TEST_USERNAME);
073: }
074:
075: /**
076: * Constructs a new instance of the test.
077: *
078: * @param name DOCUMENT ME!
079: */
080: public RdbmsUserManagerServiceTest(String name) {
081: super (name);
082: }
083:
084: /**
085: * Tests creation of a user.
086: */
087: public void testCreateUser() throws SecurityManagementException {
088: UserManager userManager = (UserManager) Lookup.getInstance()
089: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
090:
091: Principal user = userManager.createUser(TEST_USERNAME,
092: TEST_CREDENTIAL);
093:
094: Principal cycledUser = userManager.createUser(
095: TEST_STANDALONE_USERNAME, TEST_STANDALONE_CREDENTIAL);
096:
097: if (user == null) {
098: fail("After call to createUser, a null user object "
099: + "was returned.");
100: }
101: }
102:
103: /**
104: * Tests that creating a user with a name that already exists will
105: * throw the proper exception.
106: */
107: public void testCreateUserDuplicatePrincipal()
108: throws SecurityManagementException {
109: UserManager userManager = (UserManager) Lookup.getInstance()
110: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
111:
112: try {
113: Principal user = userManager.createUser(TEST_USERNAME,
114: TEST_CREDENTIAL);
115:
116: fail("Created use with name [" + TEST_USERNAME + "], but "
117: + "should have thrown DuplicatePrincipalException");
118: } catch (DuplicatePrincipalException dpe) {
119: // success
120: }
121: }
122:
123: /**
124: * Tests the retreival of an existent user.
125: */
126: public void testRetreiveUser() throws SecurityManagementException {
127: UserManager userManager = (UserManager) Lookup.getInstance()
128: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
129:
130: Principal user = userManager.retreiveUser(TEST_USERNAME);
131:
132: if (user == null) {
133: fail("After call to retreiveUser for user named ["
134: + TEST_USERNAME + "], a null user object "
135: + "was returned.");
136: }
137: }
138:
139: /**
140: * Tests the retreival of an existent user.
141: */
142: public void testRetreiveUserNullUser()
143: throws SecurityManagementException {
144: UserManager userManager = (UserManager) Lookup.getInstance()
145: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
146:
147: Principal user = userManager
148: .retreiveUser(TEST_NONEXIST_USERNAME);
149:
150: if (user != null) {
151: fail("After call to retreiveUser for non-existent user named ["
152: + TEST_NONEXIST_USERNAME
153: + "], a full object was returned.");
154: }
155: }
156:
157: /**
158: * Tests the removal of a user.
159: */
160: public void testRemoveUser() throws SecurityManagementException {
161: UserManager userManager = (UserManager) Lookup.getInstance()
162: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
163:
164: Principal user = userManager.retreiveUser(TEST_USERNAME);
165: Principal cycledUser = userManager
166: .retreiveUser(TEST_STANDALONE_USERNAME);
167:
168: try {
169: userManager.removeUser(user);
170: userManager.removeUser(cycledUser);
171: } catch (UnknownPrincipalException upe) {
172: fail("User named " + TEST_USERNAME + "cannot be found."
173: + upe + ExceptionUtility.captureStackTrace(upe));
174: }
175: }
176:
177: /**
178: * Tests that attempting to remove a non-existent user gives the
179: * correct error.
180: */
181: public void testRemoveUserUnknownPrincipal()
182: throws SecurityManagementException {
183: UserManager userManager = (UserManager) Lookup.getInstance()
184: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
185:
186: Principal user = new DefaultUserImpl(TEST_NONEXIST_USERNAME);
187:
188: try {
189: userManager.removeUser(user);
190: fail("Attempted to remove non-existent principal identified "
191: + "by name ["
192: + TEST_NONEXIST_USERNAME
193: + "] succeeded, but shouldn't have.");
194: } catch (UnknownPrincipalException upe) {
195: // success
196: }
197: }
198:
199: /**
200: * Tests the updating credentials.
201: */
202: public void testUpdateCredential()
203: throws SecurityManagementException {
204: String NEW_PASSWORD = "newpassword";
205:
206: UserManager userManager = (UserManager) Lookup.getInstance()
207: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
208:
209: Principal user = userManager.retreiveUser(TEST_USERNAME);
210:
211: if (user == null) {
212: fail("User named [" + TEST_USERNAME + "] cannot be found.");
213: }
214:
215: userManager.updateCredential(user, NEW_PASSWORD);
216:
217: if (!userManager.authenticate(TEST_USERNAME, NEW_PASSWORD)) {
218: fail("After updating user's ["
219: + TEST_USERNAME
220: + " password to ["
221: + NEW_PASSWORD
222: + "] authentication against the new password failed.");
223: }
224: }
225:
226: /**
227: * Tests that attempting to update the credential of an unknown user
228: * fails.
229: */
230: public void testUpdateCredentialUnknownPrincipal()
231: throws SecurityManagementException {
232: UserManager userManager = (UserManager) Lookup.getInstance()
233: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
234:
235: Principal user = new DefaultUserImpl(TEST_NONEXIST_USERNAME);
236:
237: try {
238: userManager.updateCredential(user, "newpassword");
239: fail("Attempted to remove non-existent principal identified "
240: + "by name ["
241: + TEST_NONEXIST_USERNAME
242: + "] succeeded, but shouldn't have.");
243: } catch (UnknownPrincipalException upe) {
244: // success
245: }
246: }
247:
248: /**
249: * Tests getting back the containing groups for a user
250: */
251: public void testRetreiveGroupsForUser()
252: throws SecurityManagementException {
253: UserManager userManager = (UserManager) Lookup.getInstance()
254: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
255:
256: Principal user = userManager.retreiveUser(TEST_USERNAME);
257: if (user == null) {
258: fail("Precondition for test cast not met. There is no "
259: + "user with name [" + TEST_USERNAME + "]");
260: }
261:
262: Set containingGroups = userManager.retreiveGroups(user);
263:
264: if (containingGroups.size() != 1) {
265: fail("Got back containing groups for principal identified by ["
266: + TEST_USERNAME
267: + "]. Expected 1 containg group, but "
268: + "got back a different number. Set ["
269: + containingGroups + "]");
270: }
271: }
272:
273: /**
274: * Tests getting back the containing groups for a group
275: */
276: public void testRetreiveGroupsForGroup()
277: throws SecurityManagementException {
278: UserManager userManager = (UserManager) Lookup.getInstance()
279: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
280:
281: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
282: if (group == null) {
283: fail("Precondition for test cast not met. There is no "
284: + "group with name [" + TEST_CHILD_GROUPNAME + "]");
285: }
286:
287: Set containingGroups = userManager.retreiveGroups(group);
288:
289: if (containingGroups.size() != 1) {
290: fail("Got back containing groups for group identified by ["
291: + TEST_USERNAME
292: + "]. Expected 1 containg group, but "
293: + "got back a different number. Set ["
294: + containingGroups + "]");
295: }
296: }
297:
298: /**
299: * Tests that attempting to update the credential of an unknown user
300: * fails.
301: */
302: public void testRetreiveGroupsUnknownPrincipal()
303: throws SecurityManagementException {
304: UserManager userManager = (UserManager) Lookup.getInstance()
305: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
306:
307: Principal user = new DefaultUserImpl(TEST_NONEXIST_USERNAME);
308:
309: try {
310: userManager.retreiveGroups(user);
311: fail("Attempted to retreive groups for non-existent principal "
312: + "identified by name ["
313: + TEST_NONEXIST_USERNAME
314: + "] succeeded, but shouldn't have.");
315: } catch (UnknownPrincipalException upe) {
316: // success
317: }
318: }
319:
320: /**
321: * Tests the creation of a group.
322: */
323: public void testCreateGroup() throws SecurityManagementException {
324: UserManager userManager = (UserManager) Lookup.getInstance()
325: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
326:
327: userManager.createGroup(TEST_CHILD_GROUPNAME);
328: userManager.createGroup(TEST_PARENT_GROUPNAME);
329: userManager.createGroup(TEST_CYCLE_GROUPNAME);
330: }
331:
332: /**
333: * Tests that attempting to create a second group with the same name
334: * fails.
335: */
336: public void testCreateGroupDuplicateGroup()
337: throws SecurityManagementException {
338: UserManager userManager = (UserManager) Lookup.getInstance()
339: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
340:
341: try {
342: userManager.createGroup(TEST_CHILD_GROUPNAME);
343: fail("Attempted to create duplicate group with name ["
344: + TEST_CHILD_GROUPNAME
345: + " succeeded, but shouldn't have.");
346: } catch (DuplicatePrincipalException dpe) {
347: // success
348: }
349: }
350:
351: /**
352: * Tests the retreival of a group.
353: */
354: public void testRetreiveGroup() throws SecurityManagementException {
355: UserManager userManager = (UserManager) Lookup.getInstance()
356: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
357:
358: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
359:
360: if (group == null) {
361: fail("After call to retreiveGroup for group named ["
362: + TEST_CHILD_GROUPNAME
363: + "], a null group object was returned.");
364: }
365: }
366:
367: /**
368: * Tests that retreiving a non-existent group returns null.
369: */
370: public void testRetreiveGroupNullGroup()
371: throws SecurityManagementException {
372: UserManager userManager = (UserManager) Lookup.getInstance()
373: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
374:
375: Group group = userManager
376: .retreiveGroup(TEST_NONEXIST_GROUPNAME);
377:
378: if (group != null) {
379: fail("After call to retreiveGroup for non-existent group named ["
380: + TEST_CHILD_GROUPNAME
381: + "], a non-null group object was returned.");
382: }
383: }
384:
385: /**
386: * Tests the removal of a group.
387: */
388: public void testRemoveGroup() throws SecurityManagementException {
389: UserManager userManager = (UserManager) Lookup.getInstance()
390: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
391:
392: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
393: Group group2 = userManager.retreiveGroup(TEST_PARENT_GROUPNAME);
394: Group group3 = userManager.retreiveGroup(TEST_CYCLE_GROUPNAME);
395:
396: try {
397: userManager.removeGroup(group);
398: userManager.removeGroup(group2);
399: userManager.removeGroup(group3);
400: } catch (UnknownPrincipalException upe) {
401: fail("User named " + TEST_USERNAME + " cannot be found."
402: + upe + ExceptionUtility.captureStackTrace(upe));
403: }
404: }
405:
406: /**
407: * Tests the attempted removal of non-existent group fails.
408: */
409: public void testRemoveGroupUnknownGroup()
410: throws SecurityManagementException {
411: UserManager userManager = (UserManager) Lookup.getInstance()
412: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
413:
414: Group group = new DefaultGroupImpl(TEST_NONEXIST_GROUPNAME,
415: new HashSet(), STANDARD_RDBMS_USERMANAGER);
416:
417: try {
418: userManager.removeGroup(group);
419: fail("Successfully removed non-existent group named ["
420: + TEST_NONEXIST_GROUPNAME + "]");
421: } catch (UnknownPrincipalException upe) {
422: // success
423: }
424: }
425:
426: /**
427: * Tests adding a user to a group.
428: */
429: public void testAddUserToGroup() throws SecurityManagementException {
430: UserManager userManager = (UserManager) Lookup.getInstance()
431: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
432:
433: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
434: Principal user = userManager.retreiveUser(TEST_USERNAME);
435:
436: if (user == null) {
437: fail("Precondition for test cast not met. There is no "
438: + "user with name [" + TEST_USERNAME + "]");
439: }
440:
441: if (group == null) {
442: fail("Precondition for test cast not met. There is no "
443: + "group with name [" + TEST_CHILD_GROUPNAME + "]");
444: }
445:
446: if (group.isMember(user)) {
447: fail("Precondition for test case not met. User ["
448: + TEST_USERNAME + "] is already a child of group ["
449: + TEST_CHILD_GROUPNAME + "]");
450: }
451:
452: userManager.addPrincipalToGroup(user, group);
453:
454: group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
455: user = userManager.retreiveUser(TEST_USERNAME);
456:
457: if (!group.isMember(user)) {
458: fail("After adding user [" + TEST_USERNAME + "] to group ["
459: + TEST_CHILD_GROUPNAME
460: + " isMember test returns false.");
461: }
462: }
463:
464: /**
465: * Tests adding a user which is not in the user-store to a group
466: * fails.
467: */
468: public void testAddUserToGroupUnknownPrincipal()
469: throws SecurityManagementException {
470: UserManager userManager = (UserManager) Lookup.getInstance()
471: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
472:
473: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
474: Principal user = new DefaultUserImpl(TEST_NONEXIST_USERNAME);
475:
476: if (group == null) {
477: fail("Precondition for test cast not met. There is no "
478: + "group with name [" + TEST_CHILD_GROUPNAME + "]");
479: }
480:
481: try {
482: userManager.addPrincipalToGroup(user, group);
483: fail("Added unknown principal [" + TEST_NONEXIST_USERNAME
484: + " to group successfully.");
485: } catch (UnknownPrincipalException sme) {
486: // success
487: }
488: }
489:
490: /**
491: * Tests that adding a user to a non-existent group gives an error.
492: *
493: * @throws UnknownPrincipalException DOCUMENT ME!
494: */
495: public void testAddUserToGroupUnknownGroup()
496: throws SecurityManagementException {
497: UserManager userManager = (UserManager) Lookup.getInstance()
498: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
499:
500: Group group = new DefaultGroupImpl(TEST_NONEXIST_GROUPNAME,
501: new HashSet(), STANDARD_RDBMS_USERMANAGER);
502: Principal user = userManager.retreiveUser(TEST_USERNAME);
503:
504: if (user == null) {
505: fail("Precondition for test cast not met. There is no "
506: + "user with name [" + TEST_USERNAME + "]");
507: }
508:
509: try {
510: userManager.addPrincipalToGroup(user, group);
511: fail("Added user to unknown group ["
512: + TEST_NONEXIST_USERNAME + " successfully.");
513: } catch (UnknownGroupException sme) {
514: // success
515: }
516: }
517:
518: /**
519: * Tests removing a user from group
520: */
521: public void testRemovePrincipalFromGroup()
522: throws SecurityManagementException {
523: UserManager userManager = (UserManager) Lookup.getInstance()
524: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
525:
526: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
527: Principal user = userManager.retreiveUser(TEST_USERNAME);
528:
529: userManager.removePrincipalFromGroup(user, group);
530:
531: group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
532: user = userManager.retreiveUser(TEST_USERNAME);
533:
534: if (group.isMember(user)) {
535: fail("After removing user [" + TEST_USERNAME
536: + "] from group [" + TEST_CHILD_GROUPNAME
537: + " isMember test returns true.");
538: }
539: }
540:
541: /**
542: * Tests removing a user from a group with a non-exist user gives an
543: * error.
544: */
545: public void testRemovePrincipalFromGroupUnknownPrincipal()
546: throws SecurityManagementException {
547: UserManager userManager = (UserManager) Lookup.getInstance()
548: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
549:
550: Group group = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
551: Principal user = new DefaultUserImpl(TEST_NONEXIST_USERNAME);
552:
553: try {
554: userManager.removePrincipalFromGroup(user, group);
555: fail("Was able remove non-existent principal ["
556: + TEST_NONEXIST_USERNAME + "] from group");
557: } catch (UnknownPrincipalException sme) {
558: // success
559: }
560: }
561:
562: /**
563: * Tests removing a user from a group with a non-exist group gives an
564: * error.
565: *
566: * @throws UnknownPrincipalException DOCUMENT ME!
567: */
568: public void testRemovePrincipalFromGroupUnknownGroup()
569: throws SecurityManagementException {
570: UserManager userManager = (UserManager) Lookup.getInstance()
571: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
572:
573: Group group = new DefaultGroupImpl(TEST_NONEXIST_GROUPNAME,
574: new HashSet(), STANDARD_RDBMS_USERMANAGER);
575: Principal user = userManager.retreiveUser(TEST_USERNAME);
576:
577: try {
578: userManager.removePrincipalFromGroup(user, group);
579: fail("Was able remove principal from non-existent group ["
580: + TEST_NONEXIST_GROUPNAME + "]");
581: } catch (UnknownGroupException sme) {
582: // success
583: }
584: }
585:
586: /**
587: * Tests that a cycled group does not cause infinite recursion.
588: *
589: * @throws UnknownPrincipalException DOCUMENT ME!
590: */
591: public void testCycledGroup() throws SecurityManagementException {
592: UserManager userManager = (UserManager) Lookup.getInstance()
593: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
594:
595: Group childGroup = userManager
596: .retreiveGroup(TEST_CHILD_GROUPNAME);
597: Group parentGroup = userManager
598: .retreiveGroup(TEST_PARENT_GROUPNAME);
599: Group cycledGroup = userManager
600: .retreiveGroup(TEST_CYCLE_GROUPNAME);
601: Principal cycledUser = userManager
602: .retreiveUser(TEST_STANDALONE_USERNAME);
603:
604: userManager.addPrincipalToGroup(cycledGroup, childGroup);
605: userManager.addPrincipalToGroup(parentGroup, cycledGroup);
606:
607: if (parentGroup.isMember(cycledUser)) {
608: fail("Found user [" + TEST_STANDALONE_USERNAME
609: + "] in group [" + TEST_PARENT_GROUPNAME
610: + "], but he is not a member!");
611: }
612:
613: // The cycle can go forever if poorly executed. Should throw
614: // a stackoverflow currently, but if the implementation becomes
615: // iterative, it may hang.
616: }
617:
618: /**
619: * Tests adding a group to a group.
620: */
621: public void testAddGroupToGroup()
622: throws SecurityManagementException {
623: UserManager userManager = (UserManager) Lookup.getInstance()
624: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
625:
626: Principal user = userManager.retreiveUser(TEST_USERNAME);
627: Group childGroup = userManager
628: .retreiveGroup(TEST_CHILD_GROUPNAME);
629: Group parentGroup = userManager
630: .retreiveGroup(TEST_PARENT_GROUPNAME);
631:
632: if (user == null) {
633: fail("Precondition for test case not met. There is no "
634: + "user with name [" + TEST_USERNAME + "]");
635: }
636:
637: if (childGroup == null) {
638: fail("Precondition for test case not met. There is no "
639: + "group with name [" + TEST_CHILD_GROUPNAME + "]");
640: }
641:
642: if (parentGroup == null) {
643: fail("Precondition for test case not met. There is no "
644: + "group with name [" + TEST_PARENT_GROUPNAME + "]");
645: }
646:
647: if (parentGroup.isMember(childGroup)) {
648: fail("Precondition for test case not met. Group ["
649: + TEST_CHILD_GROUPNAME
650: + "] is already a child of group ["
651: + TEST_PARENT_GROUPNAME + "]");
652: }
653:
654: boolean result = userManager.addPrincipalToGroup(childGroup,
655: parentGroup);
656:
657: user = userManager.retreiveUser(TEST_USERNAME);
658: childGroup = userManager.retreiveGroup(TEST_CHILD_GROUPNAME);
659: parentGroup = userManager.retreiveGroup(TEST_PARENT_GROUPNAME);
660:
661: if (!parentGroup.isMember(childGroup)) {
662: fail("After adding group [" + TEST_CHILD_GROUPNAME
663: + "] to group [" + TEST_PARENT_GROUPNAME
664: + " isMember test returns false.");
665: }
666:
667: if (!parentGroup.isMember(user)) {
668: fail("After adding group ["
669: + TEST_CHILD_GROUPNAME
670: + "] to group ["
671: + TEST_PARENT_GROUPNAME
672: + "] isMember test returns false for child member of group ["
673: + TEST_USERNAME + "]");
674: }
675: }
676:
677: /**
678: * Tests authenticating a user
679: *
680: * @throws UnknownPrincipalException DOCUMENT ME!
681: */
682: public void testAuthenticate() throws SecurityManagementException {
683: UserManager userManager = (UserManager) Lookup.getInstance()
684: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
685:
686: boolean authenticated = userManager.authenticate(TEST_USERNAME,
687: TEST_USERNAME);
688:
689: if (!authenticated) {
690: fail("Failed to authenticate user [" + TEST_USERNAME
691: + "] with password [" + TEST_USERNAME + "]");
692: }
693: }
694:
695: /**
696: * Tests authenticating when there is an incorrect password
697: *
698: * @throws UnknownPrincipalException DOCUMENT ME!
699: */
700: public void testAuthenticateFailure()
701: throws SecurityManagementException {
702: String BAD_PASSWORD = "imladris";
703:
704: UserManager userManager = (UserManager) Lookup.getInstance()
705: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
706:
707: boolean authenticated = userManager.authenticate(TEST_USERNAME,
708: BAD_PASSWORD);
709:
710: if (authenticated) {
711: fail("Successfully authenticated user [" + TEST_USERNAME
712: + "] with bad password [" + BAD_PASSWORD + "]");
713: }
714: }
715:
716: /**
717: * Tests retreiving all user names.
718: *
719: * @throws UnknownPrincipalException DOCUMENT ME!
720: */
721: public void testRetreiveAllUserNames()
722: throws SecurityManagementException {
723: UserManager userManager = (UserManager) Lookup.getInstance()
724: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
725:
726: Set allUserNames = userManager.retreiveAllUserNames();
727:
728: if ((allUserNames == null) || allUserNames.isEmpty()) {
729: fail("Call to retreiveAllUserNames returned "
730: + " and empty set: " + allUserNames);
731: }
732: }
733:
734: /**
735: * Tests retreiving all group names.
736: *
737: * @throws UnknownPrincipalException DOCUMENT ME!
738: */
739: public void testRetreiveAllGroupNames()
740: throws SecurityManagementException {
741: UserManager userManager = (UserManager) Lookup.getInstance()
742: .fetchComponent(STANDARD_RDBMS_USERMANAGER);
743:
744: Set allGroupNames = userManager.retreiveAllGroupNames();
745:
746: if ((allGroupNames == null) || allGroupNames.isEmpty()) {
747: fail("Call to retreiveAllGroupNames returned "
748: + " and empty set: " + allGroupNames);
749: }
750: }
751:
752: /**
753: * Method called by jUnit to get all the tests in this test case.
754: *
755: * @return Test the suite of tests in this test case
756: */
757: public static Test suite() {
758: TestSuite masterSuite = new TestSuite();
759:
760: // add single threaded tests
761: Test singleThreadedTests = getSingleThreadedTests();
762:
763: if (singleThreadedTests != null) {
764: masterSuite.addTest(singleThreadedTests);
765: }
766:
767: // add multi threaded tests
768: Test multiThreadedTests = getMultiThreadedTests();
769:
770: if (multiThreadedTests != null) {
771: masterSuite.addTest(multiThreadedTests);
772: }
773:
774: return masterSuite;
775: }
776:
777: /**
778: * Single threaded tests:
779: *
780: * @return Test the suite of single threaded tests in this test case
781: */
782: private static Test getSingleThreadedTests() {
783: TestSuite suite = new TestSuite();
784:
785: // Add all the test data
786: suite
787: .addTest(new RdbmsUserManagerServiceTest(
788: "testCreateUser"));
789: suite
790: .addTest(new RdbmsUserManagerServiceTest(
791: "testCreateGroup"));
792:
793: // Run the major tests
794: suite.addTest(new RdbmsUserManagerServiceTest(
795: "testRetreiveUser"));
796: suite.addTest(new RdbmsUserManagerServiceTest(
797: "testRetreiveGroup"));
798:
799: suite.addTest(new RdbmsUserManagerServiceTest(
800: "testAddUserToGroup"));
801: suite.addTest(new RdbmsUserManagerServiceTest(
802: "testAddGroupToGroup"));
803: suite
804: .addTest(new RdbmsUserManagerServiceTest(
805: "testCycledGroup"));
806:
807: suite.addTest(new RdbmsUserManagerServiceTest(
808: "testAuthenticate"));
809: suite.addTest(new RdbmsUserManagerServiceTest(
810: "testAuthenticateFailure"));
811: suite.addTest(new RdbmsUserManagerServiceTest(
812: "testUpdateCredential"));
813:
814: suite.addTest(new RdbmsUserManagerServiceTest(
815: "testRetreiveGroupsForUser"));
816: suite.addTest(new RdbmsUserManagerServiceTest(
817: "testRetreiveGroupsForGroup"));
818:
819: suite.addTest(new RdbmsUserManagerServiceTest(
820: "testRetreiveAllUserNames"));
821: suite.addTest(new RdbmsUserManagerServiceTest(
822: "testRetreiveAllGroupNames"));
823:
824: // Test exceptions
825: suite.addTest(new RdbmsUserManagerServiceTest(
826: "testCreateUserDuplicatePrincipal"));
827: suite.addTest(new RdbmsUserManagerServiceTest(
828: "testRemoveUserUnknownPrincipal"));
829: suite.addTest(new RdbmsUserManagerServiceTest(
830: "testUpdateCredentialUnknownPrincipal"));
831: suite.addTest(new RdbmsUserManagerServiceTest(
832: "testCreateGroupDuplicateGroup"));
833: suite.addTest(new RdbmsUserManagerServiceTest(
834: "testRemoveGroupUnknownGroup"));
835: suite.addTest(new RdbmsUserManagerServiceTest(
836: "testRetreiveUserNullUser"));
837: suite.addTest(new RdbmsUserManagerServiceTest(
838: "testRetreiveGroupNullGroup"));
839: suite.addTest(new RdbmsUserManagerServiceTest(
840: "testAddUserToGroupUnknownPrincipal"));
841: suite.addTest(new RdbmsUserManagerServiceTest(
842: "testAddUserToGroupUnknownGroup"));
843:
844: suite.addTest(new RdbmsUserManagerServiceTest(
845: "testRetreiveGroupsUnknownPrincipal"));
846:
847: suite.addTest(new RdbmsUserManagerServiceTest(
848: "testRemovePrincipalFromGroupUnknownPrincipal"));
849: suite.addTest(new RdbmsUserManagerServiceTest(
850: "testRemovePrincipalFromGroupUnknownGroup"));
851: suite.addTest(new RdbmsUserManagerServiceTest(
852: "testRemovePrincipalFromGroup"));
853:
854: // Remove all the test data
855: suite
856: .addTest(new RdbmsUserManagerServiceTest(
857: "testRemoveGroup"));
858: suite
859: .addTest(new RdbmsUserManagerServiceTest(
860: "testRemoveUser"));
861:
862: return suite;
863: }
864:
865: /**
866: * Multi-Threaded tests
867: *
868: * @return Test the suite of multi-threaded tests in this test case
869: */
870: private static Test getMultiThreadedTests() {
871: TestSuite suite = new ActiveTestSuite();
872:
873: return suite;
874: }
875:
876: /**
877: * This method will add the give test to the give suite the specified
878: * number of times. This is best used for multi-threaded tests where
879: * suite is an instance of ActiveTestSuite and you want to run the
880: * same test in multiple threads.
881: *
882: * @param suite the suite to add the test to.
883: * @param testName the name of the test to add.
884: * @param number the number of times to add the test to the suite
885: */
886: private static void addTest(TestSuite suite, String testName,
887: int number) {
888: for (int count = 0; count < number; count++) {
889: suite.addTest(new RdbmsUserManagerServiceTest(testName));
890: }
891: }
892: }
|