SEC-1677: Split out integration tests from LDAP test code.

This commit is contained in:
Luke Taylor 2011-02-11 17:55:16 +00:00
parent 9f8a47f73e
commit a225dc3776
13 changed files with 129 additions and 92 deletions

View File

@ -1,11 +1,5 @@
// Ldap build file // Ldap build file
test {
exclude('**/OpenLDAPIntegrationTestSuite.class')
maxParallelForks = 1
jvmArgs "-DapacheDSWorkDir=${buildDir}/apacheDSWork"
}
apacheds_libs = [ apacheds_libs = [
"org.apache.directory.server:apacheds-core:$apacheDsVersion", "org.apache.directory.server:apacheds-core:$apacheDsVersion",
"org.apache.directory.server:apacheds-core-entry:$apacheDsVersion", "org.apache.directory.server:apacheds-core-entry:$apacheDsVersion",
@ -15,6 +9,15 @@ apacheds_libs = [
'org.apache.directory.shared:shared-ldap:0.9.15' 'org.apache.directory.shared:shared-ldap:0.9.15'
] ]
configurations {
integrationTestCompile {
extendsFrom testCompile
}
integrationTestRuntime {
extendsFrom integrationTestCompile, testRuntime
}
}
dependencies { dependencies {
compile project(':spring-security-core'), compile project(':spring-security-core'),
"org.springframework:spring-beans:$springVersion", "org.springframework:spring-beans:$springVersion",
@ -35,3 +38,27 @@ dependencies {
} }
} }
sourceSets {
integrationTest {
java.srcDir file('src/integration-test/java')
resources.srcDir file('src/integration-test/resources')
compileClasspath = sourceSets.main.classes + sourceSets.test.classes + configurations.integrationTestCompile
runtimeClasspath = classes + compileClasspath + configurations.integrationTestRuntime
}
}
task integrationTest(type: Test, dependsOn: jar) {
testClassesDir = sourceSets.integrationTest.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
include('**/ApacheDSServerIntegrationTests.class')
// exclude('**/OpenLDAPIntegrationTestSuite.class')
maxParallelForks = 1
systemProperties['apacheDSWorkDir'] = "${buildDir}/apacheDSWork"
}
task(ldapServer, dependsOn: 'integrationTestClasses', type: JavaExec) {
classpath = sourceSets.integrationTest.runtimeClasspath
main = 'org.springframework.security.ldap.ApacheDSServerIntegrationTests'
systemProperties['apacheDSWorkDir'] = "${buildDir}/apacheDSWork"
}

View File

@ -0,0 +1,40 @@
/* Copyright 2004, 2005, 2006 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 org.springframework.security.ldap;
import org.junit.*;
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
/**
* @author Luke Taylor
*/
public abstract class AbstractLdapIntegrationTests {
private static DefaultSpringSecurityContextSource contextSource;
@BeforeClass
public static void createContextSource() throws Exception {
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:53389/dc=springframework,dc=org");
// OpenLDAP configuration
// contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:22389/dc=springsource,dc=com");
// contextSource.setUserDn("cn=admin,dc=springsource,dc=com");
// contextSource.setPassword("password");
contextSource.afterPropertiesSet();
}
public BaseLdapPathContextSource getContextSource() {
return contextSource;
}
}

View File

@ -1,59 +1,40 @@
/* Copyright 2004, 2005, 2006 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 org.springframework.security.ldap; package org.springframework.security.ldap;
import javax.naming.Binding; import org.junit.*;
import javax.naming.ContextNotEmptyException; import org.junit.runner.RunWith;
import javax.naming.Name; import org.junit.runners.Suite;
import javax.naming.NameNotFoundException; import org.springframework.security.ldap.authentication.BindAuthenticatorTests;
import javax.naming.NamingEnumeration; import org.springframework.security.ldap.authentication.PasswordComparisonAuthenticatorTests;
import javax.naming.NamingException; import org.springframework.security.ldap.search.FilterBasedLdapUserSearchTests;
import javax.naming.directory.DirContext;
import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.security.ldap.server.ApacheDSContainer; import org.springframework.security.ldap.server.ApacheDSContainer;
import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulatorTests;
import org.springframework.security.ldap.userdetails.LdapUserDetailsManagerTests;
/** /**
* Based on class borrowed from Spring Ldap project.
*
* @author Luke Taylor * @author Luke Taylor
*/ */
public abstract class AbstractLdapIntegrationTests { @RunWith(Suite.class)
// private static InMemoryXmlApplicationContext appContext; @Suite.SuiteClasses( {
BindAuthenticatorTests.class,
PasswordComparisonAuthenticatorTests.class,
FilterBasedLdapUserSearchTests.class,
DefaultLdapAuthoritiesPopulatorTests.class,
LdapUserDetailsManagerTests.class,
DefaultSpringSecurityContextSourceTests.class,
SpringSecurityLdapTemplateTests.class
}
)
public final class ApacheDSServerIntegrationTests {
private static ApacheDSContainer server; private static ApacheDSContainer server;
private static DefaultSpringSecurityContextSource contextSource;
protected AbstractLdapIntegrationTests() {
}
@BeforeClass @BeforeClass
public static void startServer() throws Exception { public static void startServer() throws Exception {
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:53389/dc=springframework,dc=org");
// OpenLDAP configuration // OpenLDAP configuration
// contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:22389/dc=springsource,dc=com"); // contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:22389/dc=springsource,dc=com");
// contextSource.setUserDn("cn=admin,dc=springsource,dc=com"); // contextSource.setUserDn("cn=admin,dc=springsource,dc=com");
// contextSource.setPassword("password"); // contextSource.setPassword("password");
contextSource.afterPropertiesSet();
server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
server.setPort(53389);
server.afterPropertiesSet(); server.afterPropertiesSet();
} }
@ -64,11 +45,15 @@ public abstract class AbstractLdapIntegrationTests {
} }
} }
@Before /**
public void onSetUp() throws Exception { * Main class to allow server to be started from gradle script
*/
public static void main(String[] args) throws Exception {
ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
server.afterPropertiesSet();
} }
/*
@After @After
public final void reloadServerDataIfDirty() throws Exception { public final void reloadServerDataIfDirty() throws Exception {
ClassPathResource ldifs = new ClassPathResource("test-server.ldif"); ClassPathResource ldifs = new ClassPathResource("test-server.ldif");
@ -91,11 +76,6 @@ public abstract class AbstractLdapIntegrationTests {
} }
} }
public BaseLdapPathContextSource getContextSource() {
return contextSource;
}
private void clearSubContexts(DirContext ctx, Name name) throws NamingException { private void clearSubContexts(DirContext ctx, Name name) throws NamingException {
NamingEnumeration<Binding> enumeration = null; NamingEnumeration<Binding> enumeration = null;
@ -124,4 +104,5 @@ public abstract class AbstractLdapIntegrationTests {
} }
} }
} }
*/
} }

