Skip to content

Commit 7145524

Browse files
committed
test: add unit tests
Signed-off-by: dosi <dosi.kolev@limechain.tech>
1 parent 9d0671e commit 7145524

4 files changed

Lines changed: 100 additions & 38 deletions

sdk/authorization_unit_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//go:build all || unit
2+
3+
package hiero
4+
5+
// SPDX-License-Identifier: Apache-2.0
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestUnitAuthorizationGettersSetters(t *testing.T) {
15+
t.Parallel()
16+
17+
chainId := []byte{0x01, 0x2a}
18+
addr := []byte{0xAA, 0xBB}
19+
r := []byte{0x11}
20+
s := []byte{0x22}
21+
22+
auth := NewAuthorization(chainId, addr, 5, 1, r, s)
23+
24+
assert.Equal(t, chainId, auth.GetChainId())
25+
assert.Equal(t, addr, auth.GetAddress())
26+
assert.Equal(t, uint64(5), auth.GetNonce())
27+
assert.Equal(t, uint32(1), auth.GetYParity())
28+
assert.Equal(t, r, auth.GetR())
29+
assert.Equal(t, s, auth.GetS())
30+
31+
auth.SetChainId([]byte{0x09}).
32+
SetAddress([]byte{0xCC}).
33+
SetNonce(7).
34+
SetYParity(0).
35+
SetR([]byte{0x33}).
36+
SetS([]byte{0x44})
37+
38+
assert.Equal(t, []byte{0x09}, auth.GetChainId())
39+
assert.Equal(t, []byte{0xCC}, auth.GetAddress())
40+
assert.Equal(t, uint64(7), auth.GetNonce())
41+
assert.Equal(t, uint32(0), auth.GetYParity())
42+
assert.Equal(t, []byte{0x33}, auth.GetR())
43+
assert.Equal(t, []byte{0x44}, auth.GetS())
44+
}
45+
46+
func TestUnitAuthorizationString(t *testing.T) {
47+
t.Parallel()
48+
49+
auth := NewAuthorization([]byte{0xab, 0xcd}, []byte{0xde}, 3, 1, []byte{0x11}, []byte{0x22})
50+
str := auth.String()
51+
assert.Contains(t, str, "ChainId:")
52+
assert.Contains(t, str, "abcd")
53+
assert.Contains(t, str, "Nonce: 3")
54+
assert.Contains(t, str, "YParity: 1")
55+
}
56+
57+
func TestUnitAuthorizationRLPRoundTrip(t *testing.T) {
58+
t.Parallel()
59+
60+
auth := NewAuthorization([]byte{0x01, 0x2a}, make([]byte, 20), 5, 1, make([]byte, 32), make([]byte, 32))
61+
62+
encoded, err := auth._toRLPItem().Write()
63+
require.NoError(t, err)
64+
65+
item := NewRLPItem(LIST_TYPE)
66+
require.NoError(t, item.Read(encoded))
67+
68+
decoded, err := _authorizationFromRLPItem(item)
69+
require.NoError(t, err)
70+
assert.Equal(t, auth, decoded)
71+
}
72+
73+
func TestUnitAuthorizationFromRLPItemErrors(t *testing.T) {
74+
t.Parallel()
75+
76+
// Not a list.
77+
_, err := _authorizationFromRLPItem(NewRLPItem(VALUE_TYPE).AssignValue([]byte{0x01}))
78+
assert.Error(t, err)
79+
80+
// A list, but not 6 elements.
81+
short := NewRLPItem(LIST_TYPE)
82+
short.PushBack(NewRLPItem(VALUE_TYPE).AssignValue([]byte{0x01}))
83+
_, err = _authorizationFromRLPItem(short)
84+
assert.Error(t, err)
85+
}

