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
test {
exclude('**/OpenLDAPIntegrationTestSuite.class')
maxParallelForks = 1
jvmArgs "-DapacheDSWorkDir=${buildDir}/apacheDSWork"
}
apacheds_libs = [
"org.apache.directory.server:apacheds-core:$apacheDsVersion",
"org.apache.directory.server:apacheds-core-entry:$apacheDsVersion",
@ -15,6 +9,15 @@ apacheds_libs = [
'org.apache.directory.shared:shared-ldap:0.9.15'
]
configurations {
integrationTestCompile {
extendsFrom testCompile
}
integrationTestRuntime {
extendsFrom integrationTestCompile, testRuntime
}
}
dependencies {
compile project(':spring-security-core'),
"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;
import javax.naming.Binding;
import javax.naming.ContextNotEmptyException;
import javax.naming.Name;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
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.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.security.ldap.authentication.BindAuthenticatorTests;
import org.springframework.security.ldap.authentication.PasswordComparisonAuthenticatorTests;
import org.springframework.security.ldap.search.FilterBasedLdapUserSearchTests;
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
*/
public abstract class AbstractLdapIntegrationTests {
// private static InMemoryXmlApplicationContext appContext;
@RunWith(Suite.class)
@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 DefaultSpringSecurityContextSource contextSource;
protected AbstractLdapIntegrationTests() {
}
@BeforeClass
public static void startServer() 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();
server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
server.setPort(53389);
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
public final void reloadServerDataIfDirty() throws Exception {
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 {
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.SearchResult;
import org.junit.Test;
import org.junit.*;
import org.springframework.ldap.UncategorizedLdapException;
import org.springframework.ldap.core.ContextExecutor;
@ -39,9 +39,8 @@ public class SpringSecurityLdapTemplateTests extends AbstractLdapIntegrationTest
//~ Methods ========================================================================================================
public void onSetUp() throws Exception {
super.onSetUp();
@Before
public void setUp() throws Exception {
template = new SpringSecurityLdapTemplate(getContextSource());
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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