You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/07/28 00:56:43 UTC

svn commit: r426308 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util: BaseTestCase.java SecurityManagerSetup.java

Author: djd
Date: Thu Jul 27 15:56:43 2006
New Revision: 426308

URL: http://svn.apache.org/viewvc?rev=426308&view=rev
Log:
Add framework code to BaseTestCase to automatically install the security manager
with the default test policy file for every JUnit test. Security manager
code separated into SecurityManagerSetup for clarity. Allows Derby JUnit
tests to be run via JUnit test runners without specifiying any system
properties.
Not enabled at the moment, needs testing, can be enabled by commenting
out line in runBase in BaseTestCase.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SecurityManagerSetup.java   (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java?rev=426308&r1=426307&r2=426308&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java Thu Jul 27 15:56:43 2006
@@ -21,6 +21,7 @@
 
 import junit.framework.TestCase;
 import java.io.PrintStream;
+import java.net.URL;
 import java.sql.SQLException;
 import java.security.AccessController;
 
@@ -60,6 +61,24 @@
     }
     
     /**
+     * Run the test and force installation of a security
+     * manager with the default test policy file.
+     * Individual tests can run without a security
+     * manager or with a different policy file using
+     * the decorators obtained from SecurityManagerSetup.
+     * <BR>
+     * Method is final to ensure security manager is
+     * enabled by default. Tests should not need to
+     * ovveride runTest, instead use test methods
+     * setUp, tearDown methods and decorators.
+     */
+    public final void runBare() throws Throwable {
+    	// Not ready for prime time!
+    	// SecurityManagerSetup.installSecurityManager();
+    	super.runBare();
+    }
+    
+    /**
      * Print alarm string
      * @param text String to print
      */
@@ -119,7 +138,27 @@
 	     );
 	
     }
