KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > security > JaasCertificateAuthenticationBrokerTest


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.security;
20
21 import junit.framework.TestCase;
22
23 import org.apache.activemq.broker.ConnectionContext;
24 import org.apache.activemq.broker.StubBroker;
25 import org.apache.activemq.command.ConnectionInfo;
26 import org.apache.activemq.jaas.GroupPrincipal;
27 import org.apache.activemq.jaas.UserPrincipal;
28 import org.apache.activemq.transport.tcp.StubX509Certificate;
29
30 import java.io.IOException JavaDoc;
31 import java.security.Principal JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import javax.security.auth.Subject JavaDoc;
38 import javax.security.auth.callback.CallbackHandler JavaDoc;
39 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
40 import javax.security.auth.login.Configuration JavaDoc;
41 import javax.security.auth.login.LoginContext JavaDoc;
42
43 public class JaasCertificateAuthenticationBrokerTest extends TestCase {
44     StubBroker receiveBroker;
45     
46     JaasCertificateAuthenticationBroker authBroker;
47     
48     ConnectionContext connectionContext;
49     ConnectionInfo connectionInfo;
50     
51     protected void setUp() throws Exception JavaDoc {
52         receiveBroker = new StubBroker();
53         
54         authBroker = new JaasCertificateAuthenticationBroker(receiveBroker, "");
55         
56         connectionContext = new ConnectionContext();
57         connectionInfo = new ConnectionInfo();
58         
59         connectionInfo.setTransportContext(new StubX509Certificate[] {});
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         super.tearDown();
64     }
65     
66     private void setConfiguration(Set JavaDoc userNames, Set JavaDoc groupNames, boolean loginShouldSucceed) {
67         HashMap JavaDoc configOptions = new HashMap JavaDoc();
68         
69         String JavaDoc userNamesString;
70         {
71             Iterator JavaDoc iter = userNames.iterator();
72             userNamesString = "" + (iter.hasNext() ? (String JavaDoc)iter.next() : "");
73             while (iter.hasNext()) {
74                 userNamesString += "," + (String JavaDoc)iter.next();
75             }
76         }
77         
78         String JavaDoc groupNamesString = "";
79         {
80             Iterator JavaDoc iter = groupNames.iterator();
81             groupNamesString = "" + (iter.hasNext() ? (String JavaDoc)iter.next() : "");
82             while (iter.hasNext()) {
83                 groupNamesString += "," + (String JavaDoc)iter.next();
84             }
85         }
86         
87         configOptions.put(StubLoginModule.ALLOW_LOGIN_PROPERTY, (loginShouldSucceed ? "true" : "false"));
88         configOptions.put(StubLoginModule.USERS_PROPERTY, userNamesString);
89         configOptions.put(StubLoginModule.GROUPS_PROPERTY, groupNamesString);
90         AppConfigurationEntry JavaDoc configEntry = new AppConfigurationEntry JavaDoc(
91             "org.apache.activemq.security.StubLoginModule",
92             AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
93             configOptions);
94     
95         StubJaasConfiguration jaasConfig = new StubJaasConfiguration(configEntry);
96         
97         Configuration.setConfiguration(jaasConfig);
98     }
99     
100     public void testAddConnectionSuccess() {
101         String JavaDoc dnUserName = "dnUserName";
102         
103         HashSet JavaDoc userNames = new HashSet JavaDoc();
104         userNames.add(dnUserName);
105         
106         HashSet JavaDoc groupNames = new HashSet JavaDoc();
107         groupNames.add("testGroup1");
108         groupNames.add("testGroup2");
109         groupNames.add("tesetGroup3");
110         
111         setConfiguration(
112             userNames,
113             groupNames,
114             true);
115         
116         try {
117             authBroker.addConnection(connectionContext, connectionInfo);
118         } catch (Exception JavaDoc e) {
119             fail("Call to addConnection failed: " + e.getMessage());
120         }
121         
122         assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " +
123                 "AuthenticationBroker.",
124             1, receiveBroker.addConnectionData.size());
125         
126         ConnectionContext receivedContext =
127             ((StubBroker.AddConnectionData)receiveBroker.addConnectionData.getFirst()).connectionContext;
128         
129         assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.",
130             dnUserName, receivedContext.getSecurityContext().getUserName());
131         
132         Set JavaDoc receivedPrincipals =
133             receivedContext.getSecurityContext().getPrincipals();
134         
135         for (Iterator JavaDoc iter = receivedPrincipals.iterator(); iter.hasNext(); ) {
136             Principal currentPrincipal = (Principal)iter.next();
137             
138             if (currentPrincipal instanceof UserPrincipal) {
139                 if (userNames.remove(currentPrincipal.getName())) {
140                     // Nothing, we did good.
141
} else {
142                     // Found an unknown userName.
143
fail("Unknown UserPrincipal found");
144                 }
145             } else if (currentPrincipal instanceof GroupPrincipal) {
146                 if (groupNames.remove(currentPrincipal.getName())) {
147                     // Nothing, we did good.
148
} else {
149                     fail("Unknown GroupPrincipal found.");
150                 }
151             } else {
152                 fail("Unexpected Principal subclass found.");
153             }
154         }
155         
156         if (!userNames.isEmpty()) {
157             fail("Some usernames were not added as UserPrincipals");
158         }
159         
160         if (!groupNames.isEmpty()) {
161             fail("Some group names were not added as GroupPrincipals");
162         }
163     }
164     
165     public void testAddConnectionFailure() {
166         HashSet JavaDoc userNames = new HashSet JavaDoc();
167         
168         HashSet JavaDoc groupNames = new HashSet JavaDoc();
169         groupNames.add("testGroup1");
170         groupNames.add("testGroup2");
171         groupNames.add("tesetGroup3");
172         
173         setConfiguration(
174             userNames,
175             groupNames,
176             false);
177         
178         boolean connectFailed = false;
179         try {
180             authBroker.addConnection(connectionContext, connectionInfo);
181         } catch (SecurityException JavaDoc e) {
182             connectFailed = true;
183         } catch (Exception JavaDoc e) {
184             fail("Failed to connect for unexpected reason: " + e.getMessage());
185         }
186         
187         if (!connectFailed) {
188             fail("Unauthenticated connection allowed.");
189         }
190         
191         assertEquals("Unauthenticated connection allowed.",
192             true, receiveBroker.addConnectionData.isEmpty());
193     }
194     
195     public void testRemoveConnection() throws Exception JavaDoc {
196         connectionContext.setSecurityContext(new StubSecurityContext());
197         
198         authBroker.removeConnection(connectionContext, connectionInfo, new Throwable JavaDoc());
199         
200         assertEquals("removeConnection should clear ConnectionContext.",
201             null, connectionContext.getSecurityContext());
202
203         assertEquals("Incorrect number of calls to underlying broker were made.",
204             1, receiveBroker.removeConnectionData.size());
205     }
206 }
207
Popular Tags