-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzz.go
218 lines (201 loc) · 5.25 KB
/
fuzz.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package benchmark
import (
"errors"
"fmt"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
var pattern = regexp.MustCompile(`(\w+)\((.+)\)`)
func newInstance(rt reflect.Type) reflect.Value {
el := reflect.New(rt).Elem()
for fieldIdx := 0; fieldIdx < rt.NumField(); fieldIdx++ {
field := rt.Field(fieldIdx)
if !field.IsExported() {
continue
}
fuzzVal := field.Tag.Get("fuzz")
if fuzzVal == "ignore" {
continue
}
var action string
var matches []string
var params []string
if field.Type.Kind() != reflect.Struct {
matches = pattern.FindStringSubmatch(fuzzVal)
if len(matches) < 3 {
fmt.Println("warn: invalid fuzz tag", matches)
continue
}
action = matches[1]
params = strings.Split(matches[2], ",")
} else {
action = "STRUCT"
}
spec, ok := supported.Find(field.Type.Kind(), action)
if !ok {
fmt.Println("warn: no spec found")
continue
}
args, err := spec.ParserE(params)
if err != nil {
panic(fmt.Errorf("parse field:%s err:%v, spec:%v", field.Name, err, spec.Rule))
}
fieldInstance := spec.New(args, field.Type)
el.Field(fieldIdx).Set(fieldInstance)
}
return el
}
type Spec struct {
Kind reflect.Kind
Rule string
Desc string
ParserE func(args []string) ([]any, error)
New func(args []any, elType reflect.Type) reflect.Value
}
func (s Spec) action() string {
if s.Rule == "" {
return "STRUCT"
}
matches := pattern.FindStringSubmatch(s.Rule)
if len(matches) < 3 {
fmt.Println("warn: invalid fuzz tag", matches)
return ""
}
return matches[1]
}
type SpecConfig []Spec
var supported = SpecConfig{}
func init() {
args := SpecConfig{
{
// 字符串规则1:生成字符串,填充指定长度即可
Kind: reflect.String,
Rule: "length(minLen,maxLen)",
Desc: "limit the length of generated string, minLen~maxLen runes",
ParserE: func(args []string) ([]any, error) {
vals := make([]any, len(args))
for i, arg := range args {
v, err := strconv.Atoi(arg)
if err != nil {
return nil, fmt.Errorf("args[%d]==%v err: %v", i, args[i], err)
}
vals[i] = v
}
return vals, nil
},
New: func(args []any, elType reflect.Type) reflect.Value {
if len(args) != 2 {
panic("length(a,b): string length btw a and b, err: miss 'a' or 'b'")
}
minLen, maxLen := args[0].(int), args[1].(int)
if !(minLen <= maxLen) {
panic("length(a,b): 'a <= b' not ok")
}
//fmt.Println("length:", minLen, maxLen)
chooseLen := rand.Int()%(maxLen-minLen) + minLen
ins := reflect.New(reflect.TypeOf("")).Elem()
ins.SetString(strings.Repeat("x", chooseLen))
return ins
},
},
{
// 字符串规则2:生成日期,简单按照格式格式化即可
Kind: reflect.String,
Rule: "date(2006-01-02)",
Desc: "limit the date format of generated string",
ParserE: func(args []string) ([]any, error) {
vals := make([]any, len(args))
for i, arg := range args {
vals[i] = time.Now().Format(arg)
}
return vals, nil
},
New: func(args []any, elType reflect.Type) reflect.Value {
if len(args) != 1 {
panic("date(yyyy-MM-dd): limit date format, err: miss time layout")
}
ins := reflect.New(reflect.TypeOf("")).Elem()
ins.SetString(args[0].(string))
return ins
},
},
{
// 整数规则:限定生成整数的范围,即可
Kind: reflect.Int,
Rule: "range(min,max)",
Desc: "limit the value range, min~max",
ParserE: func(args []string) ([]any, error) {
vals := make([]any, len(args))
for i, arg := range args {
v, err := strconv.Atoi(arg)
if err != nil {
return nil, fmt.Errorf("args[%d] not int", i)
}
vals[i] = v
}
return vals, nil
},
New: func(args []any, elType reflect.Type) reflect.Value {
min, max := args[0].(int), args[1].(int)
if !(min <= max) {
panic("range(a,b): 'a <= b' not ok")
}
//fmt.Println("range:", min, max)
chooseVal := rand.Int()%(max-min) + min
ins := reflect.New(reflect.TypeOf(int(0))).Elem()
ins.SetInt(int64(chooseVal))
return ins
},
},
{
// 数组规则:生成指定的切片,即可
Kind: reflect.Slice,
Rule: "length(n)",
Desc: "limit the slice length, n elements",
ParserE: func(args []string) ([]any, error) {
if len(args) != 1 {
return nil, errors.New("slice rule 'length(n)' has only 1 argument")
}
v, err := strconv.Atoi(args[0])
if err != nil {
return nil, err
}
return []any{v}, nil
},
New: func(args []any, elType reflect.Type) reflect.Value {
length := args[0].(int)
slice := reflect.MakeSlice(elType, 0, length)
for i := 0; i < length; i++ {
v := newInstance(elType.Elem())
slice = reflect.Append(slice, v)
}
return slice
},
},
{
// 结构体规则:生成对应的struct,即可
Kind: reflect.Struct,
Rule: "",
Desc: "recursive fill the struct",
ParserE: func(args []string) ([]any, error) {
return nil, nil
},
New: func(args []any, elType reflect.Type) reflect.Value {
return newInstance(elType)
},
},
}
supported = args
}
func (c SpecConfig) Find(kind reflect.Kind, action string) (Spec, bool) {
for _, v := range c {
if v.Kind == kind && v.action() == action {
return v, true
}
}
return Spec{}, false
}