Debian Codegraph @

pattern/parser_test.go

raw
1 package pattern
2
3 import (
4 "fmt"
5 "go/ast"
6 goparser "go/parser"
7 "go/token"
8 "os"
9 "path/filepath"
10 "reflect"
11 "runtime"
12 "strings"
13 "testing"
14
15 "honnef.co/go/tools/debug"
16 )
17
18 func TestParse(t *testing.T) {
19 inputs := []string{
20 `(Binding "name" _)`,
21 `(Binding "name" _:[])`,
22 `(Binding "name" _:_:[])`,
23 }
24
25 p := Parser{}
26 for _, input := range inputs {
27 if _, err := p.Parse(input); err != nil {
28 t.Errorf("failed to parse %q: %s", input, err)
29 }
30 }
31 }
32
33 func FuzzParse(f *testing.F) {
34 var files []*ast.File
35 fset := token.NewFileSet()
36
37 // Ideally we'd check against as much source code as possible, but that's fairly slow, on the order of 500ms per
38 // pattern when checking against the whole standard library.
39 //
40 // We pick the runtime package in the hopes that it contains the most diverse, and weird, code.
41 //
42 //lint:ignore SA1019 runtime.GOROOT is deprecated, we'll have to update this eventually
43 filepath.Walk(runtime.GOROOT()+"/src/runtime", func(path string, info os.FileInfo, err error) error {
44 if err != nil {
45 // XXX error handling
46 panic(err)
47 }
48 if !strings.HasSuffix(path, ".go") {
49 return nil
50 }
51 f, err := goparser.ParseFile(fset, path, nil, goparser.SkipObjectResolution)
52 if err != nil {
53 return nil
54 }
55 files = append(files, f)
56 return nil
57 })
58
59 parse := func(in string, allowTypeInfo bool) (Pattern, bool) {
60 p := Parser{
61 AllowTypeInfo: allowTypeInfo,
62 }
63 pat, err := p.Parse(string(in))
64 if err != nil {
65 if strings.Contains(err.Error(), "internal error") {
66 panic(err)
67 }
68 return Pattern{}, false
69 }
70 return pat, true
71 }
72
73 f.Fuzz(func(t *testing.T, in []byte) {
74 defer func() {
75 if err := recover(); err != nil {
76 str := fmt.Sprint(err)
77 if strings.Contains(str, "binding already created:") {
78 // This is an invalid pattern, not a real failure
79 } else {
80 // Re-panic the original panic
81 panic(err)
82 }
83 }
84 }()
85 // Parse twice, once with AllowTypeInfo set to true to exercise the parser, and once with it set to false so we
86 // can actually use it in Match, as we don't have type information available.
87
88 pat, ok := parse(string(in), true)
89 if !ok {
90 return
91 }
92 // Make sure we can turn it back into a string
93 _ = pat.Root.String()
94
95 pat, ok = parse(string(in), false)
96 if !ok {
97 return
98 }
99 // Make sure we can turn it back into a string
100 _ = pat.Root.String()
101
102 entryNodesMap := make(map[reflect.Type]struct{})
103 for _, node := range pat.EntryNodes {
104 entryNodesMap[reflect.TypeOf(node)] = struct{}{}
105 }
106
107 // Don't check patterns with too many relevant nodes; it's too expensive
108 if len(pat.EntryNodes) < 20 {
109 // Make sure trying to match nodes doesn't panic
110 for _, f := range files {
111 ast.Inspect(f, func(node ast.Node) bool {
112 rt := reflect.TypeOf(node)
113 // We'd prefer calling Match on all nodes, not just those the pattern deems relevant, to find more bugs.
114 // However, doing so has a 10x cost in execution time.
115 if _, ok := entryNodesMap[rt]; ok {
116 Match(pat, node)
117 }
118 return true
119 })
120 }
121 }
122 })
123 }
124
125 func TestMatchAlias(t *testing.T) {
126 p1 := MustParse(`(CallExpr (Symbol "foo.Alias") _)`)
127 p2 := MustParse(`(CallExpr (Symbol "int") _)`)
128
129 f, _, info, err := debug.TypeCheck(`
130 package pkg
131 type Alias = int
132 func _() { _ = Alias(0) }
133 `)
134 if err != nil {
135 t.Fatal(err)
136 }
137
138 m := &Matcher{
139 TypesInfo: info,
140 }
141 node := f.Decls[1].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt).Rhs[0]
142
143 if debug.AliasesEnabled() {
144 // Check that we can match on the name of the alias
145 if ok := m.Match(p1, node); !ok {
146 t.Errorf("%s did not match", p1.Root)
147 }
148 }
149
150 // Check that we can match on the name of the alias's target
151 if ok := m.Match(p2, node); !ok {
152 t.Errorf("%s did not match", p2.Root)
153 }
154 }
155
156 func TestCollectSymbols(t *testing.T) {
157 for _, tt := range []struct {
158 in string
159 out string
160 }{
161 {
162 `(Or (Symbol "foo") (Symbol "bar"))`,
163 `(Or (IndexSymbol "" "" "foo") (IndexSymbol "" "" "bar"))`,
164 },
165 {
166 `(CallExpr (Symbol "foo") [(Symbol "bar") (Symbol "baz")])`,
167 `(And (IndexSymbol "" "" "foo") (IndexSymbol "" "" "bar") (IndexSymbol "" "" "baz"))`,
168 },
169 {
170 `(Symbol (Or "foo" "bar"))`,
171 `(Or (IndexSymbol "" "" "foo") (IndexSymbol "" "" "bar"))`,
172 },
173 {
174 // (Or) never matches anything, so we do need the "foo" symbol for a
175 // successful match
176 `(Or (Symbol "foo") (Or))`,
177 `(IndexSymbol "" "" "foo")`,
178 },
179 {
180 // This tests (And ...)
181 `(BasicLit (Symbol "foo") (Ident "bar"))`,
182 `(IndexSymbol "" "" "foo")`,
183 },
184 {
185 `(Or (Symbol "foo") (Ident _))`,
186 `_`,
187 },
188 {
189 `(Or (Symbol "foo") (EmptyStmt))`,
190 `_`,
191 },
192 {
193 `(Or (Symbol "foo") nil)`,
194 `_`,
195 },
196 {
197 `(Symbol "example.com/foo.Get")`,
198 `(IndexSymbol "example.com/foo" "" "Get")`,
199 },
200 {
201 `(Symbol "(*example.com/foo.Client).Get")`,
202 `(IndexSymbol "example.com/foo" "Client" "Get")`,
203 },
204
205 // Don't crash on malformed symbols
206 {
207 `(Symbol "")`,
208 `(IndexSymbol "" "" "")`,
209 },
210 {
211 `(Symbol "foo.")`,
212 `(IndexSymbol "foo" "" "")`,
213 },
214 {
215 `(Symbol "(foo")`,
216 `(IndexSymbol "" "" "")`,
217 },
218 {
219 `(Symbol "(foo)")`,
220 `(IndexSymbol "" "" "")`,
221 },
222 {
223 `(Symbol "(foo.Bar)")`,
224 `(IndexSymbol "" "" "")`,
225 },
226 {
227 `(Symbol "(foo.Bar).")`,
228 `(IndexSymbol "foo" "Bar" "")`,
229 },
230 {
231 `(Symbol "(foo.Bar.")`,
232 `(IndexSymbol "" "" "")`,
233 },
234 {
235 `(Symbol "(foo).Bar")`,
236 `(IndexSymbol "" "" "")`,
237 },
238 } {
239 p := &Parser{AllowTypeInfo: true}
240 pat, err := p.Parse(tt.in)
241 if err != nil {
242 t.Fatal(err)
243 }
244 s := pat.SymbolsPattern.String()
245 if s != tt.out {
246 t.Fatalf("Symbol requirements for %s: got %s, want %s", tt.in, s, tt.out)
247 }
248 }
249 }
250
251 func BenchmarkParser(b *testing.B) {
252 const input = `
253 (CallExpr
254 (Symbol
255 name@(Or
256 "math/rand.Int31n"
257 "math/rand.Int63n"
258 "math/rand.Intn"
259 "(*math/rand.Rand).Int31n"
260 "(*math/rand.Rand).Int63n"
261 "(*math/rand.Rand).Intn"
262
263 "math/rand/v2.Int32N"
264 "math/rand/v2.Int64N"
265 "math/rand/v2.IntN"
266 "math/rand/v2.N"
267 "math/rand/v2.Uint32N"
268 "math/rand/v2.Uint64N"
269 "math/rand/v2.UintN"
270
271 "(*math/rand/v2.Rand).Int32N"
272 "(*math/rand/v2.Rand).Int64N"
273 "(*math/rand/v2.Rand).IntN"
274 "(*math/rand/v2.Rand).Uint32N"
275 "(*math/rand/v2.Rand).Uint64N"
276 "(*math/rand/v2.Rand).UintN"))
277 [(IntegerLiteral "1")])`
278
279 for range b.N {
280 p := &Parser{AllowTypeInfo: true}
281 _, err := p.Parse(input)
282 if err != nil {
283 b.Fatal(err)
284 }
285 }
286 }