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 ExcludedPrefixWebConstraintsUnitTestCase 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 WebResourcePermission wrp = new WebResourcePermission ("/unchecked", "GET"); 127 assertTrue("/unchecked GET", p.implies(pd, wrp)); 128 wrp = new WebResourcePermission ("/unchecked/x", "GET"); 129 assertTrue("/unchecked/x GET", p.implies(pd, wrp)); 130 131 wrp = new WebResourcePermission ("/restricted/not", "GET"); 133 assertTrue("/restricted/not GET", p.implies(pd, wrp)); 134 wrp = new WebResourcePermission ("/restricted/not/x", "GET"); 135 assertTrue("/restricted/not/x GET", p.implies(pd, wrp)); 136 wrp = new WebResourcePermission ("/restricted/not/x", "HEAD"); 137 assertTrue("/restricted/not/x HEAD", p.implies(pd, wrp)); 138 wrp = new WebResourcePermission ("/restricted/not/x", "POST"); 139 assertTrue("/restricted/not/x POST", p.implies(pd, wrp)); 140 141 wrp = new WebResourcePermission ("/", "GET"); 142 assertTrue("/ GET", p.implies(pd, wrp)); 143 wrp = new WebResourcePermission ("/other", "GET"); 144 assertTrue("/other GET", p.implies(pd, wrp)); 145 wrp = new WebResourcePermission ("/other", "HEAD"); 146 assertTrue("/other HEAD", p.implies(pd, wrp)); 147 wrp = new WebResourcePermission ("/other", "POST"); 148 assertTrue("/other POST", p.implies(pd, wrp)); 149 } 150 151 protected void setUp() throws Exception 152 { 153 WebMetaData metaData = new WebMetaData(); 154 ArrayList securityContraints = new ArrayList (); 155 addSC(securityContraints); 156 metaData.setSecurityConstraints(securityContraints); 157 158 DelegatingPolicy policy = new DelegatingPolicy(); 159 Policy.setPolicy(policy); 160 JBossPolicyConfigurationFactory pcf = new JBossPolicyConfigurationFactory(); 161 pc = pcf.getPolicyConfiguration("UncheckedPrefixWebConstraintsUnitTestCase", true); 162 WebPermissionMapping.createPermissions(metaData, pc); 163 pc.commit(); 164 System.out.println(policy.listContextPolicies()); 165 PolicyContext.setContextID("UncheckedPrefixWebConstraintsUnitTestCase"); 166 } 167 168 private void addSC(List securityContraints) 169 { 170 WebSecurityMetaData wsmd = new WebSecurityMetaData(); 172 securityContraints.add(wsmd); 173 WebSecurityMetaData.WebResourceCollection wrc = wsmd.addWebResource("No Access"); 175 wrc.addPattern("/excluded/*"); 176 wrc.addPattern("/restricted/get-only/excluded/*"); 177 wrc.addPattern("/restricted/post-only/excluded/*"); 178 wrc.addPattern("/restricted/any/excluded/*"); 179 wrc.addPattern("/excluded/*"); 180 181 wrc = wsmd.addWebResource("No Access"); 183 wrc.addPattern("/restricted/*"); 184 wrc.addHttpMethod("DELETE"); 185 wrc.addHttpMethod("PUT"); 186 wrc.addHttpMethod("HEAD"); 187 wrc.addHttpMethod("OPTIONS"); 188 wrc.addHttpMethod("TRACE"); 189 wrc.addHttpMethod("GET"); 190 wrc.addHttpMethod("POST"); 191 192 wsmd.setExcluded(true); 193 wsmd.setTransportGuarantee("NONE"); 194 195 wsmd = new WebSecurityMetaData(); 196 securityContraints.add(wsmd); 197 wrc = wsmd.addWebResource("Unchecked"); 198 wrc.addPattern("/unchecked/*"); 199 wrc.addPattern("/restricted/not/*"); 200 wrc.addHttpMethod("DELETE"); 201 wrc.addHttpMethod("PUT"); 202 wrc.addHttpMethod("HEAD"); 203 wrc.addHttpMethod("OPTIONS"); 204 wrc.addHttpMethod("TRACE"); 205 wrc.addHttpMethod("GET"); 206 wrc.addHttpMethod("POST"); 207 208 wsmd.setUnchecked(true); 210 wsmd.setTransportGuarantee("NONE"); 212 } 213 214 } 215 | Popular Tags |