From 29050b29b29c53ff320e96652a1216ec8599c4ce Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Fri, 11 Mar 2005 02:08:07 +0000 Subject: [PATCH] Dao populator tests for X.509. Tests matching of regexps in the certificate Subject to extract the user name. --- .../DaoX509AuthoritiesPopulatorTests.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 core/src/test/java/org/acegisecurity/providers/x509/populator/DaoX509AuthoritiesPopulatorTests.java diff --git a/core/src/test/java/org/acegisecurity/providers/x509/populator/DaoX509AuthoritiesPopulatorTests.java b/core/src/test/java/org/acegisecurity/providers/x509/populator/DaoX509AuthoritiesPopulatorTests.java new file mode 100644 index 0000000000..4d7590d650 --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/x509/populator/DaoX509AuthoritiesPopulatorTests.java @@ -0,0 +1,68 @@ +package net.sf.acegisecurity.providers.x509.populator; + +import junit.framework.TestCase; +import net.sf.acegisecurity.providers.dao.AuthenticationDao; +import net.sf.acegisecurity.providers.dao.UsernameNotFoundException; +import net.sf.acegisecurity.providers.dao.User; +import net.sf.acegisecurity.providers.x509.X509TestUtils; +import net.sf.acegisecurity.UserDetails; +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import org.springframework.dao.DataAccessException; + +import java.security.cert.X509Certificate; + +/** + * @author Luke Taylor + */ +public class DaoX509AuthoritiesPopulatorTests extends TestCase { + //~ Constructors =========================================================== + + public DaoX509AuthoritiesPopulatorTests() { + super(); + } + + public DaoX509AuthoritiesPopulatorTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public void testDefaultCNPatternMatch() throws Exception{ + X509Certificate cert = X509TestUtils.buildTestCertificate(); + DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); + + populator.setAuthenticationDao(new MockAuthenticationDaoMatchesNameOrEmail()); + populator.afterPropertiesSet(); + populator.getUserDetails(cert); + } + + public void testEmailPatternMatch() throws Exception{ + X509Certificate cert = X509TestUtils.buildTestCertificate(); + DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator(); + + populator.setAuthenticationDao(new MockAuthenticationDaoMatchesNameOrEmail()); + populator.setSubjectDNRegex("emailAddress=(.*?),"); + populator.afterPropertiesSet(); + populator.getUserDetails(cert); + } + + //~ Inner Classes ========================================================== + private class MockAuthenticationDaoMatchesNameOrEmail implements AuthenticationDao { + + public UserDetails loadUserByUsername(String username) + throws UsernameNotFoundException, DataAccessException { + if ("Luke Taylor".equals(username) || "luke@monkeymachine".equals(username)) { + return new User("luke", "monkey", true, true, true, + new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE")}); + } else { + throw new UsernameNotFoundException("Could not find: " + + username); + } + } + } +}