From 3f344aee55350cc51904673a1f18f658269b89f8 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Mon, 16 Dec 2019 00:52:20 -0600 Subject: [PATCH] add a few fuzz tests (#4291) --- beacon-chain/core/blocks/BUILD.bazel | 2 + beacon-chain/core/blocks/block_operations.go | 4 ++ .../core/blocks/block_operations_fuzz_test.go | 37 +++++++++++++++++++ beacon-chain/core/helpers/attestation.go | 6 --- 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 beacon-chain/core/blocks/block_operations_fuzz_test.go diff --git a/beacon-chain/core/blocks/BUILD.bazel b/beacon-chain/core/blocks/BUILD.bazel index 45166b769..af4e970cb 100644 --- a/beacon-chain/core/blocks/BUILD.bazel +++ b/beacon-chain/core/blocks/BUILD.bazel @@ -37,6 +37,7 @@ go_test( name = "go_default_test", size = "medium", srcs = [ + "block_operations_fuzz_test.go", "block_operations_test.go", "block_test.go", "eth1_data_test.go", @@ -51,6 +52,7 @@ go_test( "//shared/testutil:go_default_library", "//shared/trieutil:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", + "@com_github_google_gofuzz//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", diff --git a/beacon-chain/core/blocks/block_operations.go b/beacon-chain/core/blocks/block_operations.go index bf8abfb76..6dc710474 100644 --- a/beacon-chain/core/blocks/block_operations.go +++ b/beacon-chain/core/blocks/block_operations.go @@ -548,6 +548,10 @@ func ProcessAttestationNoVerify(ctx context.Context, beaconState *pb.BeaconState ctx, span := trace.StartSpan(ctx, "core.ProcessAttestationNoVerify") defer span.End() + if att == nil || att.Data == nil || att.Data.Target == nil { + return nil, errors.New("nil attestation data target") + } + data := att.Data if data.Target.Epoch != helpers.PrevEpoch(beaconState) && data.Target.Epoch != helpers.CurrentEpoch(beaconState) { return nil, fmt.Errorf( diff --git a/beacon-chain/core/blocks/block_operations_fuzz_test.go b/beacon-chain/core/blocks/block_operations_fuzz_test.go new file mode 100644 index 000000000..eab8e8ad6 --- /dev/null +++ b/beacon-chain/core/blocks/block_operations_fuzz_test.go @@ -0,0 +1,37 @@ +package blocks_test + +import ( + "context" + "testing" + + fuzz "github.com/google/gofuzz" + eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" + ethereum_beacon_p2p_v1 "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" +) + +func TestFuzzProcessAttestation_10000(t *testing.T) { + fuzzer := fuzz.NewWithSeed(0) + ctx := context.Background() + state := ðereum_beacon_p2p_v1.BeaconState{} + att := ð.Attestation{} + + for i := 0; i < 10000; i++ { + fuzzer.Fuzz(state) + fuzzer.Fuzz(att) + _, _ = blocks.ProcessAttestationNoVerify(ctx, state, att) + } +} + +func TestFuzzProcessBlockHeader_10000(t *testing.T) { + fuzzer := fuzz.NewWithSeed(0) + state := ðereum_beacon_p2p_v1.BeaconState{} + block := ð.BeaconBlock{} + + for i := 0; i < 10000; i++ { + fuzzer.Fuzz(state) + fuzzer.Fuzz(block) + _, _ = blocks.ProcessBlockHeader(state, block) + } +} + diff --git a/beacon-chain/core/helpers/attestation.go b/beacon-chain/core/helpers/attestation.go index da1999216..e0df4d249 100644 --- a/beacon-chain/core/helpers/attestation.go +++ b/beacon-chain/core/helpers/attestation.go @@ -14,12 +14,6 @@ import ( ) var ( - // ErrAttestationDataSlotNilState is returned when a nil state argument - // is provided to AttestationDataSlot. - ErrAttestationDataSlotNilState = errors.New("nil state provided for AttestationDataSlot") - // ErrAttestationDataSlotNilData is returned when a nil attestation data - // argument is provided to AttestationDataSlot. - ErrAttestationDataSlotNilData = errors.New("nil data provided for AttestationDataSlot") // ErrAttestationAggregationBitsOverlap is returned when two attestations aggregation // bits overlap with each other. ErrAttestationAggregationBitsOverlap = errors.New("overlapping aggregation bits")