types_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 common
  17. import (
  18. "encoding/json"
  19. "math/big"
  20. "strings"
  21. "testing"
  22. )
  23. func TestBytesConversion(t *testing.T) {
  24. bytes := []byte{5}
  25. hash := BytesToHash(bytes)
  26. var exp Hash
  27. exp[31] = 5
  28. if hash != exp {
  29. t.Errorf("expected %x got %x", exp, hash)
  30. }
  31. }
  32. func TestIsHexAddress(t *testing.T) {
  33. tests := []struct {
  34. str string
  35. exp bool
  36. }{
  37. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
  38. {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
  39. {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
  40. {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
  41. {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
  42. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
  43. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
  44. {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
  45. {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
  46. }
  47. for _, test := range tests {
  48. if result := IsHexAddress(test.str); result != test.exp {
  49. t.Errorf("IsHexAddress(%s) == %v; expected %v",
  50. test.str, result, test.exp)
  51. }
  52. }
  53. }
  54. func TestHashJsonValidation(t *testing.T) {
  55. var tests = []struct {
  56. Prefix string
  57. Size int
  58. Error string
  59. }{
  60. {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"},
  61. {"0x", 66, "hex string has length 66, want 64 for common.Hash"},
  62. {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"},
  63. {"0x", 0, "hex string has length 0, want 64 for common.Hash"},
  64. {"0x", 64, ""},
  65. {"0X", 64, ""},
  66. }
  67. for _, test := range tests {
  68. input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"`
  69. var v Hash
  70. err := json.Unmarshal([]byte(input), &v)
  71. if err == nil {
  72. if test.Error != "" {
  73. t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error)
  74. }
  75. } else {
  76. if err.Error() != test.Error {
  77. t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error)
  78. }
  79. }
  80. }
  81. }
  82. func TestAddressUnmarshalJSON(t *testing.T) {
  83. var tests = []struct {
  84. Input string
  85. ShouldErr bool
  86. Output *big.Int
  87. }{
  88. {"", true, nil},
  89. {`""`, true, nil},
  90. {`"0x"`, true, nil},
  91. {`"0x00"`, true, nil},
  92. {`"0xG000000000000000000000000000000000000000"`, true, nil},
  93. {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
  94. {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
  95. }
  96. for i, test := range tests {
  97. var v Address
  98. err := json.Unmarshal([]byte(test.Input), &v)
  99. if err != nil && !test.ShouldErr {
  100. t.Errorf("test #%d: unexpected error: %v", i, err)
  101. }
  102. if err == nil {
  103. if test.ShouldErr {
  104. t.Errorf("test #%d: expected error, got none", i)
  105. }
  106. if v.Big().Cmp(test.Output) != 0 {
  107. t.Errorf("test #%d: address mismatch: have %v, want %v", i, v.Big(), test.Output)
  108. }
  109. }
  110. }
  111. }
  112. func TestAddressHexChecksum(t *testing.T) {
  113. var tests = []struct {
  114. Input string
  115. Output string
  116. }{
  117. // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification
  118. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
  119. {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
  120. {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
  121. {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
  122. // Ensure that non-standard length input values are handled correctly
  123. {"0xa", "0x000000000000000000000000000000000000000A"},
  124. {"0x0a", "0x000000000000000000000000000000000000000A"},
  125. {"0x00a", "0x000000000000000000000000000000000000000A"},
  126. {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
  127. }
  128. for i, test := range tests {
  129. output := HexToAddress(test.Input).Hex()
  130. if output != test.Output {
  131. t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output)
  132. }
  133. }
  134. }
  135. func BenchmarkAddressHex(b *testing.B) {
  136. testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
  137. for n := 0; n < b.N; n++ {
  138. testAddr.Hex()
  139. }
  140. }
  141. func TestMixedcaseAccount_Address(t *testing.T) {
  142. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
  143. // Note: 0X{checksum_addr} is not valid according to spec above
  144. var res []struct {
  145. A MixedcaseAddress
  146. Valid bool
  147. }
  148. if err := json.Unmarshal([]byte(`[
  149. {"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
  150. {"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true},
  151. {"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
  152. {"A" : "0x1111111111111111111112222222222223333323", "Valid": true}
  153. ]`), &res); err != nil {
  154. t.Fatal(err)
  155. }
  156. for _, r := range res {
  157. if got := r.A.ValidChecksum(); got != r.Valid {
  158. t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String())
  159. }
  160. }
  161. //These should throw exceptions:
  162. var r2 []MixedcaseAddress
  163. for _, r := range []string{
  164. `["0x11111111111111111111122222222222233333"]`, // Too short
  165. `["0x111111111111111111111222222222222333332"]`, // Too short
  166. `["0x11111111111111111111122222222222233333234"]`, // Too long
  167. `["0x111111111111111111111222222222222333332344"]`, // Too long
  168. `["1111111111111111111112222222222223333323"]`, // Missing 0x
  169. `["x1111111111111111111112222222222223333323"]`, // Missing 0
  170. `["0xG111111111111111111112222222222223333323"]`, //Non-hex
  171. } {
  172. if err := json.Unmarshal([]byte(r), &r2); err == nil {
  173. t.Errorf("Expected failure, input %v", r)
  174. }
  175. }
  176. }