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.cas;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
024:
025: import org.acegisecurity.userdetails.User;
026: import org.acegisecurity.userdetails.UserDetails;
027:
028: import java.util.List;
029: import java.util.Vector;
030:
031: /**
032: * Tests {@link CasAuthenticationToken}.
033: *
034: * @author Ben Alex
035: * @version $Id: CasAuthenticationTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class CasAuthenticationTokenTests extends TestCase {
038: //~ Constructors ===================================================================================================
039:
040: public CasAuthenticationTokenTests() {
041: super ();
042: }
043:
044: public CasAuthenticationTokenTests(String arg0) {
045: super (arg0);
046: }
047:
048: //~ Methods ========================================================================================================
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner.run(CasAuthenticationTokenTests.class);
052: }
053:
054: private UserDetails makeUserDetails() {
055: return makeUserDetails("user");
056: }
057:
058: private UserDetails makeUserDetails(final String name) {
059: return new User(name, "password", true, true, true, true,
060: new GrantedAuthority[] {
061: new GrantedAuthorityImpl("ROLE_ONE"),
062: new GrantedAuthorityImpl("ROLE_TWO") });
063: }
064:
065: public final void setUp() throws Exception {
066: super .setUp();
067: }
068:
069: public void testConstructorRejectsNulls() {
070: try {
071: new CasAuthenticationToken(null, makeUserDetails(),
072: "Password", new GrantedAuthority[] {
073: new GrantedAuthorityImpl("ROLE_ONE"),
074: new GrantedAuthorityImpl("ROLE_TWO") },
075: makeUserDetails(), new Vector(),
076: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
077: fail("Should have thrown IllegalArgumentException");
078: } catch (IllegalArgumentException expected) {
079: assertTrue(true);
080: }
081:
082: try {
083: new CasAuthenticationToken("key", null, "Password",
084: new GrantedAuthority[] {
085: new GrantedAuthorityImpl("ROLE_ONE"),
086: new GrantedAuthorityImpl("ROLE_TWO") },
087: makeUserDetails(), new Vector(),
088: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
089: fail("Should have thrown IllegalArgumentException");
090: } catch (IllegalArgumentException expected) {
091: assertTrue(true);
092: }
093:
094: try {
095: new CasAuthenticationToken("key", makeUserDetails(), null,
096: new GrantedAuthority[] {
097: new GrantedAuthorityImpl("ROLE_ONE"),
098: new GrantedAuthorityImpl("ROLE_TWO") },
099: makeUserDetails(), new Vector(),
100: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
101: fail("Should have thrown IllegalArgumentException");
102: } catch (IllegalArgumentException expected) {
103: assertTrue(true);
104: }
105:
106: try {
107: new CasAuthenticationToken("key", makeUserDetails(),
108: "Password", null, makeUserDetails(), new Vector(),
109: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
110: fail("Should have thrown IllegalArgumentException");
111: } catch (IllegalArgumentException expected) {
112: assertTrue(true);
113: }
114:
115: try {
116: new CasAuthenticationToken("key", makeUserDetails(),
117: "Password", new GrantedAuthority[] {
118: new GrantedAuthorityImpl("ROLE_ONE"),
119: new GrantedAuthorityImpl("ROLE_TWO") },
120: makeUserDetails(), null,
121: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
122: fail("Should have thrown IllegalArgumentException");
123: } catch (IllegalArgumentException expected) {
124: assertTrue(true);
125: }
126:
127: try {
128: new CasAuthenticationToken("key", makeUserDetails(),
129: "Password", new GrantedAuthority[] {
130: new GrantedAuthorityImpl("ROLE_ONE"),
131: new GrantedAuthorityImpl("ROLE_TWO") },
132: null, new Vector(),
133: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
134: fail("Should have thrown IllegalArgumentException");
135: } catch (IllegalArgumentException expected) {
136: assertTrue(true);
137: }
138:
139: try {
140: new CasAuthenticationToken("key", makeUserDetails(),
141: "Password", new GrantedAuthority[] {
142: new GrantedAuthorityImpl("ROLE_ONE"),
143: new GrantedAuthorityImpl("ROLE_TWO") },
144: makeUserDetails(), new Vector(), null);
145: fail("Should have thrown IllegalArgumentException");
146: } catch (IllegalArgumentException expected) {
147: assertTrue(true);
148: }
149:
150: try {
151: new CasAuthenticationToken("key", makeUserDetails(),
152: "Password", new GrantedAuthority[] {
153: new GrantedAuthorityImpl("ROLE_ONE"), null,
154: new GrantedAuthorityImpl("ROLE_TWO") },
155: makeUserDetails(), new Vector(),
156: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
157: fail("Should have thrown IllegalArgumentException");
158: } catch (IllegalArgumentException expected) {
159: assertTrue(true);
160: }
161: }
162:
163: public void testEqualsWhenEqual() {
164: List proxyList1 = new Vector();
165: proxyList1
166: .add("https://localhost/newPortal/j_acegi_cas_security_check");
167:
168: CasAuthenticationToken token1 = new CasAuthenticationToken(
169: "key", makeUserDetails(), "Password",
170: new GrantedAuthority[] {
171: new GrantedAuthorityImpl("ROLE_ONE"),
172: new GrantedAuthorityImpl("ROLE_TWO") },
173: makeUserDetails(), proxyList1,
174: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
175:
176: List proxyList2 = new Vector();
177: proxyList2
178: .add("https://localhost/newPortal/j_acegi_cas_security_check");
179:
180: CasAuthenticationToken token2 = new CasAuthenticationToken(
181: "key", makeUserDetails(), "Password",
182: new GrantedAuthority[] {
183: new GrantedAuthorityImpl("ROLE_ONE"),
184: new GrantedAuthorityImpl("ROLE_TWO") },
185: makeUserDetails(), proxyList2,
186: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
187:
188: assertEquals(token1, token2);
189: }
190:
191: public void testGetters() {
192: // Build the proxy list returned in the ticket from CAS
193: List proxyList = new Vector();
194: proxyList
195: .add("https://localhost/newPortal/j_acegi_cas_security_check");
196:
197: CasAuthenticationToken token = new CasAuthenticationToken(
198: "key", makeUserDetails(), "Password",
199: new GrantedAuthority[] {
200: new GrantedAuthorityImpl("ROLE_ONE"),
201: new GrantedAuthorityImpl("ROLE_TWO") },
202: makeUserDetails(), proxyList,
203: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
204: assertEquals("key".hashCode(), token.getKeyHash());
205: assertEquals(makeUserDetails(), token.getPrincipal());
206: assertEquals("Password", token.getCredentials());
207: assertEquals("ROLE_ONE", token.getAuthorities()[0]
208: .getAuthority());
209: assertEquals("ROLE_TWO", token.getAuthorities()[1]
210: .getAuthority());
211: assertEquals(
212: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt",
213: token.getProxyGrantingTicketIou());
214: assertEquals(proxyList, token.getProxyList());
215: assertEquals(makeUserDetails().getUsername(), token
216: .getUserDetails().getUsername());
217: }
218:
219: public void testNoArgConstructorDoesntExist() {
220: Class clazz = CasAuthenticationToken.class;
221:
222: try {
223: clazz.getDeclaredConstructor((Class[]) null);
224: fail("Should have thrown NoSuchMethodException");
225: } catch (NoSuchMethodException expected) {
226: assertTrue(true);
227: }
228: }
229:
230: public void testNotEqualsDueToAbstractParentEqualsCheck() {
231: List proxyList1 = new Vector();
232: proxyList1
233: .add("https://localhost/newPortal/j_acegi_cas_security_check");
234:
235: CasAuthenticationToken token1 = new CasAuthenticationToken(
236: "key", makeUserDetails(), "Password",
237: new GrantedAuthority[] {
238: new GrantedAuthorityImpl("ROLE_ONE"),
239: new GrantedAuthorityImpl("ROLE_TWO") },
240: makeUserDetails(), proxyList1,
241: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
242:
243: List proxyList2 = new Vector();
244: proxyList2
245: .add("https://localhost/newPortal/j_acegi_cas_security_check");
246:
247: CasAuthenticationToken token2 = new CasAuthenticationToken(
248: "key", makeUserDetails("OTHER_NAME"), "Password",
249: new GrantedAuthority[] {
250: new GrantedAuthorityImpl("ROLE_ONE"),
251: new GrantedAuthorityImpl("ROLE_TWO") },
252: makeUserDetails(), proxyList2,
253: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
254:
255: assertTrue(!token1.equals(token2));
256: }
257:
258: public void testNotEqualsDueToDifferentAuthenticationClass() {
259: List proxyList1 = new Vector();
260: proxyList1
261: .add("https://localhost/newPortal/j_acegi_cas_security_check");
262:
263: CasAuthenticationToken token1 = new CasAuthenticationToken(
264: "key", makeUserDetails(), "Password",
265: new GrantedAuthority[] {
266: new GrantedAuthorityImpl("ROLE_ONE"),
267: new GrantedAuthorityImpl("ROLE_TWO") },
268: makeUserDetails(), proxyList1,
269: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
270:
271: UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken(
272: "Test", "Password", new GrantedAuthority[] {
273: new GrantedAuthorityImpl("ROLE_ONE"),
274: new GrantedAuthorityImpl("ROLE_TWO") });
275:
276: assertTrue(!token1.equals(token2));
277: }
278:
279: public void testNotEqualsDueToKey() {
280: List proxyList1 = new Vector();
281: proxyList1
282: .add("https://localhost/newPortal/j_acegi_cas_security_check");
283:
284: CasAuthenticationToken token1 = new CasAuthenticationToken(
285: "key", makeUserDetails(), "Password",
286: new GrantedAuthority[] {
287: new GrantedAuthorityImpl("ROLE_ONE"),
288: new GrantedAuthorityImpl("ROLE_TWO") },
289: makeUserDetails(), proxyList1,
290: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
291:
292: List proxyList2 = new Vector();
293: proxyList2
294: .add("https://localhost/newPortal/j_acegi_cas_security_check");
295:
296: CasAuthenticationToken token2 = new CasAuthenticationToken(
297: "DIFFERENT_KEY", makeUserDetails(), "Password",
298: new GrantedAuthority[] {
299: new GrantedAuthorityImpl("ROLE_ONE"),
300: new GrantedAuthorityImpl("ROLE_TWO") },
301: makeUserDetails(), proxyList2,
302: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
303:
304: assertTrue(!token1.equals(token2));
305: }
306:
307: public void testNotEqualsDueToProxyGrantingTicket() {
308: List proxyList1 = new Vector();
309: proxyList1
310: .add("https://localhost/newPortal/j_acegi_cas_security_check");
311:
312: CasAuthenticationToken token1 = new CasAuthenticationToken(
313: "key", makeUserDetails(), "Password",
314: new GrantedAuthority[] {
315: new GrantedAuthorityImpl("ROLE_ONE"),
316: new GrantedAuthorityImpl("ROLE_TWO") },
317: makeUserDetails(), proxyList1,
318: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
319:
320: List proxyList2 = new Vector();
321: proxyList2
322: .add("https://localhost/newPortal/j_acegi_cas_security_check");
323:
324: CasAuthenticationToken token2 = new CasAuthenticationToken(
325: "key", makeUserDetails(), "Password",
326: new GrantedAuthority[] {
327: new GrantedAuthorityImpl("ROLE_ONE"),
328: new GrantedAuthorityImpl("ROLE_TWO") },
329: makeUserDetails(), proxyList2,
330: "PGTIOU-SOME_OTHER_VALUE");
331:
332: assertTrue(!token1.equals(token2));
333: }
334:
335: public void testNotEqualsDueToProxyList() {
336: List proxyList1 = new Vector();
337: proxyList1
338: .add("https://localhost/newPortal/j_acegi_cas_security_check");
339:
340: CasAuthenticationToken token1 = new CasAuthenticationToken(
341: "key", makeUserDetails(), "Password",
342: new GrantedAuthority[] {
343: new GrantedAuthorityImpl("ROLE_ONE"),
344: new GrantedAuthorityImpl("ROLE_TWO") },
345: makeUserDetails(), proxyList1,
346: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
347:
348: List proxyList2 = new Vector();
349: proxyList2
350: .add("https://localhost/SOME_OTHER_PORTAL/j_acegi_cas_security_check");
351:
352: CasAuthenticationToken token2 = new CasAuthenticationToken(
353: "key", makeUserDetails(), "Password",
354: new GrantedAuthority[] {
355: new GrantedAuthorityImpl("ROLE_ONE"),
356: new GrantedAuthorityImpl("ROLE_TWO") },
357: makeUserDetails(), proxyList2,
358: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
359:
360: assertTrue(!token1.equals(token2));
361: }
362:
363: public void testSetAuthenticated() {
364: CasAuthenticationToken token = new CasAuthenticationToken(
365: "key", makeUserDetails(), "Password",
366: new GrantedAuthority[] {
367: new GrantedAuthorityImpl("ROLE_ONE"),
368: new GrantedAuthorityImpl("ROLE_TWO") },
369: makeUserDetails(), new Vector(),
370: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
371: assertTrue(token.isAuthenticated());
372: token.setAuthenticated(false);
373: assertTrue(!token.isAuthenticated());
374: }
375:
376: public void testToString() {
377: CasAuthenticationToken token = new CasAuthenticationToken(
378: "key", makeUserDetails(), "Password",
379: new GrantedAuthority[] {
380: new GrantedAuthorityImpl("ROLE_ONE"),
381: new GrantedAuthorityImpl("ROLE_TWO") },
382: makeUserDetails(), new Vector(),
383: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
384: String result = token.toString();
385: assertTrue(result.lastIndexOf("Proxy List:") != -1);
386: assertTrue(result.lastIndexOf("Proxy-Granting Ticket IOU:") != -1);
387: assertTrue(result
388: .lastIndexOf("Credentials (Service/Proxy Ticket):") != -1);
389: }
390: }
|