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 32 import junit.framework.TestCase; 33 import org.jboss.metadata.WebMetaData; 34 import org.jboss.metadata.WebSecurityMetaData; 35 import org.jboss.security.SimplePrincipal; 36 import org.jboss.security.jacc.DelegatingPolicy; 37 import org.jboss.security.jacc.JBossPolicyConfigurationFactory; 38 import org.jboss.web.WebPermissionMapping; 39 40 45 public class UncheckedExactWebConstraintsUnitTestCase extends TestCase 46 { 47 private PolicyConfiguration pc; 48 49 public void testUncheckedExact() throws Exception 50 { 51 Policy p = Policy.getPolicy(); 52 SimplePrincipal[] caller = null; 53 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 54 55 WebResourcePermission wrp = new WebResourcePermission ("/protected/exact/get/roleA", "GET"); 56 assertFalse("/protected/exact/get/roleA GET", p.implies(pd, wrp)); 57 wrp = new WebResourcePermission ("/protected/exact/get/roleA", "POST"); 58 assertFalse("/protected/exact/get/roleA POST", p.implies(pd, wrp)); 59 60 caller = new SimplePrincipal[]{new SimplePrincipal("RoleA")}; 61 wrp = new WebResourcePermission ("/protected/exact/get/roleA", "GET"); 62 assertFalse("/protected/exact/get/roleA GET", p.implies(pd, wrp)); 63 wrp = new WebResourcePermission ("/protected/exact/get/roleA", "POST"); 64 assertFalse("/protected/exact/get/roleA POST", p.implies(pd, wrp)); 65 66 caller = new SimplePrincipal[]{new SimplePrincipal("RoleB")}; 67 pd = new ProtectionDomain (null, null, null, caller); 68 wrp = new WebResourcePermission ("/protected/exact/get/roleA", "GET"); 69 assertFalse("/protected/exact/get/roleA GET", p.implies(pd, wrp)); 70 wrp = new WebResourcePermission ("/protected/exact/get/roleA", "POST"); 71 assertTrue("/protected/exact/get/roleA POST", p.implies(pd, wrp)); 72 } 73 74 protected void setUp() throws Exception 75 { 76 WebMetaData metaData = new WebMetaData(); 77 ArrayList securityContraints = new ArrayList (); 78 addProtectedASC(securityContraints); 79 addProtectedBSC(securityContraints); 80 metaData.setSecurityConstraints(securityContraints); 81 82 DelegatingPolicy policy = new DelegatingPolicy(); 83 Policy.setPolicy(policy); 84 JBossPolicyConfigurationFactory pcf = new JBossPolicyConfigurationFactory(); 85 pc = pcf.getPolicyConfiguration("UncheckedWebConstraintsUnitTestCase", true); 86 WebPermissionMapping.createPermissions(metaData, pc); 87 pc.commit(); 88 System.out.println(policy.listContextPolicies()); 89 PolicyContext.setContextID("UncheckedWebConstraintsUnitTestCase"); 90 } 91 92 107 private void addProtectedASC(List securityContraints) 108 { 109 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 110 securityContraints.add(wsmd); 111 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("exact, get method, roleA"); 113 wrc.addPattern("/protected/exact/get/roleA"); 114 wrc.addHttpMethod("GET"); 115 116 wsmd.addRole("RoleA"); 118 119 wsmd.setTransportGuarantee("NONE"); 121 } 122 123 140 private void addProtectedBSC(List securityContraints) 141 { 142 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 143 securityContraints.add(wsmd); 144 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("exact, get method, roleA verifier"); 146 wrc.addPattern("/protected/exact/get/roleA"); 147 wrc.addHttpMethod("POST"); 148 wrc.addHttpMethod("PUT"); 149 wrc.addHttpMethod("HEAD"); 150 wrc.addHttpMethod("TRACE"); 151 wrc.addHttpMethod("OPTIONS"); 152 wrc.addHttpMethod("DELETE"); 153 154 wsmd.addRole("RoleB"); 156 } 157 } 158 | Popular Tags |