| 1 |
[!fuzz-instrumented] skip |
| 2 |
|
| 3 |
[short] skip |
| 4 |
env GOCACHE=$WORK/cache |
| 5 |
|
| 6 |
# Fuzz cache should not exist after a regular test run. |
| 7 |
go test . |
| 8 |
exists $GOCACHE |
| 9 |
! exists $GOCACHE/fuzz |
| 10 |
|
| 11 |
# Fuzzing should write interesting values to the cache. |
| 12 |
go test -fuzz=FuzzY -fuzztime=100x . |
| 13 |
go run ./contains_files $GOCACHE/fuzz/example.com/y/FuzzY |
| 14 |
|
| 15 |
# 'go clean -cache' should not delete the fuzz cache. |
| 16 |
go clean -cache |
| 17 |
exists $GOCACHE/fuzz |
| 18 |
|
| 19 |
# 'go clean -fuzzcache' should delete the fuzz cache but not the build cache. |
| 20 |
go build -x ./empty |
| 21 |
stderr '(compile|gccgo)( |\.exe).*empty.go' |
| 22 |
go clean -fuzzcache |
| 23 |
! exists $GOCACHE/fuzz |
| 24 |
go build -x ./empty |
| 25 |
! stderr '(compile|gccgo)( |\.exe).*empty.go' |
| 26 |
|
| 27 |
# Fuzzing indicates that one new interesting value was found with an empty |
| 28 |
# corpus, and the total size of the cache is now 1. |
| 29 |
go clean -fuzzcache |
| 30 |
go test -fuzz=FuzzEmpty -fuzztime=10000x . |
| 31 |
stdout 'new interesting: 1' |
| 32 |
stdout 'total: 1' |
| 33 |
|
| 34 |
# Fuzzing again with a small fuzztime does not find any other interesting |
| 35 |
# values but still indicates that the cache size is 1. |
| 36 |
go test -fuzz=FuzzEmpty -fuzztime=2x . |
| 37 |
stdout 'new interesting: 0' |
| 38 |
stdout 'total: 1' |
| 39 |
|
| 40 |
! go clean -fuzzcache example.com/y |
| 41 |
stderr 'go: clean -fuzzcache cannot be used with package arguments' |
| 42 |
|
| 43 |
-- go.mod -- |
| 44 |
module example.com/y |
| 45 |
|
| 46 |
go 1.16 |
| 47 |
-- y_test.go -- |
| 48 |
package y |
| 49 |
|
| 50 |
import ( |
| 51 |
"io" |
| 52 |
"testing" |
| 53 |
) |
| 54 |
|
| 55 |
func FuzzEmpty(f *testing.F) { |
| 56 |
f.Fuzz(func (*testing.T, []byte) {}) |
| 57 |
} |
| 58 |
|
| 59 |
func FuzzY(f *testing.F) { |
| 60 |
f.Add([]byte("y")) |
| 61 |
f.Fuzz(func(t *testing.T, b []byte) { Y(io.Discard, b) }) |
| 62 |
} |
| 63 |
-- y.go -- |
| 64 |
package y |
| 65 |
|
| 66 |
import ( |
| 67 |
"bytes" |
| 68 |
"io" |
| 69 |
) |
| 70 |
|
| 71 |
func Y(w io.Writer, b []byte) { |
| 72 |
if !bytes.Equal(b, []byte("y")) { |
| 73 |
w.Write([]byte("not equal")) |
| 74 |
} |
| 75 |
} |
| 76 |
-- empty/empty.go -- |
| 77 |
package empty |
| 78 |
-- contains_files/contains_files.go -- |
| 79 |
package main |
| 80 |
|
| 81 |
import ( |
| 82 |
"fmt" |
| 83 |
"path/filepath" |
| 84 |
"io/ioutil" |
| 85 |
"os" |
| 86 |
) |
| 87 |
|
| 88 |
func main() { |
| 89 |
infos, err := ioutil.ReadDir(filepath.Clean(os.Args[1])) |
| 90 |
if err != nil { |
| 91 |
fmt.Fprintln(os.Stderr, err) |
| 92 |
os.Exit(1) |
| 93 |
} |
| 94 |
if len(infos) == 0 { |
| 95 |
os.Exit(1) |
| 96 |
} |
| 97 |
} |