public static void main(String[] args) throws Exception {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
final Policy policy = new DynamicPolicyProvider();
Policy.setPolicy(policy);
ClassLoader ldr1 = new URLClassLoader(new URL[]{
TestLibrary.installClassInCodebase("Foo", "cb1", false)});
ClassLoader ldr2 = new URLClassLoader(new URL[]{
TestLibrary.installClassInCodebase("Foo", "cb2")});
cl1 = Class.forName("Foo", true, ldr1);
cl2 = Class.forName("Foo", true, ldr2);
ClassLoader ldr3 = new URLClassLoader(new URL[]{
TestLibrary.installClassInCodebase("Setup", "cb3")});
TestLibrary.installClassInCodebase("Setup$Action", "cb3");
((Runnable) Class.forName("Setup", true, ldr3).newInstance()).run();
ProtectionDomain pd1 = cl1.getProtectionDomain();
if (!(policy.implies(pd1, pA) &&
policy.implies(pd1, pB) &&
policy.implies(pd1, pC)))
{
throw new Error();
}
ProtectionDomain pd2 = cl2.getProtectionDomain();
if (policy.implies(pd2, pA) ||
policy.implies(pd2, pB) ||
policy.implies(pd2, pC))
{
throw new Error();
}
final Principal prX = new StringPrincipal("X"),
prY = new StringPrincipal("Y"),
prZ = new StringPrincipal("Z");
Subject subj = new Subject(true,
new HashSet(Arrays.asList(
new Principal[]{ prX, prY })),
Collections.EMPTY_SET,
Collections.EMPTY_SET);
Subject.doAs(subj, new PrivilegedAction() {
public Object run() {
Security.grant(cl1, cl2);
return null;
}
});
if (policy.implies(pd2, pA) ||
policy.implies(pd2, pB) ||
policy.implies(pd2, pC))
{
throw new Error();
}
pd2 = new ProtectionDomain(pd2.getCodeSource(),
pd2.getPermissions(),
pd2.getClassLoader(),
new Principal[]{ prX, prY });
if (!policy.implies(pd2, pA) ||
!policy.implies(pd2, pB) ||
policy.implies(pd2, pC))
{
throw new Error();
}
pd2 = new ProtectionDomain(pd2.getCodeSource(),
pd2.getPermissions(),
pd2.getClassLoader(),
new Principal[]{ prX, prY, prZ });
if (!policy.implies(pd2, pA) ||
!policy.implies(pd2, pB) ||
policy.implies(pd2, pC))
{
throw new Error();
}
}