types_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. "bytes"
  19. "database/sql/driver"
  20. "encoding/json"
  21. "fmt"
  22. "math/big"
  23. "reflect"
  24. "strings"
  25. "testing"
  26. )
  27. func TestBytesConversion(t *testing.T) {
  28. bytes := []byte{5}
  29. hash := BytesToHash(bytes)
  30. var exp Hash
  31. exp[31] = 5
  32. if hash != exp {
  33. t.Errorf("expected %x got %x", exp, hash)
  34. }
  35. }
  36. func TestIsHexAddress(t *testing.T) {
  37. tests := []struct {
  38. str string
  39. exp bool
  40. }{
  41. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
  42. {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
  43. {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
  44. {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
  45. {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
  46. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
  47. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
  48. {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
  49. {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
  50. }
  51. for _, test := range tests {
  52. if result := IsHexAddress(test.str); result != test.exp {
  53. t.Errorf("IsHexAddress(%s) == %v; expected %v",
  54. test.str, result, test.exp)
  55. }
  56. }
  57. }
  58. func TestHashJsonValidation(t *testing.T) {
  59. var tests = []struct {
  60. Prefix string
  61. Size int
  62. Error string
  63. }{
  64. {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"},
  65. {"0x", 66, "hex string has length 66, want 64 for common.Hash"},
  66. {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"},
  67. {"0x", 0, "hex string has length 0, want 64 for common.Hash"},
  68. {"0x", 64, ""},
  69. {"0X", 64, ""},
  70. }
  71. for _, test := range tests {
  72. input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"`
  73. var v Hash
  74. err := json.Unmarshal([]byte(input), &v)
  75. if err == nil {
  76. if test.Error != "" {
  77. t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error)
  78. }
  79. } else {
  80. if err.Error() != test.Error {
  81. t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error)
  82. }
  83. }
  84. }
  85. }
  86. func TestAddressUnmarshalJSON(t *testing.T) {
  87. var tests = []struct {
  88. Input string
  89. ShouldErr bool
  90. Output *big.Int
  91. }{
  92. {"", true, nil},
  93. {`""`, true, nil},
  94. {`"0x"`, true, nil},
  95. {`"0x00"`, true, nil},
  96. {`"0xG000000000000000000000000000000000000000"`, true, nil},
  97. {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
  98. {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
  99. }
  100. for i, test := range tests {
  101. var v Address
  102. err := json.Unmarshal([]byte(test.Input), &v)
  103. if err != nil && !test.ShouldErr {
  104. t.Errorf("test #%d: unexpected error: %v", i, err)
  105. }
  106. if err == nil {
  107. if test.ShouldErr {
  108. t.Errorf("test #%d: expected error, got none", i)
  109. }
  110. if got := new(big.Int).SetBytes(v.Bytes()); got.Cmp(test.Output) != 0 {
  111. t.Errorf("test #%d: address mismatch: have %v, want %v", i, got, test.Output)
  112. }
  113. }
  114. }
  115. }
  116. func TestAddressHexChecksum(t *testing.T) {
  117. var tests = []struct {
  118. Input string
  119. Output string
  120. }{
  121. // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification
  122. {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
  123. {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
  124. {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
  125. {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
  126. // Ensure that non-standard length input values are handled correctly
  127. {"0xa", "0x000000000000000000000000000000000000000A"},
  128. {"0x0a", "0x000000000000000000000000000000000000000A"},
  129. {"0x00a", "0x000000000000000000000000000000000000000A"},
  130. {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
  131. }
  132. for i, test := range tests {
  133. output := HexToAddress(test.Input).Hex()
  134. if output != test.Output {
  135. t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output)
  136. }
  137. }
  138. }
  139. func BenchmarkAddressHex(b *testing.B) {
  140. testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
  141. for n := 0; n < b.N; n++ {
  142. testAddr.Hex()
  143. }
  144. }
  145. func TestMixedcaseAccount_Address(t *testing.T) {
  146. // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
  147. // Note: 0X{checksum_addr} is not valid according to spec above
  148. var res []struct {
  149. A MixedcaseAddress
  150. Valid bool
  151. }
  152. if err := json.Unmarshal([]byte(`[
  153. {"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
  154. {"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true},
  155. {"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
  156. {"A" : "0x1111111111111111111112222222222223333323", "Valid": true}
  157. ]`), &res); err != nil {
  158. t.Fatal(err)
  159. }
  160. for _, r := range res {
  161. if got := r.A.ValidChecksum(); got != r.Valid {
  162. t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String())
  163. }
  164. }
  165. //These should throw exceptions:
  166. var r2 []MixedcaseAddress
  167. for _, r := range []string{
  168. `["0x11111111111111111111122222222222233333"]`, // Too short
  169. `["0x111111111111111111111222222222222333332"]`, // Too short
  170. `["0x11111111111111111111122222222222233333234"]`, // Too long
  171. `["0x111111111111111111111222222222222333332344"]`, // Too long
  172. `["1111111111111111111112222222222223333323"]`, // Missing 0x
  173. `["x1111111111111111111112222222222223333323"]`, // Missing 0
  174. `["0xG111111111111111111112222222222223333323"]`, //Non-hex
  175. } {
  176. if err := json.Unmarshal([]byte(r), &r2); err == nil {
  177. t.Errorf("Expected failure, input %v", r)
  178. }
  179. }
  180. }
  181. func TestHash_Scan(t *testing.T) {
  182. type args struct {
  183. src interface{}
  184. }
  185. tests := []struct {
  186. name string
  187. args args
  188. wantErr bool
  189. }{
  190. {
  191. name: "working scan",
  192. args: args{src: []byte{
  193. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  194. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  195. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  196. 0x10, 0x00,
  197. }},
  198. wantErr: false,
  199. },
  200. {
  201. name: "non working scan",
  202. args: args{src: int64(1234567890)},
  203. wantErr: true,
  204. },
  205. {
  206. name: "invalid length scan",
  207. args: args{src: []byte{
  208. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  209. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  210. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  211. }},
  212. wantErr: true,
  213. },
  214. }
  215. for _, tt := range tests {
  216. t.Run(tt.name, func(t *testing.T) {
  217. h := &Hash{}
  218. if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr {
  219. t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr)
  220. }
  221. if !tt.wantErr {
  222. for i := range h {
  223. if h[i] != tt.args.src.([]byte)[i] {
  224. t.Errorf(
  225. "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)",
  226. i, h[i], tt.args.src.([]byte)[i],
  227. )
  228. }
  229. }
  230. }
  231. })
  232. }
  233. }
  234. func TestHash_Value(t *testing.T) {
  235. b := []byte{
  236. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  237. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  238. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  239. 0x10, 0x00,
  240. }
  241. var usedH Hash
  242. usedH.SetBytes(b)
  243. tests := []struct {
  244. name string
  245. h Hash
  246. want driver.Value
  247. wantErr bool
  248. }{
  249. {
  250. name: "Working value",
  251. h: usedH,
  252. want: b,
  253. wantErr: false,
  254. },
  255. }
  256. for _, tt := range tests {
  257. t.Run(tt.name, func(t *testing.T) {
  258. got, err := tt.h.Value()
  259. if (err != nil) != tt.wantErr {
  260. t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr)
  261. return
  262. }
  263. if !reflect.DeepEqual(got, tt.want) {
  264. t.Errorf("Hash.Value() = %v, want %v", got, tt.want)
  265. }
  266. })
  267. }
  268. }
  269. func TestAddress_Scan(t *testing.T) {
  270. type args struct {
  271. src interface{}
  272. }
  273. tests := []struct {
  274. name string
  275. args args
  276. wantErr bool
  277. }{
  278. {
  279. name: "working scan",
  280. args: args{src: []byte{
  281. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  282. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  283. }},
  284. wantErr: false,
  285. },
  286. {
  287. name: "non working scan",
  288. args: args{src: int64(1234567890)},
  289. wantErr: true,
  290. },
  291. {
  292. name: "invalid length scan",
  293. args: args{src: []byte{
  294. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  295. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a,
  296. }},
  297. wantErr: true,
  298. },
  299. }
  300. for _, tt := range tests {
  301. t.Run(tt.name, func(t *testing.T) {
  302. a := &Address{}
  303. if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr {
  304. t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr)
  305. }
  306. if !tt.wantErr {
  307. for i := range a {
  308. if a[i] != tt.args.src.([]byte)[i] {
  309. t.Errorf(
  310. "Address.Scan() didn't scan the %d src correctly (have %X, want %X)",
  311. i, a[i], tt.args.src.([]byte)[i],
  312. )
  313. }
  314. }
  315. }
  316. })
  317. }
  318. }
  319. func TestAddress_Value(t *testing.T) {
  320. b := []byte{
  321. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  322. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  323. }
  324. var usedA Address
  325. usedA.SetBytes(b)
  326. tests := []struct {
  327. name string
  328. a Address
  329. want driver.Value
  330. wantErr bool
  331. }{
  332. {
  333. name: "Working value",
  334. a: usedA,
  335. want: b,
  336. wantErr: false,
  337. },
  338. }
  339. for _, tt := range tests {
  340. t.Run(tt.name, func(t *testing.T) {
  341. got, err := tt.a.Value()
  342. if (err != nil) != tt.wantErr {
  343. t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr)
  344. return
  345. }
  346. if !reflect.DeepEqual(got, tt.want) {
  347. t.Errorf("Address.Value() = %v, want %v", got, tt.want)
  348. }
  349. })
  350. }
  351. }
  352. func TestAddress_Format(t *testing.T) {
  353. b := []byte{
  354. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  355. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  356. }
  357. var addr Address
  358. addr.SetBytes(b)
  359. tests := []struct {
  360. name string
  361. out string
  362. want string
  363. }{
  364. {
  365. name: "println",
  366. out: fmt.Sprintln(addr),
  367. want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15\n",
  368. },
  369. {
  370. name: "print",
  371. out: fmt.Sprint(addr),
  372. want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15",
  373. },
  374. {
  375. name: "printf-s",
  376. out: func() string {
  377. buf := new(bytes.Buffer)
  378. fmt.Fprintf(buf, "%s", addr)
  379. return buf.String()
  380. }(),
  381. want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15",
  382. },
  383. {
  384. name: "printf-q",
  385. out: fmt.Sprintf("%q", addr),
  386. want: `"0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15"`,
  387. },
  388. {
  389. name: "printf-x",
  390. out: fmt.Sprintf("%x", addr),
  391. want: "b26f2b342aab24bcf63ea218c6a9274d30ab9a15",
  392. },
  393. {
  394. name: "printf-X",
  395. out: fmt.Sprintf("%X", addr),
  396. want: "B26F2B342AAB24BCF63EA218C6A9274D30AB9A15",
  397. },
  398. {
  399. name: "printf-#x",
  400. out: fmt.Sprintf("%#x", addr),
  401. want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15",
  402. },
  403. {
  404. name: "printf-v",
  405. out: fmt.Sprintf("%v", addr),
  406. want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15",
  407. },
  408. // The original default formatter for byte slice
  409. {
  410. name: "printf-d",
  411. out: fmt.Sprintf("%d", addr),
  412. want: "[178 111 43 52 42 171 36 188 246 62 162 24 198 169 39 77 48 171 154 21]",
  413. },
  414. // Invalid format char.
  415. {
  416. name: "printf-t",
  417. out: fmt.Sprintf("%t", addr),
  418. want: "%!t(address=b26f2b342aab24bcf63ea218c6a9274d30ab9a15)",
  419. },
  420. }
  421. for _, tt := range tests {
  422. t.Run(tt.name, func(t *testing.T) {
  423. if tt.out != tt.want {
  424. t.Errorf("%s does not render as expected:\n got %s\nwant %s", tt.name, tt.out, tt.want)
  425. }
  426. })
  427. }
  428. }
  429. func TestHash_Format(t *testing.T) {
  430. var hash Hash
  431. hash.SetBytes([]byte{
  432. 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
  433. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  434. 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
  435. 0x10, 0x00,
  436. })
  437. tests := []struct {
  438. name string
  439. out string
  440. want string
  441. }{
  442. {
  443. name: "println",
  444. out: fmt.Sprintln(hash),
  445. want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000\n",
  446. },
  447. {
  448. name: "print",
  449. out: fmt.Sprint(hash),
  450. want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
  451. },
  452. {
  453. name: "printf-s",
  454. out: func() string {
  455. buf := new(bytes.Buffer)
  456. fmt.Fprintf(buf, "%s", hash)
  457. return buf.String()
  458. }(),
  459. want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
  460. },
  461. {
  462. name: "printf-q",
  463. out: fmt.Sprintf("%q", hash),
  464. want: `"0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000"`,
  465. },
  466. {
  467. name: "printf-x",
  468. out: fmt.Sprintf("%x", hash),
  469. want: "b26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
  470. },
  471. {
  472. name: "printf-X",
  473. out: fmt.Sprintf("%X", hash),
  474. want: "B26F2B342AAB24BCF63EA218C6A9274D30AB9A15A218C6A9274D30AB9A151000",
  475. },
  476. {
  477. name: "printf-#x",
  478. out: fmt.Sprintf("%#x", hash),
  479. want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
  480. },
  481. {
  482. name: "printf-#X",
  483. out: fmt.Sprintf("%#X", hash),
  484. want: "0XB26F2B342AAB24BCF63EA218C6A9274D30AB9A15A218C6A9274D30AB9A151000",
  485. },
  486. {
  487. name: "printf-v",
  488. out: fmt.Sprintf("%v", hash),
  489. want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
  490. },
  491. // The original default formatter for byte slice
  492. {
  493. name: "printf-d",
  494. out: fmt.Sprintf("%d", hash),
  495. want: "[178 111 43 52 42 171 36 188 246 62 162 24 198 169 39 77 48 171 154 21 162 24 198 169 39 77 48 171 154 21 16 0]",
  496. },
  497. // Invalid format char.
  498. {
  499. name: "printf-t",
  500. out: fmt.Sprintf("%t", hash),
  501. want: "%!t(hash=b26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000)",
  502. },
  503. }
  504. for _, tt := range tests {
  505. t.Run(tt.name, func(t *testing.T) {
  506. if tt.out != tt.want {
  507. t.Errorf("%s does not render as expected:\n got %s\nwant %s", tt.name, tt.out, tt.want)
  508. }
  509. })
  510. }
  511. }