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: TestPurgingMixedAuthenticated.java 3669 2007-02-26 13:51:23Z gbevin $
008: */
009: package com.uwyn.rife.authentication.elements;
010:
011: import com.meterware.httpunit.GetMethodWebRequest;
012: import com.meterware.httpunit.WebConversation;
013: import com.meterware.httpunit.WebForm;
014: import com.meterware.httpunit.WebRequest;
015: import com.meterware.httpunit.WebResponse;
016: import com.uwyn.rife.authentication.SessionManager;
017: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsers;
018: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsersFactory;
019: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
020: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
021: import com.uwyn.rife.authentication.exceptions.RememberManagerException;
022: import com.uwyn.rife.authentication.remembermanagers.DatabaseRemember;
023: import com.uwyn.rife.authentication.remembermanagers.DatabaseRememberFactory;
024: import com.uwyn.rife.authentication.remembermanagers.RememberManagerFactoryFactory;
025: import com.uwyn.rife.authentication.sessionmanagers.SessionManagerFactoryFactory;
026: import com.uwyn.rife.authentication.sessionmanagers.SimpleSessionManagerFactory;
027: import com.uwyn.rife.database.Datasource;
028: import com.uwyn.rife.database.Datasources;
029: import com.uwyn.rife.tools.ExceptionUtils;
030: import com.uwyn.rife.tools.StringEncryptor;
031:
032: public class TestPurgingMixedAuthenticated extends
033: TestsuiteDatabaseAuthenticated {
034: private Datasource mDatasource = null;
035:
036: public TestPurgingMixedAuthenticated(String datasourceName,
037: int siteType, String name) {
038: super (datasourceName, siteType, name);
039:
040: mDatasource = Datasources.getRepInstance().getDatasource(
041: datasourceName);
042:
043: mProperties.put("datasource", mDatasource);
044: mProperties.put(
045: SimpleSessionManagerFactory.PROPERTYNAME_MANAGER_ID,
046: "purging_mixed_pgsql");
047: mProperties.put(
048: SimpleSessionManagerFactory.PROPERTYNAME_MANAGER_CLASS,
049: "MemorySessions");
050: mProperties
051: .put(
052: SessionManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
053: SimpleSessionManagerFactory.class.getName());
054: mProperties
055: .put(
056: RememberManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
057: DatabaseRememberFactory.class.getName());
058: }
059:
060: private SessionManager getSessionManager() {
061: return SessionManagerFactoryFactory.getManager(mProperties);
062: }
063:
064: public void setUp() throws Exception {
065: super .setUp();
066:
067: DatabaseUsers users = DatabaseUsersFactory
068: .getInstance(mDatasource);
069: try {
070: users.install();
071:
072: users.addRole("admin");
073: users.addRole("maint");
074:
075: users.addUser("guest", new RoleUserAttributes(43,
076: "guestpass"));
077: users.addUser("gbevin", new RoleUserAttributes(432,
078: "yeolpass", new String[] { "admin", "maint" }));
079: users.addUser("johndoe", new RoleUserAttributes(174,
080: "thepassofbass", new String[] { "maint" }));
081: users.setPasswordEncryptor(StringEncryptor.SHA);
082: users.addUser("guestencrypted", new RoleUserAttributes(44,
083: "guestpass"));
084: users
085: .addUser("gbevinencrypted", new RoleUserAttributes(
086: 433, "yeolpass", new String[] { "admin",
087: "maint" }));
088: users.setPasswordEncryptor(null);
089: } catch (CredentialsManagerException e) {
090: try {
091: users.remove();
092: } catch (CredentialsManagerException e2) {
093: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
094: false);
095: }
096: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
097: }
098:
099: DatabaseRemember remember = (DatabaseRemember) RememberManagerFactoryFactory
100: .getManager(mProperties);
101:
102: try {
103: remember.install();
104: } catch (RememberManagerException e) {
105: try {
106: remember.remove();
107: } catch (RememberManagerException e2) {
108: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
109: false);
110: }
111: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
112: }
113: }
114:
115: public void tearDown() throws Exception {
116: DatabaseUsers users = DatabaseUsersFactory
117: .getInstance(mDatasource);
118:
119: try {
120: users.remove();
121: } catch (CredentialsManagerException e) {
122: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
123: }
124:
125: DatabaseRemember remember = (DatabaseRemember) RememberManagerFactoryFactory
126: .getManager(mProperties);
127:
128: try {
129: remember.remove();
130: } catch (RememberManagerException e) {
131: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
132: }
133:
134: super .tearDown();
135: }
136:
137: public void testPurgingMixedAuthenticatedBasic() throws Exception {
138: setupSite("site/authentication_database.xml");
139:
140: WebConversation conversation = new WebConversation();
141: WebRequest request = null;
142: WebResponse response = null;
143: WebForm form = null;
144: String auth_id;
145:
146: request = new GetMethodWebRequest(
147: "http://localhost:8181/authentication/purging/mixed/basic");
148: response = conversation.getResponse(request);
149: form = response.getForms()[0];
150: form.setParameter("login", "guest");
151: form.setParameter("password", "guestpass");
152: response = form.submit();
153:
154: assertEquals(0, response.getForms().length);
155: auth_id = response.getTitle();
156:
157: assertEquals(1, getSessionManager().countSessions());
158:
159: request = new GetMethodWebRequest(
160: "http://localhost:8181/authentication/purging/mixed/basic");
161: response = conversation.getResponse(request);
162: form = response.getForms()[0];
163: form.setParameter("login", "guest");
164: form.setParameter("password", "guestpass");
165: response = form.submit();
166:
167: assertEquals(0, response.getForms().length);
168: auth_id = response.getTitle();
169:
170: assertEquals(2, getSessionManager().countSessions());
171:
172: try {
173: Thread.sleep(1000);
174: } catch (InterruptedException e) {
175: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
176: }
177:
178: request = new GetMethodWebRequest(
179: "http://localhost:8181/authentication/purging/mixed/basic");
180: request.setParameter("authid", auth_id);
181: response = conversation.getResponse(request);
182:
183: assertEquals(1, response.getForms().length);
184:
185: request = new GetMethodWebRequest(
186: "http://localhost:8181/authentication/purging/mixed/basic");
187: response = conversation.getResponse(request);
188: form = response.getForms()[0];
189: form.setParameter("login", "guest");
190: form.setParameter("password", "guestpass");
191: response = form.submit();
192:
193: assertEquals(0, response.getForms().length);
194:
195: assertEquals(1, getSessionManager().countSessions());
196: getSessionManager().eraseAllSessions();
197: }
198:
199: public void testPurgingMixedAuthenticatedEncrypted()
200: throws Exception {
201: setupSite("site/authentication_database.xml");
202:
203: WebConversation conversation = new WebConversation();
204: WebRequest request = null;
205: WebResponse response = null;
206: WebForm form = null;
207:
208: request = new GetMethodWebRequest(
209: "http://localhost:8181/authentication/purging/mixed/encrypted");
210: response = conversation.getResponse(request);
211: form = response.getForms()[0];
212: form.setParameter("login", "guestencrypted");
213: form.setParameter("password", "guestpass");
214: response = form.submit();
215:
216: assertEquals(0, response.getForms().length);
217:
218: request = new GetMethodWebRequest(
219: "http://localhost:8181/authentication/purging/mixed/encrypted");
220: response = conversation.getResponse(request);
221: form = response.getForms()[0];
222: form.setParameter("login", "gbevinencrypted");
223: form.setParameter("password", "yeolpass");
224: response = form.submit();
225:
226: assertEquals(0, response.getForms().length);
227:
228: request = new GetMethodWebRequest(
229: "http://localhost:8181/authentication/purging/mixed/encrypted");
230: response = conversation.getResponse(request);
231: form = response.getForms()[0];
232: form.setParameter("login", "guest");
233: form.setParameter("password", "guestpass");
234: response = form.submit();
235:
236: assertEquals(1, response.getForms().length);
237:
238: request = new GetMethodWebRequest(
239: "http://localhost:8181/authentication/purging/mixed/encrypted");
240: response = conversation.getResponse(request);
241: form = response.getForms()[0];
242: form.setParameter("login", "gbevin");
243: form.setParameter("password", "yeolpass");
244: response = form.submit();
245:
246: assertEquals(1, response.getForms().length);
247:
248: request = new GetMethodWebRequest(
249: "http://localhost:8181/authentication/purging/mixed/encrypted");
250: response = conversation.getResponse(request);
251: form = response.getForms()[0];
252: form.setParameter("login", "invalid");
253: form.setParameter("password", "invalid");
254: response = form.submit();
255:
256: assertEquals(1, response.getForms().length);
257: }
258:
259: public void testPurgingMixedAuthenticatedRemember()
260: throws Exception {
261: setupSite("site/authentication_database.xml");
262:
263: WebConversation conversation = new WebConversation();
264: WebRequest request = null;
265: WebResponse response = null;
266: WebForm form = null;
267:
268: // try the remember feature
269: request = new GetMethodWebRequest(
270: "http://localhost:8181/authentication/purging/mixed/remember");
271: response = conversation.getResponse(request);
272:
273: assertEquals(1, response.getForms().length);
274:
275: // indicate that the authentication should be remembered
276: request = new GetMethodWebRequest(
277: "http://localhost:8181/authentication/purging/mixed/remember");
278: response = conversation.getResponse(request);
279: form = response.getForms()[0];
280: form.setParameter("login", "guest");
281: form.setParameter("password", "guestpass");
282: form.setCheckbox("remember", true);
283: response = form.submit();
284: assertNull(conversation.getCookieValue("authid"));
285: assertNotNull(conversation.getCookieValue("rememberid"));
286: assertEquals(0, response.getForms().length);
287:
288: // check that the remember cookie works
289: request = new GetMethodWebRequest(
290: "http://localhost:8181/authentication/purging/mixed/remember");
291: response = conversation.getResponse(request);
292: assertNotNull(conversation.getCookieValue("rememberid"));
293: String rememberid1 = conversation.getCookieValue("rememberid");
294: assertEquals(0, response.getForms().length);
295:
296: // wait a while
297: try {
298: Thread.sleep(2000);
299: } catch (InterruptedException e) {
300: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
301: }
302:
303: // create a new remember id
304: conversation = new WebConversation();
305: request = new GetMethodWebRequest(
306: "http://localhost:8181/authentication/purging/mixed/remember");
307: response = conversation.getResponse(request);
308: form = response.getForms()[0];
309: form.setParameter("login", "guest");
310: form.setParameter("password", "guestpass");
311: form.setCheckbox("remember", true);
312: response = form.submit();
313: String rememberid3 = conversation.getCookieValue("rememberid");
314: assertEquals(0, response.getForms().length);
315:
316: // check that the previous remember id has been purged
317: conversation = new WebConversation();
318: conversation.addCookie("rememberid", rememberid1);
319: request = new GetMethodWebRequest(
320: "http://localhost:8181/authentication/purging/mixed/remember");
321: response = conversation.getResponse(request);
322: assertEquals(1, response.getForms().length);
323:
324: // check that the new remember id has not been purged
325: conversation = new WebConversation();
326: conversation.addCookie("rememberid", rememberid3);
327: request = new GetMethodWebRequest(
328: "http://localhost:8181/authentication/purging/mixed/remember");
329: response = conversation.getResponse(request);
330: assertEquals(0, response.getForms().length);
331: }
332: }
|