| 1 22 package org.jboss.test.jbossmq.test; 23 24 import javax.management.ObjectName ; 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 37 public class SecurityUnitTestCase extends MQBase 38 { 39 public SecurityUnitTestCase(String name) 40 { 41 super(name); 42 } 43 44 public void runLoginTest() throws Exception  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 .class, "USER_NR", 0, ic); 53 sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1); 54 sub1.setUser("john", "needle"); 55 Thread t1 = new Thread (sub1); 56 t1.start(); 57 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 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 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 (t.getMessage()); 78 } 79 finally 80 { 81 try 82 { 83 if (sub1 != null) 84 sub1.close(); 85 } 86 catch (Exception ex) 87 { 88 } 89 try 90 { 91 if (pub1 != null) 92 pub1.close(); 93 } 94 catch (Exception ex) 95 { 96 } 97 } 98 } 99 100 107 public void runLoginNoCred() throws Exception  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 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 ex) 127 { 128 } 129 } 130 } 131 132 public void runLoginValidCred() throws Exception  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 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 ex) 153 { 154 } 155 } 156 } 157 158 public void runLoginInvalidPwd() throws Exception  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 e = null; 167 try 168 { 169 pub1.connect(); 170 } 171 catch (Exception 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 ); 178 } 179 finally 180 { 181 try 182 { 183 pub1.close(); 184 } 185 catch (Exception ex) 186 { 187 } 188 } 189 } 190 191 public void runLoginInvalidCred() throws Exception  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 e = null; 200 try 201 { 202 pub1.connect(); 203 } 204 catch (Exception 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 ); 211 } 212 finally 213 { 214 try 215 { 216 pub1.close(); 217 } 218 catch (Exception ex) 219 { 220 } 221 } 222 } 223 224 234 public void runClientIDNormalTest() throws Exception  235 { 236 TopicWorker pub1 = null; 237 try 238 { 239 drainTopic(); 240 int ic = 5; 241 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 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 (t.getMessage()); 255 } 256 finally 257 { 258 try 259 { 260 pub1.close(); 261 } 262 catch (Exception ex) 263 { 264 } 265 } 266 } 267 268 public void runClientIDPreconfTest() throws Exception  269 { 270 TopicWorker pub1 = null; 271 try 272 { 273 drainTopic(); 274 int ic = 5; 275 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 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 (t.getMessage()); 291 } 292 finally 293 { 294 try 295 { 296 pub1.close(); 297 } 298 catch (Exception ex) 299 { 300 } 301 } 302 } 303 304 public void runClientIDSetTest() throws Exception  305 { 306 TopicWorker pub1 = null; 307 try 308 { 309 drainTopic(); 310 int ic = 5; 311 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 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 (t.getMessage()); 326 } 327 finally 328 { 329 try 330 { 331 pub1.close(); 332 } 333 catch (Exception ex) 334 { 335 } 336 } 337 } 338 339 public void runClientIDSetSteelPreconf() throws Exception  340 { 341 TopicWorker pub1 = null; 342 try 343 { 344 drainTopic(); 345 int ic = 5; 346 IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0); 348 pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic); 349 pub1.setClientID("DurableSubscriberExample"); 350 Exception e = null; 351 try 352 { 353 pub1.connect(); 354 } 355 catch (Exception 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 ); 362 pub1.close(); 363 } 364 catch (Throwable 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 (t.getMessage()); 370 } 371 finally 372 { 373 try 374 { 375 pub1.close(); 376 } 377 catch (Exception ex) 378 { 379 } 380 } 381 } 382 383 public void runClientIDSetAfterInvoke() throws Exception  384 { 385 TopicWorker pub1 = null; 386 try 387 { 388 drainTopic(); 389 int ic = 5; 390 IntRangeMessageCreator c1 = new IntRangeMessageCreator("USER_NR", 0); 392 pub1 = new TopicWorker(PUBLISHER, TRANS_NONE, c1, ic); 393 pub1.connect(); 394 pub1.publish(); 395 Exception e = null; 396 try 397 { 398 pub1.connection.setClientID("myID"); 399 } 400 catch (Exception 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 ); 407 pub1.close(); 408 } 409 catch (Throwable 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 (t.getMessage()); 415 } 416 finally 417 { 418 try 419 { 420 pub1.close(); 421 } 422 catch (Exception ex) 423 { 424 } 425 } 426 } 427 428 453 public void runAuzValidTopicPublisher() throws Exception  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 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 ex) 475 { 476 } 477 } 478 } 479 480 public void runAuzValidTopicPublisherTransaction() throws Exception  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 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 ex) 502 { 503 } 504 } 505 } 506 507 public void runAuzInvalidTopicPublisher() throws Exception  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 e = null; 517 try 518 { 519 pub1.publish(); 520 } 521 catch (Exception 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 ); 528 } 529 finally 530 { 531 try 532 { 533 pub1.close(); 534 } 535 catch (Exception ex) 536 { 537 } 538 } 539 } 540 541 public void runAuzInvalidTopicPublisherTransaction() throws Exception  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 e = null; 551 try 552 { 553 pub1.publish(); 554 } 555 catch (Exception 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 ); 562 } 563 finally 564 { 565 try 566 { 567 pub1.close(); 568 } 569 catch (Exception ex) 570 { 571 } 572 } 573 } 574 575 public void runAuzValidTopicSubscriber() throws Exception  576 { 577 TopicWorker sub1 = null; 578 try 579 { 580 drainTopic(); 581 IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message .class, "USER_NR", 0, 1); 582 sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1); 583 sub1.setUser("john", "needle"); 584 Thread t1 = new Thread (sub1); 585 t1.start(); 586 sleep(1000L); 587 Exception 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 ex) 598 { 599 } 600 } 601 } 602 603 public void runAuzValidTopicSubscriberTransaction() throws Exception  604 { 605 TopicWorker sub1 = null; 606 try 607 { 608 drainTopic(); 609 IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message .class, "USER_NR", 0, 1); 610 sub1 = new TopicWorker(SUBSCRIBER, TRANS_INDIVIDUAL, f1); 611 sub1.setUser("john", "needle"); 612 Thread t1 = new Thread (sub1); 613 t1.start(); 614 sleep(1000L); 615 Exception 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 ex) 626 { 627 } 628 } 629 } 630 631 public void runAuzInvalidTopicSubscriber() throws Exception  632 { 633 TopicWorker sub1 = null; 634 try 635 { 636 drainTopic(); 637 IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message .class, "USER_NR", 0, 1); 638 sub1 = new TopicWorker(SUBSCRIBER, TRANS_NONE, f1); 639 sub1.setUser("nobody", "nobody"); 640 Thread t1 = new Thread (sub1); 641 t1.start(); 642 sleep(1000L); 643 Exception ex = sub1.getException(); 644 t1.interrupt(); 645 Assert.assertTrue("Unautz topic subscriber throw wrong exception: " + ex, 646 ex instanceof javax.jms.JMSSecurityException ); 647 } 648 finally 649 { 650 try 651 { 652 sub1.close(); 653 } 654 catch (Exception ex) 655 { 656 } 657 } 658 } 659 660 public void runAuzInvalidTopicSubscriberTransaction() throws Exception  661 { 662 TopicWorker sub1 = null; 663 try 664 { 665 drainTopic(); 666 IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message .class, "USER_NR", 0, 1); 667 sub1 = new TopicWorker(SUBSCRIBER, TRANS_INDIVIDUAL, f1); 668 sub1.setUser("nobody", "nobody"); 669 Thread t1 = new Thread (sub1); 670 t1.start(); 671 sleep(1000L); 672 Exception ex = sub1.getException(); 673 t1.interrupt(); 674 Assert.assertTrue("Unautz topic subscriber throw wrong exception: " + ex, 675 ex instanceof javax.jms.JMSSecurityException ); 676 } 677 finally 678 { 679 try 680 { 681 sub1.close(); 682 } 683 catch (Exception ex) 684 { 685 } 686 } 687 } 688 689 public void runAuzValidQueueSender() throws Exception  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 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 ex) 711 { 712 } 713 } 714 } 715 716 public void runAuzValidQueueSenderTransaction() throws Exception  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 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 ex) 738 { 739 } 740 } 741 } 742 743 public void runAuzInvalidQueueSender() throws Exception  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 e = null; 753 try 754 { 755 pub1.publish(); 756 } 757 catch (Exception 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 ); 764 } 765 finally 766 { 767 try 768 { 769 pub1.close(); 770 } 771 catch (Exception ex) 772 { 773 } 774 } 775 } 776 777 public void runAuzInvalidQueueSenderTransaction() throws Exception  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 e = null; 787 try 788 { 789 pub1.publish(); 790 } 791 catch (Exception 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 ); 798 } 799 finally 800 { 801 try 802 { 803 pub1.close(); 804 } 805 catch (Exception ex) 806 { 807 } 808 } 809 } 810 811 public void runAuzValidQueueReceiver() throws Exception  812 { 813 QueueWorker sub1 = null; 814 try 815 { 816 IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message .class, "USER_NR", 0, 1); 817 sub1 = new QueueWorker(GETTER, TRANS_NONE, f1); 818 sub1.setUser("john", "needle"); 819 sub1.connect(); 820 Exception ex = null; 821 try 822 { 823 sub1.get(); 824 } 825 catch (Exception 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 ex) 839 { 840 } 841 } 842 } 843 844 public void runAuzValidQueueReceiverTransaction() throws Exception  845 { 846 QueueWorker sub1 = null; 847 try 848 { 849 IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message .class, "USER_NR", 0, 1); 850 sub1 = new QueueWorker(GETTER, TRANS_INDIVIDUAL, f1); 851 sub1.setUser("john", "needle"); 852 sub1.connect(); 853 Exception ex = null; 854 try 855 { 856 sub1.get(); 857 } 858 catch (Exception
|