...

Text file src/cmd/go/testdata/script/test_fuzz_cache.txt

Documentation: cmd/go/testdata/script

     1[!fuzz-instrumented] skip
     2
     3[short] skip
     4env GOCACHE=$WORK/cache
     5
     6# Fuzz cache should not exist after a regular test run.
     7go test .
     8exists $GOCACHE
     9! exists $GOCACHE/fuzz
    10
    11# Fuzzing should write interesting values to the cache.
    12go test -fuzz=FuzzY -fuzztime=100x .
    13go run ./contains_files $GOCACHE/fuzz/example.com/y/FuzzY
    14
    15# 'go clean -cache' should not delete the fuzz cache.
    16go clean -cache
    17exists $GOCACHE/fuzz
    18
    19# 'go clean -fuzzcache' should delete the fuzz cache but not the build cache.
    20go build -x ./empty
    21stderr '(compile|gccgo)( |\.exe).*empty.go'
    22go clean -fuzzcache
    23! exists $GOCACHE/fuzz
    24go 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.
    29go clean -fuzzcache
    30go test -fuzz=FuzzEmpty -fuzztime=10000x .
    31stdout 'new interesting: 1'
    32stdout '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.
    36go test -fuzz=FuzzEmpty -fuzztime=2x .
    37stdout 'new interesting: 0'
    38stdout 'total: 1'
    39
    40! go clean -fuzzcache example.com/y
    41stderr 'go: clean -fuzzcache cannot be used with package arguments'
    42
    43-- go.mod --
    44module example.com/y
    45
    46go 1.16
    47-- y_test.go --
    48package y
    49
    50import (
    51	"io"
    52	"testing"
    53)
    54
    55func FuzzEmpty(f *testing.F) {
    56    f.Fuzz(func (*testing.T, []byte) {})
    57}
    58
    59func 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 --
    64package y
    65
    66import (
    67	"bytes"
    68	"io"
    69)
    70
    71func 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 --
    77package empty
    78-- contains_files/contains_files.go --
    79package main
    80
    81import (
    82	"fmt"
    83	"path/filepath"
    84	"io/ioutil"
    85	"os"
    86)
    87
    88func 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}

View as plain text