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;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021: import org.acegisecurity.AuthenticationException;
022: import org.acegisecurity.BadCredentialsException;
023: import org.acegisecurity.GrantedAuthority;
024: import org.acegisecurity.GrantedAuthorityImpl;
025:
026: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
027:
028: import org.acegisecurity.userdetails.User;
029: import org.acegisecurity.userdetails.UserDetails;
030:
031: import java.security.cert.X509Certificate;
032:
033: /**
034: * Tests {@link X509AuthenticationProvider}
035: *
036: * @author Luke Taylor
037: * @version $Id: X509AuthenticationProviderTests.java 1496 2006-05-23 13:38:33Z benalex $
038: */
039: public class X509AuthenticationProviderTests extends TestCase {
040: //~ Constructors ===================================================================================================
041:
042: public X509AuthenticationProviderTests() {
043: super ();
044: }
045:
046: public X509AuthenticationProviderTests(String arg0) {
047: super (arg0);
048: }
049:
050: //~ Methods ========================================================================================================
051:
052: public final void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: public void testAuthenticationIsNullWithUnsupportedToken() {
057: X509AuthenticationProvider provider = new X509AuthenticationProvider();
058: Authentication request = new UsernamePasswordAuthenticationToken(
059: "dummy", "dummy");
060: Authentication result = provider.authenticate(request);
061: assertNull(result);
062: }
063:
064: public void testFailsWithNullCertificate() {
065: X509AuthenticationProvider provider = new X509AuthenticationProvider();
066:
067: provider
068: .setX509AuthoritiesPopulator(new MockAuthoritiesPopulator(
069: false));
070:
071: try {
072: provider.authenticate(new X509AuthenticationToken(null));
073: fail("Should have thrown BadCredentialsException");
074: } catch (BadCredentialsException e) {
075: //ignore
076: }
077: }
078:
079: public void testNormalOperation() throws Exception {
080: X509AuthenticationProvider provider = new X509AuthenticationProvider();
081:
082: provider
083: .setX509AuthoritiesPopulator(new MockAuthoritiesPopulator(
084: false));
085: provider.afterPropertiesSet();
086:
087: Authentication result = provider.authenticate(X509TestUtils
088: .createToken());
089:
090: assertNotNull(result);
091: assertNotNull(result.getAuthorities());
092: }
093:
094: public void testPopulatorRejectionCausesFailure() throws Exception {
095: X509AuthenticationProvider provider = new X509AuthenticationProvider();
096: provider
097: .setX509AuthoritiesPopulator(new MockAuthoritiesPopulator(
098: true));
099:
100: try {
101: provider.authenticate(X509TestUtils.createToken());
102: fail("Should have thrown BadCredentialsException");
103: } catch (BadCredentialsException e) {
104: //ignore
105: }
106: }
107:
108: public void testRequiresPopulator() throws Exception {
109: X509AuthenticationProvider provider = new X509AuthenticationProvider();
110:
111: try {
112: provider.afterPropertiesSet();
113: fail("Should have thrown IllegalArgumentException");
114: } catch (IllegalArgumentException failed) {
115: //ignored
116: }
117: }
118:
119: //~ Inner Classes ==================================================================================================
120:
121: public static class MockAuthoritiesPopulator implements
122: X509AuthoritiesPopulator {
123: private boolean rejectCertificate;
124:
125: public MockAuthoritiesPopulator(boolean rejectCertificate) {
126: this .rejectCertificate = rejectCertificate;
127: }
128:
129: public UserDetails getUserDetails(
130: X509Certificate userCertificate)
131: throws AuthenticationException {
132: if (rejectCertificate) {
133: throw new BadCredentialsException("Invalid Certificate");
134: }
135:
136: return new User("user", "password", true, true, true, true,
137: new GrantedAuthority[] {
138: new GrantedAuthorityImpl("ROLE_A"),
139: new GrantedAuthorityImpl("ROLE_B") });
140: }
141: }
142: }
|