1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.runas;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.ConfigAttributeDefinition;
22  import org.acegisecurity.GrantedAuthority;
23  import org.acegisecurity.GrantedAuthorityImpl;
24  import org.acegisecurity.RunAsManager;
25  import org.acegisecurity.SecurityConfig;
26  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
27  
28  
29  /***
30   * Tests {@link RunAsManagerImpl}.
31   *
32   * @author Ben Alex
33   * @version $Id: RunAsManagerImplTests.java,v 1.4 2005/11/17 00:56:28 benalex Exp $
34   */
35  public class RunAsManagerImplTests extends TestCase {
36      //~ Constructors ===========================================================
37  
38      public RunAsManagerImplTests() {
39          super();
40      }
41  
42      public RunAsManagerImplTests(String arg0) {
43          super(arg0);
44      }
45  
46      //~ Methods ================================================================
47  
48      public final void setUp() throws Exception {
49          super.setUp();
50      }
51  
52      public static void main(String[] args) {
53          junit.textui.TestRunner.run(RunAsManagerImplTests.class);
54      }
55  
56      public void testAlwaysSupportsClass() {
57          RunAsManagerImpl runAs = new RunAsManagerImpl();
58          assertTrue(runAs.supports(String.class));
59      }
60  
61      public void testDoesNotReturnAdditionalAuthoritiesIfCalledWithoutARunAsSetting()
62          throws Exception {
63          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
64          def.addConfigAttribute(new SecurityConfig("SOMETHING_WE_IGNORE"));
65  
66          UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test",
67                  "Password",
68                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
69  
70          RunAsManagerImpl runAs = new RunAsManagerImpl();
71          runAs.setKey("my_password");
72  
73          Authentication resultingToken = runAs.buildRunAs(inputToken,
74                  new Object(), def);
75          assertEquals(null, resultingToken);
76      }
77  
78      public void testRespectsRolePrefix() throws Exception {
79          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
80          def.addConfigAttribute(new SecurityConfig("RUN_AS_SOMETHING"));
81  
82          UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test",
83                  "Password",
84                  new GrantedAuthority[] {new GrantedAuthorityImpl("ONE"), new GrantedAuthorityImpl("TWO")});
85  
86          RunAsManagerImpl runAs = new RunAsManagerImpl();
87          runAs.setKey("my_password");
88          runAs.setRolePrefix("FOOBAR_");
89  
90          Authentication resultingToken = runAs.buildRunAs(inputToken,
91                  new Object(), def);
92  
93          if (!(resultingToken instanceof RunAsUserToken)) {
94              fail("Should have returned a RunAsUserToken");
95          }
96  
97          assertEquals(inputToken.getPrincipal(), resultingToken.getPrincipal());
98          assertEquals(inputToken.getCredentials(),
99              resultingToken.getCredentials());
100         assertEquals("FOOBAR_RUN_AS_SOMETHING",
101             resultingToken.getAuthorities()[0].getAuthority());
102         assertEquals("ONE", resultingToken.getAuthorities()[1].getAuthority());
103         assertEquals("TWO", resultingToken.getAuthorities()[2].getAuthority());
104 
105         RunAsUserToken resultCast = (RunAsUserToken) resultingToken;
106         assertEquals("my_password".hashCode(), resultCast.getKeyHash());
107     }
108 
109     public void testReturnsAdditionalGrantedAuthorities()
110         throws Exception {
111         ConfigAttributeDefinition def = new ConfigAttributeDefinition();
112         def.addConfigAttribute(new SecurityConfig("RUN_AS_SOMETHING"));
113 
114         UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test",
115                 "Password",
116                 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
117 
118         RunAsManagerImpl runAs = new RunAsManagerImpl();
119         runAs.setKey("my_password");
120 
121         Authentication resultingToken = runAs.buildRunAs(inputToken,
122                 new Object(), def);
123 
124         if (!(resultingToken instanceof RunAsUserToken)) {
125             fail("Should have returned a RunAsUserToken");
126         }
127 
128         assertEquals(inputToken.getPrincipal(), resultingToken.getPrincipal());
129         assertEquals(inputToken.getCredentials(),
130             resultingToken.getCredentials());
131         assertEquals("ROLE_RUN_AS_SOMETHING",
132             resultingToken.getAuthorities()[0].getAuthority());
133         assertEquals("ROLE_ONE",
134             resultingToken.getAuthorities()[1].getAuthority());
135         assertEquals("ROLE_TWO",
136             resultingToken.getAuthorities()[2].getAuthority());
137 
138         RunAsUserToken resultCast = (RunAsUserToken) resultingToken;
139         assertEquals("my_password".hashCode(), resultCast.getKeyHash());
140     }
141 
142     public void testStartupDetectsMissingKey() throws Exception {
143         RunAsManagerImpl runAs = new RunAsManagerImpl();
144 
145         try {
146             runAs.afterPropertiesSet();
147             fail("Should have thrown IllegalArgumentException");
148         } catch (IllegalArgumentException expected) {
149             assertTrue(true);
150         }
151     }
152 
153     public void testStartupSuccessfulWithKey() throws Exception {
154         RunAsManagerImpl runAs = new RunAsManagerImpl();
155         runAs.setKey("hello_world");
156         runAs.afterPropertiesSet();
157         assertEquals("hello_world", runAs.getKey());
158     }
159 
160     public void testSupports() throws Exception {
161         RunAsManager runAs = new RunAsManagerImpl();
162         assertTrue(runAs.supports(new SecurityConfig("RUN_AS_SOMETHING")));
163         assertTrue(!runAs.supports(new SecurityConfig("ROLE_WHICH_IS_IGNORED")));
164         assertTrue(!runAs.supports(new SecurityConfig("role_LOWER_CASE_FAILS")));
165     }
166 }