ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/ACL/ACLTest.java
Revision: 1.6
Committed: Mon Dec 31 02:57:00 2001 UTC (22 years, 10 months ago) by tdb
Branch: MAIN
Changes since 1.5: +6 -0 lines
Log Message:
Added an ACLDatagramSocket. Operation is much the same as the
ACLServerSocket. Also tweaked the toString() to keep the i-scream bits as
well as the more useful stuff.

File Contents

# User Rev Content
1 tdb 1.2 import java.io.*;
2     import java.net.*;
3    
4 tdb 1.1 class ACLTest {
5    
6     public static void main(String args[]) {
7 tdb 1.2 // setup ACL defaulting to DENY
8 tdb 1.1 ACL acl = new ACL(ACL.DENY);
9    
10 tdb 1.2 // add some test rules
11 tdb 1.1 acl.add(ACL.ALLOW, "*.ukc.ac.uk");
12 tdb 1.5 acl.add(ACL.ALLOW, "129.12.*.*");
13 tdb 1.2 acl.add(ACL.ALLOW, "*.bishnet.net");
14     acl.add(ACL.ALLOW, "192.168.*");
15 tdb 1.5 acl.add(ACL.ALLOW, "192.168.*.*");
16     acl.add(ACL.ALLOW, "one.two.three.four.five.six.*");
17     acl.add(ACL.ALLOW, "192.129.12.*.*");
18     acl.add(ACL.ALLOW, "129.12.*");
19     acl.add(ACL.ALLOW, "129.12.4");
20     acl.add(ACL.ALLOW, "129.*");
21     acl.add(ACL.ALLOW, "129*");
22     acl.add(ACL.ALLOW, "*");
23     acl.add(ACL.ALLOW, "");
24 tdb 1.1 // not really needed if we default to deny :)
25 tdb 1.2 //acl.add(ACL.DENY, "*");
26 tdb 1.1
27 tdb 1.3 // dump our ACL to the console before serialization
28 tdb 1.4 System.out.println("BEFORE: " + acl);
29 tdb 1.3
30     // name for our serialized ACL
31     String aclFilename = "aclfile";
32    
33     // write out our ACL to a file
34     try {
35     ObjectOutputStream ostream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(aclFilename)));
36     ostream.writeObject(acl);
37     ostream.flush();
38     ostream.close();
39     }
40     catch (Exception e) {
41     System.out.println("exception: " + e);
42     e.printStackTrace();
43     System.exit(1);
44     }
45    
46     // and read it back in :)
47     try {
48     ObjectInputStream istream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(aclFilename)));
49     acl = (ACL) istream.readObject();
50     istream.close();
51     }
52     catch (Exception e) {
53     System.out.println("exception: " + e);
54     e.printStackTrace();
55     System.exit(1);
56     }
57    
58     // dump our ACL to the console after serialization
59 tdb 1.4 System.out.println("AFTER: " + acl);
60 tdb 1.1
61 tdb 1.2 // run a few tests
62 tdb 1.1 System.out.println("killigrew.ukc.ac.uk: " + acl.check("killigrew.ukc.ac.uk"));
63     System.out.println("129.12.41.13: " + acl.check("129.12.41.13"));
64     System.out.println("kruskal.18hp.net: " + acl.check("kruskal.18hp.net"));
65     System.out.println("192.168.1.1: " + acl.check("192.168.1.1"));
66 tdb 1.2
67     // setup an ACLServerSocket putting our ACL in
68 tdb 1.6 // and an ACLDatagramSocket (which we'll test better later)
69 tdb 1.2 ACLServerSocket ss = null;
70 tdb 1.6 ACLDatagramSocket ds = null;
71 tdb 1.2 try {
72     ss = new ACLServerSocket(acl, 1337);
73 tdb 1.6 ds = new ACLDatagramSocket(acl, 1337);
74 tdb 1.2 }
75     catch(IOException e) {
76     System.out.println("exception: " + e);
77     e.printStackTrace();
78     System.exit(1);
79     }
80 tdb 1.6
81     System.out.println(ss);
82     System.out.println(ds);
83 tdb 1.2
84     // start listening
85     // valid connections (allowed by ACL) will get a message sent back
86     while(true) {
87     try {
88     Socket s = ss.accept();
89     PrintWriter writer = new PrintWriter(s.getOutputStream(), true);
90     writer.println("Connection suceeded from: " + s.getInetAddress().getHostName());
91     writer.println("Closing in 5 seconds");
92     try { Thread.sleep(5000); } catch(Exception ee) {}
93     writer.close();
94     s.close();
95     }
96     catch(IOException e) {
97     System.out.println("exception: " + e);
98     e.printStackTrace();
99     System.exit(1);
100     }
101     }
102 tdb 1.1 }
103    
104     }