rlp_test_util.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // Run executes the test.
  40. func (t *RLPTest) Run() error {
  41. outb, err := hex.DecodeString(t.Out)
  42. if err != nil {
  43. return fmt.Errorf("invalid hex in Out")
  44. }
  45. // Handle simple decoding tests with no actual In value.
  46. if t.In == "VALID" || t.In == "INVALID" {
  47. return checkDecodeInterface(outb, t.In == "VALID")
  48. }
  49. // Check whether encoding the value produces the same bytes.
  50. in := translateJSON(t.In)
  51. b, err := rlp.EncodeToBytes(in)
  52. if err != nil {
  53. return fmt.Errorf("encode failed: %v", err)
  54. }
  55. if !bytes.Equal(b, outb) {
  56. return fmt.Errorf("encode produced %x, want %x", b, outb)
  57. }
  58. // Test stream decoding.
  59. s := rlp.NewStream(bytes.NewReader(outb), 0)
  60. return checkDecodeFromJSON(s, in)
  61. }
  62. func checkDecodeInterface(b []byte, isValid bool) error {
  63. err := rlp.DecodeBytes(b, new(interface{}))
  64. switch {
  65. case isValid && err != nil:
  66. return fmt.Errorf("decoding failed: %v", err)
  67. case !isValid && err == nil:
  68. return fmt.Errorf("decoding of invalid value succeeded")
  69. }
  70. return nil
  71. }
  72. // translateJSON makes test json values encodable with RLP.
  73. func translateJSON(v interface{}) interface{} {
  74. switch v := v.(type) {
  75. case float64:
  76. return uint64(v)
  77. case string:
  78. if len(v) > 0 && v[0] == '#' { // # starts a faux big int.
  79. big, ok := new(big.Int).SetString(v[1:], 10)
  80. if !ok {
  81. panic(fmt.Errorf("bad test: bad big int: %q", v))
  82. }
  83. return big
  84. }
  85. return []byte(v)
  86. case []interface{}:
  87. new := make([]interface{}, len(v))
  88. for i := range v {
  89. new[i] = translateJSON(v[i])
  90. }
  91. return new
  92. default:
  93. panic(fmt.Errorf("can't handle %T", v))
  94. }
  95. }
  96. // checkDecodeFromJSON decodes from s guided by exp. exp drives the
  97. // Stream by invoking decoding operations (Uint, Big, List, ...) based
  98. // on the type of each value. The value decoded from the RLP stream
  99. // must match the JSON value.
  100. func checkDecodeFromJSON(s *rlp.Stream, exp interface{}) error {
  101. switch exp := exp.(type) {
  102. case uint64:
  103. i, err := s.Uint()
  104. if err != nil {
  105. return addStack("Uint", exp, err)
  106. }
  107. if i != exp {
  108. return addStack("Uint", exp, fmt.Errorf("result mismatch: got %d", i))
  109. }
  110. case *big.Int:
  111. big := new(big.Int)
  112. if err := s.Decode(&big); err != nil {
  113. return addStack("Big", exp, err)
  114. }
  115. if big.Cmp(exp) != 0 {
  116. return addStack("Big", exp, fmt.Errorf("result mismatch: got %d", big))
  117. }
  118. case []byte:
  119. b, err := s.Bytes()
  120. if err != nil {
  121. return addStack("Bytes", exp, err)
  122. }
  123. if !bytes.Equal(b, exp) {
  124. return addStack("Bytes", exp, fmt.Errorf("result mismatch: got %x", b))
  125. }
  126. case []interface{}:
  127. if _, err := s.List(); err != nil {
  128. return addStack("List", exp, err)
  129. }
  130. for i, v := range exp {
  131. if err := checkDecodeFromJSON(s, v); err != nil {
  132. return addStack(fmt.Sprintf("[%d]", i), exp, err)
  133. }
  134. }
  135. if err := s.ListEnd(); err != nil {
  136. return addStack("ListEnd", exp, err)
  137. }
  138. default:
  139. panic(fmt.Errorf("unhandled type: %T", exp))
  140. }
  141. return nil
  142. }
  143. func addStack(op string, val interface{}, err error) error {
  144. lines := strings.Split(err.Error(), "\n")
  145. lines = append(lines, fmt.Sprintf("\t%s: %v", op, val))
  146. return errors.New(strings.Join(lines, "\n"))
  147. }