1
2
3
4
5
6
7
8
9
10
11 package testdeps
12
13 import (
14 "bufio"
15 "context"
16 "internal/fuzz"
17 "internal/testlog"
18 "io"
19 "os"
20 "os/signal"
21 "reflect"
22 "regexp"
23 "runtime/pprof"
24 "strings"
25 "sync"
26 "time"
27 )
28
29
30
31 type TestDeps struct{}
32
33 var matchPat string
34 var matchRe *regexp.Regexp
35
36 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
37 if matchRe == nil || matchPat != pat {
38 matchPat = pat
39 matchRe, err = regexp.Compile(matchPat)
40 if err != nil {
41 return
42 }
43 }
44 return matchRe.MatchString(str), nil
45 }
46
47 func (TestDeps) StartCPUProfile(w io.Writer) error {
48 return pprof.StartCPUProfile(w)
49 }
50
51 func (TestDeps) StopCPUProfile() {
52 pprof.StopCPUProfile()
53 }
54
55 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
56 return pprof.Lookup(name).WriteTo(w, debug)
57 }
58
59
60 var ImportPath string
61
62 func (TestDeps) ImportPath() string {
63 return ImportPath
64 }
65
66
67 type testLog struct {
68 mu sync.Mutex
69 w *bufio.Writer
70 set bool
71 }
72
73 func (l *testLog) Getenv(key string) {
74 l.add("getenv", key)
75 }
76
77 func (l *testLog) Open(name string) {
78 l.add("open", name)
79 }
80
81 func (l *testLog) Stat(name string) {
82 l.add("stat", name)
83 }
84
85 func (l *testLog) Chdir(name string) {
86 l.add("chdir", name)
87 }
88
89
90 func (l *testLog) add(op, name string) {
91 if strings.Contains(name, "\n") || name == "" {
92 return
93 }
94
95 l.mu.Lock()
96 defer l.mu.Unlock()
97 if l.w == nil {
98 return
99 }
100 l.w.WriteString(op)
101 l.w.WriteByte(' ')
102 l.w.WriteString(name)
103 l.w.WriteByte('\n')
104 }
105
106 var log testLog
107
108 func (TestDeps) StartTestLog(w io.Writer) {
109 log.mu.Lock()
110 log.w = bufio.NewWriter(w)
111 if !log.set {
112
113
114
115
116 log.set = true
117 testlog.SetLogger(&log)
118 log.w.WriteString("# test log\n")
119 }
120 log.mu.Unlock()
121 }
122
123 func (TestDeps) StopTestLog() error {
124 log.mu.Lock()
125 defer log.mu.Unlock()
126 err := log.w.Flush()
127 log.w = nil
128 return err
129 }
130
131
132 func (TestDeps) SetPanicOnExit0(v bool) {
133 testlog.SetPanicOnExit0(v)
134 }
135
136 func (TestDeps) CoordinateFuzzing(
137 timeout time.Duration,
138 limit int64,
139 minimizeTimeout time.Duration,
140 minimizeLimit int64,
141 parallel int,
142 seed []fuzz.CorpusEntry,
143 types []reflect.Type,
144 corpusDir,
145 cacheDir string) (err error) {
146
147
148
149 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
150 defer cancel()
151 err = fuzz.CoordinateFuzzing(ctx, fuzz.CoordinateFuzzingOpts{
152 Log: os.Stderr,
153 Timeout: timeout,
154 Limit: limit,
155 MinimizeTimeout: minimizeTimeout,
156 MinimizeLimit: minimizeLimit,
157 Parallel: parallel,
158 Seed: seed,
159 Types: types,
160 CorpusDir: corpusDir,
161 CacheDir: cacheDir,
162 })
163 if err == ctx.Err() {
164 return nil
165 }
166 return err
167 }
168
169 func (TestDeps) RunFuzzWorker(fn func(fuzz.CorpusEntry) error) error {
170
171
172
173
174
175
176 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
177 defer cancel()
178 err := fuzz.RunFuzzWorker(ctx, fn)
179 if err == ctx.Err() {
180 return nil
181 }
182 return err
183 }
184
185 func (TestDeps) ReadCorpus(dir string, types []reflect.Type) ([]fuzz.CorpusEntry, error) {
186 return fuzz.ReadCorpus(dir, types)
187 }
188
189 func (TestDeps) CheckCorpus(vals []any, types []reflect.Type) error {
190 return fuzz.CheckCorpus(vals, types)
191 }
192
193 func (TestDeps) ResetCoverage() {
194 fuzz.ResetCoverage()
195 }
196
197 func (TestDeps) SnapshotCoverage() {
198 fuzz.SnapshotCoverage()
199 }
200
View as plain text