Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/fuzzers: move fuzzers into native packages #28467

Merged
merged 16 commits into from
Nov 14, 2023
Merged
Prev Previous commit
Next Next commit
tests, common/bitutil: move bitutil fuzzer to native package
  • Loading branch information
holiman committed Nov 2, 2023
commit ca16dd7a75b52d471d378a81e9f4566187095fdf
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
23 changes: 14 additions & 9 deletions common/bitutil/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package bitutil

import (
"bytes"
"fmt"
"math/rand"
"testing"

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions oss-fuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down