1 22 package org.jboss.test.web.security.jacc; 23 24 import java.security.Policy ; 25 import java.security.ProtectionDomain ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import javax.security.jacc.PolicyConfiguration ; 29 import javax.security.jacc.PolicyContext ; 30 import javax.security.jacc.WebResourcePermission ; 31 import javax.security.jacc.WebUserDataPermission ; 32 33 import junit.framework.TestCase; 34 import org.jboss.metadata.WebMetaData; 35 import org.jboss.metadata.WebSecurityMetaData; 36 import org.jboss.security.SimplePrincipal; 37 import org.jboss.security.jacc.DelegatingPolicy; 38 import org.jboss.security.jacc.JBossPolicyConfigurationFactory; 39 import org.jboss.web.WebPermissionMapping; 40 41 46 public class QualifiedPatternUnitTestCase extends TestCase 47 { 48 private PolicyConfiguration pc; 49 50 public void testUnchecked() throws Exception 51 { 52 Policy p = Policy.getPolicy(); 53 SimplePrincipal[] caller = null; 54 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 55 56 WebResourcePermission wrp = new WebResourcePermission ("/a", "GET"); 57 assertTrue("/a GET", p.implies(pd, wrp)); 58 wrp = new WebResourcePermission ("/a", "POST"); 59 assertTrue("/a POST", p.implies(pd, wrp)); 60 61 caller = new SimplePrincipal[]{new SimplePrincipal("R1")}; 62 pd = new ProtectionDomain (null, null, null, caller); 63 wrp = new WebResourcePermission ("/a/x", "GET"); 64 assertTrue("/a/x GET", p.implies(pd, wrp)); 65 wrp = new WebResourcePermission ("/a/x", "POST"); 66 boolean implied = p.implies(pd, wrp); 67 assertTrue("/a/x POST", implied); 68 wrp = new WebResourcePermission ("/b/x", "GET"); 69 assertTrue("/b/x GET", p.implies(pd, wrp)); 70 wrp = new WebResourcePermission ("/b/x", "POST"); 71 assertTrue("/b/x POST", p.implies(pd, wrp)); 72 wrp = new WebResourcePermission ("/b/x", "DELETE"); 73 assertFalse("/b/x DELETE", p.implies(pd, wrp)); 74 75 wrp = new WebResourcePermission ("/a/x.asp", "GET"); 76 assertTrue("/a/x.asp GET", p.implies(pd, wrp)); 77 wrp = new WebResourcePermission ("/a/x.asp", "POST"); 78 assertTrue("/a/x.asp POST", p.implies(pd, wrp)); 79 80 WebUserDataPermission wudp = new WebUserDataPermission ("/a/*:/a", "GET:CONFIDENTIAL"); 81 assertTrue("/a/*:/a GET:CONFIDENTIAL", p.implies(pd, wudp)); 82 wudp = new WebUserDataPermission ("/a/*:/a", "GET:CONFIDENTIAL"); 83 assertTrue("/b/*:/b GET,POST:CONFIDENTIAL", p.implies(pd, wudp)); 84 85 } 86 87 protected void setUp() throws Exception 88 { 89 WebMetaData metaData = new WebMetaData(); 90 ArrayList securityContraints = new ArrayList (); 91 addSC1(securityContraints); 92 addSC2(securityContraints); 93 metaData.setSecurityConstraints(securityContraints); 94 95 DelegatingPolicy policy = new DelegatingPolicy(); 96 Policy.setPolicy(policy); 97 JBossPolicyConfigurationFactory pcf = new JBossPolicyConfigurationFactory(); 98 pc = pcf.getPolicyConfiguration("QualifiedPatternUnitTestCase", true); 99 WebPermissionMapping.createPermissions(metaData, pc); 100 pc.commit(); 101 System.out.println(policy.listContextPolicies()); 102 PolicyContext.setContextID("QualifiedPatternUnitTestCase"); 103 } 104 105 123 private void addSC1(List securityContraints) 124 { 125 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 126 securityContraints.add(wsmd); 127 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("sc1.c1"); 129 wrc.addPattern("/a/*"); 130 wrc.addPattern("/b/*"); 131 wrc.addPattern("/a"); 132 wrc.addPattern("/b"); 133 wrc.addHttpMethod("DELETE"); 134 wrc.addHttpMethod("PUT"); 135 136 wrc = wsmd.addWebResource("sc1.c2"); 137 wrc.addPattern("*.asp"); 138 139 wsmd.setExcluded(true); 140 } 141 142 163 private void addSC2(List securityContraints) 164 { 165 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 166 securityContraints.add(wsmd); 167 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("sc2.c1"); 169 wrc.addPattern("/a/*"); 170 wrc.addPattern("/b/*"); 171 wrc.addHttpMethod("GET"); 172 173 wrc = wsmd.addWebResource("sc2.c2"); 174 wrc.addPattern("/b/*"); 175 wrc.addHttpMethod("POST"); 176 177 wsmd.addRole("R1"); 178 wsmd.setTransportGuarantee("CONFIDENTIAL"); 179 } 180 } 181 | Popular Tags |