-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathstructs_fuzz_test.go
204 lines (168 loc) · 3.53 KB
/
structs_fuzz_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
198
199
200
201
202
203
204
package spectests
import (
"bytes"
"math/rand"
"os"
"strconv"
"strings"
"testing"
"github.com/ferranbt/fastssz/fuzz"
)
func randomInt(min, max int) int {
return min + rand.Intn(max-min)
}
const defaultFuzzCount = 100
func fuzzTestCount(t *testing.T, i string) int {
var num int
// read the number of tests from an env variable
numStr := os.Getenv("FUZZ_COUNT")
if numStr == "" {
num = defaultFuzzCount
} else {
var err error
if num, err = strconv.Atoi(numStr); err != nil {
t.Fatal(err)
}
}
if i == "BeaconState" {
// BeaconState is too big to run all the cases, execute
// only a 10% of them
return int(.1 * float64(num))
}
return num
}
func checkIsFuzzEnabled(t *testing.T) {
if strings.ToLower(os.Getenv("FUZZ_TESTS")) != "true" {
t.Skip("Fuzz testing not enabled, skipping")
}
}
func TestFuzzMarshalWithWrongSizes(t *testing.T) {
checkIsFuzzEnabled(t)
for name, codec := range codecs {
count := fuzzTestCount(t, name)
for i := 0; i < count; i++ {
obj := codec("")
f := fuzz.New()
f.SetFailureRatio(.1)
failed := f.Fuzz(obj)
if failed {
if _, err := obj.MarshalSSZTo(nil); err == nil {
t.Fatalf("%s it should have failed", name)
}
}
}
}
}
func TestErrorResponse(t *testing.T) {
codec := codecs["ErrorResponse"]
for i := 0; i < 1000; i++ {
obj := codec("")
f := fuzz.New()
f.SetFailureRatio(.1)
failed := f.Fuzz(obj)
dst, err := obj.MarshalSSZTo(nil)
if err != nil {
if !failed {
t.Fatal(err)
} else {
continue
}
}
obj2 := codec("")
if err := obj2.UnmarshalSSZ(dst); err != nil {
t.Fatal(err)
}
if !deepEqual(obj, obj2) {
t.Fatal("bad")
}
}
}
func TestFuzzEncoding(t *testing.T) {
checkIsFuzzEnabled(t)
for name, codec := range codecs {
count := fuzzTestCount(t, name)
for i := 0; i < count; i++ {
obj := codec("")
f := fuzz.New()
f.Fuzz(obj)
dst, err := obj.MarshalSSZTo(nil)
if err != nil {
t.Fatal(err)
}
obj2 := codec("")
if err := obj2.UnmarshalSSZ(dst); err != nil {
t.Fatal(err)
}
if !deepEqual(obj, obj2) {
t.Fatal("bad")
}
}
}
}
func TestFuzzUnmarshalAppend(t *testing.T) {
checkIsFuzzEnabled(t)
// Fuzz with append values between the fields
for name, codec := range codecs {
t.Logf("Process %s", name)
for j := 0; j < 5; j++ {
obj := codec("")
f := fuzz.New()
f.Fuzz(obj)
dst, err := obj.MarshalSSZTo(nil)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 100; i++ {
buf := []byte{}
pos := randomInt(0, len(dst))
size := randomInt(1, 20)
aux := make([]byte, size)
rand.Read(aux)
buf = append(buf, dst[:pos]...)
buf = append(buf, aux...)
buf = append(buf, dst[pos:]...)
obj2 := codec("")
if err := obj2.UnmarshalSSZ(buf); err == nil {
if deepEqual(obj, obj2) {
t.Fatal("bad")
}
}
}
}
}
}
func TestFuzzUnmarshalShuffle(t *testing.T) {
checkIsFuzzEnabled(t)
// Unmarshal a correct dst with shuffled data
for _, codec := range codecs {
obj := codec("")
f := fuzz.New()
f.Fuzz(obj)
dst, err := obj.MarshalSSZTo(nil)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 100; i++ {
buf := make([]byte, len(dst))
copy(buf, dst)
pos := randomInt(1, len(dst))
n := randomInt(2, 4)
rand.Read(buf[pos:min(pos+n, len(dst))])
if bytes.Equal(buf, dst) {
continue
}
obj2 := codec("")
if err := obj2.UnmarshalSSZ(buf); err == nil {
if deepEqual(obj, obj2) {
t.Fatal("bad")
}
}
}
}
}
func min(i, j int) int {
if i < j {
return i
}
return j
}