Tests - fixed tests failing due to recent merges

This commit is contained in:
dragonmacher 2019-05-28 17:34:32 -04:00
parent 8495cc68ee
commit bfe89551de
8 changed files with 75 additions and 65 deletions

View file

@ -53,6 +53,7 @@ import ghidra.util.datastruct.WeakSet;
import ghidra.util.table.GhidraTableFilterPanel; import ghidra.util.table.GhidraTableFilterPanel;
import ghidra.util.task.*; import ghidra.util.task.*;
import resources.ResourceManager; import resources.ResourceManager;
import util.CollectionUtils;
import utilities.util.FileUtilities; import utilities.util.FileUtilities;
public class GhidraScriptComponentProvider extends ComponentProviderAdapter { public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
@ -479,14 +480,6 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
tableModel.fireTableDataChanged(); tableModel.fireTableDataChanged();
} }
/*
* is more than just root node selected?
*/
boolean isSelectedCategory() {
TreePath path = scriptCategoryTree.getSelectionPath();
return path != null && path.getPathCount() > 1;
}
String[] getSelectedCategoryPath() { String[] getSelectedCategoryPath() {
TreePath currentPath = scriptCategoryTree.getSelectionPath(); TreePath currentPath = scriptCategoryTree.getSelectionPath();
@ -528,7 +521,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
tableModel.fireTableDataChanged(); tableModel.fireTableDataChanged();
updateTreeNodesToReflectAvailableScripts(); trimUnusedTreeCategories();
scriptRoot.fireNodeStructureChanged(scriptRoot); scriptRoot.fireNodeStructureChanged(scriptRoot);
if (preRefreshSelectionPath != null) { if (preRefreshSelectionPath != null) {
@ -537,6 +530,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
} }
private void updateAvailableScriptFilesForAllPaths() { private void updateAvailableScriptFilesForAllPaths() {
List<ResourceFile> scriptsToRemove = tableModel.getScripts(); List<ResourceFile> scriptsToRemove = tableModel.getScripts();
List<ResourceFile> scriptAccumulator = new ArrayList<>(); List<ResourceFile> scriptAccumulator = new ArrayList<>();
List<Path> dirPaths = pathManager.getPaths(); List<Path> dirPaths = pathManager.getPaths();
@ -553,7 +547,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
} }
GhidraScriptUtil.refreshDuplicates(); GhidraScriptUtil.refreshDuplicates();
refreshActions(); refreshScriptData();
} }
private void updateAvailableScriptFilesForDirectory(List<ResourceFile> scriptsToRemove, private void updateAvailableScriptFilesForDirectory(List<ResourceFile> scriptsToRemove,
@ -577,7 +571,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
} }
private void refreshActions() { private void refreshScriptData() {
List<ResourceFile> scripts = tableModel.getScripts(); List<ResourceFile> scripts = tableModel.getScripts();
for (ResourceFile script : scripts) { for (ResourceFile script : scripts) {
@ -593,26 +587,43 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
} }
} }
private void updateTreeNodesToReflectAvailableScripts() { // note: we really should just rebuild the tree instead of using this method
ArrayList<GTreeNode> nodesToRemove = new ArrayList<>(); private void trimUnusedTreeCategories() {
Iterator<GTreeNode> it = new BreadthFirstIterator(scriptCategoryTree, scriptRoot);
while (it.hasNext()) { /*
GTreeNode node = it.next(); Unusual Algorithm
String[] category = getCategoryPath(node);
Iterator<ScriptInfo> iter = GhidraScriptUtil.getScriptInfoIterator(); The tree nodes represent categories, but do not contain nodes for individual
boolean found = false; scripts. We wish to remove any of the tree nodes that no longer represent script
while (iter.hasNext()) { categories. (This can happen when a script is deleted or its category is changed.)
if (iter.next().isCategory(category)) { This algorithm will assume that all nodes need to be deleted. Then, each script is
found = true; examined, using its category to mark a given node as 'safe'; that node's parents are
break; also marked as safe. Any nodes remaining unmarked have no reference script and
} will be deleted.
} */
if (!found) {
nodesToRemove.add(node); // note: turn String[] to List<String> to use hashing
Iterator<ScriptInfo> scripts = GhidraScriptUtil.getScriptInfoIterator();
Set<List<String>> categories = new HashSet<>();
for (ScriptInfo info : CollectionUtils.asIterable(scripts)) {
String[] path = info.getCategory();
List<String> category = Arrays.asList(path);
for (int i = 1; i <= category.size(); i++) {
categories.add(category.subList(0, i));
} }
} }
for (GTreeNode node : nodesToRemove) { List<GTreeNode> toDelete = new LinkedList<>();
Iterator<GTreeNode> nodes = new BreadthFirstIterator(scriptCategoryTree, scriptRoot);
for (GTreeNode node : CollectionUtils.asIterable(nodes)) {
String[] path = getCategoryPath(node);
List<String> category = Arrays.asList(path);
if (!categories.contains(category)) {
toDelete.add(node);
}
}
for (GTreeNode node : toDelete) {
GTreeNode parent = node.getParent(); GTreeNode parent = node.getParent();
if (parent != null) { if (parent != null) {
parent.removeNode(node); parent.removeNode(node);
@ -874,7 +885,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
private void updateCategoryTree(ResourceFile script) { private void updateCategoryTree(ResourceFile script) {
scriptRoot.insert(script); scriptRoot.insert(script);
updateTreeNodesToReflectAvailableScripts(); trimUnusedTreeCategories();
} }
private void buildFilter() { private void buildFilter() {

View file

@ -206,7 +206,9 @@ public class GhidraScriptUtil {
/** /**
* Returns the output directory to which the given script file's generated .class file should * Returns the output directory to which the given script file's generated .class file should
* be written. * be written
* @param scriptFile the script file
* @return the directory
*/ */
public static ResourceFile getScriptCompileOutputDirectory(ResourceFile scriptFile) { public static ResourceFile getScriptCompileOutputDirectory(ResourceFile scriptFile) {
return new ResourceFile(USER_SCRIPTS_BIN_DIR); return new ResourceFile(USER_SCRIPTS_BIN_DIR);
@ -349,8 +351,9 @@ public class GhidraScriptUtil {
/** /**
* Returns the list of directories to which scripts are compiled. * Returns the list of directories to which scripts are compiled.
* @return the list
* *
* @see #getScriptCompileOutputDirectory(File) * @see #getScriptCompileOutputDirectory(ResourceFile)
*/ */
public static List<ResourceFile> getScriptBinDirectories() { public static List<ResourceFile> getScriptBinDirectories() {
return Arrays.asList(new ResourceFile(USER_SCRIPTS_BIN_DIR)); return Arrays.asList(new ResourceFile(USER_SCRIPTS_BIN_DIR));
@ -667,7 +670,7 @@ public class GhidraScriptUtil {
* scripts such that names are unique. If this method returns a non-null value, then the * scripts such that names are unique. If this method returns a non-null value, then the
* name given name is taken. * name given name is taken.
* *
* @param name the name of the script for which to get a ScriptInfo * @param scriptName the name of the script for which to get a ScriptInfo
* @return a ScriptInfo matching the given name; null if no script by that name is known to * @return a ScriptInfo matching the given name; null if no script by that name is known to
* the script manager * the script manager
*/ */
@ -680,11 +683,13 @@ public class GhidraScriptUtil {
} }
/** /**
* Runs the specified script with the specified state. * Runs the specified script with the specified state
* *
* @param scriptState State representing environment variables that the script is able * @param scriptState state representing environment variables that the script is able to access
* to access. * @param script Script to be run
* @param script Script to be run. * @param writer the writer to which warning and error messages will be written
* @param originator the client class requesting the script run; used for logging
* @param monitor the task monitor
* @return whether the script successfully completed running * @return whether the script successfully completed running
*/ */
public static boolean runScript(GhidraState scriptState, GhidraScript script, public static boolean runScript(GhidraState scriptState, GhidraScript script,

View file

@ -62,7 +62,8 @@ import ghidra.util.table.GhidraTableFilterPanel;
import ghidra.util.task.*; import ghidra.util.task.*;
import utilities.util.FileUtilities; import utilities.util.FileUtilities;
public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHeadedIntegrationTest { public abstract class AbstractGhidraScriptMgrPluginTest
extends AbstractGhidraHeadedIntegrationTest {
protected static final int MAX_TIME = 4000; protected static final int MAX_TIME = 4000;
protected static final int SCRIPT_TIMEOUT_SECS = 5; protected static final int SCRIPT_TIMEOUT_SECS = 5;
protected TestEnv env; protected TestEnv env;
@ -190,6 +191,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHe
protected void selectCategory(String category) { protected void selectCategory(String category) {
GTree categoryTree = (GTree) findComponentByName(provider.getComponent(), "CATEGORY_TREE"); GTree categoryTree = (GTree) findComponentByName(provider.getComponent(), "CATEGORY_TREE");
waitForTree(categoryTree);
JTree jTree = (JTree) invokeInstanceMethod("getJTree", categoryTree); JTree jTree = (JTree) invokeInstanceMethod("getJTree", categoryTree);
assertNotNull(jTree); assertNotNull(jTree);
GTreeNode child = categoryTree.getRootNode().getChild(category); GTreeNode child = categoryTree.getRootNode().getChild(category);
@ -345,6 +347,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHe
* This call will: * This call will:
* -open the file in an editor * -open the file in an editor
* -update the text area and buffer fields of this test * -update the text area and buffer fields of this test
* @param file the file to open
*/ */
protected void openInEditor(final ResourceFile file) { protected void openInEditor(final ResourceFile file) {
runSwing(() -> editor = provider.editScriptInGhidra(file)); runSwing(() -> editor = provider.editScriptInGhidra(file));
@ -500,13 +503,9 @@ public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHe
buffer.append("Test text: ").append(testName.getMethodName()); buffer.append("Test text: ").append(testName.getMethodName());
runSwing(() -> { runSwing(() -> {
// TODO editorTextArea.requestFocusInWindow()
editorTextArea.setText(buffer.toString()); editorTextArea.setText(buffer.toString());
}); });
//typeText(buffer.toString());
return buffer.toString(); return buffer.toString();
} }
@ -692,8 +691,6 @@ public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHe
//@category name //@category name
contents = contents.replaceFirst("//@category \\w+", "//@category " + newCategory); contents = contents.replaceFirst("//@category \\w+", "//@category " + newCategory);
Msg.debug(this, "new category string: " + newCategory);
writeStringToFile(script, contents); writeStringToFile(script, contents);
// //
@ -703,7 +700,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHe
// //
File file = script.getFile(false); File file = script.getFile(false);
long lastModified = file.lastModified(); long lastModified = file.lastModified();
file.setLastModified(lastModified + (1000 * System.currentTimeMillis())); long inTheFuture = 10000 + System.currentTimeMillis();
file.setLastModified(lastModified + inTheFuture);
return newCategory; return newCategory;
} }
@ -1318,7 +1316,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest extends AbstractGhidraHe
String parentClassName = parentScriptName.replaceAll("\\.java", ""); String parentClassName = parentScriptName.replaceAll("\\.java", "");
String importLine = (parentScriptPackage != null String importLine = (parentScriptPackage != null
? ("import " + parentScriptPackage + "." + parentClassName + ";\n\n") : ""); ? ("import " + parentScriptPackage + "." + parentClassName + ";\n\n")
: "");
//@formatter:off //@formatter:off
String newScript = String newScript =

View file

@ -41,10 +41,6 @@ import ghidra.util.exception.AssertException;
public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTest { public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTest {
public GhidraScriptMgrPlugin3Test() {
super();
}
@Test @Test
public void testKeyBinding() throws Exception { public void testKeyBinding() throws Exception {
@ -209,6 +205,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
String oldCategory = newCategory; String oldCategory = newCategory;
newCategory = changeScriptCategory_WithSubcatogory(newScript); newCategory = changeScriptCategory_WithSubcatogory(newScript);
refreshScriptManager(); refreshScriptManager();
assertCategoryInTree(newCategory); assertCategoryInTree(newCategory);
@ -287,7 +284,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
performAction(pathAction, false); performAction(pathAction, false);
waitForSwing(); waitForSwing();
PickPathsDialog pathsDialog = env.waitForDialogComponent(PickPathsDialog.class, MAX_TIME); PickPathsDialog pathsDialog = waitForDialogComponent(PickPathsDialog.class);
final File dir = new File(getTestDirectoryPath() + "/test_scripts"); final File dir = new File(getTestDirectoryPath() + "/test_scripts");
dir.mkdirs(); dir.mkdirs();
@ -308,7 +305,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
chooseJavaProvider(); chooseJavaProvider();
SaveDialog sd = env.waitForDialogComponent(SaveDialog.class, MAX_TIME); SaveDialog sd = waitForDialogComponent(SaveDialog.class);
final ListPanel listPanel = (ListPanel) findComponentByName(sd.getComponent(), "PATH_LIST"); final ListPanel listPanel = (ListPanel) findComponentByName(sd.getComponent(), "PATH_LIST");
assertNotNull(listPanel); assertNotNull(listPanel);

View file

@ -15,7 +15,7 @@
*/ */
package ghidra.app.plugin.core.diff; package ghidra.app.plugin.core.diff;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.*;
import java.awt.Window; import java.awt.Window;
@ -35,10 +35,6 @@ import ghidra.test.TestEnv;
public class DiffSaveSettingsTest extends DiffApplyTestAdapter { public class DiffSaveSettingsTest extends DiffApplyTestAdapter {
public DiffSaveSettingsTest() {
super();
}
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {

View file

@ -64,7 +64,10 @@ public abstract class AbstractDockingTool implements DockingTool {
@Override @Override
public void removeComponentProvider(ComponentProvider provider) { public void removeComponentProvider(ComponentProvider provider) {
Runnable r = () -> winMgr.removeComponent(provider); Runnable r = () -> {
actionMgr.removeComponentActions(provider);
winMgr.removeComponent(provider);
};
SystemUtilities.runSwingNow(r); SystemUtilities.runSwingNow(r);
} }

View file

@ -170,7 +170,7 @@ public class DockingToolActionManager implements PropertyChangeListener {
/** /**
* Get all actions that have the action name which includes the action owner's name. * Get all actions that have the action name which includes the action owner's name.
* *
* @param fullName full name for the action, e.g., "My Action (My Plugin)" * @param fullActionName full name for the action, e.g., "My Action (My Plugin)"
* @return list of actions; empty if no action exists with the given name * @return list of actions; empty if no action exists with the given name
*/ */
public List<DockingActionIf> getDockingActionsByFullActionName(String fullActionName) { public List<DockingActionIf> getDockingActionsByFullActionName(String fullActionName) {
@ -248,11 +248,10 @@ public class DockingToolActionManager implements PropertyChangeListener {
} }
/** /**
* Get the actions for the given provider and remove them from the * Get the actions for the given provider and remove them from the action map
* actionMap; call the window manager to remove the provider. * @param provider provider whose actions are to be removed
* @param provider provider to be removed
*/ */
public void removeComponent(ComponentProvider provider) { public void removeComponentActions(ComponentProvider provider) {
Iterator<DockingActionIf> iterator = winMgr.getComponentActions(provider); Iterator<DockingActionIf> iterator = winMgr.getComponentActions(provider);
while (iterator.hasNext()) { while (iterator.hasNext()) {
DockingActionIf action = iterator.next(); DockingActionIf action = iterator.next();
@ -262,7 +261,7 @@ public class DockingToolActionManager implements PropertyChangeListener {
actionMap.remove(name); actionMap.remove(name);
} }
} }
winMgr.removeComponent(provider);
} }
@Override @Override

View file

@ -52,7 +52,7 @@ public class AbstractTaskTest extends AbstractDockingTest {
TDEvent lastEvent = eventQueue.peekLast(); TDEvent lastEvent = eventQueue.peekLast();
boolean swingIsLast = lastEvent.getThreadName().contains("AWT"); boolean swingIsLast = lastEvent.getThreadName().contains("AWT");
if (!swingIsLast) { if (!swingIsLast) {
fail("The Swing thread did not block until the task finished"); fail("The Swing thread did not block until the task finished.\nEvents: " + eventQueue);
} }
} }
@ -60,7 +60,7 @@ public class AbstractTaskTest extends AbstractDockingTest {
TDEvent lastEvent = eventQueue.peekLast(); TDEvent lastEvent = eventQueue.peekLast();
boolean swingIsLast = lastEvent.getThreadName().contains("AWT"); boolean swingIsLast = lastEvent.getThreadName().contains("AWT");
if (swingIsLast) { if (swingIsLast) {
fail("The Swing thread blocked until the task finished"); fail("The Swing thread blocked until the task finished.\nEvents: " + eventQueue);
} }
} }