KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.activemq.security;
19
20 import org.apache.activemq.CombinationTestSupport;
21 import org.apache.activemq.JmsTestSupport;
22 import org.apache.activemq.broker.BrokerService;
23 import org.apache.activemq.command.ActiveMQDestination;
24 import org.apache.activemq.command.ActiveMQMessage;
25 import org.apache.activemq.command.ActiveMQQueue;
26 import org.apache.activemq.command.ActiveMQTopic;
27
28 import javax.jms.Connection JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.MessageConsumer JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34
35 /**
36  *
37  * @version $Revision: 450044 $
38  */

39 public class SecurityTestSupport extends JmsTestSupport {
40
41     public ActiveMQDestination destination;
42
43     /**
44      * Overrides to set the JMSXUserID flag to true.
45      */

46     protected BrokerService createBroker() throws Exception JavaDoc {
47         BrokerService broker = super.createBroker();
48         broker.setPopulateJMSXUserID(true);
49         return broker;
50     }
51
52     public void testUserReceiveFails() throws JMSException JavaDoc {
53         doReceive(true);
54     }
55
56     public void testInvalidAuthentication() throws JMSException JavaDoc {
57         try {
58             // No user id
59
Connection JavaDoc c = factory.createConnection();
60             connections.add(c);
61             c.start();
62             fail("Expected exception.");
63         }
64         catch (JMSException JavaDoc e) {
65         }
66
67         try {
68             // Bad password
69
Connection JavaDoc c = factory.createConnection("user", "krap");
70             connections.add(c);
71             c.start();
72             fail("Expected exception.");
73         }
74         catch (JMSException JavaDoc e) {
75         }
76
77         try {
78             // Bad userid
79
Connection JavaDoc c = factory.createConnection("userkrap", null);
80             connections.add(c);
81             c.start();
82             fail("Expected exception.");
83         }
84         catch (JMSException JavaDoc e) {
85         }
86     }
87
88     public void testUserReceiveSucceeds() throws JMSException JavaDoc {
89         Message m = doReceive(false);
90         assertEquals("system", ((ActiveMQMessage)m).getUserID());
91         assertEquals("system", m.getStringProperty("JMSXUserID"));
92     }
93
94     public void testGuestReceiveSucceeds() throws JMSException JavaDoc {
95         doReceive(false);
96     }
97
98     public void testGuestReceiveFails() throws JMSException JavaDoc {
99         doReceive(true);
100     }
101
102     public void testUserSendSucceeds() throws JMSException JavaDoc {
103         Message m = doSend(false);
104         assertEquals("user", ((ActiveMQMessage)m).getUserID());
105         assertEquals("user", m.getStringProperty("JMSXUserID"));
106     }
107
108     public void testUserSendFails() throws JMSException JavaDoc {
109         doSend(true);
110     }
111
112     public void testGuestSendFails() throws JMSException JavaDoc {
113         doSend(true);
114     }
115
116     public void testGuestSendSucceeds() throws JMSException JavaDoc {
117         doSend(false);
118     }
119
120     /**
121      * @throws JMSException
122      */

123     public Message doSend(boolean fail) throws JMSException JavaDoc {
124
125         Connection JavaDoc adminConnection = factory.createConnection("system", "manager");
126         connections.add(adminConnection);
127
128         adminConnection.start();
129         Session JavaDoc adminSession = adminConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
130         MessageConsumer JavaDoc consumer = adminSession.createConsumer(destination);
131
132         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
133         try {
134             sendMessages(session, destination, 1);
135         }
136         catch (JMSException JavaDoc e) {
137             // If test is expected to fail, the cause must only be a
138
// SecurityException
139
// otherwise rethrow the exception
140
if (!fail || !(e.getCause() instanceof SecurityException JavaDoc)) {
141                 throw e;
142             }
143         }
144
145         Message m = consumer.receive(1000);
146         if (fail)
147             assertNull(m);
148         else {
149             assertNotNull(m);
150             assertEquals("0", ((TextMessage JavaDoc) m).getText());
151             assertNull(consumer.receiveNoWait());
152         }
153         return m;
154     }
155
156     /**
157      * @throws JMSException
158      */

159     public Message doReceive(boolean fail) throws JMSException JavaDoc {
160
161         connection.start();
162         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
163         MessageConsumer JavaDoc consumer = null;
164         try {
165             consumer = session.createConsumer(destination);
166             if (fail)
167                 fail("Expected failure due to security constraint.");
168         }
169         catch (JMSException JavaDoc e) {
170             if (fail && e.getCause() instanceof SecurityException JavaDoc)
171                 return null;
172             throw e;
173         }
174
175         Connection JavaDoc adminConnection = factory.createConnection("system", "manager");
176         connections.add(adminConnection);
177         Session JavaDoc adminSession = adminConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
178         sendMessages(adminSession, destination, 1);
179
180         Message m = consumer.receive(1000);
181         assertNotNull(m);
182         assertEquals("0", ((TextMessage JavaDoc) m).getText());
183         assertNull(consumer.receiveNoWait());
184         return m;
185
186     }
187
188     /**
189      * @see {@link CombinationTestSupport}
190      */

191     public void initCombosForTestUserReceiveFails() {
192         addCombinationValues("userName", new Object JavaDoc[] { "user" });
193         addCombinationValues("password", new Object JavaDoc[] { "password" });
194         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("GUEST.BAR"),
195                 new ActiveMQTopic("GUEST.BAR"), });
196     }
197
198     /**
199      * @see {@link CombinationTestSupport}
200      */

