new GrantPermission(new FilePermission("*", "read")),
new GrantPermission(new RuntimePermission("getClassLoader")) };
Principal testPrin = new FakePrincipal();
Subject subj = new Subject();
subj.getPrincipals().add(testPrin);
TestDynamicPolicyProvider policy =
new TestDynamicPolicyProvider(testPerm);
logger.fine("Policy provider is " + policy);
Policy.setPolicy(policy);
Subject.doAs(subj, new FakePrivilegedAction(fromClass, toClass));
Object[] params = policy.getGetGrantsParams();
if (params == null || params.length == 0) {
// FAIL
throw new TestException(
"'getGrants' method of " + policy + " was not called.");
}
if (!params[0].equals(fromClass)) {
// FAIL
throw new TestException(
"1-st parameter to 'getGrants' was " + params[0]
+ " while " + fromClass + " was expected.");
}
if (params[1] == null || ((Object []) params[1]).length != 1
|| !((Object []) params[1])[0].equals(testPrin)) {
// FAIL
throw new TestException(
"2-nd parameter to 'getGrants' was "
+ Util.arrayToString((Object []) params[1])
+ " while " + testPrin + " was expected.");
}
// PASS
logger.fine("'getGrants' method of installed policy provider "
+ " was called with correct parameters.");
params = policy.getGrantParams();
if (params == null || params.length == 0) {
// FAIL
throw new TestException(
"'grant' method of " + policy + " was not called.");
}
if (!params[0].equals(toClass)) {
// FAIL
throw new TestException(
"1-st parameter was " + params[0]
+ " while " + toClass + " was expected.");
}
if (params[1] == null || ((Object []) params[1]).length != 1
|| !((Object []) params[1])[0].equals(testPrin)) {
// FAIL
throw new TestException(
"2-nd parameter was "
+ Util.arrayToString((Object []) params[1])
+ " while " + testPrin + " was expected.");
}
if (!Util.comparePermissions((Object []) params[2], testPerm)) {
// FAIL
throw new TestException(
"3-rd parameter was "
+ Util.arrayToString((Object []) params[2])
+ " while " + Util.arrayToString(testPerm)
+ " was expected.");
}
// PASS
logger.fine("'grant' method of installed policy provider "
+ " was called with correct parameters.");
// not granted list of permissions
Permission[] notGrantedPerm = new Permission[] {
new GrantPermission(new PropertyPermission("*", "read")),
new GrantPermission(new RuntimePermission("getClassLoader")) };
policy = new TestDynamicPolicyProvider(testPerm, notGrantedPerm);
logger.fine("Policy provider is " + policy);
Policy.setPolicy(policy);
Subject.doAs(subj, new FakePrivilegedAction(fromClass, toClass));
params = policy.getGetGrantsParams();
if (params == null || params.length == 0) {
// FAIL
throw new TestException(
"'getGrants' method of " + policy + " was not called.");
}
if (!params[0].equals(fromClass)) {
// FAIL
throw new TestException(
"1-st parameter to 'getGrants' was " + params[0]
+ " while " + fromClass + " was expected.");
}
if (params[1] == null || ((Object []) params[1]).length != 1
|| !((Object []) params[1])[0].equals(testPrin)) {
// FAIL
throw new TestException(
"2-nd parameter to 'getGrants' was "
+ Util.arrayToString((Object []) params[1])
+ " while " + testPrin + " was expected.");
}
// PASS
logger.fine("'getGrants' method of installed policy provider "
+ " was called with correct parameters.");
params = policy.getGrantParams();
if (params == null || params.length == 0) {
// FAIL
throw new TestException(
"'grant' method of " + policy + " was not called.");