rlp_fuzzer.go 2.5 KB

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