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.runas;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021: import org.acegisecurity.ConfigAttributeDefinition;
022: import org.acegisecurity.GrantedAuthority;
023: import org.acegisecurity.GrantedAuthorityImpl;
024: import org.acegisecurity.RunAsManager;
025: import org.acegisecurity.SecurityConfig;
026:
027: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
028:
029: /**
030: * Tests {@link RunAsManagerImpl}.
031: *
032: * @author Ben Alex
033: * @version $Id: RunAsManagerImplTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class RunAsManagerImplTests extends TestCase {
036: //~ Constructors ===================================================================================================
037:
038: public RunAsManagerImplTests() {
039: super ();
040: }
041:
042: public RunAsManagerImplTests(String arg0) {
043: super (arg0);
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(RunAsManagerImplTests.class);
050: }
051:
052: public final void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: public void testAlwaysSupportsClass() {
057: RunAsManagerImpl runAs = new RunAsManagerImpl();
058: assertTrue(runAs.supports(String.class));
059: }
060:
061: public void testDoesNotReturnAdditionalAuthoritiesIfCalledWithoutARunAsSetting()
062: throws Exception {
063: ConfigAttributeDefinition def = new ConfigAttributeDefinition();
064: def
065: .addConfigAttribute(new SecurityConfig(
066: "SOMETHING_WE_IGNORE"));
067:
068: UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken(
069: "Test", "Password", new GrantedAuthority[] {
070: new GrantedAuthorityImpl("ROLE_ONE"),
071: new GrantedAuthorityImpl("ROLE_TWO") });
072:
073: RunAsManagerImpl runAs = new RunAsManagerImpl();
074: runAs.setKey("my_password");
075:
076: Authentication resultingToken = runAs.buildRunAs(inputToken,
077: new Object(), def);
078: assertEquals(null, resultingToken);
079: }
080:
081: public void testRespectsRolePrefix() throws Exception {
082: ConfigAttributeDefinition def = new ConfigAttributeDefinition();
083: def.addConfigAttribute(new SecurityConfig("RUN_AS_SOMETHING"));
084:
085: UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken(
086: "Test", "Password", new GrantedAuthority[] {
087: new GrantedAuthorityImpl("ONE"),
088: new GrantedAuthorityImpl("TWO") });
089:
090: RunAsManagerImpl runAs = new RunAsManagerImpl();
091: runAs.setKey("my_password");
092: runAs.setRolePrefix("FOOBAR_");
093:
094: Authentication resultingToken = runAs.buildRunAs(inputToken,
095: new Object(), def);
096:
097: if (!(resultingToken instanceof RunAsUserToken)) {
098: fail("Should have returned a RunAsUserToken");
099: }
100:
101: assertEquals(inputToken.getPrincipal(), resultingToken
102: .getPrincipal());
103: assertEquals(inputToken.getCredentials(), resultingToken
104: .getCredentials());
105: assertEquals("FOOBAR_RUN_AS_SOMETHING", resultingToken
106: .getAuthorities()[0].getAuthority());
107: assertEquals("ONE", resultingToken.getAuthorities()[1]
108: .getAuthority());
109: assertEquals("TWO", resultingToken.getAuthorities()[2]
110: .getAuthority());
111:
112: RunAsUserToken resultCast = (RunAsUserToken) resultingToken;
113: assertEquals("my_password".hashCode(), resultCast.getKeyHash());
114: }
115:
116: public void testReturnsAdditionalGrantedAuthorities()
117: throws Exception {
118: ConfigAttributeDefinition def = new ConfigAttributeDefinition();
119: def.addConfigAttribute(new SecurityConfig("RUN_AS_SOMETHING"));
120:
121: UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken(
122: "Test", "Password", new GrantedAuthority[] {
123: new GrantedAuthorityImpl("ROLE_ONE"),
124: new GrantedAuthorityImpl("ROLE_TWO") });
125:
126: RunAsManagerImpl runAs = new RunAsManagerImpl();
127: runAs.setKey("my_password");
128:
129: Authentication resultingToken = runAs.buildRunAs(inputToken,
130: new Object(), def);
131:
132: if (!(resultingToken instanceof RunAsUserToken)) {
133: fail("Should have returned a RunAsUserToken");
134: }
135:
136: assertEquals(inputToken.getPrincipal(), resultingToken
137: .getPrincipal());
138: assertEquals(inputToken.getCredentials(), resultingToken
139: .getCredentials());
140: assertEquals("ROLE_RUN_AS_SOMETHING", resultingToken
141: .getAuthorities()[0].getAuthority());
142: assertEquals("ROLE_ONE", resultingToken.getAuthorities()[1]
143: .getAuthority());
144: assertEquals("ROLE_TWO", resultingToken.getAuthorities()[2]
145: .getAuthority());
146:
147: RunAsUserToken resultCast = (RunAsUserToken) resultingToken;
148: assertEquals("my_password".hashCode(), resultCast.getKeyHash());
149: }
150:
151: public void testStartupDetectsMissingKey() throws Exception {
152: RunAsManagerImpl runAs = new RunAsManagerImpl();
153:
154: try {
155: runAs.afterPropertiesSet();
156: fail("Should have thrown IllegalArgumentException");
157: } catch (IllegalArgumentException expected) {
158: assertTrue(true);
159: }
160: }
161:
162: public void testStartupSuccessfulWithKey() throws Exception {
163: RunAsManagerImpl runAs = new RunAsManagerImpl();
164: runAs.setKey("hello_world");
165: runAs.afterPropertiesSet();
166: assertEquals("hello_world", runAs.getKey());
167: }
168:
169: public void testSupports() throws Exception {
170: RunAsManager runAs = new RunAsManagerImpl();
171: assertTrue(runAs
172: .supports(new SecurityConfig("RUN_AS_SOMETHING")));
173: assertTrue(!runAs.supports(new SecurityConfig(
174: "ROLE_WHICH_IS_IGNORED")));
175: assertTrue(!runAs.supports(new SecurityConfig(
176: "role_LOWER_CASE_FAILS")));
177: }
178: }
|