From 58733a9f403b63efa8ee71f8758f9a76892b76db Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Sat, 29 Jan 2022 02:01:58 -0600 Subject: [PATCH] go 1.18 fuzz tests: initial testing (#10063) * Add basic fuzz test * Add a few tests for HTR utils * gofmt * Add requirement for fuzz gotag * Run gaz and fix tags * Update pubsub_fuzz_test.go I'll fix this later Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/p2p/pubsub_fuzz_test.go | 24 ++++++++++++ encoding/ssz/htrutils_fuzz_test.go | 55 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 beacon-chain/p2p/pubsub_fuzz_test.go create mode 100644 encoding/ssz/htrutils_fuzz_test.go diff --git a/beacon-chain/p2p/pubsub_fuzz_test.go b/beacon-chain/p2p/pubsub_fuzz_test.go new file mode 100644 index 0000000000..17ad473362 --- /dev/null +++ b/beacon-chain/p2p/pubsub_fuzz_test.go @@ -0,0 +1,24 @@ +//go:build fuzz && go1.18 && disabled +// +build fuzz,go1.18,disabled + +package p2p_test + +import ( + "fmt" + "testing" + + "github.com/prysmaticlabs/prysm/beacon-chain/p2p" + "github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder" +) + +func FuzzMsgID(f *testing.F) { + validTopic := fmt.Sprintf(p2p.BlockSubnetTopicFormat, []byte{0xb5, 0x30, 0x3f, 0x2a}) + "/" + encoder.ProtocolSuffixSSZSnappy + f.Add(validTopic) + + f.Fuzz(func(t *testing.T, topic string) { + _, err := p2p.ExtractGossipDigest(topic) + if err != nil { + t.Fatal(err) + } + }) +} diff --git a/encoding/ssz/htrutils_fuzz_test.go b/encoding/ssz/htrutils_fuzz_test.go new file mode 100644 index 0000000000..7577807fec --- /dev/null +++ b/encoding/ssz/htrutils_fuzz_test.go @@ -0,0 +1,55 @@ +//go:build fuzz && go1.18 +// +build fuzz,go1.18 + +package ssz_test + +import ( + "testing" + + fssz "github.com/ferranbt/fastssz" + "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/config/params" + "github.com/prysmaticlabs/prysm/encoding/ssz" + pb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" +) + +func FuzzUint64Root(f *testing.F) { + f.Fuzz(func(t *testing.T, i uint64) { + _ = ssz.Uint64Root(i) + }) +} + +func FuzzForkRoot(f *testing.F) { + frk := &pb.Fork{ + PreviousVersion: params.BeaconConfig().GenesisForkVersion, + CurrentVersion: params.BeaconConfig().AltairForkVersion, + Epoch: 100, + } + example, err := frk.MarshalSSZ() + if err != nil { + f.Fatal(err) + } + f.Add(example) + + f.Fuzz(func(t *testing.T, b []byte) { + frk := &pb.Fork{} + if err := frk.UnmarshalSSZ(b); err != nil { + if errors.Is(err, fssz.ErrSize) { + return + } + t.Fatal(err) + } + + if _, err := ssz.ForkRoot(frk); err != nil { + t.Fatal(err) + } + }) +} + +func FuzzPackChunks(f *testing.F) { + f.Fuzz(func(t *testing.T, b []byte) { + if _, err := ssz.PackChunks(b); err != nil { + t.Fatal(err) + } + }) +}