KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > junit > SecurityManagerSetup


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

20 package org.apache.derbyTesting.junit;
21
22 import java.net.URL JavaDoc;
23 import java.security.AccessController JavaDoc;
24 import java.security.PrivilegedActionException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Properties JavaDoc;
27
28
29 import junit.extensions.TestSetup;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 /**
34  * Setup for running Derby JUnit tests with the SecurityManager
35  * which is the default for tests.
36  *
37  */

38 public final class SecurityManagerSetup extends TestSetup {
39     
40     private static final Properties JavaDoc classPathSet = new Properties JavaDoc();
41     
42     /**
43      * True if the classes are loaded from jars.
44      */

45     static boolean isJars;
46     
47     /**
48      * True if a security manager was installed outside of the
49      * control of this class and BaseTestCase.
50      */

51     private static final boolean externalSecurityManagerInstalled;
52     
53     static {
54         // Determine what the set of properties
55
// describing the environment is.
56
externalSecurityManagerInstalled = determineClasspath();
57         
58     }
59     
60     private final String JavaDoc decoratorPolicyResource;
61     private SecurityManagerSetup(Test test, String JavaDoc policyResource)
62     {
63         super(test);
64         this.decoratorPolicyResource = policyResource;
65     }
66     
67     /**
68      * Get a decorator that will ensure no security manger
69      * is installed to run a test. Not supported for suites.
70      * <BR>
71      * An empty suite is returned if a security manager was installed
72      * externally, i.e. not under the control of the BaseTestCase
73      * and this code. In this case the code can not support the
74      * mode of no security manager as it may not have enough information
75      * to re-install the security manager. So the passed in test
76      * will be skipped.
77      */

78     public static Test noSecurityManager(BaseTestCase test)
79     {
80         if (externalSecurityManagerInstalled)
81             return new TestSuite();
82         return new SecurityManagerSetup(test, "<NONE>");
83     }
84     
85     /**
86      * Install a SecurityManager with the default test policy
87      * file:
88      * org/apache/derbyTesting/functionTests/util/derby_tests.policy
89      *
90      */

91     static void noSecurityManager() throws PrivilegedActionException JavaDoc
92     {
93         installSecurityManager("<NONE>");
94     }
95     
96     /**
97      * Install specific polciy file with the security manager
98      * including the special case of no security manager.
99      */

100     protected void setUp() throws PrivilegedActionException JavaDoc {
101         installSecurityManager(decoratorPolicyResource);
102     }
103     
104     /**
105      * Install a SecurityManager with the default test policy
106      * file:
107      * org/apache/derbyTesting/functionTests/util/derby_tests.policy
108      *
109      */

110     static void installSecurityManager() throws PrivilegedActionException JavaDoc
111     {
112         installSecurityManager(
113                 "org/apache/derbyTesting/functionTests/util/derby_tests.policy");
114                 
115     }
116     
117     private static void installSecurityManager(String JavaDoc policyFile)
118             throws PrivilegedActionException JavaDoc {
119         
120         if (externalSecurityManagerInstalled)
121             return;
122         
123         Properties JavaDoc set = new Properties JavaDoc(classPathSet);
124         setSecurityPolicy(set, policyFile);
125
126         SecurityManager JavaDoc sm = System.getSecurityManager();
127         if (sm != null) {
128             // SecurityManager installed, see if it has the same settings.
129

130             if (set.getProperty("java.security.policy").equals(
131                     BaseTestCase.getSystemProperty("java.security.policy")))
132                     return;
133             
134             // Uninstall the current manager.
135
AccessController.doPrivileged(new java.security.PrivilegedAction JavaDoc() {
136
137                 public Object JavaDoc run() {
138                     System.setSecurityManager(null);
139                     return null;
140                 }
141             });
142         }
143         
144         // Set the system properties from the desired set.
145
for (Enumeration JavaDoc e = set.propertyNames(); e.hasMoreElements();) {
146             String JavaDoc key = (String JavaDoc) e.nextElement();
147             BaseTestCase.setSystemProperty(key, set.getProperty(key));
148         }
149         
150         // Check indicator for no security manager
151
if ("<NONE>".equals(set.getProperty("java.security.policy")))
152             return;
153         
154         // and install
155
AccessController.doPrivileged(new java.security.PrivilegedAction JavaDoc() {
156
157             public Object JavaDoc run() {
158                 System.setSecurityManager(new SecurityManager JavaDoc());
159                 return null;
160             }
161         });
162
163     }
164     
165     private static void setSecurityPolicy(Properties JavaDoc set,
166             String JavaDoc policyResource) throws PrivilegedActionException JavaDoc
167     {
168         if ("<NONE>".equals(policyResource)) {
169             set.setProperty("java.security.policy", policyResource);
170             return;
171         }
172         URL JavaDoc policyURL = BaseTestCase.getTestResource(policyResource);
173
174         if (policyURL != null)
175             set.setProperty("java.security.policy",
176                     policyURL.toExternalForm());
177     }
178
179     
180     /**
181      * Determine the settings of the classpath in order to configure
182      * the variables used in the testing policy files.
183      * Looks for three items:
184      *
185      * Location of derbyTesting.jar via this class
186      * Location of derby.jar via org.apache.derby.jdbc.EmbeddedSimpleDataSource
187      * Location of derbyclient.jar via org.apache.derby.jdbc.ClientDataSource
188      *
189      * Two options are supported, either all are in jar files or
190      * all are on the classpath. Properties are set as follows:
191      *
192      * <P>
193      * Classpath:
194      * <BR>
195      * derbyTesting.codeclasses set to location of classes folder
196      * <P>
197      * Jar files:
198      * <BR>
199      * derbyTesting.codejar - location of derby.jar,
200      * derbynet.jar and derbytools.jar, all assumed to be in the
201      * same location.
202      * <BR>
203      * derbyTesting.clientjar - location of derbyclient.jar (FUTURE)
204      * <BR>
205      * derbyTesting.testjar - location of derbyTesting.jar (FUTURE)
206      *
207      */

208     private static boolean determineClasspath()
209     {
210         // Security manager already installed, assume that
211
// it is set up correctly.
212
if (System.getSecurityManager() != null) {
213             return true;
214         }
215         
216         URL JavaDoc testing = getURL(SecurityManagerSetup.class);
217         
218         boolean isClasspath = testing.toExternalForm().endsWith("/");
219         if (isClasspath) {
220             classPathSet.setProperty("derbyTesting.codeclasses",
221                     testing.toExternalForm());
222             isJars = false;
223             return false;
224         }
225         classPathSet.setProperty("derbyTesting.testjar", stripJar(testing));
226         isJars = true;
227         
228         URL JavaDoc derby = null;
229         try {
230             derby = getURL(org.apache.derby.jdbc.EmbeddedSimpleDataSource.class);
231         } catch (java.lang.NoClassDefFoundError JavaDoc e) {
232             derby = testing;
233         }
234         classPathSet.setProperty("derbyTesting.codejar", stripJar(derby));
235
236         URL JavaDoc client = null;
237         try {
238             client = getURL(org.apache.derby.jdbc.ClientDataSource.class);
239         } catch (java.lang.NoClassDefFoundError JavaDoc e) {
240             client = derby;
241         }
242         
243         classPathSet.setProperty("derbyTesting.clientjar", stripJar(client));
244         
245         return false;
246     }
247     
248     /**
249      * Return the policy file system properties for use
250      * by the old test harness. This ensures a consistent
251      * approach to setting the properties. There are the
252      * properties used to define the jar file location in
253      * any policy files.
254      * @return
255      */

256     public static Properties JavaDoc getPolicyFilePropertiesForOldHarness()
257     {
258         return classPathSet;
259     }
260     
261     /**
262      * Strip of the last token which will be the jar name.
263      * The returned string includes the trailing slash.
264      * @param url
265      * @return
266      */

267     private static String JavaDoc stripJar(URL JavaDoc url)
268     {
269         String JavaDoc ef = url.toExternalForm();
270         return ef.substring(0, ef.lastIndexOf('/') + 1);
271     }
272     
273     /**
274      * Get the URL of the code base from a class.
275      */

276     private static URL JavaDoc getURL(final Class JavaDoc cl)
277     {
278         return (URL JavaDoc)
279            AccessController.doPrivileged(new java.security.PrivilegedAction JavaDoc() {
280
281             public Object JavaDoc run() {
282                 return cl.getProtectionDomain().getCodeSource().getLocation();
283             }
284         });
285     }
286 }
287
Popular Tags