Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
224   453   31   37.33
24   318   0.16   6
6     6  
1    
 
 
  SimplePolicyTest       Line # 75 224 31 83.5% 0.8346457
 
No Tests
 
1    /*
2    * SimplePolicyTest.java
3    *
4    * Created on 10 May 2007, 14:49
5    *
6    * Copyright (c) 2006, University of Kent
7    * All rights reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10    * modification, are permitted provided that the following conditions are met:
11    *
12    * Redistributions of source code must retain the above copyright notice, this
13    * list of conditions and the following disclaimer.
14    *
15    * Redistributions in binary form must reproduce the above copyright notice,
16    * this list of conditions and the following disclaimer in the documentation
17    * and/or other materials provided with the distribution.
18    *
19    * 1. Neither the name of the University of Kent nor the names of its
20    * contributors may be used to endorse or promote products derived from this
21    * software without specific prior written permission.
22    *
23    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26    * PURPOSE ARE DISCLAIMED.
27    *
28    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35    * POSSIBILITY OF SUCH DAMAGE.
36    *
37    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
38    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
39    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
40    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
41    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
42    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
43    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
44    * SERIOUS FAULTS, IN THIS SOFTWARE.
45    *
46    * 5. This license is governed, except to the extent that local laws
47    * necessarily apply, by the laws of England and Wales.
48    *
49    * Author : George Inman
50    * Email: g.inman@kent.ac.uk
51    *
52    */
53    package issrg.test;
54   
55    import java.io.IOException;
56    import java.util.ArrayList;
57   
58    import issrg.pba.Action;
59    import issrg.pba.PbaException;
60    import issrg.pba.Subject;
61    import issrg.pba.Target;
62    import issrg.pba.rbac.BadURLException;
63    import issrg.pba.rbac.CustomisePERMIS;
64    import issrg.pba.rbac.PermisAction;
65    import issrg.pba.rbac.PermisRBAC;
66    import issrg.pba.rbac.PermisSubject;
67    import issrg.pba.rbac.PermisTarget;
68    import issrg.pba.rbac.PolicyFinder;
69    import issrg.simplePERMIS.SimplePERMISPolicyFinder;
70    import issrg.simplePERMIS.SimplePERMISPrincipal;
71    import issrg.simplePERMIS.SimplePERMISToken;
72    import issrg.utils.RFC2253ParsingException;
73    import issrg.utils.repository.VirtualRepository;
74   
 
