FileDocCategorySizeDatePackage
AccessControllerTest.javaAPI DocAndroid 1.5 API8040Wed May 06 22:41:06 BST 2009tests.security

AccessControllerTest

public class AccessControllerTest extends TestCase

Fields Summary
SecurityManager
old
TestPermission
p
CodeSource
codeSource
PermissionCollection
c0
PermissionCollection
c1
PermissionCollection
c2
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        AccessControllerTest t = new AccessControllerTest();
        t.setUp();
        t.test_do_privileged1();
        t.tearDown();
    
private static voidsetProtectionDomain(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 voidsetUp()

        old = System.getSecurityManager();
        codeSource = null;
        p = new TestPermission();
        c0 = p.newPermissionCollection();
        c1 = p.newPermissionCollection();
        c2 = p.newPermissionCollection();
        super.setUp();
    
protected voidtearDown()

        System.setSecurityManager(old);
        super.tearDown();
    
public voidtest_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 voidtest_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 voidtest_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());
        }