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

View file

@ -293,56 +293,62 @@ public class HeadlessAnalyzer {
}
}
initializeScriptPaths();
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;
GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
try {
content = wrappedContent.getContent(this);
if (!(content instanceof DomainFolder)) {
initializeScriptPaths();
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");
}
DomainFolder folder = (DomainFolder) content;
project = new HeadlessProject(getProjectManager(), c);
if (!checkUpdateOptions()) {
return; // TODO: Should an exception be thrown?
finally {
if (content != null) {
wrappedContent.release(content, this);
}
if (project != null) {
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 {
if (content != null) {
wrappedContent.release(content, this);
}
if (project != null) {
project.close();
}
GhidraScriptUtil.dispose();
}
}
@ -394,53 +400,59 @@ public class HeadlessAnalyzer {
}
}
initializeScriptPaths();
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);
}
GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
try {
initializeScriptPaths();
compileScripts();
if (!checkUpdateOptions()) {
return; // TODO: Should an exception be thrown?
}
Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
if (options.runScriptsNoImport) {
processNoImport(rootFolderPath);
File dir = new File(projectLocation);
ProjectLocator locator = new ProjectLocator(dir.getAbsolutePath(), projectName);
if (locator.getProjectDir().exists()) {
project = openProject(locator);
}
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 {
project.close();
if (!options.runScriptsNoImport && options.deleteProject) {
FileUtilities.deleteDir(locator.getProjectDir());
locator.getMarkerFile().delete();
}
GhidraScriptUtil.dispose();
}
}
@ -660,8 +672,6 @@ public class HeadlessAnalyzer {
* Gather paths where scripts may be found.
*/
private void initializeScriptPaths() {
GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");
for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {
buf.append("\n ");

View file

@ -87,8 +87,6 @@ public abstract class AbstractGhidraScriptMgrPluginTest
@Before
public void setUp() throws Exception {
System.err.printf("===== Starting %s ======\n", testName.getMethodName());
setErrorGUIEnabled(false);
// 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 generic.jar.ResourceFile;
import ghidra.app.plugin.core.osgi.BundleHost;
import ghidra.app.script.GhidraScriptUtil;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
/**
@ -35,6 +37,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
@Before
public void setUp() throws Exception {
out = new ByteArrayOutputStream();
GhidraScriptUtil.initialize(new BundleHost(), null);
interpreter = GhidraPythonInterpreter.get();
interpreter.setOut(out);
interpreter.setErr(out);
@ -44,6 +47,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
public void tearDown() throws Exception {
out.reset();
interpreter.cleanup();
GhidraScriptUtil.dispose();
}
/**

View file

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

View file

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