1 22 package org.jboss.test.cluster.test; 23 24 import java.io.IOException ; 25 import java.net.HttpURLConnection ; 26 27 import javax.management.ObjectName ; 28 29 import junit.framework.Test; 30 31 import org.apache.commons.httpclient.Cookie; 32 import org.apache.commons.httpclient.Header; 33 import org.apache.commons.httpclient.HttpClient; 34 import org.apache.commons.httpclient.HttpState; 35 import org.apache.commons.httpclient.methods.GetMethod; 36 import org.apache.commons.httpclient.methods.PostMethod; 37 import org.jboss.cache.Fqn; 38 import org.jboss.jmx.adaptor.rmi.RMIAdaptor; 39 import org.jboss.test.JBossClusteredTestCase; 40 41 47 public class ScopedTestCase 48 extends BaseTest 49 { 50 protected String setUrl; 51 protected String getUrl; 52 protected String modifyUrl; 53 protected String modifyNoSetUrl; 54 protected String removeUrl; 55 protected String invalidateUrl; 56 protected String clearUrl; 57 protected String attrListUrl; 58 protected String bindUrl_; 59 protected String protectedUrl_; 60 protected String securityCheckUrl_; 61 protected String loginFormUrl_; 62 protected String setSecuritySubjectUrl_; 63 protected String getSecuritySubjectUrl_; 64 protected String warName_; 65 protected String setUrlBase_; 66 protected String getUrlBase_; 67 protected String modifyUrlBase_; 68 protected String modifyNoSetUrlBase_; 69 protected String removeUrlBase_; 70 protected String invalidateUrlBase_; 71 protected String clearUrlBase_; 72 protected String bindUrlBase_; 73 protected String attrListUrlBase_; 74 protected String protectedUrlBase_; 75 protected String securityCheckUrlBase_; 76 protected String loginFormUrlBase_; 77 protected String setSecuritySubjectUrlBase_; 78 protected String getSecuritySubjectUrlBase_; 79 80 private ObjectName warObjectName; 81 private String warFqn_; 82 83 public ScopedTestCase(String name) 84 { 85 super(name); 86 warName_ = "/http-scoped/"; 87 setUrlBase_ = "setSession.jsp"; 88 getUrlBase_ = "getAttribute.jsp"; 89 modifyUrlBase_ = "modifyAttribute.jsp"; 90 modifyNoSetUrlBase_ = "modifyAttributeNoSet.jsp"; 91 removeUrlBase_ = "removeAttribute.jsp"; 92 invalidateUrlBase_ = "invalidateSession.jsp"; 93 clearUrlBase_ = "clearCache.jsp"; 94 bindUrlBase_ = "bindSession.jsp?Binding="; 95 protectedUrlBase_ = "index.html"; 96 securityCheckUrlBase_ = "j_security_check"; 97 loginFormUrlBase_ = "login.html"; 98 attrListUrlBase_ = "attributeNames.jsp"; 99 setSecuritySubjectUrlBase_ = "setSecuritySubject.jsp"; 100 getSecuritySubjectUrlBase_ = "getSecuritySubject.jsp"; 101 102 concatenate(); 103 } 104 105 protected void concatenate() 106 { 107 setUrl = warName_ +setUrlBase_; 108 getUrl = warName_ +getUrlBase_; 109 modifyUrl = warName_ +modifyUrlBase_; 110 modifyNoSetUrl = warName_ +modifyNoSetUrlBase_; 111 removeUrl = warName_ +removeUrlBase_; 112 invalidateUrl = warName_ +invalidateUrlBase_; 113 clearUrl = warName_ +clearUrlBase_; 114 bindUrl_ = warName_ + bindUrlBase_; 115 protectedUrl_ = warName_ + protectedUrlBase_; 116 securityCheckUrl_ = warName_ + securityCheckUrlBase_; 117 loginFormUrl_ = warName_ + loginFormUrlBase_; 118 attrListUrl = warName_ + attrListUrlBase_; 119 setSecuritySubjectUrl_ = warName_ + setSecuritySubjectUrlBase_; 120 getSecuritySubjectUrl_ = warName_ + getSecuritySubjectUrlBase_; 121 } 122 123 protected String getWarName() 124 { 125 return "http-scoped"; 126 } 127 128 public static Test suite() throws Exception 129 { 130 Test t1 = JBossClusteredTestCase.getDeploySetup(ScopedTestCase.class, 131 "http-scoped.war"); 132 return t1; 133 } 134 135 protected void setUp() throws Exception 136 { 137 super.setUp(); 138 139 if (warObjectName == null) 140 { 141 String oname = "jboss.web:J2EEApplication=none,J2EEServer=none," + 142 "j2eeType=WebModule,name=//localhost/" + getWarName(); 143 warObjectName = new ObjectName (oname); 144 145 RMIAdaptor[] adaptors = getAdaptors(); 146 147 Object [] names = {"JSESSION", "localhost", getWarName() }; 148 Fqn fqn = new Fqn(names); 149 warFqn_ = fqn.toString(); 150 } 151 } 152 153 protected void tearDown() throws Exception 154 { 155 super.tearDown(); 157 } 158 159 164 public void testNonPrimitiveGet() 165 throws Exception 166 { 167 String attr = ""; 168 getLog().debug("Enter testNonPrimitiveGet"); 169 170 getLog().debug(setUrl + ":::::::" + getUrl); 171 172 HttpClient client = new HttpClient(); 174 175 makeGet(client, baseURL0_ +setUrl); 177 178 attr = makeGet(client, baseURL0_ +getUrl); 181 182 sleepThread(DEFAULT_SLEEP); 183 184 setCookieDomainToThisServer(client, servers_[1]); 186 String attr2 = makeGet(client, baseURL1_ +getUrl); 187 188 assertEquals("Get attribute should be but is ", attr, attr2); 189 getLog().debug("Exit testNonPrimitiveGet"); 190 } 191 192 197 public void testNonPrimitiveModify() 198 throws Exception 199 { 200 String attr = ""; 201 getLog().debug("Enter testNonPrimitiveModify"); 202 203 getLog().debug(setUrl + ":::::::" + getUrl); 204 205 HttpClient client = new HttpClient(); 207 208 makeGet(client, baseURL0_ +setUrl); 210 211 String attrOld = makeGet(client, baseURL0_ +getUrl); 213 214 makeGet(client, baseURL0_ +modifyUrl); 216 217 attr = makeGet(client, baseURL0_ +getUrl); 219 220 sleepThread(DEFAULT_SLEEP); 221 222 setCookieDomainToThisServer(client, servers_[1]); 224 String attr2 = makeGet(client, baseURL1_ +getUrl); 225 226 assertNotSame("Old attribute should be different from new one.", 228 attrOld, attr); 229 assertEquals("Attributes should be the same", attr, attr2); 230 getLog().debug("Exit testNonPrimitiveModify"); 231 } 232 233 238 public void testNonPrimitiveRepeatedModify() 239 throws Exception 240 { 241 String attr = ""; 242 getLog().info("Enter testNonPrimitiveRepeatedModify"); 243 244 getLog().debug(setUrl + ":::::::" + getUrl); 245 246 HttpClient client = new HttpClient(); 248 249 makeGet(client, baseURL0_ +setUrl); 251 252 String attrOld = makeGetWithState(client, baseURL0_ +getUrl); 254 255 makeGet(client, baseURL0_ +modifyUrl); 257 258 attr = makeGetWithState(client, baseURL0_ +getUrl); 260 261 sleepThread(DEFAULT_SLEEP); 262 263 setCookieDomainToThisServer(client, servers_[1]); 265 String attr2 = makeGetWithState(client, baseURL1_ +getUrl); 266 267 assertNotSame("Old attribute should be different from new one.", 269 attrOld, attr); 270 assertEquals("Attributes should be the same", attr, attr2); 271 272 273 makeGet(client, baseURL1_ +modifyUrl); 275 276 attr = makeGetWithState(client, baseURL1_ +getUrl); 278 279 sleepThread(DEFAULT_SLEEP); 280 281 setCookieDomainToThisServer(client, servers_[0]); 283 attr2 = makeGetWithState(client, baseURL0_ +getUrl); 284 285 assertEquals("Attributes should be the same after second modify", attr, attr2); 287 getLog().debug("Exit testNonPrimitiveRepeatedModify"); 288 } 289 290 295 public void testNonPrimitiveRemove() 296 throws Exception 297 { 298 getLog().debug("Enter testNonPrimitiveRemove"); 299 300 getLog().debug(setUrl + ":::::::" + getUrl); 301 302 HttpClient client = new HttpClient(); 304 305 makeGet(client, baseURL0_ +setUrl); 307 308 makeGet(client, baseURL0_ +modifyUrl); 310 311 makeGet(client, baseURL0_ +getUrl); 313 314 String attrList = makeGet(client, baseURL0_ +attrListUrl); 316 317 assertTrue("TEST_PERSON is an attribute", attrList.indexOf("TEST_PERSON") > -1); 318 319 sleepThread(DEFAULT_SLEEP); 320 321 setCookieDomainToThisServer(client, servers_[1]); 323 makeGet(client, baseURL1_ +getUrl); 325 String attrList1 = makeGet(client, baseURL1_ +attrListUrl); 327 328 assertTrue("TEST_PERSON is an attribute on server1", attrList1.indexOf("TEST_PERSON") > -1); 329 330 makeGet(client, baseURL1_ +removeUrl); 332 makeGetFailed(client, baseURL1_ +getUrl); 334 335 attrList1 = makeGet(client, baseURL1_ +attrListUrl); 337 338 assertTrue("TEST_PERSON should not be an attribute", attrList1.indexOf("TEST_PERSON") == -1); 339 340 sleepThread(DEFAULT_SLEEP); 341 setCookieDomainToThisServer(client, servers_[0]); 343 makeGetFailed(client, baseURL0_ +getUrl); 345 346 attrList = makeGet(client, baseURL0_ +attrListUrl); 348 349 assertTrue("TEST_PERSON is not an attribute on server0", attrList.indexOf("TEST_PERSON") == -1); 350 351 getLog().debug("Exit testNonPrimitiveRemove"); 352 } 353 354 360 public void testNonPrimitiveModifyFromAlternativeNode() 361 throws Exception 362 { 363 String attr = ""; 364 getLog().debug("Enter testNonPrimitiveModifyFromAlternativeNode"); 365 366 getLog().debug(setUrl + ":::::::" + getUrl); 367 368 HttpClient client = new HttpClient(); 370 371 makeGet(client, baseURL0_ +setUrl); 373 374 String attrOld = makeGet(client, baseURL0_ +getUrl); 376 377 sleepThread(DEFAULT_SLEEP); 378 379 setCookieDomainToThisServer(client, servers_[1]); 381 String attrOld1 = makeGet(client, baseURL1_ +getUrl); 382 383 assertEquals("Attributes should be the same", attrOld, attrOld1); 384 385 makeGet(client, baseURL1_ +modifyUrl); 387 String attr2 = makeGet(client, baseURL1_ +getUrl); 388 389 sleepThread(400); 390 391 setCookieDomainToThisServer(client, servers_[0]); 393 attr = makeGet(client, baseURL0_ +getUrl); 394 395 assertEquals("Attributes should be the same", attr, attr2); 396 getLog().debug("Exit testNonPrimitiveModifyModifyFromAlternativeNode"); 397 } 398 399 404 public void testInvalidate() 405 throws Exception 406 { 407 getLog().debug("Enter testInvalidate"); 408 409 getLog().debug(setUrl + ":::::::" + getUrl); 410 411 invalidate(); 412 413 getLog().debug("Exit testInvalidate"); 414 } 415 416 public void testSessionBindingEvent() 417 throws Exception 418 { 419 String attr = ""; 420 getLog().debug("Enter testSessionBindingEvent"); 421 422 HttpClient client = new HttpClient(); 424 425 attr = makeGet(client, baseURL0_ + bindUrl_ + "new"); 428 assertTrue("Got OK when binding a new listener", 429 (attr != null && attr.indexOf("OK") >= 0 ) ); 430 431 attr = makeGet(client, baseURL0_ + bindUrl_ + "rebind"); 434 assertTrue("Got OK when rebinding an existing listener", 435 (attr != null && attr.indexOf("OK") >= 0 ) ); 436 437 attr = makeGet(client, baseURL0_ + bindUrl_ + "replace"); 440 assertTrue("Got OK when replacing a listener", 441 (attr != null && attr.indexOf("OK") >= 0 ) ); 442 443 attr = makeGet(client, baseURL0_ + bindUrl_ + "remove"); 446 assertTrue("Got OK when removing a listener", 447 (attr != null && attr.indexOf("OK") >= 0 ) ); 448 } 449 450 public void testExcludeSecuritySubject() throws Exception 451 { 452 getLog().debug("Enter testExcludeSecuritySubject"); 453 454 getLog().debug(setSecuritySubjectUrl_ + ":::::::" + getSecuritySubjectUrl_); 455 456 HttpClient client = new HttpClient(); 458 459 makeGet(client, baseURL0_ +setSecuritySubjectUrl_); 461 462 String attrOrig = makeGet(client, baseURL0_ +getSecuritySubjectUrl_); 464 assertTrue("javax.security.auth.subject available locally", 465 attrOrig.indexOf("javax.security.auth.Subject") > -1); 466 467 sleepThread(DEFAULT_SLEEP); 468 469 setCookieDomainToThisServer(client, servers_[1]); 471 String attrRepl = makeGet(client, baseURL1_ +getSecuritySubjectUrl_); 472 assertTrue("javax.security.Subject did not replicate", attrRepl.indexOf("java.lang.String") > -1); 473 } 474 475 480 public void testIsNew() throws Exception 481 { 482 getLog().debug("Enter testIsNew"); 483 484 getLog().debug(setUrl + ":::::::" + getUrl); 485 486 HttpClient client = new HttpClient(); 488 489 makeGet(client, baseURL0_ +setUrl); 491 492 sleepThread(DEFAULT_SLEEP); 493 494 setCookieDomainToThisServer(client, servers_[1]); 496 assertFalse("Session is not new", checkNew(client, baseURL1_ + getUrl)); 497 498 sleepThread(DEFAULT_SLEEP); 499 500 setCookieDomainToThisServer(client, servers_[0]); 502 assertFalse("Session is not new", checkNew(client, baseURL0_ + getUrl)); 503 } 504 505 506 514 protected boolean checkNew(HttpClient client, String url) 515 { 516 getLog().info("checkNew(): trying to get from url " +url); 517 518 GetMethod method = new GetMethod(url); 519 int responseCode = 0; 520 try 521 { 522 responseCode = client.executeMethod(method); 523 } catch (IOException e) 524 { 525 e.printStackTrace(); 526 fail("HttpClient executeMethod fails." +e.toString()); 527 } 528 assertTrue("Get OK with url: " +url + " responseCode: " +responseCode 529 , responseCode == HttpURLConnection.HTTP_OK); 530 531 Header hdr = method.getResponseHeader("X-SessionIsNew"); 532 assertNotNull("Got X-SessionIsNew header", hdr); 533 String value = hdr.getValue(); 534 assertNotNull("Got non-nullX-SessionIsNew header", value); 535 536 return Boolean.valueOf(value).booleanValue(); 537 } 538 539 protected void invalidate() throws Exception 540 { 541 HttpClient client = new HttpClient(); 543 544 makeGet(client, baseURL0_ +setUrl); 546 547 String attr0 = makeGet(client, baseURL0_ +getUrl); 549 550 sleepThread(DEFAULT_SLEEP); 551 552 setCookieDomainToThisServer(client, servers_[1]); 554 String attr1 = makeGet(client, baseURL1_ + getUrl); 555 556 assertEquals("attributes match", attr0, attr1); 557 558 makeGet(client, baseURL1_ +invalidateUrl); 560 561 sleepThread(DEFAULT_SLEEP + 200); 563 setCookieDomainToThisServer(client, servers_[0]); 565 makeGetFailed(client, baseURL0_ + getUrl); 567 } 568 569 protected void clear() throws Exception 570 { 571 HttpClient client = new HttpClient(); 573 574 makeGet(client, baseURL0_ +clearUrl); 576 } 577 578 587 public void badtestFormAuthFailover() throws Exception 588 { 589 log.info("+++ testFormAuthFailover"); 590 591 HttpClient client = new HttpClient(); 593 594 String body = makeGet(client, baseURL0_ + protectedUrl_); 595 if( body.indexOf("j_security_check") < 0 ) 596 fail("get of "+protectedUrl_+" not redirected to login page"); 597 598 HttpState state = client.getState(); 599 Cookie[] cookies = state.getCookies(); 600 String sessionID = null; 601 for(int c = 0; c < cookies.length; c ++) 602 { 603 Cookie k = cookies[c]; 604 if( k.getName().equalsIgnoreCase("JSESSIONID") ) 605 sessionID = k.getValue(); 606 } 607 log.debug("Saw JSESSIONID="+sessionID); 608 609 PostMethod formPost = new PostMethod(baseURL0_ + securityCheckUrl_); 611 formPost.addRequestHeader("Referer", baseURL0_ + loginFormUrl_); 612 formPost.addParameter("j_username", "admin"); 613 formPost.addParameter("j_password", "admin"); 614 int responseCode = client.executeMethod(formPost.getHostConfiguration(), 615 formPost, state); 616 String response = formPost.getStatusText(); 617 log.debug("responseCode="+responseCode+", response="+response); 618 assertTrue("Saw HTTP_MOVED_TEMP("+responseCode+")", 619 responseCode == HttpURLConnection.HTTP_MOVED_TEMP); 620 621 body = makeGet(client, baseURL0_ + protectedUrl_); 623 if( body.indexOf("j_security_check") > 0 ) 624 fail("get of "+baseURL0_ + protectedUrl_+" redirected to login page"); 625 626 setCookieDomainToThisServer(client, servers_[1]); 628 629 sleepThread(DEFAULT_SLEEP); 630 631 body = makeGet(client, baseURL1_ + protectedUrl_); 633 if( body.indexOf("j_security_check") > 0 ) 634 fail("get of "+baseURL1_ + protectedUrl_+" redirected to login page"); 635 } 636 637 642 public void XtestSessionPassivationWMaxIdle() throws Exception 644 { 645 getLog().debug("Enter testSessionPassivationWMaxActive"); 646 647 getLog().debug(setUrl + ":::::::" + getUrl); 648 649 HttpClient client = new HttpClient(); 651 makeGet(client, baseURL0_ +setUrl); 653 654 String attr0 = makeGet(client, baseURL0_ +getUrl); 656 657 String sessionID = getSessionID(client, servers_[0]); 659 sessionID = stripJvmRoute(sessionID); 661 String sessionFqn = "/JSESSION/localhost" + warName_ + sessionID; 662 663 sleepThread(DEFAULT_SLEEP); 664 665 setCookieDomainToThisServer(client, servers_[1]); 667 String attr1 = makeGet(client, baseURL1_ + getUrl); 668 669 assertEquals("attributes match", attr0, attr1); 671 672 sleepThread(11000); 677 678 RMIAdaptor[] adaptors = getAdaptors(); 679 getLog().debug("Session in the cache = " + SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn)); 680 assertNull("Session is passivated therefore it is not in the cache...", SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn)); 682 683 setCookieDomainToThisServer(client, servers_[0]); 686 String attr2 = makeGet(client, baseURL0_ + getUrl); 687 688 assertEquals("attribute match after activation", attr0, attr2); 689 } 690 691 696 public void XtestSessionPassivationWMaxActive() throws Exception 698 { 699 getLog().debug("Enter testSessionPassivationWMaxActive"); 700 701 getLog().debug(setUrl + ":::::::" + getUrl); 702 703 RMIAdaptor[] adaptors = getAdaptors(); 704 HttpClient client = new HttpClient(); 706 makeGet(client, baseURL0_ +setUrl); 708 709 String attr0 = makeGet(client, baseURL0_ +getUrl); 711 712 String sessionID = getSessionID(client, servers_[0]); 714 sessionID = stripJvmRoute(sessionID); 716 String sessionFqn = "/JSESSION/localhost" + warName_ + sessionID; 717 718 sleepThread(DEFAULT_SLEEP); 719 720 setCookieDomainToThisServer(client, servers_[1]); 722 String attr1 = makeGet(client, baseURL1_ + getUrl); 723 724 assertEquals("attributes match", attr0, attr1); 726 727 getLog().debug("current active sessions = " + SessionTestUtil.getSessionIds(adaptors[0], warFqn_)); 730 int numberOfActiveSessions = SessionTestUtil.getSessionIds(adaptors[0], warFqn_).size(); 731 HttpClient[] clients = new HttpClient[(20 - numberOfActiveSessions + 1)]; 732 String [] attrs = new String [clients.length]; 733 for (int i = 0; i < clients.length; i++) 734 { 735 clients[i] = new HttpClient(); 736 makeGet(clients[i], baseURL0_ +setUrl); 737 attrs[i] = makeGet(clients[i], baseURL0_ + getUrl); 738 this.setCookieDomainToThisServer(clients[i], servers_[1]); 740 } 741 getLog().debug("to reach max active sessions we needed to create " + clients.length + " clients"); 742 743 getLog().debug("Session in the cache = " + SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn)); 744 assertNull("Session is passivated therefore it is not in the cache...", SessionTestUtil.getSessionVersion(adaptors[0], sessionFqn)); 746 747 setCookieDomainToThisServer(client, servers_[0]); 750 String attr2 = makeGet(client, baseURL0_ + getUrl); 751 752 assertEquals("attribute match after activation", attr0, attr2); 753 } 754 } 755 | Popular Tags |