pack_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright 2017 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 abi
  17. import (
  18. "bytes"
  19. "math"
  20. "math/big"
  21. "reflect"
  22. "strings"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/common"
  25. )
  26. func TestPack(t *testing.T) {
  27. for i, test := range []struct {
  28. typ string
  29. input interface{}
  30. output []byte
  31. }{
  32. {
  33. "uint8",
  34. uint8(2),
  35. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  36. },
  37. {
  38. "uint8[]",
  39. []uint8{1, 2},
  40. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  41. },
  42. {
  43. "uint16",
  44. uint16(2),
  45. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  46. },
  47. {
  48. "uint16[]",
  49. []uint16{1, 2},
  50. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  51. },
  52. {
  53. "uint32",
  54. uint32(2),
  55. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  56. },
  57. {
  58. "uint32[]",
  59. []uint32{1, 2},
  60. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  61. },
  62. {
  63. "uint64",
  64. uint64(2),
  65. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  66. },
  67. {
  68. "uint64[]",
  69. []uint64{1, 2},
  70. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  71. },
  72. {
  73. "uint256",
  74. big.NewInt(2),
  75. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  76. },
  77. {
  78. "uint256[]",
  79. []*big.Int{big.NewInt(1), big.NewInt(2)},
  80. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  81. },
  82. {
  83. "int8",
  84. int8(2),
  85. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  86. },
  87. {
  88. "int8[]",
  89. []int8{1, 2},
  90. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  91. },
  92. {
  93. "int16",
  94. int16(2),
  95. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  96. },
  97. {
  98. "int16[]",
  99. []int16{1, 2},
  100. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  101. },
  102. {
  103. "int32",
  104. int32(2),
  105. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  106. },
  107. {
  108. "int32[]",
  109. []int32{1, 2},
  110. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  111. },
  112. {
  113. "int64",
  114. int64(2),
  115. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  116. },
  117. {
  118. "int64[]",
  119. []int64{1, 2},
  120. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  121. },
  122. {
  123. "int256",
  124. big.NewInt(2),
  125. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
  126. },
  127. {
  128. "int256[]",
  129. []*big.Int{big.NewInt(1), big.NewInt(2)},
  130. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
  131. },
  132. {
  133. "bytes1",
  134. [1]byte{1},
  135. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  136. },
  137. {
  138. "bytes2",
  139. [2]byte{1},
  140. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  141. },
  142. {
  143. "bytes3",
  144. [3]byte{1},
  145. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  146. },
  147. {
  148. "bytes4",
  149. [4]byte{1},
  150. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  151. },
  152. {
  153. "bytes5",
  154. [5]byte{1},
  155. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  156. },
  157. {
  158. "bytes6",
  159. [6]byte{1},
  160. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  161. },
  162. {
  163. "bytes7",
  164. [7]byte{1},
  165. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  166. },
  167. {
  168. "bytes8",
  169. [8]byte{1},
  170. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  171. },
  172. {
  173. "bytes9",
  174. [9]byte{1},
  175. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  176. },
  177. {
  178. "bytes10",
  179. [10]byte{1},
  180. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  181. },
  182. {
  183. "bytes11",
  184. [11]byte{1},
  185. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  186. },
  187. {
  188. "bytes12",
  189. [12]byte{1},
  190. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  191. },
  192. {
  193. "bytes13",
  194. [13]byte{1},
  195. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  196. },
  197. {
  198. "bytes14",
  199. [14]byte{1},
  200. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  201. },
  202. {
  203. "bytes15",
  204. [15]byte{1},
  205. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  206. },
  207. {
  208. "bytes16",
  209. [16]byte{1},
  210. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  211. },
  212. {
  213. "bytes17",
  214. [17]byte{1},
  215. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  216. },
  217. {
  218. "bytes18",
  219. [18]byte{1},
  220. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  221. },
  222. {
  223. "bytes19",
  224. [19]byte{1},
  225. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  226. },
  227. {
  228. "bytes20",
  229. [20]byte{1},
  230. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  231. },
  232. {
  233. "bytes21",
  234. [21]byte{1},
  235. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  236. },
  237. {
  238. "bytes22",
  239. [22]byte{1},
  240. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  241. },
  242. {
  243. "bytes23",
  244. [23]byte{1},
  245. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  246. },
  247. {
  248. "bytes24",
  249. [24]byte{1},
  250. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  251. },
  252. {
  253. "bytes24",
  254. [24]byte{1},
  255. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  256. },
  257. {
  258. "bytes25",
  259. [25]byte{1},
  260. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  261. },
  262. {
  263. "bytes26",
  264. [26]byte{1},
  265. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  266. },
  267. {
  268. "bytes27",
  269. [27]byte{1},
  270. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  271. },
  272. {
  273. "bytes28",
  274. [28]byte{1},
  275. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  276. },
  277. {
  278. "bytes29",
  279. [29]byte{1},
  280. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  281. },
  282. {
  283. "bytes30",
  284. [30]byte{1},
  285. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  286. },
  287. {
  288. "bytes31",
  289. [31]byte{1},
  290. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  291. },
  292. {
  293. "bytes32",
  294. [32]byte{1},
  295. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  296. },
  297. {
  298. "uint32[2][3][4]",
  299. [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}},
  300. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"),
  301. },
  302. {
  303. "address[]",
  304. []common.Address{{1}, {2}},
  305. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"),
  306. },
  307. {
  308. "bytes32[]",
  309. []common.Hash{{1}, {2}},
  310. common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"),
  311. },
  312. {
  313. "function",
  314. [24]byte{1},
  315. common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  316. },
  317. {
  318. "string",
  319. "foobar",
  320. common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"),
  321. },
  322. } {
  323. typ, err := NewType(test.typ)
  324. if err != nil {
  325. t.Fatalf("%v failed. Unexpected parse error: %v", i, err)
  326. }
  327. output, err := typ.pack(reflect.ValueOf(test.input))
  328. if err != nil {
  329. t.Fatalf("%v failed. Unexpected pack error: %v", i, err)
  330. }
  331. if !bytes.Equal(output, test.output) {
  332. t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output)
  333. }
  334. }
  335. }
  336. func TestMethodPack(t *testing.T) {
  337. abi, err := JSON(strings.NewReader(jsondata2))
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. sig := abi.Methods["slice"].Id()
  342. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  343. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  344. packed, err := abi.Pack("slice", []uint32{1, 2})
  345. if err != nil {
  346. t.Error(err)
  347. }
  348. if !bytes.Equal(packed, sig) {
  349. t.Errorf("expected %x got %x", sig, packed)
  350. }
  351. var addrA, addrB = common.Address{1}, common.Address{2}
  352. sig = abi.Methods["sliceAddress"].Id()
  353. sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
  354. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  355. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  356. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  357. packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. if !bytes.Equal(packed, sig) {
  362. t.Errorf("expected %x got %x", sig, packed)
  363. }
  364. var addrC, addrD = common.Address{3}, common.Address{4}
  365. sig = abi.Methods["sliceMultiAddress"].Id()
  366. sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
  367. sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
  368. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  369. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  370. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  371. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  372. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  373. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  374. packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. if !bytes.Equal(packed, sig) {
  379. t.Errorf("expected %x got %x", sig, packed)
  380. }
  381. sig = abi.Methods["slice256"].Id()
  382. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  383. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  384. packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
  385. if err != nil {
  386. t.Error(err)
  387. }
  388. if !bytes.Equal(packed, sig) {
  389. t.Errorf("expected %x got %x", sig, packed)
  390. }
  391. }
  392. func TestPackNumber(t *testing.T) {
  393. tests := []struct {
  394. value reflect.Value
  395. packed []byte
  396. }{
  397. // Protocol limits
  398. {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
  399. {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
  400. {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
  401. // Type corner cases
  402. {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
  403. {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
  404. {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
  405. {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
  406. {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
  407. {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
  408. {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
  409. {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
  410. {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
  411. {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
  412. {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
  413. {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
  414. }
  415. for i, tt := range tests {
  416. packed := packNum(tt.value)
  417. if !bytes.Equal(packed, tt.packed) {
  418. t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
  419. }
  420. }
  421. }