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

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

View as plain text