Search in sources :

Example 66 with Permission

use of java.security.Permission in project jboss-modules by jboss-modules.

the class PermissionsTest method testExpansion.

@Test
public void testExpansion() throws Exception {
    Module module = moduleLoader.loadModule(MODULE_WITH_INVALID_EXPANSION);
    Enumeration<Permission> permissions = module.getPermissionCollection().elements();
    assertTrue(permissions.hasMoreElements());
    Permission firstPermission = permissions.nextElement();
    assertEquals(FilePermission.class.getName(), firstPermission.getClass().getName());
    assertFalse(permissions.hasMoreElements());
}
Also used : Permission(java.security.Permission) FilePermission(java.io.FilePermission) Module(org.jboss.modules.Module) FilePermission(java.io.FilePermission) Test(org.junit.Test)

Example 67 with Permission

use of java.security.Permission in project jboss-modules by jboss-modules.

the class FactoryPermissionCollection method getAssembled.

Permissions getAssembled() {
    if (assembled == null) {
        synchronized (this) {
            if (assembled == null) {
                final Permissions assembled = new Permissions();
                for (PermissionFactory factory : factories) {
                    if (factory != null) {
                        final Permission permission = factory.construct();
                        if (permission != null) {
                            assembled.add(permission);
                        }
                    }
                }
                assembled.setReadOnly();
                this.assembled = assembled;
            }
        }
    }
    return assembled;
}
Also used : Permissions(java.security.Permissions) Permission(java.security.Permission)

Example 68 with Permission

use of java.security.Permission in project derby by apache.

the class SystemPrivilegesPermissionTest method checkImplies.

/**
 * Tests DatabasePermission.implies().
 */
private void checkImplies(Permission[] dbp0, Permission[] dbp1, boolean[][] impls) throws IOException {
    for (int i = 0; i < dbp0.length; i++) {
        final Permission p0 = dbp0[i];
        for (int j = 0; j < dbp1.length; j++) {
            final Permission p1 = dbp1[j];
            assertEquals("test: " + p0 + ".implies" + p1, impls[i][j], p0.implies(p1));
        // assertEquals("test: " + p1 + ".implies" + p0,
        // impls[j][i], p1.implies(p0));
        }
    }
}
Also used : SystemPermission(org.apache.derby.shared.common.security.SystemPermission) AllPermission(java.security.AllPermission) DatabasePermission(org.apache.derby.security.DatabasePermission) Permission(java.security.Permission)

Example 69 with Permission

use of java.security.Permission in project openj9 by eclipse.

the class PR66930ACCcheckPermission method test.

void test() {
    final String baseName = this.getClass().getName();
    ClassLoader privilegedCL = new URLClassLoader(new URL[] { this.getClass().getProtectionDomain().getCodeSource().getLocation() }, null) {

        public PermissionCollection getPermissions(CodeSource cs) {
            PermissionCollection pc = super.getPermissions(cs);
            pc.add(PERM_CREATE_ACC);
            return pc;
        }
    };
    try {
        Class<?> cls = Class.forName(baseName + "$PrivilegedClass", true, privilegedCL);
        Object obj = cls.newInstance();
        Method mt = cls.getMethod("test", AccessControlContext.class);
        ProtectionDomain pd1 = new ProtectionDomain(null, null) {

            public boolean implies(Permission perm) {
                untrustedPDimpliesCalled = true;
                System.out.println("Untrusted ProtectionDomain.implies() has been called");
                return true;
            }
        };
        System.setSecurityManager(new SecurityManager());
        AccessControlContext accSimple = new AccessControlContext(new ProtectionDomain[] { pd1 });
        untrustedPDimpliesCalled = false;
        accSimple.checkPermission(PERM_JAVA_VERSION_READ);
        if (!untrustedPDimpliesCalled) {
            System.out.println("FAILED: untrusted ProtectionDomain.implies() should have been called");
        }
        AccessControlContext accInjected = AccessController.doPrivileged(new PrivilegedAction<AccessControlContext>() {

            public AccessControlContext run() {
                return AccessController.getContext();
            }
        }, accSimple);
        untrustedPDimpliesCalled = false;
        try {
            accInjected.checkPermission(PERM_JAVA_VERSION_READ);
            System.out.println("FAILED: AccessControlException NOT thrown");
        } catch (AccessControlException ace) {
            System.out.println("GOOD: AccessControlException is expected");
        }
        if (untrustedPDimpliesCalled) {
            System.out.println("FAILED: untrusted ProtectionDomain.implies() should NOT be called");
        }
        mt.invoke(obj, accInjected);
        System.out.println("ALL TESTS FINISHED");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("FAIL: TEST FAILED, probably setup issue.");
    }
}
Also used : PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) AccessControlException(java.security.AccessControlException) Method(java.lang.reflect.Method) CodeSource(java.security.CodeSource) AccessControlException(java.security.AccessControlException) AccessControlContext(java.security.AccessControlContext) URLClassLoader(java.net.URLClassLoader) SecurityPermission(java.security.SecurityPermission) PropertyPermission(java.util.PropertyPermission) Permission(java.security.Permission) URLClassLoader(java.net.URLClassLoader)

Example 70 with Permission

use of java.security.Permission in project openj9 by eclipse.

the class Test_AccessControlContext method test_Constructor.

/**
 * @tests
 *        java.security.AccessControlContext#AccessControlContext(java.security
 *        .ProtectionDomain[])
 */
@Test
public void test_Constructor() {
    final Permission perm = new PropertyPermission("java.class.path", "read");
    PermissionCollection col = perm.newPermissionCollection();
    col.add(perm);
    final ProtectionDomain pd = new ProtectionDomain(null, col);
    AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { pd });
    try {
        acc.checkPermission(perm);
    } catch (SecurityException e) {
        AssertJUnit.assertTrue("Should have permission", false);
    }
    final boolean[] result = new boolean[] { false };
    Thread th = new Thread(new Runnable() {

        public void run() {
            AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { pd });
            try {
                acc.checkPermission(perm);
                result[0] = true;
            } catch (SecurityException e) {
            }
        }
    });
    th.start();
    try {
        th.join();
    } catch (InterruptedException e) {
    }
    AssertJUnit.assertTrue("Thread should have permission", result[0]);
}
Also used : PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) PropertyPermission(java.util.PropertyPermission) Permission(java.security.Permission) PropertyPermission(java.util.PropertyPermission) Test(org.testng.annotations.Test)

Aggregations

Permission (java.security.Permission)236 Test (org.junit.Test)55 PermissionCollection (java.security.PermissionCollection)39 FilePermission (java.io.FilePermission)38 Permissions (java.security.Permissions)31 ProtectionDomain (java.security.ProtectionDomain)27 IOException (java.io.IOException)20 AllPermission (java.security.AllPermission)20 QuickTest (com.hazelcast.test.annotation.QuickTest)17 File (java.io.File)17 URL (java.net.URL)16 AccessControlException (java.security.AccessControlException)14 Principal (java.security.Principal)14 PropertyPermission (java.util.PropertyPermission)14 Policy (java.security.Policy)13 MBeanPermission (javax.management.MBeanPermission)13 AccessControlContext (java.security.AccessControlContext)12 CodeSource (java.security.CodeSource)11 SecurityPermission (java.security.SecurityPermission)11 ArrayList (java.util.ArrayList)10