-
Notifications
You must be signed in to change notification settings - Fork 4
/
fuzz_test.go
133 lines (119 loc) · 3.07 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
package smt
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"math"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt/kvstore/simplemap"
)
// FuzzSMT uses fuzzing to attempt to break the SMT implementation
// in its current state. This fuzzing test does not confirm the SMT
// functions correctly, it only tries to detect when it fails unexpectedly
func FuzzSMT_DetectUnexpectedFailures(f *testing.F) {
seeds := [][]byte{
[]byte(""),
[]byte("foo"),
{1, 2, 3, 4},
[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"),
nil,
}
for _, s := range seeds {
f.Add(s)
}
f.Fuzz(func(t *testing.T, input []byte) {
smn := simplemap.NewSimpleMap()
trie := NewSparseMerkleTrie(smn, sha256.New())
r := bytes.NewReader(input)
var keys [][]byte
// key returns a random byte to be used as a key, either generating a new
// one or using a previously generated one with a 50/50 chance of either
key := func() []byte {
b := readByte(r)
if b < math.MaxUint8/2 {
k := make([]byte, b/2)
if _, err := r.Read(k); err != nil {
return nil
}
keys = append(keys, k)
return k
}
if len(keys) == 0 {
return nil
}
return keys[int(b)%len(keys)]
}
// `i` is the loop counter but also used as the input value to `Update` operations
for i := 0; r.Len() != 0; i++ {
originalRoot := trie.Root()
b, err := r.ReadByte()
if err != nil {
continue
}
// Randomly select an operation to perform
op := op(int(b) % int(NumOps))
switch op {
case Get:
_, err := trie.Get(key())
if err != nil {
require.ErrorIsf(
t, err, ErrKeyNotFound,
"unknown error occurred while getting",
)
}
newRoot := trie.Root()
require.Equal(t, originalRoot, newRoot, "root changed while getting")
case Update:
value := make([]byte, 32)
binary.BigEndian.PutUint64(value, uint64(i))
err := trie.Update(key(), value)
require.NoErrorf(t, err, "unknown error occurred while updating")
newRoot := trie.Root()
require.NotEqual(t, originalRoot, newRoot, "root unchanged while updating")
case Delete:
err := trie.Delete(key())
if err != nil {
require.ErrorIsf(
t, err, ErrKeyNotFound,
"unknown error occurred while deleting",
)
continue
}
// If the key was present check root has changed
newRoot := trie.Root()
require.NotEqual(t, originalRoot, newRoot, "root unchanged while deleting")
case Prove:
_, err := trie.Prove(key())
if err != nil {
require.ErrorIsf(
t, err, ErrKeyNotFound,
"unknown error occurred while proving",
)
}
newRoot := trie.Root()
require.Equal(t, originalRoot, newRoot, "root changed while proving")
default:
panic("unknown operation")
}
newRoot := trie.Root()
require.Greater(t, len(newRoot), 0, "new root is empty while err is nil")
}
})
}
// Fuzzing helpers
type op int
const (
Get op = iota
Update
Delete
Prove
NumOps
)
func readByte(r *bytes.Reader) byte {
b, err := r.ReadByte()
if err != nil {
return 0
}
return b
}