|
| 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.model.data; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import generic.test.AbstractGTest; |
| 23 | +import ghidra.program.model.address.AddressSpace; |
| 24 | +import ghidra.program.model.address.GenericAddressSpace; |
| 25 | +import ghidra.program.model.mem.ByteMemBufferImpl; |
| 26 | + |
| 27 | +public class ArrayStringableTest extends AbstractGTest { |
| 28 | + private ByteMemBufferImpl mb(boolean isBE, int... values) { |
| 29 | + byte[] bytes = new byte[values.length]; |
| 30 | + for (int i = 0; i < values.length; i++) { |
| 31 | + bytes[i] = (byte) values[i]; |
| 32 | + } |
| 33 | + GenericAddressSpace gas = new GenericAddressSpace("test", 32, AddressSpace.TYPE_RAM, 1); |
| 34 | + return new ByteMemBufferImpl(gas.getAddress(0), bytes, isBE); |
| 35 | + } |
| 36 | + |
| 37 | + private SettingsBuilder newset() { |
| 38 | + return new SettingsBuilder(); |
| 39 | + } |
| 40 | + |
| 41 | + private static class DataOrgDTM extends TestDummyDataTypeManager { |
| 42 | + private DataOrganization dataOrg; |
| 43 | + |
| 44 | + public DataOrgDTM(int charSize) { |
| 45 | + DataOrganizationImpl dataOrgImpl = DataOrganizationImpl.getDefaultOrganization(null); |
| 46 | + dataOrgImpl.setCharSize(charSize); |
| 47 | + |
| 48 | + this.dataOrg = dataOrgImpl; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public DataOrganization getDataOrganization() { |
| 53 | + return dataOrg; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private Array mkArray(DataTypeManager dtm, int count) { |
| 58 | + CharDataType charDT = new CharDataType(dtm); |
| 59 | + Array arrayDT = new ArrayDataType(charDT, count, charDT.getLength(), dtm); |
| 60 | + |
| 61 | + return arrayDT; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + private Array array10char1 = mkArray(new DataOrgDTM(1), 10); |
| 66 | + private Array array10char2 = mkArray(new DataOrgDTM(2), 10); |
| 67 | + private Array array6char4 = mkArray(new DataOrgDTM(4), 6); |
| 68 | + private Array array10char5 = mkArray(new DataOrgDTM(5), 3); |
| 69 | + |
| 70 | + //------------------------------------------------------------------------------------- |
| 71 | + // get string rep of an array of various sized character elements |
| 72 | + //------------------------------------------------------------------------------------- |
| 73 | + @Test |
| 74 | + public void testGetRep_1bytechar() { |
| 75 | + // because the char size is 1, default charset will be us-ascii, which matches character element size |
| 76 | + ByteMemBufferImpl buf = mb(false, 'h', 'e', 'l', 'l', 'o', 0, 'x', 'y', 0, 0); |
| 77 | + |
| 78 | + assertEquals("\"hello\"", |
| 79 | + array10char1.getRepresentation(buf, newset(), array10char1.getLength())); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetRep_2bytechar() { |
| 84 | + // because char size is 2, default charset will be utf-16, which matches character element size |
| 85 | + ByteMemBufferImpl buf = |
| 86 | + mb(false, 'h', 0, 'e', 0, 'l', 0, 'l', 0, 'o', 0, 0, 0, 'x', 'y', 0, 0, 0, 0, 0, 0); |
| 87 | + |
| 88 | + assertEquals("u\"hello\"", |
| 89 | + array10char2.getRepresentation(buf, newset(), array10char2.getLength())); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testGetRep_4bytechar() { |
| 94 | + // because char size is 4, default charset will be utf-32, which matches character element size |
| 95 | + ByteMemBufferImpl buf = mb(false, 'h', 0, 0, 0, 'e', 0, 0, 0, 'l', 0, 0, 0, 'l', 0, 0, 0, |
| 96 | + 'o', 0, 0, 0, 0, 0, 0, 0, 'x', 'y', 0, 0, 0, 0, 0, 0); |
| 97 | + |
| 98 | + assertEquals("U\"hello\"", |
| 99 | + array6char4.getRepresentation(buf, newset(), array6char4.getLength())); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGetRep_5bytechar() { |
| 104 | + // because the char size isn't normal, charset will default to us-ascii, which does not match |
| 105 | + // the element size of the array, triggering padding-removal in the stringdatainstance code. |
| 106 | + ByteMemBufferImpl buf = |
| 107 | + mb(false, 'h', 'x', 'x', 'x', 'x', 'e', 'x', 'x', 'x', 'x', 0, 0, 0, 0, 0); |
| 108 | + |
| 109 | + assertEquals("\"he\"", |
| 110 | + array10char5.getRepresentation(buf, newset(), array10char5.getLength())); |
| 111 | + } |
| 112 | +} |
0 commit comments