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