Repository navigation
Expand file tree
/
Copy pathfuzz_test.go
More file actions
142 lines (136 loc) · 4.61 KB
/
Copy pathfuzz_test.go
File metadata and controls
142 lines (136 loc) · 4.61 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
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
package main
import (
"encoding/json"
"os"
"testing"
)
// Fuzz tests for everything that reads input pingping does not control: a user's
// options and a targets file. Each one checks
// two things for ANY input: it never panics, and when it accepts the input, what it
// returns satisfies the rules the rest of fogping relies on. "Accepted" is where
// bugs hide: a parser that accepts something it should not hands it to code that
// trusts it.
//
// `go test` runs the seeds only (the conformance vectors and a few hand-made
// cases). To search for new inputs: go test -run '^$' -fuzz FuzzParseManifest -fuzztime 30s
// seedVectors adds the "want accepted" and "want refused" raw inputs of a
// conformance file to the corpus, so the fuzzer starts from realistic documents.
func seedVectors(f *testing.F, file, field string) {
b, err := os.ReadFile("docs/conformance/" + file)
if err != nil {
f.Fatal(err)
}
var doc struct {
Vectors []map[string]json.RawMessage `json:"vectors"`
}
if err := json.Unmarshal(b, &doc); err != nil {
f.Fatal(err)
}
for _, v := range doc.Vectors {
if raw, ok := v[field]; ok {
f.Add([]byte(raw))
}
}
}
// fuzzSpecs has one option of every kind, so one corpus exercises all of them.
var fuzzSpecs = []optionSpec{
{Key: "name", Label: "Name", Kind: optString, Required: true, MaxLen: 16},
{Key: "mode", Label: "Mode", Kind: optChoice, Choices: []string{"a", "b"}, Default: "a"},
{Key: "count", Label: "Count", Kind: optInt, Min: 1, Max: 9},
{Key: "flag", Label: "Flag", Kind: optBool},
}
func FuzzNormalizeOptions(f *testing.F) {
seedVectors(f, "options.json", "input")
f.Add([]byte(`{"name":"x","count":1e300}`))
f.Add([]byte(`{"name":"x","count":-0.0}`))
f.Add([]byte(`{"name":["x"],"mode":{"a":1}}`))
f.Add([]byte(`{"name":"\u202e\u0000"}`))
f.Fuzz(func(t *testing.T, raw []byte) {
var in map[string]any
if json.Unmarshal(raw, &in) != nil {
return // not an object: the API layer refuses it before this
}
tg := TargetCfg{Options: in}
if err := normalizeOptions("fuzz", fuzzSpecs, &tg); err != nil {
if errCode(err) == "" {
t.Fatalf("an error with no code: %v", err)
}
return
}
allowed := map[string]bool{"name": true, "mode": true, "count": true, "flag": true}
for k, v := range tg.Options {
if !allowed[k] {
t.Fatalf("an undeclared key %q survived", k)
}
switch k {
case "name", "mode":
if _, ok := v.(string); !ok {
t.Fatalf("%s is %T", k, v)
}
case "count":
if n, ok := v.(int); !ok || n < 1 || n > 9 {
t.Fatalf("count is %#v", v)
}
case "flag":
if _, ok := v.(bool); !ok {
t.Fatalf("flag is %T", v)
}
}
}
if _, ok := tg.Options["name"]; !ok {
t.Fatal("a required option is missing from an accepted target")
}
// Normalising is idempotent: what comes out is what a stored target looks like,
// and it must be accepted again as it is.
again := TargetCfg{Options: tg.Options}
if err := normalizeOptions("fuzz", fuzzSpecs, &again); err != nil {
t.Fatalf("the output is refused when fed back: %v", err)
}
if encodeOptions(again.Options) != encodeOptions(tg.Options) {
t.Fatalf("not idempotent: %s then %s", encodeOptions(tg.Options), encodeOptions(again.Options))
}
if len(encodeOptions(tg.Options)) > maxOptionBytes {
t.Fatal("accepted options over the size limit")
}
})
}
// A targets file row: whatever the bytes, an accepted row must be a target the editor
// would accept as it stands.
func FuzzFromFileRow(f *testing.F) {
f.Add([]byte(`{"type":"ping","host":"10.0.0.1"}`))
f.Add([]byte(`{"type":"tcp","host":"example.org","port":443,"pace":"fast","interval":10}`))
f.Add([]byte(`{"type":"http","host":"example.org","port":443,"options":{"path":"/x?a=b","expect_status":200}}`))
f.Add([]byte(`{"type":"http","host":"a/b","port":1}`))
f.Add([]byte(`{"type":"tcp","host":"999.999.999.999","port":99999}`))
f.Add([]byte(`[]`))
f.Add([]byte(`{"type":"ping","host":"h","extra":{"a":[1,2,3]}}`))
f.Fuzz(func(t *testing.T, raw []byte) {
tg, _, err := fromFileRow(raw)
if err != nil {
return
}
again := tg
again.Options = nil
if tg.Options != nil {
again.Options = map[string]any{}
for k, v := range tg.Options {
again.Options[k] = v
}
}
if err := normalizeTarget(&again); err != nil {
t.Fatalf("an accepted row is refused by the editor's own rules: %v (%+v)", err, tg)
}
switch tg.Type {
case "icmp":
case "tcp", typeHTTP:
if tg.Port < 1 || tg.Port > 65535 {
t.Fatalf("accepted port %d", tg.Port)
}
default:
t.Fatalf("accepted a row of unknown type %q", tg.Type)
}
if numericButNotIP(tg.Host) {
t.Fatalf("accepted host %q", tg.Host)
}
})
}