github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/tests/fuzzers/rlp/rlp_fuzzer.go (about)

     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 rlp
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  
    23  	"github.com/ethw3/go-ethereuma/core/types"
    24  	"github.com/ethw3/go-ethereuma/rlp"
    25  )
    26  
    27  func decodeEncode(input []byte, val interface{}, i int) {
    28  	if err := rlp.DecodeBytes(input, val); err == nil {
    29  		output, err := rlp.EncodeToBytes(val)
    30  		if err != nil {
    31  			panic(err)
    32  		}
    33  		if !bytes.Equal(input, output) {
    34  			panic(fmt.Sprintf("case %d: encode-decode is not equal, \ninput : %x\noutput: %x", i, input, output))
    35  		}
    36  	}
    37  }
    38  
    39  func Fuzz(input []byte) int {
    40  	if len(input) == 0 {
    41  		return 0
    42  	}
    43  	if len(input) > 500*1024 {
    44  		return 0
    45  	}
    46  
    47  	var i int
    48  	{
    49  		rlp.Split(input)
    50  	}
    51  	{
    52  		if elems, _, err := rlp.SplitList(input); err == nil {
    53  			rlp.CountValues(elems)
    54  		}
    55  	}
    56  
    57  	{
    58  		rlp.NewStream(bytes.NewReader(input), 0).Decode(new(interface{}))
    59  	}
    60  
    61  	{
    62  		decodeEncode(input, new(interface{}), i)
    63  		i++
    64  	}
    65  	{
    66  		var v struct {
    67  			Int    uint
    68  			String string
    69  			Bytes  []byte
    70  		}
    71  		decodeEncode(input, &v, i)
    72  		i++
    73  	}
    74  
    75  	{
    76  		type Types struct {
    77  			Bool  bool
    78  			Raw   rlp.RawValue
    79  			Slice []*Types
    80  			Iface []interface{}
    81  		}
    82  		var v Types
    83  		decodeEncode(input, &v, i)
    84  		i++
    85  	}
    86  	{
    87  		type AllTypes struct {
    88  			Int    uint
    89  			String string
    90  			Bytes  []byte
    91  			Bool   bool
    92  			Raw    rlp.RawValue
    93  			Slice  []*AllTypes
    94  			Array  [3]*AllTypes
    95  			Iface  []interface{}
    96  		}
    97  		var v AllTypes
    98  		decodeEncode(input, &v, i)
    99  		i++
   100  	}
   101  	{
   102  		decodeEncode(input, [10]byte{}, i)
   103  		i++
   104  	}
   105  	{
   106  		var v struct {
   107  			Byte [10]byte
   108  			Rool [10]bool
   109  		}
   110  		decodeEncode(input, &v, i)
   111  		i++
   112  	}
   113  	{
   114  		var h types.Header
   115  		decodeEncode(input, &h, i)
   116  		i++
   117  		var b types.Block
   118  		decodeEncode(input, &b, i)
   119  		i++
   120  		var t types.Transaction
   121  		decodeEncode(input, &t, i)
   122  		i++
   123  		var txs types.Transactions
   124  		decodeEncode(input, &txs, i)
   125  		i++
   126  		var rs types.Receipts
   127  		decodeEncode(input, &rs, i)
   128  	}
   129  	return 1
   130  }