source: java/net/deterlab/abac/regression/ReadCreds.java @ 1d5066d

Last change on this file since 1d5066d was 28d6d08, checked in by Ted Faber <faber@…>, 11 years ago

Restore GENI privilege credential test. Tweak regression for better reporting.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1package net.deterlab.abac.regression;
2
3import java.io.*;
4import java.util.*;
5
6import net.deterlab.abac.*;
7
8public class ReadCreds extends RegressionTest {
9    /** The credential to read */
10    protected String credName;
11    /** The ID to read */
12    protected String idName;
13    /** The expected number of creds to import from credname */
14    protected int ncreds;
15    /** The expected number of identities in the credential */
16    protected int nids;
17    /**
18     * Create a new Credential/Identity reading test for the given credential
19     * file name.
20     * @param cname a String containing filename of the credential
21     * @param idname a String containing filename of the Identity
22     * @param nc an int containing number of credentials created from cname
23     * @param ni an int containing number of identities expected in cname
24     */
25     public ReadCreds(String cname, String idname, int nc, int ni) { 
26         super("Read credential " +cname);
27         credName = cname;
28         idName = idname;
29         ncreds = nc;
30         nids = ni;
31     }
32
33    /**
34     * Create a new Credential/Identity reading test for the given credential
35     * file name.
36     * @param cname a String containing filename of the credential
37     * @param idname a String containing filename of the Identity
38     * @param nc an int containing number of credentials created from cname
39     */
40     public ReadCreds(String cname, String idname, int nc) { 
41         this(cname, idname, nc, 1);
42     }
43
44     /**
45      * Copy the bytes from from to to.
46      * @param from a File, the source file
47      * @param to a File, the destination file.
48      * @throws IOException if something goes wrong
49      */
50     public void copyFile(File from, File to) throws IOException {
51         FileInputStream fromStream = new FileInputStream(from);
52         FileOutputStream toStream = new FileOutputStream(to);
53         byte[] buf = new byte[4096];
54         int len = 0;
55
56         while ( (len = fromStream.read(buf)) != -1 ) 
57             toStream.write(buf, 0, len);
58         fromStream.close();
59         toStream.close();
60     }
61
62     /**
63      * Compare the contents of a and b, returning true if they all match.
64      * @param a a File to be compared
65      * @param b a File to be compared
66      * @return true if all bytes are the same
67      * @throws IOException if there is a problem reading the files
68      */
69     public boolean compareFile(File a, File b) throws IOException {
70         FileInputStream aStream = new FileInputStream(a);
71         FileInputStream bStream = new FileInputStream(b);
72         byte[] aBuf = new byte[4096];
73         byte[] bBuf = new byte[4096];
74         int len = 0;
75
76         while ( (len = aStream.read(aBuf)) != -1 ) {
77             int l = 0;
78             int t = 0;
79             while ( (t = bStream.read(bBuf, l, len - l)) != -1 && l < len ) 
80                 l += t;
81             if (l != len) return false;
82             for (int i=0 ; i< len; i++) 
83                 if (aBuf[i] != bBuf[i]) return false;
84         }
85         return true;
86     }
87
88
89    /**
90     * Copy credName and idName from scratch, load them into a context, making
91     * sure the right number of credentials come out, and save them to scratch.
92     * Compare the input and output files.
93     * @param data a File pointing to a directory that contains files the test
94     * may need
95     * @param scratch a File pointing to a directory that the test can use to
96     * store data
97     * @return a boolean, true if the test is passed
98     */
99    public boolean runTest(File data, File scratch) {
100        try {
101            Context ctxt = new Context();
102            File origCred = new File(scratch, credName);
103            File origID = new File(scratch, idName);
104
105            if ( credName != null ) 
106                copyFile(new File(data, credName), origCred);
107            if ( idName != null ) 
108                copyFile(new File(data, idName), origID);
109
110
111            ctxt.load_directory(scratch);
112            Collection<Identity> ids = ctxt.identities();
113            Collection<Credential> creds = ctxt.credentials();
114
115            if ( ids.size() != nids) {
116                setReason("Different number of identities read.  Got " + 
117                        ids.size() + " expected: " + nids);
118                return false;
119            }
120            if ( creds.size() != ncreds) {
121                setReason("Different number of credentials read. Got: " + 
122                        creds.size() + " expected: " + ncreds);
123                return false;
124            }
125            File credFn = new File(scratch, credName + ".new");
126            File idFn = new File(scratch, idName + ".new");
127
128            for ( Identity i: ids ) 
129                i.write(idFn.toString());
130
131            for (Credential c: creds )
132                c.write(credFn.toString());
133
134            if ( ! compareFile(credFn, origCred)) 
135                setReason("Credential files differ");
136            if ( idName != null && ! compareFile(idFn, origID)) 
137                setReason("Identity files differ");
138
139        }
140        catch (Exception e) {
141            setReason(e.getMessage());
142            return false;
143        }
144        return true;
145    }
146}
Note: See TracBrowser for help on using the repository browser.