Skip to content

Commit 17e9cc2

Browse files
committed
test(logsfilter): cover all parseDataBytes type branches
1 parent 01015a2 commit 17e9cc2

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

framework/src/test/java/org/tron/common/logsfilter/EventParserTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,81 @@ public synchronized void testEventParser() {
102102

103103
}
104104

105+
@Test
106+
public void testParseDataBytesIntegerTypes() {
107+
// uint256 = 255
108+
byte[] uintData = ByteArray.fromHexString(
109+
"00000000000000000000000000000000000000000000000000000000000000ff");
110+
Assert.assertEquals("255", ContractEventParser.parseDataBytes(uintData, "uint256", 0));
111+
112+
// int256 = -1 (two's complement 0xFF..FF is signed negative one)
113+
byte[] negIntData = ByteArray.fromHexString(
114+
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
115+
Assert.assertEquals("-1", ContractEventParser.parseDataBytes(negIntData, "int256", 0));
116+
117+
// trcToken is classified as INT_NUMBER
118+
byte[] tokenData = ByteArray.fromHexString(
119+
"0000000000000000000000000000000000000000000000000000000000000064");
120+
Assert.assertEquals("100", ContractEventParser.parseDataBytes(tokenData, "trcToken", 0));
121+
}
122+
123+
@Test
124+
public void testParseDataBytesBool() {
125+
byte[] trueData = ByteArray.fromHexString(
126+
"0000000000000000000000000000000000000000000000000000000000000001");
127+
Assert.assertEquals("true", ContractEventParser.parseDataBytes(trueData, "bool", 0));
128+
129+
byte[] falseData = ByteArray.fromHexString(
130+
"0000000000000000000000000000000000000000000000000000000000000000");
131+
Assert.assertEquals("false", ContractEventParser.parseDataBytes(falseData, "bool", 0));
132+
}
133+
134+
@Test
135+
public void testParseDataBytesFixedBytes() {
136+
String hex = "1234567890abcdef0000000000000000000000000000000000000000000000ff";
137+
byte[] data = ByteArray.fromHexString(hex);
138+
Assert.assertEquals(hex, ContractEventParser.parseDataBytes(data, "bytes32", 0));
139+
}
140+
141+
@Test
142+
public void testParseDataBytesAddress() {
143+
Wallet.setAddressPreFixByte(ADD_PRE_FIX_BYTE_MAINNET);
144+
// last 20 bytes = ca35...733c => Base58Check = TUQPrDEJkV4ttkrL7cVv1p3mikWYfM7LWt
145+
byte[] data = ByteArray.fromHexString(
146+
"000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c");
147+
Assert.assertEquals("TUQPrDEJkV4ttkrL7cVv1p3mikWYfM7LWt",
148+
ContractEventParser.parseDataBytes(data, "address", 0));
149+
}
150+
151+
@Test
152+
public void testParseDataBytesDynamicBytes() {
153+
// offset 0x20 | length 3 | 0x010203 padded to 32 bytes
154+
byte[] data = ByteArray.fromHexString(
155+
"0000000000000000000000000000000000000000000000000000000000000020"
156+
+ "0000000000000000000000000000000000000000000000000000000000000003"
157+
+ "0102030000000000000000000000000000000000000000000000000000000000");
158+
Assert.assertEquals("010203", ContractEventParser.parseDataBytes(data, "bytes", 0));
159+
}
160+
161+
@Test
162+
public void testParseDataBytesEmptyString() {
163+
// offset 0x20 | length 0
164+
byte[] data = ByteArray.fromHexString(
165+
"0000000000000000000000000000000000000000000000000000000000000020"
166+
+ "0000000000000000000000000000000000000000000000000000000000000000");
167+
Assert.assertEquals("", ContractEventParser.parseDataBytes(data, "string", 0));
168+
}
169+
170+
@Test
171+
public void testParseDataBytesNonEmptyString() {
172+
// "hello world" is 11 ASCII bytes (68656c6c6f20776f726c64), padded to 32 bytes.
173+
byte[] data = ByteArray.fromHexString(
174+
"0000000000000000000000000000000000000000000000000000000000000020"
175+
+ "000000000000000000000000000000000000000000000000000000000000000b"
176+
+ "68656c6c6f20776f726c64000000000000000000000000000000000000000000");
177+
Assert.assertEquals("hello world", ContractEventParser.parseDataBytes(data, "string", 0));
178+
}
179+
105180
@Test
106181
public void testParseRevert() {
107182
String dataHex = "08c379a0"

0 commit comments

Comments
 (0)