[8375d4f] | 1 | package net.deterlab.abac.regression; |
---|
| 2 | |
---|
| 3 | import java.io.*; |
---|
| 4 | import java.util.*; |
---|
| 5 | |
---|
| 6 | import net.deterlab.abac.*; |
---|
| 7 | |
---|
[5b277d6] | 8 | public class WriteCreds extends RegressionTest { |
---|
| 9 | protected String className; |
---|
[8375d4f] | 10 | /** |
---|
| 11 | * Put out an identity and key into dir named by the name of the Identity |
---|
| 12 | * @param i the Identity to write |
---|
| 13 | * @param dir a File holding the destination directory |
---|
| 14 | * @throws IOException if the file writing fails |
---|
| 15 | */ |
---|
| 16 | public void writeCombinedIdentity(Identity i, File dir) |
---|
| 17 | throws IOException { |
---|
| 18 | FileOutputStream f = new FileOutputStream( |
---|
| 19 | new File(dir, i.getName() + ".pem")); |
---|
| 20 | |
---|
| 21 | i.write(f); |
---|
| 22 | i.writePrivateKey(f); |
---|
| 23 | } |
---|
| 24 | /** |
---|
[d31242c] | 25 | * Create a new Credential/Identity writing test for the given class of |
---|
| 26 | * credentials. |
---|
| 27 | * @param cn a String containing the binary name of the class to test |
---|
[8375d4f] | 28 | */ |
---|
[5b277d6] | 29 | public WriteCreds(String cn) { |
---|
| 30 | super(cn.substring( |
---|
| 31 | cn.lastIndexOf('.') == -1 ? 0 : cn.lastIndexOf('.') +1 ) + |
---|
| 32 | " credential writing test"); |
---|
| 33 | className = cn; |
---|
| 34 | } |
---|
[8375d4f] | 35 | |
---|
| 36 | /** |
---|
| 37 | * Carry out the test. Create credentials for the create_experiment |
---|
| 38 | * example, run the proof and make sure the correct proof is generated. |
---|
| 39 | * @param data a File pointing to a directory that contains files the test |
---|
| 40 | * may need |
---|
| 41 | * @param scratch a File pointing to a directory that the test can use to |
---|
| 42 | * store data |
---|
| 43 | * @return a boolean, true if the test is passed |
---|
| 44 | */ |
---|
| 45 | public boolean runTest(File data, File scratch) { |
---|
| 46 | try { |
---|
| 47 | Context ctxt = new Context(); |
---|
| 48 | Identity acme = new Identity("Acme"); |
---|
| 49 | Identity globotron = new Identity("Globotron"); |
---|
| 50 | Identity alice = new Identity("Alice"); |
---|
| 51 | Identity bob = new Identity("Bob"); |
---|
| 52 | Vector<Identity> ids = new Vector<Identity>(); |
---|
| 53 | Vector<Credential> creds = new Vector<Credential>(); |
---|
| 54 | Collections.addAll(ids, acme, globotron, alice, bob); |
---|
| 55 | |
---|
| 56 | for ( Identity i: ids) |
---|
| 57 | writeCombinedIdentity(i, scratch); |
---|
| 58 | |
---|
| 59 | ctxt.setCredentialFactory(new CredentialFactory( |
---|
[d31242c] | 60 | new String[] { className})); |
---|
[8375d4f] | 61 | |
---|
| 62 | |
---|
| 63 | Credential c = ctxt.newCredential( |
---|
| 64 | new Role(acme.getKeyID() + ".experiment_create"), |
---|
| 65 | new Role(acme.getKeyID() + ".partner.experiment_create")); |
---|
| 66 | c.make_cert(acme); |
---|
| 67 | creds.add(c); |
---|
| 68 | c = ctxt.newCredential( |
---|
| 69 | new Role(acme.getKeyID() + ".partner"), |
---|
| 70 | new Role(globotron.getKeyID())); |
---|
| 71 | c.make_cert(acme); |
---|
| 72 | creds.add(c); |
---|
| 73 | c = ctxt.newCredential( |
---|
| 74 | new Role(globotron.getKeyID() + ".experiment_create"), |
---|
| 75 | new Role(globotron.getKeyID() + ".admin.power_user")); |
---|
| 76 | c.make_cert(globotron); |
---|
| 77 | creds.add(c); |
---|
| 78 | c = ctxt.newCredential( |
---|
| 79 | new Role(globotron.getKeyID() + ".admin"), |
---|
| 80 | new Role(alice.getKeyID())); |
---|
| 81 | c.make_cert(globotron); |
---|
| 82 | creds.add(c); |
---|
| 83 | c = ctxt.newCredential( |
---|
| 84 | new Role(alice.getKeyID() + ".power_user"), |
---|
| 85 | new Role(bob.getKeyID())); |
---|
| 86 | c.make_cert(alice); |
---|
| 87 | creds.add(c); |
---|
| 88 | |
---|
| 89 | int i =0; |
---|
| 90 | for (Credential cc: creds) { |
---|
[d31242c] | 91 | cc.write(new File(scratch,"e" + i + cc.getSuffix()).toString()); |
---|
| 92 | i++; |
---|
[8375d4f] | 93 | } |
---|
| 94 | |
---|
| 95 | Context ctxt2 = new Context(); |
---|
| 96 | |
---|
| 97 | ctxt2.load_directory(scratch); |
---|
| 98 | Collection<Identity> nids = ctxt2.identities(); |
---|
| 99 | Collection<Credential> ncreds = ctxt2.credentials(); |
---|
| 100 | |
---|
| 101 | if ( nids.size() != ids.size()) { |
---|
| 102 | setReason("Different number of identities read"); |
---|
| 103 | return false; |
---|
| 104 | } |
---|
| 105 | for (Identity ii: ids ) |
---|
| 106 | if ( !nids.contains(ii)) { |
---|
| 107 | setReason("Identity " + ii + " not read successfully"); |
---|
| 108 | return false; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | if ( ncreds.size() != creds.size()) { |
---|
| 112 | setReason("Different number of credentials read"); |
---|
| 113 | return false; |
---|
| 114 | } |
---|
| 115 | for (Credential cc: creds ) |
---|
| 116 | if ( !ncreds.contains(cc)) { |
---|
| 117 | setReason("Credential " + cc + " not read successfully"); |
---|
| 118 | return false; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | } |
---|
| 122 | catch (Exception e) { |
---|
| 123 | setReason(e.getMessage()); |
---|
| 124 | return false; |
---|
| 125 | } |
---|
| 126 | return true; |
---|
| 127 | } |
---|
| 128 | } |
---|