1 22 package org.jboss.test.web.security.jacc; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.HashMap ; 27 import java.security.Policy ; 28 import java.security.ProtectionDomain ; 29 import javax.security.jacc.PolicyConfiguration ; 30 import javax.security.jacc.WebResourcePermission ; 31 import javax.security.jacc.PolicyContext ; 32 33 import junit.framework.TestCase; 34 import org.jboss.metadata.WebMetaData; 35 import org.jboss.metadata.WebSecurityMetaData; 36 import org.jboss.metadata.SecurityRoleMetaData; 37 import org.jboss.web.WebPermissionMapping; 38 import org.jboss.security.jacc.DelegatingPolicy; 39 import org.jboss.security.jacc.JBossPolicyConfigurationFactory; 40 import org.jboss.security.SimplePrincipal; 41 42 226 public class WebConstraintsUnitTestCase extends TestCase 227 { 228 private PolicyConfiguration pc; 229 230 public void testUnchecked() throws Exception 231 { 232 Policy p = Policy.getPolicy(); 233 SimplePrincipal[] caller = null; 234 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 235 WebResourcePermission wrp = new WebResourcePermission ("/unchecked", "GET"); 237 assertTrue("/unchecked GET", p.implies(pd, wrp)); 238 wrp = new WebResourcePermission ("/unchecked/x", "GET"); 239 assertTrue("/unchecked/x GET", p.implies(pd, wrp)); 240 241 wrp = new WebResourcePermission ("/restricted/not", "GET"); 243 assertTrue("/restricted/not GET", p.implies(pd, wrp)); 244 wrp = new WebResourcePermission ("/restricted/not/x", "GET"); 245 assertTrue("/restricted/not/x GET", p.implies(pd, wrp)); 246 wrp = new WebResourcePermission ("/restricted/not/x", "HEAD"); 247 assertTrue("/restricted/not/x HEAD", p.implies(pd, wrp)); 248 wrp = new WebResourcePermission ("/restricted/not/x", "POST"); 249 assertTrue("/restricted/not/x POST", p.implies(pd, wrp)); 250 251 wrp = new WebResourcePermission ("/", "GET"); 252 assertTrue("/ GET", p.implies(pd, wrp)); 253 wrp = new WebResourcePermission ("/other", "GET"); 254 assertTrue("/other GET", p.implies(pd, wrp)); 255 wrp = new WebResourcePermission ("/other", "HEAD"); 256 assertTrue("/other HEAD", p.implies(pd, wrp)); 257 wrp = new WebResourcePermission ("/other", "POST"); 258 assertTrue("/other POST", p.implies(pd, wrp)); 259 } 260 261 public void testGetAccess() throws Exception 262 { 263 Policy p = Policy.getPolicy(); 264 SimplePrincipal[] caller = {new SimplePrincipal("GetRole")}; 265 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 266 267 WebResourcePermission wrp = new WebResourcePermission ("/restricted/get-only", "GET"); 269 assertTrue("/restricted/get-only GET", p.implies(pd, wrp)); 270 271 wrp = new WebResourcePermission ("/restricted/get-only/x", "GET"); 272 assertTrue("/restricted/get-only/x GET", p.implies(pd, wrp)); 273 274 wrp = new WebResourcePermission ("/restricted/any/x", "GET"); 276 assertTrue("/restricted/any/x GET", p.implies(pd, wrp)); 277 278 wrp = new WebResourcePermission ("/restricted/get-only/x", "POST"); 280 assertFalse("/restricted/get-only/x POST", p.implies(pd, wrp)); 281 282 wrp = new WebResourcePermission ("/restricted/post-only/x", "GET"); 284 assertFalse("/restricted/post-only/x GET", p.implies(pd, wrp)); 285 286 wrp = new WebResourcePermission ("/restricted/get-only/excluded/x", "GET"); 288 assertFalse("/restricted/get-only/excluded/x GET", p.implies(pd, wrp)); 289 290 caller = new SimplePrincipal[]{new SimplePrincipal("OtherRole")}; 291 pd = new ProtectionDomain (null, null, null, caller); 292 wrp = new WebResourcePermission ("/restricted/get-only", "GET"); 294 assertFalse("/restricted/get-only GET", p.implies(pd, wrp)); 295 wrp = new WebResourcePermission ("/restricted/get-only/x", "GET"); 296 assertFalse("/restricted/get-only/x GET", p.implies(pd, wrp)); 297 298 303 wrp = new WebResourcePermission ("/restricted/any/x", "GET"); 304 assertFalse("/restricted/any/x GET", p.implies(pd, wrp)); 305 } 306 307 309 public void testExcludedAccess() throws Exception 310 { 311 Policy p = Policy.getPolicy(); 312 SimplePrincipal[] caller = {new SimplePrincipal("GetRole")}; 313 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 314 315 WebResourcePermission wrp = new WebResourcePermission ("/excluded/x", "GET"); 316 assertFalse("/excluded/x GET", p.implies(pd, wrp)); 317 wrp = new WebResourcePermission ("/excluded/x", "OPTIONS"); 318 assertFalse("/excluded/x OPTIONS", p.implies(pd, wrp)); 319 wrp = new WebResourcePermission ("/excluded/x", "HEAD"); 320 assertFalse("/excluded/x HEAD", p.implies(pd, wrp)); 321 wrp = new WebResourcePermission ("/excluded/x", "POST"); 322 assertFalse("/excluded/x POST", p.implies(pd, wrp)); 323 324 wrp = new WebResourcePermission ("/restricted/", "GET"); 325 assertFalse("/restricted/ GET", p.implies(pd, wrp)); 326 wrp = new WebResourcePermission ("/restricted/", "OPTIONS"); 327 assertFalse("/restricted/ OPTIONS", p.implies(pd, wrp)); 328 wrp = new WebResourcePermission ("/restricted/", "HEAD"); 329 assertFalse("/restricted/ HEAD", p.implies(pd, wrp)); 330 wrp = new WebResourcePermission ("/restricted/", "POST"); 331 assertFalse("/restricted/ POST", p.implies(pd, wrp)); 332 333 wrp = new WebResourcePermission ("/restricted/get-only/excluded/x", "GET"); 334 assertFalse("/restricted/get-only/excluded/x GET", p.implies(pd, wrp)); 335 wrp = new WebResourcePermission ("/restricted/get-only/excluded/x", "OPTIONS"); 336 assertFalse("/restricted/get-only/excluded/x OPTIONS", p.implies(pd, wrp)); 337 wrp = new WebResourcePermission ("/restricted/get-only/excluded/x", "HEAD"); 338 assertFalse("/restricted/get-only/excluded/x HEAD", p.implies(pd, wrp)); 339 wrp = new WebResourcePermission ("/restricted/get-only/excluded/x", "POST"); 340 assertFalse("/restricted/get-only/excluded/x POST", p.implies(pd, wrp)); 341 342 wrp = new WebResourcePermission ("/restricted/post-only/excluded/x", "GET"); 343 assertFalse("/restricted/post-only/excluded/x GET", p.implies(pd, wrp)); 344 wrp = new WebResourcePermission ("/restricted/post-only/excluded/x", "OPTIONS"); 345 assertFalse("/restricted/post-only/excluded/x OPTIONS", p.implies(pd, wrp)); 346 wrp = new WebResourcePermission ("/restricted/post-only/excluded/x", "HEAD"); 347 assertFalse("/restricted/post-only/excluded/x HEAD", p.implies(pd, wrp)); 348 wrp = new WebResourcePermission ("/restricted/post-only/excluded/x", "POST"); 349 assertFalse("/restricted/post-only/excluded/x POST", p.implies(pd, wrp)); 350 351 wrp = new WebResourcePermission ("/restricted/any/excluded/x", "GET"); 352 assertFalse("/restricted/any/excluded/x GET", p.implies(pd, wrp)); 353 wrp = new WebResourcePermission ("/restricted/any/excluded/x", "OPTIONS"); 354 assertFalse("/restricted/any/excluded/x OPTIONS", p.implies(pd, wrp)); 355 wrp = new WebResourcePermission ("/restricted/any/excluded/x", "HEAD"); 356 assertFalse("/restricted/any/excluded/x HEAD", p.implies(pd, wrp)); 357 wrp = new WebResourcePermission ("/restricted/any/excluded/x", "POST"); 358 assertFalse("/restricted/any/excluded/x POST", p.implies(pd, wrp)); 359 } 360 361 364 public void testPostAccess() throws Exception 365 { 366 Policy p = Policy.getPolicy(); 367 SimplePrincipal[] caller = {new SimplePrincipal("PostRole")}; 368 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 369 370 WebResourcePermission wrp = new WebResourcePermission ("/restricted/post-only/", "POST"); 371 assertTrue("/restricted/post-only/ POST", p.implies(pd, wrp)); 372 wrp = new WebResourcePermission ("/restricted/post-only/x", "POST"); 373 assertTrue("/restricted/post-only/x POST", p.implies(pd, wrp)); 374 375 wrp = new WebResourcePermission ("/restricted/any/x", "POST"); 377 assertTrue("/restricted/any/x POST", p.implies(pd, wrp)); 378 379 wrp = new WebResourcePermission ("/restricted/post-only/excluded/x", "POST"); 381 assertFalse("/restricted/post-only/excluded/x POST", p.implies(pd, wrp)); 382 383 wrp = new WebResourcePermission ("/restricted/post-only/x", "GET"); 385 assertFalse("/restricted/post-only/excluded/x GET", p.implies(pd, wrp)); 386 wrp = new WebResourcePermission ("/restricted/get-only/x", "POST"); 388 assertFalse("/restricted/get-only/x POST", p.implies(pd, wrp)); 389 390 caller = new SimplePrincipal[]{new SimplePrincipal("OtherRole")}; 392 pd = new ProtectionDomain (null, null, null, caller); 393 394 wrp = new WebResourcePermission ("/restricted/post-only", "POST"); 396 assertFalse("/restricted/post-only POST", p.implies(pd, wrp)); 397 wrp = new WebResourcePermission ("/restricted/post-only/x", "POST"); 398 assertFalse("/restricted/post-only/x POST", p.implies(pd, wrp)); 399 400 } 401 402 protected void setUp() throws Exception 403 { 404 WebMetaData metaData = new WebMetaData(); 405 ArrayList securityContraints = new ArrayList (); 406 addExcluded(securityContraints); 407 addAllAccessSC(securityContraints); 408 addRestrictedGetSC(securityContraints); 409 addExcludedGetSC(securityContraints); 410 addRestrictedPostSC(securityContraints); 411 addExcludedPostSC(securityContraints); 412 addRestrictedAnySC(metaData, securityContraints); 413 addUnrestrictedSC(securityContraints); 414 metaData.setSecurityConstraints(securityContraints); 415 416 DelegatingPolicy policy = new DelegatingPolicy(); 417 Policy.setPolicy(policy); 418 JBossPolicyConfigurationFactory pcf = new JBossPolicyConfigurationFactory(); 419 pc = pcf.getPolicyConfiguration("WebConstraintsUnitTestCase", true); 420 WebPermissionMapping.createPermissions(metaData, pc); 421 pc.commit(); 422 System.out.println(policy.listContextPolicies()); 423 PolicyContext.setContextID("WebConstraintsUnitTestCase"); 424 } 425 426 private void addExcluded(List securityContraints) 427 { 428 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 430 securityContraints.add(wsmd); 431 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("No Access"); 433 wrc.addPattern("/excluded/*"); 434 wrc.addPattern("/restricted/get-only/excluded/*"); 435 wrc.addPattern("/restricted/post-only/excluded/*"); 436 wrc.addPattern("/restricted/any/excluded/*"); 437 wrc.addPattern("/excluded/*"); 438 439 wrc = wsmd.addWebResource("No Access"); 441 wrc.addPattern("/restricted/*"); 442 wrc.addHttpMethod("DELETE"); 443 wrc.addHttpMethod("PUT"); 444 wrc.addHttpMethod("HEAD"); 445 wrc.addHttpMethod("OPTIONS"); 446 wrc.addHttpMethod("TRACE"); 447 wrc.addHttpMethod("GET"); 448 wrc.addHttpMethod("POST"); 449 450 wsmd.setExcluded(true); 452 453 wsmd.setTransportGuarantee("NONE"); 455 } 456 457 private void addAllAccessSC(List securityContraints) 458 { 459 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 460 securityContraints.add(wsmd); 461 462 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("All Access"); 464 wrc.addPattern("/unchecked/*"); 465 wrc.addHttpMethod("DELETE"); 466 wrc.addHttpMethod("PUT"); 467 wrc.addHttpMethod("HEAD"); 468 wrc.addHttpMethod("OPTIONS"); 469 wrc.addHttpMethod("TRACE"); 470 wrc.addHttpMethod("GET"); 471 wrc.addHttpMethod("POST"); 472 473 wsmd.setUnchecked(true); 475 wsmd.setTransportGuarantee("NONE"); 477 } 478 479 private void addRestrictedGetSC(List securityContraints) 480 { 481 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 482 securityContraints.add(wsmd); 483 484 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Restricted Access - Get Only"); 486 wrc.addPattern("/restricted/get-only/*"); 487 wrc.addHttpMethod("GET"); 488 489 wsmd.addRole("GetRole"); 491 wsmd.setTransportGuarantee("NONE"); 493 } 494 495 private void addExcludedGetSC(List securityContraints) 496 { 497 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 498 securityContraints.add(wsmd); 499 500 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Restricted Access - Get Only"); 502 wrc.addPattern("/restricted/get-only/*"); 503 wrc.addHttpMethod("DELETE"); 504 wrc.addHttpMethod("PUT"); 505 wrc.addHttpMethod("HEAD"); 506 wrc.addHttpMethod("OPTIONS"); 507 wrc.addHttpMethod("TRACE"); 508 wrc.addHttpMethod("POST"); 509 510 wsmd.setExcluded(true); 512 wsmd.setTransportGuarantee("NONE"); 514 } 515 516 private void addRestrictedPostSC(List securityContraints) 517 { 518 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 519 securityContraints.add(wsmd); 520 521 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Restricted Access - Post Only"); 523 wrc.addPattern("/restricted/post-only/*"); 524 wrc.addHttpMethod("POST"); 525 526 wsmd.addRole("PostRole"); 528 wsmd.setTransportGuarantee("NONE"); 530 } 531 532 private void addExcludedPostSC(List securityContraints) 533 { 534 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 535 securityContraints.add(wsmd); 536 537 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Restricted Access - Post Only"); 539 wrc.addPattern("/restricted/post-only/*"); 540 wrc.addHttpMethod("DELETE"); 541 wrc.addHttpMethod("PUT"); 542 wrc.addHttpMethod("HEAD"); 543 wrc.addHttpMethod("OPTIONS"); 544 wrc.addHttpMethod("TRACE"); 545 wrc.addHttpMethod("GET"); 546 547 wsmd.setExcluded(true); 549 wsmd.setTransportGuarantee("NONE"); 551 } 552 553 private void addRestrictedAnySC(WebMetaData wmd, List securityContraints) 554 { 555 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 556 securityContraints.add(wsmd); 557 558 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Restricted Access - Any"); 560 wrc.addPattern("/restricted/any/*"); 561 wrc.addHttpMethod("DELETE"); 562 wrc.addHttpMethod("PUT"); 563 wrc.addHttpMethod("HEAD"); 564 wrc.addHttpMethod("OPTIONS"); 565 wrc.addHttpMethod("TRACE"); 566 wrc.addHttpMethod("GET"); 567 wrc.addHttpMethod("POST"); 568 569 wsmd.addRole("*"); 571 HashMap roles = new HashMap (); 573 roles.put("GetRole", new SecurityRoleMetaData("GetRole")); 574 roles.put("PostRole", new SecurityRoleMetaData("PostRole")); 575 wmd.setSecurityRoles(roles); 576 wsmd.setTransportGuarantee("NONE"); 578 } 579 580 private void addUnrestrictedSC(List securityContraints) 581 { 582 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 583 securityContraints.add(wsmd); 584 585 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Restricted Access - Any"); 587 wrc.addPattern("/restricted/not/*"); 588 wrc.addHttpMethod("DELETE"); 589 wrc.addHttpMethod("PUT"); 590 wrc.addHttpMethod("HEAD"); 591 wrc.addHttpMethod("OPTIONS"); 592 wrc.addHttpMethod("TRACE"); 593 wrc.addHttpMethod("GET"); 594 wrc.addHttpMethod("POST"); 595 596 wsmd.setUnchecked(true); 598 wsmd.setTransportGuarantee("NONE"); 600 } 601 } 602 | Popular Tags |