source: java/net/deterlab/abac/regression/ReadCreds.java @ 3c30b59

abac0-leakabac0-mei
Last change on this file since 3c30b59 was 3e573fb, checked in by Ted Faber <faber@…>, 12 years ago

One vs. Ell. Wow.

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