types_test.go 16 KB

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