001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.x509.populator;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.BadCredentialsException;
021: import org.acegisecurity.GrantedAuthority;
022: import org.acegisecurity.GrantedAuthorityImpl;
023:
024: import org.acegisecurity.providers.x509.X509TestUtils;
025:
026: import org.acegisecurity.userdetails.User;
027: import org.acegisecurity.userdetails.UserDetails;
028: import org.acegisecurity.userdetails.UserDetailsService;
029: import org.acegisecurity.userdetails.UsernameNotFoundException;
030:
031: import org.springframework.dao.DataAccessException;
032:
033: import java.security.cert.X509Certificate;
034:
035: /**
036: * Tests for {@link DaoX509AuthoritiesPopulator}
037: *
038: * @author Luke Taylor
039: * @version $Id: DaoX509AuthoritiesPopulatorTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class DaoX509AuthoritiesPopulatorTests extends TestCase {
042: //~ Constructors ===================================================================================================
043:
044: public DaoX509AuthoritiesPopulatorTests() {
045: super ();
046: }
047:
048: public DaoX509AuthoritiesPopulatorTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: public final void setUp() throws Exception {
055: super .setUp();
056: }
057:
058: public void testDefaultCNPatternMatch() throws Exception {
059: X509Certificate cert = X509TestUtils.buildTestCertificate();
060: DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
061:
062: populator
063: .setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
064: populator.afterPropertiesSet();
065: populator.getUserDetails(cert);
066: }
067:
068: public void testEmailPatternMatch() throws Exception {
069: X509Certificate cert = X509TestUtils.buildTestCertificate();
070: DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
071:
072: populator
073: .setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
074: populator.setSubjectDNRegex("emailAddress=(.*?),");
075: populator.afterPropertiesSet();
076: populator.getUserDetails(cert);
077: }
078:
079: public void testInvalidRegexFails() throws Exception {
080: DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
081: populator
082: .setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
083: populator.setSubjectDNRegex("CN=(.*?,"); // missing closing bracket on group
084:
085: try {
086: populator.afterPropertiesSet();
087: fail("Should have thrown IllegalArgumentException");
088: } catch (IllegalArgumentException failed) {
089: // ignored
090: }
091: }
092:
093: public void testMatchOnShoeSizeFieldInDNFails() throws Exception {
094: X509Certificate cert = X509TestUtils.buildTestCertificate();
095: DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
096:
097: populator
098: .setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
099: populator.setSubjectDNRegex("shoeSize=(.*?),");
100: populator.afterPropertiesSet();
101:
102: try {
103: populator.getUserDetails(cert);
104: fail("Should have thrown BadCredentialsException.");
105: } catch (BadCredentialsException failed) {
106: // ignored
107: }
108: }
109:
110: public void testPatternWithNoGroupFails() throws Exception {
111: X509Certificate cert = X509TestUtils.buildTestCertificate();
112: DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
113:
114: populator
115: .setUserDetailsService(new MockAuthenticationDaoMatchesNameOrEmail());
116: populator.setSubjectDNRegex("CN=.*?,");
117: populator.afterPropertiesSet();
118:
119: try {
120: populator.getUserDetails(cert);
121: fail("Should have thrown IllegalArgumentException for regexp without group");
122: } catch (IllegalArgumentException e) {
123: // ignored
124: }
125: }
126:
127: public void testRequiresDao() throws Exception {
128: DaoX509AuthoritiesPopulator populator = new DaoX509AuthoritiesPopulator();
129:
130: try {
131: populator.afterPropertiesSet();
132: fail("Should have thrown IllegalArgumentException");
133: } catch (IllegalArgumentException failed) {
134: // ignored
135: }
136: }
137:
138: //~ Inner Classes ==================================================================================================
139:
140: private class MockAuthenticationDaoMatchesNameOrEmail implements
141: UserDetailsService {
142: public UserDetails loadUserByUsername(String username)
143: throws UsernameNotFoundException, DataAccessException {
144: if ("Luke Taylor".equals(username)
145: || "luke@monkeymachine".equals(username)) {
146: return new User(
147: "luke",
148: "monkey",
149: true,
150: true,
151: true,
152: true,
153: new GrantedAuthority[] { new GrantedAuthorityImpl(
154: "ROLE_ONE") });
155: } else {
156: throw new UsernameNotFoundException("Could not find: "
157: + username);
158: }
159: }
160: }
161: }
|