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