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: TestDatabaseSessions.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.exceptions.SessionManagerException;
013: import com.uwyn.rife.database.Datasource;
014: import com.uwyn.rife.ioc.HierarchicalProperties;
015: import com.uwyn.rife.tools.ExceptionUtils;
016: import junit.framework.TestCase;
017:
018: public class TestDatabaseSessions extends TestCase {
019: private Datasource mDatasource = null;
020: private HierarchicalProperties mProperties = null;
021:
022: public TestDatabaseSessions(Datasource datasource,
023: String datasourceName, String name) {
024: super (name);
025: mDatasource = datasource;
026: mProperties = new HierarchicalProperties();
027: mProperties.put("datasource", mDatasource);
028: mProperties
029: .put(
030: SessionManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
031: DatabaseSessionsFactory.class.getName());
032: }
033:
034: public void testInstantiation() {
035: DatabaseSessions manager = new DatabaseSessionsFactory()
036: .getManager(mProperties);
037: assertNotNull(manager);
038: }
039:
040: public void testInstall() {
041: DatabaseSessions sessions = new DatabaseSessionsFactory()
042: .getManager(mProperties);
043:
044: try {
045: assertTrue(true == sessions.install());
046: } catch (SessionManagerException e) {
047: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
048: }
049: }
050:
051: public void testRemove() {
052: DatabaseSessions sessions = new DatabaseSessionsFactory()
053: .getManager(mProperties);
054:
055: try {
056: assertTrue(true == sessions.remove());
057: } catch (SessionManagerException e) {
058: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
059: }
060: }
061:
062: public void testStartSession() {
063: DatabaseSessions sessions = new DatabaseSessionsFactory()
064: .getManager(mProperties);
065:
066: int user_id = 143;
067: String host_ip = "189.38.987.43";
068:
069: String auth_id = null;
070: try {
071: sessions.install();
072:
073: auth_id = sessions.startSession(user_id, host_ip, false);
074: assertFalse(sessions.wasRemembered(auth_id));
075:
076: assertEquals(1, sessions.countSessions());
077:
078: assertNotNull(auth_id);
079: assertTrue(auth_id.length() > 0);
080: } catch (SessionManagerException e) {
081: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
082: } finally {
083: try {
084: sessions.remove();
085: } catch (SessionManagerException e) {
086: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
087: false);
088: }
089: }
090: }
091:
092: public void testStartRememberedSession() {
093: DatabaseSessions sessions = new DatabaseSessionsFactory()
094: .getManager(mProperties);
095:
096: int user_id = 143;
097: String host_ip = "189.38.987.43";
098:
099: String auth_id = null;
100: try {
101: sessions.install();
102:
103: auth_id = sessions.startSession(user_id, host_ip, true);
104: assertTrue(sessions.wasRemembered(auth_id));
105:
106: assertEquals(1, sessions.countSessions());
107:
108: assertNotNull(auth_id);
109: assertTrue(auth_id.length() > 0);
110: } catch (SessionManagerException e) {
111: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
112: } finally {
113: try {
114: sessions.remove();
115: } catch (SessionManagerException e) {
116: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
117: false);
118: }
119: }
120: }
121:
122: public void testSessionExpiration() {
123: DatabaseSessions sessions = new DatabaseSessionsFactory()
124: .getManager(mProperties);
125: sessions.setSessionDuration(2000);
126:
127: int user_id = 1243;
128: String host_ip = "837.234.23.434";
129:
130: String auth_id = null;
131: try {
132: sessions.install();
133:
134: auth_id = sessions.startSession(user_id, host_ip, false);
135: assertTrue(sessions.isSessionValid(auth_id, host_ip));
136: assertEquals(1, sessions.countSessions());
137:
138: long start = System.currentTimeMillis();
139:
140: Thread.sleep(1500);
141: if (System.currentTimeMillis() - start <= 2000) {
142: assertTrue(sessions.isSessionValid(auth_id, host_ip));
143: assertEquals(1, sessions.countSessions());
144: Thread.sleep(510);
145: }
146:
147: assertTrue(!sessions.isSessionValid(auth_id, host_ip));
148: assertEquals(0, sessions.countSessions());
149: } catch (InterruptedException e) {
150: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
151: } catch (SessionManagerException e) {
152: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
153: } finally {
154: try {
155: sessions.remove();
156: } catch (SessionManagerException e) {
157: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
158: false);
159: }
160: }
161: }
162:
163: public void testContinueSession() {
164: DatabaseSessions sessions = new DatabaseSessionsFactory()
165: .getManager(mProperties);
166: sessions.setSessionDuration(2000);
167:
168: int user_id = 41;
169: String host_ip = "113.98.46.140";
170:
171: String auth_id = null;
172: try {
173: sessions.install();
174:
175: auth_id = sessions.startSession(user_id, host_ip, false);
176: assertTrue(sessions.isSessionValid(auth_id, host_ip));
177: Thread.sleep(1900);
178: assertTrue(sessions.continueSession(auth_id));
179: Thread.sleep(100);
180: assertTrue(sessions.isSessionValid(auth_id, host_ip));
181: Thread.sleep(1901);
182: assertTrue(!sessions.isSessionValid(auth_id, host_ip));
183: } catch (InterruptedException e) {
184: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
185: } catch (SessionManagerException e) {
186: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
187: } finally {
188: try {
189: sessions.remove();
190: } catch (SessionManagerException e) {
191: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
192: false);
193: }
194: }
195: }
196:
197: public void testContinueUnknownSession() {
198: DatabaseSessions sessions = new DatabaseSessionsFactory()
199: .getManager(mProperties);
200:
201: String auth_id = "unknown";
202: try {
203: sessions.install();
204:
205: assertTrue(false == sessions.continueSession(auth_id));
206: } catch (SessionManagerException e) {
207: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
208: } finally {
209: try {
210: sessions.remove();
211: } catch (SessionManagerException e) {
212: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
213: false);
214: }
215: }
216: }
217:
218: public void testEraseSession() {
219: DatabaseSessions sessions = new DatabaseSessionsFactory()
220: .getManager(mProperties);
221:
222: int user_id = 93;
223: String host_ip = "24.534.23.444";
224:
225: try {
226: sessions.install();
227:
228: long number_of_sessions = sessions.countSessions();
229: String auth_id = null;
230: auth_id = sessions.startSession(user_id, host_ip, false);
231: assertEquals(number_of_sessions + 1, sessions
232: .countSessions());
233: assertTrue(sessions.eraseSession(auth_id));
234: assertEquals(number_of_sessions, sessions.countSessions());
235: } catch (SessionManagerException e) {
236: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
237: } finally {
238: try {
239: sessions.remove();
240: } catch (SessionManagerException e) {
241: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
242: false);
243: }
244: }
245: }
246:
247: public void testEraseUnknownSession() {
248: DatabaseSessions sessions = new DatabaseSessionsFactory()
249: .getManager(mProperties);
250:
251: String auth_id = "unknown";
252: try {
253: sessions.install();
254:
255: assertTrue(false == sessions.eraseSession(auth_id));
256: } catch (SessionManagerException e) {
257: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
258: } finally {
259: try {
260: sessions.remove();
261: } catch (SessionManagerException e) {
262: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
263: false);
264: }
265: }
266: }
267:
268: public void testEraseAllSessions() {
269: DatabaseSessions sessions = new DatabaseSessionsFactory()
270: .getManager(mProperties);
271:
272: try {
273: sessions.install();
274:
275: sessions.startSession(232, "873.232.44.333", false);
276: sessions.startSession(232, "873.232.44.333", false);
277: sessions.startSession(23, "873.232.44.333", false);
278: sessions.startSession(53, "873.232.44.333", false);
279: sessions.startSession(53, "873.232.44.333", false);
280: sessions.startSession(232, "873.232.44.333", false);
281: sessions.startSession(23, "873.232.44.333", false);
282:
283: assertTrue(sessions.countSessions() > 0);
284: sessions.eraseAllSessions();
285: assertEquals(0, sessions.countSessions());
286: } catch (SessionManagerException e) {
287: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
288: } finally {
289: try {
290: sessions.remove();
291: } catch (SessionManagerException e) {
292: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
293: false);
294: }
295: }
296: }
297:
298: public void testEraseUserSessions() {
299: DatabaseSessions sessions = new DatabaseSessionsFactory()
300: .getManager(mProperties);
301:
302: try {
303: sessions.install();
304:
305: assertEquals(0, sessions.countSessions());
306: sessions.startSession(8433, "143.98.32.545", false);
307: sessions.startSession(8433, "143.98.32.545", false);
308: sessions.startSession(8432, "143.98.32.545", false);
309: sessions.startSession(8431, "143.98.32.545", false);
310: assertTrue(sessions.countSessions() > 0);
311: assertTrue(sessions.eraseUserSessions(8433));
312: assertEquals(2, sessions.countSessions());
313: } catch (SessionManagerException e) {
314: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
315: } finally {
316: try {
317: sessions.remove();
318: } catch (SessionManagerException e) {
319: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
320: false);
321: }
322: }
323: }
324:
325: public void testEraseUnkownUserSessions() {
326: DatabaseSessions sessions = new DatabaseSessionsFactory()
327: .getManager(mProperties);
328:
329: try {
330: sessions.install();
331:
332: assertEquals(0, sessions.countSessions());
333: sessions.startSession(8432, "143.98.32.545", false);
334: sessions.startSession(8431, "143.98.32.545", false);
335: assertTrue(sessions.countSessions() > 0);
336: assertTrue(!sessions.eraseUserSessions(8433));
337: assertEquals(2, sessions.countSessions());
338: } catch (SessionManagerException e) {
339: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
340: } finally {
341: try {
342: sessions.remove();
343: } catch (SessionManagerException e) {
344: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
345: false);
346: }
347: }
348: }
349:
350: public void testPurgeSessions() {
351: DatabaseSessions sessions = new DatabaseSessionsFactory()
352: .getManager(mProperties);
353: sessions.setSessionDuration(2000);
354:
355: int user_id = 9478;
356: String host_ip = "98.232.12.456";
357:
358: try {
359: sessions.install();
360:
361: sessions.eraseAllSessions();
362: assertEquals(0, sessions.countSessions());
363:
364: sessions.startSession(user_id, host_ip, false);
365: assertEquals(1, sessions.countSessions());
366:
367: Thread.sleep(2010);
368:
369: sessions.purgeSessions();
370:
371: sessions.startSession(user_id, host_ip, false);
372: assertEquals(1, sessions.countSessions());
373: } catch (InterruptedException e) {
374: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
375: } catch (SessionManagerException e) {
376: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
377: } finally {
378: try {
379: sessions.remove();
380: } catch (SessionManagerException e) {
381: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
382: false);
383: }
384: }
385: }
386:
387: public void testCountSessions() {
388: DatabaseSessions sessions = new DatabaseSessionsFactory()
389: .getManager(mProperties);
390: sessions.setSessionDuration(4000);
391:
392: int user_id1 = 9478;
393: String host_ip1 = "98.232.12.456";
394:
395: int user_id2 = 9479;
396: String host_ip2 = "98.232.12.457";
397:
398: int user_id3 = 9480;
399: String host_ip3 = "98.232.12.458";
400:
401: try {
402: sessions.install();
403:
404: sessions.eraseAllSessions();
405: assertEquals(0, sessions.countSessions());
406:
407: sessions.startSession(user_id1, host_ip1, false);
408: assertEquals(1, sessions.countSessions());
409:
410: Thread.sleep(2000);
411:
412: sessions.startSession(user_id2, host_ip2, false);
413: assertEquals(2, sessions.countSessions());
414:
415: Thread.sleep(1000);
416:
417: sessions.startSession(user_id3, host_ip3, false);
418: assertEquals(3, sessions.countSessions());
419:
420: Thread.sleep(1100);
421:
422: assertEquals(2, sessions.countSessions());
423: } catch (InterruptedException e) {
424: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
425: } catch (SessionManagerException e) {
426: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
427: } finally {
428: try {
429: sessions.remove();
430: } catch (SessionManagerException e) {
431: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
432: false);
433: }
434: }
435: }
436:
437: public void testListSessions() {
438: DatabaseSessions sessions = new DatabaseSessionsFactory()
439: .getManager(mProperties);
440: sessions.setSessionDuration(4000);
441:
442: final int user_id1 = 9478;
443: final String host_ip1 = "98.232.12.456";
444:
445: final int user_id2 = 9479;
446: final String host_ip2 = "98.232.12.457";
447:
448: final int user_id3 = 9480;
449: final String host_ip3 = "98.232.12.458";
450:
451: final int[] count = new int[1];
452: count[0] = 0;
453: try {
454: sessions.install();
455:
456: sessions.eraseAllSessions();
457:
458: assertEquals(false, sessions
459: .listSessions(new ListSessions() {
460: public boolean foundSession(long userId,
461: String hostIp, String authId) {
462: fail();
463: return true;
464: }
465: }));
466:
467: sessions.startSession(user_id1, host_ip1, false);
468:
469: count[0] = 0;
470: assertEquals(true, sessions
471: .listSessions(new ListSessions() {
472: public boolean foundSession(long userId,
473: String hostIp, String authId) {
474: count[0]++;
475: assertTrue(count[0] <= 1);
476:
477: assertTrue(9478 == userId);
478: assertTrue(host_ip1.equals(hostIp));
479:
480: return true;
481: }
482: }));
483:
484: Thread.sleep(2000);
485:
486: sessions.startSession(user_id2, host_ip2, false);
487:
488: count[0] = 0;
489: assertEquals(true, sessions
490: .listSessions(new ListSessions() {
491: public boolean foundSession(long userId,
492: String hostIp, String authId) {
493: count[0]++;
494: assertTrue(count[0] <= 2);
495:
496: assertTrue(9478 == userId || 9479 == userId);
497: assertTrue(host_ip1.equals(hostIp)
498: || host_ip2.equals(hostIp));
499:
500: return true;
501: }
502: }));
503:
504: Thread.sleep(1000);
505:
506: sessions.startSession(user_id3, host_ip3, false);
507:
508: count[0] = 0;
509: assertEquals(true, sessions
510: .listSessions(new ListSessions() {
511: public boolean foundSession(long userId,
512: String hostIp, String authId) {
513: count[0]++;
514: assertTrue(count[0] <= 3);
515:
516: assertTrue(9478 == userId || 9479 == userId
517: || 9480 == userId);
518: assertTrue(host_ip1.equals(hostIp)
519: || host_ip2.equals(hostIp)
520: || host_ip3.equals(hostIp));
521:
522: return true;
523: }
524: }));
525:
526: Thread.sleep(1100);
527:
528: count[0] = 0;
529: assertEquals(true, sessions
530: .listSessions(new ListSessions() {
531: public boolean foundSession(long userId,
532: String hostIp, String authId) {
533: count[0]++;
534: assertTrue(count[0] <= 2);
535:
536: assertTrue(9479 == userId || 9480 == userId);
537: assertTrue(host_ip2.equals(hostIp)
538: || host_ip3.equals(hostIp));
539:
540: return true;
541: }
542: }));
543: } catch (InterruptedException e) {
544: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
545: } catch (SessionManagerException e) {
546: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
547: } finally {
548: try {
549: sessions.remove();
550: } catch (SessionManagerException e) {
551: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
552: false);
553: }
554: }
555: }
556: }
|