aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/google/gofuzz/fuzz.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/google/gofuzz/fuzz.go')
-rw-r--r--vendor/github.com/google/gofuzz/fuzz.go35
1 files changed, 27 insertions, 8 deletions
diff --git a/vendor/github.com/google/gofuzz/fuzz.go b/vendor/github.com/google/gofuzz/fuzz.go
index 1dfa80a6f..da0a5f938 100644
--- a/vendor/github.com/google/gofuzz/fuzz.go
+++ b/vendor/github.com/google/gofuzz/fuzz.go
@@ -20,6 +20,7 @@ import (
"fmt"
"math/rand"
"reflect"
+ "regexp"
"time"
)
@@ -28,13 +29,14 @@ type fuzzFuncMap map[reflect.Type]reflect.Value
// Fuzzer knows how to fill any object with random fields.
type Fuzzer struct {
- fuzzFuncs fuzzFuncMap
- defaultFuzzFuncs fuzzFuncMap
- r *rand.Rand
- nilChance float64
- minElements int
- maxElements int
- maxDepth int
+ fuzzFuncs fuzzFuncMap
+ defaultFuzzFuncs fuzzFuncMap
+ r *rand.Rand
+ nilChance float64
+ minElements int
+ maxElements int
+ maxDepth int
+ skipFieldPatterns []*regexp.Regexp
}
// New returns a new Fuzzer. Customize your Fuzzer further by calling Funcs,
@@ -150,6 +152,13 @@ func (f *Fuzzer) MaxDepth(d int) *Fuzzer {
return f
}
+// Skip fields which match the supplied pattern. Call this multiple times if needed
+// This is useful to skip XXX_ fields generated by protobuf
+func (f *Fuzzer) SkipFieldsWithPattern(pattern *regexp.Regexp) *Fuzzer {
+ f.skipFieldPatterns = append(f.skipFieldPatterns, pattern)
+ return f
+}
+
// Fuzz recursively fills all of obj's fields with something random. First
// this tries to find a custom fuzz function (see Funcs). If there is no
// custom function this tests whether the object implements fuzz.Interface and,
@@ -274,7 +283,17 @@ func (fc *fuzzerContext) doFuzz(v reflect.Value, flags uint64) {
v.Set(reflect.Zero(v.Type()))
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
- fc.doFuzz(v.Field(i), 0)
+ skipField := false
+ fieldName := v.Type().Field(i).Name
+ for _, pattern := range fc.fuzzer.skipFieldPatterns {
+ if pattern.MatchString(fieldName) {
+ skipField = true
+ break
+ }
+ }
+ if !skipField {
+ fc.doFuzz(v.Field(i), 0)
+ }
}
case reflect.Chan:
fallthrough