Skip to content
Browse files

Use SecurityManager recommended by Stefan Birkner

  • Loading branch information...
1 parent 917a88f commit 4596630d76860059088be0fe4376679452fc3b35 @matthewfarwell matthewfarwell committed Mar 9, 2012
Showing with 206 additions and 12 deletions.
  1. +206 −12 src/test/java/org/junit/tests/running/core/MainRunner.java
View
218 src/test/java/org/junit/tests/running/core/MainRunner.java
@@ -3,9 +3,10 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.security.Permission;
+import java.io.FileDescriptor;
+import java.net.InetAddress;
public class MainRunner {
-
private static class ExitException extends SecurityException {
private static final long serialVersionUID= -9104651568237766642L;
@@ -21,24 +22,218 @@ public int getStatus() {
}
}
- private static class NoExitSecurityManager extends SecurityManager {
+ /**
+ * A {@code NoExitSecurityManager} throws a {@link ExitException} exception
+ * whenever {@link #checkExit(int)} is called. All other method calls are
+ * delegated to the original security manager.
+ */
+ public class NoExitSecurityManager extends SecurityManager {
+ private final SecurityManager originalSecurityManager;
+
+ public NoExitSecurityManager(SecurityManager originalSecurityManager) {
+ this.originalSecurityManager= originalSecurityManager;
+ }
+
+ @Override
+ public void checkExit(int status) {
+ throw new ExitException(status);
+ }
+
+ @Override
+ public boolean getInCheck() {
+ return (originalSecurityManager != null) && originalSecurityManager.getInCheck();
+ }
+
+ @Override
+ public Object getSecurityContext() {
+ return (originalSecurityManager == null) ? super.getSecurityContext() : originalSecurityManager.getSecurityContext();
+ }
+
@Override
public void checkPermission(Permission perm) {
- // allow anything.
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPermission(perm);
}
@Override
public void checkPermission(Permission perm, Object context) {
- // allow anything.
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPermission(perm, context);
}
@Override
- public void checkExit(int status) {
- super.checkExit(status);
- throw new ExitException(status);
+ public void checkCreateClassLoader() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkCreateClassLoader();
+ }
+
+ @Override
+ public void checkAccess(Thread t) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAccess(t);
+ }
+
+ @Override
+ public void checkAccess(ThreadGroup g) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAccess(g);
+ }
+
+ @Override
+ public void checkExec(String cmd) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkExec(cmd);
+ }
+
+ @Override
+ public void checkLink(String lib) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkLink(lib);
+ }
+
+ @Override
+ public void checkRead(FileDescriptor fd) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkRead(fd);
+ }
+
+ @Override
+ public void checkRead(String file) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkRead(file);
+ }
+
+ @Override
+ public void checkRead(String file, Object context) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkRead(file, context);
+ }
+
+ @Override
+ public void checkWrite(FileDescriptor fd) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkWrite(fd);
+ }
+
+ @Override
+ public void checkWrite(String file) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkWrite(file);
+ }
+
+ @Override
+ public void checkDelete(String file) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkDelete(file);
+ }
+
+ @Override
+ public void checkConnect(String host, int port) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkConnect(host, port);
}
- }
+ @Override
+ public void checkConnect(String host, int port, Object context) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkConnect(host, port, context);
+ }
+
+ @Override
+ public void checkListen(int port) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkListen(port);
+ }
+
+ @Override
+ public void checkAccept(String host, int port) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAccept(host, port);
+ }
+
+ @Override
+ public void checkMulticast(InetAddress maddr) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkMulticast(maddr);
+ }
+
+ @Override
+ public void checkMulticast(InetAddress maddr, byte ttl) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkMulticast(maddr, ttl);
+ }
+
+ @Override
+ public void checkPropertiesAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPropertiesAccess();
+ }
+
+ @Override
+ public void checkPropertyAccess(String key) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPropertyAccess(key);
+ }
+
+ @Override
+ public boolean checkTopLevelWindow(Object window) {
+ return (originalSecurityManager == null) ? super.checkTopLevelWindow(window) : originalSecurityManager.checkTopLevelWindow(window);
+ }
+
+ @Override
+ public void checkPrintJobAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPrintJobAccess();
+ }
+
+ @Override
+ public void checkSystemClipboardAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkSystemClipboardAccess();
+ }
+
+ @Override
+ public void checkAwtEventQueueAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAwtEventQueueAccess();
+ }
+
+ @Override
+ public void checkPackageAccess(String pkg) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPackageAccess(pkg);
+ }
+
+ @Override
+ public void checkPackageDefinition(String pkg) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPackageDefinition(pkg);
+ }
+
+ @Override
+ public void checkSetFactory() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkSetFactory();
+ }
+
+ @Override
+ public void checkMemberAccess(Class<?> clazz, int which) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkMemberAccess(clazz, which);
+ }
+
+ @Override
+ public void checkSecurityAccess(String target) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkSecurityAccess(target);
+ }
+
+ @Override
+ public ThreadGroup getThreadGroup() {
+ return (originalSecurityManager == null) ? super.getThreadGroup() : originalSecurityManager.getThreadGroup();
+ }
+ }
+
/**
* Execute runnable.run(), preventing System.exit(). If System.exit() is called
* in runnable.run(), the value is returned. If System.exit()
@@ -48,9 +243,9 @@ public void checkExit(int status) {
* @return null if System.exit() is not called, Integer.valueof(status) if not
*/
public Integer runWithCheckForSystemExit(Runnable runnable) {
- SecurityManager oldSecurityManager = System.getSecurityManager();
- System.setSecurityManager(new NoExitSecurityManager());
- PrintStream oldPrintStream = System.out;
+ SecurityManager oldSecurityManager= System.getSecurityManager();
+ System.setSecurityManager(new NoExitSecurityManager(oldSecurityManager));
+ PrintStream oldPrintStream= System.out;
System.setOut(new PrintStream(new ByteArrayOutputStream()));
try {
@@ -65,5 +260,4 @@ public Integer runWithCheckForSystemExit(Runnable runnable) {
System.setOut(oldPrintStream);
}
}
-
}

0 comments on commit 4596630

Please sign in to comment.
Something went wrong with that request. Please try again.