001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * Steven Grimm <koreth[remove] at midwinter dot com>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id: TestDatabaseSessionValidator.java 3643 2007-01-12 15:29:45Z gbevin $
008: */
009: package com.uwyn.rife.authentication.sessionvalidators;
010:
011: import com.uwyn.rife.authentication.SessionAttributes;
012: import com.uwyn.rife.authentication.SessionValidator;
013: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsers;
014: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsersFactory;
015: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
016: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
017: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
018: import com.uwyn.rife.authentication.exceptions.SessionValidatorException;
019: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessions;
020: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessionsFactory;
021: import com.uwyn.rife.database.Datasource;
022: import com.uwyn.rife.ioc.HierarchicalProperties;
023: import com.uwyn.rife.tools.ExceptionUtils;
024: import junit.framework.TestCase;
025:
026: public class TestDatabaseSessionValidator extends TestCase {
027: private Datasource mDatasource = null;
028: private HierarchicalProperties mProperties = null;
029:
030: public TestDatabaseSessionValidator(Datasource datasource,
031: String datasourceName, String name) {
032: super (name);
033: mDatasource = datasource;
034: mProperties = new HierarchicalProperties();
035: mProperties.put("datasource", mDatasource);
036: }
037:
038: public void testInstantiation() {
039: SessionValidator validator = new DatabaseSessionValidatorFactory()
040: .getValidator(mProperties);
041: assertNotNull(validator);
042: assertTrue(validator instanceof DatabaseSessionValidator);
043: }
044:
045: public void testValidSessionId() {
046: SessionValidator validator = new DatabaseSessionValidatorFactory()
047: .getValidator(mProperties);
048:
049: assertTrue(validator.isAccessAuthorized(1));
050: }
051:
052: public void testSessionValidity() {
053: SessionValidator validator = new DatabaseSessionValidatorFactory()
054: .getValidator(mProperties);
055: DatabaseSessions sessions = new DatabaseSessionsFactory()
056: .getManager(mProperties);
057: sessions.setSessionDuration(120000);
058: validator.setSessionManager(sessions);
059:
060: int user_id = 9478;
061: String host_ip = "98.232.12.456";
062:
063: String auth_id = null;
064: try {
065: sessions.install();
066:
067: auth_id = sessions.startSession(user_id, host_ip, false);
068: assertTrue(validator.isAccessAuthorized(validator
069: .validateSession(auth_id, host_ip,
070: new DummyAttributes())));
071: sessions.setRestrictHostIp(true);
072: assertEquals(DatabaseSessionValidator.SESSION_INVALID,
073: validator.validateSession(auth_id, "1.1.1.1",
074: new DummyAttributes()));
075: sessions.setRestrictHostIp(false);
076: assertEquals(DatabaseSessionValidator.SESSION_VALID,
077: validator.validateSession(auth_id, "1.1.1.1",
078: new DummyAttributes()));
079: assertEquals(DatabaseSessionValidator.SESSION_INVALID,
080: validator.validateSession("not_valid", host_ip,
081: new DummyAttributes()));
082:
083: sessions.setSessionDuration(0);
084:
085: Thread.sleep(2);
086: assertEquals(DatabaseSessionValidator.SESSION_INVALID,
087: validator.validateSession(auth_id, host_ip,
088: new DummyAttributes()));
089: } catch (InterruptedException e) {
090: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
091: } catch (SessionManagerException e) {
092: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
093: } catch (SessionValidatorException e) {
094: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
095: } finally {
096: try {
097: sessions.remove();
098: } catch (SessionManagerException e) {
099: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
100: false);
101: }
102: }
103: }
104:
105: public void testSessionValidityRole() {
106: SessionValidator validator = new DatabaseSessionValidatorFactory()
107: .getValidator(mProperties);
108: DatabaseUsers users = DatabaseUsersFactory
109: .getInstance(mDatasource);
110: DatabaseSessions sessions = new DatabaseSessionsFactory()
111: .getManager(mProperties);
112: sessions.setSessionDuration(120000);
113: validator.setSessionManager(sessions);
114:
115: String host_ip = "98.232.12.456";
116:
117: String auth_id1 = null;
118: String auth_id2 = null;
119: String auth_id3 = null;
120: try {
121: users.install();
122: sessions.install();
123:
124: users.addRole("admin");
125: users.addRole("maint");
126:
127: users.addUser("login1", new RoleUserAttributes(1,
128: "thepassword", new String[] { "admin", "maint" }));
129: users.addUser("login2", new RoleUserAttributes(2,
130: "thepassword", new String[] { "maint" }));
131: users.addUser("login3", new RoleUserAttributes(3,
132: "thepassword"));
133:
134: auth_id1 = sessions.startSession(1, host_ip, false);
135: auth_id2 = sessions.startSession(2, host_ip, false);
136: auth_id3 = sessions.startSession(3, host_ip, false);
137:
138: assertTrue(validator.isAccessAuthorized(validator
139: .validateSession(auth_id1, host_ip,
140: new DummyAttributes())));
141: assertTrue(validator.isAccessAuthorized(validator
142: .validateSession(auth_id1, host_ip,
143: new RoleAdminAttributes())));
144: assertTrue(validator.isAccessAuthorized(validator
145: .validateSession(auth_id1, host_ip,
146: new RoleMaintAttributes())));
147:
148: assertTrue(validator.isAccessAuthorized(validator
149: .validateSession(auth_id2, host_ip,
150: new DummyAttributes())));
151: assertEquals(AbstractSessionValidator.SESSION_INVALID,
152: validator.validateSession(auth_id2, host_ip,
153: new RoleAdminAttributes()));
154: assertTrue(validator.isAccessAuthorized(validator
155: .validateSession(auth_id2, host_ip,
156: new RoleMaintAttributes())));
157:
158: assertTrue(validator.isAccessAuthorized(validator
159: .validateSession(auth_id3, host_ip,
160: new DummyAttributes())));
161: assertEquals(AbstractSessionValidator.SESSION_INVALID,
162: validator.validateSession(auth_id3, host_ip,
163: new RoleAdminAttributes()));
164: assertEquals(AbstractSessionValidator.SESSION_INVALID,
165: validator.validateSession(auth_id3, host_ip,
166: new RoleMaintAttributes()));
167: } catch (SessionManagerException e) {
168: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
169: } catch (CredentialsManagerException e) {
170: e.printStackTrace();
171: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
172: } catch (SessionValidatorException e) {
173: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
174: } finally {
175: try {
176: sessions.remove();
177: } catch (SessionManagerException e) {
178: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
179: false);
180: }
181: try {
182: users.remove();
183: } catch (CredentialsManagerException e) {
184: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
185: false);
186: }
187: }
188: }
189:
190: class DummyAttributes implements SessionAttributes {
191: public boolean hasAttribute(String key) {
192: return false;
193: }
194:
195: public String getAttribute(String key) {
196: return null;
197: }
198: }
199:
200: class RoleMaintAttributes implements SessionAttributes {
201: public boolean hasAttribute(String key) {
202: if (key.equals("role")) {
203: return true;
204: }
205:
206: return false;
207: }
208:
209: public String getAttribute(String key) {
210: if (key.equals("role")) {
211: return "maint";
212: }
213:
214: return null;
215: }
216: }
217:
218: class RoleAdminAttributes implements SessionAttributes {
219: public boolean hasAttribute(String key) {
220: if (key.equals("role")) {
221: return true;
222: }
223:
224: return false;
225: }
226:
227: public String getAttribute(String key) {
228: if (key.equals("role")) {
229: return "admin";
230: }
231:
232: return null;
233: }
234: }
235: }
|