1 | package net.deterlab.abac.regression; |
---|
2 | |
---|
3 | import java.io.*; |
---|
4 | import java.util.*; |
---|
5 | |
---|
6 | import net.deterlab.abac.*; |
---|
7 | |
---|
8 | public class ReadCreds extends RegressionTest { |
---|
9 | protected String credName; |
---|
10 | protected String idName; |
---|
11 | protected int ncreds; |
---|
12 | /** |
---|
13 | * Create a new Credential/Identity reading test for the given credential |
---|
14 | * file name. |
---|
15 | * @param cn a String containing the binary name of the class to test |
---|
16 | */ |
---|
17 | public ReadCreds(String cname, String idname, int nc) { |
---|
18 | super("Read credential " +cname); |
---|
19 | credName = cname; |
---|
20 | idName = idname; |
---|
21 | ncreds = nc; |
---|
22 | } |
---|
23 | |
---|
24 | public void copyFile(File from, File to) throws IOException { |
---|
25 | FileInputStream fromStream = new FileInputStream(from); |
---|
26 | FileOutputStream toStream = new FileOutputStream(to); |
---|
27 | byte[] buf = new byte[4096]; |
---|
28 | int len = 0; |
---|
29 | |
---|
30 | while ( (len = fromStream.read(buf)) != -1 ) |
---|
31 | toStream.write(buf, 0, len); |
---|
32 | fromStream.close(); |
---|
33 | toStream.close(); |
---|
34 | } |
---|
35 | |
---|
36 | public boolean compareFile(File a, File b) throws IOException { |
---|
37 | FileInputStream aStream = new FileInputStream(a); |
---|
38 | FileInputStream bStream = new FileInputStream(b); |
---|
39 | byte[] aBuf = new byte[4096]; |
---|
40 | byte[] bBuf = new byte[4096]; |
---|
41 | int len = 0; |
---|
42 | |
---|
43 | while ( (len = aStream.read(aBuf)) != -1 ) { |
---|
44 | int l = 0; |
---|
45 | int t = 0; |
---|
46 | while ( (t = bStream.read(bBuf, l, len - 1)) != -1 && l < len ) |
---|
47 | l += t; |
---|
48 | if (l != len) return false; |
---|
49 | for (int i=0 ; i< len; i++) |
---|
50 | if (aBuf[i] != bBuf[i]) return false; |
---|
51 | } |
---|
52 | return true; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * Carry out the test. Create credentials for the create_experiment |
---|
58 | * example, run the proof and make sure the correct proof is generated. |
---|
59 | * @param data a File pointing to a directory that contains files the test |
---|
60 | * may need |
---|
61 | * @param scratch a File pointing to a directory that the test can use to |
---|
62 | * store data |
---|
63 | * @return a boolean, true if the test is passed |
---|
64 | */ |
---|
65 | public boolean runTest(File data, File scratch) { |
---|
66 | try { |
---|
67 | Context ctxt = new Context(); |
---|
68 | File origCred = new File(scratch, credName); |
---|
69 | File origID = new File(scratch, idName); |
---|
70 | |
---|
71 | if ( credName != null ) |
---|
72 | copyFile(new File(data, credName), origCred); |
---|
73 | if ( idName != null ) |
---|
74 | copyFile(new File(data, idName), origID); |
---|
75 | |
---|
76 | |
---|
77 | ctxt.load_directory(scratch); |
---|
78 | Collection<Identity> ids = ctxt.identities(); |
---|
79 | Collection<Credential> creds = ctxt.credentials(); |
---|
80 | |
---|
81 | if ( ids.size() != 1) { |
---|
82 | setReason("Different number of identities read"); |
---|
83 | return false; |
---|
84 | } |
---|
85 | if ( creds.size() != ncreds) { |
---|
86 | setReason("Different number of credentials read: " + |
---|
87 | creds.size()); |
---|
88 | return false; |
---|
89 | } |
---|
90 | File credFn = new File(scratch, credName + ".new"); |
---|
91 | File idFn = new File(scratch, idName + ".new"); |
---|
92 | |
---|
93 | for ( Identity i: ids ) |
---|
94 | i.write(idFn.toString()); |
---|
95 | |
---|
96 | for (Credential c: creds ) |
---|
97 | c.write(credFn.toString()); |
---|
98 | |
---|
99 | if ( ! compareFile(credFn, origCred)) |
---|
100 | setReason("Credential files differ"); |
---|
101 | if ( ! compareFile(idFn, origID)) |
---|
102 | setReason("Identity files differ"); |
---|
103 | |
---|
104 | } |
---|
105 | catch (Exception e) { |
---|
106 | setReason(e.getMessage()); |
---|
107 | return false; |
---|
108 | } |
---|
109 | return true; |
---|
110 | } |
---|
111 | } |
---|