-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtypes_test.go
More file actions
121 lines (112 loc) · 3.34 KB
/
Copy pathtypes_test.go
File metadata and controls
121 lines (112 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package types
import (
"encoding/hex"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidateTransaction(t *testing.T) {
validToAddress := common.HexToAddress("0x000000000000000000000000000000000000dEaD")
zeroToAddress := common.HexToAddress("0x0000000000000000000000000000000000000000")
data, err := hex.DecodeString("c6888f")
require.NoError(t, err)
invalidTxData := hexutil.Bytes(data)
data, err = hex.DecodeString("c6888fa1000000000000000000000000000000000000000000000000000000000000ab")
require.NoError(t, err)
invalidTxDataLen := hexutil.Bytes(data)
gasLimit := uint64(125_000)
nonce := uint64(1)
tests := map[string]struct {
txArgs TransactionArgs
valid bool
errMsg string
}{
"valid transaction": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: &validToAddress,
Value: (*hexutil.Big)(big.NewInt(0)),
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &hexutil.Bytes{},
},
valid: true,
errMsg: "",
},
"conflicting input and data": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: &validToAddress,
Value: (*hexutil.Big)(big.NewInt(0)),
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &invalidTxDataLen,
Input: &invalidTxData,
},
valid: false,
errMsg: `ambiguous request: both "data" and "input" are set and are not identical`,
},
"send to 0 address": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: &zeroToAddress,
Value: (*hexutil.Big)(big.NewInt(0)),
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &hexutil.Bytes{},
},
valid: false,
errMsg: "transaction recipient is the zero address",
},
"create empty contract (no value)": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: nil,
Value: (*hexutil.Big)(big.NewInt(0)),
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &hexutil.Bytes{},
},
valid: false,
errMsg: "transaction will create a contract with empty code",
},
"create empty contract (nil value)": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: nil,
Value: nil,
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &hexutil.Bytes{},
},
valid: false,
errMsg: "transaction will create a contract with empty code",
},
"create empty contract (with value)": {
txArgs: TransactionArgs{
Nonce: (*hexutil.Uint64)(&nonce),
To: nil,
Value: (*hexutil.Big)(big.NewInt(150)),
Gas: (*hexutil.Uint64)(&gasLimit),
GasPrice: (*hexutil.Big)(big.NewInt(0)),
Data: &hexutil.Bytes{},
},
valid: false,
errMsg: "transaction will create a contract with value but empty code",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.txArgs.Validate()
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
assert.ErrorContains(t, err, tc.errMsg)
}
})
}
}