public final class SecurityManagerSetup
extends junit.extensions.TestSetup
This setup class normally installs the default policy file. This can be overridden by specifying java.security.policy=<NONE> (see NO_POLICY), and can also be overridden by installing a security manager explicitly before the default security manager is installed.
Individual tests/suites can be configured to be run without a security manager, with a specific policy file, or with a specific policy file merged with the default policy file. The last option is useful when you only need to extend the default policy with a few extra permissions to run a test.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
additionalPolicyResource
An additional policy to install (may be
null ). |
private static java.util.Properties |
classPathSet |
private java.lang.String |
decoratorPolicyResource |
private java.lang.SecurityManager |
decoratorSecurityManager |
private static boolean |
externalSecurityManagerInstalled
True if a security manager was installed outside of the
control of this class and BaseTestCase.
|
(package private) static boolean |
isJars
True if the classes are loaded from jars.
|
(package private) static boolean |
jacocoEnabled |
static boolean |
JVM_HAS_SUBJECT_AUTHORIZATION
Does the JVM support Subjects for
authorization through Java security manager.
|
(package private) static java.lang.String |
NO_POLICY
Constant used to indicate that no security policy is to be installed.
|
Constructor and Description |
---|
SecurityManagerSetup(junit.framework.Test test,
java.lang.String policyResource) |
SecurityManagerSetup(junit.framework.Test test,
java.lang.String policyResource,
boolean mergePolicies)
Installs a new security policy.
|
SecurityManagerSetup(junit.framework.Test test,
java.lang.String policyResource,
java.lang.SecurityManager securityManager)
Use custom policy and SecurityManager
|
Modifier and Type | Method and Description |
---|---|
private static boolean |
checkIfJacocoIsRunning() |
private static boolean |
determineClasspath()
Determine the settings of the classpath in order to configure
the variables used in the testing policy files.
|
private static java.lang.String |
getDefaultPolicy()
Return the name of the default policy.
|
private static java.lang.String |
getEffectivePolicyResource(java.lang.String policy1,
java.lang.String policy2)
Returns the location of the effective policy resource.
|
static java.util.Properties |
getPolicyFilePropertiesForOldHarness()
Return the policy file system properties for use
by the old test harness.
|
private static java.net.URL |
getResourceURL(java.lang.String policy)
Returns a URL for the given policy resource.
|
(package private) static java.net.URL |
getURL(java.lang.Class cl)
Get the URL of the code base from a class.
|
static java.net.URL |
getURL(java.lang.String className)
Get the URL of the code base from a class name.
|
(package private) static void |
installSecurityManager()
Install a SecurityManager with the default test policy
file:
org/apache/derbyTesting/functionTests/util/derby_tests.policy
|
private static void |
installSecurityManager(java.lang.String policyFile) |
private static void |
installSecurityManager(java.lang.String policyFile,
java.lang.SecurityManager sm) |
private static java.lang.String |
mergePolicies(java.net.URL policy1,
java.net.URL policy2)
Merges the two specified policy resources (typically files), and writes
the combined policy to a new file.
|
private static void |
mkdir(java.io.File dir)
Creates the specified directory if it doesn't exist.
|
(package private) static void |
noSecurityManager()
"Install" no security manager.
|
static junit.framework.Test |
noSecurityManager(junit.framework.Test test)
Get a decorator that will ensure no security manger
is installed to run a test.
|
private static java.io.InputStream |
openStream(java.net.URL resource)
Opens the resource stream in a privileged block.
|
private static void |
println(java.lang.String msg)
Prints a debug message if debugging is enabled.
|
private static void |
setSecurityPolicy(java.util.Properties set,
java.lang.String policyResource) |
protected void |
setUp()
Install specific policy file with the security manager
including the special case of no security manager.
|
private static java.lang.String |
stripJar(java.net.URL url)
Strip off the last token which will be the jar name.
|
protected void |
tearDown() |
private static void |
uninstallSecurityManager()
Remove the security manager.
|
basicRun, countTestCases, getTest, toString
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
static final java.lang.String NO_POLICY
public static final boolean JVM_HAS_SUBJECT_AUTHORIZATION
private static final java.util.Properties classPathSet
static boolean isJars
private static final boolean externalSecurityManagerInstalled
static final boolean jacocoEnabled
private final java.lang.String decoratorPolicyResource
private final java.lang.String additionalPolicyResource
null
).private java.lang.SecurityManager decoratorSecurityManager
public SecurityManagerSetup(junit.framework.Test test, java.lang.String policyResource)
public SecurityManagerSetup(junit.framework.Test test, java.lang.String policyResource, boolean mergePolicies)
test
- the test to wrappolicyResource
- the policy to installmergePolicies
- if false
the specified policy will be the
only policy installed, if true
the specified policy will be
merged with the default test policy for the test frameworkpublic SecurityManagerSetup(junit.framework.Test test, java.lang.String policyResource, java.lang.SecurityManager securityManager)
test
- - Test to wrappolicyResource
- - policy resource. If null use default testing policysecurityManager
- - Custom SecurityManager if null use the system security managerprivate static boolean checkIfJacocoIsRunning()
public static junit.framework.Test noSecurityManager(junit.framework.Test test)
test
- Test to run without a security manager. Note that
this must be an instance of BaseTestCase as this call depends
on setup code in that class. Arbitrary Test instances cannot be passed in.static void noSecurityManager()
protected void setUp() throws java.io.IOException
setUp
in class junit.extensions.TestSetup
java.io.IOException
protected void tearDown() throws java.lang.Exception
tearDown
in class junit.extensions.TestSetup
java.lang.Exception
private static java.lang.String getDefaultPolicy()
static void installSecurityManager()
private static void installSecurityManager(java.lang.String policyFile)
private static void installSecurityManager(java.lang.String policyFile, java.lang.SecurityManager sm)
private static void setSecurityPolicy(java.util.Properties set, java.lang.String policyResource)
private static boolean determineClasspath()
Classpath:
derbyTesting.codeclasses set to URL of classes folder
derbyTesting.ppcodeclasses set to URL of the 'classes.pptesting' folder
if it exists on the classpath. The existence of the package private tests
is determined via org.apache.derby.PackagePrivateTestSuite
Jar files:
derbyTesting.codejar - URL of derby.jar,
derbynet.jar and derbytools.jar, all assumed to be in the
same location.
derbyTesting.clientjar - URL of derbyclient.jar
derbyTesting.testjar - URL of derbyTesting.jar
derbyTesting.testjarpath - File system path to derbyTesting.jar
if the jar has a URL with a file protocol.
public static java.util.Properties getPolicyFilePropertiesForOldHarness()
private static java.lang.String stripJar(java.net.URL url)
url
- public static java.net.URL getURL(java.lang.String className)
static java.net.URL getURL(java.lang.Class cl)
private static void uninstallSecurityManager()
private static java.lang.String getEffectivePolicyResource(java.lang.String policy1, java.lang.String policy2) throws java.io.IOException
If two valid policy resources from different locations are specified, they will be merged into one policy file.
policy1
- first policypolicy2
- second policy (may be null
)java.io.IOException
- if reading or writing a policy resource failsprivate static java.net.URL getResourceURL(java.lang.String policy) throws java.net.MalformedURLException
policy
- the policy resourceURL
denoting the policy resource.java.net.MalformedURLException
- if the resource string not a valid URLprivate static java.lang.String mergePolicies(java.net.URL policy1, java.net.URL policy2) throws java.io.IOException
policy1
- the first policypolicy2
- the second policyjava.io.IOException
- if reading or writing to one of the resources failsprivate static java.io.InputStream openStream(java.net.URL resource) throws java.io.IOException
java.io.IOException
private static void mkdir(java.io.File dir)
private static void println(java.lang.String msg)
Apache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.