Skip to content

Commit

Permalink
Merge pull request kubernetes#837 from lavalamp/gofuzz
Browse files Browse the repository at this point in the history
Gofuzz
  • Loading branch information
brendandburns committed Aug 8, 2014
2 parents 71c6e08 + 079c904 commit c718661
Show file tree
Hide file tree
Showing 13 changed files with 1,006 additions and 310 deletions.
41 changes: 21 additions & 20 deletions pkg/api/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,55 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/fsouza/go-dockerclient"
"github.com/google/gofuzz"
)

var fuzzIters = flag.Int("fuzz_iters", 3, "How many fuzzing iterations to do.")
var fuzzIters = flag.Int("fuzz_iters", 50, "How many fuzzing iterations to do.")

// apiObjectFuzzer can randomly populate api objects.
var apiObjectFuzzer = util.NewFuzzer(
func(j *JSONBase) {
var apiObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 1).Funcs(
func(j *JSONBase, c fuzz.Continue) {
// We have to customize the randomization of JSONBases because their
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.Kind = ""
j.ID = util.RandString()
j.ID = c.RandString()
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
// only when all 8 bytes are set.
j.ResourceVersion = util.RandUint64() >> 8
j.SelfLink = util.RandString()
j.CreationTimestamp = util.RandString()
j.ResourceVersion = c.RandUint64() >> 8
j.SelfLink = c.RandString()
j.CreationTimestamp = c.RandString()
},
func(intstr *util.IntOrString) {
func(intstr *util.IntOrString, c fuzz.Continue) {
// util.IntOrString will panic if its kind is set wrong.
if util.RandBool() {
if c.RandBool() {
intstr.Kind = util.IntstrInt
intstr.IntVal = int(util.RandUint64())
intstr.IntVal = int(c.RandUint64())
intstr.StrVal = ""
} else {
intstr.Kind = util.IntstrString
intstr.IntVal = 0
intstr.StrVal = util.RandString()
intstr.StrVal = c.RandString()
}
},
func(u64 *uint64) {
func(u64 *uint64, c fuzz.Continue) {
// TODO: uint64's are NOT handled right.
*u64 = util.RandUint64() >> 8
*u64 = c.RandUint64() >> 8
},
func(pb map[docker.Port][]docker.PortBinding) {
func(pb map[docker.Port][]docker.PortBinding, c fuzz.Continue) {
// This is necessary because keys with nil values get omitted.
// TODO: Is this a bug?
pb[docker.Port(util.RandString())] = []docker.PortBinding{
{util.RandString(), util.RandString()},
{util.RandString(), util.RandString()},
pb[docker.Port(c.RandString())] = []docker.PortBinding{
{c.RandString(), c.RandString()},
{c.RandString(), c.RandString()},
}
},
func(pm map[string]docker.PortMapping) {
func(pm map[string]docker.PortMapping, c fuzz.Continue) {
// This is necessary because keys with nil values get omitted.
// TODO: Is this a bug?
pm[util.RandString()] = docker.PortMapping{
util.RandString(): util.RandString(),
pm[c.RandString()] = docker.PortMapping{
c.RandString(): c.RandString(),
}
},
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/conversion/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"reflect"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/google/gofuzz"
)

func TestConverter_CallsRegisteredFunctions(t *testing.T) {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestConverter_fuzz(t *testing.T) {
{newAnonType(), &TestType1{}, newAnonType()},
}

f := util.NewFuzzer()
f := fuzz.New().NilChance(.5).NumElements(0, 100)
c := NewConverter()

for i, item := range table {
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestConverter_flags(t *testing.T) {
shouldSucceed: true,
},
}
f := util.NewFuzzer()
f := fuzz.New().NilChance(.5).NumElements(0, 100)
c := NewConverter()

for i, item := range table {
Expand Down
17 changes: 9 additions & 8 deletions pkg/conversion/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/google/gofuzz"
)

var fuzzIters = flag.Int("fuzz_iters", 10, "How many fuzzing iterations to do.")
var fuzzIters = flag.Int("fuzz_iters", 50, "How many fuzzing iterations to do.")

// Test a weird version/kind embedding format.
type MyWeirdCustomEmbeddedVersionKindField struct {
Expand Down Expand Up @@ -98,25 +99,25 @@ type ExternalInternalSame struct {
}

// TestObjectFuzzer can randomly populate all the above objects.
var TestObjectFuzzer = util.NewFuzzer(
func(j *MyWeirdCustomEmbeddedVersionKindField) {
var TestObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 100).Funcs(
func(j *MyWeirdCustomEmbeddedVersionKindField, c fuzz.Continue) {
// We have to customize the randomization of MyWeirdCustomEmbeddedVersionKindFields because their
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.ObjectKind = ""
j.ID = util.RandString()
j.ID = c.RandString()
},
func(u *uint64) {
func(u *uint64, c fuzz.Continue) {
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
// only when all 8 bytes are set.
*u = util.RandUint64() >> 8
*u = c.RandUint64() >> 8
},
func(u *uint) {
func(u *uint, c fuzz.Continue) {
// TODO: Fix JSON/YAML packages and/or write custom encoding
// for uint64's. Somehow the LS *byte* of this is lost, but
// only when all 8 bytes are set.
*u = uint(util.RandUint64() >> 8)
*u = uint(c.RandUint64() >> 8)
},
)

Expand Down
Loading

0 comments on commit c718661

Please sign in to comment.