-    
+    /**
+     * Remove system property
+     *
+     * @param name name of the property
+     */
+    protected static void removeSystemProperty(final String name)
+	throws PrivilegedActionException {
+	
+	AccessController.doPrivileged
+	    (new java.security.PrivilegedAction(){
+		    
+		    public Object run(){
+			System.getProperties().remove(name);
+			return null;
+			
+		    }
+		    
+		}
+	     );
+	
+    }    
     /**
      * Get system property.
      *
@@ -138,6 +177,39 @@
 
 		}
 	     );
+    }
+    
+    /**
+     * Obtain the URL for a test resource, e.g. a policy
+     * file or a SQL script.
+     * @param name Resource name, typically - org.apache.derbyTesing.something
+     * @return URL to the resource, null if it does not exist.
+     * @throws PrivilegedActionException
+     */
+    protected static URL getTestResource(final String name)
+	throws PrivilegedActionException {
+
+	return (URL)AccessController.doPrivileged
+	    (new java.security.PrivilegedAction(){
+
+		    public Object run(){
+			return BaseTestCase.class.getClassLoader().
+			    getResource(name);
+
+		    }
+
+		}
+	     );
+    }  
+    
+    /**
+     * Assert a security manager is installed.
+     *
+     */
+    public static void assertSecurityManager()
+    {
+    	assertNotNull("No SecurityManager installed",
+    			System.getSecurityManager());
     }
     
 } // End class BaseTestCase

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SecurityManagerSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SecurityManagerSetup.java?rev=426308&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SecurityManagerSetup.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SecurityManagerSetup.java Thu Jul 27 15:56:43 2006
@@ -0,0 +1,250 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.util.SecurityManagerSetup
+ *
+ * Copyright 2006 The Apache Software Foundation or its 
+ * licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.functionTests.util;
+
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Setup for running Derby JUnit tests with the SecurityManager
+ * which is the default for tests.
+ *
+ */
+public final class SecurityManagerSetup extends TestSetup {
+	
+	
+	private static final Properties classPathSet = new Properties();
+	
+	/**
+	 * True if a security manager was installed outside of the
+	 * control of this class and BaseTestCase.
+	 */
+	private static final boolean externalSecurityManagerInstalled;
+	
+	static {
+		// Determine what the set of properties
+		// describing the environment is.
+		externalSecurityManagerInstalled = determineClasspath();
+	}
+	
+	private final String decoratorPolicyResource;
+	private SecurityManagerSetup(Test test, String policyResource)
+	{
+		super(test);
+		this.decoratorPolicyResource = policyResource;
+	}
+	
+	/**
+	 * Get a decorator that will ensure no security manger
+	 * is installed to run a test. Not supported for suites.
+	 * <BR>
+	 * An empty suite is returned if a security manager was installed
+	 * externally, i.e. not under the control of the BaseTestCase
+	 * and this code. In this case the code can not support the
+	 * mode of no security manager as it may not have enough information
+	 * to re-install the security manager. So the passed in test
+	 * will be skipped.
+	 */
+	public static Test noSecurityManager(BaseTestCase test)
+	{
+		if (externalSecurityManagerInstalled)
+			return new TestSuite();
+		return new SecurityManagerSetup(test, "<NONE>");
+	}
+	
+	/**
+	 * Install specific polciy file with the security manager
+	 * including the special case of no security manager.
+	 */
+	protected void setUp() throws PrivilegedActionException {
+		installSecurityManager(decoratorPolicyResource);
+	}
+	
+	/**
+	 * Install a SecurityManager with the default test policy
+	 * file:
+	 * org/apache/derbyTesting/functionTests/util/derby_tests.policy
+	 * 
+	 */
+	static void installSecurityManager() throws PrivilegedActionException
+	{
+		installSecurityManager(
+				"org/apache/derbyTesting/functionTests/util/derby_tests.policy");
+				
+	}
+	
+	private static void installSecurityManager(String policyFile)
+			throws PrivilegedActionException {
+		
+		if (externalSecurityManagerInstalled)
+			return;
+		
+		Properties set = new Properties(classPathSet);
+		setSecurityPolicy(set, policyFile);
+
+		SecurityManager sm = System.getSecurityManager();
+		if (sm != null) {
+			// SecurityManager installed, see if it has the same settings.
+
+			if (set.getProperty("java.security.policy").equals(
+					BaseTestCase.getSystemProperty("java.security.policy")))
+					return;
+			
+			// Uninstall the current manager.
+			AccessController.doPrivileged(new java.security.PrivilegedAction() {
+
+				public Object run() {
+					System.setSecurityManager(null);
+					return null;
+				}
+			});
+		}
+		
+		// Set the system properties from the desired set.
+		for (Enumeration e = set.propertyNames(); e.hasMoreElements();) {
+			String key = (String) e.nextElement();
+			BaseTestCase.setSystemProperty(key, set.getProperty(key));
+		}
+		
+		// Check indicator for no security manager
+		if ("<NONE>".equals(set.getProperty("java.security.policy")))
+			return;
+		
+		// and install
+		AccessController.doPrivileged(new java.security.PrivilegedAction() {
+
+			public Object run() {
+				System.setSecurityManager(new SecurityManager());
+				return null;
+			}
+		});
+
+	}
+	
+	private static void setSecurityPolicy(Properties set,
+			String policyResource) throws PrivilegedActionException
+	{
+		URL policyURL = BaseTestCase.getTestResource(policyResource);
+
+		if (policyURL != null)
+			set.setProperty("java.security.policy",
+					policyURL.toExternalForm());
+	}
+
+	
+	/**
+	 * Determine the settings of the classpath in order to configure
+	 * the variables used in the testing policy files.
+	 * Looks for three items:
+	 * 
+	 * Location of derbyTesting.jar via this class
+	 * Location of derby.jar via org.apache.derby.jdbc.EmbeddedDataSource
+	 * Location of derbyclient.jar via org.apache.derby.jdbc.ClientDataSource
+	 * 
+	 * Two options are supported, either all are in jar files or
+	 * all are on the classpath. Properties are set as follows:
+	 * 
+	 * <P>
+	 * Classpath:
+	 * <BR>
+	 * derbyTesting.codeclasses set to location of classes folder
+	 * <P>
+	 * Jar files:
+	 * <BR>
+	 * derbyTesting.codejar - location of derby.jar,
+	 * derbynet.jar and derbytools.jar, all assumed to be in the
+	 * same location.
+	 * <BR>
+	 * derbyTesting.clientjar - location of derbyclient.jar (FUTURE)
+	 * <BR>
+	 * derbyTesting.testjar - location of derbyTesting.jar (FUTURE)
+	 * 
+	 */
+	private static boolean determineClasspath()
+	{
+		// Security manager already installed, assume that
+		// it is set up correctly.
+		if (System.getSecurityManager() != null) {		
+			return true;
+		}
+		
+		URL testing = getURL(SecurityManagerSetup.class);
+		
+		boolean isClasspath = testing.toExternalForm().endsWith("/");
+		if (isClasspath) {
+			classPathSet.setProperty("derbyTesting.codeclasses",
+					testing.toExternalForm());
+			return false;
+		}
+		classPathSet.setProperty("derbyTesting.testjar", stripJar(testing));
+		
+		URL derby = null;
+		try {
+			derby = getURL(org.apache.derby.jdbc.EmbeddedDataSource.class);
+		} catch (java.lang.NoClassDefFoundError e) {
+			derby = testing;
+		}		
+		classPathSet.setProperty("derbyTesting.codejar", stripJar(derby));
+
+		URL client = null;
+		try {
+			client = getURL(org.apache.derby.jdbc.ClientDataSource.class);
+		} catch (java.lang.NoClassDefFoundError e) {
+			client = derby;
+		}
+		
+		classPathSet.setProperty("derbyTesting.clientjar", stripJar(client));
+		
+		return false;
+	}
+	
+	/**
+	 * Strip of the last token which will be the jar name.
+	 * The returned string includes the trailing slash.
+	 * @param url
+	 * @return
+	 */
+	private static String stripJar(URL url)
+	{
+		String ef = url.toExternalForm();
+		return ef.substring(0, ef.lastIndexOf('/') + 1);
+	}
+	
+	/**
+	 * Get the URL of the code base from a class.
+	 */
+	private static URL getURL(final Class cl)
+	{
+		return (URL)
+		   AccessController.doPrivileged(new java.security.PrivilegedAction() {
+
+			public Object run() {
+				return cl.getProtectionDomain().getCodeSource().getLocation();
+			}
+		});
+	}
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SecurityManagerSetup.java
------------------------------------------------------------------------------
    svn:eol-style = native