types_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 rpc
  17. import (
  18. "encoding/json"
  19. "reflect"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. )
  24. func TestBlockNumberJSONUnmarshal(t *testing.T) {
  25. tests := []struct {
  26. input string
  27. mustFail bool
  28. expected BlockNumber
  29. }{
  30. 0: {`"0x"`, true, BlockNumber(0)},
  31. 1: {`"0x0"`, false, BlockNumber(0)},
  32. 2: {`"0X1"`, false, BlockNumber(1)},
  33. 3: {`"0x00"`, true, BlockNumber(0)},
  34. 4: {`"0x01"`, true, BlockNumber(0)},
  35. 5: {`"0x1"`, false, BlockNumber(1)},
  36. 6: {`"0x12"`, false, BlockNumber(18)},
  37. 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)},
  38. 8: {`"0x8000000000000000"`, true, BlockNumber(0)},
  39. 9: {"0", true, BlockNumber(0)},
  40. 10: {`"ff"`, true, BlockNumber(0)},
  41. 11: {`"pending"`, false, PendingBlockNumber},
  42. 12: {`"latest"`, false, LatestBlockNumber},
  43. 13: {`"earliest"`, false, EarliestBlockNumber},
  44. 14: {`someString`, true, BlockNumber(0)},
  45. 15: {`""`, true, BlockNumber(0)},
  46. 16: {``, true, BlockNumber(0)},
  47. }
  48. for i, test := range tests {
  49. var num BlockNumber
  50. err := json.Unmarshal([]byte(test.input), &num)
  51. if test.mustFail && err == nil {
  52. t.Errorf("Test %d should fail", i)
  53. continue
  54. }
  55. if !test.mustFail && err != nil {
  56. t.Errorf("Test %d should pass but got err: %v", i, err)
  57. continue
  58. }
  59. if num != test.expected {
  60. t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num)
  61. }
  62. }
  63. }
  64. func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
  65. tests := []struct {
  66. input string
  67. mustFail bool
  68. expected BlockNumberOrHash
  69. }{
  70. 0: {`"0x"`, true, BlockNumberOrHash{}},
  71. 1: {`"0x0"`, false, BlockNumberOrHashWithNumber(0)},
  72. 2: {`"0X1"`, false, BlockNumberOrHashWithNumber(1)},
  73. 3: {`"0x00"`, true, BlockNumberOrHash{}},
  74. 4: {`"0x01"`, true, BlockNumberOrHash{}},
  75. 5: {`"0x1"`, false, BlockNumberOrHashWithNumber(1)},
  76. 6: {`"0x12"`, false, BlockNumberOrHashWithNumber(18)},
  77. 7: {`"0x7fffffffffffffff"`, false, BlockNumberOrHashWithNumber(math.MaxInt64)},
  78. 8: {`"0x8000000000000000"`, true, BlockNumberOrHash{}},
  79. 9: {"0", true, BlockNumberOrHash{}},
  80. 10: {`"ff"`, true, BlockNumberOrHash{}},
  81. 11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
  82. 12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
  83. 13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
  84. 14: {`someString`, true, BlockNumberOrHash{}},
  85. 15: {`""`, true, BlockNumberOrHash{}},
  86. 16: {``, true, BlockNumberOrHash{}},
  87. 17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
  88. 18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
  89. 19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
  90. 20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
  91. 21: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
  92. 22: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
  93. 23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
  94. 24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
  95. 25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}},
  96. }
  97. for i, test := range tests {
  98. var bnh BlockNumberOrHash
  99. err := json.Unmarshal([]byte(test.input), &bnh)
  100. if test.mustFail && err == nil {
  101. t.Errorf("Test %d should fail", i)
  102. continue
  103. }
  104. if !test.mustFail && err != nil {
  105. t.Errorf("Test %d should pass but got err: %v", i, err)
  106. continue
  107. }
  108. hash, hashOk := bnh.Hash()
  109. expectedHash, expectedHashOk := test.expected.Hash()
  110. num, numOk := bnh.Number()
  111. expectedNum, expectedNumOk := test.expected.Number()
  112. if bnh.RequireCanonical != test.expected.RequireCanonical ||
  113. hash != expectedHash || hashOk != expectedHashOk ||
  114. num != expectedNum || numOk != expectedNumOk {
  115. t.Errorf("Test %d got unexpected value, want %v, got %v", i, test.expected, bnh)
  116. }
  117. }
  118. }
  119. func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
  120. tests := []struct {
  121. name string
  122. number int64
  123. }{
  124. {"max", math.MaxInt64},
  125. {"pending", int64(PendingBlockNumber)},
  126. {"latest", int64(LatestBlockNumber)},
  127. {"earliest", int64(EarliestBlockNumber)},
  128. }
  129. for _, test := range tests {
  130. test := test
  131. t.Run(test.name, func(t *testing.T) {
  132. bnh := BlockNumberOrHashWithNumber(BlockNumber(test.number))
  133. marshalled, err := json.Marshal(bnh)
  134. if err != nil {
  135. t.Fatal("cannot marshal:", err)
  136. }
  137. var unmarshalled BlockNumberOrHash
  138. err = json.Unmarshal(marshalled, &unmarshalled)
  139. if err != nil {
  140. t.Fatal("cannot unmarshal:", err)
  141. }
  142. if !reflect.DeepEqual(bnh, unmarshalled) {
  143. t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
  144. }
  145. })
  146. }
  147. }