Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
87   352   31   6.69
64   142   0.49   13
13     3.31  
1    
 
 
  SetOfSubsetsCredentials       Line # 50 87 31 96.3% 0.9634146
 
  (1)
 
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    package issrg.pba.rbac;
33   
34    import java.util.Vector;
35    import issrg.pba.Credentials;
36   
37    /**
38    * This class implements the Credentials interface, as needed for representing
39    * the set of subsets.
40    *
41    * <p>The object can contain a set of any credentials and tell if it contains
42    * other credentials and build unions and intersections with other credentials.
43    *
44    * @author E Ball
45    * @author A Otenko
46    * @author D Chadwick
47    * @version 0.2
48    */
49   
 
50    public class SetOfSubsetsCredentials implements Credentials{
51    private Vector Creds=new Vector();
52   
53    /**
54    * This constructor creates an object with no credentials: an empty set of
55    * credentials.
56    */
 
57  7593 toggle public SetOfSubsetsCredentials(){}
58   
59    /**
60    * This constructor builds a Credential object from an array of Credentials.
61    *
62    * @param Creds is the array of credentials to be contained in this set
63    */
 
64  97 toggle public SetOfSubsetsCredentials(Credentials [] Creds){
65  291 for(int i=0;i<Creds.length;i++){
66  194 this.Creds.add(Creds[i]);
67    }
68    }
69   
70    /**
71    * This constructor also builds the set from the given array of credentials,
72    * but the array of credentials is supplied as a vector.
73    *
74    * @param creds is the Vector of Credential objects to be included in the set
75    */
 
76  8271 toggle public SetOfSubsetsCredentials(Vector creds){
77  8271 Creds = new Vector(creds);
78    }
79   
80    /**
81    * This method returns the set of credentials
82    *
83    * @return the Vector of credentials in this set; cannot be null, but can be
84    * empty; all elements are of type Credential
85    */
 
86  19024 toggle public Vector getValue (){
87  19024 return new Vector(Creds);
88    }
89   
90    /**
91    * This method checks to see if the given set of credentials is contained
92    * in this object. That is, for each Credential contained in the set there is
93    * at least one Credential that can grant more or the same privilege.
94    *
95    * @param subSet the credentials object (even a set of subsets) that must be a
96    * sub-set
97    * of this object
98    *
99    * @return true, if <code>subSet</code> is fully contained within this object;
100    * false, otherwise
101    */
 
102  9765 toggle public boolean contains(Credentials subSet){
103  9765 return _contains_(subSet, false);
104    }
105   
106    /**
107    * This method tells if this set of subsets is a part of the given
108    * credentials object.
109    * It is used by the SubsetCredentials class.
110    *
111    * @param superSet is the credentials object that must be a superset of
112    * this set
113    *
114    * @return true, if the given set of credentials contains this set
115    */
 
116  2 toggle public boolean partOf(Credentials superSet){
117  2 return _contains_(superSet, true);
118    }
119   
120    /**
121    * This is a private method that decides if a given subset is contained within
122    * this object or vice versa, depending on reverse. If reverse is false, then
123    * it calculates "contains"; otherwise - "partOf".
124    *
125    * @param subSet is the credential against which to compare
126    * @param reverse tells which of the credentials must be a superset; if true,
127    * then the subSet must be a superset of this credential; if false, this
128    * credential must be a superset of the subSet
129    *
130    * @return boolean result; true if the needed set contains the other
131    */
 
132  9767 toggle private boolean _contains_(Credentials subSet, boolean reverse){
133  9767 Vector v;
134    //System.out.println("\t_contains_("+subSet+","+reverse+")"); //***
135  9767 if (subSet instanceof SetOfSubsetsCredentials){
136  9764 v = ((SetOfSubsetsCredentials)subSet).getValue();
137    }else{
138  3 v = new java.util.Vector();
139  3 if (subSet!=null) v.add(subSet);
140    }
141   
142  9767 Vector lhs = reverse?v:Creds;
143  9767 Vector rhs = reverse?Creds:v;
144   
145    //System.out.println("\t\tlhs: "+lhs+"\n\t\trhs: "+rhs); //****
146  9767 cont_loop:
147  9986 for (int i=rhs.size(); i-->0; ){
148  4944 Credentials c = (Credentials)rhs.get(i);
149   
150  7067 for (int j=lhs.size(); j-->0; ){
151  2342 if (((Credentials)lhs.get(j)).contains(c)){
152    // found a credential that is greater than c
153  219 continue cont_loop;
154    }
155    }
156   
157    // one credential with no greater credential has been found
158  4725 return false;
159    }
160   
161    // we are here only if for each of the credentials of the rhs there
162    // is a greater credential in the lhs set.
163  5042 return true;
164    }
165   
166    /**
167    * This method checks to see if the supplied credentials (roles) are equal
168    * to the credentials contained in this object. a.equals(b) and b.equals(a)
169    * are both equivalent to (a.contains(b) && b.contains(a)).
170    *
171    * @param creds is the credentials object to compare to
172    *
173    * @return true if this credentials object is equal to the given set
174    */
 
175  3869 toggle public boolean equals(Credentials creds){
176  3869 return contains(creds) && creds.contains(this);
177    }
178   
179    /**
180    * This method compares this credentials object to another object. First it checks if
181    * that object is a credentials object, then calls <code>equals(Credentials)</code>.
182    *
183    * @param o is the object to compare to
184    *
185    * @return true, if o is a credentials object and it is equal to this set
186    *
187    * @see equals(Credentials)
188    */
 
189  3856 toggle public boolean equals(Object o){
190  3856 if (o instanceof Credentials){
191  3856 return equals((Credentials)o);
192    }
193   
194  0 return false;
195    }
196   
197    /**
198    * This method creates a copy of this credentials set.
199    *
200    * @return the SetOfSubsetsCredentials
201    */
 
202  107 toggle public Object clone(){
203  107 return intersection(this);
204    }
205   
206    /**
207    * This method builds an intersection of this credentials set with another set.
208    *
209    * <p>The method just intersects each component of the stored set with the given set and
210    * collects the result in a credentials object.
211    *
212    * @param set is the set of credentials to intersect with
213    *
214    * @return the Credentials object that is the intersection of the two sets; it is
215    * contained by both of the sets
216    */
 
217  3305 toggle public Credentials intersection(Credentials set){
218  3305 Vector alien;
219  3305 if (set instanceof SetOfSubsetsCredentials){
220  2998 alien = ((SetOfSubsetsCredentials)set).getValue();
221    }else{
222  307 alien = new Vector();
223  307 if (set!=null) alien.add(set);
224    }
225   
226  3305 Vector r = new Vector();
227  3305 int credsize = Creds.size();
228  3305 int aliensize = alien.size();
229   
230  7928 for (int i = 0; i < credsize; i++) {
231  4623 Credentials c = (Credentials) Creds.get(i);
232  4623 Credentials m = null;
233  9653 for (int j = 0; j < aliensize; j++) {
234  5030 Credentials c1 = ((Credentials) alien.get(j)).intersection(c);
235  5030 if (c1 != null) {
236  4068 if (m == null) {
237  3951 m = c1;
238    } else {
239  117 if (c1.contains(m)) {
240  11 m = c1;
241    } else {
242  106 if ((!c1.contains(m)) && (!m.contains(c1))) {
243  98 m = m.union(c1);
244    }
245    }
246    }
247    }
248    }
249  4623 if (m != null) {
250  3951 r.add(m);
251    }
252    }
253   
254  3305 credsize = r.size();
255  3305 Vector l = new Vector();
256  7256 for (int i = 0; i < credsize; i++) {
257  3951 Credentials c = (Credentials) r.get(i);
258  3951 Credentials temp = null;
259  4913 for (int j = 0; j < l.size(); j++) {
260  962 temp = (Credentials) l.get(j);
261  962 if ((c.contains(temp)) && (!c.equals(temp))) {
262  1 l.remove(j);
263    }
264    }
265  3951 if (temp == null) {
266  2990 l.add(c.clone());
267    } else {
268  961 if (!temp.contains(c)) l.add(c.clone());
269    }
270    }
271  3305 SetOfSubsetsCredentials a = new SetOfSubsetsCredentials(l);
272  3305 if (l.isEmpty()) return a;
273  3087 while (a.getValue().get(0) instanceof issrg.pba.rbac.SetOfSubsetsCredentials) a = (SetOfSubsetsCredentials) a.getValue().get(0);
274  2990 return a;
275   
276    //
277   
278    // for (int i=Creds.size(); i-->0;){
279    // Credentials c = (Credentials)Creds.get(i);
280    // Credentials m = null;
281    //
282    // for(int j=alien.size(); j-->0;){
283    // Credentials c1 = ((Credentials)alien.get(j)).intersection(c);
284    // if (c1!=null){
285    // c=c1;
286    // m=c1;
287    //
288    // }
289    // }
290    //
291    // if (m!=null){
292    // r.add(m);
293    // }
294    // }
295   
296    // return new SetOfSubsetsCredentials(r);
297    }
298   
299    /**
300    * This method returns a union of this credential set with another one.
301    *
302    * <p>It just collects all the credentials from the given set that are not
303    * contained in any of the elements of the other set.
304    *
305    * @param set is the set to join with
306    *
307    * @return the Credential set representing the union of the two sets; it
308    * contains both of the sets
309    */
 
310  2915 toggle public Credentials union(Credentials set){
311  2915 Vector alien;
312    //System.err.println("\t\t*** JOINING SETS OF CREDS ***"); //*****
313  2915 if (set instanceof SetOfSubsetsCredentials){
314  2914 alien = ((SetOfSubsetsCredentials)set).getValue();
315    }else{
316  1 alien = new Vector();
317  1 if (set!=null) alien.add(set);
318    }
319   
320  2915 Vector r = new Vector();
321  2915 Vector p = Creds;
322    //System.err.println("\t"+p+"\n\t"+alien); //*****
323   
324  8745 while (p!=null){
325    //System.err.println("\t\t"+p); //*****
326  8903 for(int i=p.size(); i-->0;){
327  3073 Credentials c = (Credentials)p.get(i);
328   
329  3839 for (int j=r.size(); j-->0;){
330    //System.err.println(c+" contains "+((Credentials)r.get(j))+": "+c.contains((Credentials)r.get(j))); //***************
331  766 if (c.contains((Credentials)r.get(j))){
332  91 r.remove(j);
333    }
334    }
335   
336  3073 r.add(c.clone()); //Tuan Anh: This command seems not enough. Look at intersection function.
337    }
338    //System.err.println("\t\t resulted in: "+r); //*****
339   
340  5830 p=alien;
341  5830 alien=null;
342    }
343   
344  2915 return new SetOfSubsetsCredentials(r);
345    }
346   
 
347  6634 toggle public String toString(){
348  6634 return "set of subsets "+(Creds==null?"null":Creds.toString());
349    }
350    }
351   
352