1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.cas;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22
23 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
24
25 import org.acegisecurity.userdetails.User;
26 import org.acegisecurity.userdetails.UserDetails;
27
28 import java.util.List;
29 import java.util.Vector;
30
31
32
33
34
35
36
37
38 public class CasAuthenticationTokenTests extends TestCase {
39
40
41 public CasAuthenticationTokenTests() {
42 super();
43 }
44
45 public CasAuthenticationTokenTests(String arg0) {
46 super(arg0);
47 }
48
49
50
51 public static void main(String[] args) {
52 junit.textui.TestRunner.run(CasAuthenticationTokenTests.class);
53 }
54
55 private UserDetails makeUserDetails() {
56 return makeUserDetails("user");
57 }
58
59 private UserDetails makeUserDetails(final String name) {
60 return new User(name, "password", true, true, true, true,
61 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
62 }
63
64 public final void setUp() throws Exception {
65 super.setUp();
66 }
67
68 public void testConstructorRejectsNulls() {
69 try {
70 new CasAuthenticationToken(null, makeUserDetails(), "Password",
71 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
72 makeUserDetails(), new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
73 fail("Should have thrown IllegalArgumentException");
74 } catch (IllegalArgumentException expected) {
75 assertTrue(true);
76 }
77
78 try {
79 new CasAuthenticationToken("key", null, "Password",
80 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
81 makeUserDetails(), new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
82 fail("Should have thrown IllegalArgumentException");
83 } catch (IllegalArgumentException expected) {
84 assertTrue(true);
85 }
86
87 try {
88 new CasAuthenticationToken("key", makeUserDetails(), null,
89 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
90 makeUserDetails(), new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
91 fail("Should have thrown IllegalArgumentException");
92 } catch (IllegalArgumentException expected) {
93 assertTrue(true);
94 }
95
96 try {
97 new CasAuthenticationToken("key", makeUserDetails(), "Password", null, makeUserDetails(), new Vector(),
98 "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
99 fail("Should have thrown IllegalArgumentException");
100 } catch (IllegalArgumentException expected) {
101 assertTrue(true);
102 }
103
104 try {
105 new CasAuthenticationToken("key", makeUserDetails(), "Password",
106 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
107 makeUserDetails(), null, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
108 fail("Should have thrown IllegalArgumentException");
109 } catch (IllegalArgumentException expected) {
110 assertTrue(true);
111 }
112
113 try {
114 new CasAuthenticationToken("key", makeUserDetails(), "Password",
115 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
116 null, new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
117 fail("Should have thrown IllegalArgumentException");
118 } catch (IllegalArgumentException expected) {
119 assertTrue(true);
120 }
121
122 try {
123 new CasAuthenticationToken("key", makeUserDetails(), "Password",
124 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
125 makeUserDetails(), new Vector(), null);
126 fail("Should have thrown IllegalArgumentException");
127 } catch (IllegalArgumentException expected) {
128 assertTrue(true);
129 }
130
131 try {
132 new CasAuthenticationToken("key", makeUserDetails(), "Password",
133 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), null, new GrantedAuthorityImpl("ROLE_TWO")},
134 makeUserDetails(), new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
135 fail("Should have thrown IllegalArgumentException");
136 } catch (IllegalArgumentException expected) {
137 assertTrue(true);
138 }
139 }
140
141 public void testEqualsWhenEqual() {
142 List proxyList1 = new Vector();
143 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
144
145 CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
146 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
147 makeUserDetails(), proxyList1, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
148
149 List proxyList2 = new Vector();
150 proxyList2.add("https://localhost/newPortal/j_acegi_cas_security_check");
151
152 CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
153 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
154 makeUserDetails(), proxyList2, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
155
156 assertEquals(token1, token2);
157 }
158
159 public void testGetters() {
160
161 List proxyList = new Vector();
162 proxyList.add("https://localhost/newPortal/j_acegi_cas_security_check");
163
164 CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
165 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
166 makeUserDetails(), proxyList, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
167 assertEquals("key".hashCode(), token.getKeyHash());
168 assertEquals(makeUserDetails(), token.getPrincipal());
169 assertEquals("Password", token.getCredentials());
170 assertEquals("ROLE_ONE", token.getAuthorities()[0].getAuthority());
171 assertEquals("ROLE_TWO", token.getAuthorities()[1].getAuthority());
172 assertEquals("PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt", token.getProxyGrantingTicketIou());
173 assertEquals(proxyList, token.getProxyList());
174 assertEquals(makeUserDetails().getUsername(), token.getUserDetails().getUsername());
175 }
176
177 public void testNoArgConstructorDoesntExist() {
178 Class clazz = CasAuthenticationToken.class;
179
180 try {
181 clazz.getDeclaredConstructor((Class[]) null);
182 fail("Should have thrown NoSuchMethodException");
183 } catch (NoSuchMethodException expected) {
184 assertTrue(true);
185 }
186 }
187
188 public void testNotEqualsDueToAbstractParentEqualsCheck() {
189 List proxyList1 = new Vector();
190 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
191
192 CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
193 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
194 makeUserDetails(), proxyList1, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
195
196 List proxyList2 = new Vector();
197 proxyList2.add("https://localhost/newPortal/j_acegi_cas_security_check");
198
199 CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails("OTHER_NAME"), "Password",
200 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
201 makeUserDetails(), proxyList2, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
202
203 assertTrue(!token1.equals(token2));
204 }
205
206 public void testNotEqualsDueToDifferentAuthenticationClass() {
207 List proxyList1 = new Vector();
208 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
209
210 CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
211 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
212 makeUserDetails(), proxyList1, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
213
214 UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password",
215 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
216
217 assertTrue(!token1.equals(token2));
218 }
219
220 public void testNotEqualsDueToKey() {
221 List proxyList1 = new Vector();
222 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
223
224 CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
225 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
226 makeUserDetails(), proxyList1, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
227
228 List proxyList2 = new Vector();
229 proxyList2.add("https://localhost/newPortal/j_acegi_cas_security_check");
230
231 CasAuthenticationToken token2 = new CasAuthenticationToken("DIFFERENT_KEY", makeUserDetails(), "Password",
232 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
233 makeUserDetails(), proxyList2, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
234
235 assertTrue(!token1.equals(token2));
236 }
237
238 public void testNotEqualsDueToProxyGrantingTicket() {
239 List proxyList1 = new Vector();
240 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
241
242 CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
243 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
244 makeUserDetails(), proxyList1, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
245
246 List proxyList2 = new Vector();
247 proxyList2.add("https://localhost/newPortal/j_acegi_cas_security_check");
248
249 CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
250 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
251 makeUserDetails(), proxyList2, "PGTIOU-SOME_OTHER_VALUE");
252
253 assertTrue(!token1.equals(token2));
254 }
255
256 public void testNotEqualsDueToProxyList() {
257 List proxyList1 = new Vector();
258 proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
259
260 CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
261 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
262 makeUserDetails(), proxyList1, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
263
264 List proxyList2 = new Vector();
265 proxyList2.add("https://localhost/SOME_OTHER_PORTAL/j_acegi_cas_security_check");
266
267 CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
268 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
269 makeUserDetails(), proxyList2, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
270
271 assertTrue(!token1.equals(token2));
272 }
273
274 public void testSetAuthenticated() {
275 CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
276 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
277 makeUserDetails(), new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
278 assertTrue(token.isAuthenticated());
279 token.setAuthenticated(false);
280 assertTrue(!token.isAuthenticated());
281 }
282
283 public void testToString() {
284 CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
285 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
286 makeUserDetails(), new Vector(), "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
287 String result = token.toString();
288 assertTrue(result.lastIndexOf("Proxy List:") != -1);
289 assertTrue(result.lastIndexOf("Proxy-Granting Ticket IOU:") != -1);
290 assertTrue(result.lastIndexOf("Credentials (Service/Proxy Ticket):") != -1);
291 }
292 }