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

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

View as plain text