mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 18:29:37 +02:00
GT-3512 refactor tests to use bytes() helper method, javadoc.
Allows getting rid of out-of-place ByteMemBufferImpl ctor. Fix javadoc
This commit is contained in:
parent
b6bea0fb39
commit
e4372a30f1
15 changed files with 92 additions and 95 deletions
|
@ -44,22 +44,6 @@ public class ByteMemBufferImpl implements MemBuffer {
|
|||
this.converter = GhidraDataConverter.getInstance(isBigEndian);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience constructor using varargs for specifying byte values.
|
||||
* @param addr the address to associate with the bytes
|
||||
* @param isBigEndian true for BigEndian, false for LittleEndian.
|
||||
* @param byteValues varargs for specifying the individual byte values. The int argument
|
||||
* will be truncated to a byte value.
|
||||
*/
|
||||
public ByteMemBufferImpl(Address addr, boolean isBigEndian, int... byteValues) {
|
||||
this.addr = addr;
|
||||
this.converter = GhidraDataConverter.getInstance(isBigEndian);
|
||||
bytes = new byte[byteValues.length];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bytes[i] = (byte) byteValues[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of bytes contained within buffer
|
||||
* @return byte count
|
||||
|
|
|
@ -22,6 +22,12 @@ import ghidra.program.model.mem.MemoryAccessException;
|
|||
|
||||
public interface GhidraDataConverter extends DataConverter {
|
||||
|
||||
/**
|
||||
* Returns the correct GhidraDataConverter static instance for the requested endian-ness.
|
||||
*
|
||||
* @param isBigEndian boolean flag, true means big endian
|
||||
* @return static GhidraDataConverter instance
|
||||
*/
|
||||
public static GhidraDataConverter getInstance(boolean isBigEndian) {
|
||||
return isBigEndian ? GhidraBigEndianDataConverter.INSTANCE
|
||||
: GhidraLittleEndianDataConverter.INSTANCE;
|
||||
|
|
|
@ -15,21 +15,19 @@
|
|||
*/
|
||||
package ghidra.program.model.data;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import generic.test.AbstractGTest;
|
||||
import ghidra.program.model.mem.ByteMemBufferImpl;
|
||||
import ghidra.util.LittleEndianDataConverter;
|
||||
|
||||
public class FloatDataTypeTest extends AbstractGTest {
|
||||
|
||||
private byte[] getBytes(long value, int size) {
|
||||
byte[] bytes = new byte[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
bytes[i] = (byte) value;
|
||||
value >>= 8;
|
||||
}
|
||||
LittleEndianDataConverter.INSTANCE.getBytes(value, size, bytes, 0);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,12 +26,8 @@ import ghidra.program.model.mem.ByteMemBufferImpl;
|
|||
|
||||
public class ArrayStringableTest extends AbstractGTest {
|
||||
private ByteMemBufferImpl mb(boolean isBE, int... values) {
|
||||
byte[] bytes = new byte[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
bytes[i] = (byte) values[i];
|
||||
}
|
||||
GenericAddressSpace gas = new GenericAddressSpace("test", 32, AddressSpace.TYPE_RAM, 1);
|
||||
return new ByteMemBufferImpl(gas.getAddress(0), bytes, isBE);
|
||||
return new ByteMemBufferImpl(gas.getAddress(0), bytes(values), isBE);
|
||||
}
|
||||
|
||||
private SettingsBuilder newset() {
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
*/
|
||||
package ghidra.program.model.data;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -157,8 +157,9 @@ public class BitFieldDataTypeTest extends AbstractGTest {
|
|||
assertEquals("5", getDecimalRepresentation(unsignedBitField(4, 0), 0x55));
|
||||
}
|
||||
|
||||
private String getRepresentation(BitFieldDataType bitField, int... bytes) throws Exception {
|
||||
MemBuffer membuf = membuf(bytes);
|
||||
private String getRepresentation(BitFieldDataType bitField, int... unsignedBytes)
|
||||
throws Exception {
|
||||
MemBuffer membuf = membuf(unsignedBytes);
|
||||
return bitField.getRepresentation(membuf, null, 4);
|
||||
}
|
||||
|
||||
|
@ -184,8 +185,8 @@ public class BitFieldDataTypeTest extends AbstractGTest {
|
|||
return new BitFieldDataType(UnsignedIntegerDataType.dataType, size, offset);
|
||||
}
|
||||
|
||||
private MemBuffer membuf(int... bytes) throws Exception {
|
||||
return new ByteMemBufferImpl(null, true, bytes);
|
||||
private MemBuffer membuf(int... unsignedBytes) throws Exception {
|
||||
return new ByteMemBufferImpl(null, bytes(unsignedBytes), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -36,12 +36,8 @@ import ghidra.program.model.mem.ByteMemBufferImpl;
|
|||
public class CharDataTypesRenderTest extends AbstractGTest {
|
||||
|
||||
private ByteMemBufferImpl mb(boolean isBE, int... values) {
|
||||
byte[] bytes = new byte[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
bytes[i] = (byte) values[i];
|
||||
}
|
||||
GenericAddressSpace gas = new GenericAddressSpace("test", 32, AddressSpace.TYPE_RAM, 1);
|
||||
return new ByteMemBufferImpl(gas.getAddress(0), bytes, isBE);
|
||||
return new ByteMemBufferImpl(gas.getAddress(0), bytes(values), isBE);
|
||||
}
|
||||
|
||||
private SettingsBuilder newset() {
|
||||
|
|
|
@ -29,53 +29,53 @@ public class Float10DataTypeTest extends AbstractGTest {
|
|||
@Test
|
||||
public void testGetValue() {
|
||||
|
||||
byte[] bytes = new byte[] { 0x7f, (byte) 0xff, 0, 0, 0, 0, 0, 0, 0, 0 }; // 0x7fff0000000000000000 = +infinity
|
||||
byte[] bytes = bytes(0x7f, 0xff, 0, 0, 0, 0, 0, 0, 0, 0); // 0x7fff0000000000000000 = +infinity
|
||||
Object value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, value);
|
||||
|
||||
bytes = new byte[] { (byte) 0xff, (byte) 0xff, 0, 0, 0, 0, 0, 0, 0, 0 }; // 0xffff0000000000000000 = -infinity
|
||||
bytes = bytes(0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0); // 0xffff0000000000000000 = -infinity
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, value);
|
||||
|
||||
bytes = new byte[] { (byte) 0x7f, (byte) 0xff, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 }; // 0x7fff8000000000000000 = NaN
|
||||
bytes = bytes(0x7f, 0xff, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x7fff8000000000000000 = NaN
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals(FloatFormat.BIG_NaN, value);
|
||||
|
||||
// Really small values
|
||||
|
||||
bytes = new byte[] { 0, 1, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 }; // 0x00018000000000000000 = approaches 0
|
||||
bytes = bytes(0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x00018000000000000000 = approaches 0
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals("5.04315471466814026E-4932", value.toString());
|
||||
|
||||
bytes = new byte[] { (byte) 0x80, 1, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 }; // 0x00018000000000000000 = approaches 0
|
||||
bytes = bytes(0x80, 1, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x00018000000000000000 = approaches 0
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals("-5.04315471466814026E-4932", value.toString());
|
||||
|
||||
// Really big values
|
||||
|
||||
bytes = new byte[] { (byte) 0x7f, (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 }; // 0x7ffe8000000000000000 = approaches +infinity
|
||||
bytes = bytes(0x7f, 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x7ffe8000000000000000 = approaches +infinity
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals("8.92298621517923824E+4931", value.toString());
|
||||
|
||||
bytes = new byte[] { (byte) 0xff, (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 }; // 0x7ffe8000000000000000 = approaches -infinity
|
||||
bytes = bytes(0xff, 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x7ffe8000000000000000 = approaches -infinity
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals("-8.92298621517923824E+4931", value.toString());
|
||||
|
||||
// Values within the range of Double
|
||||
|
||||
bytes = new byte[] { 0x40, 1, 0x20, 0, 0, 0, 0, 0, 0, 0 }; // 0x40002000000000000000 = approaches -infinity
|
||||
bytes = bytes(0x40, 1, 0x20, 0, 0, 0, 0, 0, 0, 0); // 0x40002000000000000000 = approaches -infinity
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals(BigDecimal.valueOf(4.5), value);
|
||||
|
||||
bytes = new byte[] { (byte) 0xc0, 1, 0x20, 0, 0, 0, 0, 0, 0, 0 }; // 0x40002000000000000000 = approaches -infinity
|
||||
bytes = bytes(0xc0, 1, 0x20, 0, 0, 0, 0, 0, 0, 0); // 0x40002000000000000000 = approaches -infinity
|
||||
value =
|
||||
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
|
||||
Assert.assertEquals(BigDecimal.valueOf(-4.5), value);
|
||||
|
|
|
@ -59,12 +59,8 @@ public class StringDataTypeTest extends AbstractGTest {
|
|||
}
|
||||
|
||||
private ByteMemBufferImpl mb(boolean isBE, int... values) {
|
||||
byte[] bytes = new byte[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
bytes[i] = (byte) values[i];
|
||||
}
|
||||
GenericAddressSpace gas = new GenericAddressSpace("test", 32, AddressSpace.TYPE_RAM, 1);
|
||||
return new ByteMemBufferImpl(gas.getAddress(0), bytes, isBE);
|
||||
return new ByteMemBufferImpl(gas.getAddress(0), bytes(values), isBE);
|
||||
}
|
||||
|
||||
private SettingsBuilder newset() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue