From 2f8cdf2042446978330f6a7bd58db7d6842992c7 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 24 Oct 2023 10:56:30 +0200 Subject: [PATCH] tests, common/bitutil: move bitutil fuzzer to native package --- .../bitutil/codec_fuzz_test.go | 21 +++++------------ common/bitutil/compress_test.go | 23 +++++++++++-------- .../vm/runtime/runtime_fuzz_test.go | 0 oss-fuzz.sh | 10 ++++++-- 4 files changed, 28 insertions(+), 26 deletions(-) rename tests/fuzzers/bitutil/compress_test.go => common/bitutil/codec_fuzz_test.go (76%) rename tests/fuzzers/runtime/runtime_test.go => core/vm/runtime/runtime_fuzz_test.go (100%) diff --git a/tests/fuzzers/bitutil/compress_test.go b/common/bitutil/codec_fuzz_test.go similarity index 76% rename from tests/fuzzers/bitutil/compress_test.go rename to common/bitutil/codec_fuzz_test.go index ed9d27eb30..aa6b3e97c2 100644 --- a/tests/fuzzers/bitutil/compress_test.go +++ b/common/bitutil/codec_fuzz_test.go @@ -19,13 +19,13 @@ package bitutil import ( "bytes" "testing" - - "github.com/ethereum/go-ethereum/common/bitutil" ) func FuzzEncoder(f *testing.F) { f.Fuzz(func(t *testing.T, data []byte) { - fuzzEncode(data) + if err := testEncodingCycle(data); err != nil { + t.Fatal(err) + } }) } func FuzzDecoder(f *testing.F) { @@ -34,31 +34,22 @@ func FuzzDecoder(f *testing.F) { }) } -// fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and -// decoding algorithm. -func fuzzEncode(data []byte) { - proc, _ := bitutil.DecompressBytes(bitutil.CompressBytes(data), len(data)) - if !bytes.Equal(data, proc) { - panic("content mismatch") - } -} - // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and // reencoding algorithm. func fuzzDecode(data []byte) { - blob, err := bitutil.DecompressBytes(data, 1024) + blob, err := DecompressBytes(data, 1024) if err != nil { return } // re-compress it (it's OK if the re-compressed differs from the // original - the first input may not have been compressed at all) - comp := bitutil.CompressBytes(blob) + comp := CompressBytes(blob) if len(comp) > len(blob) { // After compression, it must be smaller or equal panic("bad compression") } // But decompressing it once again should work - decomp, err := bitutil.DecompressBytes(data, 1024) + decomp, err := DecompressBytes(data, 1024) if err != nil { panic(err) } diff --git a/common/bitutil/compress_test.go b/common/bitutil/compress_test.go index 13a13011dc..56ce89d7b0 100644 --- a/common/bitutil/compress_test.go +++ b/common/bitutil/compress_test.go @@ -18,6 +18,7 @@ package bitutil import ( "bytes" + "fmt" "math/rand" "testing" @@ -48,19 +49,23 @@ func TestEncodingCycle(t *testing.T) { "0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe", } for i, tt := range tests { - data := hexutil.MustDecode(tt) - - proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data)) - if err != nil { - t.Errorf("test %d: failed to decompress compressed data: %v", i, err) - continue - } - if !bytes.Equal(data, proc) { - t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data) + if err := testEncodingCycle(hexutil.MustDecode(tt)); err != nil { + t.Errorf("test %d: %v", i, err) } } } +func testEncodingCycle(data []byte) error { + proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data)) + if err != nil { + return fmt.Errorf("failed to decompress compressed data: %v", err) + } + if !bytes.Equal(data, proc) { + return fmt.Errorf("compress/decompress mismatch: have %x, want %x", proc, data) + } + return nil +} + // Tests that data bitset decoding and rencoding works and is bijective. func TestDecodingCycle(t *testing.T) { tests := []struct { diff --git a/tests/fuzzers/runtime/runtime_test.go b/core/vm/runtime/runtime_fuzz_test.go similarity index 100% rename from tests/fuzzers/runtime/runtime_test.go rename to core/vm/runtime/runtime_fuzz_test.go diff --git a/oss-fuzz.sh b/oss-fuzz.sh index 5811cef137..fde3c34264 100644 --- a/oss-fuzz.sh +++ b/oss-fuzz.sh @@ -92,10 +92,16 @@ function compile_fuzzer() { cd - } +export PROJPATH=$GOPATH/src/github.com/ethereum/go-ethereum + compile_fuzzer accounts/abi FuzzABI fuzzAbi -compile_fuzzer tests/fuzzers/bitutil FuzzEncoder fuzzBitutilEncoder -compile_fuzzer tests/fuzzers/bitutil FuzzDecoder fuzzBitutilDecoder +# See https://github.com/AdamKorcz/go-118-fuzz-build#using-test-utils-from-other-_testgo-files +# If we want to use code from '.._test.go'-files, we need to remove the _test suffix. +mv $PROJPATH/common/bitutil/compress_test.go $PROJPATH/common/bitutil/compress_test_xx.go +compile_fuzzer common/bitutil FuzzEncoder fuzzBitutilEncoder +compile_fuzzer common/bitutil FuzzDecoder fuzzBitutilDecoder + compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair