Update project workspace settings to Java 1.5. NB: Maven remains at 1.3 compatibility for all subprojects except "domain". It is recommended the Eclipse "Problems" view be customised to not display items containing "Type Safety:" in their description. Developers should NOT introduce 1.5+ dependencies to any projects apart from "domain".
This commit is contained in:
@@ -1,49 +1,76 @@
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.dao.ldap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/** Important note: this class merely defines certain
|
||||
* base properties needed by all LDAP unit tests.
|
||||
|
||||
/**
|
||||
* Important note: this class merely defines certain base properties needed by
|
||||
* all LDAP unit tests.
|
||||
*/
|
||||
public class BaseLdapTestCase extends TestCase {
|
||||
//~ Static fields/initializers =============================================
|
||||
|
||||
// static finalizers, they'd be nice, as LdapTestHelper
|
||||
// never seems to get the chance to cleanup after itself
|
||||
protected static LdapTestHelper ldapTestHelper = new LdapTestHelper();
|
||||
|
||||
protected static LdapTestHelper ldapTestHelper = new LdapTestHelper();
|
||||
|
||||
static {
|
||||
//InputStream in = BaseLdapTestCase.class.getResourceAsStream("net/sf/acegisecurity/providers/dao/ldap/test-data.ldif");
|
||||
|
||||
/* InputStream in = ldapTestHelper.getClass().getResourceAsStream("test-data.ldif");
|
||||
try {
|
||||
ldapTestHelper.importLDIF(in);
|
||||
} catch (Exception x) {
|
||||
x.printStackTrace();
|
||||
ldapTestHelper.shutdownServer();
|
||||
ldapTestHelper = null;
|
||||
throw new RuntimeException("Server initialization failed.");
|
||||
} */
|
||||
DirContentsInitializer.initialize( ldapTestHelper.getServerContext() );
|
||||
try {
|
||||
ldapTestHelper.importLDIF(in);
|
||||
} catch (Exception x) {
|
||||
x.printStackTrace();
|
||||
ldapTestHelper.shutdownServer();
|
||||
ldapTestHelper = null;
|
||||
throw new RuntimeException("Server initialization failed.");
|
||||
} */
|
||||
DirContentsInitializer.initialize(ldapTestHelper.getServerContext());
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
protected DirContext getClientContext() throws NamingException {
|
||||
Hashtable env = new Hashtable();
|
||||
env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=system");
|
||||
env.put(Context.INITIAL_CONTEXT_FACTORY,
|
||||
"com.sun.jndi.ldap.LdapCtxFactory");
|
||||
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
|
||||
env.put(Context.SECURITY_CREDENTIALS, "secret");
|
||||
|
||||
return new InitialDirContext(env);
|
||||
}
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @return The server context for LDAP ops. used for things like
|
||||
* addding/removing users.
|
||||
*/
|
||||
protected DirContext getServerContext() {
|
||||
return ldapTestHelper.getServerContext();
|
||||
}
|
||||
|
||||
protected DirContext getClientContext() throws NamingException {
|
||||
Hashtable env = new Hashtable();
|
||||
env.put( Context.PROVIDER_URL, "ldap://localhost:389/ou=system" );
|
||||
env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
|
||||
env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
|
||||
env.put( Context.SECURITY_CREDENTIALS, "secret" );
|
||||
return new InitialDirContext( env );
|
||||
}
|
||||
|
||||
/** @return The server context for LDAP ops. used for things like addding/removing users. */
|
||||
protected DirContext getServerContext() {
|
||||
return ldapTestHelper.getServerContext();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,41 +1,112 @@
|
||||
package net.sf.acegisecurity.providers.dao.ldap;
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import javax.naming.NamingException;
|
||||
package net.sf.acegisecurity.providers.dao.ldap;
|
||||
|
||||
import net.sf.acegisecurity.BadCredentialsException;
|
||||
import net.sf.acegisecurity.UserDetails;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
|
||||
|
||||
/**
|
||||
* DOCUMENT ME!
|
||||
*
|
||||
* @author $author$
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class LdapPasswordAuthenticationTest extends BaseLdapTestCase {
|
||||
|
||||
private LdapPasswordAuthenticationDao dao;
|
||||
|
||||
private String DEFAULT_ROLE = "DEFAULT_ROLE";
|
||||
|
||||
/** Setup the basic properties of our LdapPasswordAuthenticationDao */
|
||||
protected void setUp() {
|
||||
dao = new LdapPasswordAuthenticationDao();
|
||||
dao.setURL("ldap://localhost:389/ou=system");
|
||||
}
|
||||
|
||||
//~ Instance fields ========================================================
|
||||
|
||||
public void testSimpleUidUser() throws NamingException {
|
||||
private LdapPasswordAuthenticationDao dao;
|
||||
private String DEFAULT_ROLE = "DEFAULT_ROLE";
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public void testEmptyRoles() {
|
||||
dao.setUserContext("uid={0},ou=users,ou=system");
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", "plaintext");
|
||||
assertEquals(1, userDetails.getAuthorities().length );
|
||||
assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() );
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two",
|
||||
"plaintext2");
|
||||
fail("No roles are accessible for user; this test _should_ fail.");
|
||||
} catch (BadCredentialsException ex) {
|
||||
assertTrue("No roles are accessible for user; this test _should_ fail.",
|
||||
ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimpleCnUser() throws NamingException {
|
||||
dao.setUserContext("cn={0},ou=users,ou=system");
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two",
|
||||
"plaintext2");
|
||||
assertEquals(1, userDetails.getAuthorities().length);
|
||||
assertEquals(DEFAULT_ROLE,
|
||||
userDetails.getAuthorities()[0].getAuthority());
|
||||
} catch (BadCredentialsException ex) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testSimpleMultiUserContext() throws NamingException {
|
||||
dao.setUserContexts(new String[] {"uid={0},ou=users,ou=system", "cn={0},ou=users,ou=system"});
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user",
|
||||
"plaintext");
|
||||
assertEquals(1, userDetails.getAuthorities().length);
|
||||
assertEquals(DEFAULT_ROLE,
|
||||
userDetails.getAuthorities()[0].getAuthority());
|
||||
|
||||
UserDetails userDetails2 = dao.loadUserByUsernameAndPassword("user.two",
|
||||
"plaintext2");
|
||||
assertEquals(1, userDetails2.getAuthorities().length);
|
||||
assertEquals(DEFAULT_ROLE,
|
||||
userDetails2.getAuthorities()[0].getAuthority());
|
||||
} catch (BadCredentialsException ex) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimpleUidUser() throws NamingException {
|
||||
dao.setUserContext("uid={0},ou=users,ou=system");
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user",
|
||||
"plaintext");
|
||||
assertEquals(1, userDetails.getAuthorities().length);
|
||||
assertEquals(DEFAULT_ROLE,
|
||||
userDetails.getAuthorities()[0].getAuthority());
|
||||
} catch (BadCredentialsException ex) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimpleUidUserBadPassword() throws NamingException {
|
||||
dao.setUserContext("uid={0},ou=users,ou=system");
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", "plainlywrong");
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user",
|
||||
"plainlywrong");
|
||||
|
||||
//assertEquals(1, userDetails.getAuthorities().length );
|
||||
//assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() );
|
||||
fail();
|
||||
@@ -43,51 +114,19 @@ public class LdapPasswordAuthenticationTest extends BaseLdapTestCase {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimpleCnUser() throws NamingException {
|
||||
dao.setUserContext("cn={0},ou=users,ou=system");
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two", "plaintext2");
|
||||
assertEquals(1, userDetails.getAuthorities().length );
|
||||
assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() );
|
||||
} catch (BadCredentialsException ex) {
|
||||
fail();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the basic properties of our LdapPasswordAuthenticationDao
|
||||
*/
|
||||
protected void setUp() {
|
||||
dao = new LdapPasswordAuthenticationDao();
|
||||
dao.setURL("ldap://localhost:389/ou=system");
|
||||
}
|
||||
|
||||
public void testSimpleMultiUserContext() throws NamingException {
|
||||
dao.setUserContexts(new String[]{"uid={0},ou=users,ou=system", "cn={0},ou=users,ou=system"});
|
||||
dao.setDefaultRole(DEFAULT_ROLE);
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("one.user", "plaintext");
|
||||
assertEquals(1, userDetails.getAuthorities().length );
|
||||
assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() );
|
||||
|
||||
UserDetails userDetails2 = dao.loadUserByUsernameAndPassword("user.two", "plaintext2");
|
||||
assertEquals(1, userDetails2.getAuthorities().length );
|
||||
assertEquals(DEFAULT_ROLE, userDetails2.getAuthorities()[0].getAuthority() );
|
||||
} catch (BadCredentialsException ex) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyRoles() {
|
||||
dao.setUserContext("uid={0},ou=users,ou=system");
|
||||
try {
|
||||
UserDetails userDetails = dao.loadUserByUsernameAndPassword("user.two", "plaintext2");
|
||||
fail("No roles are accessible for user; this test _should_ fail.");
|
||||
} catch (BadCredentialsException ex) {
|
||||
assertTrue("No roles are accessible for user; this test _should_ fail.",
|
||||
ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* @todo:
|
||||
* 1. two different groups...
|
||||
* 2. two groups, limit 'roles'
|
||||
* 3. other stuff...
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user