Created
January 27, 2011 19:03
-
-
Save bleathem/799001 to your computer and use it in GitHub Desktop.
A unit test to try binding to your ldap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.triumf.mis.ldap; | |
import java.util.Hashtable; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.naming.Context; | |
import javax.naming.NamingException; | |
import javax.naming.directory.DirContext; | |
import javax.naming.directory.InitialDirContext; | |
import junit.framework.Assert; | |
import org.junit.Test; | |
/** | |
* | |
* @author bleathem | |
*/ | |
public class LdapSearchTest { | |
@Test | |
public void testBind() { | |
//config | |
String userDn = "myusername"; | |
String userPassword = "mypassword"; | |
String host = "host.domain.tld"; | |
boolean useSsl = true; | |
String port = useSsl ? "636" : "389"; | |
// setup the environment | |
Hashtable<String, String> env = new Hashtable<String,String>(); | |
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); | |
env.put(Context.PROVIDER_URL, String.format("ldap://%s:%s", host, port)); | |
env.put("com.sun.jndi.ldap.connect.pool", "true"); | |
if (useSsl) { | |
env.put(Context.SECURITY_PROTOCOL, "ssl"); | |
} | |
env.put(Context.SECURITY_AUTHENTICATION, "simple"); | |
env.put(Context.SECURITY_PRINCIPAL, userDn); | |
env.put(Context.SECURITY_CREDENTIALS, userPassword); | |
DirContext context = null; | |
// do the ldap bind (validate username and password | |
boolean loggedIn; | |
try { | |
context = new InitialDirContext(env); | |
loggedIn = true; | |
} catch (NamingException ex) { | |
Logger.getLogger(LdapSearchTest.class.getName()).log(Level.SEVERE, null, ex); | |
loggedIn = false; | |
} | |
Assert.assertEquals(true, loggedIn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment