KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > SecurityUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmq.test;
23
24 import javax.management.ObjectName JavaDoc;
25 import junit.framework.Assert;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import org.jboss.test.JBossTestSetup;
29 import org.jboss.test.jbossmq.MQBase;
30 /**
31  * Test of security features in JBossMQ
32  *
33  *
34  * @author <a HREF="pra@tim.se">Peter Antman</a>
35  * @version $Revision: 37406 $
36  */

37 public class SecurityUnitTestCase extends MQBase
38 {
39    public SecurityUnitTestCase(String JavaDoc name)
40    {
41       super(name);
42    }
43
44    public void runLoginTest() throws Exception JavaDoc
45    {
46       TopicWorker sub1 = null;
47       TopicWorker pub1 = null;
48       try
49       {
50          drainTopic();
51          int ic = 5;
52          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, ic);
53          sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
54          sub1.setUser("john", "needle");
55          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
56          t1.start();
57          // Publish
58
IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
59          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
60          pub1.connect();
61          pub1.publish();
62          Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
63                pub1.getMessageHandled());
64          // let sub1 have some time to handle the messages.
65
log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
66          sleep(ic * 100);
67          Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic, sub1
68                .getMessageHandled());
69          sub1.close();
70          pub1.close();
71       }
72       catch (Throwable JavaDoc t)
73       {
74          if (t instanceof junit.framework.AssertionFailedError)
75             throw (junit.framework.AssertionFailedError) t;
76          log.error("Error in test: " + t, t);
77          throw new Exception JavaDoc(t.getMessage());
78       }
79       finally
80       {
81          try
82          {
83             if (sub1 != null)
84                sub1.close();
85          }
86          catch (Exception JavaDoc ex)
87          {
88          }
89          try
90          {
91             if (pub1 != null)
92                pub1.close();
93          }
94          catch (Exception JavaDoc ex)
95          {
96          }
97       }
98    }
99
100    /**
101     Tests that check authentication
102     1. Login without cred
103     2. Login with valid usedid,pwd
104     3. Login with valid user, unvalid pwd
105     4. Login with unvalid user.
106     */

107    public void runLoginNoCred() throws Exception JavaDoc
108    {
109       TopicWorker pub1 = null;
110       try
111       {
112          drainTopic();
113          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
114          pub1.connect();
115       }
116       catch (Exception JavaDoc ex)
117       {
118          Assert.fail("Could lot login without any cred");
119       }
120       finally
121       {
122          try
123          {
124             pub1.close();
125          }
126          catch (Exception JavaDoc ex)
127          {
128          }
129       }
130    }
131
132    public void runLoginValidCred() throws Exception JavaDoc
133    {
134       TopicWorker pub1 = null;
135       try
136       {
137          drainTopic();
138          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
139          pub1.setUser("john", "needle");
140          pub1.connect();
141       }
142       catch (Exception JavaDoc ex)
143       {
144          Assert.fail("Could lot login with valid cred");
145       }
146       finally
147       {
148          try
149          {
150             pub1.close();
151          }
152          catch (Exception JavaDoc ex)
153          {
154          }
155       }
156    }
157
158    public void runLoginInvalidPwd() throws Exception JavaDoc
159    {
160       TopicWorker pub1 = null;
161       try
162       {
163          drainTopic();
164          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
165          pub1.setUser("john", "bogus");
166          Exception JavaDoc e = null;
167          try
168          {
169             pub1.connect();
170          }
171          catch (Exception JavaDoc ex)
172          {
173             e = ex;
174          }
175          log.debug(e);
176          Assert.assertTrue("Loggin in with invalid password did not throw correct exception",
177                e instanceof javax.jms.JMSSecurityException JavaDoc);
178       }
179       finally
180       {
181          try
182          {
183             pub1.close();
184          }
185          catch (Exception JavaDoc ex)
186          {
187          }
188       }
189    }
190
191    public void runLoginInvalidCred() throws Exception JavaDoc
192    {
193       TopicWorker pub1 = null;
194       try
195       {
196          drainTopic();
197          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, null, 0);
198          pub1.setUser("bogus", "bogus");
199          Exception JavaDoc e = null;
200          try
201          {
202             pub1.connect();
203          }
204          catch (Exception JavaDoc ex)
205          {
206             e = ex;
207          }
208          log.debug(e);
209          Assert.assertTrue("Loggin in with invalid user did not throw correct exception",
210                e instanceof javax.jms.JMSSecurityException JavaDoc);
211       }
212       finally
213       {
214          try
215          {
216             pub1.close();
217          }
218          catch (Exception JavaDoc ex)
219          {
220          }
221       }
222    }
223
224    /**
225     An number of tests to verrify that clientID works as expected:
226     
227     1. Nothing. getClientID should return a string starting withID
228     2. user/pwd with preconfigured clientID, should return preconf
229     3. setClientID, should return the set clientID
230     4. setClienID starting with ID, should trow invalid clientID
231     5. setClientID same as a preconf, should trow invalid clientID
232     6. setClientID after any method beeing invoked on con, throw invalid state
233     */

234    public void runClientIDNormalTest() throws Exception JavaDoc
235    {
236       TopicWorker pub1 = null;
237       try
238       {
239          drainTopic();
240          int ic = 5;
241          // Publish
242
IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
243          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
244          pub1.connect();
245          pub1.publish();
246          Assert.assertTrue("Client did not get a valid clientID", pub1.connection.getClientID().startsWith("ID"));
247          pub1.close();
248       }
249       catch (Throwable JavaDoc t)
250       {
251          if (t instanceof junit.framework.AssertionFailedError)
252             throw (junit.framework.AssertionFailedError) t;
253          log.error("Error in test: " + t, t);
254          throw new Exception JavaDoc(t.getMessage());
255       }
256       finally
257       {
258          try
259          {
260             pub1.close();
261          }
262          catch (Exception JavaDoc ex)
263          {
264          }
265       }
266    }
267
268    public void runClientIDPreconfTest() throws Exception JavaDoc
269    {
270       TopicWorker pub1 = null;
271       try
272       {
273          drainTopic();
274          int ic = 5;
275          // Publish
276
IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
277          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
278          pub1.setUser("john", "needle");
279          pub1.connect();
280          pub1.publish();
281          Assert.assertEquals("Client did not get a valid clientID", "DurableSubscriberExample", pub1.connection
282                .getClientID());
283          pub1.close();
284       }
285       catch (Throwable JavaDoc t)
286       {
287          if (t instanceof junit.framework.AssertionFailedError)
288             throw (junit.framework.AssertionFailedError) t;
289          log.error("Error in test: " + t, t);
290          throw new Exception JavaDoc(t.getMessage());
291       }
292       finally
293       {
294          try
295          {
296             pub1.close();
297          }
298          catch (Exception JavaDoc ex)
299          {
300          }
301       }
302    }
303
304    public void runClientIDSetTest() throws Exception JavaDoc
305    {
306       TopicWorker pub1 = null;
307       try
308       {
309          drainTopic();
310          int ic = 5;
311          // Publish
312
IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
313          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
314          pub1.setClientID("myId");
315          pub1.connect();
316          pub1.publish();
317          Assert.assertEquals("Client did not get a valid clientID", "myId", pub1.connection.getClientID());
318          pub1.close();
319       }
320       catch (Throwable JavaDoc t)
321       {
322          if (t instanceof junit.framework.AssertionFailedError)
323             throw (junit.framework.AssertionFailedError) t;
324          log.error("Error in test: " + t, t);
325          throw new Exception JavaDoc(t.getMessage());
326       }
327       finally
328       {
329          try
330          {
331             pub1.close();
332          }
333          catch (Exception JavaDoc ex)
334          {
335          }
336       }
337    }
338
339    public void runClientIDSetSteelPreconf() throws Exception JavaDoc
340    {
341       TopicWorker pub1 = null;
342       try
343       {
344          drainTopic();
345          int ic = 5;
346          // Publish
347
IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
348          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
349          pub1.setClientID("DurableSubscriberExample");
350          Exception JavaDoc e = null;
351          try
352          {
353             pub1.connect();
354          }
355          catch (Exception JavaDoc ex)
356          {
357             e = ex;
358          }
359          log.debug(e);
360          Assert.assertTrue("Setting a clientID wich is preconfigured did not throw correct exception",
361                e instanceof javax.jms.InvalidClientIDException JavaDoc);
362          pub1.close();
363       }
364       catch (Throwable JavaDoc t)
365       {
366          if (t instanceof junit.framework.AssertionFailedError)
367             throw (junit.framework.AssertionFailedError) t;
368          log.error("Error in test: " + t, t);
369          throw new Exception JavaDoc(t.getMessage());
370       }
371       finally
372       {
373          try
374          {
375             pub1.close();
376          }
377          catch (Exception JavaDoc ex)
378          {
379          }
380       }
381    }
382
383    public void runClientIDSetAfterInvoke() throws Exception JavaDoc
384    {
385       TopicWorker pub1 = null;
386       try
387       {
388          drainTopic();
389          int ic = 5;
390          // Publish
391
IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
392          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
393          pub1.connect();
394          pub1.publish();
395          Exception JavaDoc e = null;
396          try
397          {
398             pub1.connection.setClientID("myID");
399          }
400          catch (Exception JavaDoc ex)
401          {
402             e = ex;
403          }
404          log.debug(e);
405          Assert.assertTrue("Setting a clientID after connection is used did not throw correct exception: " + e,
406                e instanceof javax.jms.IllegalStateException JavaDoc);
407          pub1.close();
408       }
409       catch (Throwable JavaDoc t)
410       {
411          if (t instanceof junit.framework.AssertionFailedError)
412             throw (junit.framework.AssertionFailedError) t;
413          log.error("Error in test: " + t, t);
414          throw new Exception JavaDoc(t.getMessage());
415       }
416       finally
417       {
418          try
419          {
420             pub1.close();
421          }
422          catch (Exception JavaDoc ex)
423          {
424          }
425       }
426    }
427
428    /**
429     Tests to check autorization.
430     
431     Remember there are actuallt two types of fails:
432     a) You are a user, but do no belong to a group that has acl.
433     b) You belong to a group, but that group does not have acl.
434     we test the first for topics and the second for queues, by
435     configuration in jbossmq-testsuite-service.xml
436     
437     Tests that check autorization.
438     1. test valid topic publisher
439     2. test invalid topic publisher
440     3. test valid topic subscriber
441     4. test invalid topic subscriber
442     5. test valid queue sender
443     6. test invalid queue sender
444     7. test valid queue receiver
445     8. test invalid queue receiver
446     9. test valid queue browser.
447     10. test invalid queue browser
448     11. test preconf dur sub, to valid dest.
449     12. test preconf dur sub, to invalid dest.
450     13. test dyn dur sub, to valid dest.
451     14. test dyn dur sub, to valid dest.
452     */

