package net.deterlab.abac.regression; import java.io.*; import java.util.*; import net.deterlab.abac.*; public class ReadCreds extends RegressionTest { protected String credName; protected String idName; protected int ncreds; /** * Create a new Credential/Identity reading test for the given credential * file name. * @param cn a String containing the binary name of the class to test */ public ReadCreds(String cname, String idname, int nc) { super("Read credential " +cname); credName = cname; idName = idname; ncreds = nc; } public void copyFile(File from, File to) throws IOException { FileInputStream fromStream = new FileInputStream(from); FileOutputStream toStream = new FileOutputStream(to); byte[] buf = new byte[4096]; int len = 0; while ( (len = fromStream.read(buf)) != -1 ) toStream.write(buf, 0, len); fromStream.close(); toStream.close(); } public boolean compareFile(File a, File b) throws IOException { FileInputStream aStream = new FileInputStream(a); FileInputStream bStream = new FileInputStream(b); byte[] aBuf = new byte[4096]; byte[] bBuf = new byte[4096]; int len = 0; while ( (len = aStream.read(aBuf)) != -1 ) { int l = 0; int t = 0; while ( (t = bStream.read(bBuf, l, len - 1)) != -1 && l < len ) l += t; if (l != len) return false; for (int i=0 ; i< len; i++) if (aBuf[i] != bBuf[i]) return false; } return true; } /** * Carry out the test. Create credentials for the create_experiment * example, run the proof and make sure the correct proof is generated. * @param data a File pointing to a directory that contains files the test * may need * @param scratch a File pointing to a directory that the test can use to * store data * @return a boolean, true if the test is passed */ public boolean runTest(File data, File scratch) { try { Context ctxt = new Context(); File origCred = new File(scratch, credName); File origID = new File(scratch, idName); if ( credName != null ) copyFile(new File(data, credName), origCred); if ( idName != null ) copyFile(new File(data, idName), origID); ctxt.load_directory(scratch); Collection ids = ctxt.identities(); Collection creds = ctxt.credentials(); if ( ids.size() != 1) { setReason("Different number of identities read"); return false; } if ( creds.size() != ncreds) { setReason("Different number of credentials read: " + creds.size()); return false; } File credFn = new File(scratch, credName + ".new"); File idFn = new File(scratch, idName + ".new"); for ( Identity i: ids ) i.write(idFn.toString()); for (Credential c: creds ) c.write(credFn.toString()); if ( ! compareFile(credFn, origCred)) setReason("Credential files differ"); if ( ! compareFile(idFn, origID)) setReason("Identity files differ"); } catch (Exception e) { setReason(e.getMessage()); return false; } return true; } }