-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfuzz.go
More file actions
53 lines (47 loc) · 1.15 KB
/
fuzz.go
File metadata and controls
53 lines (47 loc) · 1.15 KB
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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
fuzzTime := os.Getenv("FUZZTIME")
if fuzzTime == "" {
fuzzTime = "5s"
}
// Get all packages
cmd := exec.Command("go", "list", "./...")
out, err := cmd.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list packages: %v\n", err)
os.Exit(1)
}
packages := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, pkg := range packages {
// Check if package has fuzz tests
cmd := exec.Command("go", "test", "-list", "^Fuzz", pkg)
out, err := cmd.Output()
if err != nil {
continue
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Fuzz") {
fmt.Printf("Fuzzing %s in %s\n", line, pkg)
fuzzCmd := exec.Command("go", "test", "-fuzz="+line, "-fuzztime="+fuzzTime, pkg)
fuzzCmd.Stdout = os.Stdout
fuzzCmd.Stderr = os.Stderr
if err := fuzzCmd.Run(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}
fmt.Fprintf(os.Stderr, "Fuzz failed: %v\n", err)
os.Exit(1)
}
}
}
}
}