BitFields - added preliminary support for composite bitfields

This commit is contained in:
ghidra1 2019-03-26 15:23:27 -04:00
parent c23ae691e2
commit a7345527c9
209 changed files with 18617 additions and 6720 deletions

View file

@ -22,14 +22,10 @@ import java.util.Collection;
import org.junit.Test;
import generic.test.AbstractGenericTest;
import generic.test.AbstractGTest;
import ghidra.program.database.data.DataTypeUtilities;
public class DataTypeUtilitiesTest extends AbstractGenericTest {
public DataTypeUtilitiesTest() {
super();
}
public class DataTypeUtilitiesTest extends AbstractGTest {
@Test
public void testGetContainedDataTypes() {

View file

@ -22,11 +22,11 @@ import java.io.StringWriter;
import org.junit.*;
import generic.test.AbstractGenericTest;
import generic.test.AbstractGTest;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitorAdapter;
public class DataTypeWriterTest extends AbstractGenericTest {
public class DataTypeWriterTest extends AbstractGTest {
private static String EOL = System.getProperty("line.separator");

View file

@ -21,53 +21,36 @@ import java.util.NoSuchElementException;
import org.junit.*;
import generic.test.AbstractGenericTest;
import generic.test.AbstractGTest;
import ghidra.util.task.TaskMonitorAdapter;
/**
* Tests for Enum data types.
*/
public class EnumTest extends AbstractGenericTest {
public class EnumTest extends AbstractGTest {
private DataTypeManager dataMgr;
private int transactionID;
/**
* Constructor for EnumTest.
* @param arg0
*/
public EnumTest() {
super();
@Before
public void setUp() throws Exception {
dataMgr = new StandAloneDataTypeManager("Test");
dataMgr.startTransaction("");
}
/*
* @see TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
dataMgr = new StandAloneDataTypeManager("Test");
transactionID = dataMgr.startTransaction("");
}
@After
public void tearDown() {
dataMgr.endTransaction(transactionID, true);
dataMgr.close();
}
@Test
public void testCreateEnum() throws Exception {
@Test
public void testCreateEnum() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 0);
enumm.add("Green", 1);
enumm.add("Blue", 2);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
assertNotNull(enummDT);
assertEquals("Color", enummDT.getName());
assertEquals(0, enummDT.getValue("Red"));
assertEquals(1, enummDT.getValue("Green"));
@ -75,56 +58,59 @@ public class EnumTest extends AbstractGenericTest {
assertEquals(1, enummDT.getLength());
assertEquals(3, enummDT.getCount());
assertTrue(enumm.isEquivalent(enummDT));
assertTrue(enumm.isEquivalent(enummDT));
assertTrue(enummDT.isEquivalent(enumm));
assertEquals(c.getCategoryPath(), enummDT.getCategoryPath());
assertNotNull(c.getDataType("Color"));
}
@Test
public void testRemoveValue() throws Exception {
@Test
public void testRemoveValue() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 0);
enumm.add("Green", 1);
enumm.add("Blue", 2);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
assertEquals(3, enummDT.getCount());
enummDT.remove("Green");
assertEquals(2, enummDT.getCount());
}
@Test
public void testAddValue() throws Exception {
@Test
public void testAddValue() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 0);
enumm.add("Green", 1);
enumm.add("Blue", 2);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
enummDT.add("Purple", 7);
assertEquals(4, enummDT.getCount());
assertEquals(7, enummDT.getValue("Purple"));
}
@Test
public void testEditValue() throws Exception {
@Test
public void testEditValue() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
enummDT.remove("Blue");
assertEquals(2, enummDT.getCount());
enummDT.add("Blue", 30);
@ -132,100 +118,101 @@ public class EnumTest extends AbstractGenericTest {
assertEquals("Blue", enummDT.getName(30));
assertNull(enummDT.getName(20));
}
@Test
public void testCloneRetain() throws Exception {
@Test
public void testCloneRetain() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum copyDT = (Enum)enummDT.clone(null);
Enum copyDT = (Enum) enummDT.clone(null);
assertNotNull(copyDT);
Enum c2 = (Enum)root.addDataType(copyDT, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum c2 = (Enum) root.addDataType(copyDT, DataTypeConflictHandler.DEFAULT_HANDLER);
assertNotNull(c2);
assertTrue(copyDT.isEquivalent(c2));
}
@Test
public void testCopyDontRetain() throws Exception {
@Test
public void testCopyDontRetain() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum copyDT = (Enum)enummDT.copy(null);
Enum copyDT = (Enum) enummDT.copy(null);
assertNotNull(copyDT);
Enum c2 = (Enum)root.addDataType(copyDT, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum c2 = (Enum) root.addDataType(copyDT, DataTypeConflictHandler.DEFAULT_HANDLER);
assertNotNull(c2);
assertTrue(copyDT.isEquivalent(c2));
}
@Test
public void testRemoveEnum() throws Exception {
@Test
public void testRemoveEnum() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
assertNotNull(enummDT);
c.remove(enummDT, TaskMonitorAdapter.DUMMY_MONITOR);
assertNull(c.getDataType("Color"));
assertTrue(enummDT.isDeleted());
}
@Test
public void testMoveEnum() throws Exception {
@Test
public void testMoveEnum() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
root.moveDataType(enummDT, null);
assertNotNull(root.getDataType(enumm.getName()));
assertNull(c.getDataType(enumm.getName()));
}
@Test
public void testResolve() throws Exception {
@Test
public void testResolve() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Enum enummDT = (Enum)dataMgr.resolve(enumm, null);
Enum enummDT = (Enum) dataMgr.resolve(enumm, null);
assertNotNull(enummDT);
long id = dataMgr.getResolvedID(enummDT);
assertEquals(enummDT, dataMgr.getDataType(id));
assertEquals(enummDT, dataMgr.getDataType(id));
}
@Test
public void testReplace() throws Exception {
@Test
public void testReplace() throws Exception {
Enum enumm = new EnumDataType("Color", 1);
enumm.add("Red", 10);
enumm.add("Green", 15);
enumm.add("Blue", 20);
Category root = dataMgr.getRootCategory();
Category c = root.createCategory("enumms");
Enum enummDT = (Enum)c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum enummDT = (Enum) c.addDataType(enumm, DataTypeConflictHandler.DEFAULT_HANDLER);
Enum myEnum = new EnumDataType("my enumm", 1);
myEnum.add("My red", 0);
myEnum.add("My Green", 5);
@ -233,39 +220,22 @@ public class EnumTest extends AbstractGenericTest {
myEnum.add("Purple", 10);
enummDT.replaceWith(myEnum);
assertEquals(4, enummDT.getCount());
long[] values = enummDT.getValues();
assertEquals(4, values.length);
assertEquals(0, values[0]);
assertEquals(5, values[1]);
assertEquals(10, values[2]);
assertEquals(25, values[3]);
try {
try {
enummDT.getValue("Red");
Assert.fail("Should have gotten no such element exception!");
} catch (NoSuchElementException e) {
}
catch (NoSuchElementException e) {
}
}
// private class DomainObjListener implements DomainObjectListener {
// private int count;
//
// /* (non-Javadoc)
// * @see ghidra.framework.model.DomainObjectListener#domainObjectChanged(ghidra.framework.model.DomainObjectChangedEvent)
// */
// public void domainObjectChanged(DomainObjectChangedEvent ev) {
// for (int i=0; i<ev.numRecords(); i++) {
// DomainObjectChangeRecord rec = ev.getChangeRecord(i);
// if (rec.getEventType() == ChangeManager.DOCR_DATA_TYPE_CHANGED) {
// ++count;
// }
// }
// }
// int getCount() {
// return count;
// }
// }
}

View file

@ -29,10 +29,6 @@ public class FileDataTypeManagerTest extends AbstractGenericTest {
private File testArchiveFile;
public FileDataTypeManagerTest() {
super();
}
@Before
public void setUp() throws Exception {
testArchiveFile =
@ -74,7 +70,7 @@ public class FileDataTypeManagerTest extends AbstractGenericTest {
dtMgr = FileDataTypeManager.openFileArchive(testArchiveFile, false);
assertFalse(dtMgr.isUpdatable());
ArrayList<DataType> list = new ArrayList<DataType>();
ArrayList<DataType> list = new ArrayList<>();
dtMgr.getAllDataTypes(list);
int size = list.size();
@ -86,8 +82,9 @@ public class FileDataTypeManagerTest extends AbstractGenericTest {
"\n");
}
Assert.fail("Did not get exptected data types of byte, Typdef and Typedef. Instead found:\n" +
buffy.toString());
Assert.fail(
"Did not get exptected data types of byte, Typdef and Typedef. Instead found:\n" +
buffy.toString());
}
assertTrue(dt1.isEquivalent(dtMgr.getDataType(CategoryPath.ROOT, "T1")));
@ -148,7 +145,7 @@ public class FileDataTypeManagerTest extends AbstractGenericTest {
dtMgr = FileDataTypeManager.openFileArchive(testArchiveFile, false);
assertFalse(dtMgr.isUpdatable());
ArrayList<DataType> list = new ArrayList<DataType>();
ArrayList<DataType> list = new ArrayList<>();
dtMgr.getAllDataTypes(list);
assertEquals(13, list.size());

View file

@ -19,18 +19,10 @@ import static org.junit.Assert.assertEquals;
import org.junit.Test;
import generic.test.AbstractGenericTest;
import generic.test.AbstractGTest;
import ghidra.program.model.mem.ByteMemBufferImpl;
public class FloatDataTypeTest extends AbstractGenericTest {
/**
* Constructor for LongDoubleDataTypeTest.
* @param arg0
*/
public FloatDataTypeTest() {
super();
}
public class FloatDataTypeTest extends AbstractGTest {
private byte[] getBytes(long value, int size) {
byte[] bytes = new byte[size];
@ -41,8 +33,8 @@ public class FloatDataTypeTest extends AbstractGenericTest {
return bytes;
}
@Test
public void testFloat4Extremes() {
@Test
public void testFloat4Extremes() {
int bits = Float.floatToRawIntBits(Float.NaN);
byte[] bytes = getBytes(bits, 4);
@ -94,8 +86,8 @@ public class FloatDataTypeTest extends AbstractGenericTest {
}
@Test
public void testFloat8Extremes() {
@Test
public void testFloat8Extremes() {
long bits = Double.doubleToRawLongBits(Double.NaN);
byte[] bytes = getBytes(bits, 8);

View file

@ -19,44 +19,24 @@ import static org.junit.Assert.*;
import org.junit.*;
import generic.test.AbstractGenericTest;
import generic.test.AbstractGTest;
import ghidra.docking.settings.SettingsImpl;
public class FunctionDefinitionDataTypeTest extends AbstractGenericTest {
public class FunctionDefinitionDataTypeTest extends AbstractGTest {
private StandAloneDataTypeManager dtm;
private FunctionDefinition functionDt;
private int transactionID;
private FunctionDefinition createFunctionDefinition(String name) {
return (FunctionDefinition) dtm.resolve(new FunctionDefinitionDataType(name), null);
}
/**
* Constructor for FunctionDefinitionDataTypeTest.
*/
public FunctionDefinitionDataTypeTest() {
super();
}
/*
* @see TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
dtm = new StandAloneDataTypeManager("dummyDTM");
transactionID = dtm.startTransaction("");
dtm.startTransaction("");
functionDt = createFunctionDefinition("Test");
}
/*
* @see TestCase#tearDown()
*/
@After
public void tearDown() throws Exception {
dtm.endTransaction(transactionID, true);
dtm.close();
}
@Test
public void testConstructor_WithName() {
FunctionDefinitionDataType impl;

View file

@ -1,969 +0,0 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
*
*/
package ghidra.program.model.data;
import static org.junit.Assert.*;
import org.junit.*;
import generic.test.AbstractGTest;
/**
*
*/
public class StructureTest extends AbstractGTest {
private Structure struct;
/**
* @param arg0
*/
public StructureTest() {
super();
}
private Structure createStructure(String name, int length) {
return new StructureDataType(name, length);
}
private Union createUnion(String name) {
return new UnionDataType(name);
}
private TypeDef createTypeDef(DataType dataType) {
return new TypedefDataType(dataType.getName() + "TypeDef", dataType);
}
private Array createArray(DataType dataType, int numElements) {
return new ArrayDataType(dataType, numElements, dataType.getLength());
}
private Pointer createPointer(DataType dataType, int length) {
return new PointerDataType(dataType, length);
}
@Before
public void setUp() throws Exception {
struct = createStructure("TestStruct", 0);
struct.add(new ByteDataType(), "field1", "Comment1");
struct.add(new WordDataType(), null, "Comment2");
struct.add(new DWordDataType(), "field3", null);
struct.add(new ByteDataType(), "field4", "Comment4");
}
@Test
public void testAdd() throws Exception {
assertEquals(8, struct.getLength());
assertEquals(4, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(0);
assertEquals(0, dtc.getOffset());
assertEquals(0, dtc.getOrdinal());
assertEquals("field1", dtc.getFieldName());
assertEquals("Comment1", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(1);
assertEquals(1, dtc.getOffset());
assertEquals(1, dtc.getOrdinal());
assertEquals("field_0x1", dtc.getDefaultFieldName());
assertEquals(null, dtc.getFieldName());
assertEquals("Comment2", dtc.getComment());
assertEquals(WordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(2);
assertEquals(3, dtc.getOffset());
assertEquals(2, dtc.getOrdinal());
assertEquals("field3", dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(3);
assertEquals(7, dtc.getOffset());
assertEquals(3, dtc.getOrdinal());
assertEquals("field4", dtc.getFieldName());
assertEquals("Comment4", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
}
@Test
public void testAdd2() throws Exception {
struct = createStructure("Test", 10);
assertEquals(10, struct.getLength());
assertEquals(10, struct.getNumComponents());
struct.add(new ByteDataType(), "field1", "Comment1");
struct.add(new WordDataType(), null, "Comment2");
struct.add(new DWordDataType(), "field3", null);
struct.add(new ByteDataType(), "field4", "Comment4");
assertEquals(18, struct.getLength());
assertEquals(14, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(0);
assertEquals(0, dtc.getOffset());
assertEquals(0, dtc.getOrdinal());
assertEquals("field_0x0", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertNull(dtc.getComment());
assertEquals(DataType.DEFAULT, dtc.getDataType());
dtc = struct.getComponent(1);
assertEquals(1, dtc.getOffset());
assertEquals(1, dtc.getOrdinal());
assertEquals("field_0x1", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(DataType.DEFAULT, dtc.getDataType());
dtc = struct.getComponent(10);
assertEquals(10, dtc.getOffset());
assertEquals(10, dtc.getOrdinal());
assertEquals("field1", dtc.getFieldName());
assertEquals("Comment1", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(11);
assertEquals(11, dtc.getOffset());
assertEquals(11, dtc.getOrdinal());
assertEquals("field_0xb", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertEquals("Comment2", dtc.getComment());
assertEquals(WordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(12);
assertEquals(13, dtc.getOffset());
assertEquals(12, dtc.getOrdinal());
assertEquals("field3", dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
}
@Test
public void testInsert_beginning() {
struct.insert(0, new FloatDataType());
assertEquals(12, struct.getLength());
assertEquals(5, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(0);
assertEquals(0, dtc.getOffset());
assertEquals(0, dtc.getOrdinal());
assertEquals("field_0x0", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertNull(dtc.getComment());
assertEquals(FloatDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(1);
assertEquals(4, dtc.getOffset());
assertEquals(1, dtc.getOrdinal());
assertEquals("field1", dtc.getFieldName());
assertEquals("Comment1", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(2);
assertEquals(5, dtc.getOffset());
assertEquals(2, dtc.getOrdinal());
assertEquals("field_0x5", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertEquals("Comment2", dtc.getComment());
assertEquals(WordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(3);
assertEquals(7, dtc.getOffset());
assertEquals(3, dtc.getOrdinal());
assertEquals("field3", dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(4);
assertEquals(11, dtc.getOffset());
assertEquals(4, dtc.getOrdinal());
assertEquals("field4", dtc.getFieldName());
assertEquals("Comment4", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
}
@Test
public void testInsert_end() {
struct.insert(4, new FloatDataType());
assertEquals(12, struct.getLength());
assertEquals(5, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(0);
assertEquals(0, dtc.getOffset());
assertEquals(0, dtc.getOrdinal());
assertEquals("field1", dtc.getFieldName());
assertEquals("Comment1", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(1);
assertEquals(1, dtc.getOffset());
assertEquals(1, dtc.getOrdinal());
assertEquals("field_0x1", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertEquals("Comment2", dtc.getComment());
assertEquals(WordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(2);
assertEquals(3, dtc.getOffset());
assertEquals(2, dtc.getOrdinal());
assertEquals("field3", dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(3);
assertEquals(7, dtc.getOffset());
assertEquals(3, dtc.getOrdinal());
assertEquals("field4", dtc.getFieldName());
assertEquals("Comment4", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(4);
assertEquals(8, dtc.getOffset());
assertEquals(4, dtc.getOrdinal());
assertEquals("field_0x8", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(FloatDataType.class, dtc.getDataType().getClass());
}
@Test
public void testInsert_middle() {
struct.insert(2, new FloatDataType());
assertEquals(12, struct.getLength());
assertEquals(5, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(0);
assertEquals(0, dtc.getOffset());
assertEquals(0, dtc.getOrdinal());
assertEquals("field1", dtc.getFieldName());
assertEquals("Comment1", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(1);
assertEquals(1, dtc.getOffset());
assertEquals(1, dtc.getOrdinal());
assertEquals("field_0x1", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertEquals("Comment2", dtc.getComment());
assertEquals(WordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(2);
assertEquals(3, dtc.getOffset());
assertEquals(2, dtc.getOrdinal());
assertEquals("field_0x3", dtc.getDefaultFieldName());
assertNull(dtc.getFieldName());
assertNull(dtc.getComment());
assertEquals(FloatDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(3);
assertEquals(7, dtc.getOffset());
assertEquals(3, dtc.getOrdinal());
assertEquals("field3", dtc.getFieldName());
assertEquals(null, dtc.getComment());
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(4);
assertEquals(11, dtc.getOffset());
assertEquals(4, dtc.getOrdinal());
assertEquals("field4", dtc.getFieldName());
assertEquals("Comment4", dtc.getComment());
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
}
@Test
public void testInsertWithEmptySpace() {
struct = createStructure("Test", 100);
struct.insert(40, new ByteDataType());
struct.insert(20, new WordDataType());
struct.insert(10, new FloatDataType());
assertEquals(107, struct.getLength());
assertEquals(103, struct.getNumComponents());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(3, comps.length);
assertEquals(10, comps[0].getOffset());
assertEquals(10, comps[0].getOrdinal());
assertEquals(FloatDataType.class, comps[0].getDataType().getClass());
assertEquals(24, comps[1].getOffset());
assertEquals(21, comps[1].getOrdinal());
assertEquals(WordDataType.class, comps[1].getDataType().getClass());
assertEquals(46, comps[2].getOffset());
assertEquals(42, comps[2].getOrdinal());
assertEquals(ByteDataType.class, comps[2].getDataType().getClass());
}
// test inserting at offset 0
@Test
public void testInsertAtOffset() {
struct.insertAtOffset(0, new FloatDataType(), 4);
assertEquals(12, struct.getLength());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(5, comps.length);
assertEquals(0, comps[0].getOffset());
assertEquals(0, comps[0].getOrdinal());
assertEquals(FloatDataType.class, comps[0].getDataType().getClass());
assertEquals(4, comps[1].getOffset());
assertEquals(1, comps[1].getOrdinal());
assertEquals(ByteDataType.class, comps[1].getDataType().getClass());
assertEquals(5, comps[2].getOffset());
assertEquals(2, comps[2].getOrdinal());
assertEquals(WordDataType.class, comps[2].getDataType().getClass());
assertEquals(7, comps[3].getOffset());
assertEquals(3, comps[3].getOrdinal());
assertEquals(DWordDataType.class, comps[3].getDataType().getClass());
}
// test inserting at offset 1
@Test
public void testInsertAtOffset1() {
struct.insertAtOffset(1, new FloatDataType(), 4);
assertEquals(12, struct.getLength());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(5, comps.length);
assertEquals(0, comps[0].getOffset());
assertEquals(0, comps[0].getOrdinal());
assertEquals(ByteDataType.class, comps[0].getDataType().getClass());
assertEquals(1, comps[1].getOffset());
assertEquals(1, comps[1].getOrdinal());
assertEquals(FloatDataType.class, comps[1].getDataType().getClass());
assertEquals(5, comps[2].getOffset());
assertEquals(2, comps[2].getOrdinal());
assertEquals(WordDataType.class, comps[2].getDataType().getClass());
assertEquals(7, comps[3].getOffset());
assertEquals(3, comps[3].getOrdinal());
assertEquals(DWordDataType.class, comps[3].getDataType().getClass());
}
// test inserting at offset 1
@Test
public void testInsertAtOffset2() {
struct.insertAtOffset(2, new FloatDataType(), 4);
assertEquals(13, struct.getLength());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(5, comps.length);
assertEquals(0, comps[0].getOffset());
assertEquals(0, comps[0].getOrdinal());
assertEquals(ByteDataType.class, comps[0].getDataType().getClass());
assertEquals(2, comps[1].getOffset());
assertEquals(2, comps[1].getOrdinal());
assertEquals(FloatDataType.class, comps[1].getDataType().getClass());
assertEquals(6, comps[2].getOffset());
assertEquals(3, comps[2].getOrdinal());
assertEquals(WordDataType.class, comps[2].getDataType().getClass());
assertEquals(8, comps[3].getOffset());
assertEquals(4, comps[3].getOrdinal());
assertEquals(DWordDataType.class, comps[3].getDataType().getClass());
}
@Test
public void testInsertAtOffsetPastEnd() {
struct.insertAtOffset(100, new FloatDataType(), 4);
assertEquals(104, struct.getLength());
}
@Test
public void testClearComponent() {
struct.clearComponent(0);
assertEquals(8, struct.getLength());
assertEquals(4, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(0);
assertEquals(DataType.DEFAULT, dtc.getDataType());
dtc = struct.getComponent(1);
assertEquals(WordDataType.class, dtc.getDataType().getClass());
}
@Test
public void testClearComponent1() {
struct.clearComponent(1);
assertEquals(8, struct.getLength());
assertEquals(5, struct.getNumComponents());
DataTypeComponent dtc = struct.getComponent(1);
assertEquals(DataType.DEFAULT, dtc.getDataType());
dtc = struct.getComponent(2);
assertEquals(DataType.DEFAULT, dtc.getDataType());
dtc = struct.getComponent(0);
assertEquals(ByteDataType.class, dtc.getDataType().getClass());
dtc = struct.getComponent(3);
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
assertEquals(3, dtc.getOrdinal());
assertEquals(3, dtc.getOffset());
}
@Test
public void testReplace() { // bigger, no space below
try {
struct.replace(0, new QWordDataType(), 8);
Assert.fail();
}
catch (Exception e) {
}
}
@Test
public void testReplace1() { // bigger, space below
struct.insert(1, new QWordDataType());
struct.clearComponent(1);
assertEquals(16, struct.getLength());
assertEquals(12, struct.getNumComponents());
struct.replace(0, new QWordDataType(), 8);
assertEquals(16, struct.getLength());
assertEquals(5, struct.getNumComponents());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(9, comps[1].getOffset());
assertEquals(2, comps[1].getOrdinal());
}
@Test
public void testReplace2() { // same size
struct.replace(0, new CharDataType(), 1);
assertEquals(8, struct.getLength());
assertEquals(4, struct.getNumComponents());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(CharDataType.class, comps[0].getDataType().getClass());
assertEquals(1, comps[1].getOffset());
assertEquals(1, comps[1].getOrdinal());
}
@Test
public void testReplace3() { // smaller
struct.replace(1, new CharDataType(), 1);
assertEquals(8, struct.getLength());
assertEquals(5, struct.getNumComponents());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(CharDataType.class, comps[1].getDataType().getClass());
assertEquals(1, comps[1].getOffset());
assertEquals(1, comps[1].getOrdinal());
assertEquals(3, comps[2].getOffset());
assertEquals(3, comps[2].getOrdinal());
assertEquals(DWordDataType.class, comps[2].getDataType().getClass());
}
@Test
public void testDelete() {
struct.delete(1);
assertEquals(6, struct.getLength());
assertEquals(3, struct.getNumComponents());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(DWordDataType.class, comps[1].getDataType().getClass());
assertEquals(1, comps[1].getOffset());
}
@Test
public void testDeleteAtOffset() {
struct.deleteAtOffset(2);
assertEquals(6, struct.getLength());
assertEquals(3, struct.getNumComponents());
DataTypeComponent[] comps = struct.getDefinedComponents();
assertEquals(DWordDataType.class, comps[1].getDataType().getClass());
assertEquals(1, comps[1].getOffset());
}
@Test
public void testDeleteAll() {
Structure s = new StructureDataType("test1", 0);
s.add(new ByteDataType());
s.add(new FloatDataType());
struct.add(s);
s.deleteAll();
assertEquals(1, s.getLength());
assertTrue(s.isNotYetDefined());
assertEquals(0, s.getNumComponents());
}
@Test
public void testGetComponents() {
struct = createStructure("Test", 8);
struct.insert(2, new ByteDataType(), 1, "field3", "Comment1");
struct.insert(5, new WordDataType(), 2, null, "Comment2");
struct.insert(7, new DWordDataType(), 4, "field8", null);
assertEquals(15, struct.getLength());
assertEquals(11, struct.getNumComponents());
DataTypeComponent[] dtcs = struct.getComponents();
assertEquals(11, dtcs.length);
int offset = 0;
for (int i = 0; i < 11; i++) {
assertEquals(i, dtcs[i].getOrdinal());
assertEquals(offset, dtcs[i].getOffset());
offset += dtcs[i].getLength();
}
assertEquals(DataType.DEFAULT, dtcs[0].getDataType());
assertEquals(DataType.DEFAULT, dtcs[1].getDataType());
assertEquals(ByteDataType.class, dtcs[2].getDataType().getClass());
assertEquals(DataType.DEFAULT, dtcs[3].getDataType());
assertEquals(DataType.DEFAULT, dtcs[4].getDataType());
assertEquals(WordDataType.class, dtcs[5].getDataType().getClass());
assertEquals(DataType.DEFAULT, dtcs[6].getDataType());
assertEquals(DWordDataType.class, dtcs[7].getDataType().getClass());
assertEquals(DataType.DEFAULT, dtcs[8].getDataType());
assertEquals(DataType.DEFAULT, dtcs[9].getDataType());
assertEquals(DataType.DEFAULT, dtcs[10].getDataType());
}
@Test
public void testGetDefinedComponents() {
struct = createStructure("Test", 8);
struct.insert(2, new ByteDataType(), 1, "field3", "Comment1");
struct.insert(5, new WordDataType(), 2, null, "Comment2");
struct.insert(7, new DWordDataType(), 4, "field8", null);
assertEquals(15, struct.getLength());
assertEquals(11, struct.getNumComponents());
DataTypeComponent[] dtcs = struct.getDefinedComponents();
assertEquals(3, dtcs.length);
assertEquals(ByteDataType.class, dtcs[0].getDataType().getClass());
assertEquals(2, dtcs[0].getOrdinal());
assertEquals(2, dtcs[0].getOffset());
assertEquals(WordDataType.class, dtcs[1].getDataType().getClass());
assertEquals(5, dtcs[1].getOrdinal());
assertEquals(5, dtcs[1].getOffset());
assertEquals(DWordDataType.class, dtcs[2].getDataType().getClass());
assertEquals(7, dtcs[2].getOrdinal());
assertEquals(8, dtcs[2].getOffset());
}
@Test
public void testGetComponentAt() {
DataTypeComponent dtc = struct.getComponentAt(4);
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
assertEquals(2, dtc.getOrdinal());
assertEquals(3, dtc.getOffset());
}
@Test
public void testGetDataTypeAt() {
Structure s1 = createStructure("Test1", 0);
s1.add(new WordDataType());
s1.add(struct);
s1.add(new ByteDataType());
DataTypeComponent dtc = s1.getComponentAt(7);
assertEquals(struct, dtc.getDataType());
dtc = s1.getDataTypeAt(7);
assertEquals(DWordDataType.class, dtc.getDataType().getClass());
}
@Test
public void testReplaceWith() {
assertEquals(8, struct.getLength());
assertEquals(4, struct.getNumComponents());
Structure newStruct = createStructure("Replaced", 8);
newStruct.setDescription("testReplaceWith()");
DataTypeComponent dtc0 = newStruct.insert(2, new ByteDataType(), 1, "field3", "Comment1");
DataTypeComponent dtc1 = newStruct.insert(5, new WordDataType(), 2, null, "Comment2");
DataTypeComponent dtc2 = newStruct.insert(7, new DWordDataType(), 4, "field8", null);
struct.replaceWith(newStruct);
assertEquals(15, struct.getLength());
assertEquals(11, struct.getNumComponents());
DataTypeComponent[] dtcs = struct.getDefinedComponents();
assertEquals(3, dtcs.length);
assertEquals(dtc0, dtcs[0]);
assertEquals(dtc1, dtcs[1]);
assertEquals(dtc2, dtcs[2]);
assertEquals("TestStruct", struct.getName());
assertEquals("", struct.getDescription());
}
/**
* Test that a structure can't ... ???
*/
@Test
public void testCyclingProblem() {
Structure newStruct = createStructure("TestStruct", 80);
newStruct.setDescription("testReplaceWith()");
newStruct.add(new ByteDataType(), "field0", "Comment1");
newStruct.add(struct, "field1", null);
newStruct.add(new WordDataType(), null, "Comment2");
newStruct.add(new DWordDataType(), "field3", null);
try {
struct.add(newStruct);
Assert.fail();
}
catch (IllegalArgumentException e) {
}
try {
struct.insert(0, newStruct);
Assert.fail();
}
catch (IllegalArgumentException e) {
}
try {
struct.replace(0, newStruct, newStruct.getLength());
Assert.fail();
}
catch (IllegalArgumentException e) {
}
}
/**
* Test that a structure can't be added to itself.
*/
@Test
public void testCyclicDependencyProblem1() {
try {
struct.add(struct);
Assert.fail("Shouldn't be able to add a structure to itself.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the struct to itself.
}
try {
struct.insert(0, struct);
Assert.fail("Shouldn't be able to insert a structure into itself.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the struct to itself.
}
try {
struct.replace(0, struct, struct.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with the structure itself.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct to itself.
}
}
/**
* Test that a structure array can't be added to the same structure.
*/
@Test
public void testCyclicDependencyProblem2() {
Array array = createArray(struct, 3);
try {
struct.add(array);
Assert.fail("Shouldn't be able to add a structure array to the same structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the struct to itself.
}
try {
struct.insert(0, array);
Assert.fail("Shouldn't be able to insert a structure array into the same structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the struct to itself.
}
try {
struct.replace(0, array, array.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with an array of the same structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct to itself.
}
}
/**
* Test that a typedef of a structure can't be added to the structure.
*/
@Test
public void testCyclicDependencyProblem3() {
TypeDef typeDef = createTypeDef(struct);
try {
struct.add(typeDef);
Assert.fail("Shouldn't be able to add a structure typedef to the typedef's structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the struct to itself.
}
try {
struct.insert(0, typeDef);
Assert.fail(
"Shouldn't be able to insert a structure typedef into the typedef's structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the struct to itself.
}
try {
struct.replace(0, typeDef, typeDef.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with the structure's typedef.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct to itself.
}
}
/**
* Test that a structure can't contain another structure that contains it.
*/
@Test
public void testCyclicDependencyProblem4() {
Structure anotherStruct = createStructure("AnotherStruct", 0);
anotherStruct.add(struct);
try {
struct.add(anotherStruct);
Assert.fail(
"Shouldn't be able to add another structure, which contains this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the struct to itself.
}
try {
struct.insert(0, anotherStruct);
Assert.fail(
"Shouldn't be able to insert another structure, which contains this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the struct to itself.
}
try {
struct.replace(0, anotherStruct, anotherStruct.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with another structure which contains this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct to itself.
}
}
/**
* Test that a structure can't contain another structure that contains a typedef to it.
*/
@Test
public void testCyclicDependencyProblem5() {
Structure anotherStruct = createStructure("AnotherStruct", 0);
TypeDef typeDef = createTypeDef(struct);
anotherStruct.add(typeDef);
try {
struct.add(anotherStruct);
Assert.fail(
"Shouldn't be able to add another structure, which contains a typedef of this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the struct to itself.
}
try {
struct.insert(0, anotherStruct);
Assert.fail(
"Shouldn't be able to insert another structure, which contains a typedef of this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the struct to itself.
}
try {
struct.replace(0, anotherStruct, anotherStruct.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with another structure which contains a typedef of this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct to itself.
}
}
/**
* Test that a structure can't contain a union that contains that structure.
*/
@Test
public void testCyclicDependencyProblem6() {
Union union = createUnion("TestUnion");
union.add(struct);
try {
struct.add(union);
Assert.fail(
"Shouldn't be able to add a union, which contains this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the struct to itself.
}
try {
struct.insert(0, union);
Assert.fail(
"Shouldn't be able to insert a union, which contains this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the struct to itself.
}
try {
struct.replace(0, union, union.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with a union, which contains this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct to itself.
}
}
/**
* Test that a structure can't contain a typedef of a union that contains that structure.
*/
@Test
public void testCyclicDependencyProblem7() {
Union union = createUnion("TestUnion");
union.add(struct);
TypeDef typeDef = createTypeDef(union);
try {
struct.add(typeDef);
Assert.fail(
"Shouldn't be able to add a typedef of a union, which contains this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from adding the union typedef to the struct.
}
try {
struct.insert(0, typeDef);
Assert.fail(
"Shouldn't be able to insert a typedef of a union, which contains this structure, to this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from inserting the union typedef to the struct.
}
try {
struct.replace(0, typeDef, typeDef.getLength());
Assert.fail(
"Shouldn't be able to replace a structure component with a typedef of a union, which contains this structure.");
}
catch (IllegalArgumentException e) {
// Should get an exception from replacing the struct component with the union typedef.
}
}
/**
* Test that a structure can contain a pointer in it to the same structure.
*/
@Test
public void testNoCyclicDependencyProblemForStructurePointer() {
Pointer structurePointer = createPointer(struct, 4);
try {
struct.add(structurePointer);
}
catch (IllegalArgumentException e) {
Assert.fail("Should be able to add a structure pointer to the pointer's structure.");
}
try {
struct.insert(0, structurePointer);
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to insert a structure pointer into the pointer's structure.");
}
try {
struct.replace(0, structurePointer, structurePointer.getLength());
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to replace a structure component with the structure's pointer.");
}
}
/**
* Test that a structure can contain a pointer in it to a typedef of the same structure.
*/
@Test
public void testNoCyclicDependencyProblemForTypedefPointer() {
TypeDef typeDef = createTypeDef(struct);
Pointer typedefPointer = createPointer(typeDef, 4);
try {
struct.add(typedefPointer);
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to add a structure typedef pointer to the pointer's structure.");
}
try {
struct.insert(0, typedefPointer);
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to insert a structure typedef pointer into the pointer's structure.");
}
try {
struct.replace(0, typedefPointer, typedefPointer.getLength());
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to replace a structure component with the structure's typedef pointer.");
}
}
/**
* Test that a structure can contain a pointer in it to a typedef of the same structure.
*/
@Test
public void testNoCyclicDependencyProblemForArrayPointer() {
TypeDef typeDef = createTypeDef(struct);
Array array = createArray(typeDef, 5);
Pointer arrayPointer = createPointer(array, 4);
try {
struct.add(arrayPointer);
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to add a structure typedef array pointer to the pointer's structure.");
}
try {
struct.insert(0, arrayPointer);
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to insert a structure typedef arrayointer into the pointer's structure.");
}
try {
struct.replace(0, arrayPointer, arrayPointer.getLength());
}
catch (IllegalArgumentException e) {
Assert.fail(
"Should be able to replace a structure component with the structure's typedef array pointer.");
}
}
}

View file

@ -1,448 +0,0 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
*
*/
package ghidra.program.model.data;
import static org.junit.Assert.*;
import org.junit.*;
import generic.test.AbstractGenericTest;
/**
*
*/
public class UnionTest extends AbstractGenericTest {
private DataTypeManager dtm;
private Union union;
private int id;
private Union createUnion(String name) {
Union unionDt = new UnionDataType(name);
return (Union)dtm.addDataType(unionDt,DataTypeConflictHandler.DEFAULT_HANDLER);
}
private Structure createStructure(String name, int size) {
return (Structure)dtm.addDataType(new StructureDataType(name, size),DataTypeConflictHandler.DEFAULT_HANDLER);
}
private TypeDef createTypeDef(DataType dataType) {
return new TypedefDataType(dataType.getName()+"TypeDef", dataType);
}
private Array createArray(DataType dataType, int numElements) {
return new ArrayDataType(dataType, numElements, dataType.getLength());
}
private Pointer createPointer(DataType dataType, int length) {
return new PointerDataType(dataType, length);
}
/**
* Constructor for UnionTest.
* @param arg0
*/
public UnionTest() {
super();
}
/*
* @see TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
dtm = new StandAloneDataTypeManager("dummyDTM");
id = dtm.startTransaction("");
union = createUnion("TestUnion");
union.add(new ByteDataType(), "field1", "Comment1");
union.add(new WordDataType(), null, "Comment2");
union.add(new DWordDataType(), "field3", null);
union.add(new ByteDataType(), "field4", "Comment4");
}
/*
* @see TestCase#tearDown()
*/
@After
public void tearDown() throws Exception {
dtm.endTransaction(id, true);
dtm.close();
}
@Test
public void testAdd() throws Exception {
assertEquals(4, union.getLength());
assertEquals(4, union.getNumComponents());
DataTypeComponent[] dtcs = union.getComponents();
assertEquals(4, dtcs.length);
DataTypeComponent dtc = union.getComponent(3);
assertEquals("field4", dtc.getFieldName());
assertEquals("byte", dtc.getDataType().getName());
Structure struct = new StructureDataType("struct_1", 0);
struct.add(new ByteDataType());
struct.add(new StringDataType(), 10);
union.add(struct);
assertEquals(struct.getLength(), union.getLength());
}
@Test
public void testAdd2() {
Structure struct = createStructure("struct_1", 0);
struct.add(new ByteDataType());
struct.add(new StringDataType(), 10);
union.add(struct);
union.delete(4);
assertEquals(4, union.getNumComponents());
assertEquals(4, union.getLength());
}
@Test
public void testGetComponent() {
Structure struct = createStructure("struct_1", 0);
struct.add(new ByteDataType());
struct.add(new StringDataType(), 10);
DataTypeComponent newdtc = union.add(struct, "field5", "comments");
DataTypeComponent dtc = union.getComponent(4);
assertEquals(newdtc, dtc);
assertEquals("field5", dtc.getFieldName());
assertEquals("comments", dtc.getComment());
}
@Test
public void testGetComponents() {
Structure struct = createStructure("struct_1", 0);
struct.add(new ByteDataType());
struct.add(new StringDataType(), 10);
union.add(struct, "field5", "comments");
DataTypeComponent[] dtcs = union.getComponents();
assertEquals(5, dtcs.length);
assertEquals(5, union.getNumComponents());
}
@Test
public void testInsert() {
Structure struct = createStructure("struct_1", 0);
struct.add(new ByteDataType());
struct.add(new StringDataType(), 10);
DataTypeComponent dtc = union.getComponent(2);
assertEquals("field3", dtc.getFieldName());
union.insert(2, struct, struct.getLength(), "field5", "field5 comments");
assertEquals(11, union.getLength());
dtc = union.getComponent(2);
assertEquals("field5", dtc.getFieldName());
}
@Test
public void testGetName() {
assertEquals("TestUnion", union.getName());
}
@Test
public void testCloneRetainIdentity() throws Exception {
Union unionCopy = (Union)union.clone(null);
assertNull(unionCopy.getDataTypeManager());
assertEquals(4, union.getLength());
}
@Test
public void testCopyDontRetain() throws Exception {
Union unionCopy = (Union)union.copy(null);
assertNull(unionCopy.getDataTypeManager());
assertEquals(4, union.getLength());
}
@Test
public void testDelete() throws Exception {
Structure struct = createStructure("struct_1", 0);
struct.add(new ByteDataType());
struct.add(new StringDataType(), 10);
union.add(struct);
assertEquals(11, union.getLength());
union.delete(4);
assertEquals(4, union.getLength());
union.delete(2);
assertEquals(2, union.getLength());
}
@Test
public void testIsPartOf() {
Structure struct = createStructure("struct_1", 0);
struct.add(new ByteDataType());
DataTypeComponent dtc = struct.add(createStructure("mystring", 10));
DataType dt = dtc.getDataType();
DataTypeComponent newdtc = union.add(struct);
assertTrue(union.isPartOf(dt));
Structure newstruct = (Structure)newdtc.getDataType();
Structure s1 = (Structure)newstruct.add(createStructure("s1", 1)).getDataType();
dt = s1.add(new QWordDataType()).getDataType();
assertTrue(union.isPartOf(dt));
}
@Test
public void testReplaceWith() {
assertEquals(4, union.getLength());
assertEquals(4, union.getNumComponents());
Union newUnion = createUnion("Replaced");
newUnion.setDescription("testReplaceWith()");
DataTypeComponent dtc2 = newUnion.insert(0, new DWordDataType(), 4, "field2", null);
DataTypeComponent dtc1 = newUnion.insert(0, new WordDataType(), 2, null, "Comment2");
DataTypeComponent dtc0 = newUnion.insert(0, new ByteDataType(), 1, "field0", "Comment1");
union.replaceWith(newUnion);
assertEquals(4, union.getLength());
assertEquals(3, union.getNumComponents());
DataTypeComponent[] dtcs = union.getComponents();
assertEquals(3, dtcs.length);
assertEquals(dtc0, dtcs[0]);
assertEquals(dtc1, dtcs[1]);
assertEquals(dtc2, dtcs[2]);
assertEquals("TestUnion", union.getName());
assertEquals("testReplaceWith()", union.getDescription());
}
@Test
public void testCyclingProblem() {
Union newUnion = createUnion("Test");
newUnion.setDescription("testReplaceWith()");
newUnion.add(new ByteDataType(), "field0", "Comment1");
newUnion.add(union, "field1", null);
newUnion.add(new WordDataType(), null, "Comment2");
newUnion.add(new DWordDataType(), "field3", null);
try {
union.add(newUnion);
Assert.fail();
} catch (IllegalArgumentException e) {
}
try {
union.insert(0, newUnion);
Assert.fail();
} catch (IllegalArgumentException e) {
}
}
/**
* Test that a structure can't be added to itself.
*/
@Test
public void testCyclicDependencyProblem1() {
try {
union.add(union);
Assert.fail("Shouldn't be able to add a union to itself.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the union to itself.
}
try {
union.insert(0, union);
Assert.fail("Shouldn't be able to insert a union into itself.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the union to itself.
}
}
/**
* Test that a structure array can't be added to the same structure.
*/
@Test
public void testCyclicDependencyProblem2() {
Array array = createArray(union, 3);
try {
union.add(array);
Assert.fail("Shouldn't be able to add a union array to the same union.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the union to itself.
}
try {
union.insert(0, array);
Assert.fail("Shouldn't be able to insert a union array into the same union.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the union to itself.
}
}
/**
* Test that a typedef of a union can't be added to the union.
*/
@Test
public void testCyclicDependencyProblem3() {
TypeDef typeDef = createTypeDef(union);
try {
union.add(typeDef);
Assert.fail("Shouldn't be able to add a union typedef to the typedef's union.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the union to itself.
}
try {
union.insert(0, typeDef);
Assert.fail("Shouldn't be able to insert a union typedef into the typedef's union.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the union to itself.
}
}
/**
* Test that a union can't contain another union that contains it.
*/
@Test
public void testCyclicDependencyProblem4() {
Union anotherUnion = createUnion("AnotherUnion");
anotherUnion.add(union);
try {
union.add(anotherUnion);
Assert.fail("Shouldn't be able to add another union, which contains this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the union to itself.
}
try {
union.insert(0, anotherUnion);
Assert.fail("Shouldn't be able to insert another union, which contains this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the union to itself.
}
}
/**
* Test that a union can't contain another union that contains a typedef to it.
*/
@Test
public void testCyclicDependencyProblem5() {
Union anotherUnion = createUnion("AnotherUnion");
TypeDef typeDef = createTypeDef(union);
anotherUnion.add(typeDef);
try {
union.add(anotherUnion);
Assert.fail("Shouldn't be able to add another union, which contains a typedef of this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the union to itself.
}
try {
union.insert(0, anotherUnion);
Assert.fail("Shouldn't be able to insert another union, which contains a typedef of this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the union to itself.
}
}
/**
* Test that a union can't contain a structure that contains that union.
*/
@Test
public void testCyclicDependencyProblem6() {
Union structure = createUnion("TestStructure");
structure.add(union);
try {
union.add(structure);
Assert.fail("Shouldn't be able to add a structure, which contains this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the union to itself.
}
try {
union.insert(0, structure);
Assert.fail("Shouldn't be able to insert a structure, which contains this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the union to itself.
}
}
/**
* Test that a structure can't contain a typedef of a union that contains that structure.
*/
@Test
public void testCyclicDependencyProblem7() {
Structure structure = createStructure("TestStructure", 0);
structure.add(union);
TypeDef typeDef = createTypeDef(structure);
try {
union.add(typeDef);
Assert.fail("Shouldn't be able to add a typedef of a strucutre, which contains this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from adding the structure typedef to the union.
}
try {
union.insert(0, typeDef);
Assert.fail("Shouldn't be able to insert a typedef of a structure, which contains this union, to this union.");
} catch (IllegalArgumentException e) {
// Should get an exception from inserting the structure typedef to the union.
}
}
/**
* Test that a structure can contain a pointer in it to the same structure.
*/
@Test
public void testNoCyclicDependencyProblemForStructurePointer() {
Pointer unionPointer = createPointer(union, 4);
try {
union.add(unionPointer);
} catch (IllegalArgumentException e) {
Assert.fail("Should be able to add a union pointer to the pointer's union.");
}
try {
union.insert(0, unionPointer);
} catch (IllegalArgumentException e) {
Assert.fail("Should be able to insert a union pointer into the pointer's union.");
}
}
/**
* Test that a union can contain a pointer in it to a typedef of the same union.
*/
@Test
public void testNoCyclicDependencyProblemForTypedefPointer() {
TypeDef typeDef = createTypeDef(union);
Pointer typedefPointer = createPointer(typeDef, 4);
try {
union.add(typedefPointer);
} catch (IllegalArgumentException e) {
Assert.fail("Should be able to add a union typedef pointer to the pointer's union.");
}
try {
union.insert(0, typedefPointer);
} catch (IllegalArgumentException e) {
Assert.fail("Should be able to insert a union typedef pointer into the pointer's union.");
}
}
/**
* Test that a union can contain a pointer in it to a typedef of the same union.
*/
@Test
public void testNoCyclicDependencyProblemForArrayPointer() {
TypeDef typeDef = createTypeDef(union);
Array array = createArray(typeDef, 5);
Pointer arrayPointer = createPointer(array, 4);
try {
union.add(arrayPointer);
} catch (IllegalArgumentException e) {
Assert.fail("Should be able to add a union typedef array pointer to the pointer's union.");
}
try {
union.insert(0, arrayPointer);
} catch (IllegalArgumentException e) {
Assert.fail("Should be able to insert a union typedef array pointer into the pointer's union.");
}
}
}