201     public void initCombosForTestInvalidAuthentication() {
202         addCombinationValues("userName", new Object JavaDoc[] { "user" });
203         addCombinationValues("password", new Object JavaDoc[] { "password" });
204     }
205
206     /**
207      * @see {@link CombinationTestSupport}
208      */

209     public void initCombosForTestUserReceiveSucceeds() {
210         addCombinationValues("userName", new Object JavaDoc[] { "user" });
211         addCombinationValues("password", new Object JavaDoc[] { "password" });
212         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("USERS.FOO"), new ActiveMQTopic("USERS.FOO"), });
213     }
214
215     /**
216      * @see {@link CombinationTestSupport}
217      */

218     public void initCombosForTestGuestReceiveSucceeds() {
219         addCombinationValues("userName", new Object JavaDoc[] { "guest" });
220         addCombinationValues("password", new Object JavaDoc[] { "password" });
221         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR"), });
222     }
223
224     /**
225      * @see {@link CombinationTestSupport}
226      */

227     public void initCombosForTestGuestReceiveFails() {
228         addCombinationValues("userName", new Object JavaDoc[] { "guest" });
229         addCombinationValues("password", new Object JavaDoc[] { "password" });
230         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("USERS.FOO"),
231                 new ActiveMQTopic("USERS.FOO"), });
232     }
233
234     /**
235      * @see {@link CombinationTestSupport}
236      */

237     public void initCombosForTestUserSendSucceeds() {
238         addCombinationValues("userName", new Object JavaDoc[] { "user" });
239         addCombinationValues("password", new Object JavaDoc[] { "password" });
240         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("USERS.FOO"), new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("USERS.FOO"),
241                 new ActiveMQTopic("GUEST.BAR"), });
242     }
243
244     /**
245      * @see {@link CombinationTestSupport}
246      */

247     public void initCombosForTestUserSendFails() {
248         addCombinationValues("userName", new Object JavaDoc[] { "user" });
249         addCombinationValues("password", new Object JavaDoc[] { "password" });
250         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), });
251     }
252
253     /**
254      * @see {@link CombinationTestSupport}
255      */

256     public void initCombosForTestGuestSendFails() {
257         addCombinationValues("userName", new Object JavaDoc[] { "guest" });
258         addCombinationValues("password", new Object JavaDoc[] { "password" });
259         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), new ActiveMQQueue("USERS.FOO"),
260                 new ActiveMQTopic("USERS.FOO"), });
261     }
262
263     /**
264      * @see {@link CombinationTestSupport}
265      */

266     public void initCombosForTestGuestSendSucceeds() {
267         addCombinationValues("userName", new Object JavaDoc[] { "guest" });
268         addCombinationValues("password", new Object JavaDoc[] { "password" });
269         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR"), });
270     }
271 }
272
Popular Tags