-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
50 lines (44 loc) · 1016 Bytes
/
package.go
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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strings"
"golang.org/x/tools/go/packages"
)
func findPackageFunc() (string, error) {
// Parse the Go package
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, `.`, nil, 0)
if err != nil {
return ``, err
}
// Find the Fuzz function in the package
for _, pkg := range pkgs {
for fname, file := range pkg.Files {
if strings.HasSuffix(fname, `_test.go`) {
continue
}
for _, decl := range file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok && funcDecl.Name.Name == *funcName {
return pkg.Name, nil
}
}
}
}
return ``, fmt.Errorf("fuzz function %s not found", *funcName)
}
func loadPackage(pkgName string) (*packages.Package, error) {
if pkgName == `main` {
return nil, nil
}
pkgs, err := packages.Load(nil)
if err != nil {
return nil, err
}
if len(pkgs) == 0 || pkgs[0].Name != pkgName {
return nil, fmt.Errorf(`could not load package %s`, pkgName)
}
return pkgs[0], nil
}