java.security.Policy#setPolicy ( )源码实例Demo

下面列出了java.security.Policy#setPolicy ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jdk8u_jdk   文件: ClassDeclaredFieldsTest.java
static void setUp(TestCase test) {
    switch (test) {
        case SECURE:
            if (policy == null && System.getSecurityManager() != null) {
                throw new IllegalStateException("SecurityManager already set");
            } else if (policy == null) {
                policy = new SimplePolicy(TestCase.SECURE, allowAll);
                Policy.setPolicy(policy);
                System.setSecurityManager(new SecurityManager());
            }
            if (System.getSecurityManager() == null) {
                throw new IllegalStateException("No SecurityManager.");
            }
            if (policy == null) {
                throw new IllegalStateException("policy not configured");
            }
            break;
        case UNSECURE:
            if (System.getSecurityManager() != null) {
                throw new IllegalStateException("SecurityManager already set");
            }
            break;
        default:
            throw new InternalError("No such testcase: " + test);
    }
}
 
源代码2 项目: hottub   文件: FieldSetAccessibleTest.java
static void setUp(TestCase test) {
    switch (test) {
        case SECURE:
            if (policy == null && System.getSecurityManager() != null) {
                throw new IllegalStateException("SecurityManager already set");
            } else if (policy == null) {
                policy = new SimplePolicy(TestCase.SECURE, allowAll);
                Policy.setPolicy(policy);
                System.setSecurityManager(new SecurityManager());
            }
            if (System.getSecurityManager() == null) {
                throw new IllegalStateException("No SecurityManager.");
            }
            if (policy == null) {
                throw new IllegalStateException("policy not configured");
            }
            break;
        case UNSECURE:
            if (System.getSecurityManager() != null) {
                throw new IllegalStateException("SecurityManager already set");
            }
            break;
        default:
            throw new InternalError("No such testcase: " + test);
    }
}
 
源代码3 项目: openjdk-jdk8u   文件: XSLTExFuncTest.java
/**
 * Security is enabled, extension function not allowed
 */
public void testExtFuncNotAllowed() {
    Policy p = new SimplePolicy(new AllPermission());
    Policy.setPolicy(p);
    System.setSecurityManager(new SecurityManager());
    TransformerFactory factory = TransformerFactory.newInstance();

    try {
        transform(factory);
    } catch (TransformerConfigurationException e) {
        fail(e.getMessage());
    } catch (TransformerException ex) {
        //expected since extension function is disallowed
        System.out.println("testExtFuncNotAllowed: OK");
    } finally {
        System.setSecurityManager(null);
    }
}
 
源代码4 项目: jdk8u-jdk   文件: ClassDeclaredFieldsTest.java
static void setUp(TestCase test) {
    switch (test) {
        case SECURE:
            if (policy == null && System.getSecurityManager() != null) {
                throw new IllegalStateException("SecurityManager already set");
            } else if (policy == null) {
                policy = new SimplePolicy(TestCase.SECURE, allowAll);
                Policy.setPolicy(policy);
                System.setSecurityManager(new SecurityManager());
            }
            if (System.getSecurityManager() == null) {
                throw new IllegalStateException("No SecurityManager.");
            }
            if (policy == null) {
                throw new IllegalStateException("policy not configured");
            }
            break;
        case UNSECURE:
            if (System.getSecurityManager() != null) {
                throw new IllegalStateException("SecurityManager already set");
            }
            break;
        default:
            throw new InternalError("No such testcase: " + test);
    }
}
 
源代码5 项目: astor   文件: TestConverterManager.java
public void testRemoveDurationConverterSecurity() {
    if (OLD_JDK) {
        return;
    }
    try {
        Policy.setPolicy(RESTRICT);
        System.setSecurityManager(new SecurityManager());
        ConverterManager.getInstance().removeDurationConverter(StringConverter.INSTANCE);
        fail();
    } catch (SecurityException ex) {
        // ok
    } finally {
        System.setSecurityManager(null);
        Policy.setPolicy(ALLOW);
    }
    assertEquals(DURATION_SIZE, ConverterManager.getInstance().getDurationConverters().length);
}
 
