-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils_test.go
197 lines (179 loc) · 5.36 KB
/
utils_test.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package utils_test
import (
"reflect"
"strings"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/smartcontractkit/chainlink/internal/cltest"
"github.com/smartcontractkit/chainlink/utils"
"github.com/stretchr/testify/assert"
"go.uber.org/multierr"
)
func TestUtils_NewBytes32ID(t *testing.T) {
t.Parallel()
id := utils.NewBytes32ID()
assert.NotContains(t, id, "-")
}
func TestUtils_IsEmptyAddress(t *testing.T) {
tests := []struct {
name string
addr common.Address
want bool
}{
{"zero address", common.Address{}, true},
{"non-zero address", cltest.NewAddress(), false},
}
for _, tt := range tests {
test := tt
t.Run(test.name, func(t *testing.T) {
t.Parallel()
actual := utils.IsEmptyAddress(test.addr)
assert.Equal(t, test.want, actual)
})
}
}
func TestUtils_StringToHex(t *testing.T) {
tests := []struct {
utf8 string
hex string
}{
{"abc", "0x616263"},
{"Hi Mom!", "0x4869204d6f6d21"},
{"", "0x"},
}
for _, tt := range tests {
test := tt
t.Run(test.utf8, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.hex, utils.StringToHex(test.utf8))
})
}
}
func TestUtils_BackoffSleeper(t *testing.T) {
bs := utils.NewBackoffSleeper()
d := 1 * time.Nanosecond
bs.Min = d
bs.Factor = 2
assert.Equal(t, d, bs.Duration())
bs.Sleep()
d2 := 2 * time.Nanosecond
assert.Equal(t, d2, bs.Duration())
}
func TestCoerceInterfaceMapToStringMap(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input interface{}
want interface{}
wantError bool
}{
{"empty map", map[interface{}]interface{}{}, map[string]interface{}{}, false},
{"simple map", map[interface{}]interface{}{"key": "value"}, map[string]interface{}{"key": "value"}, false},
{"int map", map[int]interface{}{1: "value"}, map[int]interface{}{1: "value"}, false},
{"error map", map[interface{}]interface{}{1: "value"}, map[int]interface{}{}, true},
{
"nested string map map",
map[string]interface{}{"key": map[interface{}]interface{}{"nk": "nv"}},
map[string]interface{}{"key": map[string]interface{}{"nk": "nv"}},
false,
},
{
"nested map map",
map[interface{}]interface{}{"key": map[interface{}]interface{}{"nk": "nv"}},
map[string]interface{}{"key": map[string]interface{}{"nk": "nv"}},
false,
},
{
"nested map array",
map[interface{}]interface{}{"key": []interface{}{1, "value"}},
map[string]interface{}{"key": []interface{}{1, "value"}},
false,
},
{"empty array", []interface{}{}, []interface{}{}, false},
{"simple array", []interface{}{1, "value"}, []interface{}{1, "value"}, false},
{
"error array",
[]interface{}{map[interface{}]interface{}{1: "value"}},
[]interface{}{},
true,
},
{
"nested array map",
[]interface{}{map[interface{}]interface{}{"key": map[interface{}]interface{}{"nk": "nv"}}},
[]interface{}{map[string]interface{}{"key": map[string]interface{}{"nk": "nv"}}},
false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
decoded, err := utils.CoerceInterfaceMapToStringMap(test.input)
if test.wantError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(test.want, decoded))
}
})
}
}
func TestKeccak256(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want string
}{
{"basic", "0xf00b", "0x2433bb36d5f9b14e4fea87c2d32d79abfe34e56808b891e471f4400fca2a336c"},
{"long input", "0xf00b2433bb36d5f9b14e4fea87c2d32d79abfe34e56808b891e471f4400fca2a336c", "0x6b917c56ad7bea7d09132b9e1e29bb5d9aa7d32d067c638dfa886bbbf6874cdf"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
input, err := hexutil.Decode(test.input)
assert.NoError(t, err)
result, err := utils.Keccak256(input)
assert.NoError(t, err)
assert.Equal(t, test.want, hexutil.Encode(result))
})
}
}
// From https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#test-cases
var testAddresses = []string{
"0x52908400098527886E0F7030069857D2E4169EE7",
"0x8617E340B3D01FA5F11F306F4090FD50E238070D",
"0xde709f2102306220921060314715629080e2fb77",
"0x27b1fdb04752bbc536007a920d24acb045561c26",
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
"0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
"0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
}
func TestClient_EIP55CapitalizedAddress(t *testing.T) {
valid := utils.EIP55CapitalizedAddress
for _, address := range testAddresses {
assert.True(t, valid(address))
assert.False(t, valid(strings.ToLower(address)) &&
valid(strings.ToUpper(address)))
}
}
func TestClient_ParseEthereumAddress(t *testing.T) {
parse := utils.ParseEthereumAddress
for _, address := range testAddresses {
a1, err := parse(address)
assert.NoError(t, err)
no0xPrefix := address[2:]
a2, err := parse(no0xPrefix)
assert.NoError(t, err)
assert.True(t, a1 == a2)
_, lowerErr := parse(strings.ToLower(address))
_, upperErr := parse(strings.ToUpper(address))
shouldBeError := multierr.Combine(lowerErr, upperErr)
assert.Error(t, shouldBeError)
assert.True(t, strings.Contains(shouldBeError.Error(), no0xPrefix))
}
_, notHexErr := parse("0xCeci n'est pas une chaîne hexadécimale")
assert.Error(t, notHexErr)
_, tooLongErr := parse("0x0123456789abcdef0123456789abcdef0123456789abcdef")
assert.Error(t, tooLongErr)
}