KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > ApplicationSecurityEnforcerTest


1 /*
2
3    Copyright 2002 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util;
19
20 import org.apache.batik.test.*;
21
22 /**
23  * Validates the operation of the security enforcer class.
24  *
25  * @author <a mailto="vincent.hardy@sun.com">Vincent Hardy</a>
26  * @version $Id: ApplicationSecurityEnforcerTest.java,v 1.5 2004/08/18 07:17:15 vhardy Exp $
27  */

28 public class ApplicationSecurityEnforcerTest extends DefaultTestSuite {
29     final static Class JavaDoc APP_MAIN_CLASS = org.apache.batik.apps.svgbrowser.Main.class;
30     final static String JavaDoc APP_SECURITY_POLICY = "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy";
31
32     /**
33      * In the constructor, append atomic tests
34      */

35     public ApplicationSecurityEnforcerTest(){
36         addTest(new CheckNoSecurityManagerOverride());
37         addTest(new CheckSecurityEnforcement());
38         addTest(new CheckSecurityRemoval());
39         addTest(new CheckNoPolicyFile());
40     }
41
42     static ApplicationSecurityEnforcer buildTestTarget(){
43         return new ApplicationSecurityEnforcer(APP_MAIN_CLASS,
44                                                APP_SECURITY_POLICY);
45     }
46
47     static class CheckNoSecurityManagerOverride extends AbstractTest {
48         public boolean runImplBasic(){
49             ApplicationSecurityEnforcer aseA
50                 = buildTestTarget();
51
52             aseA.enforceSecurity(true);
53
54             ApplicationSecurityEnforcer aseB
55                 = buildTestTarget();
56
57             boolean passed = false;
58             try {
59                 // This should throw a SecurityException
60
aseB.enforceSecurity(true);
61             } catch (SecurityException JavaDoc se){
62                 System.out.println(">>>>>>>>>>>>> got expected SecurityException A");
63                 try {
64                     System.out.println(">>>>>>>>>>>>> got expected SecurityException B");
65                     aseB.enforceSecurity(false);
66                 } catch (SecurityException JavaDoc se2){
67                     passed = true;
68                 }
69             }
70
71             aseA.enforceSecurity(false);
72             
73             return passed;
74         }
75     }
76
77     static class CheckSecurityEnforcement extends AbstractTest {
78         public boolean runImplBasic() {
79             ApplicationSecurityEnforcer ase = buildTestTarget();
80
81             try {
82                 ase.enforceSecurity(true);
83                 SecurityManager JavaDoc sm = System.getSecurityManager();
84                 if (sm == ase.lastSecurityManagerInstalled){
85                     return true;
86                 }
87             } finally {
88                 System.setSecurityManager(null);
89             }
90
91             return false;
92         }
93     }
94
95     static class CheckSecurityRemoval extends AbstractTest {
96         public boolean runImplBasic() {
97             ApplicationSecurityEnforcer ase = buildTestTarget();
98
99             try {
100                 ase.enforceSecurity(true);
101                 ase.enforceSecurity(false);
102                 SecurityManager JavaDoc sm = System.getSecurityManager();
103                 if (sm == null && ase.lastSecurityManagerInstalled == null) {
104                     return true;
105                 }
106             } finally {
107                 System.setSecurityManager(null);
108             }
109
110             return false;
111         }
112     }
113
114     static class CheckNoPolicyFile extends AbstractTest {
115         public boolean runImplBasic() {
116             ApplicationSecurityEnforcer ase =
117                 new ApplicationSecurityEnforcer(APP_MAIN_CLASS,
118                                                 "dont.exist.policy");
119
120             try {
121                 ase.enforceSecurity(true);
122             } catch (NullPointerException JavaDoc se) {
123                 return true;
124             } finally {
125                 ase.enforceSecurity(false);
126             }
127             return false;
128         }
129     }
130
131 }
132
Popular Tags