Merge remote-tracking branch 'origin/GP-355_jpleasu_cleanup_script_test_timing' into patch

This commit is contained in:
ghidravore 2020-12-02 15:32:52 -05:00
commit 52d5006dc4
4 changed files with 48 additions and 56 deletions

View file

@ -67,8 +67,10 @@ import utilities.util.FileUtilities;
public abstract class AbstractGhidraScriptMgrPluginTest public abstract class AbstractGhidraScriptMgrPluginTest
extends AbstractGhidraHeadedIntegrationTest { extends AbstractGhidraHeadedIntegrationTest {
protected static final int MAX_TIME = 4000; // timeout for scripts run by invoking RunScriptTask directly
protected static final int SCRIPT_TIMEOUT_SECS = 5; protected static final int TASK_RUN_SCRIPT_TIMEOUT_SECS = 5;
// timeout for scripts run indirectly through the GUI
protected static final int GUI_RUN_SCRIPT_TIMEOUT_MSECS = 6 * DEFAULT_WAIT_TIMEOUT;
protected TestEnv env; protected TestEnv env;
protected CodeBrowserPlugin browser; protected CodeBrowserPlugin browser;
protected GhidraScriptMgrPlugin plugin; protected GhidraScriptMgrPlugin plugin;
@ -594,12 +596,6 @@ public abstract class AbstractGhidraScriptMgrPluginTest
waitForSwing(); waitForSwing();
} }
protected void performGlobalRunLastScriptAction() {
// note: this action used to be different from the 'run last script'; currently they are
// the same
pressRunLastScriptButton();
}
protected KeyBindingInputDialog pressKeyBindingAction() { protected KeyBindingInputDialog pressKeyBindingAction() {
DockingActionIf keyBindingAction = getAction(plugin, "Key Binding"); DockingActionIf keyBindingAction = getAction(plugin, "Key Binding");
performAction(keyBindingAction, false); performAction(keyBindingAction, false);
@ -625,10 +621,17 @@ public abstract class AbstractGhidraScriptMgrPluginTest
waitForSwing(); waitForSwing();
} }
protected String runScript(String scriptName) throws Exception { /**
* Run the currently selected script by pressing the run button and return its output.
*
* @param taskName name for the task listener
* @return script output written to the console
* @throws Exception on failure, e.g. timeout
*/
protected String runSelectedScript(String taskName) throws Exception {
clearConsole(); clearConsole();
TaskListenerFlag taskFlag = new TaskListenerFlag(scriptName); TaskListenerFlag taskFlag = new TaskListenerFlag(taskName);
TaskUtilities.addTrackedTaskListener(taskFlag); TaskUtilities.addTrackedTaskListener(taskFlag);
pressRunButton(); pressRunButton();
@ -639,8 +642,15 @@ public abstract class AbstractGhidraScriptMgrPluginTest
return output; return output;
} }
protected String runLastScript(String scriptName) throws Exception { /**
TaskListenerFlag taskFlag = new TaskListenerFlag(scriptName); * Run the last script by pressing the last script button and return output.
*
* @param taskName name for the task listener
* @return script output written to the console
* @throws Exception on failure, e.g. timeout
*/
protected String runLastScript(String taskName) throws Exception {
TaskListenerFlag taskFlag = new TaskListenerFlag(taskName);
TaskUtilities.addTrackedTaskListener(taskFlag); TaskUtilities.addTrackedTaskListener(taskFlag);
pressRunLastScriptButton(); pressRunLastScriptButton();
@ -651,18 +661,6 @@ public abstract class AbstractGhidraScriptMgrPluginTest
return output; return output;
} }
protected String runGlobalLastScriptAction(String scriptName) throws Exception {
TaskListenerFlag taskFlag = new TaskListenerFlag(scriptName);
TaskUtilities.addTrackedTaskListener(taskFlag);
performGlobalRunLastScriptAction();
waitForTaskEnd(taskFlag);
String output = getConsoleText();
clearConsole();
return output;
}
protected void deleteFile(ResourceFile file) { protected void deleteFile(ResourceFile file) {
assertTrue(file.delete()); assertTrue(file.delete());
} }
@ -1031,15 +1029,9 @@ public abstract class AbstractGhidraScriptMgrPluginTest
protected void waitForTaskEnd(TaskListenerFlag flag) { protected void waitForTaskEnd(TaskListenerFlag flag) {
waitForSwing(); waitForSwing();
int waitCount = 0; int totalTime = 0;
while (!flag.ended && waitCount < 401) { while (!flag.ended && totalTime <= GUI_RUN_SCRIPT_TIMEOUT_MSECS) {
try { totalTime += sleep(DEFAULT_WAIT_DELAY);
Thread.sleep(DEFAULT_WAIT_DELAY);
}
catch (InterruptedException e) {
// don't care; try again
}
waitCount++;
} }
TaskUtilities.removeTrackedTaskListener(flag); TaskUtilities.removeTrackedTaskListener(flag);
@ -1047,6 +1039,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest
if (!flag.ended) { if (!flag.ended) {
Assert.fail("Task took too long to complete: " + flag); Assert.fail("Task took too long to complete: " + flag);
} }
Msg.debug(this, flag.taskName + " task ended in " + totalTime + " ms");
} }
protected int getSelectedRow() { protected int getSelectedRow() {
@ -1165,11 +1158,11 @@ public abstract class AbstractGhidraScriptMgrPluginTest
pressButtonByText(window, "Cancel"); pressButtonByText(window, "Cancel");
} }
protected TestChangeProgramScript startCancellableScript() throws Exception { protected TestChangeProgramScript startCancellableScriptTask() throws Exception {
TestChangeProgramScript script = new TestChangeProgramScript(); TestChangeProgramScript script = new TestChangeProgramScript();
ResourceFile fakeFile = new ResourceFile(createTempFile(CANCELLABLE_SCRIPT_NAME, "java")); ResourceFile fakeFile = new ResourceFile(createTempFile(CANCELLABLE_SCRIPT_NAME, "java"));
script.setSourceFile(fakeFile); script.setSourceFile(fakeFile);
runScript(script); startRunScriptTask(script);
boolean success = script.waitForStart(); boolean success = script.waitForStart();
assertTrue("Test script did not get started!", success); assertTrue("Test script did not get started!", success);
@ -1202,7 +1195,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest
assertTrue("Timed-out waiting for cancelled script to complete", success); assertTrue("Timed-out waiting for cancelled script to complete", success);
} }
protected void runScript(GhidraScript script) throws Exception { protected void startRunScriptTask(GhidraScript script) throws Exception {
Task task = new RunScriptTask(script, plugin.getCurrentState(), console); Task task = new RunScriptTask(script, plugin.getCurrentState(), console);
task.addTaskListener(provider.getTaskListener()); task.addTaskListener(provider.getTaskListener());
new TaskLauncher(task, plugin.getTool().getToolFrame()); new TaskLauncher(task, plugin.getTool().getToolFrame());
@ -1213,10 +1206,10 @@ public abstract class AbstractGhidraScriptMgrPluginTest
GhidraScript script = GhidraScript script =
scriptProvider.getScriptInstance(scriptFile, new PrintWriter(System.err)); scriptProvider.getScriptInstance(scriptFile, new PrintWriter(System.err));
return runScriptAndGetOutput(script); return runScriptTaskAndGetOutput(script);
} }
protected String runScriptAndGetOutput(GhidraScript script) throws Exception { protected String runScriptTaskAndGetOutput(GhidraScript script) throws Exception {
SpyConsole spyConsole = installSpyConsole(); SpyConsole spyConsole = installSpyConsole();
Task task = new RunScriptTask(script, plugin.getCurrentState(), spyConsole); Task task = new RunScriptTask(script, plugin.getCurrentState(), spyConsole);
@ -1238,7 +1231,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest
TaskLauncher.launch(task); TaskLauncher.launch(task);
latch.await(SCRIPT_TIMEOUT_SECS, TimeUnit.SECONDS); latch.await(TASK_RUN_SCRIPT_TIMEOUT_SECS, TimeUnit.SECONDS);
String output = spyConsole.getApiOutput(); String output = spyConsole.getApiOutput();
spyConsole.clear(); spyConsole.clear();
@ -1604,11 +1597,11 @@ public abstract class AbstractGhidraScriptMgrPluginTest
} }
boolean waitForStart() throws Exception { boolean waitForStart() throws Exception {
return startedLatch.await(SCRIPT_TIMEOUT_SECS, TimeUnit.SECONDS); return startedLatch.await(TASK_RUN_SCRIPT_TIMEOUT_SECS, TimeUnit.SECONDS);
} }
boolean waitForFinish() throws Exception { boolean waitForFinish() throws Exception {
return doneLatch.await(SCRIPT_TIMEOUT_SECS, TimeUnit.SECONDS); return doneLatch.await(TASK_RUN_SCRIPT_TIMEOUT_SECS, TimeUnit.SECONDS);
} }
} }

