Code example for Permissions

Methods: add

0
 
    public static boolean testing = false;
 
    private Permissions permissionsDenied = new Permissions();
 
    private Permissions permissionsGranted = new Permissions();
 
    // Represents a mode(grant/deny) for installed Security Manager. 
    private boolean mode = true;
 
    public SecurityTest() { 
        denyMode(); //set default mode 
    } 
 
    public SecurityTest(String name) {
        super(name);
        denyMode(); //set default mode 
    } 
 
    @Override 
    protected void runTest() throws Throwable { 
        if (System.getSecurityManager() != null) {
            fail("There MUST be no security manager installed!");
        } 
 
        SecurityManager sm = new SecurityManager() {
            @Override 
            public void checkPermission(Permission permission) {
                //System.out.println("P: " + permission); 
                if (mode) { //deny mode
                    if (!permissionsDenied.implies(permission)) {
                        return; 
                    } 
                } else { // grant mode 
 
                    //System.out.println("P: " + permissionsGranted); 
                    if (permissionsGranted.implies(permission)) {
                        return; 
                    } 
                } 
 
                // throw exception 
                ProtectionDomain domain = new ProtectionDomain(null,
                        new Permissions());
 
                ProtectionDomain[] context = new ProtectionDomain[] { domain };
                AccessControlContext accContext = new AccessControlContext(
                        context);
 
                accContext.checkPermission(permission);
            } 
        }; 
 
        System.setSecurityManager(sm);
 
        try { 
            super.runTest(); 
        } finally { 
            System.setSecurityManager(null);
        } 
    } 
 
    /** 
     * Verifies that exception has correct associated permission class 
     * 
     * @param exception - to be verified 
     * @param permission - permission class for comparing 
     */ 
    public final void assertEquals(AccessControlException exception,
            Class<? extends Permission> permission) {
        if (!permission.isInstance(exception.getPermission())) {
            fail("No expected " + permission.getName());
        } 
    } 
 
    /** 
     * Grants specified permission 
     * It is used only in grant mode 
     */ 
    public void grantPermission(Permission permission) {
 
        assertFalse("Grant mode", mode);
 
        permissionsGranted.add(permission);
 
        //System.out.println("PG: " + permission); 
    } 
 
    /** 
     * Denies specified permission 
     * It is used only in deny mode 
     */ 
    public void denyPermission(Permission permission) {
 
        assertTrue("Deny mode", mode);
 
        permissionsDenied.add(permission);
    } 
 
    /** 
     * Sets deny mode 
     * all permissions are granted, test can only deny specific permission 
     */ 
    public void denyMode() { 
        mode = true;
 
        permissionsGranted.add(new AllPermission());
        permissionsDenied = new Permissions();
    } 
 
    /** 
     * Sets grant mode