rlp_test_util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2015 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 tests
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "errors"
  21. "fmt"
  22. "math/big"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. // RLPTest is the JSON structure of a single RLP test.
  27. type RLPTest struct {
  28. // If the value of In is "INVALID" or "VALID", the test
  29. // checks whether Out can be decoded into a value of
  30. // type interface{}.
  31. //
  32. // For other JSON values, In is treated as a driver for
  33. // calls to rlp.Stream. The test also verifies that encoding
  34. // In produces the bytes in Out.
  35. In interface{}
  36. // Out is a hex-encoded RLP value.
  37. Out string
  38. }
  39. // FromHex returns the bytes represented by the hexadecimal string s.
  40. // s may be prefixed with "0x".
  41. // This is copy-pasted from bytes.go, which does not return the error
  42. func FromHex(s string) ([]byte, error) {
  43. if len(s) > 1 && (s[0:2] == "0x" || s[0:2] == "0X") {
  44. s = s[2:]
  45. }
  46. if len(s)%2 == 1 {
  47. s = "0" + s
  48. }
  49. return hex.DecodeString(s)
  50. }
  51. // Run executes the test.
  52. func (t *RLPTest) Run() error {
  53. outb, err := FromHex(t.Out)
  54. if err != nil {
  55. return fmt.Errorf("invalid hex in Out")
  56. }
  57. // Handle simple decoding tests with no actual In value.
  58. if t.In == "VALID" || t.In == "INVALID" {
  59. return checkDecodeInterface(outb, t.In == "VALID")
  60. }
  61. // Check whether encoding the value produces the same bytes.
  62. in := translateJSON(t.In)
  63. b, err := rlp.EncodeToBytes(in)
  64. if err != nil {
  65. return fmt.Errorf("encode failed: %v", err)
  66. }
  67. if !bytes.Equal(b, outb) {
  68. return fmt.Errorf("encode produced %x, want %x", b, outb)
  69. }
  70. // Test stream decoding.
  71. s := rlp.NewStream(bytes.NewReader(outb), 0)
  72. return checkDecodeFromJSON(s, in)
  73. }
  74. func checkDecodeInterface(b []byte, isValid bool) error {
  75. err := rlp.DecodeBytes(b, new(interface{}))
  76. switch {
  77. case isValid && err != nil:
  78. return fmt.Errorf("decoding failed: %v", err)
  79. case !isValid && err == nil:
  80. return fmt.Errorf("decoding of invalid value succeeded")
  81. }
  82. return nil
  83. }
  84. // translateJSON makes test json values encodable with RLP.
  85. func translateJSON(v interface{}) interface{} {
  86. switch v := v.(type) {
  87. case float64:
  88. return uint64(v)
  89. case string:
  90. if len(v) > 0 && v[0] == '#' { // # starts a faux big int.
  91. big, ok := new(big.Int).SetString(v[1:], 10)
  92. if !ok {
  93. panic(fmt.Errorf("bad test: bad big int: %q", v))
  94. }
  95. return big
  96. }
  97. return []byte(v)
  98. case []interface{}:
  99. new := make([]interface{}, len(v))
  100. for i := range v {
  101. new[i] = translateJSON(v[i])
  102. }
  103. return new
  104. default:
  105. panic(fmt.Errorf("can't handle %T", v))
  106. }
  107. }
  108. // checkDecodeFromJSON decodes from s guided by exp. exp drives the
  109. // Stream by invoking decoding operations (Uint, Big, List, ...) based
  110. // on the type of each value. The value decoded from the RLP stream
  111. // must match the JSON value.
  112. func checkDecodeFromJSON(s *rlp.Stream, exp interface{}) error {
  113. switch exp := exp.(type) {
  114. case uint64:
  115. i, err := s.Uint()
  116. if err != nil {
  117. return addStack("Uint", exp, err)
  118. }
  119. if i != exp {
  120. return addStack("Uint", exp, fmt.Errorf("result mismatch: got %d", i))
  121. }
  122. case *big.Int:
  123. big := new(big.Int)
  124. if err := s.Decode(&big); err != nil {
  125. return addStack("Big", exp, err)
  126. }
  127. if big.Cmp(exp) != 0 {
  128. return addStack("Big", exp, fmt.Errorf("result mismatch: got %d", big))
  129. }
  130. case []byte:
  131. b, err := s.Bytes()
  132. if err != nil {
  133. return addStack("Bytes", exp, err)
  134. }
  135. if !bytes.Equal(b, exp) {
  136. return addStack("Bytes", exp, fmt.Errorf("result mismatch: got %x", b))
  137. }
  138. case []interface{}:
  139. if _, err := s.List(); err != nil {
  140. return addStack("List", exp, err)
  141. }
  142. for i, v := range exp {
  143. if err := checkDecodeFromJSON(s, v); err != nil {
  144. return addStack(fmt.Sprintf("[%d]", i), exp, err)
  145. }
  146. }
  147. if err := s.ListEnd(); err != nil {
  148. return addStack("ListEnd", exp, err)
  149. }
  150. default:
  151. panic(fmt.Errorf("unhandled type: %T", exp))
  152. }
  153. return nil
  154. }
  155. func addStack(op string, val interface{}, err error) error {
  156. lines := strings.Split(err.Error(), "\n")
  157. lines = append(lines, fmt.Sprintf("\t%s: %v", op, val))
  158. return errors.New(strings.Join(lines, "\n"))
  159. }