...

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

Documentation: cmd/go/testdata/script

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

View as plain text