Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
111   240   54   18.5
70   127   0.53   6
6     9.83  
1    
 
 
  DefaultRuleComparator       Line # 67 111 54 79.7% 0.79679143
 
  (1)
 
1    /*
2    * Copyright (c) 2006, University of Kent
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    * 1. Neither the name of the University of Kent 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    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22    * PURPOSE ARE DISCLAIMED.
23    *
24    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    * POSSIBILITY OF SUCH DAMAGE.
32    *
33    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
34    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
35    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
36    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
37    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
38    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
39    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
40    * SERIOUS FAULTS, IN THIS SOFTWARE.
41    *
42    * 5. This license is governed, except to the extent that local laws
43    * necessarily apply, by the laws of England and Wales.
44    */
45   
46    /*
47    * DefaultComparator.java
48    *
49    * Created on February 21, 2006, 9:49 AM
50    */
51   
52    package issrg.pba.rbac.xmlpolicy;
53   
54    import issrg.pba.Credentials;
55    import issrg.pba.DelegatableToken;
56    import issrg.pba.ParsedToken;
57    import issrg.pba.rbac.ExpirableCredentials;
58    import issrg.pba.rbac.SetOfSubsetsCredentials;
59    import issrg.pba.rbac.policies.AssignmentRule;
60    import java.util.Arrays;
61    import java.util.Date;
62    import java.util.Iterator;
63    import java.util.Vector;
64   
65    import org.apache.log4j.*;
66   
 
