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.adapters.catalina;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.adapters.PrincipalAcegiUserToken;
024:
025: import org.apache.catalina.LifecycleException;
026:
027: import java.io.File;
028:
029: import java.net.URL;
030:
031: import java.security.Principal;
032:
033: /**
034: * Tests {@link CatalinaAcegiUserRealm}.
035: *
036: * @author Ben Alex
037: * @version $Id: CatalinaAcegiUserRealmTests.java 1496 2006-05-23 13:38:33Z benalex $
038: */
039: public class CatalinaAcegiUserRealmTests extends TestCase {
040: //~ Instance fields ================================================================================================
041:
042: private final String ADAPTER_KEY = "my_key";
043:
044: //~ Constructors ===================================================================================================
045:
046: public CatalinaAcegiUserRealmTests() {
047: super ();
048: }
049:
050: public CatalinaAcegiUserRealmTests(String arg0) {
051: super (arg0);
052: }
053:
054: //~ Methods ========================================================================================================
055:
056: public static void main(String[] args) {
057: junit.textui.TestRunner.run(CatalinaAcegiUserRealmTests.class);
058: }
059:
060: private CatalinaAcegiUserRealm makeAdapter(String fileName)
061: throws Exception {
062: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
063:
064: URL url = Thread.currentThread().getContextClassLoader()
065: .getResource("org/acegisecurity/adapters/" + fileName);
066:
067: if (url == null) {
068: throw new Exception("Could not find " + fileName
069: + " - cannot continue");
070: }
071:
072: File file = new File(url.getFile());
073:
074: System.setProperty("catalina.base", file.getParentFile()
075: .getAbsolutePath());
076: System.out.println("catalina.base set to: "
077: + System.getProperty("catalina.base"));
078: adapter.setAppContextLocation(fileName);
079: adapter.setKey(ADAPTER_KEY);
080: adapter.startForTest();
081:
082: return adapter;
083: }
084:
085: public final void setUp() throws Exception {
086: super .setUp();
087: }
088:
089: public void testAdapterAbortsIfAppContextDoesNotContainAnAuthenticationBean()
090: throws Exception {
091: try {
092: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-invalid.xml");
093: fail("Should have thrown IllegalArgumentException");
094: } catch (IllegalArgumentException expected) {
095: assertTrue(true);
096: }
097: }
098:
099: public void testAdapterAbortsIfNoAppContextSpecified()
100: throws Exception {
101: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
102:
103: adapter.setKey("KEY");
104:
105: try {
106: adapter.startForTest();
107: fail("Should have thrown LifecycleException");
108: } catch (LifecycleException expected) {
109: assertEquals("appContextLocation must be defined", expected
110: .getMessage());
111: }
112:
113: adapter.setAppContextLocation("");
114:
115: try {
116: adapter.startForTest();
117: fail("Should have thrown LifecycleException");
118: } catch (LifecycleException expected) {
119: assertEquals("appContextLocation must be defined", expected
120: .getMessage());
121: }
122: }
123:
124: public void testAdapterAbortsIfNoKeySpecified() throws Exception {
125: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
126:
127: adapter.setAppContextLocation("SOMETHING");
128:
129: try {
130: adapter.startForTest();
131: fail("Should have thrown LifecycleException");
132: } catch (LifecycleException expected) {
133: assertEquals("key must be defined", expected.getMessage());
134: }
135:
136: adapter.setKey("");
137:
138: try {
139: adapter.startForTest();
140: fail("Should have thrown LifecycleException");
141: } catch (LifecycleException expected) {
142: assertEquals("key must be defined", expected.getMessage());
143: }
144: }
145:
146: public void testAdapterAbortsWithIncorrectApplicationContextLocation()
147: throws Exception {
148: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
149: adapter.setAppContextLocation("SOME_INVALID_PATH");
150: adapter.setKey("KEY");
151:
152: try {
153: adapter.startForTest();
154: fail("Should have thrown LifecycleException");
155: } catch (LifecycleException expected) {
156: assertTrue(expected.getMessage().startsWith(
157: "appContextLocation does not seem to exist in"));
158: }
159: }
160:
161: public void testAdapterIdentifiesItself() throws Exception {
162: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
163: assertTrue(adapter.getName().lastIndexOf(
164: "CatalinaSpringUserRealm") != -1);
165: }
166:
167: public void testAdapterStartsUpSuccess() throws Exception {
168: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
169: assertTrue(true);
170: }
171:
172: public void testAuthenticateManyParamsReturnsNull() {
173: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
174: assertEquals(null, adapter.authenticate(null, null, null, null,
175: null, null, null, null));
176: }
177:
178: public void testAuthenticateX509ReturnsNull() {
179: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
180: assertEquals(null, adapter.authenticate(null));
181: }
182:
183: public void testAuthenticationFailsForIncorrectPassword()
184: throws Exception {
185: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
186: assertEquals(null, adapter.authenticate("marissa", "kangaroo"));
187: }
188:
189: public void testAuthenticationFailsForIncorrectUserName()
190: throws Exception {
191: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
192: assertEquals(null, adapter.authenticate("melissa", "koala"));
193: }
194:
195: public void testAuthenticationUsingByteArrayForCredentials()
196: throws Exception {
197: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
198: byte[] credentials = { 'k', 'o', 'a', 'l', 'a' };
199: Principal result = adapter.authenticate("marissa", credentials);
200:
201: if (!(result instanceof PrincipalAcegiUserToken)) {
202: fail("Should have returned PrincipalAcegiUserToken");
203: }
204:
205: PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
206: assertEquals("marissa", castResult.getPrincipal());
207: assertEquals("koala", castResult.getCredentials());
208: assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
209: .getAuthority());
210: assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
211: .getAuthority());
212: assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
213: }
214:
215: public void testAuthenticationUsingStringForCredentials()
216: throws Exception {
217: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
218: Principal result = adapter.authenticate("marissa", "koala");
219:
220: if (!(result instanceof PrincipalAcegiUserToken)) {
221: fail("Should have returned PrincipalAcegiUserToken");
222: }
223:
224: PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
225: assertEquals("marissa", castResult.getPrincipal());
226: assertEquals("koala", castResult.getCredentials());
227: assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
228: .getAuthority());
229: assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
230: .getAuthority());
231: assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
232: }
233:
234: public void testAuthenticationWithNullPasswordHandledGracefully()
235: throws Exception {
236: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
237: assertEquals(null, adapter.authenticate("marissa",
238: (String) null));
239: }
240:
241: public void testAuthenticationWithNullUserNameHandledGracefully()
242: throws Exception {
243: CatalinaAcegiUserRealm adapter = makeAdapter("catalinaAdapterTest-valid.xml");
244: assertEquals(null, adapter.authenticate(null, "koala"));
245: }
246:
247: public void testGetPasswordReturnsNull() {
248: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
249: assertEquals(null, adapter.getPassword(null));
250: }
251:
252: public void testGetPrincipalReturnsNull() {
253: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
254: assertEquals(null, adapter.getPrincipal(null));
255: }
256:
257: public void testGetters() {
258: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
259: adapter.setKey("KEY");
260: assertEquals("KEY", adapter.getKey());
261: adapter.setAppContextLocation("SOME_LOCATION");
262: assertEquals("SOME_LOCATION", adapter.getAppContextLocation());
263: }
264:
265: public void testHasRoleWithANullPrincipalFails() {
266: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
267: assertTrue(!adapter.hasRole(null, "ROLE_ONE"));
268: }
269:
270: public void testHasRoleWithAPrincipalTheAdapterDidNotCreateFails() {
271: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
272: assertTrue(!adapter.hasRole(new Principal() {
273: public String getName() {
274: return "MockPrincipal";
275: }
276: }, "ROLE_ONE"));
277: }
278:
279: public void testHasRoleWithPrincipalAcegiUserToken() {
280: PrincipalAcegiUserToken token = new PrincipalAcegiUserToken(
281: "KEY", "Test", "Password", new GrantedAuthority[] {
282: new GrantedAuthorityImpl("ROLE_ONE"),
283: new GrantedAuthorityImpl("ROLE_TWO") }, null);
284: CatalinaAcegiUserRealm adapter = new CatalinaAcegiUserRealm();
285: assertTrue(adapter.hasRole(token, "ROLE_ONE"));
286: assertTrue(adapter.hasRole(token, "ROLE_TWO"));
287: assertTrue(!adapter.hasRole(token, "ROLE_WE_DO_NOT_HAVE"));
288: }
289: }
|