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 WriteCreds extends RegressionTest { |
---|
9 | protected String className; |
---|
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 | /** |
---|
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 |
---|
28 | */ |
---|
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 | } |
---|
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", 10L*365L*24L*3600L); |
---|
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( |
---|
60 | new String[] { className})); |
---|
61 | |
---|
62 | for (Identity id: new Identity[] { acme, globotron, alice, bob } ) |
---|
63 | ctxt.load_id_chunk(id.getCertificate()); |
---|
64 | |
---|
65 | Credential c = ctxt.newCredential( |
---|
66 | new Role(acme.getKeyID() + ".experiment_create"), |
---|
67 | new Role(acme.getKeyID() + ".partner.experiment_create")); |
---|
68 | c.make_cert(acme); |
---|
69 | creds.add(c); |
---|
70 | c = ctxt.newCredential( |
---|
71 | new Role(acme.getKeyID() + ".partner"), |
---|
72 | new Role(globotron.getKeyID())); |
---|
73 | c.make_cert(acme); |
---|
74 | creds.add(c); |
---|
75 | c = ctxt.newCredential( |
---|
76 | new Role(globotron.getKeyID() + ".experiment_create"), |
---|
77 | new Role(globotron.getKeyID() + ".admin.power_user")); |
---|
78 | c.make_cert(globotron); |
---|
79 | creds.add(c); |
---|
80 | c = ctxt.newCredential( |
---|
81 | new Role(globotron.getKeyID() + ".admin"), |
---|
82 | new Role(alice.getKeyID())); |
---|
83 | c.make_cert(globotron); |
---|
84 | creds.add(c); |
---|
85 | c = ctxt.newCredential( |
---|
86 | new Role(alice.getKeyID() + ".power_user"), |
---|
87 | new Role(bob.getKeyID())); |
---|
88 | c.make_cert(alice); |
---|
89 | creds.add(c); |
---|
90 | |
---|
91 | int i =0; |
---|
92 | for (Credential cc: creds) { |
---|
93 | cc.write(new File(scratch,"e" + i + cc.getSuffix()).toString()); |
---|
94 | i++; |
---|
95 | } |
---|
96 | |
---|
97 | Context ctxt2 = new Context(); |
---|
98 | |
---|
99 | ctxt2.load_directory(scratch); |
---|
100 | Collection<Identity> nids = ctxt2.identities(); |
---|
101 | Collection<Credential> ncreds = ctxt2.credentials(); |
---|
102 | |
---|
103 | if ( nids.size() != ids.size()) { |
---|
104 | setReason("Different number of identities read " + nids.size() + |
---|
105 | " expected " + ids.size()); |
---|
106 | return false; |
---|
107 | } |
---|
108 | for (Identity ii: ids ) |
---|
109 | if ( !nids.contains(ii)) { |
---|
110 | setReason("Identity " + ii + " not read successfully"); |
---|
111 | return false; |
---|
112 | } |
---|
113 | |
---|
114 | if ( ncreds.size() != creds.size()) { |
---|
115 | setReason("Different number of credentials read " + |
---|
116 | ncreds.size() + " expecting " + creds.size()); |
---|
117 | return false; |
---|
118 | } |
---|
119 | for (Credential cc: creds ) |
---|
120 | if ( !ncreds.contains(cc)) { |
---|
121 | setReason("Credential " + cc + " not read successfully"); |
---|
122 | return false; |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|
126 | catch (Exception e) { |
---|
127 | setReason(e.getMessage()); |
---|
128 | return false; |
---|
129 | } |
---|
130 | return true; |
---|
131 | } |
---|
132 | } |
---|