67    public class DefaultRuleComparator implements issrg.pba.rbac.RuleComparator, java.util.Comparator {
68   
69    private Logger logger = Logger.getLogger(DefaultRuleComparator.class);
70    Object assertion;
71    /** Creates a new instance of DefaultComparator */
 
72  26 toggle public DefaultRuleComparator() {
73    }
74   
75    /**
76    * This function compare two object o1 and o2 according to the assertion. They are two DelegateableToken objects.
77    * Order of priority: credentials - validity period - delegation depth.
78    *
79    * Get the delegateable credentials of the two objects and try to compare them with the credentials in the assertion by the
80    * morePriority function. If one object has more priority than the other then return the value -1 or 1.
81    * Compare two validity periods of the two objects. If one has more priority then return -1 or 1.
82    * Compare two delegation depths then return -1 or 1.
83    *
84    * @param o1 is the first object. It is a DelegateableToken object
85    * @param o2 is the second object. It is a DelegateableToken object.
86    * @return -1 if o1 is greater or equal o2, otherwise return 1
87    */
 
88  9 toggle public int compare(Object o1, Object o2) {
89  0 if (!(o1 instanceof ParsedToken && o2 instanceof ParsedToken && o1!=null && o2!=null)) throw new ClassCastException("Two parameters should not be null and must be the instance of ParsedToken class");
90  9 DelegatableToken obj1 = (DelegatableToken) o1;
91  9 DelegatableToken obj2 = (DelegatableToken) o2;
92   
93  9 Credentials credsTok1 = obj1.getDelegateableCredentials();
94  9 Credentials credsTok2 = obj2.getDelegateableCredentials();
95  9 Credentials assertCred;
96  9 if (assertion instanceof AssignmentRule) assertCred = ((AssignmentRule) assertion).getCredentials(); else assertCred = (Credentials) assertion;
97   
98  9 Credentials con1, con2;
99  9 con1 = credsTok1.intersection(assertCred);
100  9 con2 = credsTok2.intersection(assertCred);
101  9 int states = morePriority(con1, con2);
102  9 if (states == -1) return -1;
103  0 if (states == 1) return 1;
104    //from here two roleValues are not compareable or they are the same. Continue checking validity time and delegation depth
105  6 if (con1 instanceof SetOfSubsetsCredentials && con2 instanceof SetOfSubsetsCredentials) {
106  6 Credentials f1, f2;
107  6 f1 = (Credentials) ((SetOfSubsetsCredentials)con1).getValue().get(0);
108  6 f2 = (Credentials) ((SetOfSubsetsCredentials)con2).getValue().get(0);
109  6 if (f1 instanceof ExpirableCredentials && f2 instanceof ExpirableCredentials) {
110  6 issrg.pba.rbac.ValidityPeriod vp1 = ((ExpirableCredentials) f1).getValidityPeriod();
111  6 issrg.pba.rbac.ValidityPeriod vp2 = ((ExpirableCredentials) f2).getValidityPeriod();
112  6 Date na1 = vp1.getNotAfter();
113  6 Date nb1 = vp1.getNotBefore();
114  6 Date na2 = vp2.getNotAfter();
115  6 Date nb2 = vp2.getNotBefore();
116  0 if ((nb1.compareTo(nb2) < 0) && (na1.compareTo(na2) >= 0)) return -1;
117  0 if ((nb1.compareTo(nb2) == 0) && (na1.compareTo(na2) > 0)) return -1;
118  0 if ((nb2.compareTo(nb1) < 0) && (na2.compareTo(na1) >= 0)) return 1;
119  0 if ((nb2.compareTo(nb1) == 0) && (na2.compareTo(na1) > 0)) return 1;
120    }
121    }
122    //from here two rolesValues are not compareable (or they are the same) and validity time are not compareable
123  6 if (assertion instanceof Credentials) return 1;
124  4 int tok1Depth = obj1.getDepth();
125  4 int tok2Depth = obj2.getDepth();
126  4 int requestedDepth = ((AssignmentRule) assertion).getDelegationDepth();
127  4 if ((tok1Depth == -1) || ((tok1Depth > requestedDepth) && (requestedDepth > -1))) return -1;
128  0 if ((tok2Depth == -1) || ((tok2Depth > requestedDepth) && (requestedDepth > -1))) return 1;
129  0 if (tok1Depth >= tok2Depth) return -1; else return 1;
130    }
131   
132    /**
133    *This function will take the vector of asserted RARs of issuer, ignore unrelevant RARs for the assertion,
134    * and sort relevant RARs according to the assertion
135    *@param assertion is either a credentials or a RoleAssignmentRule of the holder that needs to be validated
136    *@param tokens stores all the RARs of issuer
137    *@param holder is the holder of the assertion
138    *@return an array of ParsedToken that is sorted according to the assertion.
139    */
140   
 
141  560 toggle public synchronized ParsedToken[] predict(Object assertion, Vector tokens, issrg.utils.repository.Entry holder){
142  560 this.assertion = assertion;
143  560 Credentials assertCred;
144  560 if (assertion instanceof AssignmentRule) assertCred = ((AssignmentRule) assertion).getCredentials(); else assertCred = (Credentials) assertion;
145  560 Vector tokensClone = (Vector) tokens.clone();
146  560 SetOfSubsetsCredentials empty = new SetOfSubsetsCredentials();
147  1151 for (Iterator i = tokensClone.iterator(); i.hasNext();) {
148  591 ParsedToken t = (ParsedToken) i.next();
149  591 if (!(t instanceof DelegatableToken)) {
150  18 i.remove();
151  18 continue;
152    }
153  573 DelegatableToken dt = (DelegatableToken)t;
154  573 if (dt.getDelegateableCredentials().intersection(assertCred).equals(empty)
155    || !dt.getSubjectDomain().contains(holder))
156  132 i.remove();
157    }
158  560 ParsedToken[] ret;
159  560 ret = (ParsedToken[]) tokensClone.toArray(new ParsedToken[0]);
160  560 Arrays.sort(ret, this);
161  560 return ret;
162   
163    }
164   
165    private boolean flag = false;
166   
 
167  1177 toggle public void setFlag (boolean manySOAs) {
168  1177 flag = manySOAs;
169    }
170   
171    /** This function test whether the constrained assertion is good enough
172    *@param asRAR is the RoleAssignmentRule of the issuer
173    *@param vaRAR is the validated RoleAssignmentRule of the issuer. Both of these RoleAssignmentRules may be null.
174    *If they are null, it means RoleAssignmentRule of the issuer is totally trusted and we do not care about it.
175    *@param assertion is either a credentials or a RoleAssignmentRule of the holder that needs to be validated
176    *@param validated is a validated credentials or a Vecor of validated RoleAssignmentRules of the holder
177    *@return a boolean value. If it is true then the issuer's RAR is good enough for validating the request and
178    *we do not need to use another issuer'RAR for validating the request. Otherwise, we need to use another RAR
179    *to validate the request.
180    *
181    */
 
182  2360 toggle public boolean isSufficient(AssignmentRule asRAR, AssignmentRule vaRAR, Object assertion, Object validated) {
183  2360 if (asRAR==null && vaRAR==null)
184    {
185  1576 if (!flag) return true;
186    else {
187  1133 if (validated instanceof Credentials) {
188  1126 if (((Credentials) validated).equals(new SetOfSubsetsCredentials())) return false; else return true;
189    } else {
190  7 if (validated instanceof Vector) {
191  0 if (!((Vector)validated).isEmpty()) return true; else return false;
192  0 } else return false;
193    }
194    }
195    }
196   
197  784 if (validated instanceof Credentials) {
198  687 if (((Credentials) validated).equals(new SetOfSubsetsCredentials())) return false; else return true;
199    } else {
200  97 if (validated instanceof Vector) {
201  97 if (!((Vector)validated).isEmpty()) return true; else return false;
202  0 } else return false;
203    }
204   
205    }
206   
207    /**
208    * This function compares two Credentials to find the one that more appropriate to the request.
209    *
210    *
211    *@param current is the first Credentials
212    *@param cred is the second Credentials
213    *
214    *@return -1 if current is greater than cred, 0 if they are equal or not comparable, 1 if current is less than cred
215    */
216   
 
217  9 toggle private int morePriority(Credentials current, Credentials cred) {
218  9 if (current instanceof SetOfSubsetsCredentials && cred instanceof SetOfSubsetsCredentials) {
219  9 Vector t = ((SetOfSubsetsCredentials)current).getValue();
220  9 Vector fromCurrent = new Vector();
221  20 for (int i = 0; i < t.size(); i++) {
222  0 if (!(t.get(i) instanceof ExpirableCredentials)) return 0;
223  11 fromCurrent.add(((ExpirableCredentials) t.get(i)).getExpirable());
224    }
225  9 t = ((SetOfSubsetsCredentials) cred).getValue();
226  9 Vector fromCred = new Vector();
227  19 for (int i = 0; i < t.size(); i++) {
228  0 if (!(t.get(i) instanceof ExpirableCredentials)) return 0;
229  10 fromCred.add(((ExpirableCredentials) t.get(i)).getExpirable());
230    }
231  9 SetOfSubsetsCredentials currentSet = new SetOfSubsetsCredentials(fromCurrent);
232  9 SetOfSubsetsCredentials credSet = new SetOfSubsetsCredentials(fromCred);
233  9 if (currentSet.contains(credSet) && !currentSet.equals(credSet)) return -1;
234  0 if (credSet.contains(currentSet) && !currentSet.equals(credSet)) return 1;
235  6 return 0; //return 0 here if the two set are the same or neither of them contains the other
236    }
237  0 return 0;
238    }
239    }
240