AccessControllerTestpublic class AccessControllerTest extends TestCase
Fields Summary |
---|
SecurityManager | old | TestPermission | p | CodeSource | codeSource | PermissionCollection | c0 | PermissionCollection | c1 | PermissionCollection | c2 |
Methods Summary |
---|
public static void | main(java.lang.String[] args)
AccessControllerTest t = new AccessControllerTest();
t.setUp();
t.test_do_privileged1();
t.tearDown();
| private static void | setProtectionDomain(java.lang.Class c, java.security.ProtectionDomain pd)
Field fields[] = Class.class.getDeclaredFields();
for(Field f : fields){
if("pd".equals(f.getName())){
f.setAccessible(true);
try {
f.set(c, pd);
} catch (IllegalArgumentException e) {
fail("Protection domain could not be set");
} catch (IllegalAccessException e) {
fail("Protection domain could not be set");
}
break;
}
}
| protected void | setUp()
old = System.getSecurityManager();
codeSource = null;
p = new TestPermission();
c0 = p.newPermissionCollection();
c1 = p.newPermissionCollection();
c2 = p.newPermissionCollection();
super.setUp();
| protected void | tearDown()
System.setSecurityManager(old);
super.tearDown();
| public void | test_do_privileged1()
// add TestPermission to T1 and T2 only
c1.add(p);
c2.add(p);
setProtectionDomain(T0.class, new ProtectionDomain(codeSource, c0));
setProtectionDomain(T1.class, new ProtectionDomain(codeSource, c1));
setProtectionDomain(T2.class, new ProtectionDomain(codeSource, c2));
System.setSecurityManager(new SecurityManager());
try {
T0.f0();
fail("expected java.security.AccessControlException");
}
catch(java.security.AccessControlException e){
// expected behavior
}
catch(Exception e){
fail("expected java.security.AccessControlException, got "+e.getClass().getName());
}
| public void | test_do_privileged2()
// add TestPermission to T0, T1, T2
c0.add(p);
c1.add(p);
c2.add(p);
setProtectionDomain(T0.class, new ProtectionDomain(codeSource, c0));
setProtectionDomain(T1.class, new ProtectionDomain(codeSource, c1));
setProtectionDomain(T2.class, new ProtectionDomain(codeSource, c2));
System.setSecurityManager(new SecurityManager());
try {
String res = T0.f0();
assertEquals("ok", res);
}
catch(java.security.AccessControlException e){
fail("expected no java.security.AccessControlException");
}
catch(Exception e){
fail("expected no exception, got "+e.getClass().getName());
}
| public void | test_do_privileged3()
// add TestPermission to T1 and T2, and call it with doPrivileged from T1
c1.add(p);
c2.add(p);
setProtectionDomain(T0.class, new ProtectionDomain(codeSource, c0));
setProtectionDomain(T1.class, new ProtectionDomain(codeSource, c1));
setProtectionDomain(T2.class, new ProtectionDomain(codeSource, c2));
System.setSecurityManager(new SecurityManager());
try {
String res = T0.f0_priv();
assertEquals("ok", res);
}
catch(java.security.AccessControlException e){
fail("expected no java.security.AccessControlException");
}
catch(Exception e){
fail("expected no exception, got "+e.getClass().getName());
}
|
|