aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/test_fuzz_fuzztime.txt
blob: 7d644b4d13269c6c8f855763cd6020ac4d97677f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# TODO(jayconrod): support shared memory on more platforms.
[!darwin] [!linux] [!windows] skip

[short] skip

# There are no seed values, so 'go test' should finish quickly.
go test

# Fuzzing should exit 0 after fuzztime, even if timeout is short.
go test -timeout=10ms -fuzz=FuzzFast -fuzztime=5s

# We should see the same behavior when invoking the test binary directly.
go test -c
exec ./fuzz.test$GOEXE -test.timeout=10ms -test.fuzz=FuzzFast -test.fuzztime=5s -test.parallel=1 -test.fuzzcachedir=$WORK/cache

# Timeout should not cause inputs to be written as crashers.
! exists testdata/fuzz

# When we use fuzztime with an "x" suffix, it runs a specific number of times.
# This fuzz function creates a file with a unique name ($pid.$count) on each run.
# We count the files to find the number of runs.
mkdir count
env GOCACHE=$WORK/tmp
go test -fuzz=FuzzCount -fuzztime=1000x -fuzzminimizetime=1x
go run check_file_count.go 1000

-- go.mod --
module fuzz

go 1.16
-- fuzz_fast_test.go --
package fuzz_test

import "testing"

func FuzzFast(f *testing.F) {
	f.Fuzz(func (*testing.T, []byte) {})
}
-- fuzz_count_test.go --
package fuzz

import (
	"fmt"
	"os"
	"testing"
)

func FuzzCount(f *testing.F) {
	pid := os.Getpid()
	n := 0
	f.Fuzz(func(t *testing.T, _ []byte) {
		name := fmt.Sprintf("count/%v.%d", pid, n)
		if err := os.WriteFile(name, nil, 0666); err != nil {
			t.Fatal(err)
		}
		n++
	})
}
-- check_file_count.go --
// +build ignore

package main

import (
	"fmt"
	"os"
	"strconv"
)

func main() {
	dir, err := os.ReadDir("count")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	got := len(dir)
	want, _ := strconv.Atoi(os.Args[1])
	if got != want {
		fmt.Fprintf(os.Stderr, "got %d files; want %d\n", got, want)
		os.Exit(1)
	}
}