mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
tests, common/bitutil: move bitutil fuzzer to native package
This commit is contained in:
parent
ae52202831
commit
2f8cdf2042
4 changed files with 28 additions and 26 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ package bitutil
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
|
|
@ -48,17 +49,21 @@ func TestEncodingCycle(t *testing.T) {
|
|||
"0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
|
||||
}
|
||||
for i, tt := range tests {
|
||||
data := hexutil.MustDecode(tt)
|
||||
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 {
|
||||
t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
|
||||
continue
|
||||
return fmt.Errorf("failed to decompress compressed data: %v", err)
|
||||
}
|
||||
if !bytes.Equal(data, proc) {
|
||||
t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data)
|
||||
}
|
||||
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.
|
||||
|
|
|
|||
10
oss-fuzz.sh
10
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue