Version: v0.0.0-...-6079644 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2015 License: Apache-2.0, Apache-2.0 Imports: 4 Imported by: 0



gofuzz is a library for populating go objects with random values.

GoDoc Travis

This is useful for testing:

  • Do your project's objects really serialize/unserialize correctly in all cases?
  • Is there an incorrectly formatted object that will cause your project to panic?

Import with import "github.com/google/gofuzz"

You can use it on single variables:

f := fuzz.New()
var myInt int
f.Fuzz(&myInt) // myInt gets a random value.

You can use it on maps:

f := fuzz.New().NilChance(0).NumElements(1, 1)
var myMap map[ComplexKeyType]string
f.Fuzz(&myMap) // myMap will have exactly one element.

Customize the chance of getting a nil pointer:

f := fuzz.New().NilChance(.5)
var fancyStruct struct {
  A, B, C, D *string
f.Fuzz(&fancyStruct) // About half the pointers should be set.

You can even customize the randomization completely if needed:

type MyEnum string
const (
        A MyEnum = "A"
        B MyEnum = "B"
type MyInfo struct {
        Type MyEnum
        AInfo *string
        BInfo *string

f := fuzz.New().NilChance(0).Funcs(
        func(e *MyInfo, c fuzz.Continue) {
                switch c.Intn(2) {
                case 0:
                        e.Type = A
                case 1:
                        e.Type = B

var myObject MyInfo
f.Fuzz(&myObject) // Type will correspond to whether A or B info is set.

See more examples in example_test.go.

Happy testing!



Package fuzz is a library for populating go objects with random values.



This section is empty.


This section is empty.


This section is empty.


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.
	// 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) RandBool

func (c Continue) RandBool() bool

RandBool returns true or false randomly.

func (Continue) RandString

func (c Continue) RandString() string

RandString makes a random string up to 20 characters long. The returned string may include a variety of (valid) UTF-8 encodings.

func (Continue) RandUint64

func (c Continue) RandUint64() uint64

RandUint64 makes random 64 bit numbers. Weirdly, rand doesn't have a function that gives you 64 random bits.

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

func (f *Fuzzer) Funcs(fuzzFuncs ...interface{}) *Fuzzer

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. 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) NilChance

func (f *Fuzzer) NilChance(p float64) *Fuzzer

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

func (f *Fuzzer) NumElements(atLeast, atMost int) *Fuzzer

NumElements sets the minimum and maximum number of elements that will be added to a non-nil map or slice.

func (*Fuzzer) RandSource

func (f *Fuzzer) RandSource(s rand.Source) *Fuzzer

RandSource causes f to get values from the given source of randomness. Use if you want deterministic fuzzing.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL