List of usage examples for java.lang SecurityManager SecurityManager
public SecurityManager()
SecurityManager
. From source file:com.alertlogic.aws.kinesis.test1.kcl.CountingRecordProcessorTest.java
/** * A test helper to prevent calls to System.exit() from existing our JVM. We need to test failure behavior and want * to know if System.exit() was called.//from w w w. j a v a2 s.c om * * @param testBlock A code block that is expected to call System.exit(). */ private void expectSystemExitWhenExecuting(Callable<Void> testBlock) throws Exception { final SecurityException expectedPreventionOfSystemExit = new SecurityException( "System.exit not allowed for this test."); // Disable System.exit() for this test final SecurityManager sm = new SecurityManager() { @Override public void checkExit(int status) { throw expectedPreventionOfSystemExit; } @Override public void checkPermission(Permission perm) { // Do nothing, allowing this security manager to be replaced } }; SecurityManager oldSm = System.getSecurityManager(); System.setSecurityManager(sm); boolean systemExitCalled = false; try { testBlock.call(); fail("Expected System.exit to be called and throw a SecurityException by our test SecurityManager"); } catch (SecurityException ex) { assertEquals("Expected SecurityException to be thrown when System.exit called", expectedPreventionOfSystemExit, ex); systemExitCalled = true; } finally { System.setSecurityManager(oldSm); } assertTrue("Expected test to call System.exit", systemExitCalled); }
From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java
@Test public void systemPropertiesSecurityManager() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(TestBean.class); bd.getPropertyValues().add("country", "#{systemProperties.country}"); ac.registerBeanDefinition("tb", bd); SecurityManager oldSecurityManager = System.getSecurityManager(); try {/*from w w w. j a v a 2 s . c o m*/ System.setProperty("country", "NL"); SecurityManager securityManager = new SecurityManager() { @Override public void checkPropertiesAccess() { throw new AccessControlException("Not Allowed"); } @Override public void checkPermission(Permission perm) { // allow everything else } }; System.setSecurityManager(securityManager); ac.refresh(); TestBean tb = ac.getBean("tb", TestBean.class); assertEquals("NL", tb.getCountry()); } finally { System.setSecurityManager(oldSecurityManager); System.getProperties().remove("country"); } }
From source file:org.apache.hadoop.hdfs.TestDFSShell.java
@Test public void testPut() throws IOException { Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build(); FileSystem fs = cluster.getFileSystem(); assertTrue("Not a HDFS: " + fs.getUri(), fs instanceof DistributedFileSystem); final DistributedFileSystem dfs = (DistributedFileSystem) fs; try {/* w w w . j a v a 2 s . com*/ // remove left over crc files: new File(TEST_ROOT_DIR, ".f1.crc").delete(); new File(TEST_ROOT_DIR, ".f2.crc").delete(); final File f1 = createLocalFile(new File(TEST_ROOT_DIR, "f1")); final File f2 = createLocalFile(new File(TEST_ROOT_DIR, "f2")); final Path root = mkdir(dfs, new Path("/test/put")); final Path dst = new Path(root, "dst"); show("begin"); final Thread copy2ndFileThread = new Thread() { @Override public void run() { try { show("copy local " + f2 + " to remote " + dst); dfs.copyFromLocalFile(false, false, new Path(f2.getPath()), dst); } catch (IOException ioe) { show("good " + StringUtils.stringifyException(ioe)); return; } //should not be here, must got IOException assertTrue(false); } }; //use SecurityManager to pause the copying of f1 and begin copying f2 SecurityManager sm = System.getSecurityManager(); System.out.println("SecurityManager = " + sm); System.setSecurityManager(new SecurityManager() { private boolean firstTime = true; @Override public void checkPermission(Permission perm) { if (firstTime) { Thread t = Thread.currentThread(); if (!t.toString().contains("DataNode")) { String s = "" + Arrays.asList(t.getStackTrace()); if (s.contains("FileUtil.copyContent")) { //pause at FileUtil.copyContent firstTime = false; copy2ndFileThread.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { } } } } } }); show("copy local " + f1 + " to remote " + dst); dfs.copyFromLocalFile(false, false, new Path(f1.getPath()), dst); show("done"); try { copy2ndFileThread.join(); } catch (InterruptedException e) { } System.setSecurityManager(sm); // copy multiple files to destination directory final Path destmultiple = mkdir(dfs, new Path("/test/putmultiple")); Path[] srcs = new Path[2]; srcs[0] = new Path(f1.getPath()); srcs[1] = new Path(f2.getPath()); dfs.copyFromLocalFile(false, false, srcs, destmultiple); srcs[0] = new Path(destmultiple, "f1"); srcs[1] = new Path(destmultiple, "f2"); assertTrue(dfs.exists(srcs[0])); assertTrue(dfs.exists(srcs[1])); // move multiple files to destination directory final Path destmultiple2 = mkdir(dfs, new Path("/test/movemultiple")); srcs[0] = new Path(f1.getPath()); srcs[1] = new Path(f2.getPath()); dfs.moveFromLocalFile(srcs, destmultiple2); assertFalse(f1.exists()); assertFalse(f2.exists()); srcs[0] = new Path(destmultiple2, "f1"); srcs[1] = new Path(destmultiple2, "f2"); assertTrue(dfs.exists(srcs[0])); assertTrue(dfs.exists(srcs[1])); f1.delete(); f2.delete(); } finally { try { dfs.close(); } catch (Exception e) { } cluster.shutdown(); } }
From source file:src.eidreader.EstEIDUtil.java
public void unused2_init() { System.err.println("Gonna set the security manager..."); //~ System.out.println("toto"); System.setSecurityManager(new SecurityManager() { @Override// w ww . j a v a 2 s . com public void checkPermission(Permission permission) { if (permission instanceof CardPermission) { return; } //~ if (permission instanceof RuntimePermission) { //~ return; //~ } //~ if (permission instanceof FilePermission) { //~ return; //~ } java.security.AccessController.checkPermission(permission); } }); System.err.println("Initialized"); }
From source file:de.fosd.jdime.Main.java
/** * Dumps the given <code>FileArtifact</code> using the <code>mode</code>. * * @param artifact//from ww w. ja v a 2s. c om * the <code>Artifact</code> to dump * @param mode * the dump format */ private static void dump(FileArtifact artifact, DumpMode mode) { if (mode == DumpMode.NONE) { return; } if (mode == DumpMode.FILE_DUMP || artifact.isDirectory()) { System.out.println(artifact.dump(mode)); } else { SecurityManager prevSecManager = System.getSecurityManager(); SecurityManager noExitManager = new SecurityManager() { @Override public void checkPermission(Permission perm) { // allow anything. } @Override public void checkPermission(Permission perm, Object context) { // allow anything. } @Override public void checkExit(int status) { super.checkExit(status); throw new SecurityException("Captured attempt to exit JVM."); } }; ASTNodeArtifact astArtifact; System.setSecurityManager(noExitManager); try { astArtifact = new ASTNodeArtifact(artifact); } catch (RuntimeException e) { LOG.log(Level.WARNING, e, () -> "Could not parse " + artifact + " to an ASTNodeArtifact."); return; } finally { System.setSecurityManager(prevSecManager); } System.out.println(astArtifact.dump(mode)); } }
From source file:org.cytoscape.app.internal.manager.App.java
public static boolean delete(File f) { if (!f.exists()) { System.err.println("Cannot delete, file does not exist: " + f.getPath()); return false; }/*from www.j a va2 s .c om*/ f.setReadable(true); f.setWritable(true); if (!f.canWrite()) { System.err.println("Cannot delete, file is read-only: " + f.getPath()); return false; } // Hack attempt File parent = f.getParentFile(); parent.setReadable(true); parent.setWritable(true); if (!parent.canWrite()) { System.err.println("Cannot delete, parent folder read-only: " + parent.getPath()); return false; } try { (new SecurityManager()).checkDelete(f.getPath()); } catch (Exception ex) { System.err.println("Cannot delete file, " + ex.getMessage()); return false; } boolean ret = f.delete(); if (!ret) System.err.println("Delete failed: " + f.getPath()); return ret; }
From source file:org.cruxframework.crux.tools.compile.AbstractCruxCompiler.java
/** * //from w w w . j av a 2 s . c om */ private void setSecurityManagerToAvoidSystemExit() { AccessController.doPrivileged(new PrivilegedAction<Boolean>() { public Boolean run() { originalSecurityManager = System.getSecurityManager(); System.setSecurityManager(new SecurityManager() { @Override public void checkExit(int status) { if (status == 0) { throw new DoNotExitException(); } super.checkExit(status); } @Override public void checkPermission(Permission perm) { } @Override public void checkPermission(Permission perm, Object context) { } }); return true; } }); }
From source file:org.digidoc4j.main.DigiDoc4JTest.java
private static void forbidSystemExitCall() { final SecurityManager preventExitSecurityManager = new SecurityManager() { public void checkPermission(Permission permission) { }//from w w w. j a v a 2 s . c o m @Override public void checkExit(int status) { super.checkExit(status); throw new DigiDoc4JUtilityException(status, "preventing system exist"); } }; System.setSecurityManager(preventExitSecurityManager); }