Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3181 - Fixed script key bindings

dragonmacherd9e27c1bf167
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
index ce998da852..f8a5689ae8 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
@@ -168,6 +168,10 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
scriptTable.repaint();
}
+ void keyBindingUpdated() {
+ scriptTable.repaint();
+ }
+
void renameScript() {
ResourceFile script = getSelectedScript();
ResourceFile directory = script.getParentFile();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java
index 3c88008e30..5d7ba8d2e3 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java
@@ -199,7 +199,7 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
@Override
public String getColumnName() {
- return "In Tool";
+ return SCRIPT_ACTION_COLUMN_NAME;
}
@Override
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/KeyBindingInputDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/KeyBindingInputDialog.java
index d986d66a5e..d2b128d8ae 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/KeyBindingInputDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/KeyBindingInputDialog.java
@@ -73,9 +73,6 @@ class KeyBindingInputDialog extends DialogComponentProvider implements KeyEntryL
return isCancelled;
}
- /**
- * @see docking.KeyEntryListener#processEntry(javax.swing.KeyStroke)
- */
@Override
public void processEntry(KeyStroke keyStroke) {
ks = keyStroke;
@@ -84,4 +81,8 @@ class KeyBindingInputDialog extends DialogComponentProvider implements KeyEntryL
KeyStroke getKeyStroke() {
return ks;
}
+
+ void setKeyStroke(KeyStroke ks) {
+ kbField.setKeyStroke(ks);
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptAction.java
index f7959b515b..1b5bb324ec 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptAction.java
@@ -36,7 +36,7 @@ class ScriptAction extends DockingAction {
private boolean isUserDefinedKeyBinding = false;
ScriptAction(GhidraScriptMgrPlugin plugin, ResourceFile script) {
- super(script.getName(), plugin.getName(), false);
+ super(script.getName(), plugin.getName());
this.plugin = plugin;
this.script = script;
setEnabled(true);
@@ -50,13 +50,7 @@ class ScriptAction extends DockingAction {
}
@Override
- public void setKeyBindingData(KeyBindingData keyBindingData) {
- isUserDefinedKeyBinding = false; // reset
- if (keyBindingData == null) {
- // simply clearing out any keybinding data settings
- super.setKeyBindingData(keyBindingData);
- return;
- }
+ public void setUnvalidatedKeyBindingData(KeyBindingData keyBindingData) {
//
// Unobvious Dependencies!:
@@ -73,6 +67,19 @@ class ScriptAction extends DockingAction {
KeyBindingData newKeyBindingData = checkForFallbackKeybindingCondition(keyBindingData);
updateUserDefinedKeybindingStatus(newKeyBindingData);
super.setUnvalidatedKeyBindingData(newKeyBindingData);
+ plugin.getProvider().keyBindingUpdated();
+ }
+
+ @Override
+ public void setKeyBindingData(KeyBindingData keyBindingData) {
+ isUserDefinedKeyBinding = false; // reset
+ if (keyBindingData == null) {
+ // simply clearing out any keybinding data settings
+ super.setKeyBindingData(keyBindingData);
+ return;
+ }
+
+ setUnvalidatedKeyBindingData(keyBindingData);
}
private KeyBindingData checkForFallbackKeybindingCondition(KeyBindingData keyBindingData) {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin1Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin1Test.java
index 66cf697bde..860fb93427 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin1Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin1Test.java
@@ -15,10 +15,22 @@
*/
package ghidra.app.plugin.core.script;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
+
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+
+import javax.swing.Action;
+import javax.swing.KeyStroke;
+import javax.swing.table.TableColumn;
import org.junit.Test;
+import docking.DockingUtils;
+import docking.action.DockingActionIf;
+import docking.actions.KeyBindingUtils;
+import docking.actions.ToolActions;
+
public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTest {
public GhidraScriptMgrPlugin1Test() {
@@ -64,4 +76,102 @@ public class GhidraScriptMgrPlugin1Test extends AbstractGhidraScriptMgrPluginTes
fullOutput = runLastScript(secondScriptName);
assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1);
}
+
+ @Test
+ public void testRunLastScriptActionWithDifferentRowSelected() throws Exception {
+
+ //
+ // Run a script once...
+ //
+ String scriptName = "HelloWorldScript.java";
+ selectScript(scriptName);
+ String fullOutput = runScript(scriptName);
+ String expectedOutput = "Hello World";
+ assertTrue("Script did not run - output: " + fullOutput,
+ fullOutput.indexOf(expectedOutput) != -1);
+
+ selectScript("PrintStructureScript.java");// note: this script will error out
+
+ //
+ // Run the script again
+ //
+ fullOutput = runLastScript(scriptName);
+ assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1);
+ }
+
+ @Test
+ public void testRunLastScriptActionWithScriptProviderClosed() throws Exception {
+ //
+ // Run a script once...
+ //
+ String scriptName = "HelloWorldScript.java";
+ selectScript(scriptName);
+ String fullOutput = runScript(scriptName);
+ String expectedOutput = "Hello World";
+ assertTrue("Script did not run - output: " + fullOutput,
+ fullOutput.indexOf(expectedOutput) != -1);
+
+ closeScriptProvider();
+
+ //
+ // Run the script again
+ //
+ fullOutput = runGlobalLastScriptAction(scriptName);
+ assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1);
+ }
+
+ @Test
+ public void testAddKeyBindingToScript() throws Exception {
+
+ //
+ // Adding a key binding to a script will not only supply a key bindings, but will also
+ // add an action to the tool.
+ //
+
+ String scriptName = "HelloWorldPopupScript.java";
+ selectScript(scriptName);
+
+ KeyBindingInputDialog kbDialog = pressKeyBindingAction();
+ KeyStroke newKs = KeyStroke.getKeyStroke(KeyEvent.VK_E,
+ DockingUtils.CONTROL_KEY_MODIFIER_MASK | InputEvent.SHIFT_DOWN_MASK);
+ runSwing(() -> kbDialog.setKeyStroke(newKs));
+ pressButtonByText(kbDialog.getComponent(), "OK");
+
+ // verify the table updated
+ assertColumnValue("In Tool", Boolean.TRUE);
+ assertColumnValue("Key", KeyBindingUtils.parseKeyStroke(newKs));
+
+ // verify the action is in the tool
+ DockingActionIf toolAction = getAction(plugin, scriptName);
+ assertNotNull(toolAction);
+ KeyStroke actionKs = toolAction.getKeyBinding();
+ assertEquals(newKs, actionKs);
+
+ ToolActions toolActions = plugin.getTool().getToolActions();
+ Action toolActionByKeyStroke = toolActions.getAction(newKs);
+ assertNotNull(toolActionByKeyStroke);
+ }
+
+ private void assertColumnValue(String columnName, Object expectedValue) {
+ int row = scriptTable.getSelectedRow();
+
+ TableColumn column = scriptTable.getColumn(columnName);
+ int modelIndex = column.getModelIndex();
+ int columnIndex = scriptTable.convertColumnIndexToView(modelIndex);
+
+ Object actualValue = runSwing(() -> scriptTable.getValueAt(row, columnIndex));
+ assertEquals(
+ "Column value is not as expected for row/col: " + row + "/" + columnIndex +
+ " for column '" + columnName + "'",
+ expectedValue.toString(), actualValue.toString());
+ }
+
+ /*
+ * TODO Tests missing
+ * ScriptAction has odd behavior (code coverage should expose the lack of tests):
+ * -test that a script with a meta data key binding
+ * -test that a user can add a key binding to a script with a meta data key binding
+ * -test that a user can clear a GUI-assigned key binding and that the value
+ * defined in the meta data will still be used
+ */
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java
index 3144f7696d..2213f24fcf 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java
@@ -15,8 +15,7 @@
*/
package ghidra.app.plugin.core.script;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
import java.io.*;
@@ -57,49 +56,6 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
consoleText.indexOf("> Hello World") >= 0);
}
- @Test
- public void testRunLastScriptActionWithDifferentRowSelected() throws Exception {
-
- //
- // Run a script once...
- //
- String scriptName = "HelloWorldScript.java";
- selectScript(scriptName);
- String fullOutput = runScript(scriptName);
- String expectedOutput = "Hello World";
- assertTrue("Script did not run - output: " + fullOutput,
- fullOutput.indexOf(expectedOutput) != -1);
-
- selectScript("PrintStructureScript.java");// note: this script will error out
-
- //
- // Run the script again
- //
- fullOutput = runLastScript(scriptName);
- assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1);
- }
-
- @Test
- public void testRunLastScriptActionWithScriptProviderClosed() throws Exception {
- //
- // Run a script once...
- //
- String scriptName = "HelloWorldScript.java";
- selectScript(scriptName);
- String fullOutput = runScript(scriptName);
- String expectedOutput = "Hello World";
- assertTrue("Script did not run - output: " + fullOutput,
- fullOutput.indexOf(expectedOutput) != -1);
-
- closeScriptProvider();
-
- //
- // Run the script again
- //
- fullOutput = runGlobalLastScriptAction(scriptName);
- assertTrue("Did not rerun last run script", fullOutput.indexOf(expectedOutput) != -1);
- }
-
@Test
public void testScriptWithInnerClassAndLocalClass() throws Exception {
ResourceFile innerScriptFile = createInnerClassScript();