Skip to content

Commit 99ecc4a

Browse files
committed
tests/fuzzers: move fuzzers into native packages (ethereum#28467)
1 parent eaf9777 commit 99ecc4a

File tree

8 files changed

+275
-247
lines changed

8 files changed

+275
-247
lines changed

common/bitutil/compress_test.go

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package bitutil
1818

1919
import (
2020
"bytes"
21+
"fmt"
2122
"math/rand"
2223
"testing"
2324

@@ -48,19 +49,23 @@ func TestEncodingCycle(t *testing.T) {
4849
"0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
4950
}
5051
for i, tt := range tests {
51-
data := hexutil.MustDecode(tt)
52-
53-
proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
54-
if err != nil {
55-
t.Errorf("test %d: failed to decompress compressed data: %v", i, err)
56-
continue
57-
}
58-
if !bytes.Equal(data, proc) {
59-
t.Errorf("test %d: compress/decompress mismatch: have %x, want %x", i, proc, data)
52+
if err := testEncodingCycle(hexutil.MustDecode(tt)); err != nil {
53+
t.Errorf("test %d: %v", i, err)
6054
}
6155
}
6256
}
6357

58+
func testEncodingCycle(data []byte) error {
59+
proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
60+
if err != nil {
61+
return fmt.Errorf("failed to decompress compressed data: %v", err)
62+
}
63+
if !bytes.Equal(data, proc) {
64+
return fmt.Errorf("compress/decompress mismatch: have %x, want %x", proc, data)
65+
}
66+
return nil
67+
}
68+
6469
// Tests that data bitset decoding and rencoding works and is bijective.
6570
func TestDecodingCycle(t *testing.T) {
6671
tests := []struct {
@@ -179,3 +184,41 @@ func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
179184
bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
180185
}
181186
}
187+
188+
func FuzzEncoder(f *testing.F) {
189+
f.Fuzz(func(t *testing.T, data []byte) {
190+
if err := testEncodingCycle(data); err != nil {
191+
t.Fatal(err)
192+
}
193+
})
194+
}
195+
196+
func FuzzDecoder(f *testing.F) {
197+
f.Fuzz(func(t *testing.T, data []byte) {
198+
fuzzDecode(data)
199+
})
200+
}
201+
202+
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
203+
// reencoding algorithm.
204+
func fuzzDecode(data []byte) {
205+
blob, err := DecompressBytes(data, 1024)
206+
if err != nil {
207+
return
208+
}
209+
// re-compress it (it's OK if the re-compressed differs from the
210+
// original - the first input may not have been compressed at all)
211+
comp := CompressBytes(blob)
212+
if len(comp) > len(blob) {
213+
// After compression, it must be smaller or equal
214+
panic("bad compression")
215+
}
216+
// But decompressing it once again should work
217+
decomp, err := DecompressBytes(data, 1024)
218+
if err != nil {
219+
panic(err)
220+
}
221+
if !bytes.Equal(decomp, blob) {
222+
panic("content mismatch")
223+
}
224+
}

core/types/rlp_fuzzer_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package types
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"math/big"
23+
"testing"
24+
25+
"github.com/XinFinOrg/XDPoSChain/rlp"
26+
"github.com/holiman/uint256"
27+
)
28+
29+
func decodeEncode(input []byte, val interface{}) error {
30+
if err := rlp.DecodeBytes(input, val); err != nil {
31+
// not valid rlp, nothing to do
32+
return nil
33+
}
34+
// If it _were_ valid rlp, we can encode it again
35+
output, err := rlp.EncodeToBytes(val)
36+
if err != nil {
37+
return err
38+
}
39+
if !bytes.Equal(input, output) {
40+
return fmt.Errorf("encode-decode is not equal, \ninput : %x\noutput: %x", input, output)
41+
}
42+
return nil
43+
}
44+
45+
func FuzzRLP(f *testing.F) {
46+
f.Fuzz(fuzzRlp)
47+
}
48+
49+
func fuzzRlp(t *testing.T, input []byte) {
50+
if len(input) == 0 || len(input) > 500*1024 {
51+
return
52+
}
53+
rlp.Split(input)
54+
if elems, _, err := rlp.SplitList(input); err == nil {
55+
rlp.CountValues(elems)
56+
}
57+
rlp.NewStream(bytes.NewReader(input), 0).Decode(new(interface{}))
58+
if err := decodeEncode(input, new(interface{})); err != nil {
59+
t.Fatal(err)
60+
}
61+
{
62+
var v struct {
63+
Int uint
64+
String string
65+
Bytes []byte
66+
}
67+
if err := decodeEncode(input, &v); err != nil {
68+
t.Fatal(err)
69+
}
70+
}
71+
{
72+
type Types struct {
73+
Bool bool
74+
Raw rlp.RawValue
75+
Slice []*Types
76+
Iface []interface{}
77+
}
78+
var v Types
79+
if err := decodeEncode(input, &v); err != nil {
80+
t.Fatal(err)
81+
}
82+
}
83+
{
84+
type AllTypes struct {
85+
Int uint
86+
String string
87+
Bytes []byte
88+
Bool bool
89+
Raw rlp.RawValue
90+
Slice []*AllTypes
91+
Array [3]*AllTypes
92+
Iface []interface{}
93+
}
94+
var v AllTypes
95+
if err := decodeEncode(input, &v); err != nil {
96+
t.Fatal(err)
97+
}
98+
}
99+
{
100+
if err := decodeEncode(input, [10]byte{}); err != nil {
101+
t.Fatal(err)
102+
}
103+
}
104+
{
105+
var v struct {
106+
Byte [10]byte
107+
Rool [10]bool
108+
}
109+
if err := decodeEncode(input, &v); err != nil {
110+
t.Fatal(err)
111+
}
112+
}
113+
{
114+
var h Header
115+
if err := decodeEncode(input, &h); err != nil {
116+
t.Fatal(err)
117+
}
118+
var b Block
119+
if err := decodeEncode(input, &b); err != nil {
120+
t.Fatal(err)
121+
}
122+
var tx Transaction
123+
if err := decodeEncode(input, &tx); err != nil {
124+
t.Fatal(err)
125+
}
126+
var txs Transactions
127+
if err := decodeEncode(input, &txs); err != nil {
128+
t.Fatal(err)
129+
}
130+
var rs Receipts
131+
if err := decodeEncode(input, &rs); err != nil {
132+
t.Fatal(err)
133+
}
134+
}
135+
{
136+
var v struct {
137+
AnIntPtr *big.Int
138+
AnInt big.Int
139+
AnU256Ptr *uint256.Int
140+
AnU256 uint256.Int
141+
NotAnU256 [4]uint64
142+
}
143+
if err := decodeEncode(input, &v); err != nil {
144+
t.Fatal(err)
145+
}
146+
}
147+
}

tests/fuzzers/rlp/rlp_test.go renamed to core/vm/contracts_fuzz_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,31 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package rlp
17+
package vm
1818

19-
import "testing"
19+
import (
20+
"testing"
2021

21-
func Fuzz(f *testing.F) {
22-
f.Fuzz(func(t *testing.T, data []byte) {
23-
fuzz(data)
22+
"github.com/XinFinOrg/XDPoSChain/common"
23+
)
24+
25+
func FuzzPrecompiledContracts(f *testing.F) {
26+
// Create list of addresses
27+
var addrs []common.Address
28+
for k := range allPrecompiles {
29+
addrs = append(addrs, k)
30+
}
31+
f.Fuzz(func(t *testing.T, addr uint8, input []byte) {
32+
a := addrs[int(addr)%len(addrs)]
33+
p := allPrecompiles[a]
34+
gas := p.RequiredGas(input)
35+
if gas > 10_000_000 {
36+
return
37+
}
38+
inWant := string(input)
39+
RunPrecompiledContract(nil, p, input, gas)
40+
if inHave := string(input); inWant != inHave {
41+
t.Errorf("Precompiled %v modified input data", a)
42+
}
2443
})
2544
}

tests/fuzzers/runtime/runtime_test.go renamed to core/vm/runtime/runtime_fuzz_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ package runtime
1818

1919
import (
2020
"testing"
21-
22-
"github.com/XinFinOrg/XDPoSChain/core/vm/runtime"
2321
)
2422

25-
func Fuzz(f *testing.F) {
23+
func FuzzVmRuntime(f *testing.F) {
2624
f.Fuzz(func(t *testing.T, code, input []byte) {
27-
runtime.Execute(code, input, &runtime.Config{
25+
Execute(code, input, &Config{
2826
GasLimit: 12000000,
2927
})
3028
})

tests/fuzzers/bitutil/compress_test.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)