update python & headless tests to manage GhidraScriptUtil resource

This commit is contained in:
Jason P. Leasure 2020-04-29 11:39:05 -04:00
parent 7ee2467016
commit 5f11c9cf7d
6 changed files with 117 additions and 93 deletions

View file

@ -49,9 +49,15 @@ public class GhidraScriptRunner implements GhidraLaunchable {
System.exit(0); System.exit(0);
} }
String logFile = null; //TODO get from arguments? String logFile = null; //TODO get from arguments?
GhidraScriptUtil.initialize(new BundleHost(), scriptPaths);
try {
initialize(layout, logFile, true); initialize(layout, logFile, true);
runScript(args[0]); runScript(args[0]);
} }
finally {
GhidraScriptUtil.dispose();
}
}
private void runScript(String string) throws Exception { private void runScript(String string) throws Exception {
GhidraScript ghidraScript = getGhidraScript(string); GhidraScript ghidraScript = getGhidraScript(string);
@ -188,8 +194,6 @@ public class GhidraScriptRunner implements GhidraLaunchable {
* Gather paths where scripts may be found. * Gather paths where scripts may be found.
*/ */
private void initializeScriptPaths() { private void initializeScriptPaths() {
GhidraScriptUtil.initialize(new BundleHost(), scriptPaths);
StringBuffer buf = new StringBuffer("HEADLESS Script Paths:"); StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");
for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) { for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {
buf.append("\n "); buf.append("\n ");

View file

@ -293,6 +293,8 @@ public class HeadlessAnalyzer {
} }
} }
GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
try {
initializeScriptPaths(); initializeScriptPaths();
compileScripts(); compileScripts();
@ -345,6 +347,10 @@ public class HeadlessAnalyzer {
} }
} }
} }
finally {
GhidraScriptUtil.dispose();
}
}
/** /**
* Process the optional import file/directory list and process each imported file: * Process the optional import file/directory list and process each imported file:
@ -394,6 +400,8 @@ public class HeadlessAnalyzer {
} }
} }
GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
try {
initializeScriptPaths(); initializeScriptPaths();
compileScripts(); compileScripts();
@ -417,8 +425,8 @@ public class HeadlessAnalyzer {
options.deleteProject = true; options.deleteProject = true;
} }
Msg.info(this, Msg.info(this, "Creating " + (options.deleteProject ? "temporary " : "") +
"Creating " + (options.deleteProject ? "temporary " : "") + "project: " + locator); "project: " + locator);
project = getProjectManager().createProject(locator, null, false); project = getProjectManager().createProject(locator, null, false);
} }
@ -443,6 +451,10 @@ public class HeadlessAnalyzer {
} }
} }
} }
finally {
GhidraScriptUtil.dispose();
}
}
/** /**
* Checks to see if the most recent analysis timed out. * Checks to see if the most recent analysis timed out.
@ -660,8 +672,6 @@ public class HeadlessAnalyzer {
* Gather paths where scripts may be found. * Gather paths where scripts may be found.
*/ */
private void initializeScriptPaths() { private void initializeScriptPaths() {
GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
StringBuffer buf = new StringBuffer("HEADLESS Script Paths:"); StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");
for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) { for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {
buf.append("\n "); buf.append("\n ");

View file

@ -87,8 +87,6 @@ public abstract class AbstractGhidraScriptMgrPluginTest
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
System.err.printf("===== Starting %s ======\n", testName.getMethodName());
setErrorGUIEnabled(false); setErrorGUIEnabled(false);
// change the eclipse port so that Eclipse doesn't try to edit the script when // change the eclipse port so that Eclipse doesn't try to edit the script when

View file

@ -22,6 +22,8 @@ import java.io.ByteArrayOutputStream;
import org.junit.*; import org.junit.*;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
import ghidra.app.plugin.core.osgi.BundleHost;
import ghidra.app.script.GhidraScriptUtil;
import ghidra.test.AbstractGhidraHeadedIntegrationTest; import ghidra.test.AbstractGhidraHeadedIntegrationTest;
/** /**
@ -35,6 +37,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
out = new ByteArrayOutputStream(); out = new ByteArrayOutputStream();
GhidraScriptUtil.initialize(new BundleHost(), null);
interpreter = GhidraPythonInterpreter.get(); interpreter = GhidraPythonInterpreter.get();
interpreter.setOut(out); interpreter.setOut(out);
interpreter.setErr(out); interpreter.setErr(out);
@ -44,6 +47,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
public void tearDown() throws Exception { public void tearDown() throws Exception {
out.reset(); out.reset();
interpreter.cleanup(); interpreter.cleanup();
GhidraScriptUtil.dispose();
} }
/** /**

View file

@ -15,10 +15,12 @@
*/ */
package ghidra.python; package ghidra.python;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.*;
import org.junit.*; import org.junit.*;
import ghidra.app.plugin.core.osgi.BundleHost;
import ghidra.app.script.GhidraScriptUtil;
import ghidra.framework.plugintool.PluginTool; import ghidra.framework.plugintool.PluginTool;
import ghidra.test.AbstractGhidraHeadedIntegrationTest; import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.test.TestEnv; import ghidra.test.TestEnv;
@ -36,12 +38,14 @@ public class PythonPluginTest extends AbstractGhidraHeadedIntegrationTest {
public void setUp() throws Exception { public void setUp() throws Exception {
env = new TestEnv(); env = new TestEnv();
tool = env.getTool(); tool = env.getTool();
GhidraScriptUtil.initialize(new BundleHost(), null);
tool.addPlugin(PythonPlugin.class.getName()); tool.addPlugin(PythonPlugin.class.getName());
plugin = env.getPlugin(PythonPlugin.class); plugin = env.getPlugin(PythonPlugin.class);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
GhidraScriptUtil.dispose();
env.dispose(); env.dispose();
} }

View file

@ -24,6 +24,8 @@ import org.junit.*;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
import ghidra.app.plugin.core.console.ConsolePlugin; import ghidra.app.plugin.core.console.ConsolePlugin;
import ghidra.app.plugin.core.osgi.BundleHost;
import ghidra.app.script.GhidraScriptUtil;
import ghidra.app.script.GhidraState; import ghidra.app.script.GhidraState;
import ghidra.app.services.ConsoleService; import ghidra.app.services.ConsoleService;
import ghidra.framework.Application; import ghidra.framework.Application;
@ -45,12 +47,14 @@ public class PythonScriptTest extends AbstractGhidraHeadedIntegrationTest {
public void setUp() throws Exception { public void setUp() throws Exception {
env = new TestEnv(); env = new TestEnv();
tool = env.getTool(); tool = env.getTool();
GhidraScriptUtil.initialize(new BundleHost(), null);
tool.addPlugin(ConsolePlugin.class.getName()); tool.addPlugin(ConsolePlugin.class.getName());
console = tool.getService(ConsoleService.class); console = tool.getService(ConsoleService.class);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
GhidraScriptUtil.dispose();
env.dispose(); env.dispose();
} }