update tests

- note: timeout increased by 33% in waitForTaskEnd because the entire
  script directory needs to compiled now
- complete recursive wipe of binary artifacts added to @After action
This commit is contained in:
Jason P. Leasure 2020-03-25 14:29:49 -04:00
parent 931a9e8230
commit 092d716e96
5 changed files with 60 additions and 55 deletions

View file

@ -35,6 +35,7 @@ import ghidra.app.events.OpenProgramPluginEvent;
import ghidra.app.plugin.core.analysis.AutoAnalysisManager; import ghidra.app.plugin.core.analysis.AutoAnalysisManager;
import ghidra.app.plugin.core.progmgr.ProgramManagerPlugin; import ghidra.app.plugin.core.progmgr.ProgramManagerPlugin;
import ghidra.app.plugin.core.script.GhidraScriptMgrPlugin; import ghidra.app.plugin.core.script.GhidraScriptMgrPlugin;
import ghidra.app.script.GhidraScript;
import ghidra.app.script.JavaScriptProvider; import ghidra.app.script.JavaScriptProvider;
import ghidra.app.services.ProgramManager; import ghidra.app.services.ProgramManager;
import ghidra.base.project.GhidraProject; import ghidra.base.project.GhidraProject;
@ -550,15 +551,18 @@ public class TestEnv {
} }
public ScriptTaskListener runScript(File script) throws PluginException { public ScriptTaskListener runScript(File script) throws PluginException {
JavaScriptProvider scriptProvider = new JavaScriptProvider(); JavaScriptProvider scriptProvider = new JavaScriptProvider();
PrintWriter writer = new PrintWriter(System.out); PrintWriter writer = new PrintWriter(System.out);
ResourceFile resourceFile = new ResourceFile(script); ResourceFile resourceFile = new ResourceFile(script);
Boolean result = (Boolean) AbstractGenericTest.invokeInstanceMethod("compile", GhidraScript scr=null;
scriptProvider, new Class<?>[] { ResourceFile.class, PrintWriter.class }, try {
new Object[] { resourceFile, writer }); scr=scriptProvider.getScriptInstance(resourceFile, writer);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
if (!result) { }
if (scr==null) {
writer.flush(); writer.flush();
throw new RuntimeException("Failed to compile script " + script.getAbsolutePath()); throw new RuntimeException("Failed to compile script " + script.getAbsolutePath());
} }
@ -881,7 +885,7 @@ public class TestEnv {
Project project = frontEndToolInstance.getProject(); Project project = frontEndToolInstance.getProject();
ToolServices toolServices = project.getToolServices(); ToolServices toolServices = project.getToolServices();
PluginTool newTool = (PluginTool) toolServices.launchTool(toolName, null); PluginTool newTool = toolServices.launchTool(toolName, null);
if (newTool == null) { if (newTool == null) {
// couldn't find the tool in the workspace...check the test area // couldn't find the tool in the workspace...check the test area
newTool = launchDefaultToolByName(toolName); newTool = launchDefaultToolByName(toolName);

View file

@ -21,6 +21,7 @@ import java.awt.Window;
import java.io.*; import java.io.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -38,15 +39,15 @@ import org.junit.*;
import docking.ActionContext; import docking.ActionContext;
import docking.action.DockingActionIf; import docking.action.DockingActionIf;
import docking.widgets.OptionDialog; import docking.widgets.OptionDialog;
import docking.widgets.bundlemanager.BundlePath;
import docking.widgets.bundlemanager.BundlePathManager;
import docking.widgets.filter.FilterTextField; import docking.widgets.filter.FilterTextField;
import docking.widgets.pathmanager.PathManager;
import docking.widgets.table.GDynamicColumnTableModel; import docking.widgets.table.GDynamicColumnTableModel;
import docking.widgets.table.RowObjectTableModel; import docking.widgets.table.RowObjectTableModel;
import docking.widgets.tree.GTree; import docking.widgets.tree.GTree;
import docking.widgets.tree.GTreeNode; import docking.widgets.tree.GTreeNode;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
import generic.test.TestUtils; import generic.test.TestUtils;
import generic.util.Path;
import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin; import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;
import ghidra.app.plugin.core.console.ConsoleComponentProvider; import ghidra.app.plugin.core.console.ConsoleComponentProvider;
import ghidra.app.script.*; import ghidra.app.script.*;
@ -65,7 +66,6 @@ import ghidra.util.table.GhidraTable;
import ghidra.util.table.GhidraTableFilterPanel; import ghidra.util.table.GhidraTableFilterPanel;
import ghidra.util.task.*; import ghidra.util.task.*;
import util.CollectionUtils; import util.CollectionUtils;
import utilities.util.FileUtilities;
public abstract class AbstractGhidraScriptMgrPluginTest public abstract class AbstractGhidraScriptMgrPluginTest
extends AbstractGhidraHeadedIntegrationTest { extends AbstractGhidraHeadedIntegrationTest {
@ -125,7 +125,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest
assertNotNull(scriptTable); assertNotNull(scriptTable);
// this clears out the static map that accumulates values between tests // this clears out the static map that accumulates values between tests
GhidraScriptUtil.clean(); GhidraScriptUtil.clearMetadata();
runSwing(() -> provider.refresh()); runSwing(() -> provider.refresh());
cleanupOldTestFiles(); cleanupOldTestFiles();
@ -171,13 +171,19 @@ public abstract class AbstractGhidraScriptMgrPluginTest
env.dispose(); env.dispose();
} }
static protected void wipe(Path path) throws IOException {
if (Files.exists(path)) {
for (Path p : (Iterable<Path>) Files.walk(path).sorted(
Comparator.reverseOrder())::iterator) {
Files.deleteIfExists(p);
}
}
}
protected void wipeUserScripts() throws IOException { protected void wipeUserScripts() throws IOException {
java.nio.file.Path userScriptDir = Path userScriptDir = java.nio.file.Paths.get(GhidraScriptUtil.USER_SCRIPTS_DIR);
java.nio.file.Paths.get(GhidraScriptUtil.USER_SCRIPTS_DIR); for (Path p : (Iterable<Path>) Files.list(userScriptDir)::iterator) {
Iterator<java.nio.file.Path> it = Files.list(userScriptDir).iterator(); wipe(p);
while (it.hasNext()) {
Files.walk(it.next()).sorted(Comparator.reverseOrder()).map(
java.nio.file.Path::toFile).forEach(File::delete);
} }
} }
@ -220,13 +226,13 @@ public abstract class AbstractGhidraScriptMgrPluginTest
} }
protected void assertScriptManagerKnowsAbout(ResourceFile script) { protected void assertScriptManagerKnowsAbout(ResourceFile script) {
assertTrue(GhidraScriptUtil.contains(script)); assertTrue(GhidraScriptUtil.containsMetadata(script));
assertNull(provider.getActionManager().get(script)); assertNull(provider.getActionManager().get(script));
} }
protected void assertScriptManagerForgotAbout(ResourceFile script) { protected void assertScriptManagerForgotAbout(ResourceFile script) {
assertFalse(GhidraScriptUtil.contains(script)); assertFalse(GhidraScriptUtil.containsMetadata(script));
assertNull(provider.getActionManager().get(script)); assertNull(provider.getActionManager().get(script));
assertNull(provider.getEditorMap().get(script)); assertNull(provider.getEditorMap().get(script));
} }
@ -965,25 +971,17 @@ public abstract class AbstractGhidraScriptMgrPluginTest
} }
protected void addScriptPath(final File file) { protected void cleanupOldTestFiles() throws IOException {
final PathManager pathManager = (PathManager) getInstanceField("pathManager", provider); // remove the compiled bundles directory so that any scripts we use will be recompiled
runSwing(() -> pathManager.addPath(new ResourceFile(file), true)); wipe(GhidraScriptUtil.getCompiledBundlesDir());
}
protected void cleanupOldTestFiles() {
// remove the 'bin' directory so that any scripts we use will be recompiled
List<ResourceFile> dirs = GhidraScriptUtil.getScriptBinDirectories();
for (ResourceFile file : dirs) {
FileUtilities.deleteDir(file.getFile(false));
file.mkdir();// recreate the file or the compiler will complain
}
String myTestName = super.testName.getMethodName(); String myTestName = super.testName.getMethodName();
// destroy any NewScriptxxx files...and Temp ones too // destroy any NewScriptxxx files...and Temp ones too
PathManager pathManager = (PathManager) TestUtils.getInstanceField("pathManager", provider); BundlePathManager pathManager =
List<Path> paths = pathManager.getPaths(); (BundlePathManager) TestUtils.getInstanceField("bundlePathManager", provider);
for (Path path : paths) { List<BundlePath> paths = pathManager.getPaths();
for (BundlePath path : paths) {
File file = path.getPath().getFile(false); File file = path.getPath().getFile(false);
File[] listFiles = file.listFiles(); File[] listFiles = file.listFiles();
if (listFiles == null) { if (listFiles == null) {
@ -1034,7 +1032,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest
waitForSwing(); waitForSwing();
int waitCount = 0; int waitCount = 0;
while (!flag.ended && waitCount < 201) { while (!flag.ended && waitCount < 301) {
try { try {
Thread.sleep(DEFAULT_WAIT_DELAY); Thread.sleep(DEFAULT_WAIT_DELAY);
} }

View file

@ -150,9 +150,12 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
// remove all class files from the user script bin dir // remove all class files from the user script bin dir
File userScriptsBinDir = File userScriptsBinDir =
SourceBundleInfo.getBindirFromScriptFile(new ResourceFile(newScriptFile)).toFile(); SourceBundleInfo.getBindirFromScriptFile(new ResourceFile(newScriptFile)).toFile();
File[] userScriptBinDirFiles = userScriptsBinDir.listFiles(classFileFilter); File[] userScriptBinDirFiles;
for (File file : userScriptBinDirFiles) { if (userScriptsBinDir.exists()) {
file.delete(); userScriptBinDirFiles = userScriptsBinDir.listFiles(classFileFilter);
for (File file : userScriptBinDirFiles) {
file.delete();
}
} }
userScriptBinDirFiles = userScriptsDir.listFiles(classFileFilter); userScriptBinDirFiles = userScriptsDir.listFiles(classFileFilter);
isEmpty = userScriptBinDirFiles == null || userScriptBinDirFiles.length == 0; isEmpty = userScriptBinDirFiles == null || userScriptBinDirFiles.length == 0;
@ -213,7 +216,7 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
FileUtilities.deleteDir(tempScriptDir); FileUtilities.deleteDir(tempScriptDir);
tempScriptDir.mkdir(); tempScriptDir.mkdir();
addScriptPath(tempScriptDir); provider.enableScriptDirectory(new ResourceFile(tempScriptDir));
// create a script file in that directory // create a script file in that directory
String rawScriptName = testName.getMethodName(); String rawScriptName = testName.getMethodName();

View file

@ -29,11 +29,11 @@ import org.junit.Test;
import docking.KeyEntryTextField; import docking.KeyEntryTextField;
import docking.action.DockingActionIf; import docking.action.DockingActionIf;
import docking.widgets.bundlemanager.BundlePath;
import docking.widgets.bundlemanager.BundlePathManager;
import docking.widgets.filter.FilterTextField; import docking.widgets.filter.FilterTextField;
import docking.widgets.list.ListPanel; import docking.widgets.list.ListPanel;
import docking.widgets.pathmanager.PathManager;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
import generic.util.Path;
import ghidra.app.script.GhidraScriptUtil; import ghidra.app.script.GhidraScriptUtil;
import ghidra.app.script.JavaScriptProvider; import ghidra.app.script.JavaScriptProvider;
import ghidra.util.StringUtilities; import ghidra.util.StringUtilities;
@ -284,15 +284,15 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
performAction(pathAction, false); performAction(pathAction, false);
waitForSwing(); waitForSwing();
PickPathsDialog pathsDialog = waitForDialogComponent(PickPathsDialog.class); BundlePathSelectionDialog pathsDialog = waitForDialogComponent(BundlePathSelectionDialog.class);
final File dir = new File(getTestDirectoryPath() + "/test_scripts"); final File dir = new File(getTestDirectoryPath() + "/test_scripts");
dir.mkdirs(); dir.mkdirs();
final PathManager pathManager = pathsDialog.getPathManager(); final BundlePathManager pathManager = pathsDialog.getBundleManager();
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
List<Path> paths = pathManager.getPaths(); List<BundlePath> paths = pathManager.getPaths();
paths.add(0, new Path(dir)); paths.add(0, new BundlePath(dir));
pathManager.setPaths(paths); pathManager.setPaths(paths);
}); });
waitForSwing(); waitForSwing();

View file

@ -24,11 +24,11 @@ import org.junit.Test;
import docking.ComponentProvider; import docking.ComponentProvider;
import docking.DockingWindowManager; import docking.DockingWindowManager;
import docking.widgets.pathmanager.PathManager; import docking.widgets.bundlemanager.BundlePath;
import docking.widgets.bundlemanager.BundlePathManager;
import docking.widgets.tree.GTree; import docking.widgets.tree.GTree;
import docking.widgets.tree.GTreeNode; import docking.widgets.tree.GTreeNode;
import generic.jar.ResourceFile; import generic.jar.ResourceFile;
import generic.util.Path;
import ghidra.app.plugin.core.console.ConsoleComponentProvider; import ghidra.app.plugin.core.console.ConsoleComponentProvider;
import ghidra.app.plugin.core.script.*; import ghidra.app.plugin.core.script.*;
import ghidra.app.script.GhidraScriptUtil; import ghidra.app.script.GhidraScriptUtil;
@ -112,18 +112,18 @@ public class GhidraScriptMgrPluginScreenShots extends GhidraScreenShotGenerator
@Test @Test
public void testScript_Dirs() throws Exception { public void testScript_Dirs() throws Exception {
List<Path> paths = new ArrayList<>(); List<BundlePath> paths = new ArrayList<>();
paths.add(new Path("$USER_HOME/ghidra_scripts")); paths.add(new BundlePath("$USER_HOME/ghidra_scripts"));
paths.add(new Path("$GHIDRA_HOME/Features/Base/ghidra_scripts")); paths.add(new BundlePath("$GHIDRA_HOME/Features/Base/ghidra_scripts"));
paths.add(new Path("/User/defined/invalid/directory")); paths.add(new BundlePath("/User/defined/invalid/directory"));
ComponentProvider provider = showProvider(GhidraScriptComponentProvider.class); ComponentProvider provider = showProvider(GhidraScriptComponentProvider.class);
PathManager pathManager = (PathManager) getInstanceField("pathManager", provider); BundlePathManager bundleManager = (BundlePathManager) getInstanceField("bundlePathManager", provider);
pathManager.setPaths(paths); bundleManager.setPaths(paths);
final PickPathsDialog pathsDialog = new PickPathsDialog(null, pathManager); final BundlePathSelectionDialog pathsDialog = new BundlePathSelectionDialog(null, bundleManager);
runSwing(() -> DockingWindowManager.showDialog(null, pathsDialog), false); runSwing(() -> DockingWindowManager.showDialog(null, pathsDialog), false);
PickPathsDialog dialog = waitForDialogComponent(PickPathsDialog.class); BundlePathSelectionDialog dialog = waitForDialogComponent(BundlePathSelectionDialog.class);
captureDialog(dialog); captureDialog(dialog);
} }