View file

@ -104,11 +104,11 @@ public class BundleStatusManagerTest extends AbstractGhidraScriptMgrPluginTest {
assertTrue(status.isEnabled()); assertTrue(status.isEnabled());
assertScriptInTable(scriptFile); assertScriptInTable(scriptFile);
runScript(SCRIPT_NAME); selectAndRunScript(SCRIPT_NAME);
cleanViaGUI(viewRow); cleanViaGUI(viewRow);
runScript(SCRIPT_NAME); runSelectedScript(SCRIPT_NAME);
} }
@Test @Test
@ -181,7 +181,7 @@ public class BundleStatusManagerTest extends AbstractGhidraScriptMgrPluginTest {
addBundlesViaGUI(dir1, dir2); addBundlesViaGUI(dir1, dir2);
String output = runScript(TEST_SCRIPT_NAME + ".java"); String output = selectAndRunScript(TEST_SCRIPT_NAME + ".java");
assertEquals(EXPECTED_OUTPUT, output); assertEquals(EXPECTED_OUTPUT, output);
int row1 = getBundleRow(dir1); int row1 = getBundleRow(dir1);
@ -392,11 +392,10 @@ public class BundleStatusManagerTest extends AbstractGhidraScriptMgrPluginTest {
testBundleHostListener.awaitActivation(); testBundleHostListener.awaitActivation();
} }
@Override public String selectAndRunScript(String scriptName) throws Exception {
public String runScript(String scriptName) throws Exception {
env.getTool().showComponentProvider(provider, true); env.getTool().showComponentProvider(provider, true);
selectScript(scriptName); selectScript(scriptName);
String output = super.runScript(scriptName); String output = runSelectedScript(scriptName);
env.getTool().showComponentProvider(bundleStatusProvider, true); env.getTool().showComponentProvider(bundleStatusProvider, true);
return output; return output;
} }

