[1fb793f] | 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 { |
---|
[f0449d1] | 9 | /** The credential to read */ |
---|
[1fb793f] | 10 | protected String credName; |
---|
[f0449d1] | 11 | /** The ID to read */ |
---|
[1fb793f] | 12 | protected String idName; |
---|
[f0449d1] | 13 | /** The expected number of creds to import from credname */ |
---|
[1fb793f] | 14 | protected int ncreds; |
---|
[28d6d08] | 15 | /** The expected number of identities in the credential */ |
---|
| 16 | protected int nids; |
---|
[1fb793f] | 17 | /** |
---|
| 18 | * Create a new Credential/Identity reading test for the given credential |
---|
| 19 | * file name. |
---|
[f0449d1] | 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 |
---|
[28d6d08] | 23 | * @param ni an int containing number of identities expected in cname |
---|
[1fb793f] | 24 | */ |
---|
[28d6d08] | 25 | public ReadCreds(String cname, String idname, int nc, int ni) { |
---|
[1fb793f] | 26 | super("Read credential " +cname); |
---|
| 27 | credName = cname; |
---|
| 28 | idName = idname; |
---|
| 29 | ncreds = nc; |
---|
[28d6d08] | 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); |
---|
[1fb793f] | 42 | } |
---|
| 43 | |
---|
[f0449d1] | 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 | */ |
---|
[1fb793f] | 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 | |
---|
[f0449d1] | 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 | */ |
---|
[1fb793f] | 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; |
---|
[3e573fb] | 79 | while ( (t = bStream.read(bBuf, l, len - l)) != -1 && l < len ) |
---|
[1fb793f] | 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 | /** |
---|
[f0449d1] | 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. |
---|
[1fb793f] | 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 | |
---|
[28d6d08] | 115 | if ( ids.size() != nids) { |
---|
| 116 | setReason("Different number of identities read. Got " + |
---|
| 117 | ids.size() + " expected: " + nids); |
---|
[1fb793f] | 118 | return false; |
---|
| 119 | } |
---|
| 120 | if ( creds.size() != ncreds) { |
---|
[28d6d08] | 121 | setReason("Different number of credentials read. Got: " + |
---|
| 122 | creds.size() + " expected: " + ncreds); |
---|
[1fb793f] | 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"); |
---|
[28d6d08] | 136 | if ( idName != null && ! compareFile(idFn, origID)) |
---|
[1fb793f] | 137 | setReason("Identity files differ"); |
---|
| 138 | |
---|
| 139 | } |
---|
| 140 | catch (Exception e) { |
---|
| 141 | setReason(e.getMessage()); |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | } |
---|