1 [!fuzz-instrumented] skip
2 [short] skip
3 env GOCACHE=$WORK/cache
4
5 # Test that fuzzing a target with a failure in f.Add prints the crash
6 # and doesn't write anything to testdata/fuzz
7 ! go test -fuzz=FuzzWithAdd -run=FuzzWithAdd -fuzztime=1x
8 ! stdout ^ok
9 ! stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithAdd[/\\]'
10 stdout FAIL
11
12 # Test that fuzzing a target with a success in f.Add and a fuzztime of only
13 # 1 does not produce a crash.
14 go test -fuzz=FuzzWithGoodAdd -run=FuzzWithGoodAdd -fuzztime=1x
15 stdout ok
16 ! stdout FAIL
17
18 # Test that fuzzing a target with a failure in testdata/fuzz prints the crash
19 # and doesn't write anything to testdata/fuzz
20 ! go test -fuzz=FuzzWithTestdata -run=FuzzWithTestdata -fuzztime=1x
21 ! stdout ^ok
22 ! stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithTestdata[/\\]'
23 stdout 'failure while testing seed corpus entry: FuzzWithTestdata/1'
24 stdout FAIL
25
26 # Test that fuzzing a target with no seed corpus or cache finds a crash, prints
27 # it, and write it to testdata
28 ! go test -fuzz=FuzzWithNoCache -run=FuzzWithNoCache -fuzztime=1x
29 ! stdout ^ok
30 stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithNoCache[/\\]'
31 stdout FAIL
32
33 # Write a crashing input to the cache
34 mkdir $GOCACHE/fuzz/example.com/x/FuzzWithCache
35 cp cache-file $GOCACHE/fuzz/example.com/x/FuzzWithCache/1
36
37 # Test that fuzzing a target with a failure in the cache prints the crash
38 # and writes this as a "new" crash to testdata/fuzz
39 ! go test -fuzz=FuzzWithCache -run=FuzzWithCache -fuzztime=1x
40 ! stdout ^ok
41 stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithCache[/\\]'
42 stdout FAIL
43
44 # Write a crashing input to the cache
45 mkdir $GOCACHE/fuzz/example.com/x/FuzzWithMinimizableCache
46 cp cache-file-bytes $GOCACHE/fuzz/example.com/x/FuzzWithMinimizableCache/1
47
48 # Test that fuzzing a target with a failure in the cache minimizes it and writes
49 # the new crash to testdata/fuzz
50 ! go test -fuzz=FuzzWithMinimizableCache -run=FuzzWithMinimizableCache -fuzztime=10000x
51 ! stdout ^ok
52 stdout 'gathering baseline coverage'
53 stdout 'got the minimum size!'
54 stdout 'contains a non-zero byte of length 10'
55 stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithMinimizableCache[/\\]'
56 stdout FAIL
57 # Make sure this crash didn't come from fuzzing
58 # (the log line that states fuzzing began shouldn't have printed)
59 ! stdout 'execs'
60
61 # Clear the fuzz cache and make sure it's gone
62 go clean -fuzzcache
63 ! exists $GOCACHE/fuzz
64
65 # The tests below should operate the exact same as the previous tests. If -fuzz
66 # is enabled, then whatever target is going to be fuzzed shouldn't be run by
67 # anything other than the workers.
68
69 # Test that fuzzing a target (with -run=None set) with a failure in f.Add prints
70 # the crash and doesn't write anything to testdata/fuzz -fuzztime=1x
71 ! go test -fuzz=FuzzWithAdd -run=None
72 ! stdout ^ok
73 ! stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithAdd[/\\]'
74 stdout FAIL
75
76 # Test that fuzzing a target (with -run=None set) with a success in f.Add and a
77 # fuzztime of only 1 does not produce a crash.
78 go test -fuzz=FuzzWithGoodAdd -run=None -fuzztime=1x
79 stdout ok
80 ! stdout FAIL
81
82 # Test that fuzzing a target (with -run=None set) with a failure in
83 # testdata/fuzz prints the crash and doesn't write anything to testdata/fuzz
84 ! go test -fuzz=FuzzWithTestdata -run=None -fuzztime=1x
85 ! stdout ^ok
86 ! stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithTestdata[/\\]'
87 stdout FAIL
88
89 # Write a crashing input to the cache
90 mkdir $GOCACHE/fuzz/example.com/x/FuzzRunNoneWithCache
91 cp cache-file $GOCACHE/fuzz/example.com/x/FuzzRunNoneWithCache/1
92
93 # Test that fuzzing a target (with -run=None set) with a failure in the cache
94 # prints the crash and writes this as a "new" crash to testdata/fuzz
95 ! go test -fuzz=FuzzRunNoneWithCache -run=None -fuzztime=1x
96 ! stdout ^ok
97 stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzRunNoneWithCache[/\\]'
98 stdout FAIL
99
100 # Clear the fuzz cache and make sure it's gone
101 go clean -fuzzcache
102 ! exists $GOCACHE/fuzz
103
104 # The tests below should operate the exact same way for the previous tests with
105 # a seed corpus (namely, they should still fail). However, the binary is built
106 # without instrumentation, so this should be a "testing only" run which executes
107 # the seed corpus before attempting to fuzz.
108
109 go test -c
110 ! exec ./x.test$GOEXE -test.fuzz=FuzzWithAdd -test.run=FuzzWithAdd -test.fuzztime=1x -test.fuzzcachedir=$WORK/cache
111 ! stdout ^ok
112 ! stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithAdd[/\\]'
113 stdout FAIL
114 stderr warning
115
116 go test -c
117 ! exec ./x.test$GOEXE -test.fuzz=FuzzWithTestdata -test.run=FuzzWithTestdata -test.fuzztime=1x -test.fuzzcachedir=$WORK/cache
118 ! stdout ^ok
119 ! stdout 'Failing input written to testdata[/\\]fuzz[/\\]FuzzWithTestdata[/\\]'
120 stdout FAIL
121 stderr warning
122
123 -- go.mod --
124 module example.com/x
125
126 go 1.16
127 -- x_test.go --
128 package x
129
130 import "testing"
131
132 func FuzzWithAdd(f *testing.F) {
133 f.Add(10)
134 f.Fuzz(func(t *testing.T, i int) {
135 if i == 10 {
136 t.Error("bad thing here")
137 }
138 })
139 }
140
141 func FuzzWithGoodAdd(f *testing.F) {
142 f.Add(10)
143 f.Fuzz(func(t *testing.T, i int) {
144 if i != 10 {
145 t.Error("bad thing here")
146 }
147 })
148 }
149
150 func FuzzWithTestdata(f *testing.F) {
151 f.Fuzz(func(t *testing.T, i int) {
152 if i == 10 {
153 t.Error("bad thing here")
154 }
155 })
156 }
157
158 func FuzzWithNoCache(f *testing.F) {
159 f.Fuzz(func(t *testing.T, i int) {
160 t.Error("bad thing here")
161 })
162 }
163
164 func FuzzWithCache(f *testing.F) {
165 f.Fuzz(func(t *testing.T, i int) {
166 if i == 10 {
167 t.Error("bad thing here")
168 }
169 })
170 }
171
172 func FuzzWithMinimizableCache(f *testing.F) {
173 f.Fuzz(func(t *testing.T, b []byte) {
174 if len(b) < 10 {
175 return
176 }
177 for _, n := range b {
178 if n != 0 {
179 if len(b) == 10 {
180 t.Log("got the minimum size!")
181 }
182 t.Fatalf("contains a non-zero byte of length %d", len(b))
183 }
184 }
185 })
186 }
187
188 func FuzzRunNoneWithCache(f *testing.F) {
189 f.Fuzz(func(t *testing.T, i int) {
190 if i == 10 {
191 t.Error("bad thing here")
192 }
193 })
194 }
195 -- testdata/fuzz/FuzzWithTestdata/1 --
196 go test fuzz v1
197 int(10)
198 -- cache-file --
199 go test fuzz v1
200 int(10)
201 -- cache-file-bytes --
202 go test fuzz v1
203 []byte("11111111111111111111")
204
View as plain text