源代码6 项目: jdk8u60   文件: TestLoggerBundleSync.java
/**
 * This test will run both with and without a security manager.
 *
 * The test starts a number of threads that will attempt to concurrently
 * set resource bundles on Logger, and verifies the consistency of the
 * obtained results.
 *
 * This is a best effort test.
 *
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {

    try {
        // 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;
                // System.out.println("Granting " + permission);
                return true; // all permissions
            }
        });
        System.setSecurityManager(new SecurityManager());
        test();
    } finally {
        SetRB.executor.shutdownNow();
        SetRBName.executor.shutdownNow();
    }
}
 
源代码7 项目: openjdk-jdk9   文件: TestConfigurationLock.java
/**
 * This test will run both with and without a security manager.
 *
 * The test starts a number of threads that will call
 *     LogManager.reset() concurrently (ResetConf), and a number of threads
 *     that will call readConfiguration() (ReadConf), and then starts a
 *     number of threads that will create new loggers concurrently
 *     (AddLogger), and finally 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 conf = new File(System.getProperty("test.src", "./src"),
            TestConfigurationLock.class.getSimpleName() + ".properties");
    if (!conf.canRead()) {
        throw new IOException("Can't read config file: " + conf.getAbsolutePath());
    }
    System.setProperty("java.util.logging.config.file", conf.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;
            // System.out.println("Granting " + permission);
            return true; // all permissions
        }
    });
    System.setSecurityManager(new SecurityManager());
    test();
}
 
源代码8 项目: birt   文件: OSGILauncher.java
protected void setupSecurityPolicy( ) throws FrameworkException
{
	String eclipseSecurity = (String) properties
			.get( PROP_ECLIPSE_SECURITY );
	if ( eclipseSecurity != null )
	{
		// setup a policy that grants the launcher and path for the
		// framework AllPermissions.
		// Do not set the security manager, this will be done by the
		// framework itself.
		ProtectionDomain domain = OSGILauncher.class.getProtectionDomain( );
		CodeSource source = null;
		if ( domain != null )
			source = OSGILauncher.class.getProtectionDomain( )
					.getCodeSource( );
		if ( domain == null || source == null )
		{
			throw new FrameworkException(
					"Can not automatically set the security manager. Please use a policy file." );
		}
		// get the list of codesource URLs to grant AllPermission to
		URL[] rootURLs = new URL[]{source.getLocation( ), osgiFramework};
		// replace the security policy
		Policy eclipsePolicy = new OSGIPolicy( Policy.getPolicy( ),
				rootURLs );
		Policy.setPolicy( eclipsePolicy );
	}
}
 
源代码9 项目: jdk8u_jdk   文件: TestBase.java
public void tearDown() {
    // turn off security manager and restore policy
    System.setSecurityManager(null);
    Policy.setPolicy(origPolicy);
    if (hasSM) {
        System.setSecurityManager(new SecurityManager());
    }
    System.out.println("\nNumber of tests passed: " + passed);
    System.out.println("Number of tests failed: " + failed + "\n");

    if (errMessage != null ) {
        throw new RuntimeException(errMessage);
    }
}
 
源代码10 项目: jdk8u-jdk   文件: TestBase.java
public void tearDown() {
    // turn off security manager and restore policy
    System.setSecurityManager(null);
    Policy.setPolicy(origPolicy);
    if (hasSM) {
        System.setSecurityManager(new SecurityManager());
    }
    System.out.println("\nNumber of tests passed: " + passed);
    System.out.println("Number of tests failed: " + failed + "\n");

    if (errMessage != null ) {
        throw new RuntimeException(errMessage);
    }
}
 
源代码11 项目: openjdk-jdk9   文件: TestBase.java
public void tearDown() {
    // turn off security manager and restore policy
    System.setSecurityManager(null);
    Policy.setPolicy(origPolicy);
    if (hasSM) {
        System.setSecurityManager(new SecurityManager());
    }
    System.out.println("\nNumber of tests passed: " + passed);
    System.out.println("Number of tests failed: " + failed + "\n");

    if (errMsg != null ) {
        throw new RuntimeException(errMsg);
    }
}
 
源代码12 项目: jdk8u-jdk   文件: TestBase.java
public void tearDown() {
    // turn off security manager and restore policy
    System.setSecurityManager(null);
    Policy.setPolicy(origPolicy);
    if (hasSM) {
        System.setSecurityManager(new SecurityManager());
    }
    System.out.println("\nNumber of tests passed: " + passed);
    System.out.println("Number of tests failed: " + failed + "\n");

    if (errMessage != null ) {
        throw new RuntimeException(errMessage);
    }
}
 
源代码13 项目: openjdk-jdk8u   文件: TestAppletLoggerContext.java
public static void init() {
    SharedSecrets.setJavaAWTAccess(javaAwtAccess);
    if (System.getProperty("test.security", "on").equals("on")) {
        Policy p = new SimplePolicy(new LoggingPermission("control", null),
            new RuntimePermission("setContextClassLoader"),
            new RuntimePermission("shutdownHooks"));
        Policy.setPolicy(p);
        System.setSecurityManager(new SecurityManager());
    }
}
 
/**
 * 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;
            // System.out.println("Granting " + permission);
            return true; // all permissions
        }
    });
    System.setSecurityManager(new SecurityManager());
    test();
}
 
源代码15 项目: openjdk-jdk9   文件: LoggerFinderLoaderTest.java
static void setSecurityManager() {
    if (System.getSecurityManager() == null) {
        Policy.setPolicy(new SimplePolicy(allowControl, allowAccess));
        System.setSecurityManager(new SecurityManager());
    }
}
 
源代码16 项目: TencentKona-8   文件: TestGetLoggerNPE.java
public static void main(String[] args) throws Exception {
    final String testCase = args.length == 0 ? "getLogger" : args[0];
    final JavaAWTAccessStub access = new JavaAWTAccessStub();
    SharedSecrets.setJavaAWTAccess(access);
    final ThreadGroup tg = new ThreadGroup("TestGroup");
    Thread t = new Thread(tg, "test") {
        public void run() {
            try {
                access.setContext(Context.ONE);
                final PrintStream out = System.out;
                System.setOut(null);
                try {
                    if ("getLogger".equals(testCase)) {
                       Logger.getLogger("sun.plugin");
                    } else {
                       LogManager.getLogManager();
                    }
                } finally {
                    System.setOut(out);
                }

                System.out.println(Logger.global);
            } catch (Throwable x) {
                x.printStackTrace();
                thrown = x;
            }
        }
    };
    Policy.setPolicy(new Policy() {
         public boolean implies(ProtectionDomain domain,
                                Permission permission) {
             return true; // all permissions
         }
    });
    System.setSecurityManager(new SecurityManager());
    t.start();
    t.join();
    if (thrown == null) {
        System.out.println("PASSED: " + testCase);
    } else {
        System.err.println("FAILED: " + testCase);
        throw new Error("Test failed: " + testCase + " - " + thrown, thrown);
    }

}
 
源代码17 项目: openjdk-jdk8u   文件: TestGetLoggerNPE.java
public static void main(String[] args) throws Exception {
    final String testCase = args.length == 0 ? "getLogger" : args[0];
    final JavaAWTAccessStub access = new JavaAWTAccessStub();
    SharedSecrets.setJavaAWTAccess(access);
    final ThreadGroup tg = new ThreadGroup("TestGroup");
    Thread t = new Thread(tg, "test") {
        public void run() {
            try {
                access.setContext(Context.ONE);
                final PrintStream out = System.out;
                System.setOut(null);
                try {
                    if ("getLogger".equals(testCase)) {
                       Logger.getLogger("sun.plugin");
                    } else {
                       LogManager.getLogManager();
                    }
                } finally {
                    System.setOut(out);
                }

                System.out.println(Logger.global);
            } catch (Throwable x) {
                x.printStackTrace();
                thrown = x;
            }
        }
    };
    Policy.setPolicy(new Policy() {
         public boolean implies(ProtectionDomain domain,
                                Permission permission) {
             return true; // all permissions
         }
    });
    System.setSecurityManager(new SecurityManager());
    t.start();
    t.join();
    if (thrown == null) {
        System.out.println("PASSED: " + testCase);
    } else {
        System.err.println("FAILED: " + testCase);
        throw new Error("Test failed: " + testCase + " - " + thrown, thrown);
    }

}
 
源代码18 项目: openjdk-jdk9   文件: PermissionTest.java
@Test
public void currentWithPermission() {
    Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
    ProcessHandle.current();
}
 
源代码19 项目: pro-grade   文件: PolicyFileGeneratorJSM.java
/**
 * JSM Constructor.
 */
public PolicyFileGeneratorJSM() {
    Policy.setPolicy(new NotifyAndAllowPolicy(null, new GeneratePolicyFromDeniedPermissions()));
}
 
源代码20 项目: openjdk-jdk9   文件: BasePlatformLoggerTest.java
static void setSecurityManager() {
    if (System.getSecurityManager() == null) {
        Policy.setPolicy(new SimplePolicy(allowControl, allowAccess, allowAll));
        System.setSecurityManager(new SecurityManager());
    }
}