Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
156   443   48   22.29
64   260   0.35   7
7     7.71  
1    
 
 
  MSoDTest       Line # 105 156 48 75.8% 0.75770926
 
No Tests
 
1    /*
2    * Copyright (c) 2000-2005, University of Salford
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * Neither the name of the University of Salford nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29    * POSSIBILITY OF SUCH DAMAGE.
30    */
31    /*
32    * Copyright (c) 2006, University of Kent
33    * All rights reserved.
34    *
35    * Redistribution and use in source and binary forms, with or without
36    * modification, are permitted provided that the following conditions are met:
37    *
38    * Redistributions of source code must retain the above copyright notice, this
39    * list of conditions and the following disclaimer.
40    *
41    * Redistributions in binary form must reproduce the above copyright notice,
42    * this list of conditions and the following disclaimer in the documentation
43    * and/or other materials provided with the distribution.
44    *
45    * 1. Neither the name of the University of Kent nor the names of its
46    * contributors may be used to endorse or promote products derived from this
47    * software without specific prior written permission.
48    *
49    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
50    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
51    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52    * PURPOSE ARE DISCLAIMED.
53    *
54    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
55    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61    * POSSIBILITY OF SUCH DAMAGE.
62    *
63    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
64    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
65    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
66    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
67    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
68    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
69    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
70    * SERIOUS FAULTS, IN THIS SOFTWARE.
71    *
72    * 5. This license is governed, except to the extent that local laws
73    * necessarily apply, by the laws of England and Wales.
74    */
75   
76    package issrg.test;
77   
78    import issrg.pba.*;
79    import issrg.pba.rbac.*;
80    import issrg.utils.RFC2253NameParser;
81    import issrg.pba.rbac.x509.RepositoryACPolicyFinder;
82    import java.io.File;
83    import java.io.FileNotFoundException;
84    import java.io.FileOutputStream;
85    import java.io.PrintStream;
86    import javax.naming.Context;
87    import javax.naming.directory.InitialDirContext;
88   
89    import java.security.cert.CertificateFactory;
90    import java.security.cert.X509Certificate;
91    import java.io.ByteArrayInputStream;
92    import java.util.*;
93   
94    //Bassem for input loglevel
95    import javax.swing.text.*;
96    import javax.swing.undo.*;
97    import javax.swing.event.*;
98    import javax.swing.*;
99    /**
100    * This is a testing tool for MSoD.
101    *
102    * @author W.Xu
103    */
104   
 
