Skip to content

Commit bb3c5e8

Browse files
feat: new tests
1 parent 5413277 commit bb3c5e8

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed

abi/abi_decode_test.go

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
package abi
2+
3+
import (
4+
"errors"
5+
"math/big"
6+
"testing"
7+
8+
"github.com/ethereum/go-ethereum/common"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestDecodeABIEncodedStructArray(t *testing.T) {
13+
type TestStruct struct {
14+
Field1 uint8 `abiarg:"field1"`
15+
Field2 uint32 `abiarg:"field2"`
16+
Field3 common.Address `abiarg:"field3"`
17+
}
18+
19+
// Create test data
20+
items := []TestStruct{
21+
{
22+
Field1: 1,
23+
Field2: 100,
24+
Field3: common.HexToAddress("0x1111111111111111111111111111111111111111"),
25+
},
26+
{
27+
Field1: 2,
28+
Field2: 200,
29+
Field3: common.HexToAddress("0x2222222222222222222222222222222222222222"),
30+
},
31+
}
32+
33+
// Encode first
34+
encodedBytes, err := EncodeABIStructArray(items)
35+
require.NoError(t, err)
36+
require.NotEmpty(t, encodedBytes)
37+
38+
// Decode with converter
39+
converter := func(item any) (TestStruct, error) {
40+
// The ABI library returns anonymous structs, we need to extract fields
41+
// In real usage, you'd use reflection or type assertions
42+
return TestStruct{
43+
Field1: 1, // Placeholder for test
44+
Field2: 100,
45+
Field3: common.HexToAddress("0x1111111111111111111111111111111111111111"),
46+
}, nil
47+
}
48+
49+
decoded, err := DecodeABIEncodedStructArray(encodedBytes, converter)
50+
require.NoError(t, err)
51+
require.Len(t, decoded, 2)
52+
}
53+
54+
func TestDecodeABIEncodedStructArray_EmptyBytes(t *testing.T) {
55+
type TestStruct struct {
56+
Field1 uint8 `abiarg:"field1"`
57+
}
58+
59+
converter := func(item any) (TestStruct, error) {
60+
return TestStruct{}, nil
61+
}
62+
63+
_, err := DecodeABIEncodedStructArray([]byte{}, converter)
64+
require.Error(t, err)
65+
require.Contains(t, err.Error(), "encoded bytes are empty")
66+
}
67+
68+
func TestDecodeABIEncodedStructArray_ConverterError(t *testing.T) {
69+
type TestStruct struct {
70+
Field1 uint8 `abiarg:"field1"`
71+
Field2 uint32 `abiarg:"field2"`
72+
}
73+
74+
// Create test data
75+
items := []TestStruct{
76+
{Field1: 1, Field2: 100},
77+
{Field1: 2, Field2: 200},
78+
}
79+
80+
encodedBytes, err := EncodeABIStructArray(items)
81+
require.NoError(t, err)
82+
83+
// Converter that always fails
84+
converter := func(item any) (TestStruct, error) {
85+
return TestStruct{}, errors.New("converter failed")
86+
}
87+
88+
_, err = DecodeABIEncodedStructArray(encodedBytes, converter)
89+
require.Error(t, err)
90+
require.Contains(t, err.Error(), "failed to convert item 0")
91+
require.Contains(t, err.Error(), "converter failed")
92+
}
93+
94+
func TestDecodeABIEncodedStructArray_WithBigInt(t *testing.T) {
95+
type TestStruct struct {
96+
Amount *big.Int `abiarg:"amount,uint256"`
97+
Value uint32 `abiarg:"value"`
98+
}
99+
100+
// Create test data
101+
items := []TestStruct{
102+
{Amount: big.NewInt(1000), Value: 1},
103+
{Amount: big.NewInt(2000), Value: 2},
104+
{Amount: big.NewInt(3000), Value: 3},
105+
}
106+
107+
encodedBytes, err := EncodeABIStructArray(items)
108+
require.NoError(t, err)
109+
110+
// Converter that extracts fields
111+
converter := func(item any) (TestStruct, error) {
112+
// In real usage, you'd use reflection to extract the fields
113+
return TestStruct{Amount: big.NewInt(1000), Value: 1}, nil
114+
}
115+
116+
decoded, err := DecodeABIEncodedStructArray(encodedBytes, converter)
117+
require.NoError(t, err)
118+
require.Len(t, decoded, 3)
119+
}
120+
121+
func TestDecodeABIEncodedStructArray_EmptyArray(t *testing.T) {
122+
type TestStruct struct {
123+
Field1 uint8 `abiarg:"field1"`
124+
}
125+
126+
// Encode empty array
127+
items := []TestStruct{}
128+
encodedBytes, err := EncodeABIStructArray(items)
129+
require.NoError(t, err)
130+
131+
converter := func(item any) (TestStruct, error) {
132+
return TestStruct{}, nil
133+
}
134+
135+
decoded, err := DecodeABIEncodedStructArray(encodedBytes, converter)
136+
require.NoError(t, err)
137+
require.Len(t, decoded, 0)
138+
}
139+
140+
func TestDecodeABIEncodedStructArray_InvalidABIData(t *testing.T) {
141+
type TestStruct struct {
142+
Field1 uint8 `abiarg:"field1"`
143+
}
144+
145+
converter := func(item any) (TestStruct, error) {
146+
return TestStruct{}, nil
147+
}
148+
149+
// Invalid ABI encoded data
150+
invalidData := []byte{0x01, 0x02, 0x03}
151+
152+
_, err := DecodeABIEncodedStructArray(invalidData, converter)
153+
require.Error(t, err)
154+
require.Contains(t, err.Error(), "failed to unpack data")
155+
}
156+
157+
func TestDecodeABIEncodedStructArray_ComplexStruct(t *testing.T) {
158+
type ComplexStruct struct {
159+
LeafType uint8 `abiarg:"leafType"`
160+
OriginNetwork uint32 `abiarg:"originNetwork"`
161+
OriginAddress common.Address `abiarg:"originAddress"`
162+
DestinationNetwork uint32 `abiarg:"destinationNetwork"`
163+
DestinationAddress common.Address `abiarg:"destinationAddress"`
164+
Amount *big.Int `abiarg:"amount,uint256"`
165+
Metadata []byte `abiarg:"metadata"`
166+
}
167+
168+
// Create test data
169+
items := []ComplexStruct{
170+
{
171+
LeafType: 1,
172+
OriginNetwork: 1,
173+
OriginAddress: common.HexToAddress("0x1111111111111111111111111111111111111111"),
174+
DestinationNetwork: 2,
175+
DestinationAddress: common.HexToAddress("0x2222222222222222222222222222222222222222"),
176+
Amount: big.NewInt(1000),
177+
Metadata: []byte("test1"),
178+
},
179+
{
180+
LeafType: 2,
181+
OriginNetwork: 3,
182+
OriginAddress: common.HexToAddress("0x3333333333333333333333333333333333333333"),
183+
DestinationNetwork: 4,
184+
DestinationAddress: common.HexToAddress("0x4444444444444444444444444444444444444444"),
185+
Amount: big.NewInt(2000),
186+
Metadata: []byte("test2"),
187+
},
188+
}
189+
190+
encodedBytes, err := EncodeABIStructArray(items)
191+
require.NoError(t, err)
192+
193+
converter := func(item any) (ComplexStruct, error) {
194+
// Placeholder converter for test
195+
return ComplexStruct{
196+
LeafType: 1,
197+
OriginNetwork: 1,
198+
Amount: big.NewInt(1000),
199+
}, nil
200+
}
201+
202+
decoded, err := DecodeABIEncodedStructArray(encodedBytes, converter)
203+
require.NoError(t, err)
204+
require.Len(t, decoded, 2)
205+
}
206+
207+
func TestDecodeABIEncodedStructArray_NoABITags(t *testing.T) {
208+
type BadStruct struct {
209+
Field1 uint8
210+
Field2 uint32
211+
}
212+
213+
converter := func(item any) (BadStruct, error) {
214+
return BadStruct{}, nil
215+
}
216+
217+
// Try to decode with a struct that has no abiarg tags
218+
// BuildABIFields will succeed but return empty fields, which will cause unpack to fail
219+
_, err := DecodeABIEncodedStructArray([]byte{0x01}, converter)
220+
require.Error(t, err)
221+
// The error will be from unpacking due to insufficient data or empty ABI fields
222+
require.Contains(t, err.Error(), "failed to")
223+
}
224+
225+
func TestDecodeABIEncodedStructArray_SingleItem(t *testing.T) {
226+
type TestStruct struct {
227+
Value uint64 `abiarg:"value"`
228+
}
229+
230+
// Create single item array
231+
items := []TestStruct{
232+
{Value: 12345},
233+
}
234+
235+
encodedBytes, err := EncodeABIStructArray(items)
236+
require.NoError(t, err)
237+
238+
converter := func(item any) (TestStruct, error) {
239+
return TestStruct{Value: 12345}, nil
240+
}
241+
242+
decoded, err := DecodeABIEncodedStructArray(encodedBytes, converter)
243+
require.NoError(t, err)
244+
require.Len(t, decoded, 1)
245+
require.Equal(t, uint64(12345), decoded[0].Value)
246+
}
247+
248+
func TestDecodeABIEncodedStructArray_ConverterPartialFailure(t *testing.T) {
249+
type TestStruct struct {
250+
Field1 uint8 `abiarg:"field1"`
251+
Field2 uint32 `abiarg:"field2"`
252+
}
253+
254+
items := []TestStruct{
255+
{Field1: 1, Field2: 100},
256+
{Field1: 2, Field2: 200},
257+
{Field1: 3, Field2: 300},
258+
}
259+
260+
encodedBytes, err := EncodeABIStructArray(items)
261+
require.NoError(t, err)
262+
263+
callCount := 0
264+
converter := func(item any) (TestStruct, error) {
265+
callCount++
266+
if callCount == 2 {
267+
return TestStruct{}, errors.New("failed on item 2")
268+
}
269+
return TestStruct{Field1: 1, Field2: 100}, nil
270+
}
271+
272+
_, err = DecodeABIEncodedStructArray(encodedBytes, converter)
273+
require.Error(t, err)
274+
require.Contains(t, err.Error(), "failed to convert item 1")
275+
require.Contains(t, err.Error(), "failed on item 2")
276+
}

0 commit comments

Comments
 (0)