System.setSecurityManager(new SecurityManager());
Policy.setPolicy(new BasePolicyProvider());
logger.fine("Security manager and policy provider are not "
+ "instances of SecurityContextSource.");
logger.fine("Calling 'Security.getContext()' method.");
SecurityContext sc = Security.getContext();
if (sc.wrap(testPA) != testPA) {
// FAIL
throw new TestException(
"Returned SecurityContext's 'wrap(PrivilegedAction)' "
+ "method returned " + sc.wrap(testPA) + " while "
+ testPA + " was expected.");
}
if (sc.wrap(testPEA) != testPEA) {
// FAIL
throw new TestException(
"Returned SecurityContext's "
+ "'wrap(PrivilegedExceptionAction)' method returned "
+ sc.wrap(testPEA) + " while " + testPEA
+ " was expected.");
}
// PASS
logger.fine("Method returned correct SecurityContext instance.");
logger.fine("Set policy provider to SCSPolicyProvider.");
SCSPolicyProvider policy = new SCSPolicyProvider();
Policy.setPolicy(policy);
sc = Security.getContext();
if (policy.getCallsNum() != 1) {
// FAIL
throw new TestException(
"'getContext' method of installed policy provider was "
+ "called " + policy.getCallsNum() + " times while 1 "
+ "call was expected.");
}
if (!sc.equals(policy.getContext())) {
// FAIL
throw new TestException(
"'Security.getContext()' method returned " + sc
+ " SecurityPolicy while " + policy.getContext()
+ " was expected.");
}
// PASS
logger.fine("Method works as expected.");
logger.fine("Set security manager to SCSSecurityManager.");
SCSSecurityManager sm = new SCSSecurityManager();
System.setSecurityManager(sm);
policy.resetCallsNum();
sc = Security.getContext();
if (sm.getCallsNum() != 1) {
// FAIL
throw new TestException(
"'getContext' method of installed security manager was "
+ "called " + sm.getCallsNum() + " times while 1 "
+ "call was expected.");
}
if (policy.getCallsNum() != 0) {
// FAIL
throw new TestException(
"'getContext' method of installed policy provider was "
+ "called " + policy.getCallsNum()
+ " times while no calls were expected.");
}
if (!sc.equals(sm.getContext())) {
// FAIL
throw new TestException(
"'Security.getContext()' method returned " + sc
+ " SecurityPolicy while " + sm.getContext()
+ " was expected.");