source: java/GraphTest.java @ 39526ce

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 39526ce was 39526ce, checked in by Ted Faber <faber@…>, 13 years ago

More remove File dependencies for read/write

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[31b67d5]1import java.io.*;
[7ef13e3]2import java.util.*;
[31b67d5]3
4import edu.uci.ics.jung.graph.*;
5
6import net.deterlab.abac.Credential;
[84f0e7a]7import net.deterlab.abac.Context;
[31b67d5]8import net.deterlab.abac.Role;
[9725efb]9import net.deterlab.abac.Identity;
[31b67d5]10
[7ef13e3]11import java.security.KeyPair;
12
13
[31b67d5]14/**
15 * Simple test of the native Java implementation of ABAC. Loads credentials
16 * from an rt0 file and runs a query against them.
17 */
18public class GraphTest {
19    public static void main(String[] args) throws IOException {
20        if (args.length < 3) {
[7ef13e3]21            System.out.println("Usage: GraphTest <files> <role> <principal>");
[31b67d5]22            System.out.println("    runs the query role <-?- principal and prints the result");
23            System.exit(1);
24        }
25
[84f0e7a]26        Context ctxt = new Context();
[5cf72cc]27        Map<String, Exception> errs = new HashMap<String, Exception>();
[31b67d5]28
[7ef13e3]29        for (int i= 0; i < args.length-2; i++) {
30            File f = new File(args[i]);
[31b67d5]31
[7ef13e3]32            try {
33                if (f.isDirectory()) 
[6432e35]34                    ctxt.load_directory(f, errs);
[be05757]35                else if (f.getPath().endsWith(".pem")) 
[6432e35]36                    ctxt.load_id_file(f);
[7ef13e3]37                else if (f.getPath().endsWith(".der"))
[6432e35]38                    ctxt.load_attribute_file(f);
[5cf72cc]39                else if (f.getPath().endsWith(".zip"))
[39526ce]40                    ctxt.load_zip(f, errs);
[ad24705]41                else if (f.getPath().endsWith(".rt0"))
[39526ce]42                    ctxt.load_rt0(f, errs);
[7ef13e3]43                else
44                    System.out.println(f + " of unknown type");
45            }
46            catch (Exception e) {
47                System.err.println("Failed to process " + f + ": " +e);
48            }
49        }
[cae7369]50
[5cf72cc]51        for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f));
[be05757]52
[31b67d5]53        //
54        // run the query
55        //
56
[84f0e7a]57        Role role = new Role(args[args.length-2], ctxt);
58        Role prin = new Role(args[args.length-1], ctxt);
[1a80501]59        Context.QueryResult ret = ctxt.query(role.toString(), prin.toString());
[84f0e7a]60        Set<Identity> ids = new TreeSet<Identity>();
61
[1a7e6d3]62        String fn = "attr";
63        int n = 0;
64        String suf = ".der";
[1a80501]65        System.out.println("Result: " + ret.getSuccess());
[ad24705]66        System.out.println("Proof");
[1a80501]67        for (Credential c : ret.getCredentials()) {
[84f0e7a]68            System.out.println(c.simpleString(ctxt));
69            if ( c.hasCertificate()) {
70                c.write(fn + n++ + suf);
[d69593c]71                ids.add(c.issuer());
[84f0e7a]72            }
[1a7e6d3]73        }
[9725efb]74
[1a7e6d3]75        fn = "id";
76        n = 0;
77        suf = ".pem";
[ad24705]78        System.out.println("Identities");
[84f0e7a]79        for (Identity i: ids) {
[9725efb]80            System.out.println("ID: " + i);
[1a7e6d3]81            i.write(fn + n++ + suf);
82        }
[ad24705]83        try {
[39526ce]84            ctxt.write_zip(new File("./testout.zip"), true, true);
[ad24705]85        }
86        catch (IOException ioe) {
87            System.err.println("Could not write ZIP: " + ioe);
88        }
89        System.out.println("rt0 with keyids");
90        ctxt.write_rt0(new OutputStreamWriter(System.out), true);
[31b67d5]91    }
92}
Note: See TracBrowser for help on using the repository browser.