...
1[!fuzz] skip
2[short] skip
3env GOCACHE=$WORK/cache
4
5# There are no seed values, so 'go test' should finish quickly.
6go test
7
8# Fuzzing should exit 0 after fuzztime, even if timeout is short.
9go test -timeout=3s -fuzz=FuzzFast -fuzztime=5s
10
11# We should see the same behavior when invoking the test binary directly.
12go test -c
13exec ./fuzz.test$GOEXE -test.timeout=3s -test.fuzz=FuzzFast -test.fuzztime=5s -test.parallel=1 -test.fuzzcachedir=$WORK/cache
14
15# Timeout should not cause inputs to be written as crashers.
16! exists testdata/fuzz
17
18# When we use fuzztime with an "x" suffix, it runs a specific number of times.
19# This fuzz function creates a file with a unique name ($pid.$count) on each
20# run. We count the files to find the number of runs.
21mkdir count
22go test -fuzz=FuzzTestCount -fuzztime=1000x -fuzzminimizetime=1x
23go run check_file_count.go count 1000
24
25# When we use fuzzminimizetime with an "x" suffix, it runs a specific number of
26# times while minimizing. This fuzz function creates a file with a unique name
27# ($pid.$count) on each run once the first crash has been found. That means that
28# there should be one file for each execution of the fuzz function during
29# minimization, so we count these to determine how many times minimization was
30# run.
31mkdir minimizecount
32! go test -fuzz=FuzzMinimizeCount -fuzzminimizetime=3x -parallel=1
33go run check_file_count.go minimizecount 3
34
35-- go.mod --
36module fuzz
37
38go 1.16
39-- fuzz_fast_test.go --
40package fuzz_test
41
42import "testing"
43
44func FuzzFast(f *testing.F) {
45 f.Fuzz(func (*testing.T, []byte) {})
46}
47-- fuzz_count_test.go --
48package fuzz
49
50import (
51 "fmt"
52 "os"
53 "testing"
54)
55
56func FuzzTestCount(f *testing.F) {
57 pid := os.Getpid()
58 n := 0
59 f.Fuzz(func(t *testing.T, _ []byte) {
60 name := fmt.Sprintf("count/%v.%d", pid, n)
61 if err := os.WriteFile(name, nil, 0666); err != nil {
62 t.Fatal(err)
63 }
64 n++
65 })
66}
67-- fuzz_minimize_count_test.go --
68package fuzz
69
70import (
71 "bytes"
72 "fmt"
73 "os"
74 "testing"
75)
76
77func FuzzMinimizeCount(f *testing.F) {
78 pid := os.Getpid()
79 n := 0
80 seed := bytes.Repeat([]byte("a"), 357)
81 f.Add(seed)
82 crashFound := false
83 f.Fuzz(func(t *testing.T, b []byte) {
84 if crashFound {
85 name := fmt.Sprintf("minimizecount/%v.%d", pid, n)
86 if err := os.WriteFile(name, nil, 0666); err != nil {
87 t.Fatal(err)
88 }
89 n++
90 }
91 if !bytes.Equal(b, seed) { // this should happen right away
92 crashFound = true
93 t.Error("minimize this!")
94 }
95 })
96}
97-- check_file_count.go --
98// +build ignore
99
100package main
101
102import (
103 "fmt"
104 "os"
105 "strconv"
106)
107
108func main() {
109 dir, err := os.ReadDir(os.Args[1])
110 if err != nil {
111 fmt.Fprintln(os.Stderr, err)
112 os.Exit(1)
113 }
114 got := len(dir)
115 want, _ := strconv.Atoi(os.Args[2])
116 if got != want {
117 fmt.Fprintf(os.Stderr, "got %d files; want %d\n", got, want)
118 os.Exit(1)
119 }
120}
View as plain text