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,8 +49,14 @@ 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?
initialize(layout, logFile, true); GhidraScriptUtil.initialize(new BundleHost(), scriptPaths);
runScript(args[0]); try {
initialize(layout, logFile, true);
runScript(args[0]);
}
finally {
GhidraScriptUtil.dispose();
}
} }
private void runScript(String string) throws Exception { private void runScript(String string) throws Exception {
@ -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,56 +293,62 @@ public class HeadlessAnalyzer {
} }
} }
initializeScriptPaths(); GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
compileScripts();
Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
GhidraURLConnection c = (GhidraURLConnection) ghidraURL.openConnection();
c.setReadOnly(options.readOnly); // writable repository connection
if (c.getRepositoryName() == null) {
throw new MalformedURLException("Unsupported repository URL: " + ghidraURL);
}
Msg.info(this, "Opening ghidra repository project: " + ghidraURL);
Object obj = c.getContent();
if (!(obj instanceof GhidraURLWrappedContent)) {
throw new IOException(
"Connect to repository folder failed. Response code: " + c.getResponseCode());
}
GhidraURLWrappedContent wrappedContent = (GhidraURLWrappedContent) obj;
Object content = null;
try { try {
content = wrappedContent.getContent(this); initializeScriptPaths();
if (!(content instanceof DomainFolder)) { compileScripts();
Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
GhidraURLConnection c = (GhidraURLConnection) ghidraURL.openConnection();
c.setReadOnly(options.readOnly); // writable repository connection
if (c.getRepositoryName() == null) {
throw new MalformedURLException("Unsupported repository URL: " + ghidraURL);
}
Msg.info(this, "Opening ghidra repository project: " + ghidraURL);
Object obj = c.getContent();
if (!(obj instanceof GhidraURLWrappedContent)) {
throw new IOException(
"Connect to repository folder failed. Response code: " + c.getResponseCode());
}
GhidraURLWrappedContent wrappedContent = (GhidraURLWrappedContent) obj;
Object content = null;
try {
content = wrappedContent.getContent(this);
if (!(content instanceof DomainFolder)) {
throw new IOException("Connect to repository folder failed");
}
DomainFolder folder = (DomainFolder) content;
project = new HeadlessProject(getProjectManager(), c);
if (!checkUpdateOptions()) {
return; // TODO: Should an exception be thrown?
}
if (options.runScriptsNoImport) {
processNoImport(folder.getPathname());
}
else {
processWithImport(folder.getPathname(), filesToImport);
}
}
catch (NotFoundException e) {
throw new IOException("Connect to repository folder failed"); throw new IOException("Connect to repository folder failed");
} }
finally {
DomainFolder folder = (DomainFolder) content; if (content != null) {
project = new HeadlessProject(getProjectManager(), c); wrappedContent.release(content, this);
}
if (!checkUpdateOptions()) { if (project != null) {
return; // TODO: Should an exception be thrown? project.close();
}
} }
if (options.runScriptsNoImport) {
processNoImport(folder.getPathname());
}
else {
processWithImport(folder.getPathname(), filesToImport);
}
}
catch (NotFoundException e) {
throw new IOException("Connect to repository folder failed");
} }
finally { finally {
if (content != null) { GhidraScriptUtil.dispose();
wrappedContent.release(content, this);
}
if (project != null) {
project.close();
}
} }
} }
@ -394,53 +400,59 @@ public class HeadlessAnalyzer {
} }
} }
initializeScriptPaths(); GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
compileScripts();
Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
File dir = new File(projectLocation);
ProjectLocator locator = new ProjectLocator(dir.getAbsolutePath(), projectName);
if (locator.getProjectDir().exists()) {
project = openProject(locator);
}
else {
if (options.runScriptsNoImport) {
Msg.error(this, "Could not find project: " + locator +
" -- should already exist in -process mode.");
throw new IOException("Could not find project: " + locator);
}
if (!options.runScriptsNoImport && options.readOnly) {
// assume temporary when importing with readOnly option
options.deleteProject = true;
}
Msg.info(this,
"Creating " + (options.deleteProject ? "temporary " : "") + "project: " + locator);
project = getProjectManager().createProject(locator, null, false);
}
try { try {
initializeScriptPaths();
compileScripts();
if (!checkUpdateOptions()) { Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
return; // TODO: Should an exception be thrown?
}
if (options.runScriptsNoImport) { File dir = new File(projectLocation);
processNoImport(rootFolderPath); ProjectLocator locator = new ProjectLocator(dir.getAbsolutePath(), projectName);
if (locator.getProjectDir().exists()) {
project = openProject(locator);
} }
else { else {
processWithImport(rootFolderPath, filesToImport); if (options.runScriptsNoImport) {
Msg.error(this, "Could not find project: " + locator +
" -- should already exist in -process mode.");
throw new IOException("Could not find project: " + locator);
}
if (!options.runScriptsNoImport && options.readOnly) {
// assume temporary when importing with readOnly option
options.deleteProject = true;
}
Msg.info(this, "Creating " + (options.deleteProject ? "temporary " : "") +
"project: " + locator);
project = getProjectManager().createProject(locator, null, false);
}
try {
if (!checkUpdateOptions()) {
return; // TODO: Should an exception be thrown?
}
if (options.runScriptsNoImport) {
processNoImport(rootFolderPath);
}
else {
processWithImport(rootFolderPath, filesToImport);
}
}
finally {
project.close();
if (!options.runScriptsNoImport && options.deleteProject) {
FileUtilities.deleteDir(locator.getProjectDir());
locator.getMarkerFile().delete();
}
} }
} }
finally { finally {
project.close(); GhidraScriptUtil.dispose();
if (!options.runScriptsNoImport && options.deleteProject) {
FileUtilities.deleteDir(locator.getProjectDir());
locator.getMarkerFile().delete();
}
} }
} }
@ -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();
} }