View file

@ -46,7 +46,7 @@ public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTes
// //
String initialScriptName = "HelloWorldScript.java"; String initialScriptName = "HelloWorldScript.java";
selectScript(initialScriptName); selectScript(initialScriptName);
String fullOutput = runScript(initialScriptName); String fullOutput = runSelectedScript(initialScriptName);
String expectedOutput = "Hello World"; String expectedOutput = "Hello World";
assertTrue("Script did not run - output: " + fullOutput, assertTrue("Script did not run - output: " + fullOutput,
fullOutput.indexOf(expectedOutput) != -1); fullOutput.indexOf(expectedOutput) != -1);
@ -63,7 +63,7 @@ public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTes
// //
String secondScriptName = "FormatExampleScript.java"; String secondScriptName = "FormatExampleScript.java";
selectScript(secondScriptName); selectScript(secondScriptName);
fullOutput = runScript(secondScriptName); fullOutput = runSelectedScript(secondScriptName);
expectedOutput = "jumped over the"; expectedOutput = "jumped over the";
assertTrue("Script did not run - output: " + fullOutput, assertTrue("Script did not run - output: " + fullOutput,
fullOutput.indexOf(expectedOutput) != -1); fullOutput.indexOf(expectedOutput) != -1);
@ -84,7 +84,7 @@ public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTes
// //
String scriptName = "HelloWorldScript.java"; String scriptName = "HelloWorldScript.java";
selectScript(scriptName); selectScript(scriptName);
String fullOutput = runScript(scriptName); String fullOutput = runSelectedScript(scriptName);
String expectedOutput = "Hello World"; String expectedOutput = "Hello World";
assertTrue("Script did not run - output: " + fullOutput, assertTrue("Script did not run - output: " + fullOutput,
fullOutput.indexOf(expectedOutput) != -1); fullOutput.indexOf(expectedOutput) != -1);
@ -105,7 +105,7 @@ public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTes
// //
String scriptName = "HelloWorldScript.java"; String scriptName = "HelloWorldScript.java";
selectScript(scriptName); selectScript(scriptName);
String fullOutput = runScript(scriptName); String fullOutput = runSelectedScript(scriptName);
String expectedOutput = "Hello World"; String expectedOutput = "Hello World";
assertTrue("Script did not run - output: " + fullOutput, assertTrue("Script did not run - output: " + fullOutput,
fullOutput.indexOf(expectedOutput) != -1); fullOutput.indexOf(expectedOutput) != -1);
@ -115,7 +115,7 @@ public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTes
// //
// Run the script again // Run the script again
// //
fullOutput = runGlobalLastScriptAction(scriptName); fullOutput = runLastScript(scriptName);
assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1); assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1);
} }

View file

@ -97,7 +97,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
pressSaveButton(); pressSaveButton();
String scriptOutput = runScript(script.getName()); String scriptOutput = runSelectedScript(script.getName());
assertTrue("Script output not generated", assertTrue("Script output not generated",
scriptOutput.contains("> new scripts are neato!")); scriptOutput.contains("> new scripts are neato!"));
@ -132,7 +132,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
pressSaveButton(); pressSaveButton();
setTimestampToTheFuture(script); setTimestampToTheFuture(script);
String updatedScriptOutput = runScript(script.getName()); String updatedScriptOutput = runSelectedScript(script.getName());
assertTrue("Script output not updated with new script contents - did recompile work?", assertTrue("Script output not updated with new script contents - did recompile work?",
StringUtilities.containsAll(updatedScriptOutput, "> new scripts are neato!", StringUtilities.containsAll(updatedScriptOutput, "> new scripts are neato!",
@ -530,7 +530,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
@Test @Test
public void testCancel() throws Exception { public void testCancel() throws Exception {
TestChangeProgramScript script = startCancellableScript(); TestChangeProgramScript script = startCancellableScriptTask();
cancel(); cancel();
@ -539,7 +539,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
@Test @Test
public void testCancel_DoNotCancel() throws Exception { public void testCancel_DoNotCancel() throws Exception {
TestChangeProgramScript script = startCancellableScript(); TestChangeProgramScript script = startCancellableScriptTask();
cancel(); cancel();