-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
72 lines (64 loc) · 1.76 KB
/
Copy pathfuzz_test.go
File metadata and controls
72 lines (64 loc) · 1.76 KB
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
package gobspect_test
import (
"bytes"
"encoding/gob"
"math/big"
"os"
"path/filepath"
"testing"
"time"
"github.com/codepuke/gobspect"
)
func FuzzDecode(f *testing.F) {
// Seed corpus from pre-generated fixture files.
entries, _ := filepath.Glob("testdata/*.gob")
for _, path := range entries {
data, err := os.ReadFile(path)
if err != nil {
f.Logf("skipping fixture %q: %v", path, err)
continue
}
f.Add(data)
}
// Additional inline seeds for value varieties not covered by fixtures.
type seedVal struct {
label string
v any
}
seeds := []seedVal{
{"int", WrapInt{V: 42}},
{"string", WrapString{V: "hello, gob"}},
{"struct", Point{X: 3, Y: 7}},
{"map", StringMap{"foo": 42, "bar": -1}},
{"slice", IntSlice{1, 2, 3, 4, 5}},
{"nested struct", NamedPoint{Name: "origin", Pt: Point{X: 1, Y: 2}}},
{"time.Time", time.Date(2024, 6, 1, 12, 0, 0, 0, time.UTC)},
{"interface", HasInterface{V: Point{X: 5, Y: 6}}},
}
for _, s := range seeds {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(s.v); err != nil {
f.Logf("skipping seed %q: %v", s.label, err)
continue
}
f.Add(buf.Bytes())
}
// big.Int uses GobEncoder; encode via a pointer.
{
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(big.NewInt(123456789)); err == nil {
f.Add(buf.Bytes())
}
}
ins := gobspect.New()
f.Fuzz(func(t *testing.T, data []byte) {
// Exercise Collect (drains stream, returns values).
ins.Stream(bytes.NewReader(data)).Collect() //nolint:errcheck
// Exercise Schema (drains stream, returns types).
ins.Stream(bytes.NewReader(data)).Schema() //nolint:errcheck
// Exercise the Values iterator path directly.
for _, err := range ins.Stream(bytes.NewReader(data)).Values() { //nolint:errcheck
_ = err
}
})
}