From 7f04f99e5a4c53d8b62c96db1ce45535f44dd614 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Mon, 22 Mar 2010 22:16:31 +0000 Subject: [PATCH] Added test for new class LookupAttemptingCallback. --- .../support/LookupAttemptingCallback.java | 29 +++++++++++++++++++ .../ldap/LdapTemplateAuthenticationITest.java | 9 ++++++ ... => LdapContextSourceIntegrationTest.java} | 4 +-- ...textSourceMultiServerIntegrationTest.java} | 4 +-- 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/org/springframework/ldap/core/support/LookupAttemptingCallback.java rename test/integration-tests/src/test/java/org/springframework/ldap/core/support/{LdapContextSourcelITest.java => LdapContextSourceIntegrationTest.java} (93%) rename test/integration-tests/src/test/java/org/springframework/ldap/core/support/{LdapContextSourcelMultiServerITest.java => LdapContextSourceMultiServerIntegrationTest.java} (87%) diff --git a/core/src/main/java/org/springframework/ldap/core/support/LookupAttemptingCallback.java b/core/src/main/java/org/springframework/ldap/core/support/LookupAttemptingCallback.java new file mode 100644 index 00000000..abf25b00 --- /dev/null +++ b/core/src/main/java/org/springframework/ldap/core/support/LookupAttemptingCallback.java @@ -0,0 +1,29 @@ +package org.springframework.ldap.core.support; + +import javax.naming.NamingException; +import javax.naming.directory.DirContext; + +import org.springframework.ldap.core.AuthenticatedLdapEntryContextCallback; +import org.springframework.ldap.core.LdapEntryIdentification; +import org.springframework.ldap.support.LdapUtils; + +/** + * Attempts to perform an LDAP operation in the authenticated context, because + * Active Directory might allow bind with incorrect password (specifically empty + * password), and later refuse operations. We want to fail fast when + * authenticating. + * + * @author Hugo Josefson + * @since 1.3.1 + */ +public class LookupAttemptingCallback implements AuthenticatedLdapEntryContextCallback { + public void executeWithContext(DirContext ctx, LdapEntryIdentification ldapEntryIdentification) { + try { + ctx.lookup(ldapEntryIdentification.getRelativeDn()); + } + catch (NamingException e) { + // rethrow, because we aren't allowed to throw checked exceptions. + throw LdapUtils.convertLdapException(e); + } + } +} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAuthenticationITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAuthenticationITest.java index d190e74e..78ba3dfe 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAuthenticationITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateAuthenticationITest.java @@ -31,6 +31,7 @@ import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.LdapEntryIdentification; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.CollectingAuthenticationErrorCallback; +import org.springframework.ldap.core.support.LookupAttemptingCallback; import org.springframework.ldap.filter.AndFilter; import org.springframework.ldap.filter.EqualsFilter; import org.springframework.ldap.filter.WhitespaceWildcardsFilter; @@ -106,4 +107,12 @@ public class LdapTemplateAuthenticationITest extends AbstractLdapTemplateIntegra filter.and(new EqualsFilter("objectclass", "person")).and(new WhitespaceWildcardsFilter("uid", "some.person")); assertFalse(tested.authenticate("", filter.toString(), "password")); } + + @Test + public void testLookupAttemptingCallback() { + AndFilter filter = new AndFilter(); + filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3")); + LookupAttemptingCallback callback = new LookupAttemptingCallback(); + assertTrue(tested.authenticate("", filter.encode(), "password", callback)); + } } diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourceIntegrationTest.java similarity index 93% rename from test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java rename to test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourceIntegrationTest.java index 94814ca5..6ef7eda2 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourceIntegrationTest.java @@ -40,12 +40,12 @@ import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; /** - * Integration tests for ContextSourceImpl. + * Integration tests for LdapContextSource. * * @author Mattias Hellborg Arthursson */ @ContextConfiguration(locations = { "/conf/ldapTemplateTestContext.xml" }) -public class LdapContextSourcelITest extends AbstractLdapTemplateIntegrationTest { +public class LdapContextSourceIntegrationTest extends AbstractLdapTemplateIntegrationTest { @Autowired private ContextSource tested; diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourceMultiServerIntegrationTest.java similarity index 87% rename from test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java rename to test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourceMultiServerIntegrationTest.java index d900d5ef..d4996b4b 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelMultiServerITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourceMultiServerIntegrationTest.java @@ -25,12 +25,12 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; /** - * Integration tests for ContextSourceImpl. + * Advanced integration tests for LdapContextSource. * * @author Mattias Hellborg Arthursson */ @ContextConfiguration(locations = { "/conf/ldapContextSourceTestContext.xml" }) -public class LdapContextSourcelMultiServerITest extends AbstractJUnit4SpringContextTests { +public class LdapContextSourceMultiServerIntegrationTest extends AbstractJUnit4SpringContextTests { @Autowired private LdapContextSource tested;