1 28 package net.sf.jguard.ext.authentication.loginmodules; 29 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import java.util.logging.Level ; 34 import java.util.logging.LogManager ; 35 import java.util.logging.Logger ; 36 37 import javax.naming.Context ; 38 import javax.naming.directory.SearchControls ; 39 import javax.security.auth.Subject ; 40 import javax.security.auth.login.LoginException ; 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 56 public class JNDILoginModuleTest extends TestCase { 57 58 private static final String SKIP_JNDI_TESTS = "jndi.test.skip"; 59 private static JNDILoginModule lm = null; 60 61 64 public void testInitialize() { 65 if (!"false".equals(System.getProperty(SKIP_JNDI_TESTS))) { 66 return; 67 } 68 69 String login = "login"; 70 HttpCallbackHandler.setLoginField(login); 71 String 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 state = new HashMap (); 80 Map env = new HashMap (); 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 101 lm.initialize(new Subject (), cbh, state, env); 102 103 } 104 105 108 public void testLogin() { 109 if (!"false".equals(System.getProperty(SKIP_JNDI_TESTS))) { 110 return; 111 } 112 113 testInitialize(); 114 try { 115 LogManager logManager = LogManager.getLogManager(); 116 Enumeration loggerNames = logManager.getLoggerNames(); 117 while(loggerNames.hasMoreElements()){ 118 String loggerName = (String )loggerNames.nextElement(); 119 Logger logger = logManager.getLogger(loggerName); 120 logger.setLevel(Level.FINEST); 121 } 122 boolean loginOK = lm.login(); 123 } catch (LoginException e) { 124 TestCase.fail(e.getMessage()); 125 } 126 } 127 128 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 e) { 141 TestCase.fail(e.getMessage()); 142 } 143 } 144 145 } 146 | Popular Tags |