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: TestMemorySessions.java 3643 2007-01-12 15:29:45Z gbevin $
008: */
009: package com.uwyn.rife.authentication.sessionmanagers;
010:
011: import com.uwyn.rife.authentication.ListSessions;
012: import com.uwyn.rife.authentication.SessionManager;
013: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
014: import com.uwyn.rife.ioc.HierarchicalProperties;
015: import com.uwyn.rife.tools.ExceptionUtils;
016: import junit.framework.TestCase;
017:
018: public class TestMemorySessions extends TestCase {
019: private HierarchicalProperties mProperties = null;
020:
021: public TestMemorySessions(String name) {
022: super (name);
023: mProperties = new HierarchicalProperties();
024: mProperties.put(
025: SimpleSessionManagerFactory.PROPERTYNAME_MANAGER_CLASS,
026: "MemorySessions");
027: }
028:
029: public void testInstantiation() {
030: SessionManager sessions = null;
031:
032: sessions = new SimpleSessionManagerFactory()
033: .getManager(mProperties);
034:
035: assertNotNull(sessions);
036: assertTrue(sessions instanceof MemorySessions);
037: }
038:
039: public void testStartSession() {
040: MemorySessions sessions = new MemorySessions();
041: try {
042: sessions.eraseAllSessions();
043: } catch (SessionManagerException e) {
044: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
045: }
046:
047: int user_id = 143;
048: String host_ip = "189.38.987.43";
049:
050: String auth_id = null;
051: try {
052: auth_id = sessions.startSession(user_id, host_ip, false);
053: assertFalse(sessions.wasRemembered(auth_id));
054:
055: assertNotNull(auth_id);
056: assertTrue(auth_id.length() > 0);
057:
058: assertEquals(1, sessions.countSessions());
059:
060: MemorySession session_instance = sessions
061: .getSession(auth_id);
062: assertEquals(user_id, sessions.getSessionUserId(auth_id));
063: assertEquals(auth_id, session_instance.getAuthId());
064: assertEquals(user_id, session_instance.getUserId());
065: assertEquals(host_ip, session_instance.getHostIp());
066: assertTrue(session_instance.getStart() <= System
067: .currentTimeMillis());
068: } catch (SessionManagerException e) {
069: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
070: }
071: }
072:
073: public void testStartRememberedSession() {
074: MemorySessions sessions = new MemorySessions();
075: try {
076: sessions.eraseAllSessions();
077: } catch (SessionManagerException e) {
078: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
079: }
080:
081: int user_id = 143;
082: String host_ip = "189.38.987.43";
083:
084: String auth_id = null;
085: try {
086: auth_id = sessions.startSession(user_id, host_ip, true);
087: assertTrue(sessions.wasRemembered(auth_id));
088:
089: assertEquals(1, sessions.countSessions());
090:
091: assertNotNull(auth_id);
092: assertTrue(auth_id.length() > 0);
093: } catch (SessionManagerException e) {
094: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
095: }
096: }
097:
098: public void testSessionExpiration() {
099: MemorySessions sessions = new MemorySessions();
100: sessions.setSessionDuration(500);
101:
102: int user_id = 1243;
103: String host_ip = "837.234.23.434";
104:
105: String auth_id = null;
106: try {
107: sessions.eraseAllSessions();
108:
109: auth_id = sessions.startSession(user_id, host_ip, false);
110: assertEquals(1, sessions.countSessions());
111:
112: assertTrue(sessions.isSessionValid(auth_id, host_ip));
113: Thread.sleep(400);
114: assertTrue(sessions.isSessionValid(auth_id, host_ip));
115:
116: Thread.sleep(101);
117: assertTrue(!sessions.isSessionValid(auth_id, host_ip));
118:
119: assertEquals(0, sessions.countSessions());
120: } catch (InterruptedException e) {
121: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
122: } catch (SessionManagerException e) {
123: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
124: }
125: }
126:
127: public void testContinueSession() {
128: MemorySessions sessions = new MemorySessions();
129: sessions.setSessionDuration(2000);
130:
131: int user_id = 41;
132: String host_ip = "113.98.46.140";
133:
134: String auth_id = null;
135: try {
136: auth_id = sessions.startSession(user_id, host_ip, false);
137: assertTrue(sessions.isSessionValid(auth_id, host_ip));
138: Thread.sleep(1900);
139: assertTrue(sessions.continueSession(auth_id));
140: Thread.sleep(100);
141: assertTrue(sessions.isSessionValid(auth_id, host_ip));
142: Thread.sleep(1901);
143: assertTrue(!sessions.isSessionValid(auth_id, host_ip));
144: } catch (InterruptedException e) {
145: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
146: } catch (SessionManagerException e) {
147: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
148: }
149: }
150:
151: public void testContinueUnknownSession() {
152: MemorySessions sessions = new MemorySessions();
153:
154: String auth_id = "unknown";
155: try {
156: assertTrue(false == sessions.continueSession(auth_id));
157: } catch (SessionManagerException e) {
158: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
159: }
160: }
161:
162: public void testEraseSession() {
163: MemorySessions sessions = new MemorySessions();
164: sessions.setSessionDuration(1200000);
165:
166: int user_id = 93;
167: String host_ip = "24.534.23.444";
168:
169: String auth_id = null;
170: try {
171: auth_id = sessions.startSession(user_id, host_ip, false);
172: long number_of_sessions = sessions.countSessions();
173: assertTrue(sessions.eraseSession(auth_id));
174: assertEquals(number_of_sessions - 1, sessions
175: .countSessions());
176: } catch (SessionManagerException e) {
177: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
178: }
179: }
180:
181: public void testEraseUnknownSession() {
182: MemorySessions sessions = new MemorySessions();
183: sessions.setSessionDuration(1200000);
184:
185: String auth_id = "unknown";
186: try {
187: assertTrue(false == sessions.eraseSession(auth_id));
188: } catch (SessionManagerException e) {
189: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
190: }
191: }
192:
193: public void testEraseAllSessions() {
194: MemorySessions sessions = new MemorySessions();
195: sessions.setSessionDuration(1200000);
196:
197: try {
198: sessions.startSession(8433, "143.98.32.545", false);
199: assertTrue(sessions.countSessions() > 0);
200: sessions.eraseAllSessions();
201: assertEquals(0, sessions.countSessions());
202: } catch (SessionManagerException e) {
203: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
204: }
205: }
206:
207: public void testEraseUserSessions() {
208: MemorySessions sessions = new MemorySessions();
209: sessions.setSessionDuration(1200000);
210:
211: try {
212: sessions.eraseAllSessions();
213: assertEquals(0, sessions.countSessions());
214: sessions.startSession(8433, "143.98.32.545", false);
215: sessions.startSession(8433, "143.98.32.545", false);
216: sessions.startSession(8432, "143.98.32.545", false);
217: sessions.startSession(8431, "143.98.32.545", false);
218: assertTrue(sessions.countSessions() > 0);
219: assertTrue(sessions.eraseUserSessions(8433));
220: assertEquals(2, sessions.countSessions());
221: } catch (SessionManagerException e) {
222: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
223: }
224: }
225:
226: public void testEraseUnkownUserSessions() {
227: MemorySessions sessions = new MemorySessions();
228: sessions.setSessionDuration(1200000);
229:
230: try {
231: sessions.eraseAllSessions();
232: assertEquals(0, sessions.countSessions());
233: sessions.startSession(8432, "143.98.32.545", false);
234: sessions.startSession(8431, "143.98.32.545", false);
235: assertTrue(sessions.countSessions() > 0);
236: assertTrue(!sessions.eraseUserSessions(8433));
237: assertEquals(2, sessions.countSessions());
238: } catch (SessionManagerException e) {
239: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
240: }
241: }
242:
243: public void testPurgeSessions() {
244: MemorySessions sessions = new MemorySessions();
245: sessions.setSessionDuration(2000);
246:
247: int user_id = 9478;
248: String host_ip = "98.232.12.456";
249:
250: try {
251: sessions.eraseAllSessions();
252: assertEquals(0, sessions.countSessions());
253:
254: sessions.startSession(user_id, host_ip, false);
255: assertEquals(1, sessions.countSessions());
256:
257: Thread.sleep(2010);
258:
259: sessions.purgeSessions();
260:
261: sessions.startSession(user_id, host_ip, false);
262: assertEquals(1, sessions.countSessions());
263: } catch (InterruptedException e) {
264: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
265: } catch (SessionManagerException e) {
266: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
267: }
268: }
269:
270: public void testCountSessions() {
271: MemorySessions sessions = new MemorySessions();
272: sessions.setSessionDuration(4000);
273:
274: int user_id1 = 9478;
275: String host_ip1 = "98.232.12.456";
276:
277: int user_id2 = 9479;
278: String host_ip2 = "98.232.12.457";
279:
280: int user_id3 = 9480;
281: String host_ip3 = "98.232.12.458";
282:
283: try {
284: sessions.eraseAllSessions();
285: assertEquals(0, sessions.countSessions());
286:
287: sessions.startSession(user_id1, host_ip1, false);
288: assertEquals(1, sessions.countSessions());
289:
290: Thread.sleep(2000);
291:
292: sessions.startSession(user_id2, host_ip2, false);
293: assertEquals(2, sessions.countSessions());
294:
295: Thread.sleep(1000);
296:
297: sessions.startSession(user_id3, host_ip3, false);
298: assertEquals(3, sessions.countSessions());
299:
300: Thread.sleep(1100);
301:
302: assertEquals(2, sessions.countSessions());
303: } catch (InterruptedException e) {
304: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
305: } catch (SessionManagerException e) {
306: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
307: }
308: }
309:
310: public void testListSessions() {
311: MemorySessions sessions = new MemorySessions();
312: sessions.setSessionDuration(4000);
313:
314: final int user_id1 = 9478;
315: final String host_ip1 = "98.232.12.456";
316:
317: final int user_id2 = 9479;
318: final String host_ip2 = "98.232.12.457";
319:
320: final int user_id3 = 9480;
321: final String host_ip3 = "98.232.12.458";
322:
323: final int[] count = new int[1];
324: count[0] = 0;
325: try {
326: sessions.eraseAllSessions();
327:
328: assertEquals(false, sessions
329: .listSessions(new ListSessions() {
330: public boolean foundSession(long userId,
331: String hostIp, String authId) {
332: fail();
333: return true;
334: }
335: }));
336:
337: sessions.startSession(user_id1, host_ip1, false);
338:
339: count[0] = 0;
340: assertEquals(true, sessions
341: .listSessions(new ListSessions() {
342: public boolean foundSession(long userId,
343: String hostIp, String authId) {
344: count[0]++;
345: assertTrue(count[0] <= 1);
346:
347: assertTrue(9478 == userId);
348: assertTrue(host_ip1.equals(hostIp));
349:
350: return true;
351: }
352: }));
353:
354: Thread.sleep(2000);
355:
356: sessions.startSession(user_id2, host_ip2, false);
357:
358: count[0] = 0;
359: assertEquals(true, sessions
360: .listSessions(new ListSessions() {
361: public boolean foundSession(long userId,
362: String hostIp, String authId) {
363: count[0]++;
364: assertTrue(count[0] <= 2);
365:
366: assertTrue(9478 == userId || 9479 == userId);
367: assertTrue(host_ip1.equals(hostIp)
368: || host_ip2.equals(hostIp));
369:
370: return true;
371: }
372: }));
373:
374: Thread.sleep(1000);
375:
376: sessions.startSession(user_id3, host_ip3, false);
377:
378: count[0] = 0;
379: assertEquals(true, sessions
380: .listSessions(new ListSessions() {
381: public boolean foundSession(long userId,
382: String hostIp, String authId) {
383: count[0]++;
384: assertTrue(count[0] <= 3);
385:
386: assertTrue(9478 == userId || 9479 == userId
387: || 9480 == userId);
388: assertTrue(host_ip1.equals(hostIp)
389: || host_ip2.equals(hostIp)
390: || host_ip3.equals(hostIp));
391:
392: return true;
393: }
394: }));
395:
396: Thread.sleep(1100);
397:
398: count[0] = 0;
399: assertEquals(true, sessions
400: .listSessions(new ListSessions() {
401: public boolean foundSession(long userId,
402: String hostIp, String authId) {
403: count[0]++;
404: assertTrue(count[0] <= 2);
405:
406: assertTrue(9479 == userId || 9480 == userId);
407: assertTrue(host_ip2.equals(hostIp)
408: || host_ip3.equals(hostIp));
409:
410: return true;
411: }
412: }));
413: } catch (InterruptedException e) {
414: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
415: } catch (SessionManagerException e) {
416: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
417: }
418: }
419: }
|