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: TestSimpleSessionValidator.java 3308 2006-06-15 18:54:14Z gbevin $
008: */
009: package com.uwyn.rife.authentication.sessionvalidators;
010:
011: import com.uwyn.rife.authentication.SessionAttributes;
012: import com.uwyn.rife.authentication.credentialsmanagers.MemoryUsers;
013: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
014: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
015: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
016: import com.uwyn.rife.authentication.exceptions.SessionValidatorException;
017: import com.uwyn.rife.authentication.sessionmanagers.MemorySessions;
018: import com.uwyn.rife.authentication.sessionmanagers.SimpleSessionManagerFactory;
019: import com.uwyn.rife.authentication.sessionvalidators.BasicSessionValidator;
020: import com.uwyn.rife.ioc.HierarchicalProperties;
021: import com.uwyn.rife.tools.ExceptionUtils;
022: import junit.framework.TestCase;
023:
024: public class TestBasicSessionValidator extends TestCase {
025: private HierarchicalProperties mProperties = null;
026:
027: public TestBasicSessionValidator(String name) {
028: super (name);
029: mProperties = new HierarchicalProperties();
030: mProperties.put(
031: SimpleSessionManagerFactory.PROPERTYNAME_MANAGER_CLASS,
032: MemorySessions.class.getName());
033: }
034:
035: public void testInstantiation() {
036: AbstractSessionValidator validator = null;
037:
038: validator = new BasicSessionValidator();
039:
040: assertNotNull(validator);
041: }
042:
043: public void testValidSessionId() {
044: BasicSessionValidator validator = new BasicSessionValidator();
045:
046: assertTrue(validator.isAccessAuthorized(1));
047: }
048:
049: public void testSessionValidity() {
050: BasicSessionValidator validator = new BasicSessionValidator();
051: MemorySessions sessions = (MemorySessions) new SimpleSessionManagerFactory()
052: .getManager(mProperties);
053: sessions.setSessionDuration(120000);
054: validator.setSessionManager(sessions);
055:
056: int user_id = 9478;
057: String host_ip = "98.232.12.456";
058:
059: String auth_id = null;
060: try {
061: auth_id = sessions.startSession(user_id, host_ip, false);
062: assertTrue(validator.isAccessAuthorized(validator
063: .validateSession(auth_id, host_ip,
064: new DummyAttributes())));
065: sessions.setRestrictHostIp(true);
066: assertEquals(AbstractSessionValidator.SESSION_INVALID,
067: validator.validateSession(auth_id, "1.1.1.1",
068: new DummyAttributes()));
069: sessions.setRestrictHostIp(false);
070: assertEquals(AbstractSessionValidator.SESSION_VALID,
071: validator.validateSession(auth_id, "1.1.1.1",
072: new DummyAttributes()));
073: assertEquals(AbstractSessionValidator.SESSION_INVALID,
074: validator.validateSession("not_valid", host_ip,
075: new DummyAttributes()));
076:
077: sessions.setSessionDuration(0);
078:
079: Thread.sleep(2);
080: assertEquals(AbstractSessionValidator.SESSION_INVALID,
081: validator.validateSession(auth_id, host_ip,
082: new DummyAttributes()));
083: } catch (InterruptedException e) {
084: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
085: } catch (SessionManagerException e) {
086: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
087: } catch (SessionValidatorException e) {
088: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
089: }
090: }
091:
092: public void testSessionValidityRole() {
093: BasicSessionValidator validator = new BasicSessionValidator();
094: MemoryUsers users = new MemoryUsers();
095: MemorySessions sessions = (MemorySessions) new SimpleSessionManagerFactory()
096: .getManager(mProperties);
097: sessions.setSessionDuration(120000);
098: validator.setSessionManager(sessions);
099: validator.setCredentialsManager(users);
100:
101: String host_ip = "98.232.12.456";
102:
103: String auth_id1 = null;
104: String auth_id2 = null;
105: String auth_id3 = null;
106: try {
107: users.addRole("admin");
108: users.addRole("maint");
109:
110: users.addUser("login1", new RoleUserAttributes(1,
111: "thepassword", new String[] { "admin", "maint" }));
112: users.addUser("login2", new RoleUserAttributes(2,
113: "thepassword", new String[] { "maint" }));
114: users.addUser("login3", new RoleUserAttributes(3,
115: "thepassword"));
116:
117: auth_id1 = sessions.startSession(1, host_ip, false);
118: auth_id2 = sessions.startSession(2, host_ip, false);
119: auth_id3 = sessions.startSession(3, host_ip, false);
120:
121: assertTrue(validator.isAccessAuthorized(validator
122: .validateSession(auth_id1, host_ip,
123: new DummyAttributes())));
124: assertTrue(validator.isAccessAuthorized(validator
125: .validateSession(auth_id1, host_ip,
126: new RoleAdminAttributes())));
127: assertTrue(validator.isAccessAuthorized(validator
128: .validateSession(auth_id1, host_ip,
129: new RoleMaintAttributes())));
130:
131: assertTrue(validator.isAccessAuthorized(validator
132: .validateSession(auth_id2, host_ip,
133: new DummyAttributes())));
134: assertEquals(AbstractSessionValidator.SESSION_INVALID,
135: validator.validateSession(auth_id2, host_ip,
136: new RoleAdminAttributes()));
137: assertTrue(validator.isAccessAuthorized(validator
138: .validateSession(auth_id2, host_ip,
139: new RoleMaintAttributes())));
140:
141: assertTrue(validator.isAccessAuthorized(validator
142: .validateSession(auth_id3, host_ip,
143: new DummyAttributes())));
144: assertEquals(AbstractSessionValidator.SESSION_INVALID,
145: validator.validateSession(auth_id3, host_ip,
146: new RoleAdminAttributes()));
147: assertEquals(AbstractSessionValidator.SESSION_INVALID,
148: validator.validateSession(auth_id3, host_ip,
149: new RoleMaintAttributes()));
150: } catch (SessionManagerException e) {
151: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
152: } catch (CredentialsManagerException e) {
153: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
154: } catch (SessionValidatorException e) {
155: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
156: }
157: }
158:
159: class DummyAttributes implements SessionAttributes {
160: public boolean hasAttribute(String key) {
161: return false;
162: }
163:
164: public String getAttribute(String key) {
165: return null;
166: }
167: }
168:
169: class RoleMaintAttributes implements SessionAttributes {
170: public boolean hasAttribute(String key) {
171: if (key.equals("role")) {
172: return true;
173: }
174:
175: return false;
176: }
177:
178: public String getAttribute(String key) {
179: if (key.equals("role")) {
180: return "maint";
181: }
182:
183: return null;
184: }
185: }
186:
187: class RoleAdminAttributes implements SessionAttributes {
188: public boolean hasAttribute(String key) {
189: if (key.equals("role")) {
190: return true;
191: }
192:
193: return false;
194: }
195:
196: public String getAttribute(String key) {
197: if (key.equals("role")) {
198: return "admin";
199: }
200:
201: return null;
202: }
203: }
204: }
|