View File

@ -25,7 +25,7 @@ import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls; import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult; import javax.naming.directory.SearchResult;
import org.junit.Test; import org.junit.*;
import org.springframework.ldap.UncategorizedLdapException; import org.springframework.ldap.UncategorizedLdapException;
import org.springframework.ldap.core.ContextExecutor; import org.springframework.ldap.core.ContextExecutor;
@ -39,9 +39,8 @@ public class SpringSecurityLdapTemplateTests extends AbstractLdapIntegrationTest
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
public void onSetUp() throws Exception { @Before
super.onSetUp(); public void setUp() throws Exception {
template = new SpringSecurityLdapTemplate(getContextSource()); template = new SpringSecurityLdapTemplate(getContextSource());
} }

View File

@ -40,7 +40,8 @@ public class BindAuthenticatorTests extends AbstractLdapIntegrationTests {
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
public void onSetUp() { @Before
public void setUp() {
authenticator = new BindAuthenticator(getContextSource()); authenticator = new BindAuthenticator(getContextSource());
authenticator.setMessageSource(new SpringSecurityMessageSource()); authenticator.setMessageSource(new SpringSecurityMessageSource());
bob = new UsernamePasswordAuthenticationToken("bob", "bobspassword"); bob = new UsernamePasswordAuthenticationToken("bob", "bobspassword");

View File

@ -16,6 +16,7 @@
package org.springframework.security.ldap.authentication; package org.springframework.security.ldap.authentication;
import org.junit.*;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder; import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
@ -29,7 +30,6 @@ import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.DistinguishedName;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test;
/** /**
* Tests for {@link PasswordComparisonAuthenticator}. * Tests for {@link PasswordComparisonAuthenticator}.
@ -45,8 +45,8 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapIntegratio
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
public void onSetUp() throws Exception { @Before
super.onSetUp(); public void setUp() throws Exception {
authenticator = new PasswordComparisonAuthenticator(getContextSource()); authenticator = new PasswordComparisonAuthenticator(getContextSource());
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder()); authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"}); authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});

View File

@ -15,15 +15,14 @@
package org.springframework.security.ldap.search; package org.springframework.security.ldap.search;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import static org.junit.Assert.*;
import org.springframework.security.ldap.AbstractLdapIntegrationTests;
import org.junit.*;
import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.support.BaseLdapPathContextSource; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.ldap.AbstractLdapIntegrationTests;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/** /**
* Tests for FilterBasedLdapUserSearch. * Tests for FilterBasedLdapUserSearch.
@ -31,20 +30,10 @@ import org.junit.Test;
* @author Luke Taylor * @author Luke Taylor
*/ */
public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests { public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests {
//~ Instance fields ================================================================================================
private BaseLdapPathContextSource dirCtxFactory;
//~ Methods ========================================================================================================
public void onSetUp() throws Exception {
super.onSetUp();
dirCtxFactory = getContextSource();
}
@Test @Test
public void basicSearchSucceeds() { public void basicSearchSucceeds() {
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory); FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", getContextSource());
locator.setSearchSubtree(false); locator.setSearchSubtree(false);
locator.setSearchTimeLimit(0); locator.setSearchTimeLimit(0);
locator.setDerefLinkFlag(false); locator.setDerefLinkFlag(false);
@ -57,7 +46,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests
@Test @Test
public void searchForNameWithCommaSucceeds() { public void searchForNameWithCommaSucceeds() {
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory); FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", getContextSource());
locator.setSearchSubtree(false); locator.setSearchSubtree(false);
DirContextOperations jerry = locator.searchForUser("jerry"); DirContextOperations jerry = locator.searchForUser("jerry");
@ -70,7 +59,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests
@Test @Test
public void extraFilterPartToExcludeBob() throws Exception { public void extraFilterPartToExcludeBob() throws Exception {
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people",
"(&(cn=*)(!(|(uid={0})(uid=rod)(uid=jerry)(uid=slashguy))))", dirCtxFactory); "(&(cn=*)(!(|(uid={0})(uid=rod)(uid=jerry)(uid=slashguy))))", getContextSource());
// Search for bob, get back ben... // Search for bob, get back ben...
DirContextOperations ben = locator.searchForUser("bob"); DirContextOperations ben = locator.searchForUser("bob");
@ -79,20 +68,20 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests
@Test(expected=IncorrectResultSizeDataAccessException.class) @Test(expected=IncorrectResultSizeDataAccessException.class)
public void searchFailsOnMultipleMatches() { public void searchFailsOnMultipleMatches() {
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(cn=*)", dirCtxFactory); FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(cn=*)", getContextSource());
locator.searchForUser("Ignored"); locator.searchForUser("Ignored");
} }
@Test(expected=UsernameNotFoundException.class) @Test(expected=UsernameNotFoundException.class)
public void searchForInvalidUserFails() { public void searchForInvalidUserFails() {
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory); FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", getContextSource());
locator.searchForUser("Joe"); locator.searchForUser("Joe");
} }
@Test @Test
public void subTreeSearchSucceeds() { public void subTreeSearchSucceeds() {
// Don't set the searchBase, so search from the root. // Don't set the searchBase, so search from the root.
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("", "(cn={0})", dirCtxFactory); FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("", "(cn={0})", getContextSource());
locator.setSearchSubtree(true); locator.setSearchSubtree(true);
DirContextOperations ben = locator.searchForUser("Ben Alex"); DirContextOperations ben = locator.searchForUser("Ben Alex");
@ -103,7 +92,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests
@Test @Test
public void searchWithDifferentSearchBaseIsSuccessful() throws Exception { public void searchWithDifferentSearchBaseIsSuccessful() throws Exception {
FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=otherpeople", "(cn={0})", dirCtxFactory); FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=otherpeople", "(cn={0})", getContextSource());
DirContextOperations joe = locator.searchForUser("Joe Smeth"); DirContextOperations joe = locator.searchForUser("Joe Smeth");
assertEquals("Joe Smeth", joe.getStringAttribute("cn")); assertEquals("Joe Smeth", joe.getStringAttribute("cn"));
} }

View File

@ -38,9 +38,8 @@ public class DefaultLdapAuthoritiesPopulatorTests extends AbstractLdapIntegratio
private DefaultLdapAuthoritiesPopulator populator; private DefaultLdapAuthoritiesPopulator populator;
//~ Methods ======================================================================================================== //~ Methods ========================================================================================================
public void onSetUp() throws Exception { @Before
super.onSetUp(); public void setUp() throws Exception {
populator = new DefaultLdapAuthoritiesPopulator(getContextSource(), "ou=groups"); populator = new DefaultLdapAuthoritiesPopulator(getContextSource(), "ou=groups");
populator.setIgnorePartialResultException(false); populator.setIgnorePartialResultException(false);
} }

View File

@ -21,8 +21,7 @@ import static org.junit.Assert.fail;
import java.util.List; import java.util.List;
import org.junit.After; import org.junit.*;
import org.junit.Test;
import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -47,8 +46,8 @@ public class LdapUserDetailsManagerTests extends AbstractLdapIntegrationTests {
private LdapUserDetailsManager mgr; private LdapUserDetailsManager mgr;
private SpringSecurityLdapTemplate template; private SpringSecurityLdapTemplate template;
public void onSetUp() throws Exception { @Before
super.onSetUp(); public void setUp() throws Exception {
mgr = new LdapUserDetailsManager(getContextSource()); mgr = new LdapUserDetailsManager(getContextSource());
template = new SpringSecurityLdapTemplate(getContextSource()); template = new SpringSecurityLdapTemplate(getContextSource());
DirContextAdapter ctx = new DirContextAdapter(); DirContextAdapter ctx = new DirContextAdapter();

View File

@ -7,6 +7,8 @@
<logger name="org.springframework.security" level="${sec.log.level}:-WARN"/> <logger name="org.springframework.security" level="${sec.log.level}:-WARN"/>
<logger name="org.apache.directory" level="ERROR"/> <logger name="org.apache.directory" level="ERROR"/>
<logger name="JdbmTable" level="INFO"/>
<logger name="JdbmIndex" level="INFO"/>
<logger name="org.apache.mina" level="WARN"/> <logger name="org.apache.mina" level="WARN"/>
<root level="${root.level}:-WARN"> <root level="${root.level}:-WARN">