1 20 package org.apache.derbyTesting.junit; 21 22 import java.net.URL ; 23 import java.security.AccessController ; 24 import java.security.PrivilegedActionException ; 25 import java.util.Enumeration ; 26 import java.util.Properties ; 27 28 29 import junit.extensions.TestSetup; 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 33 38 public final class SecurityManagerSetup extends TestSetup { 39 40 private static final Properties classPathSet = new Properties (); 41 42 45 static boolean isJars; 46 47 51 private static final boolean externalSecurityManagerInstalled; 52 53 static { 54 externalSecurityManagerInstalled = determineClasspath(); 57 58 } 59 60 private final String decoratorPolicyResource; 61 private SecurityManagerSetup(Test test, String policyResource) 62 { 63 super(test); 64 this.decoratorPolicyResource = policyResource; 65 } 66 67 78 public static Test noSecurityManager(BaseTestCase test) 79 { 80 if (externalSecurityManagerInstalled) 81 return new TestSuite(); 82 return new SecurityManagerSetup(test, "<NONE>"); 83 } 84 85 91 static void noSecurityManager() throws PrivilegedActionException  92 { 93 installSecurityManager("<NONE>"); 94 } 95 96 100 protected void setUp() throws PrivilegedActionException { 101 installSecurityManager(decoratorPolicyResource); 102 } 103 104 110 static void installSecurityManager() throws PrivilegedActionException  111 { 112 installSecurityManager( 113 "org/apache/derbyTesting/functionTests/util/derby_tests.policy"); 114 115 } 116 117 private static void installSecurityManager(String policyFile) 118 throws PrivilegedActionException { 119 120 if (externalSecurityManagerInstalled) 121 return; 122 123 Properties set = new Properties (classPathSet); 124 setSecurityPolicy(set, policyFile); 125 126 SecurityManager sm = System.getSecurityManager(); 127 if (sm != null) { 128 130 if (set.getProperty("java.security.policy").equals( 131 BaseTestCase.getSystemProperty("java.security.policy"))) 132 return; 133 134 AccessController.doPrivileged(new java.security.PrivilegedAction () { 136 137 public Object run() { 138 System.setSecurityManager(null); 139 return null; 140 } 141 }); 142 } 143 144 for (Enumeration e = set.propertyNames(); e.hasMoreElements();) { 146 String key = (String ) e.nextElement(); 147 BaseTestCase.setSystemProperty(key, set.getProperty(key)); 148 } 149 150 if ("<NONE>".equals(set.getProperty("java.security.policy"))) 152 return; 153 154 AccessController.doPrivileged(new java.security.PrivilegedAction () { 156 157 public Object run() { 158 System.setSecurityManager(new SecurityManager ()); 159 return null; 160 } 161 }); 162 163 } 164 165 private static void setSecurityPolicy(Properties set, 166 String policyResource) throws PrivilegedActionException  167 { 168 if ("<NONE>".equals(policyResource)) { 169 set.setProperty("java.security.policy", policyResource); 170 return; 171 } 172 URL policyURL = BaseTestCase.getTestResource(policyResource); 173 174 if (policyURL != null) 175 set.setProperty("java.security.policy", 176 policyURL.toExternalForm()); 177 } 178 179 180 208 private static boolean determineClasspath() 209 { 210 if (System.getSecurityManager() != null) { 213 return true; 214 } 215 216 URL 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 derby = null; 229 try { 230 derby = getURL(org.apache.derby.jdbc.EmbeddedSimpleDataSource.class); 231 } catch (java.lang.NoClassDefFoundError e) { 232 derby = testing; 233 } 234 classPathSet.setProperty("derbyTesting.codejar", stripJar(derby)); 235 236 URL client = null; 237 try { 238 client = getURL(org.apache.derby.jdbc.ClientDataSource.class); 239 } catch (java.lang.NoClassDefFoundError e) { 240 client = derby; 241 } 242 243 classPathSet.setProperty("derbyTesting.clientjar", stripJar(client)); 244 245 return false; 246 } 247 248 256 public static Properties getPolicyFilePropertiesForOldHarness() 257 { 258 return classPathSet; 259 } 260 261 267 private static String stripJar(URL url) 268 { 269 String ef = url.toExternalForm(); 270 return ef.substring(0, ef.lastIndexOf('/') + 1); 271 } 272 273 276 private static URL getURL(final Class cl) 277 { 278 return (URL ) 279 AccessController.doPrivileged(new java.security.PrivilegedAction () { 280 281 public Object run() { 282 return cl.getProtectionDomain().getCodeSource().getLocation(); 283 } 284 }); 285 } 286 } 287
| Popular Tags
|