Skip to content

Commit

Permalink
stop using math/rand.Seed, convert fuzz functions to actual fuzzers
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 29, 2023
1 parent c16b930 commit 7aaf789
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 43 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/multiformats/go-multihash v0.0.14
github.com/multiformats/go-varint v0.0.6
github.com/stretchr/testify v1.7.0
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
58 changes: 15 additions & 43 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package multiaddr
import (
"bytes"
"encoding/hex"
"math/rand"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)

func newMultiaddr(t *testing.T, a string) Multiaddr {
Expand Down Expand Up @@ -499,56 +495,32 @@ func TestGetValue(t *testing.T) {
assertValueForProto(t, a, P_UNIX, "/a/b/c/d")
}

func TestFuzzBytes(t *testing.T) {
rand.Seed(time.Now().UnixNano())
// Bump up these numbers if you want to stress this
buf := make([]byte, 256)
for i := 0; i < 2000; i++ {
l := rand.Intn(len(buf))
rand.Read(buf[:l])

// just checking that it doesnt panic
ma, err := NewMultiaddrBytes(buf[:l])
func FuzzNewMultiaddrBytes(f *testing.F) {
f.Fuzz(func(_ *testing.T, b []byte) {
// just checking that it doesn't panic
ma, err := NewMultiaddrBytes(b)
if err == nil {
// for any valid multiaddrs, make sure these calls don't panic
_ = ma.String()
ma.Protocols()
}
}
})
}

func randMaddrString() string {
good_corpus := []string{"tcp", "ip", "udp", "ipfs", "0.0.0.0", "127.0.0.1", "12345", "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"}

size := rand.Intn(256)
parts := make([]string, 0, size)
for i := 0; i < size; i++ {
switch rand.Intn(5) {
case 0, 1, 2:
parts = append(parts, good_corpus[rand.Intn(len(good_corpus))])
default:
badbuf := make([]byte, rand.Intn(256))
rand.Read(badbuf)
parts = append(parts, string(badbuf))
}
}

return "/" + strings.Join(parts, "/")
}

func TestFuzzString(t *testing.T) {
rand.Seed(time.Now().UnixNano())
// Bump up these numbers if you want to stress this
for i := 0; i < 2000; i++ {

// just checking that it doesnt panic
ma, err := NewMultiaddr(randMaddrString())
func FuzzNewMultiaddrString(f *testing.F) {
f.Add("/ip4/0.0.0.0/udp/12345/utp")
f.Add("/ip4/1.2.3.4/udp/12345/quic")
f.Add("/ip4/127.0.0.1/tcp/12345")
f.Add("/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g")
f.Fuzz(func(_ *testing.T, s string) {
// just checking that it doesn't panic
ma, err := NewMultiaddr(s)
if err == nil {
// for any valid multiaddrs, make sure these calls don't panic
_ = ma.String()
ma.Protocols()
}
}
})
}

func TestBinaryRepresentation(t *testing.T) {
Expand Down

0 comments on commit 7aaf789

Please sign in to comment.