0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: TestMemoryUsers.java 3634 2007-01-08 21:42:24Z gbevin $
0007: */
0008: package com.uwyn.rife.authentication.credentialsmanagers;
0009:
0010: import com.uwyn.rife.authentication.credentials.RoleUser;
0011: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.DuplicateLoginException;
0012: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.DuplicateRoleException;
0013: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.DuplicateUserIdException;
0014: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
0015: import com.uwyn.rife.config.RifeConfig;
0016: import com.uwyn.rife.resources.ResourceFinderClasspath;
0017: import com.uwyn.rife.tools.ExceptionUtils;
0018: import com.uwyn.rife.tools.StringEncryptor;
0019: import java.io.File;
0020: import java.util.ArrayList;
0021: import junit.framework.TestCase;
0022:
0023: public class TestMemoryUsers extends TestCase {
0024: public TestMemoryUsers(String name) {
0025: super (name);
0026: }
0027:
0028: public void testInstantiation() {
0029: MemoryUsers users = null;
0030:
0031: users = new MemoryUsers();
0032:
0033: assertNotNull(users);
0034: }
0035:
0036: public void testNoInitialUsers() {
0037: MemoryUsers users = new MemoryUsers();
0038:
0039: assertEquals(0, users.countUsers());
0040: }
0041:
0042: public void testAddRoles() {
0043: MemoryUsers users = new MemoryUsers();
0044:
0045: try {
0046: users.addRole("role1").addRole("role2").addRole("role3");
0047: try {
0048: users.addRole("role2");
0049: fail();
0050: } catch (DuplicateRoleException e) {
0051: assertEquals("role2", e.getRole());
0052: }
0053:
0054: assertEquals(3, users.countRoles());
0055:
0056: assertTrue(users.containsRole("role1"));
0057: assertTrue(users.containsRole("role2"));
0058: assertTrue(users.containsRole("role3"));
0059: assertTrue(!users.containsRole("role4"));
0060: } catch (CredentialsManagerException e) {
0061: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0062: }
0063: }
0064:
0065: public void testAddUsers() {
0066: MemoryUsers users = new MemoryUsers();
0067:
0068: try {
0069: users.addRole("role1").addRole("role2").addRole("role3");
0070:
0071: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0072: "thepassword");
0073: users.addUser("login1", user1_attributes);
0074: assertEquals(0, user1_attributes.getUserId());
0075: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0076: "thepassword2", new String[] { "role1", "role2" });
0077: users.addUser("login2", user2_attributes);
0078: assertEquals(1, user2_attributes.getUserId());
0079: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0080: 174, "thepassword3", new String[] { "role1",
0081: "role2", "role3" });
0082: users.addUser("login3", user3_attributes);
0083: assertEquals(174, user3_attributes.getUserId());
0084: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0085: "thepassword4", new String[] { "role2", "role3" });
0086: users.addUser("login4", user4_attributes);
0087: assertEquals(2, user4_attributes.getUserId());
0088: try {
0089: RoleUserAttributes user5_attributes = new RoleUserAttributes(
0090: "thepassword5", new String[] { "role1" });
0091: users.addUser("login1", user5_attributes);
0092: fail();
0093: } catch (DuplicateLoginException e) {
0094: assertEquals(e.getLogin(), "login1");
0095: }
0096:
0097: assertEquals(4, users.countUsers());
0098:
0099: assertTrue(users.containsUser("login1"));
0100: assertTrue(users.containsUser("login2"));
0101: assertTrue(users.containsUser("login3"));
0102: assertTrue(users.containsUser("login4"));
0103: assertTrue(!users.containsUser("login5"));
0104:
0105: assertEquals(3, users.countRoles());
0106:
0107: assertTrue(users.containsRole("role1"));
0108: assertTrue(users.containsRole("role2"));
0109: assertTrue(users.containsRole("role3"));
0110: assertTrue(!users.containsRole("role4"));
0111:
0112: assertEquals(users.getUserId("login1"), user1_attributes
0113: .getUserId());
0114: assertEquals(users.getUserId("login2"), user2_attributes
0115: .getUserId());
0116: assertEquals(users.getUserId("login3"), user3_attributes
0117: .getUserId());
0118: assertEquals(users.getUserId("login4"), user4_attributes
0119: .getUserId());
0120:
0121: assertEquals(users.getLogin(user1_attributes.getUserId()),
0122: "login1");
0123: assertEquals(users.getLogin(user2_attributes.getUserId()),
0124: "login2");
0125: assertEquals(users.getLogin(user3_attributes.getUserId()),
0126: "login3");
0127: assertEquals(users.getLogin(user4_attributes.getUserId()),
0128: "login4");
0129: } catch (CredentialsManagerException e) {
0130: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0131: }
0132: }
0133:
0134: public void testUpdateUsers() {
0135: MemoryUsers users = new MemoryUsers();
0136:
0137: try {
0138: users.addRole("role1").addRole("role2").addRole("role3");
0139:
0140: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0141: "thepassword");
0142: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0143: "thepassword2", new String[] { "role1", "role2" });
0144: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0145: 174, "thepassword3", new String[] { "role1",
0146: "role2", "role3" });
0147: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0148: "thepassword4", new String[] { "role2", "role3" });
0149: users.addUser("login1", user1_attributes).addUser("login2",
0150: user2_attributes).addUser("login3",
0151: user3_attributes).addUser("login4",
0152: user4_attributes);
0153:
0154: RoleUserAttributes user1_attributes_new = new RoleUserAttributes(
0155: 4, "thepassword_new", new String[] { "role1",
0156: "role2" });
0157: RoleUserAttributes user2_attributes_new = new RoleUserAttributes(
0158: 3, new String[] { "role2" });
0159: RoleUserAttributes user3_attributes_new = new RoleUserAttributes(
0160: 2, new String[] { "role1" });
0161: RoleUserAttributes user4_attributes_new = new RoleUserAttributes(
0162: 1, "thepassword_new4");
0163: assertTrue(users.updateUser("login1", user1_attributes_new));
0164: assertTrue(users.updateUser("login2", user2_attributes_new));
0165: assertTrue(users.updateUser("login3", user3_attributes_new));
0166: assertTrue(users.updateUser("login4", user4_attributes_new));
0167:
0168: assertEquals(4, users.countUsers());
0169:
0170: assertTrue(users.containsUser("login1"));
0171: assertTrue(users.containsUser("login2"));
0172: assertTrue(users.containsUser("login3"));
0173: assertTrue(users.containsUser("login4"));
0174: assertTrue(!users.containsUser("login5"));
0175:
0176: assertEquals(users.getAttributes("login1").getPassword(),
0177: user1_attributes_new.getPassword());
0178: assertEquals(users.getAttributes("login1").getUserId(),
0179: user1_attributes.getUserId());
0180: assertEquals(users.getAttributes("login1").getRoles()
0181: .size(), user1_attributes_new.getRoles().size());
0182: assertTrue(users.getAttributes("login1").getRoles()
0183: .contains("role1"));
0184: assertTrue(users.getAttributes("login1").getRoles()
0185: .contains("role2"));
0186: assertEquals(users.getAttributes("login2").getPassword(),
0187: user2_attributes.getPassword());
0188: assertEquals(users.getAttributes("login2").getUserId(),
0189: user2_attributes.getUserId());
0190: assertEquals(users.getAttributes("login2").getRoles()
0191: .size(), user2_attributes_new.getRoles().size());
0192: assertTrue(users.getAttributes("login2").getRoles()
0193: .contains("role2"));
0194: assertEquals(users.getAttributes("login3").getPassword(),
0195: user3_attributes.getPassword());
0196: assertEquals(users.getAttributes("login3").getUserId(),
0197: user3_attributes.getUserId());
0198: assertEquals(users.getAttributes("login3").getRoles()
0199: .size(), user3_attributes_new.getRoles().size());
0200: assertTrue(users.getAttributes("login3").getRoles()
0201: .contains("role1"));
0202: assertEquals(users.getAttributes("login4").getPassword(),
0203: user4_attributes_new.getPassword());
0204: assertEquals(users.getAttributes("login4").getUserId(),
0205: user4_attributes.getUserId());
0206: assertEquals(0, users.getAttributes("login4").getRoles()
0207: .size());
0208:
0209: assertEquals(2, users.countRoles());
0210:
0211: assertTrue(users.containsRole("role1"));
0212: assertTrue(users.containsRole("role2"));
0213: assertTrue(!users.containsRole("role3"));
0214: } catch (CredentialsManagerException e) {
0215: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0216: }
0217: }
0218:
0219: public void testListRoles() {
0220: MemoryUsers users = new MemoryUsers();
0221:
0222: try {
0223: users.addRole("role1").addRole("role2").addRole("role3");
0224:
0225: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0226: "thepassword");
0227: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0228: "thepassword2", new String[] { "role1", "role2" });
0229: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0230: 174, "thepassword3", new String[] { "role1",
0231: "role2", "role3" });
0232: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0233: "thepassword4", new String[] { "role2", "role3" });
0234: users.addUser("login1", user1_attributes).addUser("login2",
0235: user2_attributes).addUser("login3",
0236: user3_attributes).addUser("login4",
0237: user4_attributes);
0238:
0239: ListMemoryRoles listroles = null;
0240:
0241: listroles = new ListMemoryRoles();
0242: assertTrue(users.listRoles(listroles));
0243: assertEquals(3, listroles.getRoles().size());
0244: assertTrue(listroles.getRoles().contains("role1"));
0245: assertTrue(listroles.getRoles().contains("role2"));
0246: assertTrue(listroles.getRoles().contains("role3"));
0247:
0248: users.removeUser("login4");
0249:
0250: listroles = new ListMemoryRoles();
0251: assertTrue(users.listRoles(listroles));
0252: assertEquals(3, listroles.getRoles().size());
0253: assertTrue(listroles.getRoles().contains("role1"));
0254: assertTrue(listroles.getRoles().contains("role2"));
0255: assertTrue(listroles.getRoles().contains("role3"));
0256:
0257: users.removeUser("login3");
0258:
0259: listroles = new ListMemoryRoles();
0260: assertTrue(users.listRoles(listroles));
0261: assertEquals(2, listroles.getRoles().size());
0262: assertTrue(listroles.getRoles().contains("role1"));
0263: assertTrue(listroles.getRoles().contains("role2"));
0264: } catch (CredentialsManagerException e) {
0265: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0266: }
0267: }
0268:
0269: public void testListUsers() {
0270: MemoryUsers users = new MemoryUsers();
0271:
0272: try {
0273: users.addRole("role1").addRole("role2").addRole("role3");
0274:
0275: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0276: "thepassword");
0277: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0278: "thepassword2", new String[] { "role1", "role2" });
0279: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0280: 174, "thepassword3", new String[] { "role1",
0281: "role2", "role3" });
0282: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0283: "thepassword4", new String[] { "role2", "role3" });
0284: users.addUser("login1", user1_attributes).addUser("login2",
0285: user2_attributes).addUser("login3",
0286: user3_attributes).addUser("login4",
0287: user4_attributes);
0288:
0289: ListMemoryUsers listusers = new ListMemoryUsers();
0290: assertTrue(users.listUsers(listusers));
0291: assertEquals(4, listusers.getUsers().size());
0292: assertTrue(listusers.getUsers().contains(
0293: "0,login1,thepassword"));
0294: assertTrue(listusers.getUsers().contains(
0295: "1,login2,thepassword2"));
0296: assertTrue(listusers.getUsers().contains(
0297: "174,login3,thepassword3"));
0298: assertTrue(listusers.getUsers().contains(
0299: "2,login4,thepassword4"));
0300: } catch (CredentialsManagerException e) {
0301: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0302: }
0303: }
0304:
0305: public void testListUsersRanged() {
0306: MemoryUsers users = new MemoryUsers();
0307:
0308: try {
0309: users.addRole("role1").addRole("role2").addRole("role3");
0310:
0311: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0312: "thepassword");
0313: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0314: "thepassword2", new String[] { "role1", "role2" });
0315: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0316: 174, "thepassword3", new String[] { "role1",
0317: "role2", "role3" });
0318: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0319: "thepassword4", new String[] { "role2", "role3" });
0320: users.addUser("login1", user1_attributes).addUser("login2",
0321: user2_attributes).addUser("login3",
0322: user3_attributes).addUser("login4",
0323: user4_attributes);
0324:
0325: ListMemoryUsers listusers = null;
0326:
0327: listusers = new ListMemoryUsers();
0328: assertTrue(users.listUsers(listusers, 2, 1));
0329: assertEquals(2, listusers.getUsers().size());
0330: assertTrue(listusers.getUsers().contains(
0331: "1,login2,thepassword2"));
0332: assertTrue(listusers.getUsers().contains(
0333: "174,login3,thepassword3"));
0334:
0335: listusers = new ListMemoryUsers();
0336: assertTrue(users.listUsers(listusers, 3, 0));
0337: assertEquals(3, listusers.getUsers().size());
0338: assertTrue(listusers.getUsers().contains(
0339: "0,login1,thepassword"));
0340: assertTrue(listusers.getUsers().contains(
0341: "1,login2,thepassword2"));
0342: assertTrue(listusers.getUsers().contains(
0343: "174,login3,thepassword3"));
0344:
0345: listusers = new ListMemoryUsers();
0346: assertFalse(users.listUsers(listusers, 0, 3));
0347: assertEquals(0, listusers.getUsers().size());
0348:
0349: listusers = new ListMemoryUsers();
0350: assertTrue(users.listUsers(listusers, 2, 3));
0351: assertEquals(1, listusers.getUsers().size());
0352: assertTrue(listusers.getUsers().contains(
0353: "2,login4,thepassword4"));
0354: } catch (CredentialsManagerException e) {
0355: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0356: }
0357: }
0358:
0359: class ListMemoryRoles implements ListRoles {
0360: private ArrayList<String> mRoles = new ArrayList<String>();
0361:
0362: public ArrayList<String> getRoles() {
0363: return mRoles;
0364: }
0365:
0366: public boolean foundRole(String name) {
0367: mRoles.add(name);
0368:
0369: return true;
0370: }
0371: }
0372:
0373: class ListMemoryUsers implements ListUsers {
0374: private ArrayList<String> mUsers = new ArrayList<String>();
0375:
0376: public ArrayList<String> getUsers() {
0377: return mUsers;
0378: }
0379:
0380: public boolean foundUser(long userId, String login,
0381: String password) {
0382: mUsers.add(userId + "," + login + "," + password);
0383:
0384: return true;
0385: }
0386: }
0387:
0388: public void testGetUserAttributes() {
0389: MemoryUsers users = new MemoryUsers();
0390:
0391: try {
0392: users.addRole("role1").addRole("role2").addRole("role3");
0393:
0394: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0395: "thepassword");
0396: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0397: "thepassword2", new String[] { "role1", "role2" });
0398: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0399: 174, "thepassword3", new String[] { "role1",
0400: "role2", "role3" });
0401: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0402: "thepassword4", new String[] { "role2", "role3" });
0403: users.addUser("login1", user1_attributes).addUser("login2",
0404: user2_attributes).addUser("login3",
0405: user3_attributes).addUser("login4",
0406: user4_attributes);
0407:
0408: RoleUserAttributes attributes1 = users
0409: .getAttributes("login1");
0410: RoleUserAttributes attributes2 = users
0411: .getAttributes("login2");
0412: RoleUserAttributes attributes3 = users
0413: .getAttributes("login3");
0414: RoleUserAttributes attributes4 = users
0415: .getAttributes("login4");
0416: assertEquals(attributes1.getUserId(), 0);
0417: assertEquals(0, attributes1.getRoles().size());
0418: assertEquals(attributes1.getPassword(), "thepassword");
0419:
0420: assertEquals(attributes2.getUserId(), 1);
0421: assertEquals(attributes2.getRoles().size(), 2);
0422: assertTrue(attributes2.getRoles().contains("role1"));
0423: assertTrue(attributes2.getRoles().contains("role2"));
0424: assertEquals(attributes2.getPassword(), "thepassword2");
0425:
0426: assertEquals(attributes3.getUserId(), 174);
0427: assertEquals(attributes3.getRoles().size(), 3);
0428: assertTrue(attributes3.getRoles().contains("role1"));
0429: assertTrue(attributes3.getRoles().contains("role2"));
0430: assertTrue(attributes3.getRoles().contains("role3"));
0431: assertEquals(attributes3.getPassword(), "thepassword3");
0432:
0433: assertEquals(attributes4.getUserId(), 2);
0434: assertEquals(attributes4.getRoles().size(), 2);
0435: assertTrue(attributes4.getRoles().contains("role2"));
0436: assertTrue(attributes4.getRoles().contains("role3"));
0437: assertEquals(attributes4.getPassword(), "thepassword4");
0438: } catch (CredentialsManagerException e) {
0439: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0440: }
0441: }
0442:
0443: public void testUserIdSpecification() {
0444: MemoryUsers users = new MemoryUsers();
0445:
0446: try {
0447: users.addUser("login1",
0448: new RoleUserAttributes(0, "thepassword")).addUser(
0449: "login2", new RoleUserAttributes("thepassword"));
0450: try {
0451: users.addUser("login3", new RoleUserAttributes(1,
0452: "thepassword"));
0453: fail();
0454: } catch (DuplicateUserIdException e) {
0455: assertTrue(true);
0456: }
0457: users.addUser("login4",
0458: new RoleUserAttributes(2, "thepassword")).addUser(
0459: "login5", new RoleUserAttributes("thepassword"))
0460: .addUser("login6",
0461: new RoleUserAttributes(847, "thepassword"));
0462:
0463: assertTrue(users.containsUser("login1"));
0464: assertTrue(users.containsUser("login2"));
0465: assertTrue(!users.containsUser("login3"));
0466: assertTrue(users.containsUser("login4"));
0467: assertTrue(users.containsUser("login5"));
0468: assertTrue(users.containsUser("login6"));
0469:
0470: assertEquals("login1", users.getLogin(0));
0471: assertEquals("login2", users.getLogin(1));
0472: assertEquals("login4", users.getLogin(2));
0473: assertEquals("login5", users.getLogin(3));
0474: assertEquals("login6", users.getLogin(847));
0475: } catch (CredentialsManagerException e) {
0476: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0477: }
0478: }
0479:
0480: public void testValidUsers() {
0481: MemoryUsers users = new MemoryUsers();
0482: users.setPasswordEncryptor(StringEncryptor.SHA);
0483:
0484: try {
0485: users.addRole("role1").addRole("role2").addRole("role3");
0486:
0487: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0488: "thepassword");
0489: users.addUser("login1", user1_attributes);
0490: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0491: "SHA:iTeooS7tJ7m1mdRrbUacq/pr1uM=", new String[] {
0492: "role1", "role2" });
0493: users.addUser("login2", user2_attributes);
0494: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0495: "thepassword3", new String[] { "role1", "role2",
0496: "role3" });
0497: users.addUser("login3", user3_attributes);
0498: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0499: 174, "thepassword4", new String[] { "role2",
0500: "role3" });
0501: users.addUser("login4", user4_attributes);
0502:
0503: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0504: "login", "thepassword")));
0505:
0506: assertTrue(0 == users.verifyCredentials(new RoleUser(
0507: "login1", "thepassword")));
0508: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0509: "login1", "thepassword2")));
0510: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0511: "login1", "thepassword", "role1")));
0512:
0513: assertTrue(1 == users.verifyCredentials(new RoleUser(
0514: "login2", "thepassword2")));
0515: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0516: "login2", "thepassword3")));
0517: assertTrue(1 == users.verifyCredentials(new RoleUser(
0518: "login2", "thepassword2", "role1")));
0519: assertTrue(1 == users.verifyCredentials(new RoleUser(
0520: "login2", "thepassword2", "role2")));
0521: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0522: "login2", "thepassword2", "role3")));
0523:
0524: assertTrue(2 == users.verifyCredentials(new RoleUser(
0525: "login3", "thepassword3")));
0526: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0527: "login3", "thepassword4")));
0528: assertTrue(2 == users.verifyCredentials(new RoleUser(
0529: "login3", "thepassword3", "role1")));
0530: assertTrue(2 == users.verifyCredentials(new RoleUser(
0531: "login3", "thepassword3", "role2")));
0532: assertTrue(2 == users.verifyCredentials(new RoleUser(
0533: "login3", "thepassword3", "role3")));
0534: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0535: "login3", "thepassword3", "role4")));
0536:
0537: assertTrue(174 == users.verifyCredentials(new RoleUser(
0538: "login4", "thepassword4")));
0539: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0540: "login4", "thepassword")));
0541: assertTrue(-1 == users.verifyCredentials(new RoleUser(
0542: "login4", "thepassword4", "role1")));
0543: assertTrue(174 == users.verifyCredentials(new RoleUser(
0544: "login4", "thepassword4", "role2")));
0545: assertTrue(174 == users.verifyCredentials(new RoleUser(
0546: "login4", "thepassword4", "role3")));
0547: } catch (CredentialsManagerException e) {
0548: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0549: }
0550: }
0551:
0552: public void testUsersInRole() {
0553: MemoryUsers users = new MemoryUsers();
0554: users.setPasswordEncryptor(StringEncryptor.MD5);
0555:
0556: try {
0557: users.addRole("role1").addRole("role2").addRole("role3");
0558:
0559: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0560: "thepassword");
0561: users.addUser("login1", user1_attributes);
0562: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0563: "thepassword2", new String[] { "role1", "role2" });
0564: users.addUser("login2", user2_attributes);
0565: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0566: "thepassword3", new String[] { "role1", "role2",
0567: "role3" });
0568: users.addUser("login3", user3_attributes);
0569: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0570: 174, "thepassword4", new String[] { "role2",
0571: "role3" });
0572: users.addUser("login4", user4_attributes);
0573:
0574: assertTrue(!users.isUserInRole(
0575: user1_attributes.getUserId(), "role1"));
0576: assertTrue(users.isUserInRole(user2_attributes.getUserId(),
0577: "role1"));
0578: assertTrue(users.isUserInRole(user3_attributes.getUserId(),
0579: "role1"));
0580: assertTrue(!users.isUserInRole(
0581: user4_attributes.getUserId(), "role1"));
0582:
0583: assertTrue(!users.isUserInRole(
0584: user1_attributes.getUserId(), "role2"));
0585: assertTrue(users.isUserInRole(user2_attributes.getUserId(),
0586: "role2"));
0587: assertTrue(users.isUserInRole(user3_attributes.getUserId(),
0588: "role2"));
0589: assertTrue(users.isUserInRole(user4_attributes.getUserId(),
0590: "role2"));
0591:
0592: assertTrue(!users.isUserInRole(
0593: user1_attributes.getUserId(), "role3"));
0594: assertTrue(!users.isUserInRole(
0595: user2_attributes.getUserId(), "role3"));
0596: assertTrue(users.isUserInRole(user3_attributes.getUserId(),
0597: "role3"));
0598: assertTrue(users.isUserInRole(user4_attributes.getUserId(),
0599: "role3"));
0600: } catch (CredentialsManagerException e) {
0601: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0602: }
0603: }
0604:
0605: public void testListUsersInRole() {
0606: MemoryUsers users = new MemoryUsers();
0607:
0608: try {
0609: users.addRole("role1").addRole("role2").addRole("role3");
0610:
0611: ListMemoryUsers listusers = new ListMemoryUsers();
0612:
0613: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0614: "thepassword");
0615: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0616: "thepassword2", new String[] { "role1", "role2" });
0617: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0618: 174, "thepassword3", new String[] { "role1",
0619: "role2", "role3" });
0620: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0621: "thepassword4", new String[] { "role2", "role3" });
0622: users.addUser("login1", user1_attributes).addUser("login2",
0623: user2_attributes).addUser("login3",
0624: user3_attributes).addUser("login4",
0625: user4_attributes);
0626:
0627: assertFalse(users.listUsersInRole(null, "role1"));
0628: assertFalse(users.listUsersInRole(listusers, null));
0629: assertFalse(users.listUsersInRole(listusers, ""));
0630:
0631: assertTrue(users.listUsersInRole(listusers, "role1"));
0632: assertEquals(2, listusers.getUsers().size());
0633: assertFalse(listusers.getUsers().contains(
0634: "0,login1,thepassword"));
0635: assertTrue(listusers.getUsers().contains(
0636: "1,login2,thepassword2"));
0637: assertTrue(listusers.getUsers().contains(
0638: "174,login3,thepassword3"));
0639: assertFalse(listusers.getUsers().contains(
0640: "2,login4,thepassword4"));
0641:
0642: listusers = new ListMemoryUsers();
0643: assertTrue(users.listUsersInRole(listusers, "role2"));
0644: assertEquals(3, listusers.getUsers().size());
0645: assertFalse(listusers.getUsers().contains(
0646: "0,login1,thepassword"));
0647: assertTrue(listusers.getUsers().contains(
0648: "1,login2,thepassword2"));
0649: assertTrue(listusers.getUsers().contains(
0650: "174,login3,thepassword3"));
0651: assertTrue(listusers.getUsers().contains(
0652: "2,login4,thepassword4"));
0653:
0654: listusers = new ListMemoryUsers();
0655: assertTrue(users.listUsersInRole(listusers, "role3"));
0656: assertEquals(2, listusers.getUsers().size());
0657: assertFalse(listusers.getUsers().contains(
0658: "0,login1,thepassword"));
0659: assertFalse(listusers.getUsers().contains(
0660: "1,login2,thepassword2"));
0661: assertTrue(listusers.getUsers().contains(
0662: "174,login3,thepassword3"));
0663: assertTrue(listusers.getUsers().contains(
0664: "2,login4,thepassword4"));
0665:
0666: listusers = new ListMemoryUsers();
0667: assertFalse(users.listUsersInRole(listusers, "role4"));
0668: assertEquals(0, listusers.getUsers().size());
0669: assertFalse(listusers.getUsers().contains(
0670: "0,login1,thepassword"));
0671: assertFalse(listusers.getUsers().contains(
0672: "1,login2,thepassword2"));
0673: assertFalse(listusers.getUsers().contains(
0674: "174,login3,thepassword3"));
0675: assertFalse(listusers.getUsers().contains(
0676: "2,login4,thepassword4"));
0677: } catch (CredentialsManagerException e) {
0678: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0679: }
0680: }
0681:
0682: public void testRemoveUsersByLogin() {
0683: MemoryUsers users = new MemoryUsers();
0684:
0685: try {
0686: users.addRole("role1").addRole("role2").addRole("role3")
0687: .addUser("login1",
0688: new RoleUserAttributes("thepassword"))
0689: .addUser(
0690: "login2",
0691: new RoleUserAttributes("thepassword2",
0692: new String[] { "role1", "role2" }))
0693: .addUser(
0694: "login3",
0695: new RoleUserAttributes("thepassword3",
0696: new String[] { "role1", "role2",
0697: "role3" })).addUser(
0698: "login4",
0699: new RoleUserAttributes(174, "thepassword4",
0700: new String[] { "role2", "role3" }));
0701:
0702: assertEquals(4, users.countUsers());
0703: assertTrue(!users.removeUser("login5"));
0704: assertEquals(4, users.countUsers());
0705: assertTrue(users.removeUser("login4"));
0706: assertEquals(3, users.countUsers());
0707: assertTrue(users.removeUser("login1"));
0708: assertEquals(2, users.countUsers());
0709: assertTrue(users.removeUser("login2"));
0710: assertEquals(1, users.countUsers());
0711: assertTrue(users.removeUser("login3"));
0712: assertEquals(0, users.countUsers());
0713: } catch (CredentialsManagerException e) {
0714: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0715: }
0716: }
0717:
0718: public void testRemoveUsersByUserId() {
0719: MemoryUsers users = new MemoryUsers();
0720:
0721: try {
0722: users.addRole("role1").addRole("role2").addRole("role3")
0723: .addUser("login1",
0724: new RoleUserAttributes(1, "thepassword"))
0725: .addUser(
0726: "login2",
0727: new RoleUserAttributes(23, "thepassword2",
0728: new String[] { "role1", "role2" }))
0729: .addUser(
0730: "login3",
0731: new RoleUserAttributes(14, "thepassword3",
0732: new String[] { "role1", "role2",
0733: "role3" })).addUser(
0734: "login4",
0735: new RoleUserAttributes(174, "thepassword4",
0736: new String[] { "role2", "role3" }));
0737:
0738: assertEquals(4, users.countUsers());
0739: assertTrue(!users.removeUser(5));
0740: assertEquals(4, users.countUsers());
0741: assertTrue(users.removeUser(174));
0742: assertEquals(3, users.countUsers());
0743: assertTrue(users.removeUser(1));
0744: assertEquals(2, users.countUsers());
0745: assertTrue(users.removeUser(23));
0746: assertEquals(1, users.countUsers());
0747: assertTrue(users.removeUser(14));
0748: assertEquals(0, users.countUsers());
0749: } catch (CredentialsManagerException e) {
0750: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0751: }
0752: }
0753:
0754: public void testRemoveRole() {
0755: MemoryUsers users = new MemoryUsers();
0756:
0757: try {
0758: users.addRole("role1").addRole("role2").addRole("role3")
0759: .addUser("login1",
0760: new RoleUserAttributes(1, "thepassword"))
0761: .addUser(
0762: "login2",
0763: new RoleUserAttributes(23, "thepassword2",
0764: new String[] { "role1", "role2" }))
0765: .addUser(
0766: "login3",
0767: new RoleUserAttributes(14, "thepassword3",
0768: new String[] { "role1", "role2",
0769: "role3" })).addUser(
0770: "login4",
0771: new RoleUserAttributes(174, "thepassword4",
0772: new String[] { "role2", "role3" }));
0773:
0774: assertEquals(3, users.countRoles());
0775: assertTrue(!users.removeRole("role4"));
0776: assertEquals(3, users.countRoles());
0777: assertTrue(users.removeRole("role3"));
0778: assertEquals(2, users.countRoles());
0779:
0780: RoleUserAttributes attributes = users
0781: .getAttributes("login3");
0782: assertTrue(attributes.getRoles().contains("role1"));
0783: assertTrue(attributes.getRoles().contains("role2"));
0784: assertTrue(!attributes.getRoles().contains("role3"));
0785:
0786: assertTrue(users.removeRole("role2"));
0787: attributes = users.getAttributes("login4");
0788: assertEquals(attributes.getRoles().size(), 0);
0789:
0790: attributes = users.getAttributes("login2");
0791: assertTrue(attributes.getRoles().contains("role1"));
0792: } catch (CredentialsManagerException e) {
0793: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0794: }
0795: }
0796:
0797: public void testClearUsers() {
0798: MemoryUsers users = new MemoryUsers();
0799:
0800: try {
0801: users.addRole("role1").addRole("role2").addRole("role3")
0802: .addUser("login1",
0803: new RoleUserAttributes("thepassword"))
0804: .addUser(
0805: "login2",
0806: new RoleUserAttributes("thepassword2",
0807: new String[] { "role1", "role2" }))
0808: .addUser(
0809: "login3",
0810: new RoleUserAttributes("thepassword3",
0811: new String[] { "role1", "role2",
0812: "role3" })).addUser(
0813: "login4",
0814: new RoleUserAttributes(174, "thepassword4",
0815: new String[] { "role2", "role3" }));
0816:
0817: assertEquals(4, users.countUsers());
0818: users.clearUsers();
0819: assertEquals(0, users.countUsers());
0820: } catch (CredentialsManagerException e) {
0821: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0822: }
0823: }
0824:
0825: public void testVerifyCredentials() {
0826: MemoryUsers users = new MemoryUsers();
0827: users.setPasswordEncryptor(StringEncryptor.OBF);
0828:
0829: try {
0830: users.addRole("role1").addRole("role2").addRole("role3");
0831:
0832: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0833: "thepassword");
0834: users.addUser("login1", user1_attributes);
0835: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0836: "thepassword2", new String[] { "role1", "role2" });
0837: users.addUser("login2", user2_attributes);
0838: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0839: "thepassword3", new String[] { "role1", "role2",
0840: "role3" });
0841: users.addUser("login3", user3_attributes);
0842:
0843: RoleUser user = new RoleUser();
0844: user.setLogin("login2");
0845: user.setPassword("thepassword2");
0846: user.setRole("role2");
0847:
0848: assertEquals(user2_attributes.getUserId(), users
0849: .verifyCredentials(user));
0850:
0851: user.setRole(null);
0852:
0853: assertEquals(user2_attributes.getUserId(), users
0854: .verifyCredentials(user));
0855:
0856: user.setRole("role3");
0857:
0858: assertEquals(-1, users.verifyCredentials(user));
0859: } catch (CredentialsManagerException e) {
0860: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0861: }
0862: }
0863:
0864: public void testToXml() {
0865: MemoryUsers users = new MemoryUsers();
0866:
0867: try {
0868: users.addRole("role1").addRole("role2").addRole("role3")
0869: .addUser("login1",
0870: new RoleUserAttributes("thepassword"))
0871: .addUser(
0872: "login2",
0873: new RoleUserAttributes("thepassword2",
0874: new String[] { "role1", "role2" }))
0875: .addUser(
0876: "login3",
0877: new RoleUserAttributes("thepassword3",
0878: new String[] { "role1", "role2",
0879: "role3" })).addUser(
0880: "login4",
0881: new RoleUserAttributes(64, "thepassword4"))
0882: .addUser(
0883: "login5",
0884: new RoleUserAttributes(9, "thepassword5",
0885: new String[] { "role1", "role2" }));
0886:
0887: assertEquals("<credentials>\n"
0888: + "\t<user login=\"login1\">\n"
0889: + "\t\t<password>thepassword</password>\n"
0890: + "\t</user>\n" + "\t<user login=\"login2\">\n"
0891: + "\t\t<password>thepassword2</password>\n"
0892: + "\t\t<role>role1</role>\n"
0893: + "\t\t<role>role2</role>\n" + "\t</user>\n"
0894: + "\t<user login=\"login3\">\n"
0895: + "\t\t<password>thepassword3</password>\n"
0896: + "\t\t<role>role1</role>\n"
0897: + "\t\t<role>role2</role>\n"
0898: + "\t\t<role>role3</role>\n" + "\t</user>\n"
0899: + "\t<user login=\"login4\" userid=\"64\">\n"
0900: + "\t\t<password>thepassword4</password>\n"
0901: + "\t</user>\n"
0902: + "\t<user login=\"login5\" userid=\"9\">\n"
0903: + "\t\t<password>thepassword5</password>\n"
0904: + "\t\t<role>role1</role>\n"
0905: + "\t\t<role>role2</role>\n" + "\t</user>\n"
0906: + "</credentials>\n", users.toXml());
0907: } catch (CredentialsManagerException e) {
0908: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0909: }
0910: }
0911:
0912: public void testEncryptedToXml() {
0913: MemoryUsers users = new MemoryUsers();
0914: users.setPasswordEncryptor(StringEncryptor.SHA);
0915:
0916: try {
0917: users.addRole("role1").addRole("role2").addRole("role3")
0918: .addUser("login1",
0919: new RoleUserAttributes("thepassword"))
0920: .addUser(
0921: "login2",
0922: new RoleUserAttributes("thepassword2",
0923: new String[] { "role1", "role2" }))
0924: .addUser(
0925: "login3",
0926: new RoleUserAttributes("thepassword3",
0927: new String[] { "role1", "role2",
0928: "role3" })).addUser(
0929: "login4",
0930: new RoleUserAttributes(64, "thepassword4"))
0931: .addUser(
0932: "login5",
0933: new RoleUserAttributes(9, "thepassword5",
0934: new String[] { "role1", "role2" }));
0935:
0936: assertEquals(
0937: "<credentials>\n"
0938: + "\t<user login=\"login1\">\n"
0939: + "\t\t<password>SHA:gquHbROHv6/kbMHIou8HTq5Qyx0=</password>\n"
0940: + "\t</user>\n"
0941: + "\t<user login=\"login2\">\n"
0942: + "\t\t<password>SHA:iTeooS7tJ7m1mdRrbUacq/pr1uM=</password>\n"
0943: + "\t\t<role>role1</role>\n"
0944: + "\t\t<role>role2</role>\n"
0945: + "\t</user>\n"
0946: + "\t<user login=\"login3\">\n"
0947: + "\t\t<password>SHA:nyGLrpBEibAyX2s8mZSR97HwhpQ=</password>\n"
0948: + "\t\t<role>role1</role>\n"
0949: + "\t\t<role>role2</role>\n"
0950: + "\t\t<role>role3</role>\n"
0951: + "\t</user>\n"
0952: + "\t<user login=\"login4\" userid=\"64\">\n"
0953: + "\t\t<password>SHA:md3fSecksLrBRiH+oNDUs1Rik88=</password>\n"
0954: + "\t</user>\n"
0955: + "\t<user login=\"login5\" userid=\"9\">\n"
0956: + "\t\t<password>SHA:z6BF3VuavxQPTCgIKhiAwdiz5Z0=</password>\n"
0957: + "\t\t<role>role1</role>\n"
0958: + "\t\t<role>role2</role>\n"
0959: + "\t</user>\n" + "</credentials>\n", users
0960: .toXml());
0961: } catch (CredentialsManagerException e) {
0962: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0963: }
0964: }
0965:
0966: public void testStoreXml() {
0967: MemoryUsers users = new MemoryUsers();
0968:
0969: try {
0970: users.addRole("role1").addRole("role2").addRole("role3");
0971:
0972: RoleUserAttributes user1_attributes = new RoleUserAttributes(
0973: "thepassword");
0974: RoleUserAttributes user2_attributes = new RoleUserAttributes(
0975: "thepassword2", new String[] { "role1", "role2" });
0976: RoleUserAttributes user3_attributes = new RoleUserAttributes(
0977: "thepassword3", new String[] { "role1", "role2",
0978: "role3" });
0979: RoleUserAttributes user4_attributes = new RoleUserAttributes(
0980: 64, "thepassword4");
0981: RoleUserAttributes user5_attributes = new RoleUserAttributes(
0982: 9, "thepassword5",
0983: new String[] { "role1", "role2" });
0984: users.addUser("login1", user1_attributes).addUser("login2",
0985: user2_attributes).addUser("login3",
0986: user3_attributes).addUser("login4",
0987: user4_attributes).addUser("login5",
0988: user5_attributes);
0989:
0990: String xml_filename = "memoryusers_storexml_test.xml";
0991: String xml_path = RifeConfig.Global.getTempPath()
0992: + File.separator + xml_filename;
0993: File xml_file = new File(xml_path);
0994: users.storeToXml(xml_file);
0995:
0996: MemoryUsers users_stored = new MemoryUsers(xml_filename,
0997: ResourceFinderClasspath.getInstance());
0998:
0999: assertEquals(5, users_stored.countUsers());
1000:
1001: assertTrue(users_stored.containsUser("login1"));
1002: assertTrue(users_stored.containsUser("login2"));
1003: assertTrue(users_stored.containsUser("login3"));
1004: assertTrue(users_stored.containsUser("login4"));
1005: assertTrue(users_stored.containsUser("login5"));
1006: assertTrue(!users_stored.containsUser("login6"));
1007:
1008: assertEquals(3, users_stored.countRoles());
1009:
1010: assertTrue(users_stored.containsRole("role1"));
1011: assertTrue(users_stored.containsRole("role2"));
1012: assertTrue(users_stored.containsRole("role3"));
1013: assertTrue(!users_stored.containsRole("role4"));
1014:
1015: assertEquals(users_stored.getUserId("login1"),
1016: user1_attributes.getUserId());
1017: assertEquals(users_stored.getUserId("login2"),
1018: user2_attributes.getUserId());
1019: assertEquals(users_stored.getUserId("login3"),
1020: user3_attributes.getUserId());
1021: assertEquals(users_stored.getUserId("login4"),
1022: user4_attributes.getUserId());
1023: assertEquals(users_stored.getUserId("login5"),
1024: user5_attributes.getUserId());
1025:
1026: assertEquals(users_stored.getLogin(user1_attributes
1027: .getUserId()), "login1");
1028: assertEquals(users_stored.getLogin(user2_attributes
1029: .getUserId()), "login2");
1030: assertEquals(users_stored.getLogin(user3_attributes
1031: .getUserId()), "login3");
1032: assertEquals(users_stored.getLogin(user4_attributes
1033: .getUserId()), "login4");
1034: assertEquals(users_stored.getLogin(user5_attributes
1035: .getUserId()), "login5");
1036:
1037: RoleUserAttributes user1_attributes_new = users_stored
1038: .getAttributes("login1");
1039: RoleUserAttributes user2_attributes_new = users_stored
1040: .getAttributes("login2");
1041: RoleUserAttributes user3_attributes_new = users_stored
1042: .getAttributes("login3");
1043: RoleUserAttributes user4_attributes_new = users_stored
1044: .getAttributes("login4");
1045: RoleUserAttributes user5_attributes_new = users_stored
1046: .getAttributes("login5");
1047: assertFalse(user1_attributes == user1_attributes_new);
1048: assertFalse(user2_attributes == user2_attributes_new);
1049: assertFalse(user3_attributes == user3_attributes_new);
1050: assertFalse(user4_attributes == user4_attributes_new);
1051: assertFalse(user5_attributes == user5_attributes_new);
1052: assertEquals(user1_attributes, user1_attributes_new);
1053: assertEquals(user2_attributes, user2_attributes_new);
1054: assertEquals(user3_attributes, user3_attributes_new);
1055: assertEquals(user4_attributes, user4_attributes_new);
1056: assertEquals(user5_attributes, user5_attributes_new);
1057:
1058: RoleUserAttributes user6_attributes = new RoleUserAttributes(
1059: 90, "thepassword6", new String[] { "role2" });
1060: users_stored.addUser("login6", user6_attributes);
1061: users_stored.storeToXml();
1062:
1063: MemoryUsers users_stored2 = new MemoryUsers(xml_filename,
1064: ResourceFinderClasspath.getInstance());
1065: assertEquals(6, users_stored2.countUsers());
1066: assertTrue(users_stored2.containsUser("login6"));
1067: assertEquals(3, users_stored2.countRoles());
1068: assertEquals(users_stored2.getUserId("login6"),
1069: user6_attributes.getUserId());
1070: assertEquals(users_stored2.getLogin(user6_attributes
1071: .getUserId()), "login6");
1072: RoleUserAttributes user6_attributes_new = users_stored2
1073: .getAttributes("login6");
1074: assertFalse(user6_attributes == user6_attributes_new);
1075: assertEquals(user6_attributes, user6_attributes_new);
1076:
1077: xml_file.delete();
1078: } catch (CredentialsManagerException e) {
1079: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
1080: }
1081: }
1082:
1083: public void testListUserRoles() {
1084: MemoryUsers users = new MemoryUsers();
1085:
1086: try {
1087: users.addRole("role1").addRole("role2").addRole("role3");
1088:
1089: RoleUserAttributes user1_attributes = new RoleUserAttributes(
1090: 49, "thepassword");
1091: users.addUser("login1", user1_attributes);
1092: RoleUserAttributes user2_attributes = new RoleUserAttributes(
1093: 322, "thepassword2", new String[] { "role1",
1094: "role2" });
1095: users.addUser("login2", user2_attributes);
1096: RoleUserAttributes user3_attributes = new RoleUserAttributes(
1097: 2, "thepassword3", new String[] { "role1", "role2",
1098: "role3" });
1099: users.addUser("login3", user3_attributes);
1100:
1101: ListMemoryRoles listroles = new ListMemoryRoles();
1102:
1103: assertFalse(users.listUserRoles("login1", listroles));
1104:
1105: assertTrue(users.listUserRoles("login2", listroles));
1106: assertEquals(listroles.getRoles().size(), 2);
1107: assertTrue(listroles.getRoles().contains("role1"));
1108: assertTrue(listroles.getRoles().contains("role2"));
1109:
1110: listroles = new ListMemoryRoles();
1111:
1112: assertTrue(users.listUserRoles("login3", listroles));
1113: assertEquals(listroles.getRoles().size(), 3);
1114: assertTrue(listroles.getRoles().contains("role1"));
1115: assertTrue(listroles.getRoles().contains("role2"));
1116: assertTrue(listroles.getRoles().contains("role3"));
1117: } catch (CredentialsManagerException e) {
1118: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
1119: }
1120: }
1121: }
|