105    public class MSoDTest {
106   
107    private final static java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
108    private static final String PKC="rootca.cer";
109    private static final String PERMIS_EPF="permis.epf";
110    private static final String PERMIS_PSW="l3tM3InNow";
111    private static final String SALFORD_LDAP="ldap://sec.cs.kent.ac.uk/";
112    private static final String AC_ATTRIBUTE="attributeCertificateAttribute";
113    private static final String PKC_ATTRIBUTE="userCertificate";
114   
115    private static String SOA = "";
116    private static String oID = "";
117    private static String LDAP = "";
118    private static String uRL = "";
119    private static String AC_attribute = "";
120    private static String PKC_attribute = "";
121    private final static issrg.utils.repository.VirtualRepository vr = new issrg.utils.repository.VirtualRepository();
122   
123    private static CertificateFactory cf;
124   
125   
 
126  4 toggle public static void main(String [] args){
127   
128   
129  4 System.setProperty("line.separator", "\r\n");
130   
131  4 if ( (args.length>2) && (args[2] != null) )
132  4 try {
133  4 PrintStream pt=new PrintStream(new FileOutputStream(args[2]));
134  4 System.setOut(pt);
135  4 System.setErr(pt);
136    } catch (FileNotFoundException fnf) {
137  0 System.out.println(" File Not found : " + fnf.getMessage());
138    }
139   
140  4 try{
141   
142  4 if (args.length<1){
143  0 System.out.println("Sorry, but I need two arguments in the command line as the configuration file name and test input file name.");
144  0 System.exit(0);
145    } else{
146  4 java.io.BufferedReader in=null;
147  4 try{
148  4 in = new java.io.BufferedReader(new java.io.FileReader(args[0])); //
149    }catch(java.io.IOException io){
150  0 System.err.println("Could not open file "+args[0]);
151  0 throw io;
152    }
153   
154  4 java.io.BufferedReader testIn=null;
155  4 try{
156  4 testIn = new java.io.BufferedReader(new java.io.FileReader(args[1])); //
157    }catch(java.io.IOException io){
158  0 System.err.println("Could not open file "+args[1]);
159  0 throw io;
160    }
161   
162   
163    //Bassem: added to choose the loglevel, if not choosen by default the level is the maximum
164  4 int loglevel=4;
165   
166    //if the output file is given as parameter use loglevel=4, (added for the test ant script)
167  4 if ( (args.length==2) ){
168  0 SATJDialog ff=new SATJDialog(null,true);
169  0 if(ff!=null){
170  0 loglevel=ff.satlevel;
171  0 ff=null;
172    }
173    }
174   
175   
176  4 String s;
177   
178  0 while ((s=in.readLine())!=null){
179  28 int i = s.indexOf('='); // find the assignment mark
180  28 String varName = null;
181   
182  28 if (i>=0){
183  28 varName = s.substring(0, i).intern();
184  28 s = s.substring(i+1);
185    }
186   
187  28 if (varName == "SOA"){
188  4 SOA = s;
189    }else
190  24 if (varName == "OID"){
191  4 oID = s;
192    }else
193  20 if (varName == "LDAP_AC_attribute"){
194  4 AC_attribute = s;
195    }else
196  16 if (varName == "LDAP_PKC_attribute"){
197  0 PKC_attribute = s;
198    }else
199  16 if (varName == "LDAP"){
200  4 LDAP = s;
201    }else
202  12 if (varName == "URL"){
203  4 uRL = s;
204    }else
205  8 if (varName == "PKC"){
206  4 loadPKC(vr, s);
207    }else
208  4 if (varName == "AC"){
209  4 loadAC(vr, s);
210    }else
211  0 println("Unrecognised line; ignored: "+varName+"="+s);
212    }
213   
214   
215  4 if (LDAP.intern()==""){
216  0 LDAP=SALFORD_LDAP;
217    }
218  4 if (AC_attribute.intern()==""){
219  0 AC_attribute=AC_ATTRIBUTE;
220    }
221  4 if (PKC_attribute.intern()==""){
222  4 PKC_attribute=PKC_ATTRIBUTE;
223    }
224   
225  4 issrg.utils.repository.AttributeRepository r=vr;
226   
227   
228  4 issrg.pba.rbac.SignatureVerifier sv = new SamplePKI();
229   
230  4 String epf = null;
231  4 CustomisePERMIS.configureX509Flavour();
232   
233  4 RepositoryACPolicyFinder pf= new RepositoryACPolicyFinder(r, oID, new LDAPDNPrincipal(SOA), sv,loglevel);
234   
235  4 PBAAPI pbaApi = new PermisRBAC(pf, r, null);
236   
237  4 MSoDTest aef = new MSoDTest(pbaApi);
238   
239  4 String userDN = "";
240  4 String targetDN = "";
241  4 String action = "";
242  4 String contextInstance = null;
243  4 String expectedResult = null;
244  4 String testNo = null;
245  4 String acFilename = null;
246  4 int seq = 0;
247   
248  0 while ((s=testIn.readLine())!=null){
249  284 int i = s.indexOf('='); // find the assignment mark
250  284 String varName = null;
251   
252  284 if (i>=0){
253  245 varName = s.substring(0, i).intern();
254  245 s = s.substring(i+1);
255    }
256   
257  284 if (varName == "userDN"){
258  35 userDN = s;
259    }else
260  249 if (varName == "targetDN"){
261  35 targetDN = s;
262    }else
263  214 if (varName == "action"){
264  35 action = s;
265    }else
266  179 if (varName == "contextInstance"){
267  35 contextInstance = s;
268    }else
269  144 if (varName == "expectedResult"){
270  35 expectedResult = s;
271    }else
272  109 if (varName == "acFilename"){
273  35 acFilename = s;
274    }else
275  74 if (varName == "testNo"){
276  35 testNo = s;
277   
278  35 PermisTarget pt = null;
279  35 try{
280  35 pt=new PermisTarget(targetDN); // see if it is a URL or a DN
281    }catch (issrg.pba.rbac.BadURLException bue){
282  0 pt=new PermisTarget(targetDN, null);
283    }
284  35 Map env = new Hashtable();
285  35 if ( ( contextInstance != null) && (contextInstance.compareTo("null")!= 0) ) {
286  35 env.put("ContextInstance", contextInstance);
287    }
288  35 println(" ");
289  35 println("MSoD test " + testNo);
290  35 println("UserDN: " + userDN);
291  35 println("Action: " + action);
292  35 println("Target: " + targetDN);
293  35 println("ContextInstance: " + "context " + contextInstance );
294  35 println("Expected result: " + expectedResult);
295  35 String result1 = aef.execute2(new LDAPDNPrincipal(userDN), new PermisAction(action), pt, env, acFilename);
296  35 println("Final result: " + result1 );
297  35 if ((result1.startsWith("0") && expectedResult.startsWith("succ") ) ||
298    (result1.startsWith("1") && expectedResult.startsWith("not") )) {
299  35 println("result correct!!!!!");
300    } else {
301  0 println("result wrong????????????????????????????????????");
302    }
303   
304    }else{
305    //println("Unrecognised line; ignored: "+varName+"="+s);
306    }
307    }
308   
309   
310  4 ((PermisRBAC)pbaApi).SAWSCloseLog();
311   
312   
313   
314    }
315   
316    }catch (Throwable th){
317  0 println("Failed to initialise: "+th.getMessage());
318  0 th.printStackTrace();
319    }
320   
321    /* Thread[] ts = new Thread[100];
322    Thread.enumerate(ts);
323    System.out.println("#######################################");
324    for (int i = 0; i < ts.length; i = i + 1) {
325    if (ts[i] != null) {
326    System.out.println(ts[i].getName());
327    } else {
328    i = ts.length;
329    }
330    }
331    System.out.println("#######################################");
332    */
333   
334    // println("bassems before exit");
335  4 if ( (args != null) && (args.length>2) ){
336  4 System.out.close();
337  4 System.err.close();
338    }
339    else
340  0 System.exit(0);
341    }
342   
343   
344   
 
345  4 toggle public static void loadAC(issrg.utils.repository.VirtualRepository vr, String filename){
346  4 try{
347  4 java.io.InputStream io = new java.io.FileInputStream(filename);
348  4 byte [] ac = new byte [io.available()];
349   
350  4 io.read(ac);
351   
352  4 issrg.ac.AttributeCertificate acd = issrg.ac.AttributeCertificate.guessEncoding(ac);
353   
354    // now put the ac into the entry with the name of the holder
355    // we are working with explicit Holder specification only: no BCIDs
356   
357  4 vr.populate(issrg.ac.Util.generalNamesToString(acd.getACInfo().getHolder().getEntityName()).toUpperCase(),
358    CustomisePERMIS.getAttributeCertificateAttribute(),
359    // issrg.pba.repository.ACRepository.ATTRIBUTE_CERTIFICATE_ID,
360    ac);
361    }catch (Throwable th){
362  0 println("Failed to load AC from ["+filename+"]");
363  0 th.printStackTrace();
364    }
365    }
366   
 
367  4 toggle public static void loadPKC(issrg.utils.repository.VirtualRepository vr, String filename){
368  4 try{
369  4 if (cf==null) cf=CertificateFactory.getInstance("X.509");
370   
371  4 java.io.InputStream io = new java.io.FileInputStream(filename);
372  4 byte [] pkc = new byte [io.available()];
373   
374  4 io.read(pkc);
375   
376  4 X509Certificate x509 = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(pkc));
377    //System.out.println("loading PKC for "+x509.getSubjectDN().getName());//******************
378   
379  4 String s=RFC2253NameParser.toCanonicalDN(RFC2253NameParser.distinguishedName(x509.getSubjectDN().getName()));
380   
381  4 vr.populate(s, CustomisePERMIS.getUserCertificateAttribute(), pkc);
382    // vr.populate(s, issrg.security.PKCRepository.USER_PKC_ATTRIBUTE, pkc);
383    }catch (Throwable th){
384  0 println("Failed to load PKC from ["+filename+"]");
385  0 th.printStackTrace();
386    }
387    }
388   
 
