[c694dff] | 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 ExperimentTest extends RegressionTest { |
---|
| 9 | /** |
---|
| 10 | * Create a new Credential/Identity writing test for the given class of |
---|
| 11 | * credentials. |
---|
| 12 | * @param cn a String containing the binary name of the class to test |
---|
| 13 | */ |
---|
| 14 | public ExperimentTest(String n) { |
---|
| 15 | super(n); |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Carry out the test. Create credentials for the create_experiment |
---|
| 20 | * example, run the proof and make sure the correct proof is generated. |
---|
| 21 | * @param data a File pointing to a directory that contains files the test |
---|
| 22 | * may need |
---|
| 23 | * @param scratch a File pointing to a directory that the test can use to |
---|
| 24 | * store data |
---|
| 25 | * @return a boolean, true if the test is passed |
---|
| 26 | */ |
---|
| 27 | public boolean runTest(File data, File scratch) { |
---|
| 28 | try { |
---|
| 29 | Context ctxt = new Context(); |
---|
| 30 | Identity acme = new Identity("Acme"); |
---|
| 31 | Identity globotron = new Identity("Globotron"); |
---|
| 32 | Identity alice = new Identity("Alice"); |
---|
| 33 | Identity bob = new Identity("Bob"); |
---|
| 34 | Vector<Identity> ids = new Vector<Identity>(); |
---|
| 35 | Vector<Credential> creds = new Vector<Credential>(); |
---|
| 36 | Collections.addAll(ids, acme, globotron, alice, bob); |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | Credential c = ctxt.newCredential( |
---|
| 40 | new Role(acme.getKeyID() + ".experiment_create"), |
---|
| 41 | new Role(acme.getKeyID() + ".partner.experiment_create")); |
---|
| 42 | c.make_cert(acme); |
---|
| 43 | creds.add(c); |
---|
| 44 | c = ctxt.newCredential( |
---|
| 45 | new Role(acme.getKeyID() + ".partner"), |
---|
| 46 | new Role(globotron.getKeyID())); |
---|
| 47 | c.make_cert(acme); |
---|
| 48 | creds.add(c); |
---|
| 49 | c = ctxt.newCredential( |
---|
| 50 | new Role(globotron.getKeyID() + ".experiment_create"), |
---|
| 51 | new Role(globotron.getKeyID() + ".admin.power_user")); |
---|
| 52 | c.make_cert(globotron); |
---|
| 53 | creds.add(c); |
---|
| 54 | c = ctxt.newCredential( |
---|
| 55 | new Role(globotron.getKeyID() + ".admin"), |
---|
| 56 | new Role(alice.getKeyID())); |
---|
| 57 | c.make_cert(globotron); |
---|
| 58 | creds.add(c); |
---|
| 59 | c = ctxt.newCredential( |
---|
| 60 | new Role(alice.getKeyID() + ".power_user"), |
---|
| 61 | new Role(bob.getKeyID())); |
---|
| 62 | c.make_cert(alice); |
---|
| 63 | creds.add(c); |
---|
| 64 | |
---|
| 65 | for (Identity id: ids ) |
---|
| 66 | ctxt.load_id_chunk(id); |
---|
| 67 | for (Credential cc: creds ) |
---|
| 68 | ctxt.load_attribute_chunk(cc); |
---|
| 69 | |
---|
[736a573] | 70 | /* For proof checking. These internal credentials will be created |
---|
| 71 | * inside the context */ |
---|
| 72 | creds.add(new InternalCredential( |
---|
| 73 | new Role(acme.getKeyID() + ".partner.experiment_create"), |
---|
| 74 | new Role(globotron.getKeyID() + ".experiment_create"))); |
---|
| 75 | creds.add(new InternalCredential( |
---|
| 76 | new Role(globotron.getKeyID() + ".admin.power_user"), |
---|
| 77 | new Role(alice.getKeyID() + ".power_user"))); |
---|
| 78 | |
---|
[c694dff] | 79 | Context.QueryResult q = ctxt.query( |
---|
| 80 | acme.getKeyID() + ".experiment_create", |
---|
| 81 | bob.getKeyID()); |
---|
| 82 | |
---|
| 83 | if ( !q.getSuccess() ) { |
---|
| 84 | setReason("Could not prove Bob can create experiment"); |
---|
| 85 | return false; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if ( creds.size() != q.getCredentials().size()) { |
---|
[736a573] | 89 | for (Credential cc : q.getCredentials()) |
---|
| 90 | System.out.println(cc.simpleString(ctxt)); |
---|
[c694dff] | 91 | setReason("Bob's proof is the wrong size"); |
---|
| 92 | return false; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | for ( Credential cc: creds ) |
---|
| 96 | if ( !q.getCredentials().contains(cc)) { |
---|
| 97 | setReason("Credential missing from proof: " + cc); |
---|
| 98 | return false; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | q = ctxt.query( |
---|
| 102 | acme.getKeyID() + ".experiment_create", |
---|
| 103 | alice.getKeyID()); |
---|
| 104 | |
---|
| 105 | if ( q.getSuccess() ) { |
---|
| 106 | setReason("Could prove Alice can create experiment"); |
---|
| 107 | return false; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | catch (Exception e) { |
---|
| 112 | setReason(e.getMessage()); |
---|
| 113 | return false; |
---|
| 114 | } |
---|
| 115 | return true; |
---|
| 116 | } |
---|
| 117 | } |
---|