rlpdump_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "fmt"
  20. "strings"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/hexutil"
  24. )
  25. func TestRoundtrip(t *testing.T) {
  26. for i, want := range []string{
  27. "0xf880806482520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0a1010000000000000000000000000000000000000000000000000000000000000001801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28",
  28. "0xd5c0d3cb84746573742a2a808213378667617a6f6e6b",
  29. "0xc780c0c1c0825208",
  30. } {
  31. var out strings.Builder
  32. err := rlpToText(bytes.NewReader(common.FromHex(want)), &out)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. text := out.String()
  37. rlpBytes, err := textToRlp(strings.NewReader(text))
  38. if err != nil {
  39. t.Errorf("test %d: error %v", i, err)
  40. continue
  41. }
  42. have := fmt.Sprintf("%#x", rlpBytes)
  43. if have != want {
  44. t.Errorf("test %d: have\n%v\nwant:\n%v\n", i, have, want)
  45. }
  46. }
  47. }
  48. func TestTextToRlp(t *testing.T) {
  49. type tc struct {
  50. text string
  51. want string
  52. }
  53. cases := []tc{
  54. {
  55. text: `[
  56. "",
  57. [],
  58. [
  59. [],
  60. ],
  61. 5208,
  62. ]`,
  63. want: "0xc780c0c1c0825208",
  64. },
  65. }
  66. for i, tc := range cases {
  67. have, err := textToRlp(strings.NewReader(tc.text))
  68. if err != nil {
  69. t.Errorf("test %d: error %v", i, err)
  70. continue
  71. }
  72. if hexutil.Encode(have) != tc.want {
  73. t.Errorf("test %d:\nhave %v\nwant %v", i, hexutil.Encode(have), tc.want)
  74. }
  75. }
  76. }