Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/google/gofuzz
Fuzz testing for go.
https://github.com/google/gofuzz
Last synced: about 1 month ago
JSON representation
Fuzz testing for go.
- Host: GitHub
- URL: https://github.com/google/gofuzz
- Owner: google
- License: apache-2.0
- Archived: true
- Created: 2014-07-31T16:21:29.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-11-07T22:21:34.000Z (about 2 years ago)
- Last Synced: 2024-09-17T01:20:03.241Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 67.4 KB
- Stars: 1,494
- Watchers: 26
- Forks: 119
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - gofuzz - Library for populating go objects with random values. (Fuzzing and delta-debugging/reducing/shrinking / Mock)
- my-awesome - google/gofuzz - 11 star:1.5k fork:0.1k Fuzz testing for go. (Go)
- awesome-repositories - google/gofuzz - Fuzz testing for go. (Go)
- awesome-go - gofuzz - Library for populating go objects with random values. Stars:`1.5K`. (Testing / Fuzzing and delta-debugging/reducing/shrinking)
- awesome-go - gofuzz - Fuzz testing for go. - ★ 454 (Testing)
- awesome-go-extra - gofuzz - 07-31T16:21:29Z|2022-05-03T16:08:20Z| (Testing / Fuzzing and delta-debugging/reducing/shrinking.)
README
gofuzz
======gofuzz is a library for populating go objects with random values.
[![GoDoc](https://godoc.org/github.com/google/gofuzz?status.svg)](https://godoc.org/github.com/google/gofuzz)
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:
```go
f := fuzz.New()
var myInt int
f.Fuzz(&myInt) // myInt gets a random value.
```You can use it on maps:
```go
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:
```go
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:
```go
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
c.Fuzz(&e.AInfo)
case 1:
e.Type = B
c.Fuzz(&e.BInfo)
}
},
)var myObject MyInfo
f.Fuzz(&myObject) // Type will correspond to whether A or B info is set.
```See more examples in ```example_test.go```.
## dvyukov/go-fuzz integration
You can use this library for easier [go-fuzz](https://github.com/dvyukov/go-fuzz)ing.
go-fuzz provides the user a byte-slice, which should be converted to different inputs
for the tested function. This library can help convert the byte slice. Consider for
example a fuzz test for a the function `mypackage.MyFunc` that takes an int arguments:
```go
// +build gofuzz
package mypackageimport fuzz "github.com/google/gofuzz"
func Fuzz(data []byte) int {
var i int
fuzz.NewFromGoFuzz(data).Fuzz(&i)
MyFunc(i)
return 0
}
```Happy testing!