Search in sources :

Example 6 with Permission

use of java.security.Permission in project jdk8u_jdk by JetBrains.

the class TestLogConfigurationDeadLock method main.

/**
     * This test will run both with and without a security manager.
     *
     * The test starts a number of threads that will call
     *     LogManager.readConfiguration() concurrently (ReadConf), then starts
     *     a number of threads that will create new loggers concurrently
     *     (AddLogger), and then two additional threads: one (Stopper) that
     *     will stop the test after 4secs (TIME ms), and one DeadlockDetector
     *     that will attempt to detect deadlocks.
     * If after 4secs no deadlock was detected and no exception was thrown
     * then the test is considered a success and passes.
     *
     * This procedure is done twice: once without a security manager and once
     * again with a security manager - which means the test takes ~8secs to
     * run.
     *
     * Note that 8sec may not be enough to detect issues if there are some.
     * This is a best effort test.
     *
     * @param args the command line arguments
     */
public static void main(String[] args) throws Exception {
    // test without security
    System.out.println("No security");
    test();
    // test with security
    System.out.println("\nWith security");
    Policy.setPolicy(new Policy() {

        @Override
        public boolean implies(ProtectionDomain domain, Permission permission) {
            if (super.implies(domain, permission))
                return true;
            // all permissions
            return true;
        }
    });
    System.setSecurityManager(new SecurityManager());
    test();
}
Also used : Policy(java.security.Policy) ProtectionDomain(java.security.ProtectionDomain) Permission(java.security.Permission)

Example 7 with Permission

use of java.security.Permission in project jdk8u_jdk by JetBrains.

the class TestLogConfigurationDeadLockWithConf method main.

/**
     * This test will run both with and without a security manager.
     *
     * The test starts a number of threads that will call
     *     LogManager.readConfiguration() concurrently (ReadConf), then starts
     *     a number of threads that will create new loggers concurrently
     *     (AddLogger), and then two additional threads: one (Stopper) that
     *     will stop the test after 4secs (TIME ms), and one DeadlockDetector
     *     that will attempt to detect deadlocks.
     * If after 4secs no deadlock was detected and no exception was thrown
     * then the test is considered a success and passes.
     *
     * This procedure is done twice: once without a security manager and once
     * again with a security manager - which means the test takes ~8secs to
     * run.
     *
     * Note that 8sec may not be enough to detect issues if there are some.
     * This is a best effort test.
     *
     * @param args the command line arguments
     * @throws java.lang.Exception if the test fails.
     */
public static void main(String[] args) throws Exception {
    File config = new File(System.getProperty("test.src", "."), "deadlockconf.properties");
    if (!config.canRead()) {
        System.err.println("Can't read config file: test cannot execute.");
        System.err.println("Please check your test environment: ");
        System.err.println("\t -Dtest.src=" + System.getProperty("test.src", "."));
        System.err.println("\t config file is: " + config.getAbsolutePath());
        throw new RuntimeException("Can't read config file: " + config.getAbsolutePath());
    }
    System.setProperty("java.util.logging.config.file", config.getAbsolutePath());
    // test without security
    System.out.println("No security");
    test();
    // test with security
    System.out.println("\nWith security");
    Policy.setPolicy(new Policy() {

        @Override
        public boolean implies(ProtectionDomain domain, Permission permission) {
            if (super.implies(domain, permission))
                return true;
            // all permissions
            return true;
        }
    });
    System.setSecurityManager(new SecurityManager());
    test();
}
Also used : Policy(java.security.Policy) ProtectionDomain(java.security.ProtectionDomain) Permission(java.security.Permission) File(java.io.File)

Example 8 with Permission

use of java.security.Permission in project jdk8u_jdk by JetBrains.

the class bug8078165 method main.

public static void main(final String[] args) throws Exception {
    // Mac only
    System.setSecurityManager(new SecurityManager() {

        @Override
        public void checkPermission(Permission permission) {
        // Just allows everything
        }
    });
    // The method shouldn't throw NPE
    Toolkit.getDefaultToolkit().getImage(new URL("file://./dummyImage@2x.png"));
    Toolkit.getDefaultToolkit().getImage("./dummyImage@2x.png");
}
Also used : Permission(java.security.Permission) URL(java.net.URL)

Example 9 with Permission

use of java.security.Permission in project robovm by robovm.

the class OldRuntimeTest method test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File.

public void test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File() {
    String[] envp = getEnv();
    File workFolder = Support_Resources.createTempFolder();
    checkExec(5, envp, workFolder);
    checkExec(5, null, null);
    try {
        Runtime.getRuntime().exec((String[]) null, null, workFolder);
        fail("NullPointerException should be thrown.");
    } catch (IOException ioe) {
        fail("IOException was thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    try {
        Runtime.getRuntime().exec(new String[] { "ls", null }, null, workFolder);
        fail("NullPointerException should be thrown.");
    } catch (IOException ioe) {
        fail("IOException was thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    SecurityManager sm = new SecurityManager() {

        public void checkPermission(Permission perm) {
            if (perm.getName().equals("checkExec")) {
                throw new SecurityException();
            }
        }

        public void checkExec(String cmd) {
            throw new SecurityException();
        }
    };
    try {
        Runtime.getRuntime().exec(new String[] { "" }, envp, workFolder);
        fail("IOException should be thrown.");
    } catch (IOException e) {
    //expected
    }
}
Also used : Permission(java.security.Permission) IOException(java.io.IOException) File(java.io.File)

Example 10 with Permission

use of java.security.Permission in project robovm by robovm.

the class OldRuntimeTest method test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File.

public void test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File() {
    String[] envp = getEnv();
    File workFolder = Support_Resources.createTempFolder();
    checkExec(2, envp, workFolder);
    checkExec(2, null, null);
    try {
        Runtime.getRuntime().exec((String) null, null, workFolder);
        fail("NullPointerException should be thrown.");
    } catch (IOException ioe) {
        fail("IOException was thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    SecurityManager sm = new SecurityManager() {

        public void checkPermission(Permission perm) {
            if (perm.getName().equals("checkExec")) {
                throw new SecurityException();
            }
        }

        public void checkExec(String cmd) {
            throw new SecurityException();
        }
    };
    try {
        Runtime.getRuntime().exec("", envp, workFolder);
        fail("SecurityException should be thrown.");
    } catch (IllegalArgumentException iae) {
    //expected
    } catch (IOException e) {
        fail("IOException was thrown.");
    }
}
Also used : Permission(java.security.Permission) IOException(java.io.IOException) File(java.io.File)

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