389  0 toggle public static String readLine(String prompt){
390  0 System.out.print(prompt);
391  0 String s = null;
392   
393  0 try{
394  0 s=in.readLine();
395    }catch(java.io.IOException ioe){
396    }
397   
398  0 return s==null?"":s;
399    }
400   
 
401  315 toggle public static void println(String s){
402    // add code for logging things to a file
403  315 System.out.println(s);
404    }
405   
406   
407   
408    PBAAPI pbaApi = null;
409   
 
410  4 toggle public MSoDTest(PBAAPI pbaApi){
411  4 this.pbaApi = pbaApi;
412    }
413   
 
414  35 toggle public String execute2(java.security.Principal user, issrg.pba.Action action, Target target, Map env, String acFilename){
415  35 try{
416    // no cache is known
417  35 java.io.InputStream io = new java.io.FileInputStream(acFilename);
418    // byte [] b = new byte[io.available()];
419  35 Object [] ac = new byte[1][io.available()];
420   
421    // byte [][] ac = new byte [1][io.available()];
422   
423  35 io.read( (byte [])ac[0]);
424   
425  35 Subject s = pbaApi.getCreds(user, (Object [])ac, null);
426    // System.out.println("Roles:" + ((issrg.pba.Credentials)s.exportCreds()).toString() );
427  35 if (!(pbaApi.authzDecision(s, action, target, env).isAuthorised())){
428  17 return "1: the action is not allowed";
429    }
430   
431    // here you call the action on the target
432    }catch (PbaException pe){
433  0 pe.printStackTrace();
434  0 return "2: invalid input: "+pe.getMessage();
435    }catch (Throwable th){
436  0 th.printStackTrace();
437  0 return "3: run-time error: "+th.getMessage();
438    }
439   
440  18 return "0: action succeeded";
441    }
442   
443    }