Skip to content

Commit

Permalink
Make SitarExecutor handle security manager
Browse files Browse the repository at this point in the history
  • Loading branch information
gabegorelick committed May 3, 2011
1 parent 9989ce7 commit 0d970bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 53 deletions.
40 changes: 40 additions & 0 deletions src/edu/umd/cs/guitar/ripper/SitarExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

import java.io.InputStream;
import java.net.URL;
import java.security.Permission;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import edu.umd.cs.guitar.model.IO;
import edu.umd.cs.guitar.model.SitarApplication;
import edu.umd.cs.guitar.model.SitarApplication.ExitException;
import edu.umd.cs.guitar.model.data.Configuration;
import edu.umd.cs.guitar.util.DefaultFactory;
import edu.umd.cs.guitar.util.GUITARLog;
Expand Down Expand Up @@ -54,6 +56,8 @@ public abstract class SitarExecutor {
private final Configuration xmlConfig;

private long startTime;

private final SecurityManager oldManager;

/**
* Constructs a new <code>SitarExecutor</code>. This constructor is
Expand Down Expand Up @@ -97,6 +101,7 @@ protected SitarExecutor(SitarConfiguration config, Thread guiThread) {
this.config = config;
this.application = initSWTApplication(config, guiThread);
this.xmlConfig = loadXmlConfig();
oldManager = System.getSecurityManager();

System.setProperty(GUITARLog.LOGFILE_NAME_SYSTEM_PROPERTY, config.getLogFile());
}
Expand Down Expand Up @@ -198,6 +203,7 @@ protected Configuration getXmlConfig() {
*/
protected void onBeforeExecute() {
beginTiming();
disableExit();
}

/**
Expand Down Expand Up @@ -226,6 +232,7 @@ public final void execute() {
*/
protected void onAfterExecute() {
endTiming();
enableExit();
}

/**
Expand All @@ -245,5 +252,38 @@ private void endTiming() {
df.setTimeZone(TimeZone.getTimeZone("GMT"));
GUITARLog.log.info("Time Elapsed: " + df.format(nDuration));
}

/**
* Disable attempts by the application under test to exit the JVM. Clients
* that use this method should call {@link #enableExit()} to re-enable exiting
* the JVM, otherwise the JVM may never terminate.
*
* @see ExitException
*/
protected void disableExit() {
SecurityManager manager = 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 ExitException();
}
};
System.setSecurityManager(manager);
}

protected void enableExit() {
// re-enable exiting the JVM
System.setSecurityManager(oldManager);
}

}
65 changes: 12 additions & 53 deletions src/edu/umd/cs/guitar/ripper/SitarMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
*/
package edu.umd.cs.guitar.ripper;

import java.security.Permission;

import edu.umd.cs.guitar.model.SitarApplication;
import edu.umd.cs.guitar.model.SitarApplication.ExitException;
import edu.umd.cs.guitar.util.GUITARLog;

/**
Expand All @@ -33,52 +32,10 @@
public class SitarMonitor {

private final SitarApplication application;
private final SecurityManager oldManager;


public SitarMonitor(SitarConfiguration config, SitarApplication app) {
this.application = app;
oldManager = System.getSecurityManager();
}

public void cleanUp() {
application.getDisplay().syncExec(new Runnable() {
@Override
public void run() {
application.getDisplay().dispose();
}
});
GUITARLog.log.info("Display disposed");

// re-enable exiting the JVM
System.setSecurityManager(oldManager);
}

/**
* Disable attempts by the application under test to exit the JVM. Clients
* that use this method should call {@link #cleanUp()} to re-enable exiting
* the JVM, otherwise the JVM may never terminate.
*
* @see ExitException
*/
public void disableExit() {
SecurityManager manager = 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 ExitException();
}
};
System.setSecurityManager(manager);

// block uncaught exceptions so we can finish up
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
Expand All @@ -89,18 +46,20 @@ public void uncaughtException(Thread t, Throwable e) {
GUITARLog.log.error("Uncaught exception", e);
}

// dispose GUI and restore old SecurityManager so we can exit
// dispose GUI
cleanUp();
}
});
}

/**
* Thrown to indicate the application under test has attempted to exit the
* JVM, e.g. with a call to {@link System#exit(int)}.
*/
protected static class ExitException extends SecurityException {
private static final long serialVersionUID = 1L;

public void cleanUp() {
application.getDisplay().syncExec(new Runnable() {
@Override
public void run() {
application.getDisplay().dispose();
}
});
GUITARLog.log.info("Display disposed");
}

}

0 comments on commit 0d970bb

Please sign in to comment.