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
f749c82d95
commit
ca16dd7a75
4 changed files with 28 additions and 26 deletions
|
|
@ -19,13 +19,13 @@ package bitutil
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/bitutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func FuzzEncoder(f *testing.F) {
|
func FuzzEncoder(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
fuzzEncode(data)
|
if err := testEncodingCycle(data); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func FuzzDecoder(f *testing.F) {
|
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
|
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
|
||||||
// reencoding algorithm.
|
// reencoding algorithm.
|
||||||
func fuzzDecode(data []byte) {
|
func fuzzDecode(data []byte) {
|
||||||
blob, err := bitutil.DecompressBytes(data, 1024)
|
blob, err := DecompressBytes(data, 1024)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// re-compress it (it's OK if the re-compressed differs from the
|
// re-compress it (it's OK if the re-compressed differs from the
|
||||||
// original - the first input may not have been compressed at all)
|
// original - the first input may not have been compressed at all)
|
||||||
comp := bitutil.CompressBytes(blob)
|
comp := CompressBytes(blob)
|
||||||
if len(comp) > len(blob) {
|
if len(comp) > len(blob) {
|
||||||
// After compression, it must be smaller or equal
|
// After compression, it must be smaller or equal
|
||||||
panic("bad compression")
|
panic("bad compression")
|
||||||
}
|
}
|
||||||
// But decompressing it once again should work
|
// But decompressing it once again should work
|
||||||
decomp, err := bitutil.DecompressBytes(data, 1024)
|
decomp, err := DecompressBytes(data, 1024)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
@ -18,6 +18,7 @@ package bitutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -48,19 +49,23 @@ func TestEncodingCycle(t *testing.T) {
|
||||||
"0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
|
"0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
data := hexutil.MustDecode(tt)
|
if err := testEncodingCycle(hexutil.MustDecode(tt)); err != nil {
|
||||||
|
t.Errorf("test %d: %v", i, err)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
// Tests that data bitset decoding and rencoding works and is bijective.
|
||||||
func TestDecodingCycle(t *testing.T) {
|
func TestDecodingCycle(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
|
||||||
10
oss-fuzz.sh
10
oss-fuzz.sh
|
|
@ -92,10 +92,16 @@ function compile_fuzzer() {
|
||||||
cd -
|
cd -
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export PROJPATH=$GOPATH/src/github.com/ethereum/go-ethereum
|
||||||
|
|
||||||
compile_fuzzer accounts/abi FuzzABI fuzzAbi
|
compile_fuzzer accounts/abi FuzzABI fuzzAbi
|
||||||
|
|
||||||
compile_fuzzer tests/fuzzers/bitutil FuzzEncoder fuzzBitutilEncoder
|
# See https://github.com/AdamKorcz/go-118-fuzz-build#using-test-utils-from-other-_testgo-files
|
||||||
compile_fuzzer tests/fuzzers/bitutil FuzzDecoder fuzzBitutilDecoder
|
# 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 FuzzAdd fuzzBn256Add
|
||||||
compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul
|
compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul
|
||||||
compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair
|
compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue