Skip to content

Commit 4bf0398

Browse files
committed
test: add tests for isValidItx, getTokenID & getContractAddress methods
1 parent bacbe86 commit 4bf0398

File tree

1 file changed

+221
-94
lines changed

1 file changed

+221
-94
lines changed

backend/pkg/executionlayer/transformer_test.go

Lines changed: 221 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -206,100 +206,6 @@ func TestTransformERC20(t *testing.T) {
206206
}
207207
}
208208

209-
func TestGetTxRecipient(t *testing.T) {
210-
tests := []struct {
211-
name string
212-
tx *types.Eth1Transaction
213-
expectedTo []byte
214-
expectedIsContract bool
215-
}{
216-
{
217-
name: "normal transaction with recipient",
218-
tx: &types.Eth1Transaction{
219-
To: alice,
220-
ContractAddress: common.Address{}.Bytes(),
221-
},
222-
expectedTo: alice,
223-
expectedIsContract: false,
224-
},
225-
{
226-
name: "contract creation transaction",
227-
tx: &types.Eth1Transaction{
228-
ContractAddress: contract,
229-
},
230-
expectedTo: contract,
231-
expectedIsContract: true,
232-
},
233-
{
234-
name: "transaction with no recipient",
235-
tx: &types.Eth1Transaction{
236-
To: common.Address{}.Bytes(),
237-
ContractAddress: common.Address{}.Bytes(),
238-
},
239-
expectedTo: common.Address{}.Bytes(),
240-
expectedIsContract: false,
241-
},
242-
}
243-
244-
for _, tt := range tests {
245-
t.Run(tt.name, func(t *testing.T) {
246-
to, isContract := getTxRecipient(tt.tx)
247-
if !bytes.Equal(to, tt.expectedTo) {
248-
t.Errorf("got %v, want %v", to, tt.expectedTo)
249-
}
250-
if isContract != tt.expectedIsContract {
251-
t.Errorf("got %v, want %v", isContract, tt.expectedIsContract)
252-
}
253-
})
254-
}
255-
}
256-
257-
func TestGetMethodSignature(t *testing.T) {
258-
tests := []struct {
259-
name string
260-
tx *types.Eth1Transaction
261-
expected []byte
262-
}{
263-
{
264-
name: "transaction with data longer than 4 bytes",
265-
tx: &types.Eth1Transaction{
266-
Data: []byte("123456789"),
267-
},
268-
expected: []byte("1234"),
269-
},
270-
{
271-
name: "transaction with data exactly 4 bytes",
272-
tx: &types.Eth1Transaction{
273-
Data: []byte("1234"),
274-
},
275-
expected: []byte("1234"),
276-
},
277-
{
278-
name: "transaction with data shorter than 4 bytes",
279-
tx: &types.Eth1Transaction{
280-
Data: []byte("12"),
281-
},
282-
expected: []byte{},
283-
},
284-
{
285-
name: "transaction with no data",
286-
tx: &types.Eth1Transaction{
287-
Data: []byte{},
288-
},
289-
expected: []byte{},
290-
},
291-
}
292-
293-
for _, tt := range tests {
294-
t.Run(tt.name, func(t *testing.T) {
295-
method := getMethodSignature(tt.tx)
296-
if !bytes.Equal(method, tt.expected) {
297-
t.Errorf("got %v, want %v", method, tt.expected)
298-
}
299-
})
300-
}
301-
}
302-
303209
func TestUpdateITxStatus(t *testing.T) {
304210
tests := []struct {
305211
name string
@@ -604,6 +510,56 @@ func TestIsValidERC1155Log(t *testing.T) {
604510
}
605511
}
606512

513+
func TestIsValidItx(t *testing.T) {
514+
tests := []struct {
515+
name string
516+
itx *types.Eth1InternalTransaction
517+
expected bool
518+
}{
519+
{
520+
name: "valid internal transaction",
521+
itx: &types.Eth1InternalTransaction{
522+
Path: "0,1",
523+
Value: big.NewInt(100).Bytes(),
524+
},
525+
expected: true,
526+
},
527+
{
528+
name: "invalid internal transaction with path '0'",
529+
itx: &types.Eth1InternalTransaction{
530+
Path: "0",
531+
Value: big.NewInt(100).Bytes(),
532+
},
533+
expected: false,
534+
},
535+
{
536+
name: "invalid internal transaction with path is '[]'",
537+
itx: &types.Eth1InternalTransaction{
538+
Path: "[]",
539+
Value: big.NewInt(100).Bytes(),
540+
},
541+
expected: false,
542+
},
543+
{
544+
name: "invalid internal transaction with value 0",
545+
itx: &types.Eth1InternalTransaction{
546+
Path: "0,1",
547+
Value: []byte{0x0},
548+
},
549+
expected: false,
550+
},
551+
}
552+
553+
for _, tt := range tests {
554+
t.Run(tt.name, func(t *testing.T) {
555+
result := isValidItx(tt.itx)
556+
if result != tt.expected {
557+
t.Errorf("got %v, want %v", result, tt.expected)
558+
}
559+
})
560+
}
561+
}
562+
607563
func TestGetLogTopics(t *testing.T) {
608564
topic1 := "0x1234"
609565
topic2 := "0x2345"
@@ -652,6 +608,177 @@ func TestGetLogTopics(t *testing.T) {
652608
}
653609
}
654610

611+
func TestGetTxRecipient(t *testing.T) {
612+
tests := []struct {
613+
name string
614+
tx *types.Eth1Transaction
615+
expectedTo []byte
616+
expectedIsContract bool
617+
}{
618+
{
619+
name: "normal transaction with recipient",
620+
tx: &types.Eth1Transaction{
621+
To: alice,
622+
ContractAddress: common.Address{}.Bytes(),
623+
},
624+
expectedTo: alice,
625+
expectedIsContract: false,
626+
},
627+
{
628+
name: "contract creation transaction",
629+
tx: &types.Eth1Transaction{
630+
ContractAddress: contract,
631+
},
632+
expectedTo: contract,
633+
expectedIsContract: true,
634+
},
635+
{
636+
name: "transaction with no recipient",
637+
tx: &types.Eth1Transaction{
638+
To: common.Address{}.Bytes(),
639+
ContractAddress: common.Address{}.Bytes(),
640+
},
641+
expectedTo: common.Address{}.Bytes(),
642+
expectedIsContract: false,
643+
},
644+
}
645+
646+
for _, tt := range tests {
647+
t.Run(tt.name, func(t *testing.T) {
648+
to, isContract := getTxRecipient(tt.tx)
649+
if !bytes.Equal(to, tt.expectedTo) {
650+
t.Errorf("got %v, want %v", to, tt.expectedTo)
651+
}
652+
if isContract != tt.expectedIsContract {
653+
t.Errorf("got %v, want %v", isContract, tt.expectedIsContract)
654+
}
655+
})
656+
}
657+
}
658+
659+
func TestGetTokenID(t *testing.T) {
660+
tests := []struct {
661+
name string
662+
transfer *contracts.ERC721Transfer
663+
expected *big.Int
664+
}{
665+
{
666+
name: "transfer with token ID",
667+
transfer: &contracts.ERC721Transfer{
668+
TokenId: big.NewInt(123),
669+
},
670+
expected: big.NewInt(123),
671+
},
672+
{
673+
name: "transfer with empty token ID",
674+
transfer: &contracts.ERC721Transfer{
675+
TokenId: nil,
676+
},
677+
expected: big.NewInt(0),
678+
},
679+
}
680+
681+
for _, tt := range tests {
682+
t.Run(tt.name, func(t *testing.T) {
683+
result := getTokenID(tt.transfer)
684+
if result.Cmp(tt.expected) != 0 {
685+
t.Errorf("got %v, want %v", result, tt.expected)
686+
}
687+
})
688+
}
689+
}
690+
691+
func TestGetMethodSignature(t *testing.T) {
692+
tests := []struct {
693+
name string
694+
tx *types.Eth1Transaction
695+
expected []byte
696+
}{
697+
{
698+
name: "transaction with data longer than 4 bytes",
699+
tx: &types.Eth1Transaction{
700+
Data: []byte("123456789"),
701+
},
702+
expected: []byte("1234"),
703+
},
704+
{
705+
name: "transaction with data exactly 4 bytes",
706+
tx: &types.Eth1Transaction{
707+
Data: []byte("1234"),
708+
},
709+
expected: []byte("1234"),
710+
},
711+
{
712+
name: "transaction with data shorter than 4 bytes",
713+
tx: &types.Eth1Transaction{
714+
Data: []byte("12"),
715+
},
716+
expected: []byte{},
717+
},
718+
{
719+
name: "transaction with no data",
720+
tx: &types.Eth1Transaction{
721+
Data: []byte{},
722+
},
723+
expected: []byte{},
724+
},
725+
}
726+
727+
for _, tt := range tests {
728+
t.Run(tt.name, func(t *testing.T) {
729+
method := getMethodSignature(tt.tx)
730+
if !bytes.Equal(method, tt.expected) {
731+
t.Errorf("got %v, want %v", method, tt.expected)
732+
}
733+
})
734+
}
735+
}
736+
737+
func TestGetContractAddress(t *testing.T) {
738+
tests := []struct {
739+
name string
740+
itx *types.Eth1InternalTransaction
741+
expected []byte
742+
}{
743+
{
744+
name: "create type transaction",
745+
itx: &types.Eth1InternalTransaction{
746+
Type: "create",
747+
From: alice,
748+
To: contract,
749+
},
750+
expected: contract,
751+
},
752+
{
753+
name: "suicide type transaction",
754+
itx: &types.Eth1InternalTransaction{
755+
Type: "suicide",
756+
From: contract,
757+
To: alice,
758+
},
759+
expected: contract,
760+
},
761+
{
762+
name: "invalid type transaction",
763+
itx: &types.Eth1InternalTransaction{
764+
Type: "invalid",
765+
From: alice,
766+
To: contract,
767+
},
768+
expected: contract,
769+
},
770+
}
771+
772+
for _, tt := range tests {
773+
t.Run(tt.name, func(t *testing.T) {
774+
result := getContractAddress(tt.itx)
775+
if !bytes.Equal(result, tt.expected) {
776+
t.Errorf("got %v, want %v", result, tt.expected)
777+
}
778+
})
779+
}
780+
}
781+
655782
func TestGetERC20TransferValue(t *testing.T) {
656783
tests := []struct {
657784
name string

0 commit comments

Comments
 (0)