use of java.security.SecurityPermission in project voltdb by VoltDB.
the class JSR166TestCase method runWithSecurityManagerWithPermissions.
/**
* Runs Runnable r with a security policy that permits precisely
* the specified permissions. If there is no current security
* manager, a temporary one is set for the duration of the
* Runnable. We require that any security manager permit
* getPolicy/setPolicy.
*/
public void runWithSecurityManagerWithPermissions(Runnable r, Permission... permissions) {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Policy savedPolicy = Policy.getPolicy();
try {
Policy.setPolicy(permissivePolicy());
System.setSecurityManager(new SecurityManager());
runWithSecurityManagerWithPermissions(r, permissions);
} finally {
System.setSecurityManager(null);
Policy.setPolicy(savedPolicy);
}
} else {
Policy savedPolicy = Policy.getPolicy();
AdjustablePolicy policy = new AdjustablePolicy(permissions);
Policy.setPolicy(policy);
try {
r.run();
} finally {
policy.addPermission(new SecurityPermission("setPolicy"));
Policy.setPolicy(savedPolicy);
}
}
}
use of java.security.SecurityPermission in project lucene-solr by apache.
the class LuceneTestCase method runWithRestrictedPermissions.
/**
* Runs a code part with restricted permissions (be sure to add all required permissions,
* because it would start with empty permissions). You cannot grant more permissions than
* our policy file allows, but you may restrict writing to several dirs...
* <p><em>Note:</em> This assumes a {@link SecurityManager} enabled, otherwise it
* stops test execution. If enabled, it needs the following {@link SecurityPermission}:
* {@code "createAccessControlContext"}
*/
public static <T> T runWithRestrictedPermissions(PrivilegedExceptionAction<T> action, Permission... permissions) throws Exception {
assumeTrue("runWithRestrictedPermissions requires a SecurityManager enabled", System.getSecurityManager() != null);
// be sure to have required permission, otherwise doPrivileged runs with *no* permissions:
AccessController.checkPermission(new SecurityPermission("createAccessControlContext"));
final PermissionCollection perms = new Permissions();
Arrays.stream(permissions).forEach(perms::add);
final AccessControlContext ctx = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
try {
return AccessController.doPrivileged(action, ctx);
} catch (PrivilegedActionException e) {
throw e.getException();
}
}
use of java.security.SecurityPermission in project caffeine by ben-manes.
the class JSR166TestCase method runWithSecurityManagerWithPermissions.
/**
* Runs Runnable r with a security policy that permits precisely
* the specified permissions. If there is no current security
* manager, a temporary one is set for the duration of the
* Runnable. We require that any security manager permit
* getPolicy/setPolicy.
*/
public void runWithSecurityManagerWithPermissions(Runnable r, Permission... permissions) {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
Policy savedPolicy = Policy.getPolicy();
try {
Policy.setPolicy(permissivePolicy());
System.setSecurityManager(new SecurityManager());
runWithSecurityManagerWithPermissions(r, permissions);
} finally {
System.setSecurityManager(null);
Policy.setPolicy(savedPolicy);
}
} else {
Policy savedPolicy = Policy.getPolicy();
AdjustablePolicy policy = new AdjustablePolicy(permissions);
Policy.setPolicy(policy);
try {
r.run();
} finally {
policy.addPermission(new SecurityPermission("setPolicy"));
Policy.setPolicy(savedPolicy);
}
}
}
use of java.security.SecurityPermission in project openj9 by eclipse.
the class Test_AccessController method test_doPrivileged_createAccessControlContext.
/**
* java.security.AccessController#doPrivileged(java.security.PrivilegedAction, AccessControlContext)
*/
@Test
public void test_doPrivileged_createAccessControlContext() {
/*
* Classes loaded by this Classloader withPermCL have the Permission JAVA_HOME_READ & CREATE_ACC
*/
ClassLoader withPermCL = new TestURLClassLoader(new URL[] { this.getClass().getProtectionDomain().getCodeSource().getLocation() }, null) {
public PermissionCollection getPermissions(CodeSource cs) {
PermissionCollection pc = super.getPermissions(cs);
pc.add(new PropertyPermission("java.home", "read"));
pc.add(new SecurityPermission("createAccessControlContext"));
return pc;
}
};
try {
Class<?> mwp = Class.forName("org.openj9.test.java.security.Test_AccessController$MainWithPerm", true, withPermCL);
Object mwpObj = mwp.newInstance();
Method m = mwp.getDeclaredMethod("testCreateACC");
m.setAccessible(true);
m.invoke(mwpObj);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("FAIL: TEST FAILED, probably setup issue.");
}
}
use of java.security.SecurityPermission in project wildfly by wildfly.
the class BouncyCastleModuleTestCase method createDeployment.
@Deployment(name = BC_DEPLOYMENT, testable = true)
public static WebArchive createDeployment() {
WebArchive archive = ShrinkWrap.create(WebArchive.class, BC_DEPLOYMENT + ".war");
archive.addPackage(BouncyCastleModuleTestCase.class.getPackage());
// needed to load CDI for arquillian
archive.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
archive.addAsManifestResource(createPermissionsXmlAsset(new SecurityPermission("insertProvider")), "permissions.xml");
archive.setManifest(new StringAsset("" + "Manifest-Version: 1.0\n" + "Dependencies: org.bouncycastle.bcprov, org.bouncycastle.bcpkix, org.bouncycastle.bcmail\n"));
return archive;
}
Aggregations