ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/ACL/ACLTest.java
Revision: 1.5
Committed: Mon Dec 31 01:49:13 2001 UTC (22 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.4: +10 -1 lines
Log Message:
Some significant internal changes. It should be noted that this code is still in
the debugging stage, and thus contains lots of println's :-)
The most obvious change is that when adding a rule we try to determine if it's
and IP address given as the expression. If it is, we store this in a short[] in the
ACLRule inner class. Then when performing a check we look to see if it's an
IP address rule, and then do a special check for IP addresses - which is much
more efficient than comparing two strings. We have also halved the checks
done by only comparing a rule to either the name or IP, rather than both.

File Contents

# Content
1 import java.io.*;
2 import java.net.*;
3
4 class ACLTest {
5
6 public static void main(String args[]) {
7 // setup ACL defaulting to DENY
8 ACL acl = new ACL(ACL.DENY);
9
10 // add some test rules
11 acl.add(ACL.ALLOW, "*.ukc.ac.uk");
12 acl.add(ACL.ALLOW, "129.12.*.*");
13 acl.add(ACL.ALLOW, "*.bishnet.net");
14 acl.add(ACL.ALLOW, "192.168.*");
15 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 // not really needed if we default to deny :)
25 //acl.add(ACL.DENY, "*");
26
27 // dump our ACL to the console before serialization
28 System.out.println("BEFORE: " + acl);
29
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 System.out.println("AFTER: " + acl);
60
61 // run a few tests
62 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
67 // setup an ACLServerSocket putting our ACL in
68 ACLServerSocket ss = null;
69 try {
70 ss = new ACLServerSocket(acl, 1337);
71 }
72 catch(IOException e) {
73 System.out.println("exception: " + e);
74 e.printStackTrace();
75 System.exit(1);
76 }
77
78 // start listening
79 // valid connections (allowed by ACL) will get a message sent back
80 while(true) {
81 try {
82 Socket s = ss.accept();
83 PrintWriter writer = new PrintWriter(s.getOutputStream(), true);
84 writer.println("Connection suceeded from: " + s.getInetAddress().getHostName());
85 writer.println("Closing in 5 seconds");
86 try { Thread.sleep(5000); } catch(Exception ee) {}
87 writer.close();
88 s.close();
89 }
90 catch(IOException e) {
91 System.out.println("exception: " + e);
92 e.printStackTrace();
93 System.exit(1);
94 }
95 }
96 }
97
98 }