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