-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
100 lines (95 loc) · 2.48 KB
/
Copy pathfuzz_test.go
File metadata and controls
100 lines (95 loc) · 2.48 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package goago
import (
"bytes"
"go/ast"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
)
func FuzzLoadConfig(f *testing.F) {
f.Add("")
f.Add("enable: [no-goto]\n")
f.Add("enable: [all]\ndisable: [no-goto]\n")
f.Add("exclude: [generated, '*.pb.go', third_party/*]\n")
f.Add("enable: [no-gotoo]\n")
f.Add("enabel: [no-goto]\n")
f.Add("&a [*a, *a, *a]\n")
f.Fuzz(func(t *testing.T, body string) {
if len(body) > maxConfigBytes+1024 {
body = body[:maxConfigBytes+1024]
}
dir := t.TempDir()
path := filepath.Join(dir, ConfigName)
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
_, _ = LoadConfig(dir, path)
})
}
func FuzzGitHubReport(f *testing.F) {
f.Add("x.go", "no-goto", "goto; use a loop", 1, 1)
f.Add("a.go,line=1::x", "no-goto", "::error::forged\nnext", 2, 3)
f.Add("dir/\nfile.go", "goago stale ignore", "%0A::warning::", 0, 0)
f.Fuzz(func(t *testing.T, file, rule, msg string, line, col int) {
r := &Report{
Findings: []Finding{{
Rule: rule,
Severity: Error,
Message: msg,
File: file,
Line: line,
Column: col,
EndLine: line,
EndColumn: col,
}},
}
var buf bytes.Buffer
if err := r.Write(&buf, FormatGitHub); err != nil {
t.Fatal(err)
}
out := buf.String()
if out == "" {
t.Fatal("empty github report")
}
lines := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
if len(lines) != 1 {
t.Fatalf("finding produced %d lines, want 1: %q", len(lines), out)
}
if !strings.HasPrefix(lines[0], "::") {
t.Fatalf("missing workflow command prefix: %q", lines[0])
}
})
}
func FuzzSkip(f *testing.F) {
f.Add("generated", "internal/generated/x.go")
f.Add("*.pb.go", "api/service.pb.go")
f.Add("third_party/*", "third_party/lib/a.go")
f.Add("*", "anything.go")
f.Fuzz(func(_ *testing.T, pattern, path string) {
cfg := &Config{Exclude: []string{pattern}}
_ = cfg.Skip(path)
})
}
func FuzzParseDirective(f *testing.F) {
f.Add("//goago:ignore no-goto -- reason")
f.Add("//goago:ignore-file * -- generated")
f.Add("//goago:ignorecase not a directive")
f.Add("//goago:ignore")
f.Add("/* goago:ignore no-goto -- no */")
f.Fuzz(func(t *testing.T, text string) {
if len(text) > 65536 {
t.Skip()
}
src := []byte(text)
if len(src) == 0 {
src = []byte(" ")
}
fset := token.NewFileSet()
tf := fset.AddFile("x.go", -1, len(src))
tf.SetLinesForContent(src)
c := &ast.Comment{Slash: tf.Pos(0), Text: text}
_ = parseDirective(fset, c, src)
})
}