Documentation ¶
Overview ¶
Package fuzz is a library for populating go objects with random values.
Index ¶
- type Continue
- type Fuzzer
- func (f *Fuzzer) Funcs(fuzzFuncs ...interface{}) *Fuzzer
- func (f *Fuzzer) Fuzz(obj interface{})
- func (f *Fuzzer) FuzzNoCustom(obj interface{})
- func (f *Fuzzer) MaxDepth(d int) *Fuzzer
- func (f *Fuzzer) NilChance(p float64) *Fuzzer
- func (f *Fuzzer) NumElements(atLeast, atMost int) *Fuzzer
- func (f *Fuzzer) RandSource(s rand.Source) *Fuzzer
- func (f *Fuzzer) SkipFieldsWithPattern(pattern *regexp.Regexp) *Fuzzer
- type Interface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Continue ¶
type Continue struct { // For convenience, Continue implements rand.Rand via embedding. // Use this for generating any randomness if you want your fuzzing // to be repeatable for a given seed. *rand.Rand // contains filtered or unexported fields }
Continue can be passed to custom fuzzing functions to allow them to use the correct source of randomness and to continue fuzzing their members.
func (Continue) Fuzz ¶
func (c Continue) Fuzz(obj interface{})
Fuzz continues fuzzing obj. obj must be a pointer.
func (Continue) FuzzNoCustom ¶
func (c Continue) FuzzNoCustom(obj interface{})
FuzzNoCustom continues fuzzing obj, except that any custom fuzz function for obj's type will not be called and obj will not be tested for fuzz.Interface conformance. This applies only to obj and not other instances of obj's type.
func (Continue) RandBool ¶
RandBool returns true or false randomly.
func (Continue) RandString ¶
RandString makes a random string up to 20 characters long. The returned string may include a variety of (valid) UTF-8 encodings.
type Fuzzer ¶
type Fuzzer struct {
// contains filtered or unexported fields
}
Fuzzer knows how to fill any object with random fields.
func New ¶
func New() *Fuzzer
New returns a new Fuzzer. Customize your Fuzzer further by calling Funcs, RandSource, NilChance, or NumElements in any order.
func (*Fuzzer) Funcs ¶
Funcs adds each entry in fuzzFuncs as a custom fuzzing function.
Each entry in fuzzFuncs must be a function taking two parameters. The first parameter must be a pointer or map. It is the variable that function will fill with random data. The second parameter must be a fuzz.Continue, which will provide a source of randomness and a way to automatically continue fuzzing smaller pieces of the first parameter.
These functions are called sensibly, e.g., if you wanted custom string fuzzing, the function `func(s *string, c fuzz.Continue)` would get called and passed the address of strings. Maps and pointers will always be made/new'd for you, ignoring the NilChange option. For slices, it doesn't make much sense to pre-create them--Fuzzer doesn't know how long you want your slice--so take a pointer to a slice, and make it yourself. (If you don't want your map/pointer type pre-made, take a pointer to it, and make it yourself.) See the examples for a range of custom functions.
func (*Fuzzer) Fuzz ¶
func (f *Fuzzer) Fuzz(obj interface{})
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, if so, calls Fuzz on it to fuzz itself. If that fails, this will see if there is a default fuzz function provided by this package. If all of that fails, this will generate random values for all primitive fields and then recurse for all non-primitives.
This is safe for cyclic or tree-like structs, up to a limit. Use the MaxDepth method to adjust how deep you need it to recurse.
obj must be a pointer. Only exported (public) fields can be set (thanks, golang :/ ) Intended for tests, so will panic on bad input or unimplemented fields.
func (*Fuzzer) FuzzNoCustom ¶
func (f *Fuzzer) FuzzNoCustom(obj interface{})
FuzzNoCustom is just like Fuzz, except that any custom fuzz function for obj's type will not be called and obj will not be tested for fuzz.Interface conformance. This applies only to obj and not other instances of obj's type. Not safe for cyclic or tree-like structs! obj must be a pointer. Only exported (public) fields can be set (thanks, golang :/ ) Intended for tests, so will panic on bad input or unimplemented fields.
func (*Fuzzer) MaxDepth ¶
MaxDepth sets the maximum number of recursive fuzz calls that will be made before stopping. This includes struct members, pointers, and map and slice elements.
func (*Fuzzer) NilChance ¶
NilChance sets the probability of creating a nil pointer, map, or slice to 'p'. 'p' should be between 0 (no nils) and 1 (all nils), inclusive.
func (*Fuzzer) NumElements ¶
NumElements sets the minimum and maximum number of elements that will be added to a non-nil map or slice.
func (*Fuzzer) RandSource ¶
RandSource causes f to get values from the given source of randomness. Use if you want deterministic fuzzing.