KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jaas > CertificateLoginModuleTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.jaas;
20
21 import junit.framework.TestCase;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.security.cert.X509Certificate JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import javax.security.auth.Subject JavaDoc;
35 import javax.security.auth.login.LoginException JavaDoc;
36
37 public class CertificateLoginModuleTest extends TestCase {
38     private final String JavaDoc userName = "testUser";
39     private final List JavaDoc groupNames = new Vector JavaDoc();
40     private StubCertificateLoginModule loginModule;
41     
42     private Subject JavaDoc subject;
43     
44     public CertificateLoginModuleTest() {
45         groupNames.add("testGroup1");
46         groupNames.add("testGroup2");
47         groupNames.add("testGroup3");
48         groupNames.add("testGroup4");
49     }
50     
51     protected void setUp() throws Exception JavaDoc {
52         subject = new Subject JavaDoc();
53     }
54
55     protected void tearDown() throws Exception JavaDoc {
56     }
57     
58     private void loginWithCredentials(String JavaDoc userName, Set JavaDoc groupNames) throws LoginException JavaDoc {
59         loginModule = new StubCertificateLoginModule(userName, new HashSet JavaDoc(groupNames));
60         JaasCertificateCallbackHandler callbackHandler = new JaasCertificateCallbackHandler(null);
61         
62         loginModule.initialize(subject, callbackHandler, null, new HashMap JavaDoc());
63
64         loginModule.login();
65         loginModule.commit();
66     }
67     
68     private void checkPrincipalsMatch(Subject JavaDoc subject) {
69         boolean nameFound = false;
70         boolean groupsFound[] = new boolean[groupNames.size()];
71         for (int i = 0; i < groupsFound.length; ++i) {
72             groupsFound[i] = false;
73         }
74         
75         for (Iterator JavaDoc iter = subject.getPrincipals().iterator(); iter.hasNext(); ) {
76             Principal JavaDoc currentPrincipal = (Principal JavaDoc) iter.next();
77             
78             if (currentPrincipal instanceof UserPrincipal) {
79                 if (((UserPrincipal)currentPrincipal).getName().equals(userName)) {
80                     if (nameFound == false) {
81                         nameFound = true;
82                     } else {
83                         fail("UserPrincipal found twice.");
84                     }
85                         
86                 } else {
87                     fail("Unknown UserPrincipal found.");
88                 }
89                     
90             } else if (currentPrincipal instanceof GroupPrincipal) {
91                 int principalIdx = groupNames.indexOf(((GroupPrincipal)currentPrincipal).getName());
92                 
93                 if (principalIdx < 0) {
94                     fail("Unknown GroupPrincipal found.");
95                 }
96                 
97                 if (groupsFound[principalIdx] == false) {
98                     groupsFound[principalIdx] = true;
99                 } else {
100                     fail("GroupPrincipal found twice.");
101                 }
102             } else {
103                 fail("Unknown Principal type found.");
104             }
105         }
106     }
107     
108     public void testLoginSuccess() throws IOException JavaDoc {
109         try {
110             loginWithCredentials(userName, new HashSet JavaDoc(groupNames));
111         } catch (Exception JavaDoc e) {
112             fail("Unable to login: " + e.getMessage());
113         }
114         
115         checkPrincipalsMatch(subject);
116     }
117     
118     public void testLoginFailure() throws IOException JavaDoc {
119         boolean loginFailed = false;
120         
121         try {
122             loginWithCredentials(null, new HashSet JavaDoc());
123         } catch (LoginException JavaDoc e) {
124             loginFailed = true;
125         }
126         
127         if (!loginFailed) {
128             fail("Logged in with unknown certificate.");
129         }
130     }
131     
132     public void testLogOut() throws IOException JavaDoc {
133         try {
134             loginWithCredentials(userName, new HashSet JavaDoc(groupNames));
135         } catch (Exception JavaDoc e) {
136             fail("Unable to login: " + e.getMessage());
137         }
138         
139         loginModule.logout();
140         
141         assertEquals("logout should have cleared Subject principals.", 0, subject.getPrincipals().size());
142     }
143 }
144
145
Popular Tags