KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > xacml > XACMLUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test.xacml;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
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 //$Id: XACMLUnitTestCase.java 45725 2006-06-21 17:19:15Z asaldhana $
41

42 /**
43  * Unit Tests for the XACML Integration
44  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
45  * @since May 26, 2006
46  * @version $Revision: 45725 $
47  */

48 public class XACMLUnitTestCase extends JBossTestCase
49 {
50    /**
51     * There are basic xacml conformance tests in the resources folder(security/xacml)
52     * with the format testX where X is an integer in (firstTest,numberOfTests}.
53     * If you need to run a particular test - make both these variables to be
54     * the number of the test. So to run test6, both firstTest=6 and
55     * numberOfTests=6
56     */

57    private int firstTest = 1;
58    private int numberOfTests = 17;
59    
60    //True: Response will be dumped to System.out
61
private boolean debug = false;
62    
63    public XACMLUnitTestCase(String JavaDoc name)
64    {
65       super(name);
66    }
67
68    public void testPDPConstruction() throws Exception JavaDoc
69    {
70       assertNotNull("PDP != null", getBasicPDP());
71    }
72    
73    public void testPDPResponse() throws Exception JavaDoc
74    {
75       for(int i=firstTest; i<=numberOfTests;i++)
76       {
77          String JavaDoc[] policyFiles = new String JavaDoc[] {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          //Print out the response to the System.Out
84
XACMLUtil.logResponseCtxToSystemOut(first, debug);
85          ResponseCtx second = ResponseCtx.getInstance(new FileInputStream JavaDoc(getResponseFile(i)));
86          try
87          {
88             XACMLUtil.assertEquals(first,second);
89          }
90          catch(Exception JavaDoc e)
91          {
92             Exception JavaDoc enew = new Exception JavaDoc("Test#"+i+"::"+e.getMessage());
93             enew.initCause(e);
94             throw enew;
95          }
96       }
97    }
98    
99    /**
100     * Obtain a very basic PDP
101     * @return
102     * @throws Exception
103     */

104    private PDP getBasicPDP() throws Exception JavaDoc
105    {
106       String JavaDoc p = "security/xacml/basicConfig.xml";
107       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
108       URL JavaDoc url = tcl.getResource(p);
109       File JavaDoc file = new File JavaDoc(url.getPath());
110       ConfigurationStore store = new ConfigurationStore(file);
111       store.useDefaultFactories();
112       return new PDP(store.getDefaultPDPConfig());
113    }
114    
115    /**
116     * Ask the PDP to evaluate the input request file
117     * @param pdp
118     * @param requestFile
119     * @return
120     * @throws Exception
121     */

122    private ResponseCtx processRequest(PDP pdp, String JavaDoc requestFile) throws Exception JavaDoc
123    {
124       ResponseCtx response = null;
125       
126       try
127       {
128          response = pdp.evaluate(RequestCtx.getInstance(new FileInputStream JavaDoc(requestFile)));
129       }
130       catch(ParsingException pse)
131       {
132          response = getSyntaxErrorResponseCtx();
133       }
134       return response;
135    }
136    
137    /**
138     * Get the String that represents the temp file
139     * for the Policy 1
140     * @return
141     */

142    private String JavaDoc getPolicyFile(int num) throws Exception JavaDoc
143    {
144       String JavaDoc p1 = "security/xacml/test"+num+"/policy.xml";
145       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
146       URL JavaDoc url = tcl.getResource(p1);
147       assertNotNull("policy file " + p1 + " null",url);
148       return url.getPath();
149    }
150    
151    /**
152     * Get the String that represents the file
153     * for the Request File
154     * @return
155     */

156    private String JavaDoc getRequestFile(int num) throws Exception JavaDoc
157    {
158       String JavaDoc p1 = "security/xacml/test"+num+"/request.xml";
159       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
160       URL JavaDoc url = tcl.getResource(p1);
161       assertNotNull("request file " + p1 + " null",url);
162       return url.getPath();
163    }
164    
165    /**
166     * Get the String that represents the file
167     * for the Request File
168     * @return
169     */

170    private String JavaDoc getResponseFile(int num) throws Exception JavaDoc
171    {
172       String JavaDoc p1 = "security/xacml/test"+num+"/response.xml";
173       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
174       URL JavaDoc url = tcl.getResource(p1);
175       assertNotNull("response file " + p1 + " != null",url);
176       return url.getPath();
177    }
178    
179    /**
180     * Get the ResponseCtx that represents a Syntax Error
181     * @return
182     */

183    private ResponseCtx getSyntaxErrorResponseCtx()
184    {
185       ArrayList JavaDoc code = new ArrayList JavaDoc();
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