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 116 public class UncheckedPrefixWebConstraintsUnitTestCase extends TestCase 117 { 118 private PolicyConfiguration pc; 119 120 public void testUncheckedPrefix() throws Exception 121 { 122 Policy p = Policy.getPolicy(); 123 SimplePrincipal[] caller = null; 124 ProtectionDomain pd = new ProtectionDomain (null, null, null, caller); 125 126 WebResourcePermission wrp = new WebResourcePermission ("/restricted/post-only/x", "GET"); 128 assertFalse("/restricted/post-only/x GET", p.implies(pd, wrp)); 129 wrp = new WebResourcePermission ("/restricted/post-only/x", "POST"); 130 assertFalse("/restricted/post-only/x POST", p.implies(pd, wrp)); 131 132 caller = new SimplePrincipal[]{new SimplePrincipal("PostRole")}; 133 pd = new ProtectionDomain (null, null, null, caller); 134 wrp = new WebResourcePermission ("/restricted/post-only/x", "GET"); 135 assertFalse("/restricted/post-only/x GET", p.implies(pd, wrp)); 136 wrp = new WebResourcePermission ("/restricted/post-only/x", "POST"); 137 assertTrue("/restricted/post-only/x POST", p.implies(pd, wrp)); 138 139 } 140 141 protected void setUp() throws Exception 142 { 143 WebMetaData metaData = new WebMetaData(); 144 ArrayList securityContraints = new ArrayList (); 145 addSC(securityContraints); 146 metaData.setSecurityConstraints(securityContraints); 147 148 DelegatingPolicy policy = new DelegatingPolicy(); 149 Policy.setPolicy(policy); 150 JBossPolicyConfigurationFactory pcf = new JBossPolicyConfigurationFactory(); 151 pc = pcf.getPolicyConfiguration("UncheckedPrefixWebConstraintsUnitTestCase", true); 152 WebPermissionMapping.createPermissions(metaData, pc); 153 pc.commit(); 154 System.out.println(policy.listContextPolicies()); 155 PolicyContext.setContextID("UncheckedPrefixWebConstraintsUnitTestCase"); 156 } 157 158 private void addSC(List securityContraints) 159 { 160 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 161 securityContraints.add(wsmd); 162 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("Excluded"); 164 wrc.addPattern("/restricted/post-only/excluded/*"); 165 wrc.addPattern("/*"); 166 167 wsmd.setExcluded(true); 169 170 wsmd.setTransportGuarantee("NONE"); 172 173 wsmd = new WebSecurityMetaData(); 174 securityContraints.add(wsmd); 175 wrc = wsmd.addWebResource("Restricted POST"); 177 wrc.addPattern("/restricted/post-only/*"); 178 wrc.addHttpMethod("POST"); 179 wsmd.addRole("PostRole"); 180 wsmd.setTransportGuarantee("NONE"); 181 182 wsmd = new WebSecurityMetaData(); 183 securityContraints.add(wsmd); 184 wrc = wsmd.addWebResource("Excluded POST"); 186 wrc.addPattern("/restricted/post-only/*"); 187 wrc.addHttpMethod("DELETE"); 188 wrc.addHttpMethod("PUT"); 189 wrc.addHttpMethod("HEAD"); 190 wrc.addHttpMethod("OPTIONS"); 191 wrc.addHttpMethod("TRACE"); 192 wrc.addHttpMethod("GET"); 193 wsmd.setExcluded(true); 194 wsmd.setTransportGuarantee("NONE"); 195 } 196 197 } 198 | Popular Tags |