sdk/ethereum_eip7702_transaction_unit_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ func TestUnitEthereumEIP7702AuthorizationList(t *testing.T) {
7777
t.Parallel()
7878

7979
to, _ := hex.DecodeString("000000000000000000000000000000000000041d")
80-
auth1 := AuthorizationTuple{{0x01, 0x2a}, to, {0x00}, {0x00}, make([]byte, 32), make([]byte, 32)}
81-
auth2 := AuthorizationTuple{{0x01, 0x2a}, to, {0x01}, {0x01}, make([]byte, 32), make([]byte, 32)}
80+
auth1 := NewAuthorization([]byte{0x01, 0x2a}, to, 0, 0, make([]byte, 32), make([]byte, 32))
81+
auth2 := NewAuthorization([]byte{0x01, 0x2a}, to, 1, 1, make([]byte, 32), make([]byte, 32))
8282

83-
tx := (&EthereumEIP7702Transaction{}).SetAuthorizationList([]AuthorizationTuple{auth1})
83+
tx := (&EthereumEIP7702Transaction{}).SetAuthorizationList([]Authorization{auth1})
8484
require.Equal(t, 1, len(tx.GetAuthorizationList()))
8585

8686
tx.AddAuthorization(auth2)
@@ -101,8 +101,8 @@ func TestUnitEthereumEIP7702ToBytesRoundTripWithAuthList(t *testing.T) {
101101
t.Parallel()
102102

103103
to, _ := hex.DecodeString("000000000000000000000000000000000000041d")
104-
// Canonical (non-zero / minimal) field bytes so the RLP round-trip is exact.
105-
auth := AuthorizationTuple{{0x01, 0x2a}, to, {0x05}, {0x01}, make([]byte, 32), make([]byte, 32)}
104+
// Canonical (non-zero / minimal) field values so the RLP round-trip is exact.
105+
auth := NewAuthorization([]byte{0x01, 0x2a}, to, 5, 1, make([]byte, 32), make([]byte, 32))
106106

107107
tx := (&EthereumEIP7702Transaction{}).
108108
SetChainId(298).
@@ -132,7 +132,7 @@ func TestUnitEthereumEIP7702String(t *testing.T) {
132132
to, _ := hex.DecodeString("000000000000000000000000000000000000041d")
133133
tx := (&EthereumEIP7702Transaction{}).
134134
SetChainId(1).
135-
AddAuthorization(AuthorizationTuple{{0x01}, to, {0x00}, {0x00}, {0x11}, {0x22}})
135+
AddAuthorization(NewAuthorization([]byte{0x01}, to, 0, 0, []byte{0x11}, []byte{0x22}))
136136
s := tx.String()
137137
assert.Contains(t, s, "ChainId:")
138138
assert.Contains(t, s, "AuthorizationList:")

sdk/ethereum_transaction_body_unit_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,7 @@ func TestUnitEthereumEIP7702BuilderAndSign(t *testing.T) {
9292
key := newTestEcdsaKey(t)
9393

9494
to, _ := hex.DecodeString("000000000000000000000000000000000000041d")
95-
authTuple := AuthorizationTuple{
96-
{0x01, 0x2a},
97-
to,
98-
{0x00},
99-
{0x00},
100-
make([]byte, 32),
101-
make([]byte, 32),
102-
}
95+
auth := NewAuthorization([]byte{0x01, 0x2a}, to, 0, 0, make([]byte, 32), make([]byte, 32))
10396

10497
tx := (&EthereumEIP7702Transaction{}).
10598
SetChainId(298).
@@ -110,7 +103,7 @@ func TestUnitEthereumEIP7702BuilderAndSign(t *testing.T) {
110103
SetTo(to).
111104
SetValue(big.NewInt(0)).
112105
SetCallData([]byte{0x01, 0x02}).
113-
AddAuthorization(authTuple)
106+
AddAuthorization(auth)
114107

115108
signed, err := tx.Sign(key)
116109
require.NoError(t, err)

sdk/ethereum_transaction_e2e_test.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,10 @@ func TestIntegrationEthereumEIP7702Transaction(t *testing.T) {
319319
authR := authSignedBytes[0:32]
320320
authS := authSignedBytes[32:64]
321321
authRecoveryId := ecdsaPrivateKey.GetRecoveryId(authR, authS, authMessage)
322-
authYParity := []byte{}
323-
if authRecoveryId != 0 {
324-
authYParity = []byte{byte(authRecoveryId)}
325-
}
326322

327-
// Create authorization tuple: [chainId, contractAddress, nonce, yParity, r, s]
328-
authorizationTuple := AuthorizationTuple{
329-
chainId,
330-
contractAddressForAuthorization,
331-
nonce,
332-
authYParity,
333-
authR,
334-
authS,
335-
}
336-
authorizationList := []AuthorizationTuple{authorizationTuple}
323+
// Create authorization: [chainId, address, nonce, yParity, r, s]
324+
authorization := NewAuthorization(chainId, contractAddressForAuthorization, _ethBytesToUint64(nonce), uint32(authRecoveryId), authR, authS)
325+
authorizationList := []Authorization{authorization}
337326

338327
messageBytes, err := getEIP7702CallData(chainId, nonce, maxPriorityGas, maxGas, gasLimitBytes, contractBytes, value, callDataBytes, accessList, authorizationList, ecdsaPrivateKey)
339328
require.NoError(t, err)
@@ -364,7 +353,7 @@ func TestIntegrationEthereumEIP7702Transaction(t *testing.T) {
364353
require.NoError(t, err)
365354
}
366355

367-
func getEIP7702CallData(chainId, nonce, maxPriorityGas, maxGas, gasLimitBytes, contractBytes, value, callDataBytes []byte, accessList [][]byte, authorizationList []AuthorizationTuple, ecdsaPrivateKey PrivateKey) ([]byte, error) {
356+
func getEIP7702CallData(chainId, nonce, maxPriorityGas, maxGas, gasLimitBytes, contractBytes, value, callDataBytes []byte, accessList [][]byte, authorizationList []Authorization, ecdsaPrivateKey PrivateKey) ([]byte, error) {
368357
objectsList := &RLPItem{}
369358
objectsList.AssignList()
370359
objectsList.PushBack(NewRLPItem(VALUE_TYPE).AssignValue(chainId))
@@ -383,15 +372,10 @@ func getEIP7702CallData(chainId, nonce, maxPriorityGas, maxGas, gasLimitBytes, c
383372
}
384373
objectsList.PushBack(accessListItem)
385374

386-
// Add authorization list: array of [chainId, contractAddress, nonce, yParity, r, s] tuples
375+
// Add authorization list: array of [chainId, address, nonce, yParity, r, s] tuples
387376
authorizationListItem := NewRLPItem(LIST_TYPE)
388-
for _, authTuple := range authorizationList {
389-
// Each authorization entry is a tuple: [chainId, contractAddress, nonce, yParity, r, s]
390-
authTupleItem := NewRLPItem(LIST_TYPE)
391-
for i := 0; i < 6; i++ {
392-
authTupleItem.PushBack(NewRLPItem(VALUE_TYPE).AssignValue(authTuple[i]))
393-
}
394-
authorizationListItem.PushBack(authTupleItem)
377+
for _, auth := range authorizationList {
378+
authorizationListItem.PushBack(auth._toRLPItem())
395379
}
396380
objectsList.PushBack(authorizationListItem)
397381

0 commit comments

Comments
 (0)