Skip to content

Ghidra is a software reverse engineering (SRE) framework

Test fixes

dragonmacherc462d20ca0d5
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java
index 0016076cb8..81e8c3d39d 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java
@@ -19,11 +19,11 @@ import static org.junit.Assert.*;
import java.util.Arrays;
-import javax.swing.JRadioButton;
-import javax.swing.JTextField;
+import javax.swing.*;
import org.junit.Test;
+import docking.widgets.OptionDialog;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Library;
@@ -55,19 +55,16 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
// Check enablement for empty table with modified state.
CompositeEditorTableAction[] pActions = provider.getActions();
- for (int i = 0; i < pActions.length; i++) {
- if ((pActions[i] instanceof FavoritesAction) ||
- (pActions[i] instanceof CycleGroupAction) ||
- (pActions[i] instanceof EditFieldAction) ||
- (pActions[i] instanceof PointerAction) ||
- (pActions[i] instanceof HexNumbersAction) ||
- (pActions[i] instanceof InsertUndefinedAction) ||
- (pActions[i] instanceof AddBitFieldAction) ||
- (pActions[i] instanceof ApplyAction)) {
- checkEnablement(pActions[i], true);
+ for (CompositeEditorTableAction pAction : pActions) {
+ if ((pAction instanceof FavoritesAction) || (pAction instanceof CycleGroupAction) ||
+ (pAction instanceof EditFieldAction) || (pAction instanceof PointerAction) ||
+ (pAction instanceof HexNumbersAction) ||
+ (pAction instanceof InsertUndefinedAction) ||
+ (pAction instanceof AddBitFieldAction) || (pAction instanceof ApplyAction)) {
+ checkEnablement(pAction, true);
}
else {
- checkEnablement(pActions[i], false);
+ checkEnablement(pAction, false);
}
}
@@ -106,7 +103,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
assertLength(10);
assertActualAlignment(1);
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -138,25 +135,20 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
// Check enablement.
CompositeEditorTableAction[] pActions = provider.getActions();
- for (int i = 0; i < pActions.length; i++) {
- if ((pActions[i] instanceof FavoritesAction) ||
- (pActions[i] instanceof CycleGroupAction) ||
- (pActions[i] instanceof EditFieldAction) ||
- (pActions[i] instanceof InsertUndefinedAction) ||
- (pActions[i] instanceof PointerAction) ||
- (pActions[i] instanceof HexNumbersAction) ||
- (pActions[i] instanceof MoveDownAction) ||
- (pActions[i] instanceof DuplicateAction) ||
- (pActions[i] instanceof DuplicateMultipleAction) ||
- (pActions[i] instanceof DeleteAction) ||
- (pActions[i] instanceof ArrayAction) ||
- (pActions[i] instanceof CreateInternalStructureAction) ||
- (pActions[i] instanceof ShowComponentPathAction) ||
- (pActions[i] instanceof ApplyAction)) {
- checkEnablement(pActions[i], true);
+ for (CompositeEditorTableAction pAction : pActions) {
+ if ((pAction instanceof FavoritesAction) || (pAction instanceof CycleGroupAction) ||
+ (pAction instanceof EditFieldAction) ||
+ (pAction instanceof InsertUndefinedAction) || (pAction instanceof PointerAction) ||
+ (pAction instanceof HexNumbersAction) || (pAction instanceof MoveDownAction) ||
+ (pAction instanceof DuplicateAction) ||
+ (pAction instanceof DuplicateMultipleAction) || (pAction instanceof DeleteAction) ||
+ (pAction instanceof ArrayAction) ||
+ (pAction instanceof CreateInternalStructureAction) ||
+ (pAction instanceof ShowComponentPathAction) || (pAction instanceof ApplyAction)) {
+ checkEnablement(pAction, true);
}
else {
- checkEnablement(pActions[i], false);
+ checkEnablement(pAction, false);
}
}
@@ -182,7 +174,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
assertTrue(Arrays.equals(new int[] { 0 }, model.getSelectedRows()));
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -211,7 +203,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
waitForSwing();
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -342,8 +334,8 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
public void testAlignedEditToFunctionDefinitionDataType() throws Exception {
startTransaction("addExternal");
- ExternalLocation extLoc = program.getExternalManager().addExtFunction(Library.UNKNOWN,
- "extLabel", null, SourceType.USER_DEFINED);
+ ExternalLocation extLoc = program.getExternalManager()
+ .addExtFunction(Library.UNKNOWN, "extLabel", null, SourceType.USER_DEFINED);
Function function = extLoc.createFunction();
endTransaction(true);
@@ -394,8 +386,6 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
public void testSelectionOnGoFromNonPackedToDefaultPackedStructure() throws Exception {
init(emptyStructure, pgmRootCat, false);
- CompEditorPanel editorPanel = (CompEditorPanel) getPanel();
-
assertTrue(structureModel.hasChanges());// initial unsaved empty structure
assertTrue(structureModel.isValidName());// name should be valid
assertEquals(structureModel.getTypeName(), "Structure");
@@ -419,7 +409,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
checkSelection(new int[] { 3 });
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -808,7 +798,21 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
assertActualAlignment(4);
}
- ////////////////////////////
+//=================================================================================================
+// Private Methods
+//=================================================================================================
+
+ private void turnOnPacking() {
+ AbstractButton packingButton = findButtonByName(getPanel(), "Packing Enablement");
+ if (packingButton.isSelected()) {
+ return;
+ }
+
+ pressButton(packingButton, false);
+ OptionDialog confirmDialog = waitForDialogComponent(OptionDialog.class);
+ pressButtonByText(confirmDialog, "Yes");
+ waitForSwing();
+ }
private void checkRow(int rowIndex, int offset, int length, String mnemonic, DataType dataType,
String name, String comment) {
@@ -827,5 +831,4 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
private DataTypeComponent addDataType(DataType dataType) {
return structureModel.viewComposite.add(dataType);
}
-
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java
index f754a46413..10309c100b 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorFlexAlignmentTest.java
@@ -17,11 +17,11 @@ package ghidra.app.plugin.core.compositeeditor;
import static org.junit.Assert.*;
-import javax.swing.JRadioButton;
-import javax.swing.JTextField;
+import javax.swing.*;
import org.junit.Test;
+import docking.widgets.OptionDialog;
import ghidra.program.model.data.*;
public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTest {
@@ -88,7 +88,7 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
assertLength(2);
assertActualAlignment(1);
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -113,7 +113,7 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
waitForSwing();
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -125,7 +125,7 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
checkRow(0, 0, 1, "db", ByteDataType.dataType, "", "");
checkRow(1, 1, 1, "char", CharDataType.dataType, "", "");
// It is important to note that a trailing flex array will align the same as any other component and
- // is not guarenteed to fall at the end of the structure.
+ // is not guaranteed to fall at the end of the structure.
checkFlexArrayRow(2, 4, "ddw", DWordDataType.dataType, "", "");
checkBlankRow(3);
assertLength(8);
@@ -145,7 +145,7 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
waitForSwing();
- pressButtonByName(getPanel(), "Packing Enablement"); // toggle -> enable packing
+ turnOnPacking();
assertIsPackingEnabled(true);
assertDefaultPacked();
@@ -245,7 +245,8 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
addDataType(ByteDataType.dataType);
addDataType(CharDataType.dataType);
- addFlexDataType((Structure) structureModel.viewComposite, DWordDataType.dataType, null, null);
+ addFlexDataType((Structure) structureModel.viewComposite, DWordDataType.dataType, null,
+ null);
JRadioButton byValuePackingButton =
(JRadioButton) findComponentByName(editorPanel, "Explicit Packing");
@@ -270,7 +271,9 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
assertActualAlignment(1);
}
- ////////////////////////////
+//=================================================================================================
+// Private Methods
+//=================================================================================================
private void checkFlexArrayRow(int rowIndex, int offset, String mnemonic, DataType dataType,
String name, String comment) {
@@ -305,9 +308,22 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
return structureModel.viewComposite.add(dataType);
}
- private DataTypeComponent addFlexDataType(Structure struct, DataType dataType, String name, String comment) {
+ private DataTypeComponent addFlexDataType(Structure struct, DataType dataType, String name,
+ String comment) {
ArrayDataType a = new ArrayDataType(dataType, 0, 1);
return struct.add(a, name, comment);
}
+ private void turnOnPacking() {
+ AbstractButton packingButton = findButtonByName(getPanel(), "Packing Enablement");
+ if (packingButton.isSelected()) {
+ return;
+ }
+
+ pressButton(packingButton, false);
+ OptionDialog confirmDialog = waitForDialogComponent(OptionDialog.class);
+ pressButtonByText(confirmDialog, "Yes");
+ waitForSwing();
+ }
+
}
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java b/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java
index eba3cdd5d3..53153bf18e 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/test/AbstractDockingTest.java
@@ -1109,8 +1109,9 @@ public abstract class AbstractDockingTest extends AbstractGuiTest {
public static Set<DockingActionIf> getActionsByOwnerAndName(Tool tool, String owner,
String name) {
Set<DockingActionIf> ownerActions = tool.getDockingActionsByOwnerName(owner);
- return ownerActions.stream().filter(action -> action.getName().equals(name)).collect(
- Collectors.toSet());
+ return ownerActions.stream()
+ .filter(action -> action.getName().equals(name))
+ .collect(Collectors.toSet());
}
/**
@@ -1381,19 +1382,35 @@ public abstract class AbstractDockingTest extends AbstractGuiTest {
* component with the specified name.
*
* @param provider the provider of the component to search
- * @param componentName the name of the desired component
+ * @param name the name of the desired component
*
* @return the component, or null if not found
*/
- public static Component findComponentByName(DialogComponentProvider provider,
- String componentName) {
- return findComponentByName(provider.getComponent(), componentName, false);
+ public static Component findComponentByName(DialogComponentProvider provider, String name) {
+ return findComponentByName(provider.getComponent(), name, false);
}
public static JButton findButtonByText(DialogComponentProvider provider, String text) {
return findButtonByText(provider.getComponent(), text);
}
+ /**
+ * Searches the component and subcomponents of the indicated provider and returns the
+ * component with the specified name.
+ *
+ * @param provider the provider of the component to search
+ * @param name the name of the desired component
+ *
+ * @return the component, or null if not found
+ */
+ public static AbstractButton findButtonByName(DialogComponentProvider provider, String name) {
+ Component c = findComponentByName(provider, name);
+ if (!(c instanceof AbstractButton button)) {
+ return null;
+ }
+ return button;
+ }
+
public static JButton findButtonByIcon(DialogComponentProvider provider, Icon icon) {
return findButtonByIcon(provider.getComponent(), icon);
}
diff --git a/Ghidra/Framework/Gui/src/main/java/generic/test/AbstractGuiTest.java b/Ghidra/Framework/Gui/src/main/java/generic/test/AbstractGuiTest.java
index 4bb0c92257..a40f245914 100644
--- a/Ghidra/Framework/Gui/src/main/java/generic/test/AbstractGuiTest.java
+++ b/Ghidra/Framework/Gui/src/main/java/generic/test/AbstractGuiTest.java
@@ -370,29 +370,6 @@ public class AbstractGuiTest extends AbstractGenericTest {
return null;
}
- public static List<Component> findComponentsByName(Container container, String componentName,
- boolean checkOwnedWindows) {
-
- List<Component> retList = new ArrayList<>();
-
- Component[] components = container.getComponents();
- for (Component component : components) {
- if (component == null) {
- continue;
- }
- String name = component.getName();
- if (name != null && name.equals(componentName)) {
- retList.add(component);
- }
- else if (component instanceof Container) {
- retList.addAll(
- findComponentsByName((Container) component, componentName, checkOwnedWindows));
- }
-
- }
- return retList;
- }
-
public static JButton findButtonByIcon(Container container, Icon icon) {
Component[] comps = container.getComponents();
for (Component element : comps) {
@@ -441,6 +418,18 @@ public class AbstractGuiTest extends AbstractGenericTest {
return null;
}
+ /**
+ * Searches the sub-components of the given container and returns the AbstractButton that has
+ * the specified name.
+ *
+ * @param container container to search
+ * @param name the button name
+ * @return null if the button was not found
+ */
+ public static AbstractButton findButtonByName(Container container, String name) {
+ return findAbstractButtonByName(container, name);
+ }
+
/**
* Searches the sub-components of the given container and returns the
* AbstractButton that has the specified text.
@@ -482,7 +471,6 @@ public class AbstractGuiTest extends AbstractGenericTest {
* @return null if the button was not found
*/
public static AbstractButton findAbstractButtonByName(Container container, String name) {
-
Component[] comp = container.getComponents();
for (Component element : comp) {
if ((element instanceof AbstractButton) &&