75    public class SimplePolicyTest {
76    static java.io.PrintStream out;
77    static PolicyFinder pfinder = null;
78   
79    static PermisRBAC pbaAPI = null;
80   
 
81  1 toggle public static boolean loadPolicy(String path) {
82   
83  1 try {
84  1 pfinder = new SimplePERMISPolicyFinder(path);
85    } catch (Exception e) {
86  0 out.println(e.getMessage());
87  0 e.printStackTrace();
88  0 pfinder = null;
89    } catch (Throwable th) {
90  0 th.printStackTrace();
91  0 pfinder = null;
92    }
93  1 if (pfinder == null) {
94  0 return false;
95    }
96    //out.println("[" + path + "] has been loaded");
97  1 return true;
98    }
99   
 
100  10 toggle public static SimplePERMISToken createToken(String holder, String issuer,
101    String type, String value) {
102  10 SimplePERMISToken token = null;
103  10 try {
104   
105  10 token = new SimplePERMISToken(holder, issuer, type, value);
106  10 out.println("holder = " + holder + ",Issuer = " + issuer
107    + ", type = " + type + ",value = " + value);
108    } catch (Exception e) {
109  0 System.out
110    .println("createToken() failed - An exception occured when creating the token");
111  0 out.println(e.getMessage());
112  0 e.printStackTrace();
113  0 return null;
114    }
115  10 if (token == null) {
116  0 out.println("createToken() failed - Token is null");
117    }
118  10 return token;
119    }
120   
 
121  13 toggle public static String processDecision(Subject s,Action a,Target t){
122  13 try{
123  11 if (!pbaAPI.decision(s, a, t, null)){
124  3 return "1: the action is not allowed";
125    }
126    }catch (issrg.pba.PbaException pe){
127  2 return "2: invalid input: "+pe.getMessage();
128    }catch (Throwable th){
129  0 return "3: run-time error: "+th.getMessage();
130    }
131   
132  8 return "0: action succeeded";
133    }
134   
135   
136   
137   
138   
 
139  1 toggle public static void main (String[] args) {
140  1 System.setProperty("line.separator", "\r\n");
141  1 if (args.length < 3) {
142  0 System.out
143    .println("These tests must be initalised with a configuration file, a list of attributes and an output file");
144  0 System.exit(1);
145    }
146  1 try{
147  1 out=new java.io.PrintStream(new java.io.FileOutputStream(args[2]));
148    }catch(Exception e){
149  0 e.printStackTrace(System.out);
150  0 return;
151    }
152    // load test attributes from attribute list
153  1 out.println("##### Loading text attributes into SimplePERMISTokens #####");
154  1 out.println();
155  1 String path = args[1];
156  1 ArrayList atts = new ArrayList();
157  1 try {
158   
159  1 java.io.BufferedReader input = null;
160  1 try {
161  1 input = new java.io.BufferedReader(new java.io.FileReader(path));
162    } catch (java.io.FileNotFoundException e) {
163  0 out.println(e.getMessage());
164  0 e.printStackTrace();
165    }
166  1 String holdername = "";
167  1 String issuer = "";
168  1 String type = "";
169  1 String value = "";
170  1 String s = "";
171   
172  0 while ((s = input.readLine()) != null) {
173   
174  10 holdername = s;
175   
176  10 issuer = input.readLine();
177  10 type = input.readLine();
178  10 value = input.readLine();
179   
180  10 atts.add(createToken(holdername, issuer, type, value));
181    }
182    // test attributes now stored as SimplePERMISTokens in the arraylist
183    // atts
184   
185    } catch (Throwable th) {
186   
187  0 out.println("Failed to load text attribute from [" + path
188    + "]");
189    // th.printStackTrace();
190   
191    }
192    // load the policy
193   
194  1 out.println();
195  1 out.println("##### Loading XML Policy into SimplePERMISPolicyFinder Object #####");
196  1 out.println();
197   
198  1 String XMLfilename = args[0];
199  1 loadPolicy(XMLfilename);
200  1 if (pfinder == null) {
201  0 out.println("fail");
202    } else {
203  1 out.println("Sucess the policy finder has been created");
204    }
205  1 out.println();
206  1 out.println("##### Creating PermisRBAC decision engine #####");
207  1 out.println();
208   
209  1 try {
210  1 pbaAPI = new PermisRBAC(pfinder);
211    } catch (PbaException e) {
212   
213  0 out.println(e.getMessage());
214  0 e.printStackTrace();
215    }
216   
217  1 if (pbaAPI == null) {
218  0 out.println("creation of the decision engine failed");
219    } else {
220  1 out.println("creation of the decision engine Succeeded");
221    }
222   
223   
224  1 out.println();
225  1 out.println("##### Setting TokenParser for SimplePERMISTokens #####");
226  1 out.println();
227  1 out.println("setting the attribute certificate attribute");
228  1 CustomisePERMIS.setAttributeCertificateAttribute("simpleSAM-Attribute");
229  1 try {
230  1 out.println("setting the Auth Token Parser");
231  1 CustomisePERMIS.setAuthTokenParser("issrg.simplePERMIS.SimplePERMISTokenParser");
232  1 out.println("Success the SimplePERMISTokenPARSER was set as the tokenPArser");
233    } catch (ClassNotFoundException e) {
234  0 out.println("SimplePERMISTokenPARSER was not found");
235  0 e.printStackTrace();
236    }
237  1 out.println();
238  1 out.println("##### Creating Valid decision LDAP DN Issuer #####");
239  1 out.println();
240  1 SimplePERMISToken test = (SimplePERMISToken) atts.get(0);
241  1 Object [] tester = new Object[1];
242  1 tester[0] = test;
243  1 createDecision(test,null);
244   
245  1 out.println();
246  1 out.println("##### Creating Valid decision URL issuer #####");
247  1 out.println();
248   
249  1 test = (SimplePERMISToken) atts.get(1);
250  1 tester[0] = test;
251  1 createDecision(test,null);
252   
253  1 try{
254   
255  1 out.println();
256  1 out.println("##### Creating Invalid decision User DN - is out of Range #####");
257  1 out.println();
258  1 test = (SimplePERMISToken) atts.get(2);
259  1 createDecision(test,null);
260    }catch(Exception e){}
261   
262   
263  1 out.println();
264  1 out.println("##### Creating Invalid decision DN issuer - is incorrect #####");
265  1 out.println();
266  1 test = (SimplePERMISToken) atts.get(3);
267  1 createDecision(test,null);
268   
269   
270  1 out.println();
271  1 out.println("##### Creating Invalid decision URL issuer - is incorrect #####");
272  1 out.println();
273  1 test = (SimplePERMISToken) atts.get(4);
274  1 createDecision(test,null);
275   
276   
277  1 out.println();
278  1 out.println("##### Creating Valid decision DN target #####");
279  1 out.println();
280  1 test = (SimplePERMISToken) atts.get(5);
281  1 createDecision(test,null);
282   
283   
284  1 out.println();
285  1 out.println("##### Creating Invalid decision DN target - is incorrect #####");
286  1 out.println();
287  1 test = (SimplePERMISToken) atts.get(5);
288  1 createDecision(test,"O=TEST,C=GB");
289   
290   
291  1 out.println();
292  1 out.println("##### Creating Valid decision URL target #####");
293  1 out.println();
294  1 test = (SimplePERMISToken) atts.get(5);
295  1 createDecision(test,"http://www.testtarget.com");
296   
297  1 out.println();
298  1 out.println("##### Creating Invalid decision URL target - is incorrect #####");
299  1 out.println();
300  1 test = (SimplePERMISToken) atts.get(5);
301  1 createDecision(test,"http://www.notarget.com");
302   
303  1 out.println();
304  1 out.println("Push Tests: ");
305  1 pbaAPI = null;
306  1 Object [] creds = new Object [3];
307  1 SimplePERMISToken test1 = (SimplePERMISToken) atts.get(6);
308  1 SimplePERMISToken test2 = (SimplePERMISToken) atts.get(7);
309  1 SimplePERMISToken test3 = (SimplePERMISToken) atts.get(8);
310  1 SimplePERMISToken test4 = (SimplePERMISToken) atts.get(9);
311  1 VirtualRepository vr = new VirtualRepository();
312  1 vr.populate(test1.getHolderEntry().getEntryName().getName(),"simpleSAM-Attribute",test1);
313  1 vr.populate(test2.getHolderEntry().getEntryName().getName(),"simpleSAM-Attribute",test2);
314  1 vr.populate(test3.getHolderEntry().getEntryName().getName(),"simpleSAM-Attribute",test3);
315  1 vr.populate(test4.getHolderEntry().getEntryName().getName(),"simpleSAM-Attribute",test4);
316  1 issrg.utils.repository.AttributeRepository r = vr;
317  1 try {
318  1 pbaAPI = new PermisRBAC(pfinder, r ,null);
319    } catch (PbaException e) {
320   
321  0 out.println(e.getMessage());
322  0 e.printStackTrace();
323    }
324   
325   
326   
327  1 if (pbaAPI == null) {
328  0 out.println("creation of the decision engine failed");
329    } else {
330  1 out.println("creation of the decision engine Succeeded");
331    }
332  1 out.println();
333  1 out.println("##### Creating Valid decision URL - needs role0 #####");
334  1 out.println();
335  1 createPull(null);
336  1 out.println();
337  1 out.println("##### Creating Valid decision URL - needs role0 and role1 #####");
338  1 out.println();
339  1 createPull("O=test,C=GB");
340  1 out.println();
341  1 out.println("##### Creating Valid decision URL - needs role0, role1 and role2 #####");
342  1 out.println();
343  1 createPull("O=test1,C=GB");
344   
345  1 out.println();
346  1 out.println("##### Creating Valid decision URL - needs role3 sent superior role4 #####");
347  1 out.println();
348  1 createPull("O=test2,C=GB");
349   
350  1 out.println("End of tests!");
351   
352    }
353   
 
354  4 togglepublic static void createPull(
355    String target){
356  4 Object [] tester = new Object[1];
357  4 SimplePERMISPrincipal principle = null;
358  4 Subject subject = null;
359  4 if (target == null){
360  1 target = "cn=target0,o=permis,c=gb";
361    }
362  4 Action a = null;
363  4 Target t = null;
364   
365   
366  4 principle = new SimplePERMISPrincipal("cn=user0,o=permis,c=gb");
367   
368  4 try {
369  4 subject = pbaAPI.getCreds(principle);
370  4 out.println(subject.exportCreds().toString());
371    } catch (PbaException e) {
372  0 out.println("Error : " + e.getMessage());
373    }
374   
375  4 out
376    .println("decision returned: ");
377   
378   
379  4 try {
380  4 a= new PermisAction("Action0");
381  4 if (target.startsWith("http://")){
382   
383  0 try {
384  0 t = new PermisTarget(target);
385    } catch (BadURLException e) {
386   
387    }
388    }else{
389  4 t = new PermisTarget(target,null);
390    }
391   
392   
393    } catch (RFC2253ParsingException e1) {
394  0 out.println("Error : " + e1.getMessage());
395    }
396   
397  4 out.println(processDecision(subject,a,t));
398    }
399   
 
400  9 togglepublic static void createDecision(SimplePERMISToken test, String target){
401   
402  9 Object [] tester = new Object[1];
403  9 SimplePERMISPrincipal principle = null;
404  9 Subject subject = null;
405  9 if (target == null){
406  6 target = "cn=target0,o=permis,c=gb";
407    }
408  9 Action a = null;
409  9 Target t = null;
410   
411  9 tester[0] = test;
412   
413  9 principle = new SimplePERMISPrincipal(test.getHolderEntry().getEntryName().getName());
414  9 out.println("Simple Permis Token Values :");
415  9 out.println("Holder : " + test.getHolderEntry().getEntryName().getName());
416  9 out.println("Issuer : " + test.getIssuerTokenLocator().getLocator().getName());
417  9 out.println("RoleType : " + test.getAttributeType());
418  9 out.println("RoleValue : " + test.getAttributeValue());
419  9 out.println();
420  9 out.println("target : " + target);
421  9 out.println();
422  9 try {
423  9 subject = pbaAPI.getCreds(principle, tester);
424  8 out.println(subject.exportCreds().toString());
425    } catch (PbaException e) {
426  1 out.println("Error : " + e.getMessage());
427    }
428   
429  9 out
430    .println("decision returned: ");
431   
432   
433  9 try {
434  9 a= new PermisAction("Action0");
435  9 if (target.startsWith("http://")){
436   
437  2 try {
438  2 t = new PermisTarget(target);
439    } catch (BadURLException e) {
440   
441    }
442    }else{
443  7 t = new PermisTarget(target,null);
444    }
445   
446   
447    } catch (RFC2253ParsingException e1) {
448  0 out.println("Error : " + e1.getMessage());
449    }
450   
451  9 out.println(processDecision(subject,a,t));
452    }
453    }