Corrected test fallout from Memory changes

This commit is contained in:
ghidra1 2020-05-15 10:37:58 -04:00
parent cee8a138c0
commit 7385544bed
11 changed files with 93 additions and 59 deletions

View file

@ -360,13 +360,16 @@ class AddBlockModel {
}
private boolean hasMappedAddressIfNeeded() {
if (blockType == MemoryBlockType.BIT_MAPPED || blockType == MemoryBlockType.BYTE_MAPPED) {
if (baseAddr == null) {
String blockTypeStr =
(blockType == MemoryBlockType.BIT_MAPPED) ? "bit-mapped" : "byte-mapped";
message = "Please enter a source address for the " + blockTypeStr + " block";
return false;
}
if (blockType != MemoryBlockType.BIT_MAPPED && blockType != MemoryBlockType.BYTE_MAPPED) {
return true;
}
if (baseAddr == null) {
String blockTypeStr =
(blockType == MemoryBlockType.BIT_MAPPED) ? "bit-mapped" : "byte-mapped";
message = "Please enter a source address for the " + blockTypeStr + " block";
return false;
}
if (blockType == MemoryBlockType.BYTE_MAPPED) {
if (schemeDestByteCount <= 0 || schemeDestByteCount > Byte.MAX_VALUE ||
schemeSrcByteCount <= 0 || schemeSrcByteCount > Byte.MAX_VALUE) {
message = "Mapping ratio values must be within range: 1 to 127";

View file

@ -267,7 +267,7 @@ class MemoryMapModel extends AbstractSortedTableModel<MemoryBlock> {
if (name.equals(block.getName())) {
break;
}
if (Memory.isValidAddressSpaceName(name)) {
if (!Memory.isValidAddressSpaceName(name)) {
Msg.showError(this, provider.getComponent(), "Invalid Name",
"Invalid Memory Block Name: " + name);
break;

View file

@ -112,10 +112,16 @@ public class MemoryBlockStartFieldFactory extends FieldFactory {
}
CodeUnit cu = (CodeUnit) proxyObject;
String[] comments;
List<AttributedString> attributedStrings = createBlockStartText(cu);
String[] comments = new String[attributedStrings.size()];
for (int i = 0; i < comments.length; i++) {
comments[i] = attributedStrings.get(i).getText();
if (attributedStrings == null) {
comments = new String[0];
}
else {
comments = new String[attributedStrings.size()];
for (int i = 0; i < comments.length; i++) {
comments[i] = attributedStrings.get(i).getText();
}
}
return new MemoryBlockStartFieldLocation(cu.getProgram(), cu.getMinAddress(), null, row,

View file

@ -332,6 +332,7 @@ public class ProgramBuilder {
// can't happen
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Exception building memory", e);
}
endTransaction();
@ -419,7 +420,8 @@ public class ProgramBuilder {
startTransaction();
MemoryBlock block = program.getMemory().getBlock(address);
if (block == null) {
createMemory("Block_" + stringAddress, stringAddress, bytes.length);
createMemory("Block_" + stringAddress.toString().replace(':', '_'), stringAddress,
bytes.length);
}
Memory memory = program.getMemory();

View file

@ -268,36 +268,38 @@ public class MemoryMapProvider1Test extends AbstractGhidraHeadedIntegrationTest
assertEquals(".test", model.getValueAt(0, MemoryMapModel.NAME));
}
@Test
public void testDuplicateName() throws Exception {
table.addRowSelectionInterval(0, 0);
Rectangle rect = table.getCellRect(0, MemoryMapModel.NAME, true);
clickMouse(table, 1, rect.x, rect.y, 2, 0);
waitForPostedSwingRunnables();
SwingUtilities.invokeLater(() -> {
int row = 0;
TableCellEditor editor = table.getCellEditor(row, MemoryMapModel.NAME);
Component c = editor.getTableCellEditorComponent(table,
model.getValueAt(row, MemoryMapModel.NAME), true, row, MemoryMapModel.NAME);
JTextField tf = (JTextField) c;
tf.setText(".data");
editor.stopCellEditing();
});
waitForPostedSwingRunnables();
assertEquals(".text", model.getValueAt(0, MemoryMapModel.NAME));
final OptionDialog d =
waitForDialogComponent(tool.getToolFrame(), OptionDialog.class, 2000);
assertNotNull(d);
String msg = findMessage(d.getComponent());
assertNotNull(msg);
assertEquals("Block named .data already exists.", msg);
SwingUtilities.invokeAndWait(() -> d.close());
}
// Test Eliminated - Memory API allows duplicate names which is a common occurance
// with import formats such as ELF
//
// public void testDuplicateName() throws Exception {
// table.addRowSelectionInterval(0, 0);
// Rectangle rect = table.getCellRect(0, MemoryMapModel.NAME, true);
// clickMouse(table, 1, rect.x, rect.y, 2, 0);
// waitForPostedSwingRunnables();
//
// SwingUtilities.invokeLater(() -> {
// int row = 0;
// TableCellEditor editor = table.getCellEditor(row, MemoryMapModel.NAME);
// Component c = editor.getTableCellEditorComponent(table,
// model.getValueAt(row, MemoryMapModel.NAME), true, row, MemoryMapModel.NAME);
// JTextField tf = (JTextField) c;
//
// tf.setText(".data");
// editor.stopCellEditing();
// });
// waitForPostedSwingRunnables();
// assertEquals(".text", model.getValueAt(0, MemoryMapModel.NAME));
//
// final OptionDialog d =
// waitForDialogComponent(tool.getToolFrame(), OptionDialog.class, 2000);
//
// assertNotNull(d);
// String msg = findMessage(d.getComponent());
// assertNotNull(msg);
// assertEquals("Block named .data already exists.", msg);
// SwingUtilities.invokeAndWait(() -> d.close());
//
// }
@Test
public void testEditComment() throws Exception {

View file

@ -243,7 +243,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {
public void testWildcardInBlock() throws Exception {
loadProgram("x86");
MemoryBlock block = createOverlay("Test Overlay", "1002000", 100);
MemoryBlock block = createOverlay("TestOverlay", "1002000", 100);
String name = block.getName();
AddLabelCmd cmd = new AddLabelCmd(addr(name + "::1002000"), "Bob", SourceType.USER_DEFINED);
@ -252,7 +252,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {
// try a wildcard for an address in the new block (this gets us an extra code path tested)
setText(name + "::*ob");
performOkCallback();
assertEquals(addr("Test Overlay::1002000"), cbPlugin.getCurrentAddress());
assertEquals(addr("TestOverlay::1002000"), cbPlugin.getCurrentAddress());
}
@Test
@ -272,7 +272,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {
// queries an address has multiple matches.
//
loadProgram("x86");
createOverlay("Test Overlay", "1002000", 100);
createOverlay("TestOverlay", "1002000", 100);
assumeCurrentAddressSpace(false);
showDialog();
setText("1002000");
@ -289,7 +289,7 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {
// addresses is off *and* the current address space has a matching address.
//
loadProgram("x86");
createOverlay("Test Overlay", "1002000", 100);
createOverlay("TestOverlay", "1002000", 100);
//
// Turn off the option to show all addresses when there is a match in our current
@ -311,8 +311,8 @@ public class GoToPluginTest extends AbstractGhidraHeadedIntegrationTest {
// option to always show all addresses is off.
//
loadProgram("x86");
createOverlay("Test Overlay 1", "1002000", 100);
MemoryBlock overlay2Block = createOverlay("Test Overlay 2", "1003000", 100);
createOverlay("TestOverlay1", "1002000", 100);
MemoryBlock overlay2Block = createOverlay("TestOverlay2", "1003000", 100);
//
// Put us in an address space that does not have a match for the query address

View file

@ -95,6 +95,7 @@ public class BitMappedMemoryBlockTest extends AbstractGhidraHeadedIntegrationTes
Assert.fail("Should not have gotten a byte");
}
catch (MemoryAccessException e) {
// expected
}
}

View file

@ -351,4 +351,21 @@ public class ByteMappedMemoryBlockTest extends AbstractGhidraHeadedIntegrationTe
assertEquals(4, block2.getBytes(addr(0x100), data2));
assertTrue(Arrays.equals(new byte[] { 1, 2, -3, -4 }, data2));
}
@Test
public void testNoUnderlyingMemory() throws Exception {
MemoryBlock byteMappedBlock = memory.createByteMappedBlock("BYTE_BLOCK", addr(0x1000),
addr(0x1020), 0x10, new ByteMappingScheme(1, 1), false);
Address addr = addr(0x1040);
MemoryBlock newblock = memory.createBlock(byteMappedBlock, "Test", addr, 0x20);
try {
newblock.getByte(addr);
Assert.fail("Should not have gotten a byte");
}
catch (MemoryAccessException e) {
// expected
}
}
}

View file

@ -163,7 +163,7 @@ public class MemoryBlockDB implements MemoryBlock {
if (oldName.equals(name)) {
return;
}
memMap.checkBlockName(name);
memMap.checkBlockName(name, isOverlay());
try {
if (isOverlay()) {
memMap.overlayBlockRenamed(oldName, name);

View file

@ -597,7 +597,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
public MemoryBlock createInitializedBlock(String name, Address start, InputStream is,
long length, TaskMonitor monitor, boolean overlay) throws MemoryConflictException,
AddressOverflowException, CancelledException, LockException, DuplicateNameException {
checkBlockName(name);
checkBlockName(name, overlay);
lock.acquire();
try {
checkBlockSize(length, true);
@ -643,7 +643,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
long offset, long length, boolean overlay) throws LockException, DuplicateNameException,
MemoryConflictException, AddressOverflowException, IndexOutOfBoundsException {
checkBlockName(name);
checkBlockName(name, overlay);
lock.acquire();
try {
checkBlockSize(length, true);
@ -696,7 +696,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
boolean overlay) throws MemoryConflictException, AddressOverflowException,
LockException, DuplicateNameException {
checkBlockName(name);
checkBlockName(name, overlay);
lock.acquire();
try {
checkBlockSize(size, false);
@ -732,7 +732,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
long length, boolean overlay) throws MemoryConflictException, AddressOverflowException,
LockException, IllegalArgumentException, DuplicateNameException {
checkBlockName(name);
checkBlockName(name, overlay);
lock.acquire();
try {
checkBlockSize(length, false);
@ -768,7 +768,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
throws MemoryConflictException, AddressOverflowException, LockException,
DuplicateNameException {
checkBlockName(name);
checkBlockName(name, overlay);
int mappingScheme = 0; // use for 1:1 mapping
if (byteMappingScheme == null) {
@ -810,16 +810,17 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
/**
* Check new block name for validity
* @param name new block name
* @param isOverlay true if block is overlay
* @throws IllegalArgumentException if invalid block name specified
* @throws DuplicateNameException if name conflicts with an address space name
*/
void checkBlockName(
String name)
String name, boolean isOverlay)
throws IllegalArgumentException, DuplicateNameException {
if (!Memory.isValidAddressSpaceName(name)) {
throw new IllegalArgumentException("Invalid block name: " + name);
}
if (getAddressFactory().getAddressSpace(name) != null) {
if (isOverlay && getAddressFactory().getAddressSpace(name) != null) {
throw new DuplicateNameException(
"Block name conflicts with existing address space: " + name);
}
@ -829,7 +830,7 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
public MemoryBlock createBlock(MemoryBlock block, String name, Address start, long length)
throws MemoryConflictException, AddressOverflowException, LockException,
DuplicateNameException {
checkBlockName(name);
checkBlockName(name, false);
lock.acquire();
try {
checkBlockSize(length, block.isInitialized());
@ -840,7 +841,9 @@ public class MemoryMapDB implements Memory, ManagerDB, LiveMemoryListener {
int mappingScheme = 0;
if (block.isMapped()) {
MemoryBlockSourceInfo info = block.getSourceInfos().get(0);
mappingScheme = info.getByteMappingScheme().get().getEncodedMappingScheme();
if (block.getType() == MemoryBlockType.BYTE_MAPPED) {
mappingScheme = info.getByteMappingScheme().get().getEncodedMappingScheme();
}
mappedAddr = info.getMappedRange().get().getMinAddress();
}
MemoryBlockDB newBlock = adapter.createBlock(block.getType(), name, start, length,

View file

@ -826,7 +826,7 @@ public interface Memory extends AddressSetView {
}
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c < 0x20 || c >= 0x7f || c == ':') {
if (c <= 0x20 || c >= 0x7f || c == ':') {
return false;
}
}