-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathfuzz_test.go
174 lines (151 loc) · 3.89 KB
/
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
package ics23
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
)
func FuzzExistenceProofCalculate(f *testing.F) {
if testing.Short() {
f.Skip("in -short mode")
}
seedJSON, err := os.ReadFile(filepath.Join("..", "testdata", "TestExistenceProofData.json"))
if err != nil {
f.Fatal(err)
}
// 2. Isolate the individual JSON per case which eases with fuzz case mutation.
existenceWholeSeedJSON := make(map[string]json.RawMessage)
if err := json.Unmarshal(seedJSON, &existenceWholeSeedJSON); err != nil {
f.Fatal(err)
}
// 3. Add the seeds:
for _, epJSON := range existenceWholeSeedJSON {
f.Add([]byte(epJSON))
}
// 4. Now run the fuzzer.
f.Fuzz(func(t *testing.T, fJSON []byte) {
ep := new(ExistenceProof)
if err := json.Unmarshal(fJSON, ep); err != nil {
return
}
// Now let's try this seemingly well formed ExistenceProof.
_, _ = ep.Calculate()
})
}
func FuzzExistenceProofCheckAgainstSpec(f *testing.F) {
if testing.Short() {
f.Skip("in -short mode")
}
seedDataMap := CheckAgainstSpecTestData(f)
for _, seed := range seedDataMap {
if seed.Err != "" {
// Erroneous data, skip it.
continue
}
if blob, err := json.Marshal(seed); err == nil {
f.Add(blob)
}
}
// 2. Now run the fuzzer.
f.Fuzz(func(t *testing.T, fJSON []byte) {
pvs := new(CheckAgainstSpecTestStruct)
if err := json.Unmarshal(fJSON, pvs); err != nil {
return
}
if pvs.Proof == nil {
// No need checking from a nil ExistenceProof.
return
}
ep, spec := pvs.Proof, pvs.Spec
_ = ep.CheckAgainstSpec(spec)
})
}
var batchVectorDataSeeds []*BatchVectorData
func init() {
svtdL := VectorsTestData()
bsL := make([]*BatchVectorData, 0, len(svtdL))
for _, tc := range svtdL {
proof, ref := LoadFile(new(testing.T), tc.Dir, tc.Filename)
// Test Calculate method and skip if it produces invalid values.
if _, err := proof.Calculate(); err != nil {
continue
}
bsL = append(bsL, &BatchVectorData{
Spec: tc.Spec,
Ref: ref,
Proof: proof,
})
}
batchVectorDataSeeds = bsL
}
func FuzzVerifyNonMembership(f *testing.F) {
if testing.Short() {
f.Skip("in -short mode")
}
// 1. Add some seeds.
for _, batchVec := range batchVectorDataSeeds {
blob, err := json.Marshal(batchVec)
if err != nil {
f.Fatal(err)
}
f.Add(blob)
}
// 2. Now run the fuzzer.
f.Fuzz(func(t *testing.T, inputJSON []byte) {
var bv BatchVectorData
if err := json.Unmarshal(inputJSON, &bv); err != nil {
return
}
if bv.Ref == nil || bv.Proof == nil || bv.Ref.RootHash == nil {
return
}
// Otherwise now run VerifyNonMembership.
_ = VerifyNonMembership(bv.Spec, bv.Ref.RootHash, bv.Proof, bv.Ref.Key)
})
}
// verifyJSON is necessary so we already have sources of Proof, Ref and Spec
// that'll be mutated by the fuzzer to get much closer to values for greater coverage.
type verifyJSON struct {
Proof *CommitmentProof
Ref *RefData
Spec *ProofSpec
}
func FuzzVerifyMembership(f *testing.F) {
seeds := VectorsTestData()
// VerifyMembership requires:
// Spec, ExistenceProof, CommitmentRootref.
for _, seed := range seeds {
proof, ref := LoadFile(f, seed.Dir, seed.Filename)
root, err := proof.Calculate()
if err != nil {
continue
}
if !bytes.Equal(ref.RootHash, root) {
continue
}
if ref.Value == nil {
continue
}
// Now serialize this value as a seed.
// The use of already calculated values is necessary
// for the fuzzers to have a basis of much better coverage
// generating values.
blob, err := json.Marshal(&verifyJSON{Proof: proof, Ref: ref, Spec: seed.Spec})
if err == nil {
f.Add(blob)
}
}
// 2. Now let's run the fuzzer.
f.Fuzz(func(t *testing.T, input []byte) {
var con verifyJSON
if err := json.Unmarshal(input, &con); err != nil {
return
}
spec, ref, proof := con.Spec, con.Ref, con.Proof
if ref == nil {
return
}
_ = VerifyMembership(spec, ref.RootHash, proof, ref.Key, ref.Value)
})
}