ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/ACL/ACLTest.java
Revision: 1.4
Committed: Sun Dec 23 01:05:35 2001 UTC (22 years, 10 months ago) by tdb
Branch: MAIN
Changes since 1.3: +2 -2 lines
Log Message:
The getACLString() method has been changed to a toString() method, and the
i-scream style toString() has been dropped. The functionality of toString() now
more closely resembles the Java Map's in layout.

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     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.1 // not really needed if we default to deny :)
16 tdb 1.2 //acl.add(ACL.DENY, "*");
17 tdb 1.1
18 tdb 1.3 // dump our ACL to the console before serialization
19 tdb 1.4 System.out.println("BEFORE: " + acl);
20 tdb 1.3
21     // name for our serialized ACL
22     String aclFilename = "aclfile";
23    
24     // write out our ACL to a file
25     try {
26     ObjectOutputStream ostream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(aclFilename)));
27     ostream.writeObject(acl);
28     ostream.flush();
29     ostream.close();
30     }
31     catch (Exception e) {
32     System.out.println("exception: " + e);
33     e.printStackTrace();
34     System.exit(1);
35     }
36    
37     // and read it back in :)
38     try {
39     ObjectInputStream istream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(aclFilename)));
40     acl = (ACL) istream.readObject();
41     istream.close();
42     }
43     catch (Exception e) {
44     System.out.println("exception: " + e);
45     e.printStackTrace();
46     System.exit(1);
47     }
48    
49     // dump our ACL to the console after serialization
50 tdb 1.4 System.out.println("AFTER: " + acl);
51 tdb 1.1
52 tdb 1.2 // run a few tests
53 tdb 1.1 System.out.println("killigrew.ukc.ac.uk: " + acl.check("killigrew.ukc.ac.uk"));
54     System.out.println("129.12.41.13: " + acl.check("129.12.41.13"));
55     System.out.println("kruskal.18hp.net: " + acl.check("kruskal.18hp.net"));
56     System.out.println("192.168.1.1: " + acl.check("192.168.1.1"));
57 tdb 1.2
58     // setup an ACLServerSocket putting our ACL in
59     ACLServerSocket ss = null;
60     try {
61     ss = new ACLServerSocket(acl, 1337);
62     }
63     catch(IOException e) {
64     System.out.println("exception: " + e);
65     e.printStackTrace();
66     System.exit(1);
67     }
68    
69     // start listening
70     // valid connections (allowed by ACL) will get a message sent back
71     while(true) {
72     try {
73     Socket s = ss.accept();
74     PrintWriter writer = new PrintWriter(s.getOutputStream(), true);
75     writer.println("Connection suceeded from: " + s.getInetAddress().getHostName());
76     writer.println("Closing in 5 seconds");
77     try { Thread.sleep(5000); } catch(Exception ee) {}
78     writer.close();
79     s.close();
80     }
81     catch(IOException e) {
82     System.out.println("exception: " + e);
83     e.printStackTrace();
84     System.exit(1);
85     }
86     }
87 tdb 1.1 }
88    
89     }