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:
dev747368 2020-02-06 20:04:30 -05:00
parent b6bea0fb39
commit e4372a30f1
15 changed files with 92 additions and 95 deletions

View File

@ -15,18 +15,14 @@
*/
package ghidra.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.util.BigEndianDataConverter;
import ghidra.util.DataConverter;
/**
*
@ -74,6 +70,10 @@ public class BigEndianConverterTest extends AbstractGhidraHeadedIntegrationTest
assertEquals(0x000102L, dc.getValue(b, 3));
assertEquals(0x0001020304050607L, dc.getValue(b, 8));
assertEquals(0x0001L, dc.getSignedValue(b, 2));
assertEquals(0x000102L, dc.getSignedValue(b, 3));
assertEquals(0x0001020304050607L, dc.getSignedValue(b, 8));
assertEquals(0x0203L, dc.getValue(b, 2, 2));
assertEquals(0x020304L, dc.getValue(b, 2, 3));
assertEquals(0x0203040506070809L, dc.getValue(b, 2, 8));
@ -82,15 +82,21 @@ public class BigEndianConverterTest extends AbstractGhidraHeadedIntegrationTest
assertEquals(0x04050607, dc.getBigInteger(b, 4, 4, true).intValue());
assertEquals(0x0405060708090a0bL, dc.getBigInteger(b, 4, 8, true).longValue());
BigInteger bint =
dc.getBigInteger(new byte[] { 0x01, 0x02, (byte) 0xff, 0x03 }, 2, 2, true);
BigInteger bint = dc.getBigInteger(bytes(0x01, 0x02, 0xff, 0x03), 2, 2, true);
assertEquals((short) 0xff03, bint.shortValue());// -253
assertEquals(0xffffff03, bint.intValue());
bint = dc.getBigInteger(new byte[] { 0x01, 0x02, (byte) 0xff, 0x03 }, 2, 2, false);
bint = dc.getBigInteger(bytes(0x01, 0x02, 0xff, 0x03), 2, 2, false);
assertEquals((short) 0xff03, bint.shortValue());
assertEquals(0x0000ff03, bint.intValue());
}
@Test
public void testGetSignedValues() {
assertEquals(Integer.MIN_VALUE, dc.getSignedValue(bytes(0x80, 00, 00, 00), 4));
assertEquals(-0x800000L, dc.getSignedValue(bytes(0x80, 00, 00, 00), 3));
assertEquals(-256, dc.getSignedValue(bytes(0xFF, 00, 00, 00), 2));
}
@Test

View File

@ -15,14 +15,12 @@
*/
package ghidra.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
@ -72,6 +70,10 @@ public class LittleEndianConverterTest extends AbstractGhidraHeadedIntegrationTe
assertEquals(0x020100L, dc.getValue(b, 3));
assertEquals(0x0706050403020100L, dc.getValue(b, 8));
assertEquals(0x0100L, dc.getSignedValue(b, 2));
assertEquals(0x020100L, dc.getSignedValue(b, 3));
assertEquals(0x0706050403020100L, dc.getSignedValue(b, 8));
assertEquals(0x0302L, dc.getValue(b, 2, 2));
assertEquals(0x040302L, dc.getValue(b, 2, 3));
assertEquals(0x0908070605040302L, dc.getValue(b, 2, 8));
@ -91,6 +93,14 @@ public class LittleEndianConverterTest extends AbstractGhidraHeadedIntegrationTe
}
@Test
public void testGetSignedValues() {
assertEquals(Integer.MIN_VALUE, dc.getSignedValue(bytes(0, 00, 00, 0x80), 4));
assertEquals(-0x800000L, dc.getSignedValue(bytes(0, 00, 0x80, 00), 3));
assertEquals(-256, dc.getSignedValue(bytes(0, 0xFF, 00, 00), 2));
}
@Test
public void testPut() {
byte[] b2 = new byte[12];

View File

@ -15,7 +15,7 @@
*/
package ghidra.program.database.code;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import org.junit.*;
@ -133,14 +133,6 @@ public class CodeManager64Test extends AbstractGenericTest {
}
private byte[] bytes(int... v) {
byte[] byteArray = new byte[v.length];
for (int i = 0; i < v.length; i++) {
byteArray[i] = (byte) v[i];
}
return byteArray;
}
private Address addr(long l) {
return space.getAddress(l);
}

View File

@ -638,14 +638,6 @@ public class CodeManagerTest extends AbstractGenericTest {
assertEquals(addr(0x0101), referencesFrom[0].getToAddress());
}
private byte[] bytes(int... v) {
byte[] byteArray = new byte[v.length];
for (int i = 0; i < v.length; i++) {
byteArray[i] = (byte) v[i];
}
return byteArray;
}
@Test
public void testGetDataAt() throws Exception {
listing.createData(addr(0x1740), DefaultDataType.dataType, 1);

View File

@ -325,6 +325,21 @@ public abstract class AbstractGTest {
return testName.getMethodName();
}
/**
* Friendly way to create an array of bytes with static values.
*
* @param unsignedBytes var-args list of unsigned byte values (ie. 0..255)
* @return array of bytes
*/
public static byte[] bytes(int... unsignedBytes) {
byte[] result = new byte[unsignedBytes.length];
for (int i = 0; i < unsignedBytes.length; i++) {
result[i] = (byte) unsignedBytes[i];
}
return result;
}
//==================================================================================================
// Wait Methods
//==================================================================================================

View File

@ -19,14 +19,19 @@ import java.io.Serializable;
import java.math.BigInteger;
/**
*
* Defines methods to convert Java numeric types to and from their
* raw form in a byte array.
* Stateless helper classes with static singleton instances that contain methods to convert
* Java numeric types to and from their raw form in a byte array.
* <p>
*
*/
public interface DataConverter extends Serializable {
/**
* Returns the correct DataConverter static instance for the requested endian-ness.
*
* @param isBigEndian boolean flag, true means big endian
* @return static DataConverter instance
*/
public static DataConverter getInstance(boolean isBigEndian) {
return isBigEndian ? BigEndianDataConverter.INSTANCE : LittleEndianDataConverter.INSTANCE;
}
@ -183,7 +188,6 @@ public interface DataConverter extends Serializable {
val = val << shiftBits;
val = val >> shiftBits;
return val;
}
/**
@ -271,6 +275,7 @@ public interface DataConverter extends Serializable {
*
* @param b array to contain the bytes
* @param value the short value
* @throws IndexOutOfBoundsException if byte array is too small to hold the value
*/
default void putShort(byte[] b, short value) {
putShort(b, 0, value);
@ -282,10 +287,10 @@ public interface DataConverter extends Serializable {
* @param b array to contain the bytes
* @param offset the offset into the byte array to store the value
* @param value the short value
* @throws IndexOutOfBoundsException if offset is too large or byte array
* is too small to hold the value
*/
default void putShort(byte[] b, int offset, short value) {
getBytes(value, b, offset);
}
void putShort(byte[] b, int offset, short value);
/**
* Writes a int value into a byte array.
@ -294,6 +299,7 @@ public interface DataConverter extends Serializable {
*
* @param b array to contain the bytes
* @param value the int value
* @throws IndexOutOfBoundsException if byte array is too small to hold the value
*/
default void putInt(byte[] b, int value) {
putInt(b, 0, value);
@ -307,6 +313,8 @@ public interface DataConverter extends Serializable {
* @param b array to contain the bytes
* @param offset the offset into the byte array to store the value
* @param value the int value
* @throws IndexOutOfBoundsException if offset is too large or byte array
* is too small to hold the value
*/
void putInt(byte[] b, int offset, int value);
@ -317,6 +325,7 @@ public interface DataConverter extends Serializable {
*
* @param b array to contain the bytes
* @param value the long value
* @throws IndexOutOfBoundsException if byte array is too small to hold the value
*/
default void putLong(byte[] b, long value) {
putLong(b, 0, value);
@ -330,6 +339,8 @@ public interface DataConverter extends Serializable {
* @param b array to contain the bytes
* @param offset the offset into the byte array to store the value
* @param value the long value
* @throws IndexOutOfBoundsException if offset is too large or byte array
* is too small to hold the value
*/
default void putLong(byte[] b, int offset, long value) {
putValue(value, Long.BYTES, b, offset);
@ -356,6 +367,7 @@ public interface DataConverter extends Serializable {
* @param b array to contain the bytes at offset 0
* @param size number of bytes to be written
* @param value BigInteger value to convert
* @throws IndexOutOfBoundsException if byte array is less than specified size
*/
default void putBigInteger(byte[] b, int size, BigInteger value) {
putBigInteger(b, 0, size, value);
@ -370,6 +382,7 @@ public interface DataConverter extends Serializable {
* @param offset the offset into the byte array to store the value
* @param size number of bytes to be written
* @param value BigInteger value to convert
* @throws IndexOutOfBoundsException if (offset+size)&gt;b.length
*/
public void putBigInteger(byte[] b, int offset, int size, BigInteger value);

View File

@ -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

View File

@ -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;

View File

@ -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;
}

View File

@ -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() {

View File

@ -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);
}
}

View File

@ -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() {

View File

@ -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);

View File

@ -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() {

View File

@ -80,14 +80,6 @@ public class ViewStringsPluginScreenShots extends GhidraScreenShotGenerator {
}
private static byte[] bytes(int... intValues) {
byte[] result = new byte[intValues.length];
for (int i = 0; i < intValues.length; i++) {
result[i] = (byte) (intValues[i]);
}
return result;
}
@Test
public void testDefined_String_Table() {
ViewStringsProvider provider = showProvider(ViewStringsProvider.class);