github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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/ethereum/go-ethereum/core/types" 24 "github.com/ethereum/go-ethereum/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 44 var i int 45 { 46 rlp.Split(input) 47 } 48 { 49 if elems, _, err := rlp.SplitList(input); err == nil { 50 rlp.CountValues(elems) 51 } 52 } 53 54 { 55 rlp.NewStream(bytes.NewReader(input), 0).Decode(new(interface{})) 56 } 57 58 { 59 decodeEncode(input, new(interface{}), i) 60 i++ 61 } 62 { 63 var v struct { 64 Int uint 65 String string 66 Bytes []byte 67 } 68 decodeEncode(input, &v, i) 69 i++ 70 } 71 72 { 73 type Types struct { 74 Bool bool 75 Raw rlp.RawValue 76 Slice []*Types 77 Iface []interface{} 78 } 79 var v Types 80 decodeEncode(input, &v, i) 81 i++ 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 decodeEncode(input, &v, i) 96 i++ 97 } 98 { 99 decodeEncode(input, [10]byte{}, i) 100 i++ 101 } 102 { 103 var v struct { 104 Byte [10]byte 105 Rool [10]bool 106 } 107 decodeEncode(input, &v, i) 108 i++ 109 } 110 { 111 var h types.Header 112 decodeEncode(input, &h, i) 113 i++ 114 var b types.Block 115 decodeEncode(input, &b, i) 116 i++ 117 var t types.Transaction 118 decodeEncode(input, &t, i) 119 i++ 120 var txs types.Transactions 121 decodeEncode(input, &txs, i) 122 i++ 123 var rs types.Receipts 124 decodeEncode(input, &rs, i) 125 } 126 return 1 127 }