hexutil_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2016 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 hexutil
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. )
  22. type marshalTest struct {
  23. input interface{}
  24. want string
  25. }
  26. type unmarshalTest struct {
  27. input string
  28. want interface{}
  29. wantErr error // if set, decoding must fail on any platform
  30. wantErr32bit error // if set, decoding must fail on 32bit platforms (used for Uint tests)
  31. }
  32. var (
  33. encodeBytesTests = []marshalTest{
  34. {[]byte{}, "0x"},
  35. {[]byte{0}, "0x00"},
  36. {[]byte{0, 0, 1, 2}, "0x00000102"},
  37. }
  38. encodeBigTests = []marshalTest{
  39. {referenceBig("0"), "0x0"},
  40. {referenceBig("1"), "0x1"},
  41. {referenceBig("ff"), "0xff"},
  42. {referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
  43. {referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
  44. {referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"},
  45. }
  46. encodeUint64Tests = []marshalTest{
  47. {uint64(0), "0x0"},
  48. {uint64(1), "0x1"},
  49. {uint64(0xff), "0xff"},
  50. {uint64(0x1122334455667788), "0x1122334455667788"},
  51. }
  52. encodeUintTests = []marshalTest{
  53. {uint(0), "0x0"},
  54. {uint(1), "0x1"},
  55. {uint(0xff), "0xff"},
  56. {uint(0x11223344), "0x11223344"},
  57. }
  58. decodeBytesTests = []unmarshalTest{
  59. // invalid
  60. {input: ``, wantErr: ErrEmptyString},
  61. {input: `0`, wantErr: ErrMissingPrefix},
  62. {input: `0x0`, wantErr: ErrOddLength},
  63. {input: `0x023`, wantErr: ErrOddLength},
  64. {input: `0xxx`, wantErr: ErrSyntax},
  65. {input: `0x01zz01`, wantErr: ErrSyntax},
  66. // valid
  67. {input: `0x`, want: []byte{}},
  68. {input: `0X`, want: []byte{}},
  69. {input: `0x02`, want: []byte{0x02}},
  70. {input: `0X02`, want: []byte{0x02}},
  71. {input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
  72. {
  73. input: `0xffffffffffffffffffffffffffffffffffff`,
  74. want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  75. },
  76. }
  77. decodeBigTests = []unmarshalTest{
  78. // invalid
  79. {input: `0`, wantErr: ErrMissingPrefix},
  80. {input: `0x`, wantErr: ErrEmptyNumber},
  81. {input: `0x01`, wantErr: ErrLeadingZero},
  82. {input: `0xx`, wantErr: ErrSyntax},
  83. {input: `0x1zz01`, wantErr: ErrSyntax},
  84. {
  85. input: `0x10000000000000000000000000000000000000000000000000000000000000000`,
  86. wantErr: ErrBig256Range,
  87. },
  88. // valid
  89. {input: `0x0`, want: big.NewInt(0)},
  90. {input: `0x2`, want: big.NewInt(0x2)},
  91. {input: `0x2F2`, want: big.NewInt(0x2f2)},
  92. {input: `0X2F2`, want: big.NewInt(0x2f2)},
  93. {input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
  94. {input: `0xbBb`, want: big.NewInt(0xbbb)},
  95. {input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
  96. {
  97. input: `0x112233445566778899aabbccddeeff`,
  98. want: referenceBig("112233445566778899aabbccddeeff"),
  99. },
  100. {
  101. input: `0xffffffffffffffffffffffffffffffffffff`,
  102. want: referenceBig("ffffffffffffffffffffffffffffffffffff"),
  103. },
  104. {
  105. input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`,
  106. want: referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
  107. },
  108. }
  109. decodeUint64Tests = []unmarshalTest{
  110. // invalid
  111. {input: `0`, wantErr: ErrMissingPrefix},
  112. {input: `0x`, wantErr: ErrEmptyNumber},
  113. {input: `0x01`, wantErr: ErrLeadingZero},
  114. {input: `0xfffffffffffffffff`, wantErr: ErrUint64Range},
  115. {input: `0xx`, wantErr: ErrSyntax},
  116. {input: `0x1zz01`, wantErr: ErrSyntax},
  117. // valid
  118. {input: `0x0`, want: uint64(0)},
  119. {input: `0x2`, want: uint64(0x2)},
  120. {input: `0x2F2`, want: uint64(0x2f2)},
  121. {input: `0X2F2`, want: uint64(0x2f2)},
  122. {input: `0x1122aaff`, want: uint64(0x1122aaff)},
  123. {input: `0xbbb`, want: uint64(0xbbb)},
  124. {input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
  125. }
  126. )
  127. func TestEncode(t *testing.T) {
  128. for _, test := range encodeBytesTests {
  129. enc := Encode(test.input.([]byte))
  130. if enc != test.want {
  131. t.Errorf("input %x: wrong encoding %s", test.input, enc)
  132. }
  133. }
  134. }
  135. func TestDecode(t *testing.T) {
  136. for _, test := range decodeBytesTests {
  137. dec, err := Decode(test.input)
  138. if !checkError(t, test.input, err, test.wantErr) {
  139. continue
  140. }
  141. if !bytes.Equal(test.want.([]byte), dec) {
  142. t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
  143. continue
  144. }
  145. }
  146. }
  147. func TestEncodeBig(t *testing.T) {
  148. for _, test := range encodeBigTests {
  149. enc := EncodeBig(test.input.(*big.Int))
  150. if enc != test.want {
  151. t.Errorf("input %x: wrong encoding %s", test.input, enc)
  152. }
  153. }
  154. }
  155. func TestDecodeBig(t *testing.T) {
  156. for _, test := range decodeBigTests {
  157. dec, err := DecodeBig(test.input)
  158. if !checkError(t, test.input, err, test.wantErr) {
  159. continue
  160. }
  161. if dec.Cmp(test.want.(*big.Int)) != 0 {
  162. t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
  163. continue
  164. }
  165. }
  166. }
  167. func TestEncodeUint64(t *testing.T) {
  168. for _, test := range encodeUint64Tests {
  169. enc := EncodeUint64(test.input.(uint64))
  170. if enc != test.want {
  171. t.Errorf("input %x: wrong encoding %s", test.input, enc)
  172. }
  173. }
  174. }
  175. func TestDecodeUint64(t *testing.T) {
  176. for _, test := range decodeUint64Tests {
  177. dec, err := DecodeUint64(test.input)
  178. if !checkError(t, test.input, err, test.wantErr) {
  179. continue
  180. }
  181. if dec != test.want.(uint64) {
  182. t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
  183. continue
  184. }
  185. }
  186. }