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.jaas;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.context.SecurityContextHolder;
021:
022: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
023:
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Map;
027:
028: import javax.security.auth.Subject;
029: import javax.security.auth.login.LoginException;
030:
031: /**
032: * Tests SecurityContextLoginModule
033: *
034: * @author Ray Krueger
035: */
036: public class SecurityContextLoginModuleTests extends TestCase {
037: //~ Instance fields ================================================================================================
038:
039: private SecurityContextLoginModule module = null;
040: private Subject subject = new Subject(false, new HashSet(),
041: new HashSet(), new HashSet());
042: private UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
043: "principal", "credentials");
044:
045: //~ Methods ========================================================================================================
046:
047: protected void setUp() throws Exception {
048: module = new SecurityContextLoginModule();
049: module.initialize(subject, null, null, null);
050: SecurityContextHolder.clearContext();
051: }
052:
053: protected void tearDown() throws Exception {
054: SecurityContextHolder.clearContext();
055: module = null;
056: }
057:
058: public void testAbort() throws Exception {
059: assertFalse("Should return false, no auth is set", module
060: .abort());
061: SecurityContextHolder.getContext().setAuthentication(auth);
062: module.login();
063: module.commit();
064: assertTrue(module.abort());
065: }
066:
067: public void testLoginException() throws Exception {
068: try {
069: module.login();
070: fail("LoginException expected, there is no Authentication in the SecurityContext");
071: } catch (LoginException e) {
072: }
073: }
074:
075: public void testLoginSuccess() throws Exception {
076: SecurityContextHolder.getContext().setAuthentication(auth);
077: assertTrue(
078: "Login should succeed, there is an authentication set",
079: module.login());
080: assertTrue(
081: "The authentication is not null, this should return true",
082: module.commit());
083: assertTrue("Principals should contain the authentication",
084: subject.getPrincipals().contains(auth));
085: }
086:
087: public void testLogout() throws Exception {
088: SecurityContextHolder.getContext().setAuthentication(auth);
089: module.login();
090: assertTrue("Should return true as it succeeds", module.logout());
091: assertEquals("Authentication should be null", null, module
092: .getAuthentication());
093:
094: assertFalse(
095: "Principals should not contain the authentication after logout",
096: subject.getPrincipals().contains(auth));
097: }
098:
099: public void testNullAuthenticationInSecurityContext()
100: throws Exception {
101: try {
102: SecurityContextHolder.getContext().setAuthentication(null);
103: module.login();
104: fail("LoginException expected, the authentication is null in the SecurityContext");
105: } catch (Exception e) {
106: }
107: }
108:
109: public void testNullAuthenticationInSecurityContextIgnored()
110: throws Exception {
111: module = new SecurityContextLoginModule();
112:
113: Map options = new HashMap();
114: options.put("ignoreMissingAuthentication", "true");
115:
116: module.initialize(subject, null, null, options);
117: SecurityContextHolder.getContext().setAuthentication(null);
118: assertFalse("Should return false and ask to be ignored", module
119: .login());
120: }
121:
122: public void testNullLogout() throws Exception {
123: assertFalse(module.logout());
124: }
125: }
|