diff --git a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/BaseLdapTestCase.java b/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/BaseLdapTestCase.java deleted file mode 100644 index c95573afd5..0000000000 --- a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/BaseLdapTestCase.java +++ /dev/null @@ -1,76 +0,0 @@ -/* 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 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; - - -/** - * 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(); - - 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()); - } - - //~ 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(); - } -} diff --git a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/DirContentsInitializer.java b/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/DirContentsInitializer.java deleted file mode 100644 index 51adca5577..0000000000 --- a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/DirContentsInitializer.java +++ /dev/null @@ -1,113 +0,0 @@ -package net.sf.acegisecurity.providers.dao.ldap; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; - -/** - * Since I can't get resource loading to work inside of eclipse; - * for now I am writing this stuff as java. - * - * @author robert.sanders - */ -public class DirContentsInitializer { - - private DirContext serverContext; - - private DirContentsInitializer(DirContext serverContext) { - super(); - this.serverContext = serverContext; - } - - - public static void initialize(DirContext serverContext) { - DirContentsInitializer dci = new DirContentsInitializer(serverContext); - dci.doInit(); - dci = null; - } - - /** calls individual init methods. */ - private void doInit() { - try { - initSimpleUidUser(); - initSimpleCnUser(); - - initOthersGroup(); - initOthersUsers(); - } catch (NamingException e) { - // TODO Auto-generated catch block - e.printStackTrace(System.err); - } - } - - private void initSimpleUidUser() throws NamingException { - String name = "uid=one.user,ou=users"; - Attributes attrs = new BasicAttributes(); - attrs.put("dn", name + ",ou=system"); - attrs.put("cn", "User One"); - attrs.put("sn", "One"); - attrs.put("givenName", "User"); - attrs.put("uid", "user.one"); - attrs.put("mail", "one.user@hotmail.com"); - attrs.put("userPassword", "plaintext"); - attrs.put("objectClass", "inetOrgPerson"); - attrs.put("objectClass", "top"); - - serverContext.createSubcontext(name, attrs); - } - - private void initSimpleCnUser() throws NamingException { - String name = "cn=user.two,ou=users"; - Attributes attrs = new BasicAttributes(); - attrs.put("dn", name + ",ou=system"); - attrs.put("cn", "Two User"); - attrs.put("givenName", "Two"); - attrs.put("sn", "User"); - attrs.put("uid", "user.two"); - attrs.put("mail", "user.two@hotmail.com"); - attrs.put("userPassword", "plaintext2"); - attrs.put("objectClass", "inetOrgPerson"); - attrs.put("objectClass", "top"); - - serverContext.createSubcontext(name, attrs); - } - - private void initOthersGroup() throws NamingException { - String otherUserOU = "ou=others"; - Attributes attrs = new BasicAttributes(); - attrs.put("dn", otherUserOU + ",ou=system"); - attrs.put("ou", "others"); - attrs.put("objectClass", "top"); - attrs.put("objectClass", "organizationalUnit"); - serverContext.createSubcontext(otherUserOU, attrs); - } - - private void initOthersUsers() throws NamingException { - String name1 = "uid=other.one,ou=others"; - Attributes attrs1 = new BasicAttributes(); - attrs1.put("dn", name1 + ",ou=system"); - attrs1.put("cn", "Other One"); - attrs1.put("givenName", "Other"); - attrs1.put("sn", "One"); - attrs1.put("uid", "other.one"); - attrs1.put("mail", "other.one@hotmail.com"); - attrs1.put("userPassword", "otherone"); - attrs1.put("objectClass", "inetOrgPerson"); - attrs1.put("objectClass", "top"); - serverContext.createSubcontext(name1, attrs1); - - String name2 = "uid=other.two,ou=others"; - Attributes attrs2 = new BasicAttributes(); - attrs2.put("dn", name2 + ",ou=system"); - attrs2.put("cn", "Other Two"); - attrs2.put("givenName", "Other"); - attrs2.put("sn", "Two"); - attrs2.put("uid", "other.two"); - attrs2.put("mail", "other.two@hotmail.com"); - attrs2.put("userPassword", "othertwo"); - attrs2.put("objectClass", "inetOrgPerson"); - attrs2.put("objectClass", "top"); - serverContext.createSubcontext(name2, attrs2); - } -} diff --git a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapPasswordAuthenticationTest.java b/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapPasswordAuthenticationTest.java deleted file mode 100644 index dfbdc0cf6f..0000000000 --- a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapPasswordAuthenticationTest.java +++ /dev/null @@ -1,132 +0,0 @@ -/* 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 net.sf.acegisecurity.BadCredentialsException; -import net.sf.acegisecurity.UserDetails; - -import javax.naming.NamingException; - - -/** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ - */ -public class LdapPasswordAuthenticationTest extends BaseLdapTestCase { - //~ Instance fields ======================================================== - - private LdapPasswordAuthenticationDao dao; - private String DEFAULT_ROLE = "DEFAULT_ROLE"; - - //~ Methods ================================================================ - - 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)); - } - } - - 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"); - - //assertEquals(1, userDetails.getAuthorities().length ); - //assertEquals(DEFAULT_ROLE, userDetails.getAuthorities()[0].getAuthority() ); - fail(); - } catch (BadCredentialsException ex) { - assertTrue(true); - } - } - - /** - * Setup the basic properties of our LdapPasswordAuthenticationDao - */ - protected void setUp() { - dao = new LdapPasswordAuthenticationDao(); - dao.setURL("ldap://localhost:389/ou=system"); - } - - /* - * @todo: - * 1. two different groups... - * 2. two groups, limit 'roles' - * 3. other stuff... - */ -} diff --git a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapTestHelper.java b/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapTestHelper.java deleted file mode 100644 index 6d129de7d6..0000000000 --- a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapTestHelper.java +++ /dev/null @@ -1,193 +0,0 @@ -package net.sf.acegisecurity.providers.dao.ldap; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Hashtable; - -import javax.naming.Context; -import javax.naming.Name; -import javax.naming.NamingException; -import javax.naming.directory.Attributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; - -import org.apache.ldap.common.ldif.LdifIterator; -import org.apache.ldap.common.ldif.LdifParser; -import org.apache.ldap.common.ldif.LdifParserImpl; -import org.apache.ldap.common.message.LockableAttributesImpl; -import org.apache.ldap.common.name.LdapName; -import org.apache.ldap.server.jndi.EnvKeys; - -/** - * LdapTestHelper - used as static field in BaseLdapTestCase; - * responsible for global state during JUnit tests - since - * JUnit reinstantiates the test class for every method. - * - */ -public class LdapTestHelper { - - private File tempDirectory; - - private DirContext serverContext; - - /** - * - */ - public LdapTestHelper() { - // create temporary directory for directory-server to store files in - tempDirectory = initTempFiles(); - // start the apache directory server - startServer(); - } - - /** - * Creates if needed a temporary directory to store the apache directory - * server files. Since I can't get the class to shutdown cleanly, - * it also ensures a clean start by removing any files in the temp. directory. - * - * @return The directory that should be used to store temporary files in. - */ - protected File initTempFiles() { - String tmpDir = System.getProperty("java.io.tmpdir"); - File dir = new File(tmpDir); - File tmp = new File(dir, "apacheds_tmp"); - if (tmp.exists()) { - cleanupTempFiles(tmp); - } else { - tmp.mkdir(); - } - System.out.println("Directory temp files at: " + tmp.getAbsolutePath()); - return tmp; - } - - /** Attempts to open the file and import the contents as LDIF entries - * into the test directory. - * - * @param file The LDIF file to import - * @throws IOException - * @throws NamingException - */ - public void importLDIF(File file) throws IOException, NamingException { - FileInputStream fis = new FileInputStream(file); - importLDIF(fis); - } - - /** Attempts to read the provided InputStream for LDIF entries - * and adds those entries to the test directory server. - * - * @param in InputStream of LDIF data. - * @throws NamingException - * @throws IOException - */ - public void importLDIF(InputStream in) throws NamingException, IOException { - DirContext ctx = new InitialDirContext( getServerEnvironment() ); - try { - LdifParser parser = new LdifParserImpl(); - LdifIterator iterator = new LdifIterator( in ); - while ( iterator.hasNext() ) { - Attributes attributes = new LockableAttributesImpl(); - String ldif = ( String ) iterator.next(); - parser.parse( attributes, ldif ); - Name dn = new LdapName( ( String ) attributes.remove( "dn" ).get() ); - dn.remove( 0 ); - ctx.createSubcontext( dn, attributes ); - } - } finally { - ctx.close(); - } - } - - /** starts the apache directory server. */ - protected void startServer() { - try { - serverContext = new InitialDirContext( getServerEnvironment() ); - } catch (NamingException nx) { - nx.printStackTrace( System.err ); - } - } - - /** stops the apache directory server, and attempts to remove - * the data files that the server creates. - */ - protected void shutdownServer() { - // close our internal instance of the server-context - try { - serverContext.close(); - } catch (NamingException e) { - e.printStackTrace( System.err ); - } - serverContext = null; - - // signal the server that its time to say goodbye - Hashtable env = getServerEnvironment(); - env.put(EnvKeys.SHUTDOWN, "true"); - try { - new InitialDirContext( env ); - } catch (NamingException e) { - e.printStackTrace( System.err ); - } - } - - /** Utility method to remove any files in the temporary directory - * that we use to store the directory server's data files. - * - * @param tempDir The temporary directory. - */ - protected void cleanupTempFiles(File tempDir) { - if ((null != tempDir) && (tempDir.exists())) { - File[] files = tempDir.listFiles(); - for (int i = 0; i < files.length; i++) { - if (!files[i].delete()) { - System.err.println("Error: unable to cleanup Apache Directory Server file: " + files[i]); - } - } - } - } - - /** - * This isn't working, probably because I am referencing the class - * as a static field, but maybe someone can figure out a way to - * implement this correctly. - */ - public void finalize() throws Throwable { - System.out.println("Entering LdapTestHelper.finalize()"); - shutdownServer(); - cleanupTempFiles(tempDirectory); - tempDirectory.delete(); - super.finalize(); - System.out.println("Leaving LdapTestHelper.finalize()"); - } - - /** - * @return The directory that the directory server will use to store its data files. - */ - public File getTempDirectory() { - return tempDirectory; - } - - /** - * @return The directory that the directory server will use to store its data files. - */ - public String getTempDirectoryPath() { - return tempDirectory.getAbsolutePath(); - } - - /** Create and return a Hashtable with standard JNDI settings for our tests. */ - protected Hashtable getServerEnvironment() { - Hashtable env = new Hashtable(); - env.put( Context.PROVIDER_URL, "ou=system" ); - env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" ); - env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" ); - env.put( Context.SECURITY_CREDENTIALS, "secret" ); - env.put( EnvKeys.WKDIR, tempDirectory.getAbsolutePath() ); - return env; - } - - /** Get our reference to the server-mode context. */ - public DirContext getServerContext() { - return serverContext; - } - -} diff --git a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/TestLdapPasswordAuthenticationDao.java b/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/TestLdapPasswordAuthenticationDao.java deleted file mode 100644 index 774b129b22..0000000000 --- a/sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/TestLdapPasswordAuthenticationDao.java +++ /dev/null @@ -1,189 +0,0 @@ -/* Copyright 2004 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 junit.framework.TestCase; - -import net.sf.acegisecurity.BadCredentialsException; -import net.sf.acegisecurity.GrantedAuthorityImpl; -import net.sf.acegisecurity.UserDetails; - -import org.springframework.dao.DataAccessException; - - -/** - * DOCUMENT ME! - * - * @author Karel Miarka - */ -public class TestLdapPasswordAuthenticationDao extends TestCase { - //~ Static fields/initializers ============================================= - - static String PORT = "389"; - static String HOSTNAME = "ntserver"; - static String HOST_IP = "192.168.1.1"; - static String ROOT_CONTEXT = "DC=issa,DC=cz"; - static String USER_CONTEXT = "CN=Users"; - - // objectClass is a mandatory attribute in AD with list of classes - // so it is suitable for testing - static String ROLES_ATTRIBUTE = "objectClass"; - static String USERNAME = "Karel Miarka"; - static String PASSWORD = "password"; - - //~ Instance fields ======================================================== - - LdapPasswordAuthenticationDao dao; - - //~ Methods ================================================================ - - public void testAuthenticationEmptyPassword() { - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, ""); - fail(); - } catch (BadCredentialsException ex) { - assertEquals("Empty password", ex.getMessage()); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidHost() { - dao.setURL("ldap://xxx/"); - - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, - PASSWORD); - fail(); - } catch (DataAccessException ex) { - assertTrue(true); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidPassword() { - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, "xxx"); - fail(); - } catch (BadCredentialsException ex) { - assertTrue(ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE)); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidPort() { - dao.setURL("ldap://" + HOSTNAME + ":123"); - - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, - PASSWORD); - fail(); - } catch (DataAccessException ex) { - assertTrue(true); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidRolesAttribute() { -// dao.setRolesAttribute("xxx"); - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, PASSWORD); - fail(); - } catch (BadCredentialsException ex) { - assertEquals("The user has no granted authorities or the rolesAttribute is invalid", - ex.getMessage()); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidRootContext() { - dao.setRootContext("DN=xxx"); - - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, - PASSWORD); - fail(); - } catch (BadCredentialsException ex) { - assertTrue(ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE)); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidUserContext() { - dao.setUserContext("CN=xxx"); - - try { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, - PASSWORD); - fail(); - } catch (BadCredentialsException ex) { - assertTrue(ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE)); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationInvalidUsername() { - try { - UserDetails user = dao.loadUserByUsernameAndPassword("xxx", PASSWORD); - fail(); - } catch (BadCredentialsException ex) { - assertTrue(ex.getMessage().startsWith(LdapPasswordAuthenticationDao.BAD_CREDENTIALS_EXCEPTION_MESSAGE)); - } catch (Exception ex) { - fail(); - } - } - - public void testAuthenticationValid() { - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, PASSWORD); - assertEquals(USERNAME, user.getUsername()); - assertEquals(PASSWORD, user.getPassword()); - assertEquals(new GrantedAuthorityImpl("ROLE_TOP"), - user.getAuthorities()[0]); - assertEquals(new GrantedAuthorityImpl("ROLE_USER"), - user.getAuthorities()[3]); - } - - public void testAuthenticationValidWithIpHost() { - //dao.setHost(HOST_IP); - dao.setURL("ldap://" + HOST_IP + ":" + PORT); - - UserDetails user = dao.loadUserByUsernameAndPassword(USERNAME, PASSWORD); - assertEquals(USERNAME, user.getUsername()); - assertEquals(PASSWORD, user.getPassword()); - assertEquals(new GrantedAuthorityImpl("ROLE_TOP"), - user.getAuthorities()[0]); - assertEquals(new GrantedAuthorityImpl("ROLE_USER"), - user.getAuthorities()[3]); - } - - protected void setUp() throws Exception { - super.setUp(); - dao = new LdapPasswordAuthenticationDao(); - //dao.setHost(HOSTNAME); // ldap://lojza:389/DC=elcom,DC=cz - //dao.setPort(389); - dao.setURL("ldap://" + HOSTNAME + ":" + PORT); - dao.setRootContext(ROOT_CONTEXT); //Depending on server this can be confusing... - dao.setUserContext(USER_CONTEXT); - - // dao.setRolesAttribute(ROLES_ATTRIBUTE); - } -}