-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathengine_fuzz_test.go
111 lines (96 loc) · 2.42 KB
/
engine_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
// Copyright 2022 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"context"
"testing"
. "pgregory.net/rapid"
)
func checkInt(t *T) {
answer := Int().Draw(t, "answer")
if answer == 42 {
t.Fatalf("fuzzing works")
}
}
func checkSlice(t *T) {
slice := SliceOfN(Int(), 5, 5).Draw(t, "slice")
if slice[0] < slice[1] && slice[1] < slice[2] && slice[2] < slice[3] && slice[3] < slice[4] {
t.Fatalf("fuzzing works")
}
}
func checkString(t *T) {
hello := String().Draw(t, "hello")
if hello == "world" {
t.Fatalf("fuzzing works")
}
}
func checkStuckStateMachine(t *T) {
die := 0
t.Repeat(map[string]func(*T){
"roll": func(t *T) {
if die == 6 {
t.Skip("game over")
}
die = IntRange(1, 6).Draw(t, "die")
},
})
}
func TestRapidInt(t *testing.T) {
t.Skip()
Check(t, checkInt)
}
func TestRapidSlice(t *testing.T) {
t.Skip()
Check(t, checkSlice)
}
func TestRapidString(t *testing.T) {
t.Skip()
Check(t, checkString)
}
func TestRapidStuckStateMachine(t *testing.T) {
t.Skip()
Check(t, checkStuckStateMachine)
}
func FuzzInt(f *testing.F) { f.Fuzz(MakeFuzz(checkInt)) }
func FuzzSlice(f *testing.F) { f.Fuzz(MakeFuzz(checkSlice)) }
func FuzzString(f *testing.F) { f.Fuzz(MakeFuzz(checkString)) }
func FuzzStuckStateMachine(f *testing.F) { f.Fuzz(MakeFuzz(checkStuckStateMachine)) }
func FuzzContext(f *testing.F) {
type key struct{}
var ctx context.Context
f.Fuzz(MakeFuzz(func(t *T) {
// Assign to outer variable
// so we can check it after the fuzzing.
ctx = context.WithValue(t.Context(), key{}, "value")
if err := ctx.Err(); err != nil {
t.Fatalf("context must be valid: %v", err)
}
}))
// ctx is set only if the fuzzing function was called.
if ctx != nil {
if err := ctx.Err(); err == nil {
f.Fatalf("context must be canceled")
}
if want, got := "value", ctx.Value(key{}); want != got {
f.Fatalf("context must have value %q, got %q", want, got)
}
}
}
func FuzzCleanup(f *testing.F) {
var state []bool
f.Fuzz(MakeFuzz(func(t *T) {
idx := len(state)
state = append(state, false)
t.Cleanup(func() {
state[idx] = true
})
}))
for _, ok := range state {
if !ok {
f.Fatalf("cleanup must be called")
}
}
}