[8780cbec] | 1 | package edu.stanford.peer.rbtm.test; |
---|
| 2 | |
---|
| 3 | import java.io.*; |
---|
| 4 | import java.util.*; |
---|
| 5 | |
---|
| 6 | import edu.stanford.peer.rbtm.RBTMConstants; |
---|
| 7 | import edu.stanford.peer.rbtm.engine.*; |
---|
| 8 | import edu.stanford.peer.rbtm.credential.*; |
---|
| 9 | import edu.stanford.peer.rbtm.util.*; |
---|
| 10 | |
---|
| 11 | public class TestEngine implements RBTMConstants { |
---|
| 12 | |
---|
| 13 | static HashSet credSet; |
---|
| 14 | |
---|
| 15 | static GraphEngine mgr, oppoEngine, sensEngine, simpEngine; |
---|
| 16 | |
---|
| 17 | static PrintStream out, oppoOut, sensOut, simpOut; |
---|
| 18 | |
---|
| 19 | static String outPrefix = "frontier"; |
---|
| 20 | |
---|
| 21 | static void createOutput(String prefix) { |
---|
| 22 | if(prefix == null || prefix.length() == 0) |
---|
| 23 | prefix = outPrefix; |
---|
| 24 | try { |
---|
| 25 | out = new PrintStream(new FileOutputStream(prefix + "-cred.txt")); |
---|
| 26 | oppoOut = new PrintStream(new FileOutputStream(prefix + "-oppo.txt")); |
---|
| 27 | sensOut = new PrintStream(new FileOutputStream(prefix + "-sens.txt")); |
---|
| 28 | simpOut = new PrintStream(new FileOutputStream(prefix + "-simp.txt")); |
---|
| 29 | } |
---|
| 30 | catch(Exception ex) { |
---|
| 31 | ex.printStackTrace(); |
---|
| 32 | } |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | static void parseSection(BufferedReader in, HashMap conf) |
---|
| 36 | throws Exception { |
---|
| 37 | HashSet set = new HashSet(); |
---|
| 38 | String name = null; |
---|
| 39 | try { |
---|
| 40 | String heading = in.readLine(); |
---|
| 41 | while(heading.startsWith("#")) { |
---|
| 42 | System.out.println("Skipping over: " + heading); |
---|
| 43 | heading = in.readLine(); |
---|
| 44 | } |
---|
| 45 | StringTokenizer st = new StringTokenizer(heading, "[]"); |
---|
| 46 | name = st.nextToken().trim(); |
---|
| 47 | System.out.println("Processing section " + name); |
---|
| 48 | conf.put(name, set); |
---|
| 49 | while(true) { |
---|
| 50 | String line = in.readLine().trim(); |
---|
| 51 | if(line.length() < 1) |
---|
| 52 | return; |
---|
| 53 | if(!line.startsWith("#")) { |
---|
| 54 | System.out.println("line = '" + line + "'"); |
---|
| 55 | set.add(line); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | catch(EOFException eof) { |
---|
| 60 | conf.put(name, set); |
---|
| 61 | throw eof; |
---|
| 62 | } |
---|
| 63 | catch(Exception ex) { |
---|
| 64 | //ex.printStackTrace(); |
---|
| 65 | throw(ex); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public static HashMap loadConfiguration(String name) { |
---|
| 70 | HashMap confMap = new HashMap(6); |
---|
| 71 | try { |
---|
| 72 | BufferedReader in = new BufferedReader(new FileReader(name)); |
---|
| 73 | while(true) { |
---|
| 74 | parseSection(in, confMap); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | catch(Exception ex) { |
---|
| 78 | //ex.printStackTrace(); |
---|
| 79 | return confMap; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | static HashSet loadCredentials(HashMap confs) { |
---|
| 85 | HashSet credSet = new HashSet(); |
---|
| 86 | |
---|
| 87 | try { |
---|
| 88 | Iterator i = ((HashSet)confs.get("credentials")).iterator(); |
---|
| 89 | while(i.hasNext()) { |
---|
| 90 | credSet.add(new StaticCredential((String)i.next())); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | catch(Exception ex) { |
---|
| 94 | //ex.printStackTrace(); |
---|
| 95 | return credSet; |
---|
| 96 | } |
---|
| 97 | return credSet; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | static HashSet loadTraceable(HashMap conf) { |
---|
| 101 | HashSet names = new HashSet(); |
---|
| 102 | |
---|
| 103 | try { |
---|
| 104 | Iterator i = ((HashSet)conf.get("traceable")).iterator(); |
---|
| 105 | while(i.hasNext()) { |
---|
| 106 | names.add(new SimpleRoleName((String)i.next())); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | catch(Exception ex) { |
---|
| 110 | ex.printStackTrace(); |
---|
| 111 | return names; |
---|
| 112 | } |
---|
| 113 | System.out.println("traceable: " + names); |
---|
| 114 | return names; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | static HashSet loadSensitive(HashMap conf) { |
---|
| 118 | HashSet sens = new HashSet(); |
---|
| 119 | |
---|
| 120 | try { |
---|
| 121 | Iterator i = ((HashSet)conf.get("sensitive")).iterator(); |
---|
| 122 | while(i.hasNext()) { |
---|
| 123 | sens.add(StaticCredential.getRole((String)i.next())); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | catch(Exception ex) { |
---|
| 127 | ex.printStackTrace(); |
---|
| 128 | return sens; |
---|
| 129 | } |
---|
| 130 | System.out.println("sensitive: " + sens); |
---|
| 131 | return sens; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | static void createEngines(HashMap conf) { |
---|
| 136 | Oppo oppo = new Oppo(new Vector(loadTraceable(conf))); |
---|
| 137 | Sens sens = new Sens(new Vector(loadSensitive(conf))); |
---|
| 138 | Simp simp = new Simp(); |
---|
| 139 | int track = TRACK_ALL; |
---|
| 140 | |
---|
| 141 | System.out.println("------> Credential Graph"); |
---|
| 142 | mgr = new GraphEngine(credSet, track, null, null); |
---|
| 143 | System.out.println("------> Opponent Graph"); |
---|
| 144 | oppoEngine = new GraphEngine(credSet, track, null, oppo); |
---|
| 145 | System.out.println("------> Sensitive Graph"); |
---|
| 146 | sensEngine = new GraphEngine(credSet, track, null, sens); |
---|
| 147 | System.out.println("------> Simple Graph"); |
---|
| 148 | simpEngine = new GraphEngine(credSet, track, null, simp); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | static void searchExpression |
---|
| 152 | (EntityExpression child, GraphEngine graph, PrintStream out) { |
---|
| 153 | ResultEvidenceMap map = graph.backwardSearch(child); |
---|
| 154 | Iterator i = map.resultSet().iterator(); |
---|
| 155 | while(i.hasNext()) { |
---|
| 156 | EntityExpression ancestor = (EntityExpression)i.next(); |
---|
| 157 | HashSet chain = graph.getChain(child, ancestor); |
---|
| 158 | Iterator credentials = chain.iterator(); |
---|
| 159 | |
---|
| 160 | out.println("Credential chain for" + child + "<--" + ancestor ); |
---|
| 161 | while(credentials.hasNext()) { |
---|
| 162 | out.print("\t"); |
---|
| 163 | out.println(credentials.next()); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | static void searchExpression(EntityExpression expr) { |
---|
| 169 | System.out.println("[cred for " + expr + "]"); |
---|
| 170 | out.println("Credential Graph of " + expr + " = " + |
---|
| 171 | mgr.backwardSearch(expr)); |
---|
| 172 | searchExpression(expr, mgr, out); |
---|
| 173 | System.out.println("[oppo for " + expr + "]"); |
---|
| 174 | oppoOut.println("Oppo Frontier of " + expr + " = " + |
---|
| 175 | oppoEngine.backwardSearch(expr)); |
---|
| 176 | oppoOut.println("[oppo chains for " + expr + "]"); |
---|
| 177 | searchExpression(expr, oppoEngine, oppoOut); |
---|
| 178 | System.out.println("[sens for " + expr + "]"); |
---|
| 179 | sensOut.println("Sens Frontier of " + expr + " = " + |
---|
| 180 | sensEngine.backwardSearch(expr)); |
---|
| 181 | sensOut.println("[sens chains for " + expr + "]"); |
---|
| 182 | searchExpression(expr, sensEngine, sensOut); |
---|
| 183 | System.out.println("[simp for " + expr + "]"); |
---|
| 184 | simpOut.println("Simp Frontier of " + expr + " = " + |
---|
| 185 | simpEngine.backwardSearch(expr)); |
---|
| 186 | simpOut.println("[simp chains for " + expr + "]"); |
---|
| 187 | searchExpression(expr, sensEngine, sensOut); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | static void performSearches(HashMap conf) { |
---|
| 191 | String line; |
---|
| 192 | EntityExpression expr; |
---|
| 193 | |
---|
| 194 | try { |
---|
| 195 | Iterator i = ((HashSet)conf.get("searches")).iterator(); |
---|
| 196 | while(i.hasNext()) { |
---|
| 197 | expr = StaticCredential.getEntityExpression |
---|
| 198 | (((String)i.next()).trim()); |
---|
| 199 | searchExpression(expr); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | catch(Exception ex) { |
---|
| 203 | ex.printStackTrace(); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | public static void main(String arg[]) { |
---|
| 212 | HashMap conf = loadConfiguration((arg.length > 0)?arg[0]:"abac.conf"); |
---|
| 213 | credSet = loadCredentials(conf); |
---|
| 214 | |
---|
| 215 | createEngines(conf); |
---|
| 216 | createOutput("frontier"); |
---|
| 217 | performSearches(conf); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|