-
Notifications
You must be signed in to change notification settings - Fork 166
/
test_utils.go
252 lines (206 loc) · 6.09 KB
/
test_utils.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testutils
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/blake2b"
)
type FuzzOpt func(f *fuzz.Fuzzer)
func WithNilChance(p float64) FuzzOpt {
return func(f *fuzz.Fuzzer) {
f.NilChance(p)
}
}
func WithFuzzFuncs(fuzzFns ...any) FuzzOpt {
return func(f *fuzz.Fuzzer) {
f.Funcs(fuzzFns...)
}
}
func WithNumElements(atLeast, atMost int) FuzzOpt {
return func(f *fuzz.Fuzzer) {
f.NumElements(atLeast, atMost)
}
}
func WithMaxDepth(depth int) FuzzOpt {
return func(f *fuzz.Fuzzer) {
f.MaxDepth(depth)
}
}
func CombineFuzzOpts(optsSlice ...[]FuzzOpt) []FuzzOpt {
var o []FuzzOpt
for _, opts := range optsSlice {
o = append(o, opts...)
}
return o
}
func AssertRoundTripFuzz[T any](t *testing.T, fuzzCount int, fuzzOpts ...FuzzOpt) {
f := fuzz.New().NilChance(0)
for _, opt := range fuzzOpts {
opt(f)
}
for i := 0; i < fuzzCount; i++ {
var fuzzData T
f.Fuzz(&fuzzData)
AssertRoundtrip(t, fuzzData)
}
}
func AssertDecodeNilData[T any](t *testing.T) {
var obj T
err := scale.NewDecoder(bytes.NewReader(nil)).Decode(&obj)
assert.NotNil(t, err)
}
func AssertEncodeEmptyObj[T any](t *testing.T, expectedByteLen int) {
var obj T
var buffer = bytes.Buffer{}
err := scale.NewEncoder(&buffer).Encode(obj)
assert.Nil(t, err)
assert.Len(t, buffer.Bytes(), expectedByteLen)
}
type EncodedLengthAssert struct {
Input interface{}
Expected int
}
func AssertEqual(t *testing.T, a interface{}, b interface{}) {
if reflect.DeepEqual(a, b) {
return
}
t.Errorf("Received %#v (type %v), expected %#v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b))
}
func AssertRoundtrip(t *testing.T, value interface{}) {
var buffer = bytes.Buffer{}
err := scale.NewEncoder(&buffer).Encode(value)
assert.NoError(t, err)
target := reflect.New(reflect.TypeOf(value))
err = scale.NewDecoder(&buffer).Decode(target.Interface())
assert.NoError(t, err)
AssertEqual(t, target.Elem().Interface(), value)
}
func AssertEncodedLength(t *testing.T, encodedLengthAsserts []EncodedLengthAssert) {
for _, test := range encodedLengthAsserts {
result, err := codec.EncodedLength(test.Input)
if err != nil {
t.Errorf("Encoded length error for input %v: %v\n", test.Input, err)
}
if result != test.Expected {
t.Errorf("Fail, input %v, expected %v, result %v\n", test.Input, test.Expected, result)
}
}
}
type EncodingAssert struct {
Input interface{}
Expected []byte
}
func AssertEncode(t *testing.T, encodingAsserts []EncodingAssert) {
for _, test := range encodingAsserts {
result, err := codec.Encode(test.Input)
if err != nil {
t.Errorf("Encoding error for input %v: %v\n", test.Input, err)
}
if !bytes.Equal(result, test.Expected) {
t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.Input, test.Expected, result)
}
}
}
type DecodingAssert struct {
Input []byte
Expected interface{}
}
func AssertDecode(t *testing.T, decodingAsserts []DecodingAssert) {
for _, test := range decodingAsserts {
target := reflect.New(reflect.TypeOf(test.Expected))
err := codec.Decode(test.Input, target.Interface())
if err != nil {
t.Errorf("Encoding error for input %v: %v\n", test.Input, err)
}
AssertEqual(t, target.Elem().Interface(), test.Expected)
}
}
type HashAssert struct {
Input interface{}
Expected []byte
}
func AssertHash(t *testing.T, hashAsserts []HashAssert) {
for _, test := range hashAsserts {
enc, err := codec.Encode(test.Input)
if err != nil {
t.Errorf("Couldn't encode input %v: %s", test.Input, err)
}
result := blake2b.Sum256(enc)
if !bytes.Equal(result[:], test.Expected) {
t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.Input, test.Expected, result)
}
}
}
type EncodeToHexAssert struct {
Input interface{}
Expected string
}
func AssertEncodeToHex(t *testing.T, encodeToHexAsserts []EncodeToHexAssert) {
for _, test := range encodeToHexAsserts {
result, err := codec.EncodeToHex(test.Input)
if err != nil {
t.Errorf("Hex error for input %v: %v\n", test.Input, err)
}
if result != test.Expected {
t.Errorf("Fail, input %v, expected %v, result %v\n", test.Input, test.Expected, result)
}
}
}
type StringAssert struct {
Input interface{}
Expected string
}
func AssertString(t *testing.T, stringAsserts []StringAssert) {
for _, test := range stringAsserts {
result := fmt.Sprintf("%v", test.Input)
if result != test.Expected {
t.Errorf("Fail, input %v, expected %v, result %v\n", test.Input, test.Expected, result)
}
}
}
type EqAssert struct {
Input interface{}
Other interface{}
Expected bool
}
func AssertEq(t *testing.T, eqAsserts []EqAssert) {
for _, test := range eqAsserts {
result := codec.Eq(test.Input, test.Other)
if result != test.Expected {
t.Errorf("Fail, input %v, other %v, expected %v, result %v\n", test.Input, test.Other, test.Expected, result)
}
}
}
type jsonMarshalerUnmarshaler[T any] interface {
UnmarshalJSON([]byte) error
MarshalJSON() ([]byte, error)
*T // helper type that allows us to instantiate a non-nil T
}
func AssertJSONRoundTrip[T any, U jsonMarshalerUnmarshaler[T]](t *testing.T, value U) {
b, err := value.MarshalJSON()
assert.NoError(t, err)
tt := U(new(T))
err = tt.UnmarshalJSON(b)
assert.NoError(t, err)
AssertEqual(t, value, tt)
}