453    public void runAuzValidTopicPublisher() throws Exception JavaDoc
454    {
455       TopicWorker pub1 = null;
456       try
457       {
458          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
459          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, 1);
460          pub1.setUser("john", "needle");
461          pub1.connect();
462          pub1.publish();
463       }
464       catch (Exception JavaDoc ex)
465       {
466          Assert.fail("Could not publish to valid destination");
467       }
468       finally
469       {
470          try
471          {
472             pub1.close();
473          }
474          catch (Exception JavaDoc ex)
475          {
476          }
477       }
478    }
479
480    public void runAuzValidTopicPublisherTransaction() throws Exception JavaDoc
481    {
482       TopicWorker pub1 = null;
483       try
484       {
485          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
486          pub1 = new TopicWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
487          pub1.setUser("john", "needle");
488          pub1.connect();
489          pub1.publish();
490       }
491       catch (Exception JavaDoc ex)
492       {
493          Assert.fail("Could not publish to valid destination");
494       }
495       finally
496       {
497          try
498          {
499             pub1.close();
500          }
501          catch (Exception JavaDoc ex)
502          {
503          }
504       }
505    }
506
507    public void runAuzInvalidTopicPublisher() throws Exception JavaDoc
508    {
509       TopicWorker pub1 = null;
510       try
511       {
512          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
513          pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, 1);
514          pub1.setUser("nobody", "nobody");
515          pub1.connect();
516          Exception JavaDoc e = null;
517          try
518          {
519             pub1.publish();
520          }
521          catch (Exception JavaDoc ex)
522          {
523             e = ex;
524          }
525          log.debug(e);
526          Assert.assertTrue("Unauz topic publishing throw wrong exception: " + e,
527                e instanceof javax.jms.JMSSecurityException JavaDoc);
528       }
529       finally
530       {
531          try
532          {
533             pub1.close();
534          }
535          catch (Exception JavaDoc ex)
536          {
537          }
538       }
539    }
540
541    public void runAuzInvalidTopicPublisherTransaction() throws Exception JavaDoc
542    {
543       TopicWorker pub1 = null;
544       try
545       {
546          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
547          pub1 = new TopicWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
548          pub1.setUser("nobody", "nobody");
549          pub1.connect();
550          Exception JavaDoc e = null;
551          try
552          {
553             pub1.publish();
554          }
555          catch (Exception JavaDoc ex)
556          {
557             e = ex;
558          }
559          log.debug(e);
560          Assert.assertTrue("Unauz topic publishing throw wrong exception: " + e,
561                e instanceof javax.jms.JMSSecurityException JavaDoc);
562       }
563       finally
564       {
565          try
566          {
567             pub1.close();
568          }
569          catch (Exception JavaDoc ex)
570          {
571          }
572       }
573    }
574
575    public void runAuzValidTopicSubscriber() throws Exception JavaDoc
576    {
577       TopicWorker sub1 = null;
578       try
579       {
580          drainTopic();
581          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
582          sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
583          sub1.setUser("john", "needle");
584          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
585          t1.start();
586          sleep(1000L);
587          Exception JavaDoc ex = sub1.getException();
588          t1.interrupt();
589          Assert.assertTrue("Autz topic subscriber did not work", ex == null);
590       }
591       finally
592       {
593          try
594          {
595             sub1.close();
596          }
597          catch (Exception JavaDoc ex)
598          {
599          }
600       }
601    }
602
603    public void runAuzValidTopicSubscriberTransaction() throws Exception JavaDoc
604    {
605       TopicWorker sub1 = null;
606       try
607       {
608          drainTopic();
609          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
610          sub1 = new TopicWorker(SUBSCRIBER, TRANS_INDIVIDUAL, f1);
611          sub1.setUser("john", "needle");
612          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
613          t1.start();
614          sleep(1000L);
615          Exception JavaDoc ex = sub1.getException();
616          t1.interrupt();
617          Assert.assertTrue("Autz topic subscriber did not work", ex == null);
618       }
619       finally
620       {
621          try
622          {
623             sub1.close();
624          }
625          catch (Exception JavaDoc ex)
626          {
627          }
628       }
629    }
630
631    public void runAuzInvalidTopicSubscriber() throws Exception JavaDoc
632    {
633       TopicWorker sub1 = null;
634       try
635       {
636          drainTopic();
637          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
638          sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
639          sub1.setUser("nobody", "nobody");
640          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
641          t1.start();
642          sleep(1000L);
643          Exception JavaDoc ex = sub1.getException();
644          t1.interrupt();
645          Assert.assertTrue("Unautz topic subscriber throw wrong exception: " + ex,
646                ex instanceof javax.jms.JMSSecurityException JavaDoc);
647       }
648       finally
649       {
650          try
651          {
652             sub1.close();
653          }
654          catch (Exception JavaDoc ex)
655          {
656          }
657       }
658    }
659
660    public void runAuzInvalidTopicSubscriberTransaction() throws Exception JavaDoc
661    {
662       TopicWorker sub1 = null;
663       try
664       {
665          drainTopic();
666          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
667          sub1 = new TopicWorker(SUBSCRIBER, TRANS_INDIVIDUAL, f1);
668          sub1.setUser("nobody", "nobody");
669          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
670          t1.start();
671          sleep(1000L);
672          Exception JavaDoc ex = sub1.getException();
673          t1.interrupt();
674          Assert.assertTrue("Unautz topic subscriber throw wrong exception: " + ex,
675                ex instanceof javax.jms.JMSSecurityException JavaDoc);
676       }
677       finally
678       {
679          try
680          {
681             sub1.close();
682          }
683          catch (Exception JavaDoc ex)
684          {
685          }
686       }
687    }
688
689    public void runAuzValidQueueSender() throws Exception JavaDoc
690    {
691       QueueWorker pub1 = null;
692       try
693       {
694          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
695          pub1 = new QueueWorker(PUBLISHER, TRANS_NONE, c1, 1);
696          pub1.setUser("john", "needle");
697          pub1.connect();
698          pub1.publish();
699       }
700       catch (Exception JavaDoc ex)
701       {
702          Assert.fail("Could not publish to valid destination");
703       }
704       finally
705       {
706          try
707          {
708             pub1.close();
709          }
710          catch (Exception JavaDoc ex)
711          {
712          }
713       }
714    }
715
716    public void runAuzValidQueueSenderTransaction() throws Exception JavaDoc
717    {
718       QueueWorker pub1 = null;
719       try
720       {
721          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
722          pub1 = new QueueWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
723          pub1.setUser("john", "needle");
724          pub1.connect();
725          pub1.publish();
726       }
727       catch (Exception JavaDoc ex)
728       {
729          Assert.fail("Could not publish to valid destination");
730       }
731       finally
732       {
733          try
734          {
735             pub1.close();
736          }
737          catch (Exception JavaDoc ex)
738          {
739          }
740       }
741    }
742
743    public void runAuzInvalidQueueSender() throws Exception JavaDoc
744    {
745       QueueWorker pub1 = null;
746       try
747       {
748          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
749          pub1 = new QueueWorker(PUBLISHER, TRANS_NONE, c1, 1);
750          pub1.setUser("nobody", "nobody");
751          pub1.connect();
752          Exception JavaDoc e = null;
753          try
754          {
755             pub1.publish();
756          }
757          catch (Exception JavaDoc ex)
758          {
759             e = ex;
760          }
761          log.debug(e);
762          Assert.assertTrue("Unauz queue publishing throw wrong exception: " + e,
763                e instanceof javax.jms.JMSSecurityException JavaDoc);
764       }
765       finally
766       {
767          try
768          {
769             pub1.close();
770          }
771          catch (Exception JavaDoc ex)
772          {
773          }
774       }
775    }
776
777    public void runAuzInvalidQueueSenderTransaction() throws Exception JavaDoc
778    {
779       QueueWorker pub1 = null;
780       try
781       {
782          IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0);
783          pub1 = new QueueWorker(PUBLISHER, TRANS_INDIVIDUAL, c1, 1);
784          pub1.setUser("nobody", "nobody");
785          pub1.connect();
786          Exception JavaDoc e = null;
787          try
788          {
789             pub1.publish();
790          }
791          catch (Exception JavaDoc ex)
792          {
793             e = ex;
794          }
795          log.debug(e);
796          Assert.assertTrue("Unauz queue publishing throw wrong exception: " + e,
797                e instanceof javax.jms.JMSSecurityException JavaDoc);
798       }
799       finally
800       {
801          try
802          {
803             pub1.close();
804          }
805          catch (Exception JavaDoc ex)
806          {
807          }
808       }
809    }
810
811    public void runAuzValidQueueReceiver() throws Exception JavaDoc
812    {
813       QueueWorker sub1 = null;
814       try
815       {
816          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
817          sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
818          sub1.setUser("john", "needle");
819          sub1.connect();
820          Exception JavaDoc ex = null;
821          try
822          {
823             sub1.get();
824          }
825          catch (Exception JavaDoc e)
826          {
827             ex = e;
828             log.error("ValidQueueReceiver got an exception: " + e, e);
829          }
830          Assert.assertTrue("Autz queue receiver did not work", ex == null);
831       }
832       finally
833       {
834          try
835          {
836             sub1.close();
837          }
838          catch (Exception JavaDoc ex)
839          {
840          }
841       }
842    }
843
844    public void runAuzValidQueueReceiverTransaction() throws Exception JavaDoc
845    {
846       QueueWorker sub1 = null;
847       try
848       {
849          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
850          sub1 = new QueueWorker(GETTER, TRANS_INDIVIDUAL, f1);
851          sub1.setUser("john", "needle");
852          sub1.connect();
853          Exception JavaDoc ex = null;
854          try
855          {
856             sub1.get();
857          }
858          catch (Exception JavaDoc e)
859          {
860             ex = e;
861             log.error("ValidQueueReceiver got an exception: " + e, e);
862          }
863          Assert.assertTrue("Autz queue receiver did not work", ex == null);
864       }
865       finally
866       {
867          try
868          {
869             sub1.close();
870          }
871          catch (Exception JavaDoc ex)
872          {
873          }
874       }
875    }
876
877    public void runAuzInvalidQueueReceiver() throws Exception JavaDoc
878    {
879       QueueWorker sub1 = null;
880       try
881       {
882          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
883          sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
884          sub1.setUser("nobody", "nobody");
885          sub1.connect();
886          Exception JavaDoc ex = null;
887          try
888          {
889             sub1.get();
890          }
891          catch (Exception JavaDoc e)
892          {
893             ex = e;
894          }
895          Assert.assertTrue("Unautz queue receiver throw wrong exception: " + ex,
896                ex instanceof javax.jms.JMSSecurityException JavaDoc);
897       }
898       finally
899       {
900          try
901          {
902             sub1.close();
903          }
904          catch (Exception JavaDoc ex)
905          {
906          }
907       }
908    }
909
910    public void runAuzInvalidQueueReceiverTransaction() throws Exception JavaDoc
911    {
912       QueueWorker sub1 = null;
913       try
914       {
915          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
916          sub1 = new QueueWorker(GETTER, TRANS_INDIVIDUAL, f1);
917          sub1.setUser("nobody", "nobody");
918          sub1.connect();
919          Exception JavaDoc ex = null;
920          try
921          {
922             sub1.get();
923          }
924          catch (Exception JavaDoc e)
925          {
926             ex = e;
927          }
928          Assert.assertTrue("Unautz queue receiver throw wrong exception: " + ex,
929                ex instanceof javax.jms.JMSSecurityException JavaDoc);
930       }
931       finally
932       {
933          try
934          {
935             sub1.close();
936          }
937          catch (Exception JavaDoc ex)
938          {
939          }
940       }
941    }
942
943    public void runAuzValidQueueBrowser() throws Exception JavaDoc
944    {
945       QueueWorker sub1 = null;
946       try
947       {
948          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
949          sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
950          sub1.setUser("john", "needle");
951          sub1.connect();
952          Exception JavaDoc ex = null;
953          try
954          {
955             sub1.browse();
956          }
957          catch (Exception JavaDoc e)
958          {
959             ex = e;
960             log.error("ValidQueueBrowser throw exception: " + e, e);
961          }
962          Assert.assertTrue("Autz queue receiver did not work", ex == null);
963       }
964       finally
965       {
966          try
967          {
968             sub1.close();
969          }
970          catch (Exception JavaDoc ex)
971          {
972          }
973       }
974    }
975
976    public void runAuzInvalidQueueBrowser() throws Exception JavaDoc
977    {
978       QueueWorker sub1 = null;
979       try
980       {
981          IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "USER_NR", 0, 1);
982          sub1 = new QueueWorker(GETTER, TRANS_NONE, f1);
983          sub1.setUser("nobody", "nobody");
984          sub1.connect();
985          Exception JavaDoc ex = null;
986          try
987          {
988             sub1.browse();
989          }
990          catch (Exception JavaDoc e)
991          {
992             ex = e;
993          }
994          Assert.assertTrue("Unautz queue receiver throw wrong exception: " + ex,
995                ex instanceof javax.jms.JMSSecurityException JavaDoc);
996       }
997       finally
998       {
999          try
1000         {
1001            sub1.close();
1002         }
1003         catch (Exception JavaDoc ex)
1004         {
1005         }
1006      }
1007   }
1008
1009   public void runValidPreconfDurSub() throws Exception JavaDoc
1010   {
1011      TopicWorker sub1 = null;
1012      TopicWorker pub1 = null;
1013      try
1014      {
1015         // Clean testarea up
1016
drainTopic();
1017         int ic = 5;
1018         // Set up a durable subscriber
1019
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "DURABLE_NR", 0, ic);
1020         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
1021         sub1.setDurable("john", "needle", "sub2");
1022         Thread JavaDoc t1 = new Thread JavaDoc(sub1);
1023         t1.start();
1024         // Let is take some time to really set up the dur sub
1025
sleep(2000);
1026         // Publish
1027
IntRangeMessageCreator c1 = new IntRangeMessageCreator("DURABLE_NR", 0);
1028         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
1029         pub1.connect();
1030         pub1.publish();
1031         Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
1032               pub1.getMessageHandled());
1033         // let sub1 have some time to handle the messages.
1034
log.debug("Sleeping for " + ((ic * 100) / 60000) + " minutes");
1035         sleep(ic * 100);
1036         Exception JavaDoc ex = sub1.getException();
1037         if (ex != null)
1038            log.error("ValidPreconfDurSub got an exception: " + ex, ex);
1039         Assert.assertTrue("ValidPreconfDurSub did not work", ex == null);
1040         Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic, sub1
1041               .getMessageHandled());
1042         t1.interrupt();
1043      }
1044      finally
1045      {
1046         try
1047         {
1048            pub1.close();
1049         }
1050         catch (Exception JavaDoc ex)
1051         {
1052         }
1053         try
1054         {
1055            // if this stops working it might be that we have become spec
1056
// compliant an do not allow unsubscribe with an open consumer.
1057
sub1.unsubscribe();
1058            sub1.close();
1059         }
1060         catch (Exception JavaDoc ex)
1061         {
1062         }
1063      }
1064   }
1065
1066   public void runInvalidPreconfDurSub() throws Exception JavaDoc
1067   {
1068      TopicWorker sub1 = null;
1069      try
1070      {
1071         // Clean testarea up
1072
TEST_TOPIC = "topic/securedTopic";
1073         //drainTopic();
1074
int ic = 5;
1075         // Set up a durable subscriber
1076
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "DURABLE_NR", 0, ic);
1077         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
1078         sub1.setDurable("john", "needle", "sub3");
1079         Thread JavaDoc t1 = new Thread JavaDoc(sub1);
1080         t1.start();
1081         // Let is take some time to really set up the dur sub
1082
sleep(2000);
1083         Exception JavaDoc ex = sub1.getException();
1084         Assert.assertTrue("InvalidPreconfDurSub did not get correct exception:" + ex,
1085               ex instanceof javax.jms.JMSSecurityException JavaDoc);
1086         t1.interrupt();
1087      }
1088      finally
1089      {
1090         try
1091         {
1092            sub1.close();
1093         }
1094         catch (Exception JavaDoc ex)
1095         {
1096         }
1097      }
1098   }
1099
1100   public void runValidDynDurSub() throws Exception JavaDoc
1101   {
1102      TopicWorker sub1 = null;
1103      TopicWorker pub1 = null;
1104      try
1105      {
1106         // Clean testarea up
1107
drainTopic();
1108         int ic = 5;
1109         // Set up a durable subscriber
1110
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "DURABLE_NR", 0, ic);
1111         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
1112         sub1.setDurable("dynsub", "dynsub", "sub4");
1113         sub1.setClientID("myId");
1114         Thread JavaDoc t1 = new Thread JavaDoc(sub1);
1115         t1.start();
1116         // Let is take some time to really set up the dur sub
1117
sleep(2000);
1118         // Publish
1119
IntRangeMessageCreator c1 = new IntRangeMessageCreator("DURABLE_NR", 0);
1120         pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic);
1121         pub1.connect();
1122         pub1.publish();
1123         Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
1124               pub1.getMessageHandled());
1125         // let sub1 have some time to handle the messages.
1126
log.debug("Sleeping for " + ((ic * 100) / 60000) + " minutes");
1127         sleep(ic * 100);
1128         Exception JavaDoc ex = sub1.getException();
1129         if (ex != null)
1130            log.error("ValidDynDurSub got an exception: " + ex, ex);
1131         Assert.assertTrue("ValidDynDurSub did not work", ex == null);
1132         Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic, sub1
1133               .getMessageHandled());
1134         t1.interrupt();
1135      }
1136      finally
1137      {
1138         try
1139         {
1140            pub1.close();
1141         }
1142         catch (Exception JavaDoc ex)
1143         {
1144         }
1145         try
1146         {
1147            // if this stops working it might be that we have become spec
1148
// compliant an do not allow unsubscribe with an open consumer.
1149
sub1.unsubscribe();
1150            sub1.close();
1151         }
1152         catch (Exception JavaDoc ex)
1153         {
1154         }
1155      }
1156   }
1157
1158   public void runInvalidDynDurSub() throws Exception JavaDoc
1159   {
1160      TopicWorker sub1 = null;
1161      try
1162      {
1163         // Clean testarea up
1164
TEST_TOPIC = "topic/securedTopic";
1165         //drainTopic();
1166
int ic = 5;
1167         // Set up a durable subscriber
1168
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class, "DURABLE_NR", 0, ic);
1169         sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1);
1170         sub1.setDurable("dynsub", "dynsub", "sub5");
1171         sub1.setClientID("myId2");
1172         Thread JavaDoc t1 = new Thread JavaDoc(sub1);
1173         t1.start();
1174         // Let is take some time to really set up the dur sub
1175
sleep(2000);
1176         Exception JavaDoc ex = sub1.getException();
1177         Assert.assertTrue("InvalidDynDurSub did not get correct exception:" + ex,
1178               ex instanceof javax.jms.JMSSecurityException JavaDoc);
1179         t1.interrupt();
1180      }
1181      finally
1182      {
1183         try
1184         {
1185            sub1.close();
1186         }
1187         catch (Exception JavaDoc ex)
1188         {
1189         }
1190      }
1191   }
1192
1193   public static junit.framework.Test suite() throws Exception JavaDoc
1194   {
1195      TestSuite suite = new TestSuite();
1196
1197      suite.addTest(new SecurityUnitTestCase("runLoginTest"));
1198      // Authentication tests
1199
suite.addTest(new SecurityUnitTestCase("runLoginNoCred"));
1200      suite.addTest(new SecurityUnitTestCase("runLoginValidCred"));
1201      suite.addTest(new SecurityUnitTestCase("runLoginInvalidPwd"));
1202      suite.addTest(new SecurityUnitTestCase("runLoginInvalidCred"));
1203      // ClientID tests
1204
suite.addTest(new SecurityUnitTestCase("runClientIDNormalTest"));
1205      suite.addTest(new SecurityUnitTestCase("runClientIDPreconfTest"));
1206      suite.addTest(new SecurityUnitTestCase("runClientIDSetTest"));
1207      suite.addTest(new SecurityUnitTestCase("runClientIDSetSteelPreconf"));
1208      suite.addTest(new SecurityUnitTestCase("runClientIDSetAfterInvoke"));
1209      // Autorization tests
1210
suite.addTest(new SecurityUnitTestCase("runAuzValidTopicPublisher"));
1211      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicPublisher"));
1212      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicSubscriber"));
1213      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicSubscriber"));
1214      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueSender"));
1215      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueSender"));
1216      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueReceiver"));
1217      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueReceiver"));
1218      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicPublisherTransaction"));
1219      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicPublisherTransaction"));
1220      suite.addTest(new SecurityUnitTestCase("runAuzValidTopicSubscriberTransaction"));
1221      suite.addTest(new SecurityUnitTestCase("runAuzInvalidTopicSubscriberTransaction"));
1222      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueSenderTransaction"));
1223      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueSenderTransaction"));
1224      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueReceiverTransaction"));
1225      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueReceiverTransaction"));
1226      suite.addTest(new SecurityUnitTestCase("runAuzValidQueueBrowser"));
1227      suite.addTest(new SecurityUnitTestCase("runAuzInvalidQueueBrowser"));
1228      suite.addTest(new SecurityUnitTestCase("runValidPreconfDurSub"));
1229      suite.addTest(new SecurityUnitTestCase("runInvalidPreconfDurSub"));
1230      suite.addTest(new SecurityUnitTestCase("runValidDynDurSub"));
1231      suite.addTest(new SecurityUnitTestCase("runInvalidDynDurSub"));
1232      //suite.addTest(new DurableSubscriberTest("testBadClient"));
1233
// Create an initializer for the test suite
1234
Test wrapper = new JBossTestSetup(suite)
1235      {
1236         protected void setUp() throws Exception JavaDoc
1237         {
1238            super.setUp();
1239            ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
1240            deploy (loader.getResource("messaging/test-destinations-service.xml").toString());
1241         }
1242         protected void tearDown() throws Exception JavaDoc
1243         {
1244            super.tearDown();
1245            // Remove all the messages created during this test
1246
getServer().invoke(new ObjectName JavaDoc("jboss.mq.destination:service=Queue,name=testQueue"),
1247                  "removeAllMessages", new Object JavaDoc[0], new String JavaDoc[0]);
1248            ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
1249            undeploy (loader.getResource("messaging/test-destinations-service.xml").toString());
1250         }
1251      };
1252
1253      return wrapper;
1254   }
1255
1256   public static void main(String JavaDoc[] args)
1257   {
1258   }
1259} // SecurityTest
1260
Popular Tags