Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
cee14ab5
Commit
cee14ab5
authored
Mar 03, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update gofuzz dep
parent
2700871b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
231 additions
and
24 deletions
+231
-24
Godeps.json
Godeps/Godeps.json
+1
-1
.travis.yml
Godeps/_workspace/src/github.com/google/gofuzz/.travis.yml
+3
-2
fuzz.go
Godeps/_workspace/src/github.com/google/gofuzz/fuzz.go
+101
-21
fuzz_test.go
Godeps/_workspace/src/github.com/google/gofuzz/fuzz_test.go
+126
-0
No files found.
Godeps/Godeps.json
View file @
cee14ab5
...
...
@@ -161,7 +161,7 @@
},
{
"ImportPath"
:
"github.com/google/gofuzz"
,
"Rev"
:
"
aef70dacbc78771e35beb261bb3a72986adf7906
"
"Rev"
:
"
bbcb9da2d746f8bdbd6a936686a0a6067ada0ec5
"
},
{
"ImportPath"
:
"github.com/imdario/mergo"
,
...
...
Godeps/_workspace/src/github.com/google/gofuzz/.travis.yml
View file @
cee14ab5
language
:
go
go
:
-
1.4
-
1.3
-
1.2
-
tip
install
:
-
go get code.google.com/p/go.tools/cmd/cover
install
:
-
if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
script
:
-
go test -cover
Godeps/_workspace/src/github.com/google/gofuzz/fuzz.go
View file @
cee14ab5
...
...
@@ -28,17 +28,22 @@ type fuzzFuncMap map[reflect.Type]reflect.Value
// Fuzzer knows how to fill any object with random fields.
type
Fuzzer
struct
{
fuzzFuncs
fuzzFuncMap
r
*
rand
.
Rand
nilChance
float64
minElements
int
maxElements
int
fuzzFuncs
fuzzFuncMap
defaultFuzzFuncs
fuzzFuncMap
r
*
rand
.
Rand
nilChance
float64
minElements
int
maxElements
int
}
// New returns a new Fuzzer. Customize your Fuzzer further by calling Funcs,
// RandSource, NilChance, or NumElements in any order.
func
New
()
*
Fuzzer
{
f
:=
&
Fuzzer
{
defaultFuzzFuncs
:
fuzzFuncMap
{
reflect
.
TypeOf
(
&
time
.
Time
{})
:
reflect
.
ValueOf
(
fuzzTime
),
},
fuzzFuncs
:
fuzzFuncMap
{},
r
:
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
())),
nilChance
:
.2
,
...
...
@@ -131,8 +136,16 @@ func (f *Fuzzer) genShouldFill() bool {
return
f
.
r
.
Float64
()
>
f
.
nilChance
}
// Fuzz recursively fills all of obj's fields with something random.
// 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.
//
// 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
(
f
*
Fuzzer
)
Fuzz
(
obj
interface
{})
{
...
...
@@ -141,20 +154,45 @@ func (f *Fuzzer) Fuzz(obj interface{}) {
panic
(
"needed ptr!"
)
}
v
=
v
.
Elem
()
f
.
doFuzz
(
v
)
f
.
doFuzz
(
v
,
0
)
}
func
(
f
*
Fuzzer
)
doFuzz
(
v
reflect
.
Value
)
{
if
!
v
.
CanSet
()
{
return
// 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
(
f
*
Fuzzer
)
FuzzNoCustom
(
obj
interface
{})
{
v
:=
reflect
.
ValueOf
(
obj
)
if
v
.
Kind
()
!=
reflect
.
Ptr
{
panic
(
"needed ptr!"
)
}
// Check for both pointer and non-pointer custom functions.
if
v
.
CanAddr
()
&&
f
.
tryCustom
(
v
.
Addr
())
{
v
=
v
.
Elem
()
f
.
doFuzz
(
v
,
flagNoCustomFuzz
)
}
const
(
// Do not try to find a custom fuzz function. Does not apply recursively.
flagNoCustomFuzz
uint64
=
1
<<
iota
)
func
(
f
*
Fuzzer
)
doFuzz
(
v
reflect
.
Value
,
flags
uint64
)
{
if
!
v
.
CanSet
()
{
return
}
if
f
.
tryCustom
(
v
)
{
return
if
flags
&
flagNoCustomFuzz
==
0
{
// Check for both pointer and non-pointer custom functions.
if
v
.
CanAddr
()
&&
f
.
tryCustom
(
v
.
Addr
())
{
return
}
if
f
.
tryCustom
(
v
)
{
return
}
}
if
fn
,
ok
:=
fillFuncMap
[
v
.
Kind
()];
ok
{
fn
(
v
,
f
.
r
)
return
...
...
@@ -166,9 +204,9 @@ func (f *Fuzzer) doFuzz(v reflect.Value) {
n
:=
f
.
genElementCount
()
for
i
:=
0
;
i
<
n
;
i
++
{
key
:=
reflect
.
New
(
v
.
Type
()
.
Key
())
.
Elem
()
f
.
doFuzz
(
key
)
f
.
doFuzz
(
key
,
0
)
val
:=
reflect
.
New
(
v
.
Type
()
.
Elem
())
.
Elem
()
f
.
doFuzz
(
val
)
f
.
doFuzz
(
val
,
0
)
v
.
SetMapIndex
(
key
,
val
)
}
return
...
...
@@ -177,7 +215,7 @@ func (f *Fuzzer) doFuzz(v reflect.Value) {
case
reflect
.
Ptr
:
if
f
.
genShouldFill
()
{
v
.
Set
(
reflect
.
New
(
v
.
Type
()
.
Elem
()))
f
.
doFuzz
(
v
.
Elem
())
f
.
doFuzz
(
v
.
Elem
()
,
0
)
return
}
v
.
Set
(
reflect
.
Zero
(
v
.
Type
()))
...
...
@@ -186,14 +224,14 @@ func (f *Fuzzer) doFuzz(v reflect.Value) {
n
:=
f
.
genElementCount
()
v
.
Set
(
reflect
.
MakeSlice
(
v
.
Type
(),
n
,
n
))
for
i
:=
0
;
i
<
n
;
i
++
{
f
.
doFuzz
(
v
.
Index
(
i
))
f
.
doFuzz
(
v
.
Index
(
i
)
,
0
)
}
return
}
v
.
Set
(
reflect
.
Zero
(
v
.
Type
()))
case
reflect
.
Struct
:
for
i
:=
0
;
i
<
v
.
NumField
();
i
++
{
f
.
doFuzz
(
v
.
Field
(
i
))
f
.
doFuzz
(
v
.
Field
(
i
)
,
0
)
}
case
reflect
.
Array
:
fallthrough
...
...
@@ -211,9 +249,22 @@ func (f *Fuzzer) doFuzz(v reflect.Value) {
// tryCustom searches for custom handlers, and returns true iff it finds a match
// and successfully randomizes v.
func
(
f
*
Fuzzer
)
tryCustom
(
v
reflect
.
Value
)
bool
{
// First: see if we have a fuzz function for it.
doCustom
,
ok
:=
f
.
fuzzFuncs
[
v
.
Type
()]
if
!
ok
{
return
false
// Second: see if it can fuzz itself.
if
v
.
CanInterface
()
{
intf
:=
v
.
Interface
()
if
fuzzable
,
ok
:=
intf
.
(
Interface
);
ok
{
fuzzable
.
Fuzz
(
Continue
{
f
:
f
,
Rand
:
f
.
r
})
return
true
}
}
// Finally: see if there is a default fuzz function.
doCustom
,
ok
=
f
.
defaultFuzzFuncs
[
v
.
Type
()]
if
!
ok
{
return
false
}
}
switch
v
.
Kind
()
{
...
...
@@ -242,6 +293,13 @@ func (f *Fuzzer) tryCustom(v reflect.Value) bool {
return
true
}
// Interface represents an object that knows how to fuzz itself. Any time we
// find a type that implements this interface we will delegate the act of
// fuzzing itself.
type
Interface
interface
{
Fuzz
(
c
Continue
)
}
// Continue can be passed to custom fuzzing functions to allow them to use
// the correct source of randomness and to continue fuzzing their members.
type
Continue
struct
{
...
...
@@ -260,7 +318,20 @@ func (c Continue) Fuzz(obj interface{}) {
panic
(
"needed ptr!"
)
}
v
=
v
.
Elem
()
c
.
f
.
doFuzz
(
v
)
c
.
f
.
doFuzz
(
v
,
0
)
}
// 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
(
c
Continue
)
FuzzNoCustom
(
obj
interface
{})
{
v
:=
reflect
.
ValueOf
(
obj
)
if
v
.
Kind
()
!=
reflect
.
Ptr
{
panic
(
"needed ptr!"
)
}
v
=
v
.
Elem
()
c
.
f
.
doFuzz
(
v
,
flagNoCustomFuzz
)
}
// RandString makes a random string up to 20 characters long. The returned string
...
...
@@ -288,6 +359,15 @@ func fuzzUint(v reflect.Value, r *rand.Rand) {
v
.
SetUint
(
randUint64
(
r
))
}
func
fuzzTime
(
t
*
time
.
Time
,
c
Continue
)
{
var
sec
,
nsec
int64
// Allow for about 1000 years of random time values, which keeps things
// like JSON parsing reasonably happy.
sec
=
c
.
Rand
.
Int63n
(
1000
*
365
*
24
*
60
*
60
)
c
.
Fuzz
(
&
nsec
)
*
t
=
time
.
Unix
(
sec
,
nsec
)
}
var
fillFuncMap
=
map
[
reflect
.
Kind
]
func
(
reflect
.
Value
,
*
rand
.
Rand
){
reflect
.
Bool
:
func
(
v
reflect
.
Value
,
r
*
rand
.
Rand
)
{
v
.
SetBool
(
randBool
(
r
))
...
...
Godeps/_workspace/src/github.com/google/gofuzz/fuzz_test.go
View file @
cee14ab5
...
...
@@ -19,6 +19,7 @@ package fuzz
import
(
"reflect"
"testing"
"time"
)
func
TestFuzz_basic
(
t
*
testing
.
T
)
{
...
...
@@ -36,6 +37,7 @@ func TestFuzz_basic(t *testing.T) {
Uptr
uintptr
S
string
B
bool
T
time
.
Time
}{}
failed
:=
map
[
string
]
int
{}
...
...
@@ -81,6 +83,9 @@ func TestFuzz_basic(t *testing.T) {
if
n
,
v
:=
"b"
,
obj
.
B
;
v
==
false
{
failed
[
n
]
=
failed
[
n
]
+
1
}
if
n
,
v
:=
"t"
,
obj
.
T
;
v
.
IsZero
()
{
failed
[
n
]
=
failed
[
n
]
+
1
}
}
checkFailed
(
t
,
failed
)
}
...
...
@@ -256,3 +261,124 @@ func TestFuzz_custom(t *testing.T) {
return
7
,
true
})
}
type
SelfFuzzer
string
// Implement fuzz.Interface.
func
(
sf
*
SelfFuzzer
)
Fuzz
(
c
Continue
)
{
*
sf
=
selfFuzzerTestPhrase
}
const
selfFuzzerTestPhrase
=
"was fuzzed"
func
TestFuzz_interface
(
t
*
testing
.
T
)
{
f
:=
New
()
var
obj1
SelfFuzzer
tryFuzz
(
t
,
f
,
&
obj1
,
func
()
(
int
,
bool
)
{
if
obj1
!=
selfFuzzerTestPhrase
{
return
1
,
false
}
return
1
,
true
})
var
obj2
map
[
int
]
SelfFuzzer
tryFuzz
(
t
,
f
,
&
obj2
,
func
()
(
int
,
bool
)
{
for
_
,
v
:=
range
obj2
{
if
v
!=
selfFuzzerTestPhrase
{
return
1
,
false
}
}
return
1
,
true
})
}
func
TestFuzz_interfaceAndFunc
(
t
*
testing
.
T
)
{
const
privateTestPhrase
=
"private phrase"
f
:=
New
()
.
Funcs
(
// This should take precedence over SelfFuzzer.Fuzz().
func
(
s
*
SelfFuzzer
,
c
Continue
)
{
*
s
=
privateTestPhrase
},
)
var
obj1
SelfFuzzer
tryFuzz
(
t
,
f
,
&
obj1
,
func
()
(
int
,
bool
)
{
if
obj1
!=
privateTestPhrase
{
return
1
,
false
}
return
1
,
true
})
var
obj2
map
[
int
]
SelfFuzzer
tryFuzz
(
t
,
f
,
&
obj2
,
func
()
(
int
,
bool
)
{
for
_
,
v
:=
range
obj2
{
if
v
!=
privateTestPhrase
{
return
1
,
false
}
}
return
1
,
true
})
}
func
TestFuzz_noCustom
(
t
*
testing
.
T
)
{
type
Inner
struct
{
Str
string
}
type
Outer
struct
{
Str
string
In
Inner
}
testPhrase
:=
"gotcalled"
f
:=
New
()
.
Funcs
(
func
(
outer
*
Outer
,
c
Continue
)
{
outer
.
Str
=
testPhrase
c
.
Fuzz
(
&
outer
.
In
)
},
func
(
inner
*
Inner
,
c
Continue
)
{
inner
.
Str
=
testPhrase
},
)
c
:=
Continue
{
f
:
f
,
Rand
:
f
.
r
}
// Fuzzer.Fuzz()
obj1
:=
Outer
{}
f
.
Fuzz
(
&
obj1
)
if
obj1
.
Str
!=
testPhrase
{
t
.
Errorf
(
"expected Outer custom function to have been called"
)
}
if
obj1
.
In
.
Str
!=
testPhrase
{
t
.
Errorf
(
"expected Inner custom function to have been called"
)
}
// Continue.Fuzz()
obj2
:=
Outer
{}
c
.
Fuzz
(
&
obj2
)
if
obj2
.
Str
!=
testPhrase
{
t
.
Errorf
(
"expected Outer custom function to have been called"
)
}
if
obj2
.
In
.
Str
!=
testPhrase
{
t
.
Errorf
(
"expected Inner custom function to have been called"
)
}
// Fuzzer.FuzzNoCustom()
obj3
:=
Outer
{}
f
.
FuzzNoCustom
(
&
obj3
)
if
obj3
.
Str
==
testPhrase
{
t
.
Errorf
(
"expected Outer custom function to not have been called"
)
}
if
obj3
.
In
.
Str
!=
testPhrase
{
t
.
Errorf
(
"expected Inner custom function to have been called"
)
}
// Continue.FuzzNoCustom()
obj4
:=
Outer
{}
c
.
FuzzNoCustom
(
&
obj4
)
if
obj4
.
Str
==
testPhrase
{
t
.
Errorf
(
"expected Outer custom function to not have been called"
)
}
if
obj4
.
In
.
Str
!=
testPhrase
{
t
.
Errorf
(
"expected Inner custom function to have been called"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment