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

     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 list -f {{.Stale}} ./empty
    21  stdout true
    22  go install ./empty
    23  go list -f {{.Stale}} ./empty
    24  stdout false
    25  go clean -fuzzcache
    26  ! exists $GOCACHE/fuzz
    27  go list -f {{.Stale}} ./empty
    28  stdout false
    29  
    30  # Fuzzing indicates that one new interesting value was found with an empty
    31  # corpus, and the total size of the cache is now 1.
    32  go clean -fuzzcache
    33  go test -fuzz=FuzzEmpty -fuzztime=10000x .
    34  stdout 'new interesting: 1'
    35  stdout 'total: 1'
    36  
    37  # Fuzzing again with a small fuzztime does not find any other interesting
    38  # values but still indicates that the cache size is 1.
    39  go test -fuzz=FuzzEmpty -fuzztime=2x .
    40  stdout 'new interesting: 0'
    41  stdout 'total: 1'
    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  }
    98  

View as plain text