|
| 1 | +/* ### |
| 2 | + * IP: GHIDRA |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package ghidra.program.util; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import ghidra.program.database.ProgramDB; |
| 27 | +import ghidra.program.model.data.*; |
| 28 | +import ghidra.program.model.listing.Data; |
| 29 | +import ghidra.program.model.util.CodeUnitInsertionException; |
| 30 | +import ghidra.test.AbstractGhidraHeadlessIntegrationTest; |
| 31 | +import ghidra.test.ToyProgramBuilder; |
| 32 | +import util.CollectionUtils; |
| 33 | + |
| 34 | +public class DefinedDataIteratorTest extends AbstractGhidraHeadlessIntegrationTest { |
| 35 | + |
| 36 | + private ToyProgramBuilder builder; |
| 37 | + private ProgramDB program; |
| 38 | + private DataTypeManager dtm; |
| 39 | + private DataType intDT; |
| 40 | + private StringDataType stringDT; |
| 41 | + private CharDataType charDT; |
| 42 | + private DataType charArray; |
| 43 | + private StructureDataType struct1DT; |
| 44 | + private ArrayDataType structArray; |
| 45 | + private StructureDataType struct2DT; |
| 46 | + private TypeDef intTD; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() throws Exception { |
| 50 | + |
| 51 | + builder = new ToyProgramBuilder("DefinedDataIteratorTests", false); |
| 52 | + program = builder.getProgram(); |
| 53 | + dtm = program.getDataTypeManager(); |
| 54 | + |
| 55 | + intDT = AbstractIntegerDataType.getSignedDataType(4, dtm); |
| 56 | + intTD = new TypedefDataType("int_typedef", intDT); |
| 57 | + stringDT = StringDataType.dataType; |
| 58 | + charDT = new CharDataType(dtm); |
| 59 | + charArray = new ArrayDataType(charDT, 20, charDT.getLength()); |
| 60 | + |
| 61 | + struct1DT = new StructureDataType("struct1", 100); |
| 62 | + struct1DT.replaceAtOffset(0, intDT, intDT.getLength(), "f1", null); |
| 63 | + struct1DT.replaceAtOffset(10, charArray, charArray.getLength(), "f2", null); |
| 64 | + struct1DT.replaceAtOffset(50, stringDT, 10, "f3", null); |
| 65 | + |
| 66 | + structArray = new ArrayDataType(struct1DT, 10, struct1DT.getLength()); |
| 67 | + |
| 68 | + struct2DT = new StructureDataType("struct2", 200); |
| 69 | + struct2DT.replaceAtOffset(0, intDT, intDT.getLength(), "f1", null); |
| 70 | + struct2DT.replaceAtOffset(10, struct1DT, intDT.getLength(), "f2", null); |
| 71 | + |
| 72 | + builder.createMemory("test", "0x0", 0x2000); |
| 73 | + program = builder.getProgram(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void test_Ints() throws Exception { |
| 78 | + builder.applyFixedLengthDataType("0x0", intDT, intDT.getLength()); |
| 79 | + builder.createString("0x10", "test1", StandardCharsets.UTF_8, true, stringDT); |
| 80 | + builder.applyFixedLengthDataType("0x100", struct1DT, struct1DT.getLength()); |
| 81 | + |
| 82 | + List<Data> list = CollectionUtils.asList((Iterable<Data>) |
| 83 | + DefinedDataIterator.byDataType(program, dt -> dt instanceof IntegerDataType)); |
| 84 | + |
| 85 | + assertTrue(list.get(0).getAddress().getOffset() == 0x0); |
| 86 | + assertTrue(list.get(1).getAddress().getOffset() == 0x100); |
| 87 | + |
| 88 | + assertEquals(2, list.size()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void test_Strings() throws Exception { |
| 93 | + builder.applyFixedLengthDataType("0x0", intDT, intDT.getLength()); |
| 94 | + builder.createString("0x10", "test1", StandardCharsets.UTF_8, true, stringDT); |
| 95 | + builder.applyFixedLengthDataType("0x100", struct1DT, struct1DT.getLength()); |
| 96 | + |
| 97 | + List<Data> list = |
| 98 | + CollectionUtils.asList((Iterable<Data>) DefinedDataIterator.definedStrings(program)); |
| 99 | + |
| 100 | + assertTrue(list.get(0).getAddress().getOffset() == 0x10); |
| 101 | + assertTrue(list.get(1).getAddress().getOffset() == 0x100 + 10); |
| 102 | + assertTrue(list.get(2).getAddress().getOffset() == 0x100 + 50); |
| 103 | + |
| 104 | + assertEquals(3, list.size()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void test_ArrayOfStructs() throws Exception { |
| 109 | + builder.applyFixedLengthDataType("0x0", intDT, intDT.getLength()); |
| 110 | + builder.createString("0x10", "test1", StandardCharsets.UTF_8, true, stringDT); |
| 111 | + builder.applyFixedLengthDataType("0x100", structArray, structArray.getLength()); |
| 112 | + |
| 113 | + int numElements = structArray.getNumElements(); |
| 114 | + int lastEle = numElements - 1; |
| 115 | + int elementSize = structArray.getElementLength(); |
| 116 | + |
| 117 | + List<Data> list = |
| 118 | + CollectionUtils.asList((Iterable<Data>) DefinedDataIterator.definedStrings(program)); |
| 119 | + |
| 120 | + assertEquals(list.get(0).getAddress().getOffset(), 0x10); |
| 121 | + assertEquals(list.get(1 + 0).getAddress().getOffset(), 0x100 + 10); |
| 122 | + assertEquals(list.get(1 + 1).getAddress().getOffset(), 0x100 + 50); |
| 123 | + |
| 124 | + assertEquals(list.get(1 + (lastEle * 2) + 0).getAddress().getOffset(), |
| 125 | + 0x100 + (elementSize * lastEle) + 10); |
| 126 | + assertEquals(list.get(1 + (lastEle * 2) + 1).getAddress().getOffset(), |
| 127 | + 0x100 + (elementSize * lastEle) + 50); |
| 128 | + |
| 129 | + assertEquals(1 + (numElements * 2), list.size()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void test_Typedefs() throws CodeUnitInsertionException { |
| 134 | + // 3 ints: 2 are typedefs, 1 is regular int |
| 135 | + builder.applyFixedLengthDataType("0x0", intTD, intTD.getLength()); |
| 136 | + builder.applyFixedLengthDataType("0x10", intTD, intTD.getLength()); |
| 137 | + builder.applyFixedLengthDataType("0x20", intDT, intTD.getLength()); |
| 138 | + |
| 139 | + // iterating by data type ignores typedefs, so we should get all 3 ints |
| 140 | + List<Data> list = CollectionUtils.asList((Iterable<Data>) |
| 141 | + DefinedDataIterator.byDataType(program, dt -> dt instanceof IntegerDataType)); |
| 142 | + |
| 143 | + assertEquals(3, list.size()); |
| 144 | + |
| 145 | + // iterating by data instance, we can inspect the actual data type and get the |
| 146 | + // typedef |
| 147 | + list = CollectionUtils.asList((Iterable<Data>) DefinedDataIterator.byDataInstance(program, |
| 148 | + data -> data.getDataType() instanceof TypeDef)); |
| 149 | + assertEquals(2, list.size()); |
| 150 | + } |
| 151 | +} |
0 commit comments