1 22 package org.jboss.test.security.test.xacml; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 29 import org.jboss.test.JBossTestCase; 30 31 import com.sun.xacml.ConfigurationStore; 32 import com.sun.xacml.PDP; 33 import com.sun.xacml.PDPConfig; 34 import com.sun.xacml.ParsingException; 35 import com.sun.xacml.ctx.RequestCtx; 36 import com.sun.xacml.ctx.ResponseCtx; 37 import com.sun.xacml.ctx.Result; 38 import com.sun.xacml.ctx.Status; 39 40 42 48 public class XACMLUnitTestCase extends JBossTestCase 49 { 50 57 private int firstTest = 1; 58 private int numberOfTests = 17; 59 60 private boolean debug = false; 62 63 public XACMLUnitTestCase(String name) 64 { 65 super(name); 66 } 67 68 public void testPDPConstruction() throws Exception 69 { 70 assertNotNull("PDP != null", getBasicPDP()); 71 } 72 73 public void testPDPResponse() throws Exception 74 { 75 for(int i=firstTest; i<=numberOfTests;i++) 76 { 77 String [] policyFiles = new String [] {getPolicyFile(i)}; 78 PDP pdp = new PDP(new PDPConfig(XACMLUtil.getAttributeFinder(), 79 XACMLUtil.getPolicyFinder( policyFiles), null)); 80 assertNotNull("PDP != null", pdp); 81 ResponseCtx first = processRequest(pdp,getRequestFile(i)); 82 assertNotNull("Response != null", first); 83 XACMLUtil.logResponseCtxToSystemOut(first, debug); 85 ResponseCtx second = ResponseCtx.getInstance(new FileInputStream (getResponseFile(i))); 86 try 87 { 88 XACMLUtil.assertEquals(first,second); 89 } 90 catch(Exception e) 91 { 92 Exception enew = new Exception ("Test#"+i+"::"+e.getMessage()); 93 enew.initCause(e); 94 throw enew; 95 } 96 } 97 } 98 99 104 private PDP getBasicPDP() throws Exception 105 { 106 String p = "security/xacml/basicConfig.xml"; 107 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 108 URL url = tcl.getResource(p); 109 File file = new File (url.getPath()); 110 ConfigurationStore store = new ConfigurationStore(file); 111 store.useDefaultFactories(); 112 return new PDP(store.getDefaultPDPConfig()); 113 } 114 115 122 private ResponseCtx processRequest(PDP pdp, String requestFile) throws Exception 123 { 124 ResponseCtx response = null; 125 126 try 127 { 128 response = pdp.evaluate(RequestCtx.getInstance(new FileInputStream (requestFile))); 129 } 130 catch(ParsingException pse) 131 { 132 response = getSyntaxErrorResponseCtx(); 133 } 134 return response; 135 } 136 137 142 private String getPolicyFile(int num) throws Exception 143 { 144 String p1 = "security/xacml/test"+num+"/policy.xml"; 145 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 146 URL url = tcl.getResource(p1); 147 assertNotNull("policy file " + p1 + " null",url); 148 return url.getPath(); 149 } 150 151 156 private String getRequestFile(int num) throws Exception 157 { 158 String p1 = "security/xacml/test"+num+"/request.xml"; 159 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 160 URL url = tcl.getResource(p1); 161 assertNotNull("request file " + p1 + " null",url); 162 return url.getPath(); 163 } 164 165 170 private String getResponseFile(int num) throws Exception 171 { 172 String p1 = "security/xacml/test"+num+"/response.xml"; 173 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 174 URL url = tcl.getResource(p1); 175 assertNotNull("response file " + p1 + " != null",url); 176 return url.getPath(); 177 } 178 179 183 private ResponseCtx getSyntaxErrorResponseCtx() 184 { 185 ArrayList code = new ArrayList (); 186 code.add(Status.STATUS_SYNTAX_ERROR); 187 Status status = new Status(code); 188 189 return new ResponseCtx(new Result(Result.DECISION_INDETERMINATE, 190 status)); 191 } 192 } 193 | Popular Tags |