KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > authentication > loginmodules > JNDILoginModuleTest


1 /*
2  jGuard is a security framework based on top of jaas (java authentication and authorization security).
3  it is written for web applications, to resolve simply, access control problems.
4  version $Name$
5  http://sourceforge.net/projects/jguard/
6
7  Copyright (C) 2004 Charles GAY
8
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26
27  */

28 package net.sf.jguard.ext.authentication.loginmodules;
29
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.LogManager JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 import javax.naming.Context JavaDoc;
38 import javax.naming.directory.SearchControls JavaDoc;
39 import javax.security.auth.Subject JavaDoc;
40 import javax.security.auth.login.LoginException JavaDoc;
41
42 import junit.framework.TestCase;
43 import net.sf.jguard.jee.authentication.callbacks.HttpCallbackHandler;
44 import net.sf.jguard.jee.authentication.http.HttpConstants;
45
46 import com.kizna.servletunit.HttpServletRequestSimulator;
47 import com.kizna.servletunit.HttpServletResponseSimulator;
48
49 /**
50  * to enable this test, set "jndi.test.skip" system property to "false".
51  * class used to unit test JNDILoginModule class.
52  *
53  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles GAY</a>
54  *
55  */

56 public class JNDILoginModuleTest extends TestCase {
57
58     private static final String JavaDoc SKIP_JNDI_TESTS = "jndi.test.skip";
59     private static JNDILoginModule lm = null;
60
61     /*
62      * Test method for 'net.sf.jguard.authentication.loginmodules.JNDILoginModule.initialize(Subject, CallbackHandler, Map, Map)'
63      */

64     public void testInitialize() {
65         if (!"false".equals(System.getProperty(SKIP_JNDI_TESTS))) {
66             return;
67         }
68
69         String JavaDoc login = "login";
70         HttpCallbackHandler.setLoginField(login);
71         String JavaDoc password = "password";
72         HttpCallbackHandler.setPasswordField(password);
73         HttpServletRequestSimulator request = new HttpServletRequestSimulator();
74         request.addParameter(login, "mysamaccountname");
75         request.addParameter(password, "toto");
76         HttpServletResponseSimulator response = new HttpServletResponseSimulator();
77         HttpCallbackHandler cbh = new HttpCallbackHandler(request, response, HttpConstants.FORM_AUTH);
78         lm = new JNDILoginModule();
79         Map JavaDoc state = new HashMap JavaDoc();
80         Map JavaDoc env = new HashMap JavaDoc();
81         env.put("preauth."+"com.sun.jndi.ldap.connect.pool","true");
82         env.put("preauth."+"com.sun.jndi.ldap.connect.pool.prefsize","5");
83         env.put("preauth."+Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
84         env.put("preauth."+Context.PROVIDER_URL, "ldap://myserver:389");
85         env.put("preauth."+Context.SECURITY_AUTHENTICATION, "none");
86         env.put("preauth."+"searchcontrols."+"searchscope", Integer.toString(SearchControls.SUBTREE_SCOPE).toString());
87         env.put("preauth."+"search.base.dn", "dc=toto,dc=com");
88         env.put("preauth."+"search.filter", "(&(samAccountName={0})(!(proxyAddresses=*)))");
89         
90         
91         env.put("auth."+"com.sun.jndi.ldap.connect.pool","true");
92         env.put("auth."+"com.sun.jndi.ldap.connect.pool.prefsize","5");
93         
94         env.put("auth."+Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
95         env.put("auth."+Context.PROVIDER_URL, "ldap://myserver:389");
96         env.put("auth."+Context.SECURITY_AUTHENTICATION, "simple");
97         
98
99         //env.put("contextforcommit", "preauth");
100

101         lm.initialize(new Subject JavaDoc(), cbh, state, env);
102
103     }
104
105     /*
106      * Test method for 'net.sf.jguard.authentication.loginmodules.JNDILoginModule.login()'
107      */

108     public void testLogin() {
109         if (!"false".equals(System.getProperty(SKIP_JNDI_TESTS))) {
110             return;
111         }
112
113         testInitialize();
114         try {
115             LogManager JavaDoc logManager = LogManager.getLogManager();
116             Enumeration JavaDoc loggerNames = logManager.getLoggerNames();
117             while(loggerNames.hasMoreElements()){
118                 String JavaDoc loggerName = (String JavaDoc)loggerNames.nextElement();
119                 Logger JavaDoc logger = logManager.getLogger(loggerName);
120                 logger.setLevel(Level.FINEST);
121             }
122             boolean loginOK = lm.login();
123         } catch (LoginException JavaDoc e) {
124             TestCase.fail(e.getMessage());
125         }
126     }
127
128     /*
129      * Test method for 'net.sf.jguard.authentication.loginmodules.JNDILoginModule.commit()'
130      */

131     public void testCommit() {
132         if (!"false".equals(System.getProperty(SKIP_JNDI_TESTS))) {
133             return;
134         }
135
136         testInitialize();
137         testLogin();
138         try {
139             boolean commitOK = lm.commit();
140         } catch (LoginException JavaDoc e) {
141             TestCase.fail(e.getMessage());
142         }
143     }
144
145 }
146
Popular Tags