1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// +build gofuzz
package network // import "collectd.org/network"
import (
"bytes"
"fmt"
)
type fuzzLookup struct {
user, password string
}
func (fl fuzzLookup) Password(user string) (string, error) {
if fl.user == user {
return fl.password, nil
}
return "", fmt.Errorf("no such user: %q", user)
}
// Fuzz is used by the https://github.com/dvyukov/go-fuzz framework
// It's method signature must match the prescribed format and it is expected to panic upon failure
// Usage:
// $ go-fuzz-build collectd.org/network
// $ mkdir -p /tmp/fuzzwork/corpus
// $ cp network/testdata/packet1.bin /tmp/fuzzwork/corpus
// $ go-fuzz -bin=./network-fuzz.zip -workdir=/tmp/fuzzwork
func Fuzz(data []byte) int {
// deserialize
d1, err := Parse(data, ParseOpts{
PasswordLookup: fuzzLookup{
user: "test",
password: "test",
},
})
if err != nil || len(d1) == 0 {
return 0
}
// serialize
s1 := NewBuffer(0)
if err := s1.Write(d1[0]); err != nil {
panic(err)
}
// deserialize
d2, err := Parse(s1.buffer.Bytes(), ParseOpts{})
if err != nil {
return 0
}
if len(d2) == 0 {
panic("d2 is empty but no err was returned")
}
// serialize
s2 := NewBuffer(0)
if err := s2.Write(d2[0]); err != nil {
panic(err)
}
if bytes.Compare(s1.buffer.Bytes(), s2.buffer.Bytes()) != 0 {
panic(fmt.Sprintf("Comparison of two serialized versions failed s1 [%v] s2[%v]", s1.buffer.Bytes(), s2.buffer.Bytes